From 08fb2d8dd8122990bf97feaefbc7871c00acbbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alve=20Svar=C3=A9n?= Date: Tue, 6 Oct 2020 17:15:19 +0200 Subject: [PATCH 01/24] Added requirements file --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8d2870c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +SpeechRecognition>=3.8.1 \ No newline at end of file From 4275ef8c426f5dcde74da916d383e89731d9f2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alve=20Svar=C3=A9n?= Date: Tue, 6 Oct 2020 17:16:35 +0200 Subject: [PATCH 02/24] Update readme to add instructions for installing requirements --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a9bf07c..d5aa263 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Room-Computer is a simple room controller and is basically a controller for your ### Installation git clone https://github.com/E-Almqvist/roomcomputer.git -This is written in python, so you will literally only have to clone this repository. + pip install -r requirements.txt +This is written in python, so you will literally only have to clone this repository and install the dependencies. ### Configuration Create a copy of the file "default-config.py" and name it "config.py" then configure its contents to your needs. From eab1b369f6c2afc5c40e18434a7862f4fa013b2f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 17:52:24 +0200 Subject: [PATCH 03/24] Google API test --- speech/speech.py | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/speech/speech.py b/speech/speech.py index fa70c3c..3079987 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -1,27 +1,37 @@ import speech_recognition as sr +import io +from google.cloud import speech as sp -class sr_microphone(object): +class voiceInput(object): recognizer = sr.Recognizer() + commandFunc = None muted = True - def getInput(self): # use the object as a generator - print("Awaiting input") - if( not self.muted ): - try: - with sr.Microphone() as src: - self.recognizer.adjust_for_ambient_noise( src, duration=0.2 ) # adjust for ambient noise + def transcribe_voice( self, streamFile ): + cl = sp.SpeechClient() - audio = self.recognizer.listen(src) + with io.open( streamFile, "rb" ) as audioFile: + cont = audioFile.read() - # Make audio -> text - return (self.recognizer.recognize_google( audio )).lower() # use googles recognizer and lower its output + stream = [cont] + req = ( sp.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream ) - except sr.RequestError as err: - print("Unable to request results: {0}".format(err)) + conf = sp.RecognitionConfig( + encoding = sp.RecognitionConfig.AudioEncoding.LINEAR16, + sample_rate_hertz = 16000, + language_code = "en-US" + ) - except sr.UnknownValueError as err: - print("Unknown Error: {0}".format(err)) + + streamConf = sp.StreamingRecognitionConfig(config=conf) + + responses = cl.streaming_recognize( steamConf, req ) + + for res in responses: + for result in res.results: + for alt in result.alternatives: + print(alt.transcript) def setMuted( self, setm: bool=True ): self.muted = setm @@ -30,7 +40,5 @@ class sr_microphone(object): self.setMuted( not self.muted ) -# Small test -voice = sr_microphone() -voice.setMuted(False) -print( voice.getInput() ) +vc = voiceInput() +vc.transcribe_voice( "./stream.txt" ) From a5fa26b11762210093272a1456c518233a8889b4 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 17:58:38 +0200 Subject: [PATCH 04/24] Removed google bloat --- speech/speech.py | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/speech/speech.py b/speech/speech.py index 3079987..88ae971 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -4,41 +4,11 @@ from google.cloud import speech as sp class voiceInput(object): recognizer = sr.Recognizer() - commandFunc = None muted = True - def transcribe_voice( self, streamFile ): - cl = sp.SpeechClient() - - with io.open( streamFile, "rb" ) as audioFile: - cont = audioFile.read() - - stream = [cont] - req = ( sp.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream ) - - conf = sp.RecognitionConfig( - encoding = sp.RecognitionConfig.AudioEncoding.LINEAR16, - sample_rate_hertz = 16000, - language_code = "en-US" - ) - - - streamConf = sp.StreamingRecognitionConfig(config=conf) - - responses = cl.streaming_recognize( steamConf, req ) - - for res in responses: - for result in res.results: - for alt in result.alternatives: - print(alt.transcript) - def setMuted( self, setm: bool=True ): self.muted = setm def switchMute( self ): self.setMuted( not self.muted ) - - -vc = voiceInput() -vc.transcribe_voice( "./stream.txt" ) From db3b93e15b7af02aae69119718abb65ee9c0fde2 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 17:59:01 +0200 Subject: [PATCH 05/24] Removed more google bloat --- speech/speech.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/speech/speech.py b/speech/speech.py index 88ae971..970937f 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -1,6 +1,4 @@ import speech_recognition as sr -import io -from google.cloud import speech as sp class voiceInput(object): recognizer = sr.Recognizer() From 085bf39b29eed14c602a9efeae3765abc1451473 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 18:26:01 +0200 Subject: [PATCH 06/24] Everything costs money sadly --- speech/speech.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/speech/speech.py b/speech/speech.py index 970937f..f6926e8 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -5,8 +5,28 @@ class voiceInput(object): muted = True + def voiceToText( self, deviceIndex=30 ): + try: + with sr.Microphone( deviceIndex ) as src: + self.recognizer.adjust_for_ambient_noise( src, 0.2 ) + audio = self.recognizer.listen( src ) + text = self.recognizer.recognize_google(audio) + print(text) + + except sr.RequestError as err: + print("Unable to request results: {0}".format(err)) + + except sr.UnknownValueError as err: + print("Unknown Error: {0}".format(err)) + + def setMuted( self, setm: bool=True ): self.muted = setm def switchMute( self ): self.setMuted( not self.muted ) + + +voice = voiceInput() +voice.setMuted(False) +print( "out:", voice.voiceToText() ) From bee08b354165797dbbdbc93a5ad3220a2636c2ec Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 18:38:56 +0200 Subject: [PATCH 07/24] Added recursive listening hehe --- speech/speech.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/speech/speech.py b/speech/speech.py index f6926e8..6233e9c 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -9,10 +9,14 @@ class voiceInput(object): try: with sr.Microphone( deviceIndex ) as src: self.recognizer.adjust_for_ambient_noise( src, 0.2 ) - audio = self.recognizer.listen( src ) + print("Listening...") + audio = self.recognizer.listen( src, phrase_time_limit=5 ) + print("Thinking...") text = self.recognizer.recognize_google(audio) print(text) + return self.voiceToText(deviceIndex) + except sr.RequestError as err: print("Unable to request results: {0}".format(err)) From fcb40807b90d2215926e6f953606570629a93b89 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 18:43:08 +0200 Subject: [PATCH 08/24] Removed unneeded code --- speech/speech.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/speech/speech.py b/speech/speech.py index 6233e9c..f026911 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -19,9 +19,11 @@ class voiceInput(object): except sr.RequestError as err: print("Unable to request results: {0}".format(err)) + return self.voiceToText(deviceIndex) - except sr.UnknownValueError as err: - print("Unknown Error: {0}".format(err)) + except sr.UnknownValueError: + print("????") + return self.voiceToText(deviceIndex) def setMuted( self, setm: bool=True ): From 6927557d4813a49637390d44add45965e6562304 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 19:08:31 +0200 Subject: [PATCH 09/24] Fixed the voice to text --- speech/speech.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/speech/speech.py b/speech/speech.py index f026911..1043fc8 100644 --- a/speech/speech.py +++ b/speech/speech.py @@ -5,25 +5,28 @@ class voiceInput(object): muted = True - def voiceToText( self, deviceIndex=30 ): - try: - with sr.Microphone( deviceIndex ) as src: - self.recognizer.adjust_for_ambient_noise( src, 0.2 ) - print("Listening...") - audio = self.recognizer.listen( src, phrase_time_limit=5 ) - print("Thinking...") - text = self.recognizer.recognize_google(audio) - print(text) - - return self.voiceToText(deviceIndex) - - except sr.RequestError as err: - print("Unable to request results: {0}".format(err)) - return self.voiceToText(deviceIndex) - - except sr.UnknownValueError: - print("????") - return self.voiceToText(deviceIndex) + # "Error codes", can be used to check stuff + what = "??" + error = "ERROR" + + def start( self, deviceIndex=30 ): # a generator for everything that is said + while( True ): # loop + try: + if( not self.muted ): # this thing is not the NSA + with sr.Microphone( deviceIndex ) as src: + self.recognizer.adjust_for_ambient_noise( src, 0.2 ) + print("Listening...") + audio = self.recognizer.listen( src, phrase_time_limit=5 ) + print("Thinking...") + text = self.recognizer.recognize_google(audio) + yield text + + except sr.RequestError as err: + print("Unable to request results: {0}".format(err)) + yield self.error + + except sr.UnknownValueError: + yield self.what def setMuted( self, setm: bool=True ): @@ -32,7 +35,3 @@ class voiceInput(object): def switchMute( self ): self.setMuted( not self.muted ) - -voice = voiceInput() -voice.setMuted(False) -print( "out:", voice.voiceToText() ) From 0f50b5f4a1bbe4e88982f3a60a0e6f9600916adb Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 19:23:38 +0200 Subject: [PATCH 10/24] Module fix --- modules/hue/__init__.py | 0 modules/hue/__main__.py | 0 modules/hue/config.py | 6 ++++++ {hue_remote => modules/hue}/default-config.py | 0 {hue_remote => modules/hue}/hue_controller.py | 0 {hue_remote => modules/hue}/hue_remote.py | 0 {hue_remote => modules/hue}/lib/func.py | 0 {hue_remote => modules/hue}/presets.py | 0 modules/speech/__init__.py | 0 modules/speech/__main__.py | 0 {speech => modules/speech}/speech.py | 0 speech_daemon.py | 3 +++ 12 files changed, 9 insertions(+) create mode 100644 modules/hue/__init__.py create mode 100644 modules/hue/__main__.py create mode 100644 modules/hue/config.py rename {hue_remote => modules/hue}/default-config.py (100%) rename {hue_remote => modules/hue}/hue_controller.py (100%) rename {hue_remote => modules/hue}/hue_remote.py (100%) rename {hue_remote => modules/hue}/lib/func.py (100%) rename {hue_remote => modules/hue}/presets.py (100%) create mode 100644 modules/speech/__init__.py create mode 100644 modules/speech/__main__.py rename {speech => modules/speech}/speech.py (100%) create mode 100755 speech_daemon.py diff --git a/modules/hue/__init__.py b/modules/hue/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/hue/__main__.py b/modules/hue/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/hue/config.py b/modules/hue/config.py new file mode 100644 index 0000000..2328100 --- /dev/null +++ b/modules/hue/config.py @@ -0,0 +1,6 @@ +# Speech CMD settings +prefix = "computer" + +# Hue Remote Settings +address = "192.168.0.3" +username = "E0ru0AeVFKEH1E30X40JAJfovg4Uu1aTkdrKQ2Oi" diff --git a/hue_remote/default-config.py b/modules/hue/default-config.py similarity index 100% rename from hue_remote/default-config.py rename to modules/hue/default-config.py diff --git a/hue_remote/hue_controller.py b/modules/hue/hue_controller.py similarity index 100% rename from hue_remote/hue_controller.py rename to modules/hue/hue_controller.py diff --git a/hue_remote/hue_remote.py b/modules/hue/hue_remote.py similarity index 100% rename from hue_remote/hue_remote.py rename to modules/hue/hue_remote.py diff --git a/hue_remote/lib/func.py b/modules/hue/lib/func.py similarity index 100% rename from hue_remote/lib/func.py rename to modules/hue/lib/func.py diff --git a/hue_remote/presets.py b/modules/hue/presets.py similarity index 100% rename from hue_remote/presets.py rename to modules/hue/presets.py diff --git a/modules/speech/__init__.py b/modules/speech/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/speech/__main__.py b/modules/speech/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/speech/speech.py b/modules/speech/speech.py similarity index 100% rename from speech/speech.py rename to modules/speech/speech.py diff --git a/speech_daemon.py b/speech_daemon.py new file mode 100755 index 0000000..42a18ca --- /dev/null +++ b/speech_daemon.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python + +import lib.speech.speech From 0f0f539895e7e657f9f6152f062536578a7f25c0 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 19:32:16 +0200 Subject: [PATCH 11/24] Added daemon --- speech_daemon.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/speech_daemon.py b/speech_daemon.py index 42a18ca..23e4f00 100755 --- a/speech_daemon.py +++ b/speech_daemon.py @@ -1,3 +1,17 @@ #!/usr/bin/env python -import lib.speech.speech +from modules.hue.hue_remote import parseCommand +from modules.speech.speech import voiceInput + +class speech_daemon(object): + voiceInpObj = None + + def __init__(self, deviceIndex=30): + voiceInpObj = voiceInput() + + voiceInpObj.setMuted(False) + + voiceInpObj.start(deviceIndex) + +if __name__ == "__main__": + daemon = speech_daemon() From 0052656f33d87e01cf4a5d967b20235e1650a515 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 19:34:27 +0200 Subject: [PATCH 12/24] Added bugs --- modules/hue/hue_remote.py => hue_remote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename modules/hue/hue_remote.py => hue_remote.py (97%) diff --git a/modules/hue/hue_remote.py b/hue_remote.py similarity index 97% rename from modules/hue/hue_remote.py rename to hue_remote.py index d0bf730..3f64656 100755 --- a/modules/hue/hue_remote.py +++ b/hue_remote.py @@ -3,7 +3,7 @@ import sys -import hue_controller as hue # Actual controller +from modules.hue import hue_controller as hue # Actual controller cmd = "hue" From 54a8c761a6d23b07ea8cd9e3299f12ec1aa512e7 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 19:55:43 +0200 Subject: [PATCH 13/24] Added import errors --- hue_cmd.py | 12 ++++++++++++ modules/hue/hue_controller.py | 6 +++--- hue_remote.py => modules/hue/hue_remote.py | 8 -------- speech_daemon.py | 1 - 4 files changed, 15 insertions(+), 12 deletions(-) create mode 100755 hue_cmd.py rename hue_remote.py => modules/hue/hue_remote.py (93%) diff --git a/hue_cmd.py b/hue_cmd.py new file mode 100755 index 0000000..73e8793 --- /dev/null +++ b/hue_cmd.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +from modules.hue.hue_controller import controller +from modules.hue.hue_remote import parseCommandline + +def init(): + hue.controller.init() # very important to initialize the controller + parseCommandline() + hue.controller.end() # also to end it + +if __name__ == "__main__": + init() diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index 20bd133..2cbbd14 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -3,10 +3,10 @@ import json # API uses JSON import asyncio # ASync stuff import time -from lib.func import * # useful functions +from .lib.func import * # useful functions -import config # Configuration for the controller (/config.py <- change this file) -from presets import * # presets for the lights +#import .config as config # Configuration for the controller (/config.py <- change this file) +from .presets import * # presets for the lights LIGHTS = {} # dictionary of all the lights diff --git a/hue_remote.py b/modules/hue/hue_remote.py similarity index 93% rename from hue_remote.py rename to modules/hue/hue_remote.py index 3f64656..5ce5763 100755 --- a/hue_remote.py +++ b/modules/hue/hue_remote.py @@ -97,11 +97,3 @@ def parseCommandline(): parseCommand( cmd, 2 ) else: help() - - -def init(): - hue.controller.init() # very important to initialize the controller - parseCommandline() - hue.controller.end() # also to end it - -init() # actually call the init function diff --git a/speech_daemon.py b/speech_daemon.py index 23e4f00..55b240c 100755 --- a/speech_daemon.py +++ b/speech_daemon.py @@ -8,7 +8,6 @@ class speech_daemon(object): def __init__(self, deviceIndex=30): voiceInpObj = voiceInput() - voiceInpObj.setMuted(False) voiceInpObj.start(deviceIndex) From 2f5ea00b3f975bbd7f42ff00fb34daa9c8be4ac5 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 20:12:54 +0200 Subject: [PATCH 14/24] Made the daemon wrok --- modules/hue/hue_remote.py | 14 +++++++------- speech_daemon.py | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/modules/hue/hue_remote.py b/modules/hue/hue_remote.py index 5ce5763..b97cb97 100755 --- a/modules/hue/hue_remote.py +++ b/modules/hue/hue_remote.py @@ -31,7 +31,7 @@ boolConvert = { # this is the most spaghetti-ish code I have ever written but it works -def parseCommand( cmd:list, pos:int, i=-1 ): +def parseCommand( cmd:list, pos:int, i=-1, displayHelp=True ): index = int(i) try: if( cmd[pos] == "on" or cmd[pos] == "off" ): @@ -82,18 +82,18 @@ def parseCommand( cmd:list, pos:int, i=-1 ): help() # display help if function did nothing except (RuntimeError, TypeError, NameError, IndexError) as err: - help() # display the help page if parameters are missing (it will give out an IndexError) - print( "\n\nError: " + str(err) ) + if(displayHelp): + help() # display the help page if parameters are missing (it will give out an IndexError) + print( "\n\nError: " + str(err) ) -def parseCommandline(): - cmd = sys.argv +def parseCommandline( cmd=sys.argv, needHelp=True ): if( len(cmd) > 1 ): if( cmd[1] == "light" ): - parseCommand( cmd, 3, cmd[2] ) + parseCommand( cmd, 3, cmd[2], displayHelp=needHelp ) elif( cmd[1] == "lights" ): - parseCommand( cmd, 2 ) + parseCommand( cmd, 2, displayHelp=needHelp ) else: help() diff --git a/speech_daemon.py b/speech_daemon.py index 55b240c..896744b 100755 --- a/speech_daemon.py +++ b/speech_daemon.py @@ -1,16 +1,26 @@ #!/usr/bin/env python -from modules.hue.hue_remote import parseCommand +from modules.hue.hue_remote import parseCommandline from modules.speech.speech import voiceInput +prefixes = ["computer", "computers"] + class speech_daemon(object): voiceInpObj = None def __init__(self, deviceIndex=30): - voiceInpObj = voiceInput() - voiceInpObj.setMuted(False) + self.voiceInpObj = voiceInput() + self.voiceInpObj.setMuted(False) - voiceInpObj.start(deviceIndex) + def start(self): + return self.voiceInpObj.start() if __name__ == "__main__": daemon = speech_daemon() + + cmdBuf = None + for inp in daemon.start(): + cmdBuf = inp.lower().split(" ") + if( cmdBuf[0] in prefixes ): + print("CMD:", cmdBuf) + parseCommandline( cmdBuf[1:], False ) From a664cbb1a810b19fe7106e56d17a2dd8de52e502 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 20:39:35 +0200 Subject: [PATCH 15/24] Finished speech daemon --- hue_cmd.py | 4 ++-- modules/hue/config.py | 8 +++----- modules/hue/hue_controller.py | 4 ++-- modules/hue/hue_remote.py | 6 +++--- speech_daemon.py | 19 +++++++++++-------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/hue_cmd.py b/hue_cmd.py index 73e8793..8a048b9 100755 --- a/hue_cmd.py +++ b/hue_cmd.py @@ -4,9 +4,9 @@ from modules.hue.hue_controller import controller from modules.hue.hue_remote import parseCommandline def init(): - hue.controller.init() # very important to initialize the controller + controller.init() # very important to initialize the controller parseCommandline() - hue.controller.end() # also to end it + controller.end() # also to end it if __name__ == "__main__": init() diff --git a/modules/hue/config.py b/modules/hue/config.py index 2328100..6e7119e 100644 --- a/modules/hue/config.py +++ b/modules/hue/config.py @@ -1,6 +1,4 @@ -# Speech CMD settings -prefix = "computer" - # Hue Remote Settings -address = "192.168.0.3" -username = "E0ru0AeVFKEH1E30X40JAJfovg4Uu1aTkdrKQ2Oi" +class hue_config: + address = "192.168.0.3" + username = "E0ru0AeVFKEH1E30X40JAJfovg4Uu1aTkdrKQ2Oi" diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index 2cbbd14..629fd1e 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -5,7 +5,7 @@ import time from .lib.func import * # useful functions -#import .config as config # Configuration for the controller (/config.py <- change this file) +from .config import * # Configuration for the controller (/config.py <- change this file) from .presets import * # presets for the lights LIGHTS = {} # dictionary of all the lights @@ -13,7 +13,7 @@ LIGHTS = {} # dictionary of all the lights loop = asyncio.get_event_loop() # ASync loop def genUrl(params: str): - return "http://" + config.address + "/api/" + config.username + params + return "http://" + hue_config.address + "/api/" + hue_config.username + params class APIrequest: # Get Req diff --git a/modules/hue/hue_remote.py b/modules/hue/hue_remote.py index b97cb97..46e890f 100755 --- a/modules/hue/hue_remote.py +++ b/modules/hue/hue_remote.py @@ -31,8 +31,7 @@ boolConvert = { # this is the most spaghetti-ish code I have ever written but it works -def parseCommand( cmd:list, pos:int, i=-1, displayHelp=True ): - index = int(i) +def parseCommand( cmd:list, pos:int, index=-1, displayHelp=True ): try: if( cmd[pos] == "on" or cmd[pos] == "off" ): if( index == -1 ): @@ -94,6 +93,7 @@ def parseCommandline( cmd=sys.argv, needHelp=True ): parseCommand( cmd, 3, cmd[2], displayHelp=needHelp ) elif( cmd[1] == "lights" ): + print("gothere1") parseCommand( cmd, 2, displayHelp=needHelp ) - else: + elif( needHelp ): help() diff --git a/speech_daemon.py b/speech_daemon.py index 896744b..f2f7a01 100755 --- a/speech_daemon.py +++ b/speech_daemon.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from modules.hue.hue_remote import parseCommandline +from modules.hue.hue_controller import controller from modules.speech.speech import voiceInput prefixes = ["computer", "computers"] @@ -13,14 +14,16 @@ class speech_daemon(object): self.voiceInpObj.setMuted(False) def start(self): - return self.voiceInpObj.start() + controller.init() + + for inp in self.voiceInpObj.start(): + cmdBuf = inp.lower().split(" ") + if( cmdBuf[0] in prefixes ): + print("CMD:", cmdBuf) + parseCommandline( cmdBuf, False ) + + controller.end() if __name__ == "__main__": daemon = speech_daemon() - - cmdBuf = None - for inp in daemon.start(): - cmdBuf = inp.lower().split(" ") - if( cmdBuf[0] in prefixes ): - print("CMD:", cmdBuf) - parseCommandline( cmdBuf[1:], False ) + daemon.start() From a637ed3e3fc0b8ecd3c9823143d652ad7c5fd648 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 20:40:35 +0200 Subject: [PATCH 16/24] Removed small debug code --- modules/hue/hue_remote.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/hue/hue_remote.py b/modules/hue/hue_remote.py index 46e890f..fc81abd 100755 --- a/modules/hue/hue_remote.py +++ b/modules/hue/hue_remote.py @@ -93,7 +93,6 @@ def parseCommandline( cmd=sys.argv, needHelp=True ): parseCommand( cmd, 3, cmd[2], displayHelp=needHelp ) elif( cmd[1] == "lights" ): - print("gothere1") parseCommand( cmd, 2, displayHelp=needHelp ) elif( needHelp ): help() From 76cd3b67cada30489ceaadcceba5738b515cac7d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 20:44:40 +0200 Subject: [PATCH 17/24] Updated config --- modules/hue/default-config.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/hue/default-config.py b/modules/hue/default-config.py index 372cacb..db1a6ba 100644 --- a/modules/hue/default-config.py +++ b/modules/hue/default-config.py @@ -2,5 +2,7 @@ # RENAME THIS FILE TO "config.py"# ################################## -address = "" -username = "" +# Hue Remote Settings +class hue_config: + address = "" # Local IPv4 address to the HUE bridge + username = "" # Username for the bridge From 5b00300df6579433afcd1630aac2ab0d179ef683 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 21:50:58 +0200 Subject: [PATCH 18/24] 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 d8a13aea858972e5813a2358a0f37a8582dfec28 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 21:55:34 +0200 Subject: [PATCH 19/24] 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 2082cee4e07e1da4747243afb276d08400cd82d2 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 22:11:06 +0200 Subject: [PATCH 20/24] 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 622303a6326ffa694248852307effe03b318ea3f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 22:12:04 +0200 Subject: [PATCH 21/24] 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"] } } From 8a226946d88606118f27486c35f7af1e425958de Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 6 Oct 2020 22:30:10 +0200 Subject: [PATCH 22/24] Updated the README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d5aa263..76bb095 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,12 @@ Room-Computer is a simple room controller and is basically a controller for your ### Installation git clone https://github.com/E-Almqvist/roomcomputer.git pip install -r requirements.txt -This is written in python, so you will literally only have to clone this repository and install the dependencies. -### Configuration -Create a copy of the file "default-config.py" and name it "config.py" then configure its contents to your needs. +### Setup and Configuration +Run the `setup.sh` script in order to copy the necessary files to `~/.config/roomcomputer/`. If you are planning to create a service for the `speech_daemon.py` with systemd; then you can specify its configuration file as the first argument: `speech_daemon.py /path/to/config/config.json`. -#### Hue Light Controller -You can create presets in the "presets.py" file. Follow this syntax *(and Python syntax of course)*: +#### HUE Lights presets +You can create presets in the `~/.config/roomcomputer/presets.json` file. Follow this syntax *(and JSON syntax of course)*: ```python PRESETS = { "mypreset": { @@ -22,7 +21,7 @@ PRESETS = { ### Usage -#### Hue Light Controller +#### HUE Remote --Help page-- 'hue' : Display this help page 'hue light (index)' ... : Specify light target, from 1-3 @@ -41,11 +40,12 @@ PRESETS = { 'hue lights set color 255 255 255' : Set all lights colors to white ----------------- - For convenience, you can create an alias for the script file. Append this to your shells rc file: - alias hue="/path/to/the/cloned/repo/hue_remote.py" +For convenience, you can create an alias for the script file. Append this to your shells rc file: +`alias hue="/path/to/the/cloned/repo/hue_cmd.py"`

Features

-* Hue Light Controller *(hue_remote.py)* +* HUE Light Controller (command-line) `hue_cmd.py)` +* HUE Light Controller (voice daemon) `speech_daemon.py` And more to come! From d316656112cb393aa7b62a1b742c77b2542ef1b1 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" <38406360+E-Almqvist@users.noreply.github.com> Date: Tue, 6 Oct 2020 22:32:19 +0200 Subject: [PATCH 23/24] Small fix --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76bb095..2f07604 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ Run the `setup.sh` script in order to copy the necessary files to `~/.config/roo #### HUE Lights presets You can create presets in the `~/.config/roomcomputer/presets.json` file. Follow this syntax *(and JSON syntax of course)*: -```python -PRESETS = { +```json +{ "mypreset": { - "color": (178, 199, 255), # RGB, from 0-255 + "color": [178, 199, 255], # RGB, from 0-255 "brightness": 100 # from 0-255 }, } From 7a861ae75709482cc337d761b3e3da237ce55889 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" <38406360+E-Almqvist@users.noreply.github.com> Date: Tue, 6 Oct 2020 22:33:11 +0200 Subject: [PATCH 24/24] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2f07604..2dfe313 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ You can create presets in the `~/.config/roomcomputer/presets.json` file. Follow ```json { "mypreset": { - "color": [178, 199, 255], # RGB, from 0-255 - "brightness": 100 # from 0-255 + "color": [178, 199, 255], + "brightness": 100 }, } ```