Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
736 olof 1
###########################################################
2
#
3
# CanStim interface, used to emulate connected can nodes
4
#
738 olof 5
# - Create fake node network
6
# - Replay log
7
#
736 olof 8
###########################################################
9
import logging as log
10
from NodeIfBase import NodeIfBase
11
from threading import Thread
738 olof 12
from CanPkt import CanPkt
736 olof 13
import time
14
 
15
''' Generic dummy node '''
16
class DummyNode():
738 olof 17
    def tick(self):
18
        pass
736 olof 19
 
738 olof 20
 
736 olof 21
''' Temperature sensor node '''
22
class TempNode(DummyNode):
738 olof 23
 
24
    frames = 0
25
 
26
    def __init__ (self):
27
        pass
28
 
29
    def read(self):
30
        self.frames = self.frames + 1
31
        if self.frames % 100 == 0:
32
            pktstr = CanPkt(0x1f8f0100,[0x5f,0x3a,0x40,0x01]).toString()
33
            return pktstr
736 olof 34
 
35
''' Relay node, controls switches '''
36
class RelayNode(DummyNode):
37
    pass
38
 
39
''' View node, controls an LCD or similar '''
40
class ViewNode(DummyNode):
41
    pass
42
 
43
''' IO Node, a bridge to some other protocol over can '''
44
class IONode(DummyNode):
45
    pass
46
 
47
class CanStimThread(Thread):
48
    dummyList = []
49
    numDummies = 0
50
    terminated = False
738 olof 51
    ownerNotifier = None
52
    pktHandler = None
736 olof 53
 
738 olof 54
    def __init__ (self, pktHandler, ownerNotifier):
55
        self.ownerNotifier = ownerNotifier
56
        self.pktHandler = pktHandler
736 olof 57
        Thread.__init__(self)
738 olof 58
 
59
 
736 olof 60
    def run(self):
61
        log.debug('CanStimThread.run')
738 olof 62
        try:
63
            readBuffer = None
64
            while not self.terminated:
65
                for dummy in self.dummyList:
66
                    readBuffer = dummy.read()
67
                    if readBuffer and len(readBuffer) > 0:
68
                        self.pktHandler.input(readBuffer)
69
                time.sleep(0.001)
70
 
71
            self.ownerNotifier.notify(self.ownerNotifier.TERMINATE)
72
 
73
        except: # sys.excepthook does not work in threads
74
            import traceback
75
            traceback.print_exc()
76
            self.terminate()
77
 
736 olof 78
    def terminate(self):
738 olof 79
        log.debug('CanStimThread.terminate')
736 olof 80
        self.terminated = True
738 olof 81
 
736 olof 82
 
83
class NodeIfCanStim(NodeIfBase):
738 olof 84
 
736 olof 85
    DEFAULT_CONFIG = {'DummyNodes':[]}
738 olof 86
    config = None
736 olof 87
    stimThread = None
738 olof 88
    ifNotifier = None
89
    pktHandler = None
736 olof 90
 
738 olof 91
    def __init__ (self, cfg = None, pktHandler = None, ifNotifier = None):
736 olof 92
        if cfg is None:
93
            self.config = DEFAULT_CONFIG
738 olof 94
        if ifNotifier:
95
            self.ifNotifier = ifNotifier
736 olof 96
 
738 olof 97
        self.pktHandler = pktHandler
736 olof 98
        NodeIfBase.__init__(self, cfg, pktHandler)
99
 
100
    def start(self):
738 olof 101
        self.stimThread = CanStimThread(self.pktHandler, self.ifNotifier)
102
        tempDummy = TempNode()
103
        self.stimThread.dummyList.append(tempDummy)
104
        self.stimThread.start()
105
        return True
106
 
736 olof 107
    def stop(self):
738 olof 108
        if self.stimThread is not None and not self.stimThread.terminated:
109
            self.stimThread.terminate()
110
        else:
111
            log.debug('CanStim interface not started, or already stopped')
112
 
113
    def running(self):
114
        if self.stimThread is not None:
739 olof 115
            return not self.stimThread.terminated
738 olof 116
        else:
117
            return False
736 olof 118
 
119
    def removeDummy(self, DummyNode):
120
        ''' if possible'''
738 olof 121
        self.stimThread.dummyList.remove(DummyNode)
736 olof 122
 
123
    def addDummy(self, DummyNode):
124
        '''if possible'''
738 olof 125
        self.stimThread.dummyList.add(DummyNode)
736 olof 126