def header(self, byte_stream: BytesIO) -> Header: """ Header extractor helper. :param byte_stream: :type byte_stream: :return: :rtype: """ # Get ID subpacket_id: int = byte_stream.read(1)[0] # check that id is valid: if subpacket_id not in self.packet_type_to_parser: LOGGER.error("Subpacket id %d not valid.", subpacket_id) raise ValueError("Subpacket id " + str(subpacket_id) + " not valid.") # Get timestamp timestamp: int = self.bytestoint(byte_stream.read(4)) return Header(subpacket_id, timestamp)
def _run(self): LOGGER.debug( f"SIM connection started (device_address={self.device_address})") try: while True: id = self.stdout.read(1)[0] # Returns 0 if process was killed if id not in SimConnection.packetHandlers.keys(): LOGGER.error( f"SIM protocol violation!!! Shutting down. (device_address={self.device_address})" ) for b in self.stdout.getHistory(): LOGGER.error(hex(b[0])) LOGGER.error("^^^^ violation.") return # Call packet handler SimConnection.packetHandlers[id](self) except Exception as ex: with self._shutdown_lock: if not self._is_shutting_down: LOGGER.exception( f"Error in SIM connection. (device_address={self.device_address})" ) LOGGER.warning( f"SIM connection thread shut down (device_address={self.device_address})" )
def run(self): """ """ LOGGER.debug("Send thread started") # TODO : Once we have multiple connections, we will loop over and send a config request to each # Starting up, request hello/ha ndshake/identification for connection in self.connections.values(): try: connection.broadcast(self.command_parser.broadcast_data(CommandType.CONFIG)) except Exception as ex: LOGGER.exception("Exception in send thread while sending config requests") while True: try: message = self.commandQueue.get(block=True, timeout=None) # Block until something new self.commandQueue.task_done() if message is None: # Either received None or woken up for shutdown with self._shutdown_lock: if self._is_shutting_down: break else: continue try: (device, command, data) = self.command_parser.pase_command(message) except CommandParsingError as ex: LOGGER.error(f"Error parsing command: {str(ex)}") continue full_address = self.device_manager.get_full_address(device) if full_address is None: LOGGER.error(f"Device not yet connected: {device.name}") continue connection = self.connections[full_address.connection_name] LOGGER.info(f"Sending command {command.name} to device {device.name} ({full_address})") connection.send(full_address.device_address, data) LOGGER.info("Sent command!") COMMAND_SENT_EVENT.increment() except TimeoutException: # TODO: Connection should have converted this to a generic exception for decoupling LOGGER.error("Message timed-out!") except queue.Empty: pass except Exception as ex: LOGGER.exception("Unexpected error while sending!") # Automatically grabs and prints exception info LOGGER.warning("Send thread shut down")
def run(self): """ """ LOGGER.debug("Send thread started") # TODO : Once we have multiple connections, we will loop over and send a config request to each # Starting up, request hello/ha ndshake/identification for connection in self.connections.values(): try: connection.broadcast(bytes([CommandType.CONFIG.value])) except Exception as ex: LOGGER.exception( "Exception in send thread while sending config requests") while True: try: message = self.commandQueue.get( block=True, timeout=None) # Block until something new self.commandQueue.task_done() if message is None: # Either received None or woken up for shutdown with self._shutdown_lock: if self._is_shutting_down: break else: continue message_parts = message.split('.') if len(message_parts) != 2: LOGGER.error("Bad command format") continue (device_str, command_str) = message_parts try: device = DeviceType[device_str.upper()] except KeyError: LOGGER.error(f"Unknown device: {device_str}") continue full_address = self.device_manager.get_full_address(device) if full_address is None: LOGGER.error(f"Device not yet connected: {device.name}") continue connection = self.connections[full_address.connection_name] try: command = CommandType[command_str.upper()] except KeyError: LOGGER.error(f"Unknown command {command_str}") continue LOGGER.info( f"Sending command {command} to device {device.name} ({full_address})" ) data = bytes([command.value]) connection.send(full_address.device_address, data) LOGGER.info("Sent command!") COMMAND_SENT_EVENT.increment() except TimeoutException: # TODO: Connection should have converted this to a generic exception for decoupling LOGGER.error("Message timed-out!") except queue.Empty: pass except Exception as ex: LOGGER.exception( "Unexpected error while sending!" ) # Automatically grabs and prints exception info LOGGER.warning("Send thread shut down")