Fix #45 - Store state of hugvey and allow to resume from it

This commit is contained in:
Ruben van de Ven 2019-05-11 23:40:52 +02:00
parent 08e3805819
commit cb81c0616e
3 changed files with 32 additions and 19 deletions

View File

@ -577,18 +577,16 @@ class HugveyState(object):
if not self.story: if not self.story:
self.logger.critical("No story to play message in") self.logger.critical("No story to play message in")
else: else:
#restart first so that story loads the new json
# self.restart()
if self.story is None: if self.story is None:
return return
if event['reloadStory']:
self.startMsgId = event['msg_id'] self.startMsgId = event['msg_id']
self.logger.debug(f"Restart from {self.startMsgId}") self.logger.debug(f"Restart from {self.startMsgId}")
self.restart() self.restart()
# Unfortunately setCurrentMessage() doesn't cut it, as the story else:
# needs to be reloaded, this cannot be done with keeping the state either way msg = self.story.get(event['msg_id'])
# msg = self.story.get(event['msg_id']) await self.story.setCurrentMessage(msg)
# await self.story.setCurrentMessage(msg)
self.eventQueue = None self.eventQueue = None

View File

@ -69,7 +69,7 @@ def getWebSocketHandler(central_command):
elif msg['action'] == 'change_light': elif msg['action'] == 'change_light':
self.msgChangeLightId(msg['hugvey'], int(msg['light_id'])) self.msgChangeLightId(msg['hugvey'], int(msg['light_id']))
elif msg['action'] == 'play_msg': elif msg['action'] == 'play_msg':
self.msgPlayMsg(msg['hugvey'], msg['msg_id']) self.msgPlayMsg(msg['hugvey'], msg['msg_id'], msg['reloadStory'])
else: else:
self.send({'alert': 'Unknown request: {}'.format(message)}) self.send({'alert': 'Unknown request: {}'.format(message)})
logger.warn('Unknown request: {}'.format(message)) logger.warn('Unknown request: {}'.format(message))
@ -125,8 +125,8 @@ def getWebSocketHandler(central_command):
def msgChangeLightId(self, hv_id, lightId): def msgChangeLightId(self, hv_id, lightId):
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_light', 'light_id': lightId}) central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_light', 'light_id': lightId})
def msgPlayMsg(self, hv_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}) central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'play_msg', 'msg_id': msg_id, 'reloadStory':bool(reloadStory)})
@classmethod @classmethod
def write_to_clients(wsHandlerClass, msg): def write_to_clients(wsHandlerClass, msg):

View File

@ -237,11 +237,11 @@ class Panopticon {
this.send( { action: 'change_light', hugvey: hv_id, light_id: light_id } ); this.send( { action: 'change_light', hugvey: hv_id, light_id: light_id } );
} }
playFromSelected(msg_id) { playFromSelected(msg_id, reloadStory) {
if(!this.hugveys.selectedId) { if(!this.hugveys.selectedId) {
alert('No hugvey selected'); alert('No hugvey selected');
} else { } 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) { if(panopticon.hugveys.selectedId) {
let playEl = crel( msgEl.appendChild(crel(
'div', 'div',
{'class': 'play'}, {'class': 'play'},
crel( crel(
@ -1084,15 +1084,30 @@ class Graph {
console.log('go save'); console.log('go save');
panopticon.graph.saveJson(null, null, function(){ panopticon.graph.saveJson(null, null, function(){
console.log('saved, now play'); console.log('saved, now play');
panopticon.playFromSelected(msg['@id']); panopticon.playFromSelected(msg['@id'], true);
}); });
} }
} }
}, },
"Save & play on #" + panopticon.hugveys.selectedId "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
)
));
} }