diff --git a/plan.py b/plan.py index f76b9e6..f6d190f 100644 --- a/plan.py +++ b/plan.py @@ -15,15 +15,16 @@ from typing import Dict import os + class PlanObject: - _timeslotitemid: int = 0 + _channel_weight: int = 0 _filename: str = "" _title: str = "" _artist: str = "" @property - def timeslotitemid(self) -> int: - return self._timeslotitemid + def channel_weight(self) -> int: + return self._channel_weight @property def filename(self) -> str: @@ -36,7 +37,7 @@ class PlanObject: @property def __dict__(self) -> Dict[str, any]: return { - "timeslotitemid": self.timeslotitemid, + "channel_weight": self.channel_weight, "title": self._title, "artist": self._artist, "name": self.name, @@ -44,7 +45,7 @@ class PlanObject: } def __init__(self, new_item: Dict[str, any]): - self._timeslotitemid = new_item["timeslotitemid"] + self._channel_weight = new_item["channel_weight"] self._filename = new_item["filename"] self._title = new_item["title"] self._artist = new_item["artist"] @@ -54,4 +55,3 @@ class PlanObject: self._filename = self.filename.replace("\\", '/') else: self._filename = self.filename.replace("/", '\\') - \ No newline at end of file diff --git a/player.py b/player.py index 54669d1..58bca9c 100644 --- a/player.py +++ b/player.py @@ -42,7 +42,6 @@ from helpers.state_manager import StateManager from helpers.logging_manager import LoggingManager - class Player(): state = None running = False @@ -63,7 +62,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": [] @@ -134,7 +133,7 @@ class Player(): res = json.dumps(state) return res - ### Audio Playout Related Methods + # Audio Playout Related Methods def play(self, pos=0): try: @@ -197,7 +196,7 @@ class Player(): def set_auto_advance(self, message: int) -> bool: if message == 0: self.state.update("auto_advance", False) - return True # It did it + return True # It did it elif message == 1: self.state.update("auto_advance", True) return True @@ -214,23 +213,23 @@ class Player(): def set_play_on_load(self, message: int) -> bool: if message == 0: self.state.update("play_on_load", False) - return True # It did it + return True # It did it elif message == 1: self.state.update("play_on_load", True) return True else: return False - ### Show Plan Related Methods + # Show Plan Related Methods def add_to_plan(self, new_item: Dict[str, any]) -> bool: self.state.update("show_plan", self.state.state["show_plan"] + [PlanObject(new_item)]) return True - def remove_from_plan(self, timeslotitemid: int) -> bool: + def remove_from_plan(self, channel_weight: int) -> bool: plan_copy = copy.copy(self.state.state["show_plan"]) for i in range(len(plan_copy)): - if plan_copy[i].timeslotitemid == timeslotitemid: + if plan_copy[i].channel_weight == channel_weight: plan_copy.remove(i) self.state.update("show_plan", plan_copy) return True @@ -240,25 +239,24 @@ class Player(): self.state.update("show_plan", []) return True - def load(self, timeslotitemid: int): + def load(self, channel_weight: int): if not self.isPlaying: self.unload() updated: bool = False for i in range(len(self.state.state["show_plan"])): - if self.state.state["show_plan"][i].timeslotitemid == timeslotitemid: + if self.state.state["show_plan"][i].channel_weight == channel_weight: self.state.update("loaded_item", self.state.state["show_plan"][i]) updated = True break if not updated: - print("Failed to find timeslotitemid:", timeslotitemid) + print("Failed to find channel_weight:", channel_weight) return False filename: str = self.state.state["loaded_item"].filename - try: self.logger.log.info("Loading file: " + str(filename)) mixer.music.load(filename) @@ -313,7 +311,7 @@ class Player(): loadedItem = self.state.state["loaded_item"] if (loadedItem): - self.load(loadedItem.timeslotitemid) + self.load(loadedItem.channel_weight) if wasPlaying: self.unpause() @@ -331,8 +329,6 @@ class Player(): self.state.update("playing", self.isPlaying) self.state.update("loaded", self.isLoaded) - - self.state.update("pos_true", self.state.state["pos"] + self.state.state["pos_offset"]) self.state.update("remaining", self.state.state["length"] - self.state.state["pos_true"]) @@ -348,20 +344,19 @@ class Player(): # Auto Advance elif self.state.state["auto_advance"]: for i in range(len(self.state.state["show_plan"])): - if self.state.state["show_plan"][i].timeslotitemid == self.state.state["loaded_item"].timeslotitemid: + if self.state.state["show_plan"][i].channel_weight == self.state.state["loaded_item"].channel_weight: if len(self.state.state["show_plan"]) > i+1: - self.load(self.state.state["show_plan"][i+1].timeslotitemid) + self.load(self.state.state["show_plan"][i+1].channel_weight) break # Repeat All elif self.state.state["repeat"] == "ALL": - self.load(self.state.state["show_plan"][0].timeslotitemid) + self.load(self.state.state["show_plan"][0].channel_weight) # Play on Load if self.state.state["play_on_load"]: self.play() - def _retMsg(self, msg, okay_str=False): response = self.last_msg + ":" if msg == True: @@ -387,7 +382,8 @@ class Player(): self.logger = LoggingManager("channel" + str(channel)) - self.state = StateManager("channel" + str(channel), self.logger, self.__default_state, self.__rate_limited_params) + self.state = StateManager("channel" + str(channel), self.logger, + self.__default_state, self.__rate_limited_params) self.state.update("channel", channel) loaded_state = copy.copy(self.state.state) @@ -401,7 +397,7 @@ class Player(): if loaded_state["loaded_item"]: self.logger.log.info("Loading filename: " + loaded_state["loaded_item"].filename) - self.load(loaded_state["loaded_item"].timeslotitemid) + self.load(loaded_state["loaded_item"].channel_weight) if loaded_state["pos_true"] != 0: self.logger.log.info("Seeking to pos_true: " + str(loaded_state["pos_true"])) @@ -434,26 +430,26 @@ class Player(): elif self.isInit: - message_types: Dict[str, Callable[any, bool]] = { # TODO Check Types - "STATUS": lambda: self._retMsg(self.status, True), + message_types: Dict[str, Callable[any, bool]] = { # TODO Check Types + "STATUS": lambda: self._retMsg(self.status, True), # Audio Playout - "PLAY": lambda: self._retMsg(self.play()), - "PAUSE": lambda: self._retMsg(self.pause()), - "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]))), - "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]))), + "PLAY": lambda: self._retMsg(self.play()), + "PAUSE": lambda: self._retMsg(self.pause()), + "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]))), + "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]))), # Show Plan Items - "LOAD": lambda: self._retMsg(self.load(int(self.last_msg.split(":")[1]))), - "LOADED?": lambda: self._retMsg(self.isLoaded), - "UNLOAD": lambda: self._retMsg(self.unload()), - "ADD": lambda: self._retMsg(self.add_to_plan(json.loads(":".join(self.last_msg.split(":")[1:])))), - "REMOVE": lambda: self._retMsg(self.remove_from_plan(int(self.last_msg.split(":")[1]))), - "CLEAR": lambda: self._retMsg(self.clear_channel_plan()) + "LOAD": lambda: self._retMsg(self.load(int(self.last_msg.split(":")[1]))), + "LOADED?": lambda: self._retMsg(self.isLoaded), + "UNLOAD": lambda: self._retMsg(self.unload()), + "ADD": lambda: self._retMsg(self.add_to_plan(json.loads(":".join(self.last_msg.split(":")[1:])))), + "REMOVE": lambda: self._retMsg(self.remove_from_plan(int(self.last_msg.split(":")[1]))), + "CLEAR": lambda: self._retMsg(self.clear_channel_plan()) } message_type: str = self.last_msg.split(":")[0] diff --git a/server.py b/server.py index 58fcee4..f88358e 100644 --- a/server.py +++ b/server.py @@ -229,9 +229,9 @@ def playonload(channel: int, state: int): # Channel Items -@app.route("/player//load/") -def load(channel: int, timeslotitemid: int): - channel_to_q[channel].put("LOAD:" + str(timeslotitemid)) +@app.route("/player//load/") +def load(channel: int, channel_weight: int): + channel_to_q[channel].put("LOAD:" + str(channel_weight)) return ui_status() @@ -246,7 +246,7 @@ def unload(channel): @app.route("/player//add", methods=["POST"]) def add_to_plan(channel: int): new_item: Dict[str, any] = { - "timeslotitemid": int(request.form["timeslotitemid"]), + "channel_weight": int(request.form["channel_weight"]), "filename": request.form["filename"], "title": request.form["title"], "artist": request.form["artist"], @@ -257,17 +257,17 @@ def add_to_plan(channel: int): return new_item -@app.route("/player//move//") -def move_plan(channel: int, timeslotitemid: int, position: int): - channel_to_q[channel].put("MOVE:" + json.dumps({"timeslotitemid": timeslotitemid, "position": position})) +@app.route("/player//move//") +def move_plan(channel: int, channel_weight: int, position: int): + channel_to_q[channel].put("MOVE:" + json.dumps({"channel_weight": channel_weight, "position": position})) # TODO Return return True -@app.route("/player//remove/") -def remove_plan(channel: int, timeslotitemid: int): - channel_to_q[channel].put("REMOVE:" + timeslotitemid) +@app.route("/player//remove/") +def remove_plan(channel: int, channel_weight: int): + channel_to_q[channel].put("REMOVE:" + channel_weight) # TODO Return return True @@ -385,7 +385,7 @@ def startServer(): text_to_speach.runAndWait() new_item: Dict[str, any] = { - "timeslotitemid": 0, + "channel_weight": 0, "filename": "dev/welcome.mp3", "title": "Welcome to BAPSicle", "artist": "University Radio York", diff --git a/websocket_server.py b/websocket_server.py index 8944d50..c5f1815 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -28,15 +28,23 @@ async def websocket_handler(websocket, path): elif data["command"] == "SEEK": channel_to_q[channel].put("SEEK:" + str(data["time"])) elif data["command"] == "LOAD": - pass + channel_to_q[channel].put("LOAD:" + str(data["weight"])) + elif data["command"] == "ADD": + new_item: Dict[str, any] = { + "channel_weight": int(data["weight"]), + "filename": "dev\\test.mp3", + "title": data["title"], + "artist": None + } + channel_to_q[channel].put("ADD:" + json.dumps(new_item)) - asyncio.wait([await conn.send(message) for conn in baps_clients]) + await asyncio.wait([conn.send(message) for conn in baps_clients]) except websockets.exceptions.ConnectionClosedError: print("RIP {}".format(websocket)) except Exception as e: - print(e) + print("Exception", e) finally: baps_clients.remove(websocket)