From 4c7f15655eca0ec0fd39a664349af13fa8120f1e Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 17 May 2019 16:39:53 +0200 Subject: [PATCH] Track people again trough their story --- hugvey/central_command.py | 12 ++++++------ hugvey/panopticon.py | 35 +++++++++++++++++++++++++---------- www/js/hugvey_console.js | 9 +++++++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/hugvey/central_command.py b/hugvey/central_command.py index 40953d8..bda39a5 100644 --- a/hugvey/central_command.py +++ b/hugvey/central_command.py @@ -133,7 +133,7 @@ class CentralCommand(object): return status - def getStatusSummary(self, selected_id = None): + def getStatusSummary(self, selected_ids = []): status = { 'uptime': "-" if not self.start_time else (time.time() - self.start_time), 'languages': self.config['languages'], @@ -148,12 +148,12 @@ class CentralCommand(object): # print(threading.enumerate()) for hv_id in self.hugvey_ids: - status['hugveys'].append(self.getHugveyStatus(hv_id, selected_id == hv_id)) + status['hugveys'].append(self.getHugveyStatus(hv_id, hv_id in selected_ids)) - if selected_id and selected_id in self.hugveys: - if self.hugveys[selected_id].recorder: - status['logbook'] = self.hugveys[selected_id].recorder.currentLog - status['logbookId'] = selected_id +# if selected_id and selected_id in self.hugveys: +# if self.hugveys[selected_id].recorder: +# status['logbook'] = self.hugveys[selected_id].recorder.currentLog +# status['logbookId'] = selected_id return status diff --git a/hugvey/panopticon.py b/hugvey/panopticon.py index 99cdc06..da1fdb4 100644 --- a/hugvey/panopticon.py +++ b/hugvey/panopticon.py @@ -30,6 +30,7 @@ def getWebSocketHandler(central_command): class WebSocketHandler(tornado.websocket.WebSocketHandler): CORS_ORIGINS = ['localhost', 'hugveycmd.local'] connections = set() + selections = {} def check_origin(self, origin): parsed_origin = urlparse(origin) @@ -50,6 +51,8 @@ def getWebSocketHandler(central_command): msg = json.loads(message) if msg['action'] == 'init': self.msgInit() + elif msg['action'] == 'selection': + self.selectHugvey(msg['selected_id']) # elif msg['action'] == 'get_status': # self.msgStatus(msg['selected_id']) elif msg['action'] == 'block': @@ -85,24 +88,29 @@ def getWebSocketHandler(central_command): # client disconnected def on_close(self): - WebSocketHandler.connections.remove(self) + WebSocketHandler.rmConnection(self) logger.info("Client disconnected") @classmethod - def getStatusMsg(cls, selected_id = False): - msg = central_command.getStatusSummary(selected_id) + def getStatusMsg(cls): + msg = central_command.getStatusSummary(cls.selections.values()) msg['action'] = 'status' return msg @classmethod - def broadcastStatus(cls, selected_id = None): - cls.write_to_clients(cls.getStatusMsg(selected_id)) -# self.send() + def broadcastStatus(cls): + cls.write_to_clients(cls.getStatusMsg()) def msgInit(self): msg = self.getStatusMsg() self.send(msg) + + def selectHugvey(self, hv_id): + if hv_id is None: + self.__class__.selections.pop(self, None) + else: + self.__class__.selections[self] = int(hv_id) def msgBlock(self, hv_id): central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'block'}) @@ -131,6 +139,15 @@ def getWebSocketHandler(central_command): 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 rmConnection(cls, client): + if client not in cls.connections: + return + + if client in cls.selections: + cls.selections.pop(client, None) + cls.connections.remove(client) + @classmethod def write_to_clients(wsHandlerClass, msg): if msg is None: @@ -146,9 +163,7 @@ def getWebSocketHandler(central_command): toRemove.append(client) # If we remove it here from the set we get an exception about changing set size during iteration for client in toRemove: - if client not in wsHandlerClass.connections: - continue - wsHandlerClass.connections.remove(client) + rmConnection(client) return WebSocketHandler @@ -287,7 +302,7 @@ class Panopticon(object): async def statusSender(self, wsHandler): while True: try: - self.wsHandler.broadcastStatus(self.wsHandler) + self.wsHandler.broadcastStatus() await asyncio.sleep(3) except Exception as e: logger.exception(e) diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 87214b6..14801fd 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -27,7 +27,7 @@ class Panopticon { return moment(time).utc().format("hh:mm:ss"); }, loadNarrative: function( code, file ) { - panopticon.hugveys.selectedId = null; + panopticon.selectHugvey(null); if(panopticon.hasGraph) { return panopticon.loadNarrative( code, file ); @@ -68,7 +68,7 @@ class Panopticon { return panopticon.change_light_id(hv_id, light_id); }, showHugvey: function(hv) { - panopticon.hugveys.selectedId = hv.language ? hv.id : null; + panopticon.selectHugvey(hv.language ? hv.id : null); panopticon.hugveys.logbook = []; panopticon.hugveys.logbookId = null; panopticon.updateSelectedHugvey(); @@ -134,6 +134,11 @@ class Panopticon { } } ); } + + selectHugvey(hv_id) { + this.hugveys.selectedId = hv_id; + this.send({ action: 'selection', selected_id: hv_id }); + } updateSelectedHugvey() { let hv = null;