def got_data(self, data): """ Callback for receiving new data from the device. """ if self.get_current_state() == ProtocolStates.DIRECT_ACCESS: # direct access mode if len(data) > 0: log.debug("mavs4InstrumentProtocol.got_data(): <" + data + ">") if self._driver_event: self._driver_event(DriverAsyncEvent.DIRECT_ACCESS, data) # TODO: what about logging this as an event? return if len(data)>0: # Call the superclass to update line and prompt buffers. MenuInstrumentProtocol.got_data(self, data) # If in streaming mode, process the buffer for samples to publish. cur_state = self.get_current_state() if cur_state == ProtocolStates.AUTOSAMPLE: if INSTRUMENT_NEWLINE in self._linebuf: lines = self._linebuf.split(INSTRUMENT_NEWLINE) self._linebuf = lines[-1] for line in lines: self._extract_sample(line)
def __init__(self, prompts, newline, driver_event): """ """ MenuInstrumentProtocol.__init__(self, prompts, newline, driver_event) self.write_delay = WRITE_DELAY self._last_data_timestamp = None self.eoln = EOLN ##### Setup the state machine self._protocol_fsm = InstrumentFSM(State, Event, Event.ENTER, Event.EXIT) self._protocol_fsm.add_handler(State.UNCONFIGURED_MODE, Event.INITIALIZE, self._handler_initialize) self._protocol_fsm.add_handler(State.UNCONFIGURED_MODE, Event.DISCOVER, self._handler_unknown_discover) self._protocol_fsm.add_handler(State.CONTINUOUS_MODE, Event.MENU_CMD, self._handler_continuous_menu) self._protocol_fsm.add_handler(State.CONTINUOUS_MODE, Event.GO_CMD, self._handler_continuous_go) self._protocol_fsm.add_handler(State.CONTINUOUS_MODE, Event.STOP_CMD, self._handler_continuous_stop) # ... and so on with the operation handler listings... # In general, naming is _handler_currentstate_eventreceived self._protocol_fsm.add_handler(State.ROOT_MENU, Event.ENTER, self._handler_root_menu_enter) self._protocol_fsm.add_handler(State.ROOT_MENU, Event.CONFIG_MENU, self._handler_root_config) self._protocol_fsm.add_handler(State.ROOT_MENU, Event.SETUP_MENU, self._handler_root_setup) self._protocol_fsm.add_handler(State.ROOT_MENU, Event.FILE_MENU, self._handler_root_file) # # DHE added # self._protocol_fsm.add_handler(State.ROOT_MENU, Event.GET, self._handler_command_get) # @todo ... and so on with the menu handler listings... # these build handlers will be called by the base class during the # navigate_and_execute sequence. self._add_build_handler(Event.CONFIG_MENU, self._build_simple_command) self._add_build_handler(Event.SHOW_CONFIG, self._build_simple_command) self._add_build_handler(Event.BAUD_RATE, self._build_simple_command) # Add response handlers for parsing command responses self._add_response_handler(Event.SHOW_CONFIG, self._parse_show_config_menu_response) # Construct the parameter dictionary self._build_param_dict() # State state machine in UNCONFIGURED state. self._protocol_fsm.start(State.UNCONFIGURED_MODE) """
def got_data(self, data): """ Callback for receiving new data from the device. """ #if self.get_current_state() == State.DIRECT_ACCESS: # # direct access mode # if len(data) > 0: # #mi_logger.debug("ooicoreInstrumentProtocol.got_data(): <" + data + ">") # # check for echoed commands from instrument (TODO: this should only be done for telnet?) # if len(self._sent_cmds) > 0: # # there are sent commands that need to have there echoes filtered out # oldest_sent_cmd = self._sent_cmds[0] # if string.count(data, oldest_sent_cmd) > 0: # # found a command echo, so remove it from data and delete the command form list # data = string.replace(data, oldest_sent_cmd, "", 1) # self._sent_cmds.pop(0) # if len(data) > 0 and self._driver_event: # self._driver_event(DriverAsyncEvent.DIRECT_ACCESS, data) # # TODO: what about logging this as an event? # return if len(data) > 0: # Call the superclass to update line and prompt buffers. MenuInstrumentProtocol.got_data(self, data)
class TestUnitMenuInstrumentProtocol(MiUnitTestCase): """ Test cases for instrument protocol class. Functions in this class provide instrument protocol unit tests and provide a tutorial on use of the protocol interface. """ class SubMenu(BaseEnum): MAIN = "SUBMENU_MAIN" ONE = "SUBMENU_ONE" TWO = "SUBMENU_TWO" class Prompt(BaseEnum): CMD_PROMPT = "-->" CONTINUE_PROMPT = "Press ENTER to continue." MAIN_MENU = "MAIN -->" ONE_MENU = "MENU 1 -->" TWO_MENU = "MENU 2 -->" MENU = MenuInstrumentProtocol.MenuTree({ SubMenu.MAIN: [], SubMenu.ONE: [Directions(command="1", response=Prompt.ONE_MENU)], SubMenu.TWO: [ Directions(SubMenu.ONE), Directions(command="2", response=Prompt.CONTINUE_PROMPT) ] }) def setUp(self): """ """ self.callback_result = None def protocol_callback(self, arg): callback_result = arg # Call no longer valid. MenuInstrumentProtocol now takes 4 args. #self.protocol = MenuInstrumentProtocol(protocol_callback) @unittest.skip("SKIP - Not Written") def test_navigation(self): """ Test the navigate method to get between menus """ pass
def __init__(self, prompts, newline, driver_event): """ """ self.write_delay = WRITE_DELAY self._last_data_timestamp = None self.eoln = INSTRUMENT_NEWLINE # create short alias for Directions class Directions = MenuInstrumentProtocol.MenuTree.Directions # create MenuTree object menu = MenuInstrumentProtocol.MenuTree({ SubMenues.ROOT : [], SubMenues.SET_TIME : [Directions(InstrumentCmds.SET_TIME, InstrumentPrompts.SET_TIME)], SubMenues.DEPLOY : [Directions(InstrumentCmds.DEPLOY_MENU, InstrumentPrompts.SUB_MENU, 20)] }) MenuInstrumentProtocol.__init__(self, menu, prompts, newline, driver_event) # these build handlers will be called by the base class during the # navigate_and_execute sequence. self._add_build_handler(InstrumentCmds.ENTER_TIME, self._build_time_command) self._add_build_handler(InstrumentCmds.SET_TIME, self._build_keypress_command) self._add_build_handler(InstrumentCmds.ANSWER_YES, self._build_keypress_command) self._add_build_handler(InstrumentCmds.DEPLOY_MENU, self._build_keypress_command) self._add_build_handler(InstrumentCmds.DEPLOY_GO, self._build_keypress_command) self._add_build_handler(InstrumentCmds.EXIT_SUB_MENU, self._build_keypress_command) # Add response handlers for device commands. self._add_response_handler(InstrumentCmds.SET_TIME, self._parse_time_response) self._protocol_fsm = InstrumentFSM(ProtocolStates, ProtocolEvents, ProtocolEvents.ENTER, ProtocolEvents.EXIT) # Add event handlers for protocol state machine. self._protocol_fsm.add_handler(ProtocolStates.UNKNOWN, ProtocolEvents.ENTER, self._handler_unknown_enter) self._protocol_fsm.add_handler(ProtocolStates.UNKNOWN, ProtocolEvents.EXIT, self._handler_unknown_exit) self._protocol_fsm.add_handler(ProtocolStates.UNKNOWN, ProtocolEvents.DISCOVER, self._handler_unknown_discover) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.ENTER, self._handler_command_enter) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.EXIT, self._handler_command_exit) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.START_AUTOSAMPLE, self._handler_command_start_autosample) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.SET, self._handler_command_set) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.GET, self._handler_get) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.TEST, self._handler_command_test) self._protocol_fsm.add_handler(ProtocolStates.COMMAND, ProtocolEvents.START_DIRECT, self._handler_command_start_direct) self._protocol_fsm.add_handler(ProtocolStates.AUTOSAMPLE, ProtocolEvents.ENTER, self._handler_autosample_enter) self._protocol_fsm.add_handler(ProtocolStates.AUTOSAMPLE, ProtocolEvents.EXIT, self._handler_autosample_exit) self._protocol_fsm.add_handler(ProtocolStates.AUTOSAMPLE, ProtocolEvents.STOP_AUTOSAMPLE, self._handler_autosample_stop_autosample) self._protocol_fsm.add_handler(ProtocolStates.DIRECT_ACCESS, ProtocolEvents.ENTER, self._handler_direct_access_enter) self._protocol_fsm.add_handler(ProtocolStates.DIRECT_ACCESS, ProtocolEvents.EXIT, self._handler_direct_access_exit) self._protocol_fsm.add_handler(ProtocolStates.DIRECT_ACCESS, ProtocolEvents.EXECUTE_DIRECT, self._handler_direct_access_execute_direct) self._protocol_fsm.add_handler(ProtocolStates.DIRECT_ACCESS, ProtocolEvents.STOP_DIRECT, self._handler_direct_access_stop_direct) # Construct the parameter dictionary containing device parameters, # current parameter values, and set formatting functions. self._build_param_dict() # Set state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolStates.UNKNOWN)
def __init__(self, menu, 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. """ # Construct protocol superclass. MenuInstrumentProtocol.__init__(self, menu, prompts, newline, driver_event) # Build protocol state machine. self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT) # Add event handlers for protocol state machine. self._protocol_fsm.add_handler(ProtocolState.UNKNOWN, ProtocolEvent.ENTER, self._handler_unknown_enter) self._protocol_fsm.add_handler(ProtocolState.UNKNOWN, ProtocolEvent.DISCOVER, self._handler_discover) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.ENTER, self._handler_command_enter) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.DISCOVER, self._handler_discover) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.GET, self._handler_command_get) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.SET, self._handler_command_set) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.START_AUTOSAMPLE, self._handler_command_autosample) self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.START_DIRECT, self._handler_command_start_direct) self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.STOP_AUTOSAMPLE, self._handler_autosample_stop) self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.DISCOVER, self._handler_discover) self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.ENTER, self._handler_direct_access_enter) self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.EXIT, self._handler_direct_access_exit) self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct) self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.EXECUTE_DIRECT, self._handler_direct_access_execute_direct) # Construct the parameter dictionary containing device parameters, # current parameter values, and set formatting functions. self._build_param_dict() # Add build handlers for device commands. self._add_build_handler(Command.BACK_MENU, self._build_menu_command) self._add_build_handler(Command.BLANK, self._build_solo_command) self._add_build_handler(Command.START_AUTOSAMPLE, self._build_menu_command) self._add_build_handler(Command.CHANGE_PARAM, self._build_menu_command) self._add_build_handler(Command.SHOW_PARAM, self._build_menu_command) self._add_build_handler(Command.SENSOR_POWER, self._build_menu_command) self._add_build_handler(Command.DIRECT_SET, self._build_direct_command) self._add_build_handler(Command.CHANGE_CYCLE_TIME, self._build_menu_command) self._add_build_handler(Command.CHANGE_VERBOSE, self._build_menu_command) self._add_build_handler(Command.CHANGE_METADATA_RESTART, self._build_menu_command) self._add_build_handler(Command.CHANGE_METADATA_POWERUP, self._build_menu_command) self._add_build_handler(Command.CHANGE_RES_SENSOR_POWER, self._build_menu_command) self._add_build_handler(Command.CHANGE_INST_AMP_POWER, self._build_menu_command) self._add_build_handler(Command.CHANGE_EH_ISOLATION_AMP_POWER, self._build_menu_command) self._add_build_handler(Command.CHANGE_HYDROGEN_POWER, self._build_menu_command) self._add_build_handler(Command.CHANGE_REFERENCE_TEMP_POWER, self._build_menu_command) # Add response handlers for device commands. #self._add_response_handler(Command.GET, self._parse_get_response) #self._add_response_handler(Command.SET, self._parse_get_response) self._add_response_handler(Command.BACK_MENU, self._parse_menu_change_response) self._add_response_handler(Command.BLANK, self._parse_menu_change_response) self._add_response_handler(Command.SHOW_PARAM, self._parse_show_param_response) self._add_response_handler(Command.CHANGE_CYCLE_TIME, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_VERBOSE, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_METADATA_RESTART, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_METADATA_POWERUP, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_RES_SENSOR_POWER, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_INST_AMP_POWER, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_EH_ISOLATION_AMP_POWER, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_HYDROGEN_POWER, self._parse_menu_change_response) self._add_response_handler(Command.CHANGE_REFERENCE_TEMP_POWER, self._parse_menu_change_response) self._add_response_handler(Command.DIRECT_SET, self._parse_menu_change_response) # Add sample handlers. # State state machine in UNKNOWN state. self._protocol_fsm.start(ProtocolState.UNKNOWN) # commands sent sent to device to be filtered in responses for telnet DA self._sent_cmds = [] self._chunker = StringChunker(self.sieve_function)
MENU = MenuInstrumentProtocol.MenuTree({ SubMenu.MAIN:[], SubMenu.CHANGE_PARAM:[Directions(command=Command.CHANGE_PARAM, response=Prompt.CHANGE_PARAM_MENU)], SubMenu.SHOW_PARAM:[Directions(SubMenu.CHANGE_PARAM), Directions(command=Command.SHOW_PARAM, response=Prompt.CONTINUE_PROMPT)], SubMenu.SENSOR_POWER:[Directions(command=Command.SENSOR_POWER, response=Prompt.SENSOR_POWER_MENU)], SubMenu.CYCLE_TIME:[Directions(SubMenu.CHANGE_PARAM), Directions(command=Command.CHANGE_CYCLE_TIME, response=Prompt.CYCLE_TIME_PROMPT)], SubMenu.VERBOSE:[Directions(SubMenu.CHANGE_PARAM), Directions(command=Command.CHANGE_VERBOSE, response=Prompt.VERBOSE_PROMPT)], SubMenu.METADATA_POWERUP:[Directions(SubMenu.CHANGE_PARAM), Directions(command=Command.CHANGE_METADATA_POWERUP, response=Prompt.METADATA_PROMPT)], SubMenu.METADATA_RESTART:[Directions(SubMenu.CHANGE_PARAM), Directions(command=Command.CHANGE_METADATA_RESTART, response=Prompt.METADATA_PROMPT)], SubMenu.RES_SENSOR_POWER:[Directions(SubMenu.SENSOR_POWER), Directions(command=Command.CHANGE_RES_SENSOR_POWER, response=Prompt.SENSOR_POWER_MENU)], SubMenu.INST_AMP_POWER:[Directions(SubMenu.SENSOR_POWER), Directions(command=Command.CHANGE_INST_AMP_POWER, response=Prompt.SENSOR_POWER_MENU)], SubMenu.EH_ISOLATION_AMP_POWER:[Directions(SubMenu.SENSOR_POWER), Directions(command=Command.CHANGE_EH_ISOLATION_AMP_POWER, response=Prompt.SENSOR_POWER_MENU)], SubMenu.HYDROGEN_POWER:[Directions(SubMenu.SENSOR_POWER), Directions(command=Command.CHANGE_HYDROGEN_POWER, response=Prompt.SENSOR_POWER_MENU)], SubMenu.REFERENCE_TEMP_POWER:[Directions(SubMenu.SENSOR_POWER), Directions(command=Command.CHANGE_REFERENCE_TEMP_POWER, response=Prompt.SENSOR_POWER_MENU)], })