diff --git a/hugvey/panopticon.py b/hugvey/panopticon.py index d95daed..047afb3 100644 --- a/hugvey/panopticon.py +++ b/hugvey/panopticon.py @@ -90,7 +90,7 @@ def getWebSocketHandler(central_command): # client disconnected def on_close(self): - WebSocketHandler.rmConnection(self) + self.__class__.rmConnection(self) logger.info("Client disconnected") @classmethod @@ -168,7 +168,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: - rmConnection(client) + wsHandlerClass.rmConnection(client) return WebSocketHandler diff --git a/hugvey/story.py b/hugvey/story.py index de80ea9..81373a8 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -1,24 +1,26 @@ -import json -import time -import logging -import re import asyncio -import urllib.parse -from .communication import LOG_BS -from tornado.httpclient import AsyncHTTPClient, HTTPRequest -import uuid -import shortuuid -import threading -import faulthandler -from zmq.asyncio import Context -import zmq -import wave -import sox -from pythonosc import udp_client -import random -import pickle +import json +import logging import os +import pickle +import random +import re +import threading +import time +from tornado.httpclient import AsyncHTTPClient, HTTPRequest import traceback +import urllib.parse +import uuid +import wave +import zmq +from zmq.asyncio import Context + +from pythonosc import udp_client +import shortuuid +import sox + +from .communication import LOG_BS + mainLogger = logging.getLogger("hugvey") logger = mainLogger.getChild("narrative") @@ -1034,6 +1036,16 @@ class Configuration(object): id = 'configuration' volume = 1 # Volume multiplier for 'play' command nothing_text = "nothing" # When variable is not set, but used in sentence, replace it with this word. + light0_intensity = 0 + light0_fade = 30. # fade duration in seconds + light1_intensity = 150 + light1_fade = 10. + light2_intensity = 75 + light2_fade = 10. + light3_intensity = 150 + light3_fade = 10. + light4_intensity = 150 + light4_fade = 10. @classmethod def initFromJson(configClass, data, story): @@ -1041,6 +1053,16 @@ class Configuration(object): config.__dict__.update(data) return config + def getLightPresets(self): + c = self.__dict__ + l = [] + for i in range(5): + l.append({ + 'intensity': int(c[f"light{i}_intensity"]), + 'fade': float(c[f"light{i}_fade"]) + }) + return l + storyClasses = { 'Msg': Message, @@ -1686,7 +1708,8 @@ class Story(object): }) if message.lightChange is not None: - self.hugvey.setLightStatus(message.lightChange) + self.fadeLightPreset(message.lightChange) +# self.hugvey.setLightStatus(message.lightChange) # 2019-02-22 temporary disable listening while playing audio: # if self.hugvey.google is not None: @@ -1701,6 +1724,15 @@ class Story(object): self.logger.log(LOG_BS,logmsg) self.storeState() + + def fadeLightPreset(self, presetNr: int): + if presetNr < 0 or presetNr > 4: + self.logger.critical(f"Error parsing light fade preset code '{presetNr}'") + return + + preset = self.configuration.getLightPresets()[presetNr] + + self.hugvey.transitionLight(preset['intensity'], preset['fade']) def getCurrentDirections(self): if self.currentMessage.id not in self.directionsPerMsg: diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 0af5f48..55e2703 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -1014,7 +1014,7 @@ class Graph { }) ), crel('hr'), - crel('h2', 'Light setting #0'), + crel('h2', 'Light fade setting #0'), crel( 'label', "Light intensity: ", @@ -1027,7 +1027,7 @@ class Graph { panopticon.graph.configuration['light0_intensity'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light0_intensity') ? this.configuration.light1_intensity : 0 + 'value': this.configuration.hasOwnProperty('light0_intensity') ? this.configuration.light1_intensity : "" }) ), crel( @@ -1036,17 +1036,17 @@ class Graph { crel('input', { 'type': 'number', 'min': 1, - 'max': 5, +// 'max': 5, 'step': .1, 'on': { 'change': function(e){ panopticon.graph.configuration['light0_fade'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light0_fade') ? this.configuration.light0_fade: 30 + 'value': this.configuration.hasOwnProperty('light0_fade') ? this.configuration.light0_fade: "" }) ), - crel('h2', 'Light setting #1'), + crel('h2', 'Light fade setting #1'), crel( 'label', "Light intensity: ", @@ -1059,7 +1059,7 @@ class Graph { panopticon.graph.configuration['light1_intensity'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light1_intensity') ? this.configuration.light1_intensity : 150 + 'value': this.configuration.hasOwnProperty('light1_intensity') ? this.configuration.light1_intensity : "" }) ), crel( @@ -1068,17 +1068,17 @@ class Graph { crel('input', { 'type': 'number', 'min': 1, - 'max': 5, +// 'max': 5, 'step': .1, 'on': { 'change': function(e){ panopticon.graph.configuration['light1_fade'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light1_fade') ? this.configuration.light1_fade: 10 + 'value': this.configuration.hasOwnProperty('light1_fade') ? this.configuration.light1_fade: "" }) ), - crel('h2', 'Light setting #2'), + crel('h2', 'Light fade setting #2'), crel( 'label', "Light intensity: ", @@ -1091,7 +1091,7 @@ class Graph { panopticon.graph.configuration['light2_intensity'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light2_intensity') ? this.configuration.light2_intensity : 75 + 'value': this.configuration.hasOwnProperty('light2_intensity') ? this.configuration.light2_intensity : "" }) ), crel( @@ -1100,17 +1100,17 @@ class Graph { crel('input', { 'type': 'number', 'min': 1, - 'max': 5, +// 'max': 5, 'step': .1, 'on': { 'change': function(e){ panopticon.graph.configuration['light2_fade'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light2_fade') ? this.configuration.light2_fade: 10 + 'value': this.configuration.hasOwnProperty('light2_fade') ? this.configuration.light2_fade: "" }) ), - crel('h2', 'Light setting #3'), + crel('h2', 'Light fade setting #3'), crel( 'label', "Light intensity: ", @@ -1123,7 +1123,7 @@ class Graph { panopticon.graph.configuration['light3_intensity'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light3_intensity') ? this.configuration.light3_intensity : 150 + 'value': this.configuration.hasOwnProperty('light3_intensity') ? this.configuration.light3_intensity : "" }) ), crel( @@ -1132,17 +1132,17 @@ class Graph { crel('input', { 'type': 'number', 'min': 1, - 'max': 5, +// 'max': 5, 'step': .1, 'on': { 'change': function(e){ panopticon.graph.configuration['light3_fade'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light3_fade') ? this.configuration.light3_fade: 10 + 'value': this.configuration.hasOwnProperty('light3_fade') ? this.configuration.light3_fade: "" }) ), - crel('h2', 'Light setting #4'), + crel('h2', 'Light fade setting #4'), crel( 'label', "Light intensity: ", @@ -1155,7 +1155,7 @@ class Graph { panopticon.graph.configuration['light4_intensity'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light4_intensity') ? this.configuration.light4_intensity : 150 + 'value': this.configuration.hasOwnProperty('light4_intensity') ? this.configuration.light4_intensity : "" }) ), crel( @@ -1164,14 +1164,14 @@ class Graph { crel('input', { 'type': 'number', 'min': 1, - 'max': 5, +// 'max': 5, 'step': .1, 'on': { 'change': function(e){ panopticon.graph.configuration['light4_fade'] = e.target.value } }, - 'value': this.configuration.hasOwnProperty('light4_fade') ? this.configuration.light4_fade: 10 + 'value': this.configuration.hasOwnProperty('light4_fade') ? this.configuration.light4_fade: "" }) ) ); @@ -1259,16 +1259,36 @@ class Graph { ) : 'Auto-generated') ); - let lightOptionNone = {'value': null} - let lightOptionOn = {'value': 1} - let lightOptionOff = {'value': 0} - - if(msg.hasOwnProperty('light')) { - if(msg['light'] === 1) lightOptionOn['selected'] = 'selected'; - if(msg['light'] === 0) lightOptionOff['selected'] = 'selected'; - if(msg['light'] === null) lightOptionNone['selected'] = 'selected'; + let lightOptions = [ + crel("option", {'value': null}, "Do nothing") + ]; + for(let i = 0; i < 5; i++) { + let l = {'value': i}; + if(msg.hasOwnProperty('light') && msg['light'] == i) { + l['selected'] = 'selected'; + } + let intensity = "?"; + if(this.configuration.hasOwnProperty(`light${i}_intensity`)) { + intensity = this.configuration[`light${i}_intensity`]; + } + let duration = "?"; + if(this.configuration.hasOwnProperty(`light${i}_fade`)) { + duration = this.configuration[`light${i}_fade`]; + } + lightOptions.push(crel("option", l, `Fade preset #${i} (${intensity} in ${duration}s)`)); } +// let lightOptionNone = {'value': null} +// +// let lightOptionOn = {'value': 1} +// let lightOptionOff = {'value': 0} +// +// if(msg.hasOwnProperty('light')) { +// if(msg['light'] === 1) lightOptionOn['selected'] = 'selected'; +// if(msg['light'] === 0) lightOptionOff['selected'] = 'selected'; +// if(msg['light'] === null) lightOptionNone['selected'] = 'selected'; +// } + let msgInfoEl = crel( 'div', { 'class': 'msg__info' }, crel('div', { 'class':'btn btn--delete btn--delete-msg', @@ -1416,9 +1436,7 @@ class Graph { } } }, - crel('option', lightOptionNone, "Do nothing"), - crel('option', lightOptionOn, "Turn on"), - crel('option', lightOptionOff, "Turn off") + lightOptions ) ) );