diff --git a/helpers/state_manager.py b/helpers/state_manager.py index a98d7ab..89c5eed 100644 --- a/helpers/state_manager.py +++ b/helpers/state_manager.py @@ -17,6 +17,7 @@ from typing import Any, Dict, List, NewType, Optional, Union class StateManager: filepath = None logger = None + callbacks: List[Any] = [] __state = {} __state_in_file = {} # Dict of times that params can be updated after, if the time is before current time, it can be written immediately. @@ -136,7 +137,18 @@ class StateManager: self.state = state_to_update if (update_file == True): + # Either a routine write, or state has changed. + # Update the file self.write_to_file(state_to_update) + # Now tell any callback functions. + for callback in self.callbacks: + try: + callback() + except Exception as e: + self.logger.log.critical("Failed to execute status callback: {}".format(e)) + + def add_callback(self, function): + self.callbacks.append(function) def _log(self, text:str, level: int = INFO): self.logger.log.log(level, "State Manager: " + text) diff --git a/player.py b/player.py index e2a25ac..786f9ac 100644 --- a/player.py +++ b/player.py @@ -470,6 +470,10 @@ class Player(): self.logger.log.info(("Sending: {}".format(response))) self.out_q.put(response) + def _send_status(self): + self.last_msg = "STATUS" + self._retMsg(self.status, True) + def __init__(self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue): process_title = "Player: Channel " + str(channel) @@ -488,6 +492,9 @@ class Player(): self.state = StateManager("channel" + str(channel), self.logger, self.__default_state, self.__rate_limited_params) + + self.state.add_callback(self._send_status) + self.state.update("channel", channel) loaded_state = copy.copy(self.state.state) @@ -565,11 +572,7 @@ class Player(): if message_type in message_types.keys(): message_types[message_type]() - if message_type != "STATUS": - ## Then a super hacky hack. Send the status again to update Webstudio - self._updateState() - self.last_msg = "STATUS" - self._retMsg(self.status, True) + elif (self.last_msg == 'QUIT'): self.running = False