diff --git a/bapsicle_standalone.py b/bapsicle_standalone.py index ab23236..66a7652 100644 --- a/bapsicle_standalone.py +++ b/bapsicle_standalone.py @@ -3,8 +3,12 @@ import time import json from mutagen.mp3 import MP3 +from state_manager import stateManager + class bapsicle(): - state = { + state = None + + __default_state = { "filename": "", "channel": -1, "playing": False, @@ -35,7 +39,7 @@ class bapsicle(): pygame.mixer.music.pause() def unpause(self): - pygame.mixer.music.play(0, self.state["pos"]) + pygame.mixer.music.play(0, self.state.state["pos"]) def stop(self): pygame.mixer.music.stop() @@ -48,13 +52,13 @@ class bapsicle(): def load(self, filename): if not self.isPlaying(): - self.state["filename"] = filename + self.state.update("filename",filename) pygame.mixer.music.load(filename) if ".mp3" in filename: song = MP3(filename) - self.state["length"] = song.info.length + self.state.update("length",song.info.length) else: - self.state["length"] = pygame.mixer.Sound(filename).get_length()/1000 + self.state.update("length",pygame.mixer.Sound(filename).get_length()/1000) def output(self, name = None): pygame.mixer.quit() @@ -67,29 +71,31 @@ class bapsicle(): return "FAIL:Failed to init mixer, check sound devices." else: if name: - self.state["output"] = name + self.state.update("output",name) else: - self.state["output"] = "default" + self.state.update("output","default") return "OK" def updateState(self, pos = None): - self.state["playing"] = self.isPlaying() + self.state.update("playing",self.isPlaying()) if (pos): - self.state["pos"] = pos + self.state.update("pos",pos) else: - self.state["pos"] = pygame.mixer.music.get_pos()/1000 - - self.state["remaining"] = self.state["length"] - self.state["pos"] + self.state.update("pos",pygame.mixer.music.get_pos()/1000) + print(self.state.state) + self.state.update("remaining",self.state.state["length"] - self.state.state["pos"]) def getDetails(self): - res = "RESP:DETAILS: " + json.dumps(self.state) + res = "RESP:DETAILS: " + json.dumps(self.state.state) return res def __init__(self, channel, in_q, out_q): - self.state["channel"] = channel + self.state = stateManager("channel" + str(channel), self.__default_state) + + self.state.update("channel", channel) self.output() diff --git a/state_manager.py b/state_manager.py new file mode 100644 index 0000000..a57ef13 --- /dev/null +++ b/state_manager.py @@ -0,0 +1,59 @@ +import json +import os + +class stateManager: + filepath = None + __state = {} + + def __init__(self, name, default_state = None): + self.filepath = "state/" + name + ".json" + if not os.path.isfile(self.filepath): + self.log("No file found for " + self.filepath) + try: + # Try creating the file. + open(self.filepath, "x") + except: + self.log("failed to create state file") + return + + self.log("Saving state to " + self.filepath) + + + file = open(self.filepath, 'r') + + file_state = file.read() + file.close() + + #TODO: also check for invalid JSON state + if file_state == "": + print("file empty") + + self.state = default_state + + else: + self.__state = json.loads(file_state) + + @property + def state(self): + return self.__state + + @state.setter + def state(self, state): + self.__state = state + + file = open(self.filepath, "w") + + file.write(json.dumps(state, indent=2, sort_keys=True)) + + file.close() + + def update(self, key, value): + state = self.state + state[key] = value + self.state = state + + def log(self, msg): + print(msg) + + +