def evrs(self): """Getter EVRs dictionary""" if self._evrs is None: import ait.core.evr as evr self._evrs = evr.getDefaultDict() return self._evrs
def test_evr_no_formatters_found(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] input_data = bytearray([0x21]) example.message = "%M this formatter doesn't exist" result = example.format_message(input_data) assert result == example.message
def test_evr_message_format_single_formatter(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "Unexpected length for %c command." exclamation = bytearray([0x21]) expected = "Unexpected length for ! command." result = example.format_message(exclamation) assert result == expected
def test_evr_message_format_multiple_formatters(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "Unexpected length for %c command %s and %u." input_data = bytearray( [0x21, 0x46, 0x6f, 0x6f, 0x00, 0xff, 0x11, 0x33, 0x44]) expected = "Unexpected length for ! command Foo and 4279317316." result = example.format_message(input_data) assert result == expected
def test_bad_formatter_parsing(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "Unexpected length for %c command %s and %d." input_data = bytearray([0x21]) msg = "Unable to format EVR Message with data {}".format(input_data) try: result = example.format_message(input_data) assert False except ValueError as e: assert str(e) == msg
def test_evr_message_format_complex_formatters(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "Unexpected length for %c command %s and %llu." input_data = bytearray([ 0x21, 0x46, 0x6f, 0x6f, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0x11, 0x33, 0x44 ]) expected = "Unexpected length for ! command Foo and 9223372041134093124." result = example.format_message(input_data) assert result == expected
def test_standard_formatter_handling(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = '%c' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '\x01' example.message = '%d' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '16909060' example.message = '%u' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '16909060' example.message = '%i' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '16909060' example.message = '%x' result = example.format_message(bytearray([0x00, 0x00, 0x00, 0x0f])) assert result == 'f' example.message = '%X' result = example.format_message(bytearray([0x00, 0x00, 0x00, 0x0f])) assert result == 'F' example.message = '%f' result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == '123.438743' example.message = '%e' result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == '1.234387e+02' example.message = '%E' result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == '1.234387E+02' example.message = '%g' result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == '123.439'
def test_standard_formatter_handling(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "%c" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "\x01" example.message = "%d" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "16909060" example.message = "%u" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "16909060" example.message = "%i" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "16909060" example.message = "%x" result = example.format_message(bytearray([0x00, 0x00, 0x00, 0x0F])) assert result == "f" example.message = "%X" result = example.format_message(bytearray([0x00, 0x00, 0x00, 0x0F])) assert result == "F" example.message = "%f" result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == "123.438743" example.message = "%e" result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == "1.234387e+02" example.message = "%E" result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == "1.234387E+02" example.message = "%g" result = example.format_message( bytearray([0x40, 0x5E, 0xDC, 0x14, 0x5D, 0x85, 0x16, 0x55])) assert result == "123.439"
def test_complex_formatter_handling(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = '%hhu' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '1' example.message = '%hu' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '258' example.message = '%lu' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '16909060' example.message = '%llu' result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == '72623859790382856'
def test_complex_formatter_handling(): evr_dicts = evr.getDefaultDict() example = evr_dicts.codes[1] example.message = "%hhu" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "1" example.message = "%hu" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "258" example.message = "%lu" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "16909060" example.message = "%llu" result = example.format_message( bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) assert result == "72623859790382856"
def create_packets_from_results(self, packet_name, result_set): ''' Generate AIT Packets from a InfluxDB query ResultSet Extract Influx DB query results into one packet per result entry. This assumes that telemetry data was inserted in the format generated by :func:`InfluxDBBackend.insert`. Complex types such as CMD16 and EVR16 are evaluated if they can be properly encoded from the raw value in the query result. If there is no opcode / EVR-code for a particular raw value the value is skipped (and thus defaulted to 0). Arguments packet_name (string) The name of the AIT Packet to create from each result entry result_set (influxdb.resultset.ResultSet) The query ResultSet object to convert into packets Returns A list of packets extracted from the ResultSet object or None if an invalid packet name is supplied. ''' try: pkt_defn = tlm.getDefaultDict()[packet_name] except KeyError: log.error('Unknown packet name {} Unable to unpack ResultSet'.format(packet_name)) return None pkt = tlm.Packet(pkt_defn) pkts = [] for r in result_set.get_points(): new_pkt = tlm.Packet(pkt_defn) for f, f_defn in pkt_defn.fieldmap.iteritems(): field_type_name = f_defn.type.name if field_type_name == 'CMD16': if cmd.getDefaultDict().opcodes.get(r[f], None): setattr(new_pkt, f, cmd_def.name) elif field_type_name == 'EVR16': if evr.getDefaultDict().codes.get(r[f], None): setattr(new_pkt, f, r[f]) elif field_type_name == 'TIME8': setattr(new_pkt, f, r[f] / 256.0) elif field_type_name == 'TIME32': new_val = dmc.GPS_Epoch + dt.timedelta(seconds=r[f]) setattr(new_pkt, f, new_val) elif field_type_name == 'TIME40': sec = int(r[f]) microsec = r[f] * 1e6 new_val = dmc.GPS_Epoch + dt.timedelta(seconds=sec, microseconds=microsec) setattr(new_pkt, f, new_val) elif field_type_name == 'TIME64': sec = int(r[f]) microsec = r[f] % 1 * 1e6 new_val = dmc.GPS_Epoch + dt.timedelta(seconds=sec, microseconds=microsec) setattr(new_pkt, f, new_val) else: try: setattr(new_pkt, f, r[f]) except KeyError: log.info('Field not found in query results {} Skipping ...'.format(f)) pkts.append(new_pkt) return pkts
def evrs(self): """Getter EVRs dictionary""" if self._evrs is None: self._evrs = evr.getDefaultDict() return self._evrs
def handle(): """Return JSON EVR dictionary""" return json.dumps(evr.getDefaultDict().toJSON())
def test_evr_load(): evr_dicts = evr.getDefaultDict() assert len(evr_dicts.keys()) == 4 assert evr_dicts.codes[1].name == "NO_ERROR"
def create_packet_from_result(cls, packet_id, result): """Create an AIT Packet from an InfluxDB query ResultSet item Extract Influx DB query results entry into an AIT packet. This assumes that telemetry data was inserted in the format generated by :func:`InfluxDBBackend.insert`. Complex types such as CMD16 and EVR16 are evaluated if they can be properly encoded from the raw value in the query result. If there is no opcode / EVR-code for a particular raw value the value is skipped (and thus defaulted to 0). TODO: Reevaluate this assumption that missing opcodes / EVR-codes should be left as 0 if a match isn't found in the dictionary. Arguments packet_id (string or PacketDefinition) The "id" for the packet to create. If packet_id is a string it must name a valid PacketDefintion in the telemetry dictionary. Otherwise, it must be a PacketDefinition. result (dict) The :class:`influxdb.resultset.ResultSet` entry from which values should be extracted to form the AIT packet Returns A :class:`ait.core.tlm.Packet` with values initialized from the values in the ResultSet entry. If a field cannot be located in the result entry it will left as the default value in the Packet or set to None if it's a CMD / EVR type. """ if isinstance(packet_id, str): try: pkt_defn = tlm.getDefaultDict()[packet_id] except KeyError: log.error( f"Unknown packet name {packet_id} Unable to unpack ResultSet" ) return None elif isinstance(packet_id, tlm.PacketDefinition): pkt_defn = packet_id else: log.error( f"Unknown packet id type {packet_id}. Unable to unpack ResultSet" ) return None new_pkt = tlm.Packet(pkt_defn) cmd_dict = cmd.getDefaultDict() evr_dict = evr.getDefaultDict() for f, f_defn in pkt_defn.fieldmap.items(): field_type_name = f_defn.type.name if field_type_name == "CMD16": if cmd_dict.opcodes.get(result[f], None): cmd_def = cmd_dict.opcodes.get(result[f]) setattr(new_pkt, f, cmd_def.name) elif field_type_name == "EVR16": if evr_dict.codes.get(result[f], None): evr_def = evr_dict.codes.get(result[f]) setattr(new_pkt, f, evr_def.name) elif field_type_name == "TIME8": setattr(new_pkt, f, result[f] / 256.0) elif field_type_name == "TIME32": new_val = dmc.GPS_Epoch + dt.timedelta(seconds=result[f]) setattr(new_pkt, f, new_val) elif field_type_name == "TIME40": sec = int(result[f]) microsec = result[f] % 1 * 1e6 new_val = dmc.GPS_Epoch + dt.timedelta(seconds=sec, microseconds=microsec) setattr(new_pkt, f, new_val) elif field_type_name == "TIME64": sec = int(result[f]) microsec = result[f] % 1 * 1e6 new_val = dmc.GPS_Epoch + dt.timedelta(seconds=sec, microseconds=microsec) setattr(new_pkt, f, new_val) else: try: setattr(new_pkt, f, result[f]) except KeyError: log.info( "Field not found in query results {} Skipping ...". format(f)) return new_pkt