From 45d9c321466c50f47fc0558541779d9129ac819c Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sun, 14 Feb 2021 17:53:28 +0000 Subject: [PATCH] Handle setting Channel Controls from WS. --- helpers/types.py | 6 +++--- player.py | 39 ++++++++++++++------------------------- server.py | 2 +- websocket_server.py | 14 +++++++++++++- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/helpers/types.py b/helpers/types.py index dea8a5d..e778271 100644 --- a/helpers/types.py +++ b/helpers/types.py @@ -12,9 +12,9 @@ class ServerState(TypedDict): num_channels: int class RepeatMode(Enum): - NONE = 0 - ONE = 1 - ALL = 2 + none = 0 + one = 1 + all = 2 class PlayerState(TypedDict): initialised: bool diff --git a/player.py b/player.py index 786f9ac..aef8b9e 100644 --- a/player.py +++ b/player.py @@ -68,7 +68,7 @@ class Player(): "remaining": 0, "length": 0, "auto_advance": True, - "repeat": "NONE", # NONE, ONE or ALL + "repeat": "none", # none, one or all "play_on_load": False, "output": None, "show_plan": [] @@ -209,32 +209,21 @@ class Player(): self._updateState(pos=pos) return True - def set_auto_advance(self, message: int) -> bool: - if message == 0: - self.state.update("auto_advance", False) - return True # It did it - elif message == 1: - self.state.update("auto_advance", True) - return True - else: - return False + def set_auto_advance(self, message: bool) -> bool: + self.state.update("auto_advance", message) + return True + def set_repeat(self, message: str) -> bool: - if message in ["ALL", "ONE", "NONE"]: + if message in ["all", "one", "none"]: self.state.update("repeat", message) return True else: return False - def set_play_on_load(self, message: int) -> bool: - if message == 0: - self.state.update("play_on_load", False) - return True # It did it - elif message == 1: - self.state.update("play_on_load", True) - return True - else: - return False + def set_play_on_load(self, message: bool) -> bool: + self.state.update("play_on_load", message) + return True # Show Plan Related Methods def get_plan(self, message: int): @@ -389,7 +378,7 @@ class Player(): global already_stopped - print(already_stopped, self.state.state["remaining"], self.isPlaying) + #print(already_stopped, self.state.state["remaining"], self.isPlaying) if loaded_item == None or already_stopped or (self.state.state["remaining"] > 1): return @@ -465,7 +454,7 @@ class Player(): response += "FAIL:" + msg else: response += "FAIL" - self.logger.log.info(("Preparing to send: {}".format(response))) + self.logger.log.debug(("Preparing to send: {}".format(response))) if self.out_q: self.logger.log.info(("Sending: {}".format(response))) self.out_q.put(response) @@ -528,6 +517,7 @@ class Player(): try: try: self.last_msg = in_q.get_nowait() + self.logger.log.info("Recieved message: {}".format(self.last_msg)) except Empty: # The incomming message queue was empty, # skip message processing @@ -542,7 +532,6 @@ class Player(): self._retMsg(self.output(split[1])) elif self.isInit: - message_types: Dict[str, Callable[..., Any]] = { # TODO Check Types "STATUS": lambda: self._retMsg(self.status, True), @@ -552,9 +541,9 @@ class Player(): "UNPAUSE": lambda: self._retMsg(self.unpause()), "STOP": lambda: self._retMsg(self.stop()), "SEEK": lambda: self._retMsg(self.seek(float(self.last_msg.split(":")[1]))), - "AUTOADVANCE": lambda: self._retMsg(self.set_auto_advance(int(self.last_msg.split(":")[1]))), + "AUTOADVANCE": lambda: self._retMsg(self.set_auto_advance((self.last_msg.split(":")[1] == "True"))), "REPEAT": lambda: self._retMsg(self.set_repeat(self.last_msg.split(":")[1])), - "PLAYONLOAD": lambda: self._retMsg(self.set_play_on_load(int(self.last_msg.split(":")[1]))), + "PLAYONLOAD": lambda: self._retMsg(self.set_play_on_load((self.last_msg.split(":")[1] == "True"))), # Show Plan Items "GET_PLAN": lambda: self._retMsg(self.get_plan(int(self.last_msg.split(":")[1]))), diff --git a/server.py b/server.py index 45e483c..739f1f9 100644 --- a/server.py +++ b/server.py @@ -80,7 +80,7 @@ class PlayerHandler(): try: message = channel_from_q[channel].get_nowait() websocket_to_q[channel].put(message) - print("Player Handler saw:", message.split(":")[0]) + #print("Player Handler saw:", message.split(":")[0]) ui_to_q[channel].put(message) except: pass diff --git a/websocket_server.py b/websocket_server.py index b5fe526..babc9f6 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -38,6 +38,18 @@ async def websocket_handler(websocket, path): channel_to_q[channel].put("SEEK:" + str(data["time"])) elif data["command"] == "LOAD": channel_to_q[channel].put("LOAD:" + str(data["weight"])) + + elif data["command"] == "AUTOADVANCE": + channel_to_q[channel].put("AUTOADVANCE:" + str(data["enabled"])) + + elif data["command"] == "PLAYONLOAD": + channel_to_q[channel].put("PLAYONLOAD:" + str(data["enabled"])) + + elif data["command"] == "REPEAT": + channel_to_q[channel].put("REPEAT:" + str(data["mode"]).lower()) + + + # Wasteland elif data["command"] == "ADD": if "managedId" in data["newItem"].keys() and isinstance(data["newItem"]["managedId"], str): if data["newItem"]["managedId"].startswith("managed"): @@ -77,7 +89,7 @@ async def websocket_handler(websocket, path): try: message = webstudio_to_q[channel].get_nowait() command = message.split(":")[0] - print("Websocket Out:", command) + #print("Websocket Out:", command) if command == "STATUS": try: message = message.split("OKAY:")[1]