def boardupdate(self, boarddata=None): super().boardupdate() if boarddata is None: return if self.motor_port is None: if boarddata["fw"] in POSSIBLE_MOTOR_FIRMWARES: self.set_motor_port(boarddata["port"]) if self.motor_port == boarddata["port"]: try: self.ws.write_to_socket( commandmessage( cmd="set_acceleration", sender=self.name, target="gui", acceleration=list(boarddata["acceleration"])[0], )) self.ws.write_to_socket( commandmessage( cmd="set_steps_per_mm", sender=self.name, target="gui", steps_per_mm=list(boarddata["steps_per_mm"])[0], )) self.ws.write_to_socket( commandmessage( cmd="set_max_mm_sec", sender=self.name, target="gui", max_mm_sec=list(boarddata["max_mm_sec"])[0], )) except: self.logger.exception(Exception)
def ask_for_identification(self): self.server.logger.debug("ask for identification") self.sendMsg( commandmessage( sender="server", cmd="indentify", requires_password=self.server.password is not None, )) self.sendMsg( commandmessage(sender="server", cmd="set_time", time=self.server.t0))
def get_vials(self, data_target="gui"): self.ws.write_to_socket( commandmessage( cmd="set_vials", sender=self.name, target=data_target, vials=self.config.get("vials", default=[]), ))
def identify(self, data): cmd_data = data["data"] if not self.server.verify_password(cmd_data["kwargs"].get( "password", "")): self.sendMsg(commandmessage(sender="server", cmd="password_reset")) self.ask_for_identification() if "name" in cmd_data["kwargs"]: self.name = cmd_data["kwargs"]["name"] self.identified = True
def get_frame_size(self, data_target="gui"): self.ws.write_to_socket( commandmessage( cmd="set_frame_size", sender=self.name, target=data_target, width=self.config.get("frame", "size", "width", default=100), height=self.config.get("frame", "size", "height", default=100), savedist=self.config.get("frame", "savedist", default=0), ))
def set_steps_per_mm(self, steps_per_mm=None): if steps_per_mm is None or self.motor_port is None: return self.ws.write_to_socket( commandmessage( cmd="boardfunction", board_cmd="set_steps_per_mm", sender=self.name, target=self.motor_port, steps_per_mm=[steps_per_mm, steps_per_mm, 200], ))
def set_acceleration(self, acceleration=None): if acceleration is None or self.motor_port is None: return self.ws.write_to_socket( commandmessage( cmd="boardfunction", board_cmd="set_acceleration", sender=self.name, target=self.motor_port, acceleration=[acceleration, acceleration, 1], ))
def set_max_mm_sec(self, max_mm_sec=None): if max_mm_sec is None or self.motor_port is None: return self.ws.write_to_socket( commandmessage( cmd="boardfunction", board_cmd="set_max_mm_sec", sender=self.name, target=self.motor_port, max_mm_sec=[max_mm_sec, max_mm_sec, 10], ))
def set_position(self, x, y): if self.motor_port is None: return if self.switchxy: x, y = y * (-1 if self.iverty else 1), x * (-1 if self.ivertx else 1) self.ws.write_to_socket( commandmessage(cmd="boardfunction", sender=self.name, target=self.motor_port, **dict(board_cmd="set_position", xyz=[x + y, x - y, 0])))
def move_to(self, x, y): if self.motor_port is None: return w = self.config.get("frame", "size", "width", default=400) h = self.config.get("frame", "size", "height", default=400) sd = self.config.get("frame", "savedist", default=0) x = min(max(sd, x), w - sd) y = min(max(sd, y), h - sd) if self.switchxy: x, y = y * (-1 if self.iverty else 1), x * (-1 if self.ivertx else 1) self.ws.write_to_socket( commandmessage(cmd="boardfunction", sender=self.name, target=self.motor_port, **dict(board_cmd="move_to", xyz=[x + y, x - y, 0])))
def pass_ball( self, catch=None ): #methods called by the message validator should have default attributes to be valid against "TypeError: pass_ball() missing 1 required positional argument: 'catch'" if catch is None: print("End of the game because the ball went missing somewhere") return print(self.pingpong) # prints ping or pong if not catch: print("Damn!") # dont like losing the ball time.sleep( 1 ) # delay in game because it would be way to fast without, maybe the flytime of the ball :) self.wsc.write_to_socket( commandmessage( cmd="pass_ball", # the command to call at the reciver sender=self.name, # sender of the command target=self.opponent, # reciver catch=random.random() < 0.8, # 80% ball catch ))
def __init__(self, name, logger=None, host=None, reconnect=True, password=None): self.password = password name = str(name) self.message_cmd_functions = {} self.message_types = {} self.ws = None self.time = time.time() self._on_open_functions = {} self._on_error_functions = {} self._on_close_functions = {} self._on_message_functions = {} if logger is None: logger = logging.getLogger("ListenWebsocket_" + name) self.logger = logger self.ws_thread = None self.reconnect = reconnect self.host = None self.reconnect_time = 1 self.name = name self.add_on_message(name="default", func=self.default_messagevalidator) self.add_message_type("cmd", self.default_command_validator) self.add_cmd_function( "indentify", lambda: self.write_to_socket( commandmessage(cmd="indentify", sender=self.name, name=self.name, password=self.password)), ) self.add_cmd_function("set_time", self.set_time) if host is not None: self.connect_to_socket(host)