From cb81c0616e6b90f62d157dfbf11f6b885f8085af Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Sat, 11 May 2019 23:40:52 +0200 Subject: [PATCH] Fix #45 - Store state of hugvey and allow to resume from it --- hugvey/central_command.py | 18 ++++++++---------- hugvey/panopticon.py | 6 +++--- www/js/hugvey_console.js | 27 +++++++++++++++++++++------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/hugvey/central_command.py b/hugvey/central_command.py index 526313e..d30b734 100644 --- a/hugvey/central_command.py +++ b/hugvey/central_command.py @@ -577,18 +577,16 @@ class HugveyState(object): if not self.story: self.logger.critical("No story to play message in") else: - #restart first so that story loads the new json -# self.restart() if self.story is None: return - - self.startMsgId = event['msg_id'] - self.logger.debug(f"Restart from {self.startMsgId}") - self.restart() -# Unfortunately setCurrentMessage() doesn't cut it, as the story -# needs to be reloaded, this cannot be done with keeping the state either way -# msg = self.story.get(event['msg_id']) -# await self.story.setCurrentMessage(msg) + + if event['reloadStory']: + self.startMsgId = event['msg_id'] + self.logger.debug(f"Restart from {self.startMsgId}") + self.restart() + else: + msg = self.story.get(event['msg_id']) + await self.story.setCurrentMessage(msg) self.eventQueue = None diff --git a/hugvey/panopticon.py b/hugvey/panopticon.py index 64b6959..4ef79be 100644 --- a/hugvey/panopticon.py +++ b/hugvey/panopticon.py @@ -69,7 +69,7 @@ def getWebSocketHandler(central_command): elif msg['action'] == 'change_light': self.msgChangeLightId(msg['hugvey'], int(msg['light_id'])) elif msg['action'] == 'play_msg': - self.msgPlayMsg(msg['hugvey'], msg['msg_id']) + self.msgPlayMsg(msg['hugvey'], msg['msg_id'], msg['reloadStory']) else: self.send({'alert': 'Unknown request: {}'.format(message)}) logger.warn('Unknown request: {}'.format(message)) @@ -125,8 +125,8 @@ def getWebSocketHandler(central_command): def msgChangeLightId(self, hv_id, lightId): central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_light', 'light_id': lightId}) - def msgPlayMsg(self, hv_id, msg_id): - central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'play_msg', 'msg_id': msg_id}) + def msgPlayMsg(self, hv_id, msg_id, reloadStory): + central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'play_msg', 'msg_id': msg_id, 'reloadStory':bool(reloadStory)}) @classmethod def write_to_clients(wsHandlerClass, msg): diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 0da83eb..ab0fcab 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -237,11 +237,11 @@ class Panopticon { this.send( { action: 'change_light', hugvey: hv_id, light_id: light_id } ); } - playFromSelected(msg_id) { + playFromSelected(msg_id, reloadStory) { if(!this.hugveys.selectedId) { alert('No hugvey selected'); } else { - this.send({ action: 'play_msg', hugvey: this.hugveys.selectedId, msg_id: msg_id }) + this.send({ action: 'play_msg', hugvey: this.hugveys.selectedId, msg_id: msg_id, reloadStory: reloadStory }) } } } @@ -1073,7 +1073,7 @@ class Graph { if(panopticon.hugveys.selectedId) { - let playEl = crel( + msgEl.appendChild(crel( 'div', {'class': 'play'}, crel( @@ -1084,15 +1084,30 @@ class Graph { console.log('go save'); panopticon.graph.saveJson(null, null, function(){ console.log('saved, now play'); - panopticon.playFromSelected(msg['@id']); + panopticon.playFromSelected(msg['@id'], true); }); } } }, "Save & play on #" + panopticon.hugveys.selectedId ) - ); - msgEl.appendChild(playEl); + )); + msgEl.appendChild(crel( + 'div', + {'class': 'play'}, + crel( + 'div', { + 'class': 'btn btn--play', + 'on': { + 'click': function (e) { + panopticon.playFromSelected(msg['@id'], false); + } + } + }, + "Continue on #" + panopticon.hugveys.selectedId + ) + )); + }