Subversion Repositories HomeAutomation

Rev

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

  1. ###########################################################
  2. #
  3. # CanStim interface, used to emulate connected can nodes
  4. #
  5. ###########################################################
  6. import logging as log
  7. from NodeIfBase import NodeIfBase
  8. from threading import Thread
  9. import time
  10.  
  11. ''' Generic dummy node '''
  12. class DummyNode():
  13.     pass
  14.  
  15. ''' Temperature sensor node '''
  16. class TempNode(DummyNode):
  17.     pass
  18.  
  19. ''' Relay node, controls switches '''
  20. class RelayNode(DummyNode):
  21.     pass
  22.  
  23. ''' View node, controls an LCD or similar '''
  24. class ViewNode(DummyNode):
  25.     pass
  26.  
  27. ''' IO Node, a bridge to some other protocol over can '''
  28. class IONode(DummyNode):
  29.     pass
  30.  
  31. class CanStimThread(Thread):
  32.     dummyList = []
  33.     numDummies = 0
  34.     terminated = False
  35.    
  36.     def __init__ (self):
  37.         Thread.__init__(self)
  38.     def run(self):
  39.         log.debug('CanStimThread.run')
  40.         while not self.terminated:
  41.             time.sleep(0.1)
  42.     def terminate(self):
  43.         self.terminated = True
  44.  
  45. class NodeIfCanStim(NodeIfBase):
  46.     DEFAULT_CONFIG = {'DummyNodes':[]}
  47.    
  48.     stimThread = None
  49.    
  50.     def __init__ (self, cfg = None, pktHandler = None):
  51.         if cfg is None:
  52.             self.config = DEFAULT_CONFIG
  53.            
  54.         NodeIfBase.__init__(self, cfg, pktHandler)
  55.        
  56.     def start(self):
  57.         stimThread = CanStimThread()
  58.         stimThread.start()
  59.        
  60.     def stop(self):
  61.         stimThread.dummyList.clear()
  62.  
  63.     def removeDummy(self, DummyNode):
  64.         ''' if possible'''
  65.         stimThread.dummyList.remove(DummyNode)
  66.         DummyNode.terminate()
  67.    
  68.     def addDummy(self, DummyNode):
  69.         '''if possible'''
  70.         stimThread.dummyList.add(DummyNode)
  71.         DummyNode.start()
  72.  
  73.