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
750 olof 88
    ownerNotifier = None
738 olof 89
    pktHandler = None
736 olof 90
 
750 olof 91
    def __init__ (self, pktHandler, cfg = None):
736 olof 92
        if cfg is None:
93
            self.config = DEFAULT_CONFIG
94
 
738 olof 95
        self.pktHandler = pktHandler
736 olof 96
        NodeIfBase.__init__(self, cfg, pktHandler)
750 olof 97
 
98
    def setPktHandler(self, pktHandler):
99
        self.pktHandler = pktHandler
736 olof 100
 
752 olof 101
    def setIfNotifier(self, ifNotifier):
750 olof 102
        self.ownerNotifier = ifNotifier
103
 
736 olof 104
    def start(self):
752 olof 105
        self.stimThread = CanStimThread(self.pktHandler, self.ownerNotifier)
738 olof 106
        tempDummy = TempNode()
107
        self.stimThread.dummyList.append(tempDummy)
108
        self.stimThread.start()
109
        return True
110
 
736 olof 111
    def stop(self):
738 olof 112
        if self.stimThread is not None and not self.stimThread.terminated:
113
            self.stimThread.terminate()
114
        else:
115
            log.debug('CanStim interface not started, or already stopped')
116
 
117
    def running(self):
118
        if self.stimThread is not None:
739 olof 119
            return not self.stimThread.terminated
738 olof 120
        else:
121
            return False
736 olof 122
 
123
    def removeDummy(self, DummyNode):
124
        ''' if possible'''
738 olof 125
        self.stimThread.dummyList.remove(DummyNode)
736 olof 126
 
127
    def addDummy(self, DummyNode):
128
        '''if possible'''
738 olof 129
        self.stimThread.dummyList.add(DummyNode)
736 olof 130