Add basic callback to state manager to send STATUS on change.

This commit is contained in:
Matthew Stratford 2021-02-14 13:57:07 +00:00
parent 12d7f6fdd2
commit a18d4317e4
2 changed files with 20 additions and 5 deletions

View file

@ -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)

View file

@ -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