本文整理汇总了Python中mi.core.instrument.instrument_fsm.ThreadSafeFSM.on_event方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadSafeFSM.on_event方法的具体用法?Python ThreadSafeFSM.on_event怎么用?Python ThreadSafeFSM.on_event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mi.core.instrument.instrument_fsm.ThreadSafeFSM
的用法示例。
在下文中一共展示了ThreadSafeFSM.on_event方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import ThreadSafeFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.ThreadSafeFSM import on_event [as 别名]
#.........这里部分代码省略.........
def _handler_unknown_discover(self, *args, **kwargs):
"""
Discover current state; can only be COMMAND (instrument has no actual AUTOSAMPLE mode).
@retval (next_state, result), (ProtocolState.COMMAND, None) if successful.
"""
(protocol_state, agent_state) = self._discover()
# If we are just starting up and we land in command mode then our state should
# be idle
if(agent_state == ResourceAgentState.COMMAND):
agent_state = ResourceAgentState.IDLE
log.debug("_handler_unknown_discover: state = %s", protocol_state)
return (protocol_state, agent_state)
########################################################################
# Clock Sync handlers.
# Not much to do in this state except sync the clock then transition
# back to autosample. When in command mode we don't have to worry about
# stopping the scheduler so we just sync the clock without state
# transitions
########################################################################
def _handler_sync_clock_enter(self, *args, **kwargs):
"""
Enter sync clock state.
"""
# Tell driver superclass to send a state change event.
# Superclass will query the state.
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
self._protocol_fsm.on_event(ProtocolEvent.CLOCK_SYNC)
def _handler_sync_clock_sync(self, *args, **kwargs):
"""
Sync the clock
"""
next_state = ProtocolState.AUTOSAMPLE
next_agent_state = ResourceAgentState.STREAMING
result = None
self._sync_clock()
self._async_agent_state_change(ResourceAgentState.STREAMING)
return(next_state,(next_agent_state, result))
########################################################################
# Command handlers.
# just implemented to make DA possible, instrument has no actual command mode
########################################################################
def _handler_command_enter(self, *args, **kwargs):
"""
Enter command state.
"""
self._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_exit(self, *args, **kwargs):
"""
Exit command state.
示例2: McLaneProtocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import ThreadSafeFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.ThreadSafeFSM import on_event [as 别名]
#.........这里部分代码省略.........
result = 'clear successful'
# if Status, nothing to do
return next_state, (next_state, result)
def _handler_clear_flush(self, *args, **kwargs):
"""
Attempt to recover from failed attempt to clear by flushing home port. Only try once.
"""
next_state = None
result = []
log.info('Attempting to flush main port during clear')
if self._second_attempt:
next_state = ProtocolState.RECOVERY
result = 'unable to flush main port during clear'
else:
self._second_attempt = True
self._do_cmd_flush()
result = 'attempting to flush main port a second time'
return next_state, (next_state, result)
########################################################################
# Command handlers.
# just implemented to make DA possible, instrument has no actual command mode
########################################################################
def _handler_command_enter(self, *args, **kwargs):
"""
Enter command state.
"""
# Command device to update parameters and send a config change event if needed.
self._update_params()
self._protocol_fsm.on_event(ProtocolEvent.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_init_params(self, *args, **kwargs):
"""
Setup initial parameters.
"""
next_state = None
result = self._init_params()
return next_state, (next_state, result)
def _handler_command_set(self, *args, **kwargs):
"""
Set instrument parameters
"""
next_state = None
result = []
startup = False
try:
params = args[0]
except IndexError:
raise InstrumentParameterException('set command requires a parameter dictionary.')
try:
startup = args[1]
except IndexError:
pass
示例3: SingleConnectionInstrumentDriver
# 需要导入模块: from mi.core.instrument.instrument_fsm import ThreadSafeFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.ThreadSafeFSM 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 = ThreadSafeFSM(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 = {}
# Idempotency flag for lost connections.
# This set to false when a connection is established to
# allow for lost callback to become activated.
self._connection_lost = True
#############################################################
# 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)
#.........这里部分代码省略.........
示例4: Protocol
# 需要导入模块: from mi.core.instrument.instrument_fsm import ThreadSafeFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.ThreadSafeFSM import on_event [as 别名]
#.........这里部分代码省略.........
raise InstrumentParameterException('Parameter %s value [%d] is out of range [%d %d]' %
(key, value, MIN_SAMPLE_RATE, MAX_SAMPLE_RATE))
startup = False
try:
startup = args[1]
except IndexError:
pass
self._set_params(input_params, startup)
return None, None
# return None, (None, None)
def _handler_command_autosample(self, *args, **kwargs):
"""
Begin autosample.
"""
return ProtocolState.AUTOSAMPLE, (ResourceAgentState.STREAMING, None)
def _handler_command_start_direct(self, *args, **kwargs):
"""
"""
return ProtocolState.DIRECT_ACCESS, (ResourceAgentState.DIRECT_ACCESS, None)
########################################################################
# Event handlers for AUTOSAMPLE state.
########################################################################
def _handler_autosample_enter(self, *args, **kwargs):
"""
Start auto polling the temperature sensors.
"""
self._driver_event(DriverAsyncEvent.STATE_CHANGE)
self._protocol_fsm.on_event(ProtocolEvent.ACQUIRE_SAMPLE)
job_name = ScheduledJob.SAMPLE
config = {
DriverConfigKey.SCHEDULER: {
job_name: {
DriverSchedulerConfigKey.TRIGGER: {
DriverSchedulerConfigKey.TRIGGER_TYPE: TriggerType.INTERVAL,
DriverSchedulerConfigKey.SECONDS: self._param_dict.get(Parameter.SAMPLE_INTERVAL)
}
}
}
}
self.set_init_params(config)
self._add_scheduler_event(ScheduledJob.SAMPLE, ProtocolEvent.ACQUIRE_SAMPLE)
def _handler_autosample_exit(self, *args, **kwargs):
"""
Stop autosampling - remove the scheduled autosample
"""
if self._scheduler is not None:
try:
self._remove_scheduler(ScheduledJob.SAMPLE)
except KeyError:
log.debug('_remove_scheduler count not find: %s', ScheduledJob.SAMPLE)
def _handler_sample(self, *args, **kwargs):
"""
Poll the three temperature probes for current temperature readings.
"""
for i in self._units:
self._do_command(Command.READ, i)
示例5: SingleConnectionInstrumentDriver
# 需要导入模块: from mi.core.instrument.instrument_fsm import ThreadSafeFSM [as 别名]
# 或者: from mi.core.instrument.instrument_fsm.ThreadSafeFSM import on_event [as 别名]
class SingleConnectionInstrumentDriver(InstrumentDriver):
"""
Base class for instrument drivers with a single device connection.
Provides connection state logic for single connection drivers. This is
the base class for the majority of driver implementation classes.
"""
__metaclass__ = META_LOGGER
def __init__(self, event_callback, refdes=None):
"""
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 one and only instrument connection.
# Exists in the connected state.
self._connection = None
# The one and only instrument protocol.
self._protocol = None
# Consul
self.consul = consulate.Consul()
# Reference Designator to the port agent service
self.refdes = refdes
# Build connection state machine.
self._connection_fsm = ThreadSafeFSM(DriverConnectionState,
DriverEvent,
DriverEvent.ENTER,
DriverEvent.EXIT)
# Add handlers for all events.
handlers = {
DriverState.UNCONFIGURED: [
(DriverEvent.ENTER, self._handler_unconfigured_enter),
(DriverEvent.EXIT, self._handler_unconfigured_exit),
(DriverEvent.INITIALIZE, self._handler_unconfigured_initialize),
(DriverEvent.CONFIGURE, self._handler_unconfigured_configure),
],
DriverConnectionState.DISCONNECTED: [
(DriverEvent.ENTER, self._handler_disconnected_enter),
(DriverEvent.EXIT, self._handler_disconnected_exit),
(DriverEvent.INITIALIZE, self._handler_disconnected_initialize),
(DriverEvent.CONFIGURE, self._handler_disconnected_configure),
(DriverEvent.CONNECT, self._handler_disconnected_connect),
],
DriverConnectionState.CONNECTED: [
(DriverEvent.ENTER, self._handler_connected_enter),
(DriverEvent.EXIT, self._handler_connected_exit),
(DriverEvent.DISCONNECT, self._handler_connected_disconnect),
(DriverEvent.CONNECTION_LOST, self._handler_connected_connection_lost),
(DriverEvent.DISCOVER, self._handler_connected_protocol_event),
(DriverEvent.GET, self._handler_connected_protocol_event),
(DriverEvent.SET, self._handler_connected_protocol_event),
(DriverEvent.EXECUTE, self._handler_connected_protocol_event),
(DriverEvent.FORCE_STATE, self._handler_connected_protocol_event),
(DriverEvent.START_DIRECT, self._handler_connected_start_direct_event),
(DriverEvent.STOP_DIRECT, self._handler_connected_stop_direct_event),
],
}
for state in handlers:
for event, handler in handlers[state]:
self._connection_fsm.add_handler(state, event, handler)
self._pre_da_config = {}
self._startup_config = {}
# Idempotency flag for lost connections.
# This set to false when a connection is established to
# allow for lost callback to become activated.
self._connection_lost = True
# Autoconnect flag
# Set this to false to disable autoconnect
self._autoconnect = True
self._reconnect_interval = STARTING_RECONNECT_INTERVAL
self._max_reconnect_interval = MAXIMUM_RECONNECT_INTERVAL
# 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):
#.........这里部分代码省略.........