def send_ports(self, data_target=None): if data_target is None: data_target = self.data_targets if not isinstance(data_target, set): data_target = set(data_target) for data_target in data_target: filter_dict.call_method( target=data_target.set_ports, kwargs=dict( available_ports=list(self.available_ports), ignored_ports=list( self.ignored_ports | self.permanently_ignored_ports ), connected_ports=[ dict(port=sp.port, baudrate=sp.baudrate) for sp in self.connected_ports ], identified_ports=[ dict(port=sp.port, baudrate=sp.baudrate) for sp in self.identified_ports ], ), )
def get_board(self, data_target=None): if data_target is None: return board = self.board.get_board() filter_dict.call_method(data_target.set_board, kwargs={ "port": self.port, "board": board })
def send_board_data(self, **kwargs): if self.board is not None: if self.board.identified: data = self.board.save() self.config.put("board_data", self.board.id, value=data) data["firmware"] = self.board.firmware data["id"] = self.board.id data["class"] = self.board.CLASSNAME msg_d = dict(board_data=data, **kwargs) for data_target in self.data_targets: filter_dict.call_method(data_target.board_update, kwargs=msg_d) self.logger.debug(msg_d)
def set_board(self, board_class): self.board = board_class() self.board.set_serial_port(self) time.sleep(2) self.board.identify() if not self.board.identified: self.stop_read() self.serial_reader.ignored_ports.add(self.port) self.logger.error("unable to identify " + self.port) raise PortIdentifyError() if self.board.FIRMWARE != self.board.firmware: self.logger.warning("firmware detected {}".format( self.board.firmware)) newb = board_by_firmware(self.board.firmware) if newb is not None: return self.set_board(newb["classcaller"]) else: self.stop_read() self.serial_reader.ignored_ports.add(self.port) self.logger.error("firmware not found " + str(self.board.firmware)) raise FirmwareNotFoundError() # self.board.specific_identification() if not self.board.identified: self.stop_read() self.serial_reader.ignored_ports.add(self.port) raise ValueError("unable to specificidentify " + self.port + "with firmware:" + str(self.board.firmware)) self.logger.info(str(self.port) + " identified ") self.serial_reader.identified_ports.add(self) self.config.put("portdata", self.port, "baud", value=self.baudrate) self.config.put("portdata", self.port, "firmware", value=self.board.firmware) self.board.restore( self.config.get("board_data", self.board.id, default={})) self.board.get_portcommand_by_name("identify").sendfunction(True) for data_target in self.serial_reader.data_targets: filter_dict.call_method(data_target.port_identified, kwargs={"port": self.port}) return True
def initialize(self, instance, name): self.board = instance new_var = filter_dict.call_method(self.targetclass, kwargs=self.__dict__) new_var.name = name setattr(instance, name, new_var) return new_var
def start_read(self): self.logger.info("port opened " + self.port) for data_target in self.serial_reader.data_targets: filter_dict.call_method( data_target.port_opened, kwargs={ "port": self.port, "baud": self.baudrate }, ) if not self.is_open: self.open() self.work_thread = threading.Thread(target=self.work_port, ) self.update_thread = threading.Thread(target=self.board_updater, ) self.work_thread.start() self.update_thread.start()
def add_data_point(self, board, key, y, x=None): t = (int(1000 * (time.time() - self.time) / self.datapoint_resolution) * self.datapoint_resolution) if x is None: x = t try: for data_target in self.data_targets: filter_dict.call_method( target=data_target.port_data_point, kwargs=dict(key=key, x=x, y=y, port=board.port, board=board.id), ) except RuntimeError: pass
def parse_command(self, data): if hasattr(self, data["cmd"]) and not "api" in data["data"]: answer = filter_dict.call_method( getattr(self, data["cmd"]), kwargs=data["data"] ) else: api = data["data"]["api"] del data["data"]["api"] answer = filter_dict.call_method( getattr(self.apis[api], data["cmd"]), kwargs=data["data"] ) if answer is not None: if not isinstance(answer, dict): answer = {"data": answer} answer["api_position"] = api if answer is not None: self.to_client( dict(cmd=data["cmd"].replace("get_", "set_"), data=answer), type="cmd" )
def stop_read(self): self.close() try: self.work_thread.join() except (RuntimeError, AttributeError): pass self.work_thread = None try: self.update_thread.join() except (RuntimeError, AttributeError): pass self.close() self.update_thread = None self.logger.info("port closed " + self.port) for data_target in self.serial_reader.data_targets: filter_dict.call_method(data_target.port_closed, kwargs={"port": self.port}) if self in self.serial_reader.connected_ports: self.serial_reader.connected_ports.remove(self) if self in self.serial_reader.identified_ports: self.serial_reader.identified_ports.remove(self) del self
def cmd_message(self, data): if not isinstance(data, dict): logger.error("Invalid command structure: " + str(data)) return cmd = data.get("cmd") try: if "data" not in data: data["data"] = {} data["data"]["data_target"] = self func = getattr(self.local_object, cmd) if func is None: logger.error("Unknown command: " + str(cmd)) ans = filter_dict.call_method(target=func, kwargs=data["data"]) if ans is not None: data = {"cmd": cmd, "data": ans} for target in self.ws_targets: target.to_client(data, type="cmd") # except AttributeError as e: # logger.error("Unknown command: "+ str(cmd)) except Exception as e: logger.exception(e)