svghmi/svghmi_server.py
branchsvghmi
changeset 2831 6c9cfdbe94dc
parent 2830 15d7bd79d9e8
child 2832 e9ba4dee6ffb
equal deleted inserted replaced
2830:15d7bd79d9e8 2831:6c9cfdbe94dc
    76     def sendMessage(self, msg):
    76     def sendMessage(self, msg):
    77         self.protocol_instance.sendMessage(msg, True)
    77         self.protocol_instance.sendMessage(msg, True)
    78         return 0
    78         return 0
    79 
    79 
    80 class Watchdog(object):
    80 class Watchdog(object):
    81     def __init__(self, initial_timeout, callback):
    81     def __init__(self, initial_timeout, interval, callback):
    82         self._callback = callback
    82         self._callback = callback
    83         self.lock = RLock()
    83         self.lock = RLock()
    84         self.initial_timeout = initial_timeout
    84         self.initial_timeout = initial_timeout
       
    85         self.interval = interval
    85         self.callback = callback
    86         self.callback = callback
    86         with self.lock:
    87         with self.lock:
    87             self._start()
    88             self._start()
    88 
    89 
    89     def _start(self):
    90     def _start(self, rearm=False):
    90         self.timer = Timer(self.initial_timeout, self.trigger)
    91         duration = self.interval if rearm else self.initial_timeout
    91         self.timer.start()
    92         if duration:
       
    93             self.timer = Timer(duration, self.trigger)
       
    94             self.timer.start()
    92 
    95 
    93     def _stop(self):
    96     def _stop(self):
    94         if self.timer is not None:
    97         if self.timer is not None:
    95             self.timer.cancel()
    98             self.timer.cancel()
    96             self.timer = None
    99             self.timer = None
   100             self._stop()
   103             self._stop()
   101 
   104 
   102     def feed(self):
   105     def feed(self):
   103         with self.lock:
   106         with self.lock:
   104             self._stop()
   107             self._stop()
   105             self._start()
   108             self._start(rearm=True)
   106 
   109 
   107     def trigger(self):
   110     def trigger(self):
   108         self._callback()
   111         self._callback()
   109         self.feed()
   112         self.feed()
   110 
   113 
   160     print("SVGHMI watchdog trigger")
   163     print("SVGHMI watchdog trigger")
   161     
   164     
   162 
   165 
   163 # Called by PLCObject at start
   166 # Called by PLCObject at start
   164 def _runtime_svghmi0_start():
   167 def _runtime_svghmi0_start():
   165     global svghmi_listener, svghmi_root, svghmi_send_thread, svghmi_watchdog
   168     global svghmi_listener, svghmi_root, svghmi_send_thread
   166 
   169 
   167     svghmi_root = Resource()
   170     svghmi_root = Resource()
   168     svghmi_root.putChild("ws", WebSocketResource(HMIWebSocketServerFactory()))
   171     svghmi_root.putChild("ws", WebSocketResource(HMIWebSocketServerFactory()))
   169 
   172 
   170     svghmi_listener = reactor.listenTCP(8008, Site(svghmi_root))
   173     svghmi_listener = reactor.listenTCP(8008, Site(svghmi_root))
   171 
   174 
   172     # start a thread that call the C part of SVGHMI
   175     # start a thread that call the C part of SVGHMI
   173     svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send")
   176     svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send")
   174     svghmi_send_thread.start()
   177     svghmi_send_thread.start()
   175 
   178 
   176     svghmi_watchdog = Watchdog(5, watchdog_trigger)
       
   177 
   179 
   178 # Called by PLCObject at stop
   180 # Called by PLCObject at stop
   179 def _runtime_svghmi0_stop():
   181 def _runtime_svghmi0_stop():
   180     global svghmi_listener, svghmi_root, svghmi_send_thread, svghmi_session, svghmi_watchdog
   182     global svghmi_listener, svghmi_root, svghmi_send_thread, svghmi_session
   181 
       
   182     if svghmi_watchdog is not None:
       
   183         svghmi_watchdog.cancel()
       
   184         svghmi_watchdog = None
       
   185 
   183 
   186     if svghmi_session is not None:
   184     if svghmi_session is not None:
   187         svghmi_session.close()
   185         svghmi_session.close()
   188     svghmi_root.delEntity("ws")
   186     svghmi_root.delEntity("ws")
   189     svghmi_root = None
   187     svghmi_root = None