def _is_time_set(self, time_param, expected_time, time_format="%d %b %Y %H:%M:%S", tolerance=DEFAULT_CLOCK_DIFF): """ Verify is what we expect it to be within a given tolerance @param time_param: driver parameter @param expected_time: what the time should be in seconds since unix epoch or formatted time string @param time_format: date time format @param tolerance: how close to the set time should the get be? """ log.debug("Expected time unformatted: %s", expected_time) result_time = self.assert_get(time_param) result_time_struct = time.strptime(result_time, time_format) converted_time = timegm_to_float(result_time_struct) if isinstance(expected_time, float): expected_time_struct = time.localtime(expected_time) else: expected_time_struct = time.strptime(expected_time, time_format) log.debug("Current Time: %s, Expected Time: %s", time.strftime("%d %b %y %H:%M:%S", result_time_struct), time.strftime("%d %b %y %H:%M:%S", expected_time_struct)) log.debug("Current Time: %s, Expected Time: %s, Tolerance: %s", converted_time, timegm_to_float(expected_time_struct), tolerance) # Verify the clock is set within the tolerance return abs(converted_time - timegm_to_float(expected_time_struct)) <= tolerance
def _is_time_set(self, time_param, expected_time, time_format = "%d %b %Y %H:%M:%S", tolerance=DEFAULT_CLOCK_DIFF): """ Verify is what we expect it to be within a given tolerance @param time_param: driver parameter @param expected_time: what the time should be in seconds since unix epoch or formatted time string @param time_format: date time format @param tolerance: how close to the set time should the get be? """ log.debug("Expected time unformatted: %s", expected_time) result_time = self.assert_get(time_param) result_time_struct = time.strptime(result_time, time_format) converted_time = timegm_to_float(result_time_struct) if isinstance(expected_time, float): expected_time_struct = time.localtime(expected_time) else: expected_time_struct = time.strptime(expected_time, time_format) log.debug("Current Time: %s, Expected Time: %s", time.strftime("%d %b %y %H:%M:%S", result_time_struct), time.strftime("%d %b %y %H:%M:%S", expected_time_struct)) log.debug("Current Time: %s, Expected Time: %s, Tolerance: %s", converted_time, timegm_to_float(expected_time_struct), tolerance) # Verify the clock is set within the tolerance return abs(converted_time - timegm_to_float(expected_time_struct)) <= tolerance
def test_execute_clock_sync(self): """ Verify we can synchronize the instrument internal clock """ self.assert_enter_command_mode() self.assert_execute_resource(ProtocolEvent.CLOCK_SYNC) # get the time from the driver check_new_params = self.instrument_agent_client.get_resource( [Parameter.CLOCK]) # convert driver's time from formatted date/time string to seconds integer instrument_time = timegm_to_float( time.strptime( check_new_params.get(Parameter.CLOCK).lower(), "%Y/%m/%d %H:%M:%S")) # need to convert local machine's time to date/time string and back to seconds to 'drop' the DST attribute so test passes # get time from local machine lt = time.strftime("%d %b %Y %H:%M:%S", time.gmtime(timegm_to_float(time.localtime()))) # convert local time from formatted date/time string to seconds integer to drop DST local_time = timegm_to_float(time.strptime(lt, "%d %b %Y %H:%M:%S")) # Now verify that the time matches to within 5 seconds self.assertLessEqual(abs(instrument_time - local_time), 5)
def assert_time_synched(self, pps_time, tolerance=5): """ Verify the retrieved time is within acceptable tolerance """ pps_time = time.strptime(pps_time + 'UTC', '%m/%d/%y %H:%M:%S %Z') current_time = time.gmtime() diff = timegm_to_float(current_time) - timegm_to_float(pps_time) log.info('clock synched within %d seconds', diff) # verify that the time matches to within tolerance seconds self.assertLessEqual(diff, tolerance)
def test_system_sample_format(self): """ Verify driver can get system sample data out in a reasonable format. Parsed is all we care about...raw is tested in the base DataParticle tests """ port_timestamp = 3555423720.711772 driver_timestamp = 3555423722.711772 text_timestamp = time.strptime('13/12/2012 17:03:26', "%d/%m/%Y %H:%M:%S") internal_timestamp = ntplib.system_to_ntp_time( timegm_to_float(text_timestamp)) # construct the expected particle expected_particle = { DataParticleKey.PKT_FORMAT_ID: DataParticleValue.JSON_DATA, DataParticleKey.PKT_VERSION: 1, DataParticleKey.STREAM_NAME: DataParticleType.SYSTEM, DataParticleKey.PORT_TIMESTAMP: port_timestamp, DataParticleKey.DRIVER_TIMESTAMP: driver_timestamp, DataParticleKey.INTERNAL_TIMESTAMP: internal_timestamp, DataParticleKey.PREFERRED_TIMESTAMP: DataParticleKey.PORT_TIMESTAMP, DataParticleKey.QUALITY_FLAG: DataParticleValue.OK, DataParticleKey.VALUES: system_particle } self.compare_parsed_data_particle(VectorSystemDataParticle, system_sample(), expected_particle)
def assert_clock_set(self, time_param, sync_clock_cmd=DriverEvent.ACQUIRE_STATUS, timeout=60, tolerance=DEFAULT_CLOCK_DIFF): """ Verify the clock is set to at least the current date """ log.debug("verify clock is set to the current time") timeout_time = time.time() + timeout while not self._is_time_set(time_param, timegm_to_float(time.gmtime()), tolerance=tolerance): log.debug("time isn't current. sleep for a bit") # Run acquire status command to set clock parameter self.assert_driver_command(sync_clock_cmd) log.debug("T: %s T: %s", time.time(), timeout_time) time.sleep(5) self.assertLess(time.time(), timeout_time, msg="Timeout waiting for clock sync event")
def test_system_sample_format(self): """ Verify driver can get system sample data out in a reasonable format. Parsed is all we care about...raw is tested in the base DataParticle tests """ port_timestamp = 3555423720.711772 driver_timestamp = 3555423722.711772 text_timestamp = time.strptime('13/12/2012 17:03:26', "%d/%m/%Y %H:%M:%S") internal_timestamp = ntplib.system_to_ntp_time(timegm_to_float(text_timestamp)) # construct the expected particle expected_particle = { DataParticleKey.PKT_FORMAT_ID: DataParticleValue.JSON_DATA, DataParticleKey.PKT_VERSION: 1, DataParticleKey.STREAM_NAME: DataParticleType.SYSTEM, DataParticleKey.PORT_TIMESTAMP: port_timestamp, DataParticleKey.DRIVER_TIMESTAMP: driver_timestamp, DataParticleKey.INTERNAL_TIMESTAMP: internal_timestamp, DataParticleKey.PREFERRED_TIMESTAMP: DataParticleKey.PORT_TIMESTAMP, DataParticleKey.QUALITY_FLAG: DataParticleValue.OK, DataParticleKey.VALUES: system_particle } self.compare_parsed_data_particle(VectorSystemDataParticle, system_sample(), expected_particle)
def assert_clock_sync(self): """ Verify the clock is set to at least the current date """ self.assert_driver_command(ProtocolEvent.ACQUIRE_STATUS) dt = self.assert_get(Parameter.TIME) lt = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(timegm_to_float(time.localtime()))) self.assertTrue(lt[:12].upper() in dt.upper())
def assert_clock_synced(self): """ Verify the clock is set to the current time with in a few seconds. """ reply = self.driver_client.cmd_dvr('get_resource', Parameter.CLOCK) # convert driver's time from formatted date/time string to seconds integer instrument_time = timegm_to_float(time.strptime(reply.get(Parameter.CLOCK).lower(), "%Y/%m/%d %H:%M:%S")) # need to convert local machine's time to date/time string and back to seconds to 'drop' the DST attribute so test passes # get time from local machine lt = time.strftime("%d %b %Y %H:%M:%S", time.gmtime(timegm_to_float(time.localtime()))) # convert local time from formatted date/time string to seconds integer to drop DST local_time = timegm_to_float(time.strptime(lt, "%d %b %Y %H:%M:%S")) # Now verify that the time matches to within 5 seconds self.assertLessEqual(abs(instrument_time - local_time), 5)
def assert_clock_synced(self): """ Verify the clock is set to the current time with in a few seconds. """ reply = self.driver_client.cmd_dvr('get_resource', Parameter.CLOCK) # convert driver's time from formatted date/time string to seconds integer instrument_time = timegm_to_float( time.strptime( reply.get(Parameter.CLOCK).lower(), "%Y/%m/%d %H:%M:%S")) # need to convert local machine's time to date/time string and back to seconds to 'drop' the DST attribute so test passes # get time from local machine lt = time.strftime("%d %b %Y %H:%M:%S", time.gmtime(timegm_to_float(time.localtime()))) # convert local time from formatted date/time string to seconds integer to drop DST local_time = timegm_to_float(time.strptime(lt, "%d %b %Y %H:%M:%S")) # Now verify that the time matches to within 5 seconds self.assertLessEqual(abs(instrument_time - local_time), 5)
def get_datetime(ts, local_time=True): """ Returns a datatime instance in either local time or GMT time based on the given ION timestamp @param ts ION timestamp (str with millis in epoch) @param local_time if True, returns local time (default), otherwise GMT @retval datetime instance """ tsf = float(ts) / 1000 timev = time.localtime(tsf) if local_time else time.gmtime(tsf) dt = datetime.datetime.fromtimestamp(timegm_to_float(timev)) return dt
def set_botpt_timestamp(self): """ Set the internal timestamp based on the embedded timestamp in the sample """ ts = self.match.group('date_time') if '.' in ts: ts, right = ts.split('.', 1) fraction = float('.' + right) else: fraction = 0 timestamp = time.strptime(ts, "%Y/%m/%d %H:%M:%S") self.set_internal_timestamp(unix_time=timegm_to_float(timestamp) + fraction)
def test_execute_clock_sync(self): """ Verify we can synchronize the instrument internal clock """ self.assert_enter_command_mode() self.assert_execute_resource(ProtocolEvent.CLOCK_SYNC) # get the time from the driver check_new_params = self.instrument_agent_client.get_resource([Parameter.CLOCK]) # convert driver's time from formatted date/time string to seconds integer instrument_time = timegm_to_float(time.strptime(check_new_params.get(Parameter.CLOCK).lower(), "%Y/%m/%d %H:%M:%S")) # need to convert local machine's time to date/time string and back to seconds to 'drop' the DST attribute so test passes # get time from local machine lt = time.strftime("%d %b %Y %H:%M:%S", time.gmtime(timegm_to_float(time.localtime()))) # convert local time from formatted date/time string to seconds integer to drop DST local_time = timegm_to_float(time.strptime(lt, "%d %b %Y %H:%M:%S")) # Now verify that the time matches to within 5 seconds self.assertLessEqual(abs(instrument_time - local_time), 5)
def _build_parsed_values(self): """ Take something in the data sample format and parse it into values with appropriate tags. @throws SampleException If there is a problem with sample creation """ match = SAMPLE_DATA_REGEX.match(self.raw_data) if not match: raise SampleException("Mavs4SampleDataParticle: No regex match of \ parsed sample data: [%s]", self.raw_data) try: fractional_second = int(match.group(3)) datetime = "%s %s.%s" % (match.group(1), match.group(2), fractional_second) datetime_nofrac = "%s %s" % (match.group(1), match.group(2)) timestamp = time.strptime(datetime_nofrac, "%m %d %Y %H %M %S") self.set_internal_timestamp(unix_time=(timegm_to_float(timestamp) + fractional_second)) velocity_frame_east = float(match.group(8)) velocity_frame_north = float(match.group(9)) velocity_frame_up = float(match.group(10)) temperature = float(match.group(11)) compass_mx = float(match.group(12)) compass_my = float(match.group(13)) pitch = float(match.group(14)) roll = float(match.group(15)) except (ValueError, TypeError, IndexError) as ex: raise SampleException("Error (%s) while decoding parameters in data: [%s]" % (ex, self.raw_data)) result = [{DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.DATE_TIME_STRING, DataParticleKey.VALUE: datetime}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_EAST, DataParticleKey.VALUE: velocity_frame_east}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_NORTH, DataParticleKey.VALUE: velocity_frame_north}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_UP, DataParticleKey.VALUE: velocity_frame_up}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.TEMPERATURE, DataParticleKey.VALUE: temperature}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.COMPASS_MX, DataParticleKey.VALUE: compass_mx}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.COMPASS_MY, DataParticleKey.VALUE: compass_my}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.PITCH, DataParticleKey.VALUE: pitch}, {DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.ROLL, DataParticleKey.VALUE: roll}] log.debug('Mavs4SampleDataParticle: particle=%s', result) return result
def assert_clock_set(self, time_param, sync_clock_cmd = DriverEvent.ACQUIRE_STATUS, timeout = 60, tolerance=DEFAULT_CLOCK_DIFF): """ Verify the clock is set to at least the current date """ log.debug("verify clock is set to the current time") timeout_time = time.time() + timeout while not self._is_time_set(time_param, timegm_to_float(time.gmtime()), tolerance=tolerance): log.debug("time isn't current. sleep for a bit") # Run acquire status command to set clock parameter self.assert_driver_command(sync_clock_cmd) log.debug("T: %s T: %s", time.time(), timeout_time) time.sleep(5) self.assertLess(time.time(), timeout_time, msg="Timeout waiting for clock sync event")
def _build_parsed_values(self): """ Take something in the data sample format and parse it into values with appropriate tags. @throws SampleException If there is a problem with sample creation """ match = SAMPLE_DATA_REGEX.match(self.raw_data) if not match: raise SampleException( "Mavs4SampleDataParticle: No regex match of \ parsed sample data: [%s]", self.raw_data) try: fractional_second = int(match.group(3)) datetime = "%s %s.%s" % (match.group(1), match.group(2), fractional_second) datetime_nofrac = "%s %s" % (match.group(1), match.group(2)) timestamp = time.strptime(datetime_nofrac, "%m %d %Y %H %M %S") self.set_internal_timestamp(unix_time=(timegm_to_float(timestamp) + fractional_second)) velocity_frame_east = float(match.group(8)) velocity_frame_north = float(match.group(9)) velocity_frame_up = float(match.group(10)) temperature = float(match.group(11)) compass_mx = float(match.group(12)) compass_my = float(match.group(13)) pitch = float(match.group(14)) roll = float(match.group(15)) except (ValueError, TypeError, IndexError) as ex: raise SampleException( "Error (%s) while decoding parameters in data: [%s]" % (ex, self.raw_data)) result = [{ DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.DATE_TIME_STRING, DataParticleKey.VALUE: datetime }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_EAST, DataParticleKey.VALUE: velocity_frame_east }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_NORTH, DataParticleKey.VALUE: velocity_frame_north }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.VELOCITY_FRAME_UP, DataParticleKey.VALUE: velocity_frame_up }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.TEMPERATURE, DataParticleKey.VALUE: temperature }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.COMPASS_MX, DataParticleKey.VALUE: compass_mx }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.COMPASS_MY, DataParticleKey.VALUE: compass_my }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.PITCH, DataParticleKey.VALUE: pitch }, { DataParticleKey.VALUE_ID: Mavs4SampleDataParticleKey.ROLL, DataParticleKey.VALUE: roll }] log.debug('Mavs4SampleDataParticle: particle=%s', result) return result
def _build_parsed_values(self): # Initialize matches = {} lines = self.raw_data.split(NEWLINE) match = self.RE01.match(lines[1]) timestamp = match.group(1) matches[AdcpCompassCalibrationKey.FLUXGATE_CALIBRATION_TIMESTAMP] = timegm_to_float( time.strptime(timestamp, "%m/%d/%Y %H:%M:%S")) match = self.RE04.match(lines[4]) matches[AdcpCompassCalibrationKey.S_INVERSE_BX] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE05.match(lines[5]) matches[AdcpCompassCalibrationKey.S_INVERSE_BY] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE06.match(lines[6]) matches[AdcpCompassCalibrationKey.S_INVERSE_BZ] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE07.match(lines[7]) matches[AdcpCompassCalibrationKey.S_INVERSE_ERR] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE11.match(lines[11]) matches[AdcpCompassCalibrationKey.COIL_OFFSET] = [float(match.group(1))] match = self.RE12.match(lines[12]) matches[AdcpCompassCalibrationKey.COIL_OFFSET].append(float(match.group(1))) match = self.RE13.match(lines[13]) matches[AdcpCompassCalibrationKey.COIL_OFFSET].append(float(match.group(1))) match = self.RE14.match(lines[14]) matches[AdcpCompassCalibrationKey.COIL_OFFSET].append(float(match.group(1))) match = self.RE18.match(lines[18]) matches[AdcpCompassCalibrationKey.ELECTRICAL_NULL] = float(match.group(1)) match = self.RE21.match(lines[21]) timestamp = match.group(1) matches[AdcpCompassCalibrationKey.TILT_CALIBRATION_TIMESTAMP] = timegm_to_float( time.strptime(timestamp, "%m/%d/%Y %H:%M:%S")) match = self.RE22.match(lines[22]) matches[AdcpCompassCalibrationKey.CALIBRATION_TEMP] = float(match.group(1)) match = self.RE27.match(lines[27]) matches[AdcpCompassCalibrationKey.ROLL_UP_DOWN] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE28.match(lines[28]) matches[AdcpCompassCalibrationKey.PITCH_UP_DOWN] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE32.match(lines[32]) matches[AdcpCompassCalibrationKey.OFFSET_UP_DOWN] = [float(match.group(1)), float(match.group(2)), float(match.group(3)), float(match.group(4))] match = self.RE36.match(lines[36]) matches[AdcpCompassCalibrationKey.TILT_NULL] = float(match.group(1)) result = [] for key, value in matches.iteritems(): result.append({DataParticleKey.VALUE_ID: key, DataParticleKey.VALUE: value}) return result