Ejemplo n.º 1
0
    def start_driver(self):
        """
        Start the driver process.
        """

        # Launch driver process based on test config.
        this_pid = os.getpid()
        (dvr_proc, cmd_port, evt_port) = ZmqDriverProcess.launch_process(self.driver_module,
                                                                         self.driver_class,
                                                                         self.work_dir,
                                                                         this_pid)
        self._dvr_proc = dvr_proc
        mi_logger.info('Started driver process for %d %d %s %s', cmd_port,
            evt_port, self.driver_module, self.driver_class)
        mi_logger.info('Driver process pid %d', self._dvr_proc.pid)

        # Create driver client.
        self._dvr_client = ZmqDriverClient('localhost', cmd_port,
            evt_port)
        mi_logger.info('Created driver client for %d %d %s %s', cmd_port,
            evt_port, self.driver_module, self.driver_class)

        # Start client messaging.
        self._dvr_client.start_messaging(self.evt_recd)
        mi_logger.info('Driver messaging started.')
        gevent.sleep(.5)
Ejemplo n.º 2
0
 def init_driver_process_client(self):
     """
     @brief Launch the driver process and driver client
     @retval return driver process and driver client object
     """
     self.log.info("Startup Driver Process")
     self.log.debug("Server Command Port: %d" % self.server_command_port())
     self.log.debug("Server Event Port: %d" % self.server_event_port())
     self.log.debug("Driver Module: %s" % self.driver_module())
     self.log.debug("Driver Class: %s" % self.driver_class())
     
     # Spawn the driver subprocess
     driver_process = ZmqDriverProcess.launch_process(self.server_command_port(), self.server_event_port(), self.driver_module(), self.driver_class())
     self._driver_process = driver_process
     
     self.log.info("Startup Driver Client")
     self.log.debug("Server Address: %s" % self.server_address())
     self.log.debug("Server Command Port: %d" % self.server_command_port())
     self.log.debug("Server Event Port: %d" % self.server_event_port())
     
     # Create client and start messaging.
     driver_client = ZmqDriverClient(self.server_address(), self.server_command_port(),
                                     self.server_event_port())
     self._driver_client = driver_client
     self.clear_events()
     
     driver_client.start_messaging(self.event_received)
     return driver_process, driver_client
Ejemplo n.º 3
0
    def test_connect(self):
        """
        Test to establish device comms and transition to command state.
        """
        # Launch driver process.
        driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)
        
        # Create client and start messaging.
        driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)
        driver_client.start_messaging()
        time.sleep(2)

        # Configure driver for comms and transition to disconnected.
        reply = driver_client.cmd_dvr('configure', self.comms_config)
        time.sleep(2)

        # Establish device comms and transition to command.
        reply = driver_client.cmd_dvr('connect')
        time.sleep(2)

        # Disconnect devcie comms and transition to disconnected.                
        reply = driver_client.cmd_dvr('disconnect')
        time.sleep(2)
        
        # Initialize driver and transition to unconfigured.
        reply = driver_client.cmd_dvr('initialize')
        time.sleep(2)

        # Terminate driver process and stop client messaging.
        driver_client.done()
        driver_process.wait()        
Ejemplo n.º 4
0
    def _start_driver(self, dvr_config):
        """
        Start the driver process and driver client.
        @param dvr_config The driver configuration.
        @param comms_config The driver communications configuration.
        @retval None or error.
        """
        try:        
            cmd_port = dvr_config['cmd_port']
            evt_port = dvr_config['evt_port']
            dvr_mod = dvr_config['dvr_mod']
            dvr_cls = dvr_config['dvr_cls']
            svr_addr = dvr_config['svr_addr']
            
        except (TypeError, KeyError):
            # Not a dict. or missing required parameter.
            log.error('Insturment agent %s missing required parameter in start_driver.',
                      self._proc_name)            
            return InstErrorCode.REQUIRED_PARAMETER
                
        # Launch driver process.
        self._dvr_proc = ZmqDriverProcess.launch_process(cmd_port, evt_port,
                                                         dvr_mod,  dvr_cls)

        self._dvr_proc.poll()
        if self._dvr_proc.returncode:
            # Error proc didn't start.
            log.error('Insturment agent %s driver process did not launch.',
                      self._proc_name)
            return InstErrorCode.AGENT_INIT_FAILED

        log.info('Insturment agent %s launched driver process.', self._proc_name)
        
        # Create client and start messaging.
        self._dvr_client = ZmqDriverClient(svr_addr, cmd_port, evt_port)
        self._dvr_client.start_messaging(self.evt_recv)
        log.info('Insturment agent %s driver process client started.',
                 self._proc_name)
        time.sleep(1)

        try:        
            retval = self._dvr_client.cmd_dvr('process_echo', 'Test.')
            log.info('Insturment agent %s driver process echo test: %s.',
                     self._proc_name, str(retval))
            
        except Exception:
            self._dvr_proc.terminate()
            self._dvr_proc.wait()
            self._dvr_proc = None
            self._dvr_client = None
            log.error('Insturment agent %s error commanding driver process.',
                      self._proc_name)            
            return InstErrorCode.AGENT_INIT_FAILED

        else:
            log.info('Insturment agent %s started its driver.', self._proc_name)
            self._construct_packet_factories()
Ejemplo n.º 5
0
    def test_poll(self):
        """
        Test sample polling commands and events.
        """
        # Launch driver process.
        driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)
        
        # Create client and start messaging.
        driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)
        self.clear_events()
        driver_client.start_messaging(self.evt_recd)
        time.sleep(2)

        reply = driver_client.cmd_dvr('configure', self.comms_config)
        time.sleep(2)

        reply = driver_client.cmd_dvr('connect')
        time.sleep(2)
        
        reply = driver_client.cmd_dvr('get_active_channels')
        time.sleep(2)
        
        reply = driver_client.cmd_dvr('execute_acquire_sample')
        time.sleep(2)
        
        reply = driver_client.cmd_dvr('execute_acquire_sample')
        time.sleep(2)

        reply = driver_client.cmd_dvr('execute_acquire_sample')
        time.sleep(2)

        print 'EVENTS RECEIVED:'
        print str(self.events)

        reply = driver_client.cmd_dvr('disconnect')
        time.sleep(2)
        
        # Deconfigure the driver.
        reply = driver_client.cmd_dvr('initialize')
        time.sleep(2)
        
        # Terminate driver process and stop client messaging.
        driver_client.done()
        driver_process.wait()
Ejemplo n.º 6
0
    def test_process(self):
        """
        Test for correct launch of driver process and communications, including
        asynchronous driver events.
        """
        # Launch driver process.
        driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)
        
        # Create client and start messaging.
        driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)        
        self.clear_events()
        driver_client.start_messaging(self.evt_recd)
        time.sleep(2)

        # Add test to verify process exists.

        # Send a test message to the process interface, confirm result.
        msg = 'I am a ZMQ message going to the process.'
        reply = driver_client.cmd_dvr('process_echo', msg)
        self.assertEqual(reply,'process_echo: '+msg)

        # Send a test message to the driver interface, confirm result.
        msg = 'I am a ZMQ message going to the driver.'
        reply = driver_client.cmd_dvr('driver_echo', msg)
        self.assertEqual(reply, 'driver_echo: '+msg)
        
        # Test the event thread publishes and client side picks up events.
        events = [
            'I am important event #1!',
            'And I am important event #2!'
            ]
        reply = driver_client.cmd_dvr('test_events', events=events)
        time.sleep(2)
        
        # Confirm the events received are as expected.
        self.assertEqual(self.events, events)
        
        # Terminate driver process and stop client messaging.
        driver_client.done()
        driver_process.wait()
Ejemplo n.º 7
0
    def setUp(self):
        super(BarsDriverTest, self).setUp()

        # Zmq parameters used by driver process and client.
        self.server_addr = 'localhost'
        self.cmd_port = 5556
        self.evt_port = 5557

        # Driver module parameters.
        self.dvr_mod = 'ion.services.mi.drivers.uw_bars.driver'
        self.dvr_cls = 'BarsInstrumentDriver'

        self._driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)

        self._driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)
        self._driver_client.start_messaging()
        time.sleep(1)

        self.addCleanup(self._clean_up)
Ejemplo n.º 8
0
    def test_autosample(self):
        """
        Test autosample command and state, including events.
        """
        # Launch driver process.
        driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)
        
        # Create client and start messaging.
        driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)
        driver_client.start_messaging()
        time.sleep(2)

        reply = driver_client.cmd_dvr('configure', self.comms_config)
        time.sleep(2)

        reply = driver_client.cmd_dvr('connect')
        time.sleep(2)
        
        reply = driver_client.cmd_dvr('start_autosample')
        time.sleep(30)
        
        while True:
            reply = driver_client.cmd_dvr('stop_autosample')
            if not reply[SBE37Channel.CTD]:
                break
            time.sleep(2)
        time.sleep(2)

        reply = driver_client.cmd_dvr('disconnect')
        time.sleep(2)
        
        # Deconfigure the driver.
        reply = driver_client.cmd_dvr('initialize')
        time.sleep(2)
        
        # Terminate driver process and stop client messaging.
        driver_client.done()
        driver_process.wait()
Ejemplo n.º 9
0
    def test_get_set(self):
        """
        Test driver parameter get/set interface including device persistence.
        TA2=-4.858579e-06
        PTCA1=-0.6603433
        TCALDATE=(8, 11, 2005)        
        """
        
        # Launch driver process.
        driver_process = ZmqDriverProcess.launch_process(self.cmd_port,
            self.evt_port, self.dvr_mod,  self.dvr_cls)

        # Create client and start messaging.        
        driver_client = ZmqDriverClient(self.server_addr, self.cmd_port,
                                        self.evt_port)
        driver_client.start_messaging()
        time.sleep(3)

        # Configure driver for comms and transition to disconnected.
        reply = driver_client.cmd_dvr('configure', self.comms_config)
        time.sleep(2)

        # Establish devcie comms and transition to command.
        reply = driver_client.cmd_dvr('connect')
        time.sleep(2)
        
        # Get all parameters.
        get_params = [
            (SBE37Channel.CTD, SBE37Parameter.ALL)            
        ]
        reply = driver_client.cmd_dvr('get', get_params)
        time.sleep(2)
        
        # Check overall and individual parameter success. Check parameter types.
        self.assertIsInstance(reply, dict)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TA2)],
                                                            float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)],
                                                            float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)],
                                                            (list, tuple))
        
        # Set up a param dict of the original values.
        old_ta2 = reply[(SBE37Channel.CTD, SBE37Parameter.TA2)]
        old_ptca1 = reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)]
        old_tcaldate = reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)]
        orig_params = {
            (SBE37Channel.CTD, SBE37Parameter.TA2): old_ta2,
            (SBE37Channel.CTD, SBE37Parameter.PTCA1): old_ptca1,
            (SBE37Channel.CTD, SBE37Parameter.TCALDATE): old_tcaldate            
        }

        # Set up a param dict of new values.
        new_ta2 = old_ta2*2
        new_ptcal1 = old_ptca1*2
        new_tcaldate = list(old_tcaldate)
        new_tcaldate[2] = new_tcaldate[2] + 1
        new_tcaldate = tuple(new_tcaldate)
        new_params = {
            (SBE37Channel.CTD, SBE37Parameter.TA2): new_ta2,
            (SBE37Channel.CTD, SBE37Parameter.PTCA1): new_ptcal1,
            (SBE37Channel.CTD, SBE37Parameter.TCALDATE): new_tcaldate
        }
        
        # Set the params to their new values.
        reply = driver_client.cmd_dvr('set', new_params)
        time.sleep(2)
        
        # Check overall success and success of the individual paramters.
        self.assertIsInstance(reply, dict)
        mi_logger.debug('set result: %s', str(reply))
        
        # Get the same paramters back from the driver.
        get_params = [
            (SBE37Channel.CTD, SBE37Parameter.TA2),
            (SBE37Channel.CTD, SBE37Parameter.PTCA1),
            (SBE37Channel.CTD, SBE37Parameter.TCALDATE)
        ]
        reply = driver_client.cmd_dvr('get', get_params)
        time.sleep(2)

        # Check success, and check that the parameters were set to the
        # new values.
        self.assertIsInstance(reply, dict)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TA2)],
                                                            float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)],
                                                            float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)],
                                                            (list, tuple))
        self.assertAlmostEqual(reply[(SBE37Channel.CTD, SBE37Parameter.TA2)],
                                    new_ta2, delta=abs(0.01*new_ta2))
        self.assertAlmostEqual(reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)],
                                    new_ptcal1, delta=abs(0.01*new_ptcal1))
        self.assertEqual(reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)],
                                                            new_tcaldate)


        # Set the paramters back to their original values.        
        reply = driver_client.cmd_dvr('set', orig_params)
        self.assertIsInstance(reply, dict)
        mi_logger.debug('set result: %s', str(reply))

        # Get the parameters back from the driver.
        reply = driver_client.cmd_dvr('get', get_params)

        # Check overall and individual sucess, and that paramters were
        # returned to their original values.
        self.assertIsInstance(reply, dict)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TA2)],
                                                    float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)],
                                                    float)
        self.assertIsInstance(reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)],
                                                    (list, tuple))
        self.assertAlmostEqual(reply[(SBE37Channel.CTD, SBE37Parameter.TA2)],
                                            old_ta2, delta=abs(0.01*old_ta2))
        self.assertAlmostEqual(reply[(SBE37Channel.CTD, SBE37Parameter.PTCA1)],
                                        old_ptca1, delta=abs(0.01*old_ptca1))
        self.assertEqual(reply[(SBE37Channel.CTD, SBE37Parameter.TCALDATE)],
                                        old_tcaldate)
        
        # Disconnect driver from the device and transition to disconnected.
        reply = driver_client.cmd_dvr('disconnect', [SBE37Channel.CTD])
        time.sleep(2)
        
        # Deconfigure the driver and transition to unconfigured.
        reply = driver_client.cmd_dvr('initialize', [SBE37Channel.CTD])
        time.sleep(2)
        
        # End driver process and client messaging.
        driver_client.done()
        driver_process.wait()