def _wakeup(self, wakeup_timeout=10, response_timeout=3): """ Over-ridden because waking this instrument up is a multi-step process with two different requests required @param wakeup_timeout The timeout to wake the device. @param response_timeout The time to look for response to a wakeup attempt. @throw InstrumentTimeoutException if the device could not be woken. """ sleep_time = .1 command = Command.END_OF_LINE # Grab start time for overall wakeup timeout. starttime = time.time() while True: # Clear the prompt buffer. log.debug("_wakeup: clearing promptbuf: %s" % self._promptbuf) self._promptbuf = '' # Send a command and wait delay amount for response. log.debug('_wakeup: Sending command %s, delay=%s' % (command.encode("hex"), response_timeout)) for char in command: self._connection.send(char) time.sleep(INTER_CHARACTER_DELAY) sleep_amount = 0 while True: time.sleep(sleep_time) if self._promptbuf.find(Prompt.COMMAND_INPUT) != -1: # instrument is awake log.debug('_wakeup: got command input prompt %s' % Prompt.COMMAND_INPUT) # add inter-character delay which _do_cmd_resp() incorrectly doesn't add to the start of a transmission time.sleep(INTER_CHARACTER_DELAY) return Prompt.COMMAND_INPUT if self._promptbuf.find(Prompt.ENTER_CTRL_C) != -1: command = Command.CONTROL_C break if self._promptbuf.find(Prompt.PERIOD) == 0: command = Command.CONTROL_C break sleep_amount += sleep_time if sleep_amount >= response_timeout: log.debug( "_wakeup: expected response not received, buffer=%s" % self._promptbuf) break if time.time() > starttime + wakeup_timeout: raise InstrumentTimeoutException( "_wakeup(): instrument failed to wakeup in %d seconds time" % wakeup_timeout)
def execute_stop_autosample(self, *args, **kwargs): if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.CONNECTED) timeout = kwargs.get('timeout', self._timeout) try: result = self._client.stop_autosample(timeout=timeout) return result except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def execute_get_metadata(self, *args, **kwargs): if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.CONNECTED) timeout = kwargs.get('timeout', self._timeout) sections = kwargs.get('sections', None) try: result = self._client.get_metadata(sections, timeout) return result except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def _handler_command_run_all_tests(self, *args, **kwargs): """ """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) next_state = self._protocol_fsm.get_current_state() result = None timeout = kwargs.get('timeout', self._timeout) try: result = self._connection.run_all_tests(timeout) except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def _get_response(self, timeout=10, expected_prompt=None): """ Overriding _get_response: this one uses regex on chunks that have already been filtered by the chunker. An improvement to the chunker could be metadata labeling the chunk so that we don't have to do another match, although I don't think it is that expensive once the chunk has been pulled out to match again @param timeout The timeout in seconds @param expected_prompt Only consider the specific expected prompt as presented by this string @throw InstrumentProtocolExecption on timeout """ # Grab time for timeout and wait for response starttime = time.time() response = None """ Spin around for <timeout> looking for the response to arrive, but not if there is no expected prompt to look for. """ if None == expected_prompt: continuing = False else: continuing = True response = "no response" while continuing: if self.cmd_rsp_regex.match(self._promptbuf): response = NANOCommandResponse(self._promptbuf) log.debug("_get_response() matched CommandResponse") response.check_command_response(expected_prompt) continuing = False elif self.status_01_regex.match(self._promptbuf): response = NANOStatus_01_Particle(self._promptbuf) log.debug("_get_response() matched Status_01_Response") response.build_response() continuing = False else: self._promptbuf = '' time.sleep(.1) if timeout and time.time() > starttime + timeout: raise InstrumentTimeoutException( "in BOTPT NANO driver._get_response()") return ('NANO_RESPONSE', response)
def _handler_command_autosample(self, *args, **kwargs): """ """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) next_state = None result = None timeout = kwargs.get('timeout', self._timeout) try: result = self._connection.start_autosample(timeout=timeout) next_state = ProtocolState.AUTOSAMPLE_MODE except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def _handler_autosample_stop(self, *args, **kwargs): """ """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) next_state = None result = None duration = int(kwargs.get('duration', 1000)) try: result = self._connection.send_break(duration) next_state = ProtocolState.COMMAND_MODE except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def _wakeup(self, wakeup_timeout=WAKEUP_TIMEOUT, response_timeout=RESPONSE_TIMEOUT): """ waking this instrument up by sending MAX_COM_TEST communication test commands (aP*) @param wakeup_timeout The timeout to wake the device. @param response_timeout The time to look for response to a wakeup attempt. @throw InstrumentTimeoutException if the device could not be woken. """ log.debug("_wakeup ") sleep_time = CMD_RESP_TIME cmd_line = self._build_simple_command(Command.COMM_TEST) # Grab start time for overall wakeup timeout. start_time = time.time() test_count = 0 while test_count < MAX_COMM_TEST: # Clear the prompt buffer. self._promptbuf = '' # Send a communication test command and wait delay amount for response. self._connection.send(cmd_line) time.sleep(sleep_time) if self._promptbuf.find(Prompt.COMM_RESPONSE) != -1: # instrument is awake log.debug('_wakeup: got communication test response %s', Prompt.COMM_RESPONSE) test_count += 1 else: #clear test_count since we want MAX_COMM_TEST consecutive successful communication test test_count = 0 # Stop wake up the instrument if the wake up time out has elapsed if time.time() > start_time + wakeup_timeout: break if test_count != MAX_COMM_TEST: log.debug('instrument failed to wakeup in %d seconds time' % wakeup_timeout) raise InstrumentTimeoutException( "_wakeup(): instrument failed to wakeup in %d seconds time" % wakeup_timeout) else: return Prompt.COMM_RESPONSE
def _get_response(self, timeout=10, expected_prompt=None, response_regex=None, connection=None): """ Overridden to handle multiple port agent connections """ if connection is None: raise InstrumentProtocolException('_get_response: no connection supplied!') # Grab time for timeout and wait for prompt. end_time = time.time() + timeout if response_regex and not isinstance(response_regex, RE_PATTERN): raise InstrumentProtocolException('Response regex is not a compiled pattern!') if expected_prompt and response_regex: raise InstrumentProtocolException('Cannot supply both regex and expected prompt!') if expected_prompt is None: prompt_list = self._get_prompts() else: if isinstance(expected_prompt, str): prompt_list = [expected_prompt] else: prompt_list = expected_prompt if response_regex is None: pattern = None else: pattern = response_regex.pattern log.debug('_get_response: timeout=%s, prompt_list=%s, expected_prompt=%r, response_regex=%r, promptbuf=%r', timeout, prompt_list, expected_prompt, pattern, self._promptbuf) while time.time() < end_time: if response_regex: # noinspection PyArgumentList match = response_regex.search(self._linebuf[connection]) if match: return match.groups() else: for item in prompt_list: index = self._promptbuf[connection].find(item) if index >= 0: result = self._promptbuf[connection][0:index + len(item)] return item, result time.sleep(.1) raise InstrumentTimeoutException("in InstrumentProtocol._get_response()")
def _set_cycle_time(self, seconds, timeout): if not isinstance(seconds, int): # return InstErrorCode.INVALID_PARAM_VALUE raise InstrumentParameterException( msg='seconds object is not an int: %s' % seconds) if seconds < 15: # return InstErrorCode.INVALID_PARAM_VALUE raise InstrumentParameterException( msg='seconds must be >= 15: %s' % seconds) try: self.trhph_client.set_cycle_time(seconds, timeout) # return InstErrorCode.OK return except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))
def _send_continuous_break(self): """ send break every 0.3 seconds until the Command Console banner """ self._promptbuf = "" self._connection.send(Command.BREAK) starttime = time.time() resendtime = time.time() while True: time.sleep(0.1) if time.time() > resendtime + 0.3: log.debug("Sending break again.") self._connection.send(Command.BREAK) resendtime = time.time() if COMMAND_PATTERN in self._promptbuf: break if time.time() > starttime + 5: raise InstrumentTimeoutException("Break command failing to stop autosample!")
def _wakeup(self, timeout, delay=1, connection=None): """ Clear buffers and send a wakeup command to the instrument @param timeout The timeout to wake the device. @param delay The time to wait between consecutive wakeups. @throw InstrumentTimeoutException if the device could not be woken. """ if connection is None: raise InstrumentProtocolException( '_wakeup: no connection supplied!') # Clear the prompt buffer. log.trace("clearing promptbuf: %r", self._promptbuf) self._promptbuf[connection] = '' # Grab time for timeout. starttime = time.time() while True: # Send a line return and wait a sec. log.trace('Sending wakeup. timeout=%s', timeout) self._send_wakeup(connection=connection) time.sleep(delay) log.trace("Prompts: %s", self._get_prompts()) for item in self._get_prompts(): log.trace("buffer: %r", self._promptbuf[connection]) log.trace("find prompt: %r", item) index = self._promptbuf[connection].find(item) log.trace("Got prompt (index: %s): %r ", index, self._promptbuf[connection]) if index >= 0: log.trace('wakeup got prompt: %r', item) return item log.trace("Searched for all prompts") if time.time() > starttime + timeout: raise InstrumentTimeoutException("in _wakeup()")
def _send_break(self): """ Send break every 0.3 seconds until the Command Console banner is received. @throws InstrumentTimeoutException if not Command Console banner not received within 5 seconds. """ self._promptbuf = "" self._connection.send(Command.BREAK) starttime = time.time() resendtime = time.time() while True: if time.time() > resendtime + 0.3: log.debug("Sending break again.") self._connection.send(Command.BREAK) resendtime = time.time() if COMMAND_PATTERN in self._promptbuf: break if time.time() > starttime + 10: raise InstrumentTimeoutException("Break command failing to stop autosample!") time.sleep(0.1)
def execute_start_autosample(self, *args, **kwargs): """ Switch to autosample mode. @param timeout Timeout for each involved instrument interation, self._timeout by default. @raises InstrumentTimeoutException if could not wake device or no response. @raises InstrumentStateException if command not allowed in current state. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(TrhphDriverState.CONNECTED) timeout = kwargs.get('timeout', self._timeout) try: self.trhph_client.resume_data_streaming(timeout) # result = InstErrorCode.OK result = None return result except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e))