def get_pdm_address(): r = RileyLink() try: verify_auth(request) while True: timeout = 30000 if request.args.get('timeout') is not None: timeout = int(request.args.get('timeout')) * 1000 if timeout > 30000: raise RestApiException("Timeout cannot be more than 30 seconds") data = r.get_packet(timeout) if data is None: p = None break if data is not None and len(data) > 2: calc = crc8(data[2:-1]) if data[-1] == calc: p = Packet.from_data(data[2:-1]) break if p is None: raise RestApiException("No pdm packet detected") return {"radio_address": p.address, "radio_address_hex": "%8X" % p.address} finally: r.disconnect(ignore_errors=True)
class MockPacketRadio(PacketRadio): def __init__(self, send_callback=None, allow_connect=False, allow_listen=False): self.radio = RileyLink() self.response_packets = Queue() self.responses = Queue() self.last_pdm_message_sequence = 0 self.last_pdm_packet_sequence = 0 self.send_callback = send_callback self.allow_listen = allow_listen self.allow_connect = allow_connect def add_response(self, response, msg_address, pkt_address, critical): self.responses.put((response, msg_address, pkt_address, critical)) def add_response_packet(self, response_packet): self.response_packets.put(response_packet) def connect(self, force_initialize=False): if self.allow_connect: return self.radio.connect(force_initialize=force_initialize) def disconnect(self, ignore_errors=True): if self.allow_connect: return self.radio.disconnect(ignore_errors=ignore_errors) def get_info(self): if self.allow_connect: return self.radio.get_info() def init_radio(self, force_init=False): if self.allow_connect: return self.radio.init_radio(force_init=force_init) def tx_up(self): if self.allow_connect: return self.radio.tx_up() def tx_down(self): if self.allow_connect: return self.radio.tx_down() def set_tx_power(self, tx_power): if self.allow_connect: return self.radio.set_tx_power(tx_power=tx_power) def _translate_responses(self): if self.response_packets.empty(): if not self.responses.empty(): msg, msg_address, pkt_address, critical = self.responses.get() packets = msg.get_radio_packets(msg_address, (self.last_pdm_message_sequence + 1) % 16, pkt_address, (self.last_pdm_packet_sequence + 1) % 32, critical) for packet in packets: self.response_packets.put(packet) def get_packet(self, timeout=5.0): self._translate_responses() if not self.response_packets.empty(): p = self.response_packets.get() if p is not None: return bytes([0xff, 0xff]) + p.get_data() if self.allow_listen: return self.radio.get_packet(timeout=timeout) else: return None def send_and_receive_packet(self, packet, repeat_count, delay_ms, timeout_ms, retry_count, preamble_ext_ms): send_packet = RadioPacket.parse(packet) self.last_pdm_packet_sequence = send_packet.sequence if send_packet.type == RadioPacketType.PDM: self.last_pdm_message_sequence = (send_packet.body[4] >> 2) & 0x0f self._translate_responses() if not self.response_packets.empty(): p = self.response_packets.get() if p is not None: return bytes([0xff, 0xff]) + p.get_data() if self.allow_listen: if self.send_callback is not None: packet = self.send_callback(send_packet) if packet is None: return None else: packet = packet.get_data() return self.radio.send_and_receive_packet(packet=packet, repeat_count=repeat_count, delay_ms=delay_ms, timeout_ms=timeout_ms, retry_count=retry_count, preamble_ext_ms=preamble_ext_ms) else: return None def send_packet(self, packet, repeat_count, delay_ms, preamble_extension_ms): if self.allow_listen: send_packet = RadioPacket.parse(packet) if send_packet is not None: self.last_pdm_packet_sequence = send_packet.sequence if self.send_callback is not None: packet = self.send_callback(send_packet) if packet is None: return None else: packet = packet.get_data() return self.radio.send_packet(packet=packet, repeat_count=repeat_count, delay_ms=delay_ms, preamble_extension_ms=preamble_extension_ms) else: return