def __init__(self, prompts, newline, driver_event): """ Protocol constructor. @param prompts A BaseEnum class containing instrument prompts. @param newline The newline. @param driver_event Driver process event callback. """ CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event) self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT) handlers = { ProtocolState.UNKNOWN: [(ProtocolEvent.ENTER, self._handler_unknown_enter), (ProtocolEvent.EXIT, self._handler_unknown_exit)], } for state in handlers: for event, handler in handlers[state]: self._protocol_fsm.add_handler(state, event, handler) # State state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolState.UNKNOWN) # create chunker for processing instrument samples. self._chunker = StringChunker(self.sieve_function)
def __init__(self, prompts, newline, driver_event): """ Protocol constructor. @param prompts A BaseEnum class containing instrument prompts. @param newline The newline. @param driver_event Driver process event callback. """ CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event) self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT) handlers = { ProtocolState.UNKNOWN: [ (ProtocolEvent.ENTER, self._handler_unknown_enter), (ProtocolEvent.EXIT, self._handler_unknown_exit) ], } for state in handlers: for event, handler in handlers[state]: self._protocol_fsm.add_handler(state, event, handler) # State state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolState.UNKNOWN) # create chunker for processing instrument samples. self._chunker = StringChunker(self.sieve_function)
class Protocol(CommandResponseInstrumentProtocol): """ Instrument protocol class Subclasses NortekInstrumentProtocol """ def __init__(self, prompts, newline, driver_event): """ Protocol constructor. @param prompts A BaseEnum class containing instrument prompts. @param newline The newline. @param driver_event Driver process event callback. """ CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event) self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT) handlers = { ProtocolState.UNKNOWN: [ (ProtocolEvent.ENTER, self._handler_unknown_enter), (ProtocolEvent.EXIT, self._handler_unknown_exit) ], } for state in handlers: for event, handler in handlers[state]: self._protocol_fsm.add_handler(state, event, handler) # State state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolState.UNKNOWN) # create chunker for processing instrument samples. self._chunker = StringChunker(self.sieve_function) @classmethod def sieve_function(cls, raw_data): """ The method that detects data sample structures from instrument Should be in the format [[structure_sync_bytes, structure_len]*] """ return_list = [] sieve_matchers = [VELOCITY_DATA_REGEX] for matcher in sieve_matchers: for match in matcher.finditer(raw_data): return_list.append((match.start(), match.end())) log.debug("sieve_function: regex found %r", raw_data[match.start():match.end()]) return return_list ######################################################################## # overridden superclass methods ######################################################################## def _got_chunk(self, structure, timestamp): """ The base class got_data has gotten a structure from the chunker. Pass it to extract_sample with the appropriate particle objects and REGEXes. """ self._extract_sample(AquadoppDwVelocityAsciiDataParticle, VELOCITY_DATA_REGEX, structure, timestamp) ######################################################################## # Unknown handlers. ######################################################################## def _handler_unknown_enter(self, *args, **kwargs): """ Enter unknown state. """ self._driver_event(DriverAsyncEvent.STATE_CHANGE) def _handler_unknown_exit(self, *args, **kwargs): """ Exiting Unknown state """ pass
class Protocol(CommandResponseInstrumentProtocol): """ Instrument protocol class Subclasses NortekInstrumentProtocol """ def __init__(self, prompts, newline, driver_event): """ Protocol constructor. @param prompts A BaseEnum class containing instrument prompts. @param newline The newline. @param driver_event Driver process event callback. """ CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event) self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT) handlers = { ProtocolState.UNKNOWN: [(ProtocolEvent.ENTER, self._handler_unknown_enter), (ProtocolEvent.EXIT, self._handler_unknown_exit)], } for state in handlers: for event, handler in handlers[state]: self._protocol_fsm.add_handler(state, event, handler) # State state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolState.UNKNOWN) # create chunker for processing instrument samples. self._chunker = StringChunker(self.sieve_function) @classmethod def sieve_function(cls, raw_data): """ The method that detects data sample structures from instrument Should be in the format [[structure_sync_bytes, structure_len]*] """ return_list = [] sieve_matchers = [VELOCITY_DATA_REGEX] for matcher in sieve_matchers: for match in matcher.finditer(raw_data): return_list.append((match.start(), match.end())) log.debug("sieve_function: regex found %r", raw_data[match.start():match.end()]) return return_list ######################################################################## # overridden superclass methods ######################################################################## def _got_chunk(self, structure, timestamp): """ The base class got_data has gotten a structure from the chunker. Pass it to extract_sample with the appropriate particle objects and REGEXes. """ self._extract_sample(AquadoppDwVelocityAsciiDataParticle, VELOCITY_DATA_REGEX, structure, timestamp) ######################################################################## # Unknown handlers. ######################################################################## def _handler_unknown_enter(self, *args, **kwargs): """ Enter unknown state. """ self._driver_event(DriverAsyncEvent.STATE_CHANGE) def _handler_unknown_exit(self, *args, **kwargs): """ Exiting Unknown state """ pass