def _build_connection(self, config): """ Constructs and returns a Connection object according to the given configuration. The object returned here is a VadcpClient instance. @param config configuration dict @retval a VadcpClient instance @throws InstrumentParameterException Invalid configuration. """ log.info('_build_connection: config=%s' % config) c4 = config['four_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c4.host, c4.port) u4_outfile = file(outfilename, 'w') c5 = config['fifth_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c5.host, c5.port) u5_outfile = file(outfilename, 'w') log.info("setting VadcpClient with config: %s" % config) try: client = VadcpClient(config, u4_outfile, u5_outfile) except (TypeError, KeyError): raise InstrumentParameterException('Invalid comms config dict.' ' config=%s' % config) # set data_listener to the client so we can notify corresponding # DriverAsyncEvent.SAMPLE events: def _data_listener(sample): log.info("_data_listener: sample = %s" % str(sample)) self._driver_event(DriverAsyncEvent.SAMPLE, val=sample) client.set_data_listener(_data_listener) return client
def setUpClass(cls): super(Test, cls).setUpClass() if cls._skip_reason: return ReceiverBuilder.use_greenlets() cls._samples_recd = 0 c4 = cls._conn_config['four_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c4.host, c4.port) u4_outfile = file(outfilename, 'w') c5 = cls._conn_config['fifth_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c5.host, c5.port) u5_outfile = file(outfilename, 'w') cls._client = VadcpClient(cls._conn_config, u4_outfile, u5_outfile) cls._client.set_generic_timeout(cls._timeout) log.info("connecting") cls._client.set_data_listener(cls._data_listener) cls._client.connect() log.info("sending break and waiting for prompt") cls._client.send_break()
def configure(self, *args, **kwargs): """ Configure the driver for communications with the device via port agent / logger (valid but unconnected connection object). @param config comms config dict. @raises InstrumentStateException if command not allowed in current state @throws InstrumentParameterException if missing comms or invalid config dict. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.UNCONFIGURED) config = kwargs.get('config', None) if config is None: # raise InstrumentParameterException(msg="'config' parameter required") config = args[0] c4 = config['four_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c4.host, c4.port) u4_outfile = file(outfilename, 'w') c5 = config['fifth_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c5.host, c5.port) u5_outfile = file(outfilename, 'w') # Verify dict and construct connection client. log.info("setting VadcpClient with config: %s" % config) try: self._client = VadcpClient(config, u4_outfile, u5_outfile) except (TypeError, KeyError): raise InstrumentParameterException('Invalid comms config dict.' ' config=%s' % config) self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.DISCONNECTED
class VadcpDriver(InstrumentDriver): """ driver """ def __init__(self, evt_callback): """ Constructor. @param evt_callback Driver process event callback. """ InstrumentDriver.__init__(self, evt_callback) self._ensembles_recd = 0 # _client created in configure() self._client = None self._state = DriverState.UNCONFIGURED # TODO probably promote this convenience to super-class? self._timeout = 30 """Default timeout value for operations accepting an optional timeout argument.""" def _assert_state(self, obj): """ Asserts that the current state is either the same as the one given (if not a list) or one of the elements of the given list. @raises InstrumentStateException if the assertion fails """ cs = self.get_current_state() if isinstance(obj, list): if cs in obj: return # OK else: raise InstrumentStateException(msg= "current state=%s not one of %s" % (cs, str(obj))) state = obj if cs != state: raise InstrumentStateException( "current state=%s, expected=%s" % (cs, state)) def _data_listener(self, pd0): self._ensembles_recd += 1 log.info("_data_listener: received PD0=%s" % prefix(pd0)) self._driver_event(DriverAsyncEvent.SAMPLE, val=pd0) ############################################################# # 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 """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) if self._state == DriverState.UNCONFIGURED: assert self._client is None return if self._client is not None: try: self._client.end() finally: self._client = None self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.UNCONFIGURED def configure(self, *args, **kwargs): """ Configure the driver for communications with the device via port agent / logger (valid but unconnected connection object). @param config comms config dict. @raises InstrumentStateException if command not allowed in current state @throws InstrumentParameterException if missing comms or invalid config dict. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.UNCONFIGURED) config = kwargs.get('config', None) if config is None: # raise InstrumentParameterException(msg="'config' parameter required") config = args[0] c4 = config['four_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c4.host, c4.port) u4_outfile = file(outfilename, 'w') c5 = config['fifth_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c5.host, c5.port) u5_outfile = file(outfilename, 'w') # Verify dict and construct connection client. log.info("setting VadcpClient with config: %s" % config) try: self._client = VadcpClient(config, u4_outfile, u5_outfile) except (TypeError, KeyError): raise InstrumentParameterException('Invalid comms config dict.' ' config=%s' % config) self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.DISCONNECTED 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. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.DISCONNECTED) self._client.set_data_listener(self._data_listener) self._client.connect() self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.CONNECTED def disconnect(self, *args, **kwargs): """ Disconnect from device via port agent / logger. @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(DriverState.CONNECTED) self._client.end() self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.DISCONNECTED def execute_init_protocol(self, *args, **kwargs): # added here as part of the preparation using the general scheme # TODO pass def execute_get_latest_sample(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.get_latest_sample(timeout) return result except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e)) except ClientException, e: log.warn("ClientException while get_latest_sample: %s" % str(e)) raise InstrumentException('ClientException: %s' % str(e))
class VadcpDriver(InstrumentDriver): """ driver """ def __init__(self, evt_callback): """ Constructor. @param evt_callback Driver process event callback. """ InstrumentDriver.__init__(self, evt_callback) self._ensembles_recd = 0 # _client created in configure() self._client = None self._state = DriverState.UNCONFIGURED # TODO probably promote this convenience to super-class? self._timeout = 30 """Default timeout value for operations accepting an optional timeout argument.""" def _assert_state(self, obj): """ Asserts that the current state is either the same as the one given (if not a list) or one of the elements of the given list. @raises InstrumentStateException if the assertion fails """ cs = self.get_current_state() if isinstance(obj, list): if cs in obj: return # OK else: raise InstrumentStateException( msg="current state=%s not one of %s" % (cs, str(obj))) state = obj if cs != state: raise InstrumentStateException("current state=%s, expected=%s" % (cs, state)) def _data_listener(self, pd0): self._ensembles_recd += 1 log.info("_data_listener: received PD0=%s" % prefix(pd0)) self._driver_event(DriverAsyncEvent.SAMPLE, val=pd0) ############################################################# # 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 """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) if self._state == DriverState.UNCONFIGURED: assert self._client is None return if self._client is not None: try: self._client.end() finally: self._client = None self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.UNCONFIGURED def configure(self, *args, **kwargs): """ Configure the driver for communications with the device via port agent / logger (valid but unconnected connection object). @param config comms config dict. @raises InstrumentStateException if command not allowed in current state @throws InstrumentParameterException if missing comms or invalid config dict. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.UNCONFIGURED) config = kwargs.get('config', None) if config is None: # raise InstrumentParameterException(msg="'config' parameter required") config = args[0] c4 = config['four_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c4.host, c4.port) u4_outfile = file(outfilename, 'w') c5 = config['fifth_beam'] outfilename = 'vadcp_output_%s_%s.txt' % (c5.host, c5.port) u5_outfile = file(outfilename, 'w') # Verify dict and construct connection client. log.info("setting VadcpClient with config: %s" % config) try: self._client = VadcpClient(config, u4_outfile, u5_outfile) except (TypeError, KeyError): raise InstrumentParameterException('Invalid comms config dict.' ' config=%s' % config) self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.DISCONNECTED 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. """ if log.isEnabledFor(logging.DEBUG): log.debug("args=%s kwargs=%s" % (str(args), str(kwargs))) self._assert_state(DriverState.DISCONNECTED) self._client.set_data_listener(self._data_listener) self._client.connect() self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.CONNECTED def disconnect(self, *args, **kwargs): """ Disconnect from device via port agent / logger. @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(DriverState.CONNECTED) self._client.end() self._driver_event(DriverAsyncEvent.STATE_CHANGE) self._state = DriverState.DISCONNECTED def execute_init_protocol(self, *args, **kwargs): # added here as part of the preparation using the general scheme # TODO pass def execute_get_latest_sample(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.get_latest_sample(timeout) return result except TimeoutException, e: raise InstrumentTimeoutException(msg=str(e)) except ClientException, e: log.warn("ClientException while get_latest_sample: %s" % str(e)) raise InstrumentException('ClientException: %s' % str(e))