def onJoin(self, details): port = self.config.extra['port'] baudrate = self.config.extra['baudrate'] # install signal handler signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) self.toWAMP_logger = Log_WAMP_Handler(self, self.logQueue) self.toWAMP_logger.addFilter(NoHTTP_GetFilter()) logging.getLogger('serial2ws').addHandler(self.toWAMP_logger) # first of all, register the getErrors Function yield self.register(self.getLog, u"de.me_systeme.gsv.getLog") yield self.register(self.getIsSerialConnected, u"de.me_systeme.gsv.getIsSerialConnected") # create an router object/thread self.router = FrameRouter(self, self.frameInBuffer, self.antwortQueue) # create GSV6 Serial-Protocol-Object serialProtocol = GSV_6Protocol(self, self.frameInBuffer, self.antwortQueue) logging.getLogger('serial2ws.WAMP_Component').debug( 'About to open serial port {0} [{1} baud] ..'.format( port, baudrate)) # try to init Serial-Connection try: self.serialPort = SerialPort(serialProtocol, port, reactor, baudrate=baudrate) self.isSerialConnected = True except Exception as e: logging.getLogger('serial2ws.WAMP_Component').critical( 'Could not open serial port: {0}. exit!'.format(e)) os.kill(os.getpid(), signal.SIGTERM) else: # when erial-init okay -> start FrameRouter self.router.start()
def onJoin(self, details): port = self.config.extra['port'] baudrate = self.config.extra['baudrate'] # install signal handler signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) self.toWAMP_logger = Log_WAMP_Handler(self, self.logQueue) self.toWAMP_logger.addFilter(NoHTTP_GetFilter()) logging.getLogger('serial2ws').addHandler(self.toWAMP_logger) # first of all, register the getErrors Function yield self.register(self.getLog, u"de.me_systeme.gsv.getLog") yield self.register(self.getIsSerialConnected, u"de.me_systeme.gsv.getIsSerialConnected") # create an router object/thread self.router = FrameRouter(self, self.frameInBuffer, self.antwortQueue) # create GSV6 Serial-Protocol-Object serialProtocol = GSV_6Protocol(self, self.frameInBuffer, self.antwortQueue) logging.getLogger('serial2ws.WAMP_Component').debug( 'About to open serial port {0} [{1} baud] ..'.format(port, baudrate)) # try to init Serial-Connection try: self.serialPort = SerialPort(serialProtocol, port, reactor, baudrate=baudrate) self.isSerialConnected = True except Exception as e: logging.getLogger('serial2ws.WAMP_Component').critical('Could not open serial port: {0}. exit!'.format(e)) os.kill(os.getpid(), signal.SIGTERM) else: # when erial-init okay -> start FrameRouter self.router.start()
class WAMP_Component(ApplicationSession): """ RPi WAMP application component. """ # The Queue-Object is an threadsafe FIFO Buffer. # Operations like put and get are atomic # this queue holds all incomming complete Frames frameInBuffer = Queue(50) # this queue holds the ordered config requests antwortQueue = Queue(50) # what ist deque? = double-ended queue. it's a thread-safe ringbuffer # this deque holds the errors as string logQueue = deque([], 200) # hier werden die messungen gespeichert messCSVDictList = [] messCSVDictList_lock = threading.Lock() # to ensure that we have a thead-safe write function, we need that look serialWrite_lock = threading.Lock() isSerialConnected = False # GSV-6CPU RX bufferoverflow prevention actTime = None lastTime = datetime.now() # ready falg sys_ready = False # cleanup here def onLeave(self, details): try: self.serialPort.stopReading() except Exception: pass try: self.serialPort.reactor.stop() except Exception: pass if self.router.isAlive(): self.router.stop() # wait max 1 Sec. self.router.join(1.0) @inlineCallbacks def onJoin(self, details): port = self.config.extra['port'] baudrate = self.config.extra['baudrate'] # install signal handler signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) self.toWAMP_logger = Log_WAMP_Handler(self, self.logQueue) self.toWAMP_logger.addFilter(NoHTTP_GetFilter()) logging.getLogger('serial2ws').addHandler(self.toWAMP_logger) # first of all, register the getErrors Function yield self.register(self.getLog, u"de.me_systeme.gsv.getLog") yield self.register(self.getIsSerialConnected, u"de.me_systeme.gsv.getIsSerialConnected") # create an router object/thread self.router = FrameRouter(self, self.frameInBuffer, self.antwortQueue) # create GSV6 Serial-Protocol-Object serialProtocol = GSV_6Protocol(self, self.frameInBuffer, self.antwortQueue) logging.getLogger('serial2ws.WAMP_Component').debug( 'About to open serial port {0} [{1} baud] ..'.format(port, baudrate)) # try to init Serial-Connection try: self.serialPort = SerialPort(serialProtocol, port, reactor, baudrate=baudrate) self.isSerialConnected = True except Exception as e: logging.getLogger('serial2ws.WAMP_Component').critical('Could not open serial port: {0}. exit!'.format(e)) os.kill(os.getpid(), signal.SIGTERM) else: # when erial-init okay -> start FrameRouter self.router.start() def __exit__(self): logging.getLogger('serial2ws.WAMP_Component').trace('Exit.') def __del__(self): logging.getLogger('serial2ws.WAMP_Component').trace('del.') def getLog(self): return list(self.logQueue) sendCounter = 0 def writeAntwort(self, data, functionName, args=None): # okay this function have to be atomic # we protect it with a lock! self.serialWrite_lock.acquire() self.actTime = datetime.now() diffTime = self.actTime - self.lastTime if diffTime.days <= 0 and diffTime.seconds <= 2: if (diffTime.seconds == 0 and diffTime.microseconds < 4000): self.sendCounter += 1 if self.sendCounter >= 8: self.sendCounter = 0 logging.getLogger('serial2ws.WAMP_Component').debug( "serialWait, prevent GSV-6CPU RX Buffer overflow") sleep(0.2) # Time in seconds else: self.sendCounter = 0 try: self.antwortQueue.put_nowait({functionName: args}) self.serialPort.write(str(data)) logging.getLogger('serial2ws.WAMP_Component').debug( '[serialWrite] Data: ' + ' '.join(format(z, '02x') for z in data)) except NameError: logging.getLogger('serial2ws.WAMP_Component').debug('[WAMP_Component] serialport not openend') finally: self.lastTime = datetime.now() self.serialWrite_lock.release() def write(self, data): # okay this function have to be atomic # we protect it with a lock! self.serialWrite_lock.acquire() try: self.serialPort.write(str(data)) logging.getLogger('serial2ws.WAMP_Component').debug( '[serialWrite] Data: ' + ' '.join(format(z, '02x') for z in data)) except NameError: logging.getLogger('serial2ws.WAMP_Component').debug('serialport not openend') finally: self.serialWrite_lock.release() def getIsSerialConnected(self): return self.isSerialConnected def lostSerialConnection(self, errorMessage): logging.getLogger('serial2ws.WAMP_Component').critical("Lost SerialConnection: " + errorMessage) self.isSerialConnected = False # not implemented at web-frontend self.publish(u"de.me_systeme.gsv.onSystemGoingDown") # shut down app os.kill(os.getpid(), signal.SIGTERM) def signal_handler(self, signal, frame): if signal == 15: self.stopMeasurement() logger = logging.getLogger('serial2ws') logger.removeHandler(self.toWAMP_logger) logger.info('received SIGNAL, going down.') self.disconnect() def stopMeasurement(self): data = self.router.gsv6.buildStopTransmission() logging.getLogger('serial2ws.WAMP_Component').warning('Sending stop Message to GSV-Modul') self.write(data) self.router.writeCSVdata()
class WAMP_Component(ApplicationSession): """ RPi WAMP application component. """ # The Queue-Object is an threadsafe FIFO Buffer. # Operations like put and get are atomic # this queue holds all incomming complete Frames frameInBuffer = Queue(50) # this queue holds the ordered config requests antwortQueue = Queue(50) # what ist deque? = double-ended queue. it's a thread-safe ringbuffer # this deque holds the errors as string logQueue = deque([], 200) # hier werden die messungen gespeichert messCSVDictList = [] messCSVDictList_lock = threading.Lock() # to ensure that we have a thead-safe write function, we need that look serialWrite_lock = threading.Lock() isSerialConnected = False # GSV-6CPU RX bufferoverflow prevention actTime = None lastTime = datetime.now() # ready falg sys_ready = False # cleanup here def onLeave(self, details): try: self.serialPort.stopReading() except Exception: pass try: self.serialPort.reactor.stop() except Exception: pass if self.router.isAlive(): self.router.stop() # wait max 1 Sec. self.router.join(1.0) @inlineCallbacks def onJoin(self, details): port = self.config.extra['port'] baudrate = self.config.extra['baudrate'] # install signal handler signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) self.toWAMP_logger = Log_WAMP_Handler(self, self.logQueue) self.toWAMP_logger.addFilter(NoHTTP_GetFilter()) logging.getLogger('serial2ws').addHandler(self.toWAMP_logger) # first of all, register the getErrors Function yield self.register(self.getLog, u"de.me_systeme.gsv.getLog") yield self.register(self.getIsSerialConnected, u"de.me_systeme.gsv.getIsSerialConnected") # create an router object/thread self.router = FrameRouter(self, self.frameInBuffer, self.antwortQueue) # create GSV6 Serial-Protocol-Object serialProtocol = GSV_6Protocol(self, self.frameInBuffer, self.antwortQueue) logging.getLogger('serial2ws.WAMP_Component').debug( 'About to open serial port {0} [{1} baud] ..'.format( port, baudrate)) # try to init Serial-Connection try: self.serialPort = SerialPort(serialProtocol, port, reactor, baudrate=baudrate) self.isSerialConnected = True except Exception as e: logging.getLogger('serial2ws.WAMP_Component').critical( 'Could not open serial port: {0}. exit!'.format(e)) os.kill(os.getpid(), signal.SIGTERM) else: # when erial-init okay -> start FrameRouter self.router.start() def __exit__(self): logging.getLogger('serial2ws.WAMP_Component').trace('Exit.') def __del__(self): logging.getLogger('serial2ws.WAMP_Component').trace('del.') def getLog(self): return list(self.logQueue) sendCounter = 0 def writeAntwort(self, data, functionName, args=None): # okay this function have to be atomic # we protect it with a lock! self.serialWrite_lock.acquire() self.actTime = datetime.now() diffTime = self.actTime - self.lastTime if diffTime.days <= 0 and diffTime.seconds <= 2: if (diffTime.seconds == 0 and diffTime.microseconds < 4000): self.sendCounter += 1 if self.sendCounter >= 8: self.sendCounter = 0 logging.getLogger('serial2ws.WAMP_Component').debug( "serialWait, prevent GSV-6CPU RX Buffer overflow") sleep(0.2) # Time in seconds else: self.sendCounter = 0 try: self.antwortQueue.put_nowait({functionName: args}) self.serialPort.write(str(data)) logging.getLogger('serial2ws.WAMP_Component').debug( '[serialWrite] Data: ' + ' '.join(format(z, '02x') for z in data)) except NameError: logging.getLogger('serial2ws.WAMP_Component').debug( '[WAMP_Component] serialport not openend') finally: self.lastTime = datetime.now() self.serialWrite_lock.release() def write(self, data): # okay this function have to be atomic # we protect it with a lock! self.serialWrite_lock.acquire() try: self.serialPort.write(str(data)) logging.getLogger('serial2ws.WAMP_Component').debug( '[serialWrite] Data: ' + ' '.join(format(z, '02x') for z in data)) except NameError: logging.getLogger('serial2ws.WAMP_Component').debug( 'serialport not openend') finally: self.serialWrite_lock.release() def getIsSerialConnected(self): return self.isSerialConnected def lostSerialConnection(self, errorMessage): logging.getLogger('serial2ws.WAMP_Component').critical( "Lost SerialConnection: " + errorMessage) self.isSerialConnected = False # not implemented at web-frontend self.publish(u"de.me_systeme.gsv.onSystemGoingDown") # shut down app os.kill(os.getpid(), signal.SIGTERM) def signal_handler(self, signal, frame): if signal == 15: self.stopMeasurement() logger = logging.getLogger('serial2ws') logger.removeHandler(self.toWAMP_logger) logger.info('received SIGNAL, going down.') self.disconnect() def stopMeasurement(self): data = self.router.gsv6.buildStopTransmission() logging.getLogger('serial2ws.WAMP_Component').warning( 'Sending stop Message to GSV-Modul') self.write(data) self.router.writeCSVdata()