From 62b7cbbeabfda1ee24276f29881bcc7b3a0531b4 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 21:50:58 +0200 Subject: [PATCH 1/4] Added new config system --- default-config.json | 9 +++++++ default-presets.json | 41 ++++++++++++++++++++++++++++++++ modules/configloader/__init__.py | 1 + modules/configloader/__main__.py | 0 modules/configloader/loader.py | 12 ++++++++++ modules/hue/hue_controller.py | 26 ++++++++++++++++---- setup.sh | 7 ++++++ 7 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 default-config.json create mode 100644 default-presets.json create mode 100644 modules/configloader/__init__.py create mode 100644 modules/configloader/__main__.py create mode 100644 modules/configloader/loader.py create mode 100755 setup.sh diff --git a/default-config.json b/default-config.json new file mode 100644 index 0000000..fd226bd --- /dev/null +++ b/default-config.json @@ -0,0 +1,9 @@ +{ + "hue": { + "address": "", + "username": "" + }, + "speech": { + "device_index": 30 + } +} diff --git a/default-presets.json b/default-presets.json new file mode 100644 index 0000000..619e55e --- /dev/null +++ b/default-presets.json @@ -0,0 +1,41 @@ +{ + "default": { + "color": {178, 199, 255}, + "brightness": 255 + }, + + "dim": { + "color": {178, 199, 255}, + "brightness": 111 + }, + + "dim": { + "color": {178, 199, 255}, + "brightness": 80 + }, + + "red": { + "color": {255, 0, 0}, + "brightness": 255 + }, + + "green": { + "color": {0, 255, 0}, + "brightness": 255 + }, + + "blue": { + "color": {0, 0, 255}, + "brightness": 255 + }, + + "ice" : { + "color": {80, 100, 255}, + "brightness": 120 + }, + + "sleep": { + "color": {185, 155, 25}, + "brightness": 60 + } +} diff --git a/modules/configloader/__init__.py b/modules/configloader/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/modules/configloader/__init__.py @@ -0,0 +1 @@ + diff --git a/modules/configloader/__main__.py b/modules/configloader/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/configloader/loader.py b/modules/configloader/loader.py new file mode 100644 index 0000000..802520d --- /dev/null +++ b/modules/configloader/loader.py @@ -0,0 +1,12 @@ +import json + +def readconfig(path): + #try: + with open(path) as cfg: + data = json.load(cfg) + + return data + + #except: + #print("[Error] Something went wrong reading the configuration file.") + #print("--", path) diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index 629fd1e..bbc1b4a 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -5,15 +5,20 @@ import time from .lib.func import * # useful functions -from .config import * # Configuration for the controller (/config.py <- change this file) -from .presets import * # presets for the lights +from modules.configloader.loader import readconfig # used to read the config files +from os.path import expanduser # to get the home dir + +homedir = expanduser("~") # get the home directory of the current user LIGHTS = {} # dictionary of all the lights +CONFIG = {} # the configuration +PRESETS = {} # the presets +PRE_URL = "" # prefix loop = asyncio.get_event_loop() # ASync loop def genUrl(params: str): - return "http://" + hue_config.address + "/api/" + hue_config.username + params + return PRE_URL + params class APIrequest: # Get Req @@ -142,9 +147,20 @@ class controller: def delay(n:int): time.sleep(n) - def init(): - jsonLights = loop.run_until_complete(APIrequest.get("/lights")) + def init( cfgPath="{0}/.config/roomcomputer/config.json".format(homedir), presetPath="{0}/.config/roomcomputer/presets.json".format(homedir) ): + config = readconfig(cfgPath) + presets = readconfig(presetPath) + + global CONFIG + CONFIG = config["hue"] + global PRESETS + PRESETS = presets + + global PRE_URL + PRE_URL = "http://" + CONFIG["address"] + "/api/" + CONFIG["username"] + + jsonLights = loop.run_until_complete(APIrequest.get("/lights")) global LIGHTS LIGHTS = json.loads(jsonLights.text) diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..8b21409 --- /dev/null +++ b/setup.sh @@ -0,0 +1,7 @@ +#!/usr/bin/bash + +cfgPath="$HOME/.config/roomcomputer" + +mkdir $cfgPath +cp default-config.json $cfgPath/config.json +cp default-presets.json $cfgPath/presets.json From 60dbe4dae7325097dca85c55ed24c634ede1364c Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 21:55:34 +0200 Subject: [PATCH 2/4] Fixed default config --- default-presets.json | 16 ++++++++-------- modules/configloader/loader.py | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/default-presets.json b/default-presets.json index 619e55e..65cee0f 100644 --- a/default-presets.json +++ b/default-presets.json @@ -1,41 +1,41 @@ { "default": { - "color": {178, 199, 255}, + "color": [178, 199, 255], "brightness": 255 }, "dim": { - "color": {178, 199, 255}, + "color": [178, 199, 255], "brightness": 111 }, "dim": { - "color": {178, 199, 255}, + "color": [178, 199, 255], "brightness": 80 }, "red": { - "color": {255, 0, 0}, + "color": [255, 0, 0], "brightness": 255 }, "green": { - "color": {0, 255, 0}, + "color": [0, 255, 0], "brightness": 255 }, "blue": { - "color": {0, 0, 255}, + "color": [0, 0, 255], "brightness": 255 }, "ice" : { - "color": {80, 100, 255}, + "color": [80, 100, 255], "brightness": 120 }, "sleep": { - "color": {185, 155, 25}, + "color": [185, 155, 25], "brightness": 60 } } diff --git a/modules/configloader/loader.py b/modules/configloader/loader.py index 802520d..87617db 100644 --- a/modules/configloader/loader.py +++ b/modules/configloader/loader.py @@ -1,12 +1,12 @@ import json def readconfig(path): - #try: - with open(path) as cfg: - data = json.load(cfg) + try: + with open(path) as cfg: + data = json.load(cfg) - return data + return data - #except: - #print("[Error] Something went wrong reading the configuration file.") - #print("--", path) + except: + print("[Error] Something went wrong reading the configuration file.") + print("--", path) From dc1d37e689e4ad94464f3bf4a5e600f5789944de Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 22:11:06 +0200 Subject: [PATCH 3/4] Fixed config loading bugs --- modules/hue/config.py | 4 ---- modules/hue/default-config.py | 8 ------- modules/hue/presets.py | 44 ----------------------------------- speech_daemon.py | 30 +++++++++++++++++++++--- 4 files changed, 27 insertions(+), 59 deletions(-) delete mode 100644 modules/hue/config.py delete mode 100644 modules/hue/default-config.py delete mode 100644 modules/hue/presets.py diff --git a/modules/hue/config.py b/modules/hue/config.py deleted file mode 100644 index 6e7119e..0000000 --- a/modules/hue/config.py +++ /dev/null @@ -1,4 +0,0 @@ -# Hue Remote Settings -class hue_config: - address = "192.168.0.3" - username = "E0ru0AeVFKEH1E30X40JAJfovg4Uu1aTkdrKQ2Oi" diff --git a/modules/hue/default-config.py b/modules/hue/default-config.py deleted file mode 100644 index db1a6ba..0000000 --- a/modules/hue/default-config.py +++ /dev/null @@ -1,8 +0,0 @@ -################################## -# RENAME THIS FILE TO "config.py"# -################################## - -# Hue Remote Settings -class hue_config: - address = "" # Local IPv4 address to the HUE bridge - username = "" # Username for the bridge diff --git a/modules/hue/presets.py b/modules/hue/presets.py deleted file mode 100644 index cfe6027..0000000 --- a/modules/hue/presets.py +++ /dev/null @@ -1,44 +0,0 @@ -# Presets goes in here -PRESETS = { - - "default": { - "color": (178, 199, 255), - "brightness": 255 - }, - - "dim": { - "color": (178, 199, 255), - "brightness": 111 - }, - - "dim": { - "color": (178, 199, 255), - "brightness": 80 - }, - - "red": { - "color": (255, 0, 0), - "brightness": 255 - }, - - "green": { - "color": (0, 255, 0), - "brightness": 255 - }, - - "blue": { - "color": (0, 0, 255), - "brightness": 255 - }, - - "ice" : { - "color": ( 80, 100, 255 ), - "brightness": 120 - }, - - "sleep": { - "color": (185, 155, 25), - "brightness": 60 - } - -} diff --git a/speech_daemon.py b/speech_daemon.py index f2f7a01..0683395 100755 --- a/speech_daemon.py +++ b/speech_daemon.py @@ -1,24 +1,47 @@ #!/usr/bin/env python +import sys + from modules.hue.hue_remote import parseCommandline from modules.hue.hue_controller import controller from modules.speech.speech import voiceInput -prefixes = ["computer", "computers"] +from modules.configloader.loader import readconfig + +from os.path import expanduser +homedir = expanduser("~") + +CONFIG = {} class speech_daemon(object): voiceInpObj = None + deviceIndex = 30 def __init__(self, deviceIndex=30): self.voiceInpObj = voiceInput() self.voiceInpObj.setMuted(False) + self.deviceIndex = deviceIndex + + def loadconfig(self): + path = homedir + "/.config/roomcomputer/config.json" + # if no config path is + # specified then choose the users default + + if( len(sys.argv) > 1 ): + path = sys.argv[1] + + cfg = readconfig(path) # read the config + + global CONFIG + CONFIG = cfg + def start(self): controller.init() - for inp in self.voiceInpObj.start(): + for inp in self.voiceInpObj.start( self.deviceIndex ): cmdBuf = inp.lower().split(" ") - if( cmdBuf[0] in prefixes ): + if( cmdBuf[0] in CONFIG["speech"]["prefixes"] ): print("CMD:", cmdBuf) parseCommandline( cmdBuf, False ) @@ -26,4 +49,5 @@ class speech_daemon(object): if __name__ == "__main__": daemon = speech_daemon() + daemon.loadconfig() daemon.start() From bd87c126e88a58c773d2df38efe4a62ed6242abe Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 22:12:04 +0200 Subject: [PATCH 4/4] Updated the default config --- default-config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/default-config.json b/default-config.json index fd226bd..0bf08f9 100644 --- a/default-config.json +++ b/default-config.json @@ -4,6 +4,7 @@ "username": "" }, "speech": { - "device_index": 30 + "device_index": 30, + "prefixes": ["computer", "computers"] } }