本文整理汇总了Python中mi.core.instrument.protocol_param_dict.ProtocolParameterDict.add方法的典型用法代码示例。如果您正苦于以下问题:Python ProtocolParameterDict.add方法的具体用法?Python ProtocolParameterDict.add怎么用?Python ProtocolParameterDict.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mi.core.instrument.protocol_param_dict.ProtocolParameterDict
的用法示例。
在下文中一共展示了ProtocolParameterDict.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
def test_get(self):
"""
test getting values with expiration
"""
# from mi.core.exceptions import InstrumentParameterExpirationException
pd = ProtocolParameterDict()
# No expiration, should work just fine
pd.add('noexp', r'', None, None, expiration=None)
pd.add('zeroexp', r'', None, None, expiration=0)
pd.add('lateexp', r'', None, None, expiration=2)
###
# Set and get with no expire
###
pd.set_value('noexp', 1)
self.assertEqual(pd.get('noexp'), 1)
###
# Set and get with a 0 expire
###
basetime = pd.get_current_timestamp()
pd.set_value('zeroexp', 2)
# We should fail because we are calculating exp against current time
with self.assertRaises(InstrumentParameterExpirationException):
pd.get('zeroexp')
# Should succeed because exp is calculated using basetime
self.assertEqual(pd.get('zeroexp', basetime), 2)
###
# Set and get with a delayed expire
###
basetime = pd.get_current_timestamp()
futuretime = pd.get_current_timestamp(3)
self.assertGreater(futuretime - basetime, 3)
pd.set_value('lateexp', 2)
# Success because data is not expired
self.assertEqual(pd.get('lateexp', basetime), 2)
# Fail because data is expired (simulated three seconds from now)
with self.assertRaises(InstrumentParameterExpirationException):
pd.get('lateexp', futuretime)
示例2: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(CommandResponseInstrumentProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
last_sample = ''
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.
"""
# Construct protocol superclass.
CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
self._protocol_fsm = ThreadSafeFSM(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.EXIT, self._handler_unknown_exit)
self._protocol_fsm.add_handler(ProtocolState.UNKNOWN, ProtocolEvent.DISCOVER, self._handler_unknown_discover)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.ENTER, self._handler_command_enter)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.EXIT, self._handler_command_exit)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.ACQUIRE_SAMPLE, self._handler_acquire_sample)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.START_DIRECT, self._handler_command_start_direct)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.CLOCK_SYNC, self._handler_command_sync_clock)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.GET, self._handler_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_start_autosample)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.FLASH_STATUS, self._handler_flash_status)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.ACQUIRE_STATUS, self._handler_acquire_status)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.ENTER, self._handler_autosample_enter)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.EXIT, self._handler_autosample_exit)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.STOP_AUTOSAMPLE, self._handler_autosample_stop_autosample)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.ACQUIRE_SAMPLE, self._handler_acquire_sample)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.CLOCK_SYNC, self._handler_autosample_sync_clock)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.GET, self._handler_get)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.FLASH_STATUS, self._handler_flash_status)
self._protocol_fsm.add_handler(ProtocolState.AUTOSAMPLE, ProtocolEvent.ACQUIRE_STATUS, self._handler_acquire_status)
# We setup a new state for clock sync because then we could use the state machine so the autosample scheduler
# is disabled before we try to sync the clock. Otherwise there could be a race condition introduced when we
# are syncing the clock and the scheduler requests a sample.
self._protocol_fsm.add_handler(ProtocolState.SYNC_CLOCK, ProtocolEvent.ENTER, self._handler_sync_clock_enter)
self._protocol_fsm.add_handler(ProtocolState.SYNC_CLOCK, ProtocolEvent.CLOCK_SYNC, self._handler_sync_clock_sync)
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.EXECUTE_DIRECT, self._handler_direct_access_execute_direct)
self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct)
# Add build handlers for device commands.
self._add_build_handler(Command.GET_CLOCK, self._build_simple_command)
self._add_build_handler(Command.SET_CLOCK, self._build_set_clock_command)
self._add_build_handler(Command.D, self._build_simple_command)
self._add_build_handler(Command.GO, self._build_simple_command)
self._add_build_handler(Command.STOP, self._build_simple_command)
self._add_build_handler(Command.FS, self._build_simple_command)
self._add_build_handler(Command.STAT, self._build_simple_command)
# Add response handlers for device commands.
self._add_response_handler(Command.GET_CLOCK, self._parse_clock_response)
self._add_response_handler(Command.SET_CLOCK, self._parse_clock_response)
self._add_response_handler(Command.FS, self._parse_fs_response)
self._add_response_handler(Command.STAT, self._parse_common_response)
# Construct the parameter dictionary containing device parameters,
# current parameter values, and set formatting functions.
self._build_param_dict()
self._build_command_dict()
self._build_driver_dict()
self._chunker = StringChunker(Protocol.sieve_function)
self._add_scheduler_event(ScheduledJob.ACQUIRE_STATUS, ProtocolEvent.ACQUIRE_STATUS)
self._add_scheduler_event(ScheduledJob.CLOCK_SYNC, ProtocolEvent.CLOCK_SYNC)
# Start state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
@staticmethod
def sieve_function(raw_data):
"""
The method that splits samples and status
"""
matchers = []
return_list = []
matchers.append(METBK_SampleDataParticle.regex_compiled())
matchers.append(METBK_StatusDataParticle.regex_compiled())
for matcher in matchers:
for match in matcher.finditer(raw_data):
return_list.append((match.start(), match.end()))
#.........这里部分代码省略.........
示例3: McLaneProtocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class McLaneProtocol(CommandResponseInstrumentProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
# __metaclass__ = get_logging_metaclass(log_level='debug')
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.
"""
# Construct protocol superclass.
CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
self._protocol_fsm = ThreadSafeFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT)
# Add event handlers for protocol state machine.
handlers = {
ProtocolState.UNKNOWN: [
(ProtocolEvent.ENTER, self._handler_unknown_enter),
(ProtocolEvent.DISCOVER, self._handler_unknown_discover),
],
ProtocolState.COMMAND: [
(ProtocolEvent.ENTER, self._handler_command_enter),
(ProtocolEvent.INIT_PARAMS, self._handler_command_init_params),
(ProtocolEvent.START_DIRECT, self._handler_command_start_direct),
(ProtocolEvent.CLOCK_SYNC, self._handler_sync_clock),
(ProtocolEvent.ACQUIRE_SAMPLE, self._handler_command_acquire),
# (ProtocolEvent.ACQUIRE_STATUS, self._handler_command_status),
(ProtocolEvent.CLEAR, self._handler_command_clear),
(ProtocolEvent.GET, self._handler_get),
(ProtocolEvent.SET, self._handler_command_set),
],
ProtocolState.FLUSH: [
(ProtocolEvent.ENTER, self._handler_flush_enter),
(ProtocolEvent.FLUSH, self._handler_flush_flush),
(ProtocolEvent.PUMP_STATUS, self._handler_flush_pump_status),
(ProtocolEvent.INSTRUMENT_FAILURE, self._handler_all_failure),
],
ProtocolState.FILL: [
(ProtocolEvent.ENTER, self._handler_fill_enter),
(ProtocolEvent.FILL, self._handler_fill_fill),
(ProtocolEvent.PUMP_STATUS, self._handler_fill_pump_status),
(ProtocolEvent.INSTRUMENT_FAILURE, self._handler_all_failure),
],
ProtocolState.CLEAR: [
(ProtocolEvent.ENTER, self._handler_clear_enter),
(ProtocolEvent.CLEAR, self._handler_clear_clear),
(ProtocolEvent.PUMP_STATUS, self._handler_clear_pump_status),
(ProtocolEvent.INSTRUMENT_FAILURE, self._handler_all_failure),
],
ProtocolState.RECOVERY: [
(ProtocolEvent.ENTER, self._handler_recovery_enter),
],
ProtocolState.DIRECT_ACCESS: [
(ProtocolEvent.ENTER, self._handler_direct_access_enter),
(ProtocolEvent.EXECUTE_DIRECT, self._handler_direct_access_execute_direct),
(ProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct),
],
}
for state in handlers:
for event, handler in handlers[state]:
self._protocol_fsm.add_handler(state, event, handler)
# Add build handlers for device commands - we are only using simple commands
for cmd in McLaneCommand.list():
self._add_build_handler(cmd, self._build_command)
# Add response handlers for device commands.
# self._add_response_handler(McLaneCommand.BATTERY, self._parse_battery_response)
# self._add_response_handler(McLaneCommand.CLOCK, self._parse_clock_response)
# self._add_response_handler(McLaneCommand.PORT, self._parse_port_response)
# Construct the parameter dictionary containing device parameters,
# current parameter values, and set formatting functions.
self._build_param_dict()
self._build_command_dict()
self._build_driver_dict()
self._chunker = StringChunker(McLaneProtocol.sieve_function)
self._add_scheduler_event(ScheduledJob.CLOCK_SYNC, ProtocolEvent.CLOCK_SYNC)
# Start state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
self._sent_cmds = None
# TODO - reset next_port on mechanical refresh of the PPS filters - how is the driver notified?
# TODO - need to persist state for next_port to save driver restart
self.next_port = 1 # next available port
self._second_attempt = False
@staticmethod
def sieve_function(raw_data):
#.........这里部分代码省略.........
示例4: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(SamiProtocol):
"""
Instrument protocol class
Subclasses SamiProtocol and CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
SamiProtocol.__init__(self, prompts, newline, driver_event)
## Continue building protocol state machine from SamiProtocol
self._protocol_fsm.add_handler(
ProtocolState.SCHEDULED_SAMPLE, ProtocolEvent.SUCCESS,
self._handler_sample_success)
self._protocol_fsm.add_handler(
ProtocolState.SCHEDULED_SAMPLE, ProtocolEvent.TIMEOUT,
self._handler_sample_timeout)
self._protocol_fsm.add_handler(
ProtocolState.POLLED_SAMPLE, ProtocolEvent.SUCCESS,
self._handler_sample_success)
self._protocol_fsm.add_handler(
ProtocolState.POLLED_SAMPLE, ProtocolEvent.TIMEOUT,
self._handler_sample_timeout)
# State state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
# build the chunker bot
self._chunker = StringChunker(Protocol.sieve_function)
@staticmethod
def sieve_function(raw_data):
"""
The method that splits samples
"""
return_list = []
sieve_matchers = [REGULAR_STATUS_REGEX_MATCHER,
CONTROL_RECORD_REGEX_MATCHER,
SAMI_SAMPLE_REGEX_MATCHER,
CONFIGURATION_REGEX_MATCHER,
ERROR_REGEX_MATCHER]
for matcher in sieve_matchers:
for match in matcher.finditer(raw_data):
return_list.append((match.start(), match.end()))
return return_list
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Pass it to extract_sample
with the appropriate particle objects and REGEXes.
"""
self._extract_sample(SamiRegularStatusDataParticle, REGULAR_STATUS_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(SamiControlRecordDataParticle, CONTROL_RECORD_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(PhsenSamiSampleDataParticle, SAMI_SAMPLE_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(PhsenConfigDataParticle, CONFIGURATION_REGEX_MATCHER, chunk, timestamp)
#########################################################################
## General (for POLLED and SCHEDULED states) Sample handlers.
#########################################################################
def _handler_sample_success(self, *args, **kwargs):
next_state = None
result = None
return (next_state, result)
def _handler_sample_timeout(self, ):
next_state = None
result = None
return (next_state, result)
####################################################################
# Build Parameter dictionary
####################################################################
def _build_param_dict(self):
"""
Populate the parameter dictionary with parameters.
For each parameter key, add match stirng, match lambda function,
and value formatting function for set commands.
"""
# Add parameter handlers to parameter dict.
self._param_dict = ProtocolParameterDict()
self._param_dict.add(Parameter.LAUNCH_TIME, CONFIGURATION_REGEX,
lambda match: int(match.group(1), 16),
lambda x: self._int_to_hexstring(x, 8),
type=ParameterDictType.INT,
startup_param=False,
#.........这里部分代码省略.........
示例5: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(CommandResponseInstrumentProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
self._protocol_fsm = ThreadSafeFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT)
# Add event handlers for protocol state machine.
handlers = {
ProtocolState.UNKNOWN: [
(ProtocolEvent.ENTER, self._handler_unknown_enter),
(ProtocolEvent.EXIT, self._handler_unknown_exit),
(ProtocolEvent.DISCOVER, self._handler_unknown_discover),
],
ProtocolState.COMMAND: [
(ProtocolEvent.ENTER, self._handler_command_enter),
(ProtocolEvent.EXIT, self._handler_command_exit),
(ProtocolEvent.START_DIRECT, self._handler_command_start_direct),
(ProtocolEvent.ACQUIRE_SAMPLE, self._handler_sample),
(ProtocolEvent.START_AUTOSAMPLE, self._handler_command_autosample),
(ProtocolEvent.GET, self._handler_get),
(ProtocolEvent.SET, self._handler_command_set),
],
ProtocolState.AUTOSAMPLE: [
(ProtocolEvent.ENTER, self._handler_autosample_enter),
(ProtocolEvent.ACQUIRE_SAMPLE, self._handler_sample),
(ProtocolEvent.STOP_AUTOSAMPLE, self._handler_autosample_stop),
(ProtocolEvent.EXIT, self._handler_autosample_exit),
],
ProtocolState.DIRECT_ACCESS: [
(ProtocolEvent.ENTER, self._handler_direct_access_enter),
(ProtocolEvent.EXIT, self._handler_direct_access_exit),
(ProtocolEvent.EXECUTE_DIRECT, self._handler_direct_access_execute_direct),
(ProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct),
],
}
for state in handlers:
for event, handler in handlers[state]:
self._protocol_fsm.add_handler(state, event, handler)
# Add build handlers for device commands - we are only using simple commands
for cmd in Command.list():
self._add_build_handler(cmd, self._build_command)
self._add_response_handler(cmd, self._check_command)
self._add_build_handler(Command.SETUP, self._build_setup_command)
self._add_response_handler(Command.READ_SETUP, self._read_setup_response_handler)
# Add response handlers for device commands.
# self._add_response_handler(Command.xyz, self._parse_xyz_response)
# Construct the parameter dictionary containing device parameters,
# current parameter values, and set formatting functions.
self._build_param_dict()
self._build_command_dict()
self._build_driver_dict()
self._chunker = StringChunker(Protocol.sieve_function)
# Start state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
self._sent_cmds = None
self.initialize_scheduler()
# unit identifiers - must match the setup command (SU31 - '1')
self._units = ['1', '2', '3']
self._setup = None # set by the read setup command handler for comparison to see if the config needs reset
@staticmethod
def sieve_function(raw_data):
"""
The method that splits samples and status
"""
matchers = []
return_list = []
matchers.append(D1000TemperatureDataParticle.regex_compiled())
for matcher in matchers:
for match in matcher.finditer(raw_data):
return_list.append((match.start(), match.end()))
if not return_list:
log.debug("sieve_function: raw_data=%r, return_list=%s", raw_data, return_list)
return return_list
def _got_chunk(self, chunk, timestamp):
#.........这里部分代码省略.........
示例6: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(McLaneProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
McLaneProtocol.__init__(self, prompts, newline, driver_event)
# TODO - reset next_port on mechanical refresh of the PPS filters - how is the driver notified?
# TODO - need to persist state for next_port to save driver restart
self.next_port = 1 # next available port
# get the following:
# - VERSION
# - CAPACITY (pump flow)
# - BATTERY
# - CODES (termination codes)
# - COPYRIGHT (termination codes)
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Pass it to extract_sample
with the appropriate particle objects and REGEXes.
"""
filling = self.get_current_state() == ProtocolState.FILL
log.debug("_got_chunk:\n%s", chunk)
sample_dict = self._extract_sample(
PPSDNSampleDataParticle, PPSDNSampleDataParticle.regex_compiled(), chunk, timestamp, publish=filling
)
if sample_dict:
self._linebuf = ""
self._promptbuf = ""
self._protocol_fsm.on_event(
ProtocolEvent.PUMP_STATUS, PPSDNSampleDataParticle.regex_compiled().search(chunk)
)
def _build_param_dict(self):
"""
Populate the parameter dictionary with XR-420 parameters.
For each parameter key add value formatting function for set commands.
"""
# The parameter dictionary.
self._param_dict = ProtocolParameterDict()
# Add parameter handlers to parameter dictionary for instrument configuration parameters.
self._param_dict.add(
Parameter.FLUSH_VOLUME,
r"Flush Volume: (.*)mL",
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_VOLUME,
units=Prefixes.MILLI + Units.LITER,
startup_param=True,
display_name="Flush Volume",
description="Amount of sea water to flush prior to taking sample: (10 - 20000)",
visibility=ParameterDictVisibility.IMMUTABLE,
)
self._param_dict.add(
Parameter.FLUSH_FLOWRATE,
r"Flush Flow Rate: (.*)mL/min",
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_RATE,
units=Prefixes.MILLI + Units.LITER + "/" + Units.MINUTE,
startup_param=True,
display_name="Flush Flow Rate",
description="Rate at which to flush: (100 - 250)",
visibility=ParameterDictVisibility.IMMUTABLE,
)
self._param_dict.add(
Parameter.FLUSH_MINFLOW,
r"Flush Min Flow: (.*)mL/min",
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_MIN_RATE,
units=Prefixes.MILLI + Units.LITER + "/" + Units.MINUTE,
startup_param=True,
display_name="Flush Minimum Flow",
description="If the flow rate falls below this value the flush will be terminated, "
"obstruction suspected: (75 - 100)",
visibility=ParameterDictVisibility.IMMUTABLE,
)
self._param_dict.add(
Parameter.FILL_VOLUME,
r"Fill Volume: (.*)mL",
None,
self._int_to_string,
type=ParameterDictType.INT,
#.........这里部分代码省略.........
示例7: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
#.........这里部分代码省略.........
build_handler = self._build_handlers.get(cmd, None)
if not callable(build_handler):
log.error('_do_cmd_no_resp: no handler for command: %s' % cmd)
raise InstrumentProtocolException(error_code=InstErrorCode.BAD_DRIVER_COMMAND)
cmd_line = build_handler(cmd, *args)
# Wakeup the device, timeout exception as needed
self._wakeup(timeout, connection=connection)
# Clear line and prompt buffers for result, then send command.
self._linebuf[connection] = ''
self._promptbuf[connection] = ''
self._send_data(cmd_line, write_delay, connection=connection)
def _do_cmd_direct(self, cmd, connection=None):
"""
Issue an untranslated command to the instrument. No response is handled
as a result of the command.
@param cmd The high level command to issue
"""
# Send command.
self._send_data(cmd, connection=connection)
########################################################################
# Incoming data (for parsing) callback.
########################################################################
def got_data(self, port_agent_packet, connection=None):
"""
Called by the instrument connection when data is available.
Append line and prompt buffers.
Also add data to the chunker and when received call got_chunk
to publish results.
"""
if connection is None:
raise InstrumentProtocolException('got_data: no connection supplied!')
data_length = port_agent_packet.get_data_length()
data = port_agent_packet.get_data()
timestamp = port_agent_packet.get_timestamp()
log.debug("Got Data: %r %r", connection, data)
log.debug("Add Port Agent Timestamp: %r %s", connection, timestamp)
if data_length > 0:
if self.get_current_state() == DriverProtocolState.DIRECT_ACCESS:
self._driver_event(DriverAsyncEvent.DIRECT_ACCESS, data)
self.add_to_buffer(data, connection=connection)
self.chunkers[connection].add_chunk(data, timestamp)
(timestamp, chunk) = self.chunkers[connection].get_next_data()
while chunk:
self._got_chunk(chunk, timestamp, connection=connection)
(timestamp, chunk) = self.chunkers[connection].get_next_data()
########################################################################
# Incoming raw data callback.
########################################################################
def got_raw(self, port_agent_packet, connection=None):
"""
Called by the port agent client when raw data is available, such as data
sent by the driver to the instrument, the instrument responses,etc.
"""
示例8: TestUnitProtocolParameterDict
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class TestUnitProtocolParameterDict(TestUnitStringsDict):
"""
Test cases for instrument driver class. Functions in this class provide
instrument driver unit tests and provide a tutorial on use of
the driver interface.
"""
__test__ = True
@staticmethod
def pick_byte2(input_val):
""" Get the 2nd byte as an example of something tricky and
arbitrary"""
val = int(input_val) >> 8
val &= 255
return val
def setUp(self):
self.param_dict = ProtocolParameterDict()
self.param_dict.add("foo", r'.*foo=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
direct_access=True,
startup_param=True,
default_value=10,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("bar", r'.*bar=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
direct_access=False,
startup_param=True,
default_value=15,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("baz", r'.*baz=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
direct_access=True,
default_value=20,
visibility=ParameterDictVisibility.DIRECT_ACCESS,
get_timeout=30,
set_timeout=40,
display_name="Baz",
description="The baz parameter",
type=ParameterDictType.INT,
units="nano-bazers",
value_description="Should be an integer between 2 and 2000")
self.param_dict.add("bat", r'.*bat=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
startup_param=False,
default_value=20,
visibility=ParameterDictVisibility.READ_ONLY,
get_timeout=10,
set_timeout=20,
display_name="Bat",
description="The bat parameter",
type=ParameterDictType.INT,
units="nano-batbit",
value_description="Should be an integer between 1 and 1000")
self.param_dict.add("qux", r'.*qux=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
startup_param=False,
visibility=ParameterDictVisibility.READ_ONLY)
self.param_dict.add("pho", r'.*qux=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
startup_param=False,
visibility=ParameterDictVisibility.IMMUTABLE)
self.param_dict.add("dil", r'.*qux=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
startup_param=False,
visibility=ParameterDictVisibility.IMMUTABLE)
self.param_dict.add("qut", r'.*qut=(\d+).*',
lambda match: int(match.group(1)),
lambda x: str(x),
direct_access=True,
default_value=[10, 100],
visibility=ParameterDictVisibility.DIRECT_ACCESS,
expiration=1,
get_timeout=10,
set_timeout=20,
display_name="Qut",
description="The qut list parameter",
type=ParameterDictType.LIST,
units="nano-qutters",
value_description="Should be a 2-10 element list of integers between 2 and 2000")
self.target_schema = {
"bar": {
"direct_access": False,
"get_timeout": 10,
"set_timeout": 10,
"startup": True,
"value": {
"default": 15
},
"visibility": "READ_WRITE",
#.........这里部分代码省略.........
示例9: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(CommandResponseInstrumentProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
__metaclass__ = META_LOGGER
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.
"""
# Construct protocol superclass.
CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
self._protocol_fsm = ThreadSafeFSM(ProtocolState, ProtocolEvent, ProtocolEvent.ENTER, ProtocolEvent.EXIT)
# Add event handlers for protocol state machine.
handlers = {
ProtocolState.UNKNOWN: [
(ProtocolEvent.ENTER, self._handler_generic_enter),
(ProtocolEvent.EXIT, self._handler_generic_exit),
(ProtocolEvent.DISCOVER, self._handler_unknown_discover),
],
ProtocolState.AUTOSAMPLE: [
(ProtocolEvent.ENTER, self._handler_autosample_enter),
(ProtocolEvent.EXIT, self._handler_generic_exit),
(ProtocolEvent.GET, self._handler_command_get),
(ProtocolEvent.STOP_AUTOSAMPLE, self._handler_autosample_stop_autosample),
(ProtocolEvent.VERY_LONG_COMMAND, self._very_long_command),
],
ProtocolState.COMMAND: [
(ProtocolEvent.ENTER, self._handler_command_enter),
(ProtocolEvent.EXIT, self._handler_generic_exit),
(ProtocolEvent.GET, self._handler_command_get),
(ProtocolEvent.SET, self._handler_command_set),
(ProtocolEvent.START_AUTOSAMPLE, self._handler_command_start_autosample),
(ProtocolEvent.VERY_LONG_COMMAND, self._very_long_command),
],
}
for state in handlers:
for event, handler in handlers[state]:
self._protocol_fsm.add_handler(state, event, handler)
# Construct the metadata dictionaries
self._build_command_dict()
self._build_driver_dict()
# Start state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
# set up scheduled event handling
self.initialize_scheduler()
self._schedulers = []
def _generate_particle(self, stream_name, count=1):
# we're faking it anyway, send these as fast as we can...
# the overall rate will be close enough
particle = VirtualParticle(stream_name, port_timestamp=0)
for x in range(count):
particle.contents["port_timestamp"] = ntplib.system_to_ntp_time(time.time())
self._driver_event(DriverAsyncEvent.SAMPLE, particle.generate())
time.sleep(0.001)
def _create_scheduler(self, stream_name, rate):
job_name = stream_name
if rate > 1:
interval = 1
else:
interval = 1 / rate
config = {
DriverConfigKey.SCHEDULER: {
job_name: {
DriverSchedulerConfigKey.TRIGGER: {
DriverSchedulerConfigKey.TRIGGER_TYPE: TriggerType.INTERVAL,
DriverSchedulerConfigKey.SECONDS: interval,
}
}
}
}
self.set_init_params(config)
self._schedulers.append(stream_name)
if rate > 1:
self._add_scheduler(stream_name, functools.partial(self._generate_particle, stream_name, count=rate))
else:
self._add_scheduler(stream_name, functools.partial(self._generate_particle, stream_name))
def _delete_all_schedulers(self):
for name in self._schedulers:
try:
self._remove_scheduler(name)
except:
#.........这里部分代码省略.........
示例10: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(McLaneProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
McLaneProtocol.__init__(self, prompts, newline, driver_event)
# TODO - reset next_port on mechanical refresh of the RAS bags - how is the driver notified?
# TODO - need to persist state for next_port to save driver restart
self.next_port = 1 # next available port
# TODO - on init, perform the following commands:
# - VERSION
# - CAPACITY (pump flow)
# - BATTERY
# - CODES (termination codes)
# - COPYRIGHT (termination codes)
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Pass it to extract_sample
with the appropriate particle objects and REGEXes.
"""
filling = self.get_current_state() == ProtocolState.FILL
log.debug("_got_chunk:\n%s", chunk)
sample_dict = self._extract_sample(RASFLSampleDataParticle,
RASFLSampleDataParticle.regex_compiled(), chunk, timestamp, publish=filling)
if sample_dict:
self._linebuf = ''
self._promptbuf = ''
self._protocol_fsm.on_event(ProtocolEvent.PUMP_STATUS,
RASFLSampleDataParticle.regex_compiled().search(chunk))
def _build_param_dict(self):
"""
Populate the parameter dictionary with XR-420 parameters.
For each parameter key add value formatting function for set commands.
"""
# The parameter dictionary.
self._param_dict = ProtocolParameterDict()
# Add parameter handlers to parameter dictionary for instrument configuration parameters.
self._param_dict.add(Parameter.FLUSH_VOLUME,
r'Flush Volume: (.*)mL',
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_VOLUME,
units='mL',
startup_param=True,
display_name="flush_volume",
visibility=ParameterDictVisibility.IMMUTABLE)
self._param_dict.add(Parameter.FLUSH_FLOWRATE,
r'Flush Flow Rate: (.*)mL/min',
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_RATE,
units='mL/min',
startup_param=True,
display_name="flush_flow_rate",
visibility=ParameterDictVisibility.IMMUTABLE)
self._param_dict.add(Parameter.FLUSH_MINFLOW,
r'Flush Min Flow: (.*)mL/min',
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FLUSH_MIN_RATE,
units='mL/min',
startup_param=True,
display_name="flush_min_flow",
visibility=ParameterDictVisibility.IMMUTABLE)
self._param_dict.add(Parameter.FILL_VOLUME,
r'Fill Volume: (.*)mL',
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FILL_VOLUME,
units='mL',
startup_param=True,
display_name="fill_volume",
visibility=ParameterDictVisibility.IMMUTABLE)
self._param_dict.add(Parameter.FILL_FLOWRATE,
r'Fill Flow Rate: (.*)mL/min',
None,
self._int_to_string,
type=ParameterDictType.INT,
default_value=FILL_RATE,
units='mL/min',
startup_param=True,
#.........这里部分代码省略.........
示例11: TestUnitProtocolParameterDict
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class TestUnitProtocolParameterDict(MiUnitTestCase):
@staticmethod
def pick_byte2(input):
""" Get the 2nd byte as an example of something tricky and
arbitrary"""
val = int(input) >> 8
val = val & 255
return val
"""
Test cases for instrument driver class. Functions in this class provide
instrument driver unit tests and provide a tutorial on use of
the driver interface.
"""
def setUp(self):
self.param_dict = ProtocolParameterDict()
self.param_dict.add("foo", r'.*foo=(\d+).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=True,
startup_param=True,
default_value=10,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("bar", r'.*bar=(\d+).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=False,
startup_param=True,
default_value=15,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("baz", r'.*baz=(\d+).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=True,
default_value=20,
visibility=ParameterDictVisibility.DIRECT_ACCESS)
self.param_dict.add("bat", r'.*bat=(\d+).*',
lambda match : int(match.group(1)),
lambda x : str(x),
startup_param=False,
default_value=20,
visibility=ParameterDictVisibility.READ_ONLY)
self.param_dict.add("qux", r'.*qux=(\d+).*',
lambda match : int(match.group(1)),
lambda x : str(x),
startup_param=False,
visibility=ParameterDictVisibility.READ_ONLY)
def test_get_direct_access_list(self):
"""
Test to see we can get a list of direct access parameters
"""
result = self.param_dict.get_direct_access_list()
self.assertTrue(isinstance(result, list))
self.assertEquals(len(result), 2)
self.assert_("foo" in result)
self.assert_("baz" in result)
def test_get_startup_list(self):
"""
Test to see we can get a list of direct access parameters
"""
result = self.param_dict.get_startup_list()
self.assertTrue(isinstance(result, list))
self.assertEquals(len(result), 2)
self.assert_("foo" in result)
self.assert_("bar" in result)
def test_set_default(self):
"""
Test setting a default value
"""
result = self.param_dict.get_config()
self.assertEquals(result["foo"], None)
self.param_dict.set_default("foo")
self.assertEquals(self.param_dict.get("foo"), 10)
self.param_dict.update("foo=1000")
self.assertEquals(self.param_dict.get("foo"), 1000)
self.param_dict.set_default("foo")
self.assertEquals(self.param_dict.get("foo"), 10)
self.assertRaises(ValueError, self.param_dict.set_default, "qux")
def test_update_many(self):
"""
Test updating of multiple variables from the same input
"""
sample_input = """
foo=100
bar=200, baz=300
"""
self.assertNotEquals(self.param_dict.get("foo"), 100)
self.assertNotEquals(self.param_dict.get("bar"), 200)
self.assertNotEquals(self.param_dict.get("baz"), 300)
result = self.param_dict.update_many(sample_input)
log.debug("result: %s", result)
self.assertEquals(result["foo"], True)
self.assertEquals(result["bar"], True)
self.assertEquals(result["baz"], True)
#.........这里部分代码省略.........
示例12: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(CommandResponseInstrumentProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
CommandResponseInstrumentProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
self._protocol_fsm = ThreadSafeFSM(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.EXIT, self._handler_unknown_exit)
self._protocol_fsm.add_handler(ProtocolState.UNKNOWN, ProtocolEvent.DISCOVER, self._handler_unknown_discover)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.ENTER, self._handler_command_enter)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.EXIT, self._handler_command_exit)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.START_DIRECT, self._handler_command_start_direct)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.CLOCK_SYNC, self._handler_sync_clock)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.GET, self._handler_get)
self._protocol_fsm.add_handler(ProtocolState.COMMAND, ProtocolEvent.SET, self._handler_command_set)
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.EXECUTE_DIRECT, self._handler_direct_access_execute_direct)
self._protocol_fsm.add_handler(ProtocolState.DIRECT_ACCESS, ProtocolEvent.STOP_DIRECT, self._handler_direct_access_stop_direct)
# Add build handlers for device commands.
self._add_build_handler(Command.BATTERY, self._build_simple_command)
# Add response handlers for device commands.
self._add_response_handler(Command.BATTERY, self._parse_battery_response)
# Construct the parameter dictionary containing device parameters,
# current parameter values, and set formatting functions.
self._build_param_dict()
self._build_command_dict()
self._build_driver_dict()
self._chunker = StringChunker(Protocol.sieve_function)
self._add_scheduler_event(ScheduledJob.CLOCK_SYNC, ProtocolEvent.CLOCK_SYNC)
# Start state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
@staticmethod
def sieve_function(raw_data):
"""
The method that splits samples and status
"""
matchers = []
return_list = []
matchers.append(RASFL_SampleDataParticle.regex_compiled())
for matcher in matchers:
for match in matcher.finditer(raw_data):
return_list.append((match.start(), match.end()))
"""
if return_list != []:
log.debug("sieve_function: raw_data=%s, return_list=%s" %(raw_data, return_list))
"""
return return_list
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Pass it to extract_sample
with the appropriate particle objects and REGEXes.
"""
log.debug("_got_chunk: chunk=%s" %chunk)
self._extract_sample(RASFL_SampleDataParticle, RASFL_SampleDataParticle.regex_compiled(), chunk, timestamp)
def _filter_capabilities(self, events):
"""
Return a list of currently available capabilities.
"""
return [x for x in events if Capability.has(x)]
########################################################################
# implement virtual methods from base class.
########################################################################
def apply_startup_params(self):
"""
Apply sample_interval startup parameter.
"""
config = self.get_startup_config()
log.debug("apply_startup_params: startup config = %s" %config)
#.........这里部分代码省略.........
示例13: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(MenuInstrumentProtocol):
"""
Instrument protocol class
Subclasses MenuInstrumentProtocol
"""
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)
@staticmethod
def sieve_function(raw_data):
""" The method that splits samples
"""
return_list = []
for match in SAMPLE_REGEX.finditer(raw_data):
return_list.append((match.start(), match.end()))
return return_list
def _go_to_root_menu(self):
""" Get back to the root menu, assuming we are in COMMAND mode.
Getting to command mode should be done before this method is called.
#.........这里部分代码省略.........
示例14: TestUnitProtocolParameterDict
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class TestUnitProtocolParameterDict(MiUnitTestCase):
"""
Test cases for instrument driver class. Functions in this class provide
instrument driver unit tests and provide a tutorial on use of
the driver interface.
"""
def setUp(self):
self.param_dict = ProtocolParameterDict()
self.param_dict.add("foo", r'.*foo=(\d*).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=True,
startup_param=True,
default_value=10,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("bar", r'.*bar=(\d*).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=False,
startup_param=True,
default_value=15,
visibility=ParameterDictVisibility.READ_WRITE)
self.param_dict.add("baz", r'.*baz=(\d*).*',
lambda match : int(match.group(1)),
lambda x : str(x),
direct_access=True,
default_value=20,
visibility=ParameterDictVisibility.DIRECT_ACCESS)
self.param_dict.add("bat", r'.*bat=(\d*).*',
lambda match : int(match.group(1)),
lambda x : str(x),
startup_param=False,
default_value=20,
visibility=ParameterDictVisibility.READ_ONLY)
self.param_dict.add("qux", r'.*qux=(\d*).*',
lambda match : int(match.group(1)),
lambda x : str(x),
startup_param=False,
visibility=ParameterDictVisibility.READ_ONLY)
def test_get_direct_access_list(self):
"""
Test to see we can get a list of direct access parameters
"""
result = self.param_dict.get_direct_access_list()
self.assertTrue(isinstance(result, list))
self.assertEquals(len(result), 2)
self.assert_("foo" in result)
self.assert_("baz" in result)
def test_get_startup_list(self):
"""
Test to see we can get a list of direct access parameters
"""
result = self.param_dict.get_startup_list()
self.assertTrue(isinstance(result, list))
self.assertEquals(len(result), 2)
self.assert_("foo" in result)
self.assert_("bar" in result)
def test_set_default(self):
"""
Test setting a default value
"""
result = self.param_dict.get_config()
self.assertEquals(result["foo"], None)
self.param_dict.set_default("foo")
self.assertEquals(self.param_dict.get("foo"), 10)
self.param_dict.update("foo=1000")
self.assertEquals(self.param_dict.get("foo"), 1000)
self.param_dict.set_default("foo")
self.assertEquals(self.param_dict.get("foo"), 10)
self.assertRaises(ValueError, self.param_dict.set_default, "qux")
def test_update_many(self):
"""
Test updating of multiple variables from the same input
"""
sample_input = """
foo=100
bar=200, baz=300
"""
self.assertNotEquals(self.param_dict.get("foo"), 100)
self.assertNotEquals(self.param_dict.get("bar"), 200)
self.assertNotEquals(self.param_dict.get("baz"), 300)
result = self.param_dict.update_many(sample_input)
log.debug("result: %s", result)
self.assertEquals(result["foo"], True)
self.assertEquals(result["bar"], True)
self.assertEquals(result["baz"], True)
self.assertEquals(self.param_dict.get("foo"), 100)
self.assertEquals(self.param_dict.get("bar"), 200)
self.assertEquals(self.param_dict.get("baz"), 300)
def test_visibility_list(self):
lst = self.param_dict.get_visibility_list(ParameterDictVisibility.READ_WRITE)
self.assertEquals(lst, ["foo", "bar"])
lst = self.param_dict.get_visibility_list(ParameterDictVisibility.DIRECT_ACCESS)
#.........这里部分代码省略.........
示例15: Protocol
# 需要导入模块: from mi.core.instrument.protocol_param_dict import ProtocolParameterDict [as 别名]
# 或者: from mi.core.instrument.protocol_param_dict.ProtocolParameterDict import add [as 别名]
class Protocol(SamiProtocol):
"""
Instrument protocol class
Subclasses CommandResponseInstrumentProtocol
"""
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.
"""
# Construct protocol superclass.
SamiProtocol.__init__(self, prompts, newline, driver_event)
# Build protocol state machine.
###
# most of these are defined in the base class with exception of handlers
# defined below that differ for the two instruments (what defines
# success and the timeout duration)
###
# this state would be entered whenever an ACQUIRE_SAMPLE event occurred
# while in the AUTOSAMPLE state and will last anywhere from a few
# seconds to ~12 minutes depending on instrument and the type of
# sampling.
self._protocol_fsm.add_handler(ProtocolState.SCHEDULED_SAMPLE, ProtocolEvent.SUCCESS,
self._handler_sample_success)
self._protocol_fsm.add_handler(ProtocolState.SCHEDULED_SAMPLE, ProtocolEvent.TIMEOUT,
self._handler_sample_timeout)
# this state would be entered whenever an ACQUIRE_SAMPLE event occurred
# while in either the COMMAND state (or via the discover transition
# from the UNKNOWN state with the instrument unresponsive) and will
# last anywhere from a few seconds to 3 minutes depending on instrument
# and sample type.
self._protocol_fsm.add_handler(ProtocolState.POLLED_SAMPLE, ProtocolEvent.SUCCESS,
self._handler_sample_success)
self._protocol_fsm.add_handler(ProtocolState.POLLED_SAMPLE, ProtocolEvent.TIMEOUT,
self._handler_sample_timeout)
# Add build handlers for device commands.
### primarily defined in base class
self._add_build_handler(InstrumentCommand.ACQUIRE_SAMPLE_DEV1, self._build_sample_dev1)
# Add response handlers for device commands.
### primarily defined in base class
self._add_response_handler(InstrumentCommand.ACQUIRE_SAMPLE_DEV1, self._build_response_sample_dev1)
# Add sample handlers
# State state machine in UNKNOWN state.
self._protocol_fsm.start(ProtocolState.UNKNOWN)
# build the chunker
self._chunker = StringChunker(Protocol.sieve_function)
@staticmethod
def sieve_function(raw_data):
"""
The method that splits samples
"""
return_list = []
sieve_matchers = [REGULAR_STATUS_REGEX_MATCHER,
CONTROL_RECORD_REGEX_MATCHER,
SAMI_SAMPLE_REGEX_MATCHER,
DEV1_SAMPLE_REGEX_MATCHER,
CONFIGURATION_REGEX_MATCHER]
for matcher in sieve_matchers:
for match in matcher.finditer(raw_data):
return_list.append((match.start(), match.end()))
return return_list
def _got_chunk(self, chunk, timestamp):
"""
The base class got_data has gotten a chunk from the chunker. Pass it to
extract_sample with the appropriate particle objects and REGEXes.
"""
self._extract_sample(SamiRegularStatusDataParticle, REGULAR_STATUS_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(SamiControlRecordDataParticle, CONTROL_RECORD_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(Pco2wSamiSampleDataParticle, SAMI_SAMPLE_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(Pco2wDev1SampleDataParticle, DEV1_SAMPLE_REGEX_MATCHER, chunk, timestamp)
self._extract_sample(Pco2wConfigurationDataParticle, CONFIGURATION_REGEX_MATCHER, chunk, timestamp)
########################################################################
# Build Command, Driver and Parameter dictionaries
########################################################################
def _build_param_dict(self):
"""
For each parameter key, add match stirng, match lambda function,
and value formatting function for set commands.
"""
# Add parameter handlers to parameter dict.
self._param_dict = ProtocolParameterDict()
#.........这里部分代码省略.........