def get(self, skipCache=0): if self._last_value and (now() - self._last_rcvd_at) < self.ttl: return self._last_value self._last_value = self.parent.getValue( self.parent.username, self.parent.password, self.uri ) self._last_rcvd_at = now() return self._last_value
def get(self, skipCache=0): if self._last_value and (now() - self._last_rcvd_at) < self.ttl: return self._last_value if self.__call_args_r: value = self._do_read( **self.SOAPCallParams(self.__call_args_r, self.inparams_read)) else: value = self._do_read() self._last_rcvd_at = now() self._last_value = value return self._last_value
def _read(self, buffer, count, timeout): count = count - self._read_internal_buffer(buffer, count) t_end = now() + timeout ms = int(timeout * 1000) while count and ms >= 0: poll_list = self.poll.poll(ms) for (file_descriptor, event) in poll_list: if event == select.POLLIN: self._write_internal_buffer(self._read_port()) count = count - self._read_internal_buffer(buffer, count) ms = int((t_end - now()) * 1000) if count: raise ETimeout
def get(self, skipCache=0): if self._last_value and (now() - self._last_rcvd_at) < self.ttl: return self._last_value if self.__call_args_r: value = self._do_read( **self.SOAPCallParams( self.__call_args_r, self.inparams_read ) ) else: value = self._do_read() self._last_rcvd_at = now() self._last_value = value return self._last_value
def __init__(self, *args): super(RemoteWebServiceOp, self).__init__(*args) self.__call_args_r = None self.__call_args_w = None self._last_value = None self._last_rcvd_at = now() return
def _read_upto(self, buffer, list, timeout): count = self._scan_for(list) if count != None: self._read_internal_buffer(buffer, count) return self._buf.pop(0) t_end = now() + timeout ms = int(timeout * 1000) while ms >= 0: poll_list = self.poll.poll(ms) for (file_descriptor, event) in poll_list: if event == select.POLLIN: self._write_internal_buffer(self._read_port()) count = self._scan_for(list) if count != None: self._read_internal_buffer(buffer, count) return self._buf.pop(0) else: self._read_internal_buffer(buffer, len(self._buf)) ms = int((t_end - now()) * 1000) raise ETimeout
def __init__(self): CompositeNode.__init__(self) self.timeout = REQUIRED self.debug = REQUIRED self.interval_calculation = REQUIRED self.silent_interval_characters = REQUIRED self.silent_interval_absolute = REQUIRED self.__silent_interval = None # Initialized in start() self.__last_character_time = now() # OK, this isn't true... self.retries = 3 #used by register_cache.RegisterCache._refresh self.report_timeouts = 1 #0 == try but give back last good value return
def __wait_for_port(self, timeout=10.0): chatter_timeout = now() + timeout last_character_time = self.__last_character_time silent_interval = self.__silent_interval silence_timeout = last_character_time + silent_interval chatter = self.port.drain() while now() < chatter_timeout: if len(chatter): chatter = self.port.drain() last_character_time = now() silence_timeout = last_character_time + silent_interval continue if now() > silence_timeout: # OK, there has been no "chatter" for the required interval self.__last_character_time = last_character_time return sleep_timeout = silence_timeout - now() if sleep_timeout > 0.0: sleep(sleep_timeout) chatter = self.port.drain() raise ETimeout('Failed to detect %.2d millisecond silent interval ' 'after %d seconds.' % (silent_interval / 1000.0, timeout))
def __wait_for_port(self, timeout=10.0): chatter_timeout = now() + timeout last_character_time = self.__last_character_time silent_interval = self.__silent_interval silence_timeout = last_character_time + silent_interval chatter = self.port.drain() while now() < chatter_timeout: if len(chatter): chatter = self.port.drain() last_character_time = now() silence_timeout = last_character_time + silent_interval continue if now() > silence_timeout: # OK, there has been no "chatter" for the required interval self.__last_character_time = last_character_time return sleep_timeout = silence_timeout - now() if sleep_timeout > 0.0: sleep(sleep_timeout) chatter = self.port.drain() raise ETimeout( 'Failed to detect %.2d millisecond silent interval ' 'after %d seconds.' % (silent_interval/1000.0, timeout) )
def command(self, cmd, ip=None): b = cmd.buffer timeout = cmd.timeout(self.timeout) # @fixme Caching monitors based on address could speed things up. monitor = self.device_manager.monitor_from_kw( ModbusDeviceIdentifier, device_class="modbus", port=self.port_url, address=cmd.slave_address) self.port.lock() try: self.__wait_for_port() self.port.write(b) self.port.flush() header = buffer() # All Modbus responses are at least five bytes in length. bytesleft = 5 discarded = 0 timeleft = timeout # If this is a Megatron RS485 port then consume echoed transmit characters if properties.HARDWARE_CODENAME == 'Megatron': megatron_485 = self.parent.name in ('com3', 'com4', 'com5', 'com6') if megatron_485: expiration = now() + timeout try: self.port.read(header, len(b), timeleft) # We can check the received data here. If it is not what we sent then we have a # transmit error. Might be a collision. if (header != b): msglog.log( 'MODBUS', msglog.types.WARN, 'Transmitted message corrupt: Collision?') del (header[:]) except: msglog.exception() expiration = now() + timeout while bytesleft: self.port.read(header, bytesleft, timeleft) bytesleft = 0 while header and not header[0]: header.pop(0) bytesleft += 1 if bytesleft: discarded += bytesleft timeleft = expiration - now() if self.debug: print "Modbus.LinHandler: Discard %d 0s" % bytesleft if timeleft <= 0: raise ETimeout("Timeout discarding %d 0s" % discarded) if header[1] & 0x80: self.__last_character_time = now() e = cmd.exception(exception.factory)(self, header) raise e resp = cmd.response(response.factory)(self, header, timeout) self.__last_character_time = now() if (resp.slave_address != cmd.slave_address or resp.function != cmd.function): # Not a response to this command, ignore it raise exception.EModbusMismatch() except Exception, e: monitor.notify_exception(e) raise # Reraise the exception for processing...
def __init__(self): self._last_value = None self._last_rcvd_at = now() return
def command(self,cmd, ip=None): b = cmd.buffer timeout = cmd.timeout(self.timeout) # @fixme Caching monitors based on address could speed things up. monitor = self.device_manager.monitor_from_kw( ModbusDeviceIdentifier, device_class="modbus", port=self.port_url, address=cmd.slave_address) self.port.lock() try: self.__wait_for_port() self.port.write(b) self.port.flush() header = buffer() # All Modbus responses are at least five bytes in length. bytesleft = 5 discarded = 0 timeleft = timeout # If this is a Megatron RS485 port then consume echoed transmit characters if properties.HARDWARE_CODENAME == 'Megatron': megatron_485 = self.parent.name in ('com3', 'com4', 'com5', 'com6') if megatron_485: expiration = now() + timeout try: self.port.read(header, len(b), timeleft) # We can check the received data here. If it is not what we sent then we have a # transmit error. Might be a collision. if (header != b): msglog.log('MODBUS', msglog.types.WARN, 'Transmitted message corrupt: Collision?') del(header[:]) except: msglog.exception() expiration = now() + timeout while bytesleft: self.port.read(header, bytesleft, timeleft) bytesleft = 0 while header and not header[0]: header.pop(0) bytesleft += 1 if bytesleft: discarded += bytesleft timeleft = expiration - now() if self.debug: print "Modbus.LinHandler: Discard %d 0s" % bytesleft if timeleft <= 0: raise ETimeout("Timeout discarding %d 0s" % discarded) if header[1] & 0x80: self.__last_character_time = now() e = cmd.exception(exception.factory)(self, header) raise e resp = cmd.response(response.factory)(self, header, timeout) self.__last_character_time = now() if (resp.slave_address != cmd.slave_address or resp.function != cmd.function): # Not a response to this command, ignore it raise exception.EModbusMismatch() except Exception, e: monitor.notify_exception(e) raise # Reraise the exception for processing...