From 4741694d6629fcf2a3a6f6a903206430b4e8340e Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sun, 14 Feb 2021 20:10:32 +0000 Subject: [PATCH] Migrate internal planitem format to match webstudio/api --- helpers/myradio_api.py | 8 ++--- plan.py | 67 ++++++++++++++++++++++++------------------ player.py | 39 ++++++++++-------------- websocket_server.py | 20 +------------ 4 files changed, 60 insertions(+), 74 deletions(-) diff --git a/helpers/myradio_api.py b/helpers/myradio_api.py index 2b8aefc..7d499a0 100644 --- a/helpers/myradio_api.py +++ b/helpers/myradio_api.py @@ -71,14 +71,14 @@ class MyRadioAPI(): def get_filename(self, item: PlanItem): format = "mp3" # TODO: Maybe we want this customisable? - if item.trackId: + if item.trackid: itemType = "track" - id = item.trackId + id = item.trackid url = "/NIPSWeb/secure_play?trackid={}&{}".format(id, format) - elif item.managedId: + elif item.managedid: itemType = "managed" - id = item.managedId + id = item.managedid url = "/NIPSWeb/managed_play?managedid={}".format(id) else: diff --git a/plan.py b/plan.py index c376ed0..0ffdeec 100644 --- a/plan.py +++ b/plan.py @@ -16,28 +16,28 @@ from typing import Any, Dict, Optional import os class PlanItem: - _timeslotItemId: int = 0 - _channelWeight: int = 0 - _filename: str = "" - _title: str = "" - _artist: str = "" - _trackId: Optional[int] = None - _managedId: Optional[int] = None + _timeslotitemid: int = 0 + _weight: int = 0 + _filename: Optional[str] + _title: str + _artist: Optional[str] + _trackid: Optional[int] + _managedid: Optional[int] @property - def channelWeight(self) -> int: - return self._channelWeight + def weight(self) -> int: + return self._weight @property - def timeslotItemId(self) -> int: - return self._timeslotItemId + def timeslotitemid(self) -> int: + return self._timeslotitemid @property - def filename(self) -> str: + def filename(self) -> Optional[str]: return self._filename @filename.setter - def filename(self, value: str): + def filename(self, value: Optional[str]): self._filename = value @property @@ -45,12 +45,12 @@ class PlanItem: return "{0} - {1}".format(self._title, self._artist) if self._artist else self._title @property - def trackId(self) -> Optional[int]: - return self._trackId + def trackid(self) -> Optional[int]: + return self._trackid @property - def managedId(self) -> Optional[int]: - return self._managedId + def managedid(self) -> Optional[int]: + return self._managedid @property def title(self) -> Optional[str]: @@ -60,27 +60,38 @@ class PlanItem: def artist(self) -> Optional[str]: return self._artist + @property + def length(self) -> Optional[str]: + return self._length + + @property + def type(self) -> Optional[str]: + return "aux" if self.managedid else "central" + @property def __dict__(self): return { - "channelWeight": self.channelWeight, - "timeslotItemId": self.timeslotItemId, - "trackId": self._trackId, - "managedId": self._managedId, + "weight": self.weight, + "timeslotitemid": self.timeslotitemid, + "trackid": self._trackid, + "type": self.type, + "managedid": self._managedid, "title": self._title, "artist": self._artist, "name": self.name, - "filename": self.filename + "filename": self.filename, + "length": self.length } def __init__(self, new_item: Dict[str, Any]): - self._timeslotItemId = new_item["timeslotItemId"] - self._trackId = new_item["trackId"] if "trackId" in new_item else None - self._managedId = new_item["managedId"] if "managedId" in new_item else None - self._filename = new_item["filename"] # This could be a temp dir for API-downloaded items, or a mapped drive. - self._channelWeight = new_item["channelWeight"] + self._timeslotitemid = new_item["timeslotitemid"] + self._managedid = new_item["managedid"] if "managedid" in new_item else None + self._trackid = new_item["trackid"] if "trackid" in new_item and not self._managedid else None + self._filename = new_item["filename"] if "filename" in new_item else None # This could be a temp dir for API-downloaded items, or a mapped drive. + self._weight = new_item["weight"] self._title = new_item["title"] - self._artist = new_item["artist"] + self._artist = new_item["artist"] if "artist" in new_item else None + self._length = new_item["length"] # Fix any OS specific / or \'s if self.filename: diff --git a/player.py b/player.py index aef8b9e..925ead3 100644 --- a/player.py +++ b/player.py @@ -142,6 +142,8 @@ class Player(): # Audio Playout Related Methods def play(self, pos: float = 0): + if self.isPlaying or not self.isLoaded: + return global starting global already_stopped starting = True @@ -233,18 +235,9 @@ class Player(): if len(plan) > channel: for plan_item in plan[str(channel)]: try: - new_item: Dict[str, any] = { - "channelWeight": int(plan_item["weight"]), - "filename": None, - "title": plan_item["title"], - "artist": plan_item["artist"] if "artist" in plan_item.keys() else None, - "timeslotItemId": int(plan_item["timeslotitemid"]) if "timeslotitemid" in plan_item.keys() and plan_item["timeslotitemid"] != None else None, - "trackId": int(plan_item["trackid"]) if "managedid" not in plan_item.keys() and plan_item["trackid"] != None else None, - "recordId": int(plan_item["trackid"]) if "trackid" in plan_item.keys() and plan_item["trackid"] != None else None, # TODO This is wrong. - "managedId": int(plan_item["managedid"]) if "managedid" in plan_item.keys() and plan_item["managedid"] != None else None, - } - self.add_to_plan(new_item) - except: + self.add_to_plan(plan_item) + except Exception as e: + self.logger.log.critical("Failed to add item to show plan: {}".format(e)) continue return True @@ -254,10 +247,10 @@ class Player(): self.state.update("show_plan", self.state.state["show_plan"] + [PlanItem(new_item)]) return True - def remove_from_plan(self, channel_weight: int) -> bool: + def remove_from_plan(self, weight: int) -> bool: plan_copy: List[PlanItem] = copy.copy(self.state.state["show_plan"]) for i in plan_copy: - if i.channelWeight == channel_weight: + if i.weight == weight: plan_copy.remove(i) self.state.update("show_plan", plan_copy) return True @@ -267,7 +260,7 @@ class Player(): self.state.update("show_plan", []) return True - def load(self, channelWeight: int): + def load(self, weight: int): if not self.isPlaying: self.unload() @@ -276,12 +269,12 @@ class Player(): loaded_item: Optional[PlanItem] = None for i in range(len(showplan)): - if showplan[i].channelWeight == channelWeight: + if showplan[i].weight == weight: loaded_item = showplan[i] break if loaded_item == None: - self.logger.log.error("Failed to find channelWeight: {}".format(channelWeight)) + self.logger.log.error("Failed to find weight: {}".format(weight)) return False if (loaded_item.filename == "" or loaded_item.filename == None): @@ -293,7 +286,7 @@ class Player(): self.state.update("loaded_item", loaded_item) for i in range(len(showplan)): - if showplan[i].channelWeight == channelWeight: + if showplan[i].weight == weight: self.state.update("show_plan", index=i, value=loaded_item) break # TODO: Update the show plan filenames @@ -356,7 +349,7 @@ class Player(): loadedItem = self.state.state["loaded_item"] if (loadedItem): - self.load(loadedItem.channelWeight) + self.load(loadedItem.weight) if wasPlaying: self.unpause() @@ -401,14 +394,14 @@ 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].channelWeight == loaded_item.channelWeight: + if self.state.state["show_plan"][i].weight == loaded_item.weight: if len(self.state.state["show_plan"]) > i+1: - self.load(self.state.state["show_plan"][i+1].channelWeight) + self.load(self.state.state["show_plan"][i+1].weight) break # Repeat All elif self.state.state["repeat"] == "ALL": - self.load(self.state.state["show_plan"][0].channelWeight) + self.load(self.state.state["show_plan"][0].weight) # Play on Load if self.state.state["play_on_load"]: @@ -498,7 +491,7 @@ class Player(): loaded_item = loaded_state["loaded_item"] if loaded_item: self.logger.log.info("Loading filename: " + str(loaded_item.filename)) - self.load(loaded_item.channelWeight) + self.load(loaded_item.weight) if loaded_state["pos_true"] != 0: self.logger.log.info("Seeking to pos_true: " + str(loaded_state["pos_true"])) diff --git a/websocket_server.py b/websocket_server.py index babc9f6..d36161b 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -49,26 +49,8 @@ async def websocket_handler(websocket, path): 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"): - managed_id = int(data["newItem"]["managedId"].split(":")[1]) - else: - managed_id = int(data["newItem"]["managedId"]) - else: - managed_id = None - new_item: Dict[str, any] = { - "channelWeight": int(data["newItem"]["weight"]), - "filename": None, - "title": data["newItem"]["title"], - "artist": data["newItem"]["artist"] if "artist" in data["newItem"].keys() else None, - "timeslotItemId": int(data["newItem"]["timeslotItemId"]) if "timeslotItemId" in data["newItem"].keys() and data["newItem"]["timeslotItemId"] != None else None, - "trackId": int(data["newItem"]["trackId"]) if "trackId" in data["newItem"].keys() and data["newItem"]["trackId"] != None else None, - "recordId": int(data["newItem"]["trackId"]) if "trackId" in data["newItem"].keys() and data["newItem"]["trackId"] != None else None, - "managedId": managed_id - } - channel_to_q[channel].put("ADD:" + json.dumps(new_item)) + channel_to_q[channel].put("ADD:" + json.dumps(data["newItem"])) elif data["command"] == "REMOVE": channel_to_q[channel].put("REMOVE:" + str(data["weight"]))