本文整理汇总了Python中mi.core.instrument.instrument_fsm.InstrumentFSM.on_event方法的典型用法代码示例。如果您正苦于以下问题:Python InstrumentFSM.on_event方法的具体用法?Python InstrumentFSM.on_event怎么用?Python InstrumentFSM.on_event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mi.core.instrument.instrument_fsm.InstrumentFSM
的用法示例。
在下文中一共展示了InstrumentFSM.on_event方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SBE37Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
result = self._param_dict.get_config()
# If not all params, confirm a list or tuple of params to retrieve.
# Raise if not a list or tuple.
# Retireve each key in the list, raise if any are invalid.
else:
if not isinstance(params, (list, tuple)):
raise InstrumentParameterException('Get argument not a list or tuple.')
result = {}
for key in params:
try:
val = self._param_dict.get(key)
result[key] = val
except KeyError:
raise InstrumentParameterException(('%s is not a valid parameter.' % key))
return (next_state, result)
########################################################################
# Test handlers.
########################################################################
def _handler_test_enter(self, *args, **kwargs):
"""
Enter test state. Setup the secondary call to run the tests.
"""
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
# Forward the test event again to run the test handler and
# switch back to command mode afterward.
Timer(1, lambda: self._protocol_fsm.on_event(SBE37ProtocolEvent.RUN_TEST)).start()
def _handler_test_exit(self, *args, **kwargs):
"""
Exit test state.
"""
pass
def _handler_test_run_tests(self, *args, **kwargs):
"""
Run test routines and validate results.
@throws InstrumentTimeoutException if device cannot be woken for command.
@throws InstrumentProtocolException if command misunderstood or
incorrect prompt received.
"""
next_state = None
result = None
tc_pass = False
tt_pass = False
tp_pass = False
tc_result = None
tt_result = None
tp_result = None
test_result = {}
try:
tc_pass, tc_result = self._do_cmd_resp('tc', timeout=200)
tt_pass, tt_result = self._do_cmd_resp('tt', timeout=200)
tp_pass, tp_result = self._do_cmd_resp('tp', timeout=200)
except Exception as e:
示例2: SatlanticPARInstrumentProtocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
class SatlanticPARInstrumentProtocol(CommandResponseInstrumentProtocol):
"""The instrument protocol classes to deal with a Satlantic PAR sensor.
The protocol is a very simple command/response protocol with a few show
commands and a few set commands.
Note protocol state machine must be called "self._protocol_fsm"
@todo Check for valid state transitions and handle requests appropriately
possibly using better exceptions from the fsm.on_event() method
"""
def __init__(self, callback=None):
CommandResponseInstrumentProtocol.__init__(self, Prompt, EOLN, callback)
self.write_delay = WRITE_DELAY
self._last_data_timestamp = None
self.eoln = EOLN
self._protocol_fsm = InstrumentFSM(PARProtocolState, PARProtocolEvent, PARProtocolEvent.ENTER_STATE,
PARProtocolEvent.EXIT_STATE)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.AUTOSAMPLE,
self._handler_command_autosample)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.COMMAND,
self._handler_command_command)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.ENTER_STATE,
self._handler_command_enter_state)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.GET,
self._handler_command_get)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.SET,
self._handler_command_set)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.POLL,
self._handler_command_poll)
#self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.SAMPLE,
# self._handler_command_sample)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND_MODE, PARProtocolEvent.BREAK,
self._handler_noop)
self._protocol_fsm.add_handler(PARProtocolState.AUTOSAMPLE_MODE, PARProtocolEvent.BREAK,
self._handler_autosample_break)
self._protocol_fsm.add_handler(PARProtocolState.AUTOSAMPLE_MODE, PARProtocolEvent.STOP,
self._handler_autosample_stop)
self._protocol_fsm.add_handler(PARProtocolState.AUTOSAMPLE_MODE, PARProtocolEvent.RESET,
self._handler_reset)
self._protocol_fsm.add_handler(PARProtocolState.AUTOSAMPLE_MODE, PARProtocolEvent.COMMAND,
self._handler_autosample_command)
self._protocol_fsm.add_handler(PARProtocolState.AUTOSAMPLE_MODE, PARProtocolEvent.ENTER_STATE,
self._handler_autosample_enter_state)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.AUTOSAMPLE,
self._handler_poll_autosample)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.RESET,
self._handler_reset)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.BREAK,
self._handler_poll_break)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.SAMPLE,
self._handler_poll_sample)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.COMMAND,
self._handler_poll_command)
self._protocol_fsm.add_handler(PARProtocolState.POLL_MODE, PARProtocolEvent.ENTER_STATE,
self._handler_poll_enter_state)
self._protocol_fsm.add_handler(PARProtocolState.UNKNOWN, PARProtocolEvent.INITIALIZE,
self._handler_initialize)
self._protocol_fsm.start(PARProtocolState.UNKNOWN)
self._add_build_handler(Command.SET, self._build_set_command)
self._add_build_handler(Command.GET, self._build_param_fetch_command)
self._add_build_handler(Command.SAVE, self._build_exec_command)
self._add_build_handler(Command.EXIT, self._build_exec_command)
self._add_build_handler(Command.EXIT_AND_RESET, self._build_exec_command)
self._add_build_handler(Command.AUTOSAMPLE, self._build_multi_control_command)
self._add_build_handler(Command.RESET, self._build_control_command)
self._add_build_handler(Command.BREAK, self._build_multi_control_command)
self._add_build_handler(Command.SAMPLE, self._build_control_command)
self._add_build_handler(Command.STOP, self._build_multi_control_command)
self._add_response_handler(Command.GET, self._parse_get_response)
self._add_response_handler(Command.SET, self._parse_set_response)
self._add_response_handler(Command.STOP, self._parse_silent_response)
self._add_response_handler(Command.SAMPLE, self._parse_sample_poll_response, PARProtocolState.POLL_MODE)
self._add_response_handler(Command.SAMPLE, self._parse_cmd_prompt_response, PARProtocolState.COMMAND_MODE)
self._add_response_handler(Command.BREAK, self._parse_silent_response, PARProtocolState.COMMAND_MODE)
self._add_response_handler(Command.BREAK, self._parse_header_response, PARProtocolState.POLL_MODE)
self._add_response_handler(Command.BREAK, self._parse_header_response, PARProtocolState.AUTOSAMPLE_MODE)
self._add_response_handler(Command.RESET, self._parse_silent_response, PARProtocolState.COMMAND_MODE)
self._add_response_handler(Command.RESET, self._parse_reset_response, PARProtocolState.POLL_MODE)
self._add_response_handler(Command.RESET, self._parse_reset_response, PARProtocolState.AUTOSAMPLE_MODE)
self._param_dict.add(Parameter.TELBAUD,
r'Telemetry Baud Rate:\s+(\d+) bps',
lambda match : int(match.group(1)),
self._int_to_string)
self._param_dict.add(Parameter.MAXRATE,
r'Maximum Frame Rate:\s+(\d+) Hz',
lambda match : int(match.group(1)),
self._int_to_string)
def execute_exit(self, *args, **kwargs):
""" Execute the exit command
#.........这里部分代码省略.........
示例3: VadcpProtocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
class VadcpProtocol(CommandResponseInstrumentProtocol):
"""
"""
def __init__(self, callback=None):
CommandResponseInstrumentProtocol.__init__(self, Prompt, EOLN, callback)
# TODO probably promote this convenience to super-class?
# _timeout: Default timeout value for operations accepting an
# optional timeout argument
self._timeout = 30
self._last_data_timestamp = None
self.eoln = EOLN
self._protocol_fsm = InstrumentFSM(ProtocolState, ProtocolEvent,
None, None)
# UNKNOWN
self._protocol_fsm.add_handler(ProtocolState.UNKNOWN,
ProtocolEvent.INITIALIZE,
self._handler_initialize)
# COMMAND_MODE
self._protocol_fsm.add_handler(ProtocolState.COMMAND_MODE,
ProtocolEvent.GET_LAST_ENSEMBLE,
self._handler_command_get_latest_sample)
self._protocol_fsm.add_handler(ProtocolState.COMMAND_MODE,
ProtocolEvent.GET_METADATA,
self._handler_command_get_metadata)
self._protocol_fsm.add_handler(ProtocolState.COMMAND_MODE,
ProtocolEvent.RUN_RECORDER_TESTS,
self._handler_command_run_recorder_tests)
self._protocol_fsm.add_handler(ProtocolState.COMMAND_MODE,
ProtocolEvent.RUN_ALL_TESTS,
self._handler_command_run_all_tests)
self._protocol_fsm.add_handler(ProtocolState.COMMAND_MODE,
ProtocolEvent.START_AUTOSAMPLE,
self._handler_command_autosample)
# AUTOSAMPLE_MODE
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE_MODE,
ProtocolEvent.STOP_AUTOSAMPLE,
self._handler_autosample_stop)
self._protocol_fsm.start(ProtocolState.UNKNOWN)
def execute_init_protocol(self, *args, **kwargs):
"""
"""
return self._protocol_fsm.on_event(ProtocolEvent.INITIALIZE,
*args, **kwargs)
def execute_get_latest_sample(self, *args, **kwargs):
"""
"""
return self._protocol_fsm.on_event(ProtocolEvent.GET_LAST_ENSEMBLE,
*args, **kwargs)
def execute_get_metadata(self, *args, **kwargs):
"""
"""
return self._protocol_fsm.on_event(ProtocolEvent.GET_METADATA,
*args, **kwargs)
def execute_run_recorder_tests(self, *args, **kwargs):
"""
"""
return self._protocol_fsm.on_event(ProtocolEvent.RUN_RECORDER_TESTS,
*args, **kwargs)
def execute_run_all_tests(self, *args, **kwargs):
"""
"""
return self._protocol_fsm.on_event(ProtocolEvent.RUN_ALL_TESTS,
*args, **kwargs)
################
# State handlers
################
def _handler_initialize(self, *args, **kwargs):
"""
Determines initial protocol state according to instrument's state
"""
next_state = None
result = None
# TODO determine the state. For now, assume command mode
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
next_state = ProtocolState.COMMAND_MODE
return (next_state, result)
def _handler_command_get_latest_sample(self, *args, **kwargs):
"""
"""
if log.isEnabledFor(logging.DEBUG):
log.debug("args=%s kwargs=%s" % (str(args), str(kwargs)))
#.........这里部分代码省略.........
示例4: SingleConnectionInstrumentDriver
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
class SingleConnectionInstrumentDriver(InstrumentDriver):
"""
Base class for instrument drivers with a single device connection.
Provides connenction state logic for single connection drivers. This is
the base class for the majority of driver implementation classes.
"""
def __init__(self, event_callback):
"""
Constructor for singly connected instrument drivers.
@param event_callback Callback to the driver process to send asynchronous
driver events back to the agent.
"""
InstrumentDriver.__init__(self, event_callback)
# The only and only instrument connection.
# Exists in the connected state.
self._connection = None
# The one and only instrument protocol.
self._protocol = None
# Build connection state machine.
self._connection_fsm = InstrumentFSM(DriverConnectionState,
DriverEvent,
DriverEvent.ENTER,
DriverEvent.EXIT)
# Add handlers for all events.
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.ENTER, self._handler_unconfigured_enter)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.EXIT, self._handler_unconfigured_exit)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.INITIALIZE, self._handler_unconfigured_initialize)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.CONFIGURE, self._handler_unconfigured_configure)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.ENTER, self._handler_disconnected_enter)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.EXIT, self._handler_disconnected_exit)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.INITIALIZE, self._handler_disconnected_initialize)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.CONFIGURE, self._handler_disconnected_configure)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.CONNECT, self._handler_disconnected_connect)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.ENTER, self._handler_connected_enter)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.EXIT, self._handler_connected_exit)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.DISCONNECT, self._handler_connected_disconnect)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.CONNECTION_LOST, self._handler_connected_connection_lost)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.DISCOVER, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.GET, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.SET, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.EXECUTE, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.FORCE_STATE, self._handler_connected_protocol_event)
# Start state machine.
self._connection_fsm.start(DriverConnectionState.UNCONFIGURED)
#############################################################
# Device connection interface.
#############################################################
def initialize(self, *args, **kwargs):
"""
Initialize driver connection, bringing communications parameters
into unconfigured state (no connection object).
@raises InstrumentStateException if command not allowed in current state
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.INITIALIZE, *args, **kwargs)
def configure(self, *args, **kwargs):
"""
Configure the driver for communications with the device via
port agent / logger (valid but unconnected connection object).
@param arg[0] comms config dict.
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentParameterException if missing comms or invalid config dict.
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.CONFIGURE, *args, **kwargs)
def connect(self, *args, **kwargs):
"""
Establish communications with the device via port agent / logger
(connected connection object).
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentConnectionException if the connection failed.
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.CONNECT, *args, **kwargs)
def disconnect(self, *args, **kwargs):
"""
Disconnect from device via port agent / logger.
@raises InstrumentStateException if command not allowed in current state
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.DISCONNECT, *args, **kwargs)
#############################################################
# Commande and control interface.
#############################################################
def discover_state(self, *args, **kwargs):
"""
Determine initial state upon establishing communications.
#.........这里部分代码省略.........
示例5: SingleConnectionInstrumentDriver
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
class SingleConnectionInstrumentDriver(InstrumentDriver):
"""
Base class for instrument drivers with a single device connection.
Provides connenction state logic for single connection drivers. This is
the base class for the majority of driver implementation classes.
"""
def __init__(self, event_callback):
"""
Constructor for singly connected instrument drivers.
@param event_callback Callback to the driver process to send asynchronous
driver events back to the agent.
"""
InstrumentDriver.__init__(self, event_callback)
# The only and only instrument connection.
# Exists in the connected state.
self._connection = None
# The one and only instrument protocol.
self._protocol = None
# Build connection state machine.
self._connection_fsm = InstrumentFSM(DriverConnectionState,
DriverEvent,
DriverEvent.ENTER,
DriverEvent.EXIT)
# Add handlers for all events.
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.ENTER, self._handler_unconfigured_enter)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.EXIT, self._handler_unconfigured_exit)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.INITIALIZE, self._handler_unconfigured_initialize)
self._connection_fsm.add_handler(DriverConnectionState.UNCONFIGURED, DriverEvent.CONFIGURE, self._handler_unconfigured_configure)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.ENTER, self._handler_disconnected_enter)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.EXIT, self._handler_disconnected_exit)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.INITIALIZE, self._handler_disconnected_initialize)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.CONFIGURE, self._handler_disconnected_configure)
self._connection_fsm.add_handler(DriverConnectionState.DISCONNECTED, DriverEvent.CONNECT, self._handler_disconnected_connect)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.ENTER, self._handler_connected_enter)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.EXIT, self._handler_connected_exit)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.DISCONNECT, self._handler_connected_disconnect)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.CONNECTION_LOST, self._handler_connected_connection_lost)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.DISCOVER, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.GET, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.SET, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.EXECUTE, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.FORCE_STATE, self._handler_connected_protocol_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.START_DIRECT, self._handler_connected_start_direct_event)
self._connection_fsm.add_handler(DriverConnectionState.CONNECTED, DriverEvent.STOP_DIRECT, self._handler_connected_stop_direct_event)
# Start state machine.
self._connection_fsm.start(DriverConnectionState.UNCONFIGURED)
self._pre_da_config = {}
self._startup_config = {}
#############################################################
# Device connection interface.
#############################################################
def initialize(self, *args, **kwargs):
"""
Initialize driver connection, bringing communications parameters
into unconfigured state (no connection object).
@raises InstrumentStateException if command not allowed in current state
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.INITIALIZE, *args, **kwargs)
def configure(self, *args, **kwargs):
"""
Configure the driver for communications with the device via
port agent / logger (valid but unconnected connection object).
@param arg[0] comms config dict.
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentParameterException if missing comms or invalid config dict.
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.CONFIGURE, *args, **kwargs)
def connect(self, *args, **kwargs):
"""
Establish communications with the device via port agent / logger
(connected connection object).
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentConnectionException if the connection failed.
"""
# Forward event and argument to the connection FSM.
result = self._connection_fsm.on_event(DriverEvent.CONNECT, *args, **kwargs)
init_config = {}
if len(args) > 0 and isinstance(args[0], dict):
init_config = args[0]
self.set_init_params(init_config)
return result
def disconnect(self, *args, **kwargs):
"""
Disconnect from device via port agent / logger.
#.........这里部分代码省略.........
示例6: Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
########################################################################
def _handler_unknown_enter(self, *args, **kwargs):
"""
Enter unknown state.
"""
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
def _handler_unknown_exit(self, *args, **kwargs):
"""
Exit unknown state.
"""
pass
def _handler_unknown_discover(self, *args, **kwargs):
"""
Discover current state
@retval (next_state, result)
"""
return (ProtocolState.COMMAND, ResourceAgentState.IDLE)
########################################################################
# Command handlers.
########################################################################
def _handler_command_enter(self, *args, **kwargs):
"""
Enter command state.
@throws InstrumentTimeoutException if the device cannot be woken.
@throws InstrumentProtocolException if the update commands and not recognized.
"""
self._protocol_fsm.on_event(DriverEvent.INIT_PARAMS)
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
def _handler_command_set(self, *args, **kwargs):
"""
Set parameter
"""
next_state = None
result = None
self._set_params(*args, **kwargs)
log.debug("_handler_command_set: result: %s", result)
return (next_state, result)
def _handler_command_exit(self, *args, **kwargs):
"""
Exit command state.
"""
pass
def _handler_command_start_direct(self):
"""
Start direct access
"""
next_state = ProtocolState.DIRECT_ACCESS
next_agent_state = ResourceAgentState.DIRECT_ACCESS
result = None
log.debug("_handler_command_start_direct: entering DA mode")
示例7: Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
from the observatory. However, because so much data arrives here that
is not applicable, the add_to_buffer method has been overridden to do
nothing.
@param data: bytes to add to the buffer
"""
# Update the line and prompt buffers; first acquire mutex.
promptbuf_mutex.acquire()
self._linebuf += data
self._promptbuf += data
promptbuf_mutex.release()
self._last_data_timestamp = time.time()
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Invoke
this driver's _my_add_to_buffer, or pass it to extract_sample
with the appropriate particle objects and REGEXes. We need to invoke
_my_add_to_buffer, because we've overridden the base class
add_to_buffer that is called from got_data(). The reason is explained
in comments in _my_add_to_buffer.
"""
log.debug("_got_chunk_: %s", chunk)
"""
If we're in leveling mode, use a different got_chunk
"""
if (self._protocol_fsm.get_current_state() == ProtocolState.LEVELING):
log.error("~~~~~~~~~~~~~~~~~~~~~~~~~ YAHOOO!!!! LEVELING CHUNK: %s", chunk)
if (self.leveling_regex.match(chunk)):
self._protocol_fsm.on_event(ProtocolEvent.LEVELING_COMPLETE)
return
else:
log.error("!!!!!!!!!!!!!!!!!!!! NOOOO!!")
if (self.cmd_rsp_regex.match(chunk) \
#or self.signon_regex.match(chunk) \ # currently not using the signon chunk
or self.status_01_regex.match(chunk) \
or self.status_02_regex.match(chunk)):
self._my_add_to_buffer(chunk)
log.error("++++++++++++++++++++++++ Adding CHUNK: %s to buffer", chunk)
else:
if not self._extract_sample(LILYDataParticle,
self.data_regex,
chunk, timestamp):
raise InstrumentProtocolException("Unhandled chunk")
def _build_command(self, cmd, *args, **kwargs):
command = cmd + NEWLINE
log.debug("_build_command: command is: %s", command)
return command
def _parse_data_on_off_resp(self, response, prompt):
log.debug("_parse_data_on_off_resp: response: %r; prompt: %s", response, prompt)
return response.lily_command_response
def _parse_status_01_resp(self, response, prompt):
log.debug("_parse_status_01_resp: response: %r; prompt: %s", response, prompt)
return response.lily_status_response
def _parse_status_02_resp(self, response, prompt):
log.debug("_parse_status_02_resp: response: %r; prompt: %s", response, prompt)
示例8: SingleConnectionInstrumentDriver
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.STOP_AUTOSAMPLE, self._handler_connected_protocol_event
)
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.TEST, self._handler_connected_protocol_event
)
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.CALIBRATE, self._handler_connected_protocol_event
)
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.EXECUTE_DIRECT, self._handler_connected_protocol_event
)
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.START_DIRECT, self._handler_connected_protocol_event
)
self._connection_fsm.add_handler(
DriverConnectionState.CONNECTED, DriverEvent.STOP_DIRECT, self._handler_connected_protocol_event
)
# Start state machine.
self._connection_fsm.start(DriverConnectionState.UNCONFIGURED)
#############################################################
# Device connection interface.
#############################################################
def initialize(self, *args, **kwargs):
"""
Initialize driver connection, bringing communications parameters
into unconfigured state (no connection object).
@raises InstrumentStateException if command not allowed in current state
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.INITIALIZE, *args, **kwargs)
def configure(self, *args, **kwargs):
"""
Configure the driver for communications with the device via
port agent / logger (valid but unconnected connection object).
@param arg[0] comms config dict.
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentParameterException if missing comms or invalid config dict.
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.CONFIGURE, *args, **kwargs)
def connect(self, *args, **kwargs):
"""
Establish communications with the device via port agent / logger
(connected connection object).
@raises InstrumentStateException if command not allowed in current state
@throws InstrumentConnectionException if the connection failed.
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.CONNECT, *args, **kwargs)
def disconnect(self, *args, **kwargs):
"""
Disconnect from device via port agent / logger.
@raises InstrumentStateException if command not allowed in current state
"""
# Forward event and argument to the connection FSM.
return self._connection_fsm.on_event(DriverEvent.DISCONNECT, *args, **kwargs)
#############################################################
示例9: SatlanticPARInstrumentProtocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
class SatlanticPARInstrumentProtocol(CommandResponseInstrumentProtocol):
"""The instrument protocol classes to deal with a Satlantic PAR sensor.
The protocol is a very simple command/response protocol with a few show
commands and a few set commands.
Note protocol state machine must be called "self._protocol_fsm"
@todo Check for valid state transitions and handle requests appropriately
possibly using better exceptions from the fsm.on_event() method
"""
def __init__(self, callback=None):
CommandResponseInstrumentProtocol.__init__(self, Prompt, EOLN, callback)
self.write_delay = WRITE_DELAY
self._last_data_timestamp = None
self.eoln = EOLN
self._protocol_fsm = InstrumentFSM(
PARProtocolState, PARProtocolEvent, PARProtocolEvent.ENTER, PARProtocolEvent.EXIT
)
self._protocol_fsm.add_handler(PARProtocolState.UNKNOWN, PARProtocolEvent.ENTER, self._handler_unknown_enter)
self._protocol_fsm.add_handler(
PARProtocolState.UNKNOWN, PARProtocolEvent.DISCOVER, self._handler_unknown_discover
)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND, PARProtocolEvent.ENTER, self._handler_command_enter)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND, PARProtocolEvent.GET, self._handler_command_get)
self._protocol_fsm.add_handler(PARProtocolState.COMMAND, PARProtocolEvent.SET, self._handler_command_set)
self._protocol_fsm.add_handler(
PARProtocolState.COMMAND, PARProtocolEvent.START_AUTOSAMPLE, self._handler_command_start_autosample
)
self._protocol_fsm.add_handler(
PARProtocolState.COMMAND, PARProtocolEvent.START_POLL, self._handler_command_start_poll
)
self._protocol_fsm.add_handler(
PARProtocolState.COMMAND, PARProtocolEvent.START_DIRECT, self._handler_command_start_direct
)
self._protocol_fsm.add_handler(
PARProtocolState.AUTOSAMPLE, PARProtocolEvent.ENTER, self._handler_autosample_enter
)
self._protocol_fsm.add_handler(
PARProtocolState.AUTOSAMPLE, PARProtocolEvent.START_POLL, self._handler_autosample_start_poll
)
self._protocol_fsm.add_handler(
PARProtocolState.AUTOSAMPLE, PARProtocolEvent.STOP_AUTOSAMPLE, self._handler_autosample_stop_autosample
)
self._protocol_fsm.add_handler(
PARProtocolState.AUTOSAMPLE, PARProtocolEvent.RESET, self._handler_autosample_reset
)
self._protocol_fsm.add_handler(PARProtocolState.POLL, PARProtocolEvent.ENTER, self._handler_poll_enter)
self._protocol_fsm.add_handler(
PARProtocolState.POLL, PARProtocolEvent.START_AUTOSAMPLE, self._handler_poll_start_autosample
)
self._protocol_fsm.add_handler(PARProtocolState.POLL, PARProtocolEvent.STOP_POLL, self._handler_poll_stop_poll)
self._protocol_fsm.add_handler(
PARProtocolState.POLL, PARProtocolEvent.ACQUIRE_SAMPLE, self._handler_poll_acquire_sample
)
self._protocol_fsm.add_handler(PARProtocolState.POLL, PARProtocolEvent.RESET, self._handler_poll_reset)
self._protocol_fsm.add_handler(
PARProtocolState.DIRECT_ACCESS, PARProtocolEvent.ENTER, self._handler_direct_access_enter
)
self._protocol_fsm.add_handler(
PARProtocolState.DIRECT_ACCESS, PARProtocolEvent.EXECUTE_DIRECT, self._handler_direct_access_execute_direct
)
self._protocol_fsm.add_handler(
PARProtocolState.DIRECT_ACCESS, PARProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct
)
self._protocol_fsm.start(PARProtocolState.UNKNOWN)
self._add_build_handler(Command.SET, self._build_set_command)
self._add_build_handler(Command.GET, self._build_param_fetch_command)
self._add_build_handler(Command.SAVE, self._build_exec_command)
self._add_build_handler(Command.EXIT, self._build_exec_command)
self._add_build_handler(Command.EXIT_AND_RESET, self._build_exec_command)
self._add_build_handler(Command.SWITCH_TO_AUTOSAMPLE, self._build_control_command)
self._add_build_handler(Command.RESET, self._build_control_command)
self._add_build_handler(Command.BREAK, self._build_multi_control_command)
self._add_build_handler(Command.SAMPLE, self._build_control_command)
self._add_build_handler(Command.SWITCH_TO_POLL, self._build_control_command)
self._add_response_handler(Command.GET, self._parse_get_response)
self._add_response_handler(Command.SET, self._parse_set_response)
self._add_response_handler(Command.SWITCH_TO_POLL, self._parse_silent_response)
self._add_response_handler(Command.SAMPLE, self._parse_sample_poll_response, PARProtocolState.POLL)
self._add_response_handler(Command.SAMPLE, self._parse_cmd_prompt_response, PARProtocolState.COMMAND)
self._add_response_handler(Command.BREAK, self._parse_silent_response, PARProtocolState.COMMAND)
self._add_response_handler(Command.BREAK, self._parse_header_response, PARProtocolState.POLL)
self._add_response_handler(Command.BREAK, self._parse_header_response, PARProtocolState.AUTOSAMPLE)
self._add_response_handler(Command.RESET, self._parse_silent_response, PARProtocolState.COMMAND)
self._add_response_handler(Command.RESET, self._parse_reset_response, PARProtocolState.POLL)
self._add_response_handler(Command.RESET, self._parse_reset_response, PARProtocolState.AUTOSAMPLE)
self._param_dict.add(
Parameter.MAXRATE, r"Maximum Frame Rate:\s+(\d+) Hz", lambda match: int(match.group(1)), self._int_to_string
)
def _filter_capabilities(self, events):
"""
"""
#.........这里部分代码省略.........
示例10: SBE16Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
result = self._param_dict.get_config()
# If not all params, confirm a list or tuple of params to retrieve.
# Raise if not a list or tuple.
# Retireve each key in the list, raise if any are invalid.
else:
if not isinstance(params, (list, tuple)):
raise InstrumentParameterException('Get argument not a list or tuple.')
result = {}
for key in params:
try:
val = self._param_dict.get(key)
result[key] = val
except KeyError:
raise InstrumentParameterException(('%s is not a valid parameter.' % key))
return (next_state, result)
########################################################################
# Test handlers.
########################################################################
def _handler_test_enter(self, *args, **kwargs):
"""
Enter test state. Setup the secondary call to run the tests.
"""
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
# Forward the test event again to run the test handler and
# switch back to command mode afterward.
Timer(1, lambda: self._protocol_fsm.on_event(ProtocolEvent.RUN_TEST)).start()
def _handler_test_exit(self, *args, **kwargs):
"""
Exit test state.
"""
pass
def _handler_test_run_tests(self, *args, **kwargs):
"""
Run test routines and validate results.
@throws InstrumentTimeoutException if device cannot be woken for command.
@throws InstrumentProtocolException if command misunderstood or
incorrect prompt received.
"""
next_state = None
result = None
tc_pass = False
tt_pass = False
#tp_pass = False
tc_result = None
tt_result = None
#tp_result = None
test_result = {}
try:
tc_pass, tc_result = self._do_cmd_resp(SBE16Command.TC, timeout=200)
tt_pass, tt_result = self._do_cmd_resp(SBE16Command.TT, timeout=200)
# DHE: our SBE16 has no pressure sensor
#tp_pass, tp_result = self._do_cmd_resp(SBE16Command.TP, timeout=200)
示例11: SamiProtocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import InstrumentFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.InstrumentFSM import on_event [as 别名]
#.........这里部分代码省略.........
def _handler_unknown_exit(self, *args, **kwargs):
"""
Exit unknown state.
"""
pass
def _handler_unknown_discover(self, *args, **kwargs):
"""
Discover current state; can be UNKNOWN, COMMAND or POLLED_SAMPLE
@retval (next_state, result)
"""
next_state = None
result = None
log.debug("_handler_unknown_discover: starting discover")
(next_state, next_agent_state) = self._discover()
log.debug("_handler_unknown_discover: next agent state: %s", next_agent_state)
return (next_state, (next_agent_state, result))
########################################################################
# Waiting handlers.
########################################################################
def _handler_waiting_enter(self, *args, **kwargs):
"""
Enter discover state.
"""
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
# Test to determine what state we truly are in, command or unknown.
self._protocol_fsm.on_event(ProtocolEvent.DISCOVER)
def _handler_waiting_exit(self, *args, **kwargs):
"""
Exit discover state.
"""
pass
def _handler_waiting_discover(self, *args, **kwargs):
"""
Discover current state; can be UNKNOWN or COMMAND
@retval (next_state, result)
"""
# Exit states can be either COMMAND or back to UNKNOWN.
next_state = None
next_agent_state = None
result = None
# try to discover our state
count = 1
while count <= 6:
log.debug("_handler_waiting_discover: starting discover")
(next_state, next_agent_state) = self._discover()
if next_state is ProtocolState.COMMAND:
log.debug("_handler_waiting_discover: discover succeeded")
log.debug("_handler_waiting_discover: next agent state: %s", next_agent_state)
return (next_state, (next_agent_state, result))
else:
log.debug("_handler_waiting_discover: discover failed, attempt %d of 3", count)
count += 1
time.sleep(20)
log.debug("_handler_waiting_discover: discover failed")