diff --git a/hue_cmd.py b/hue_cmd.py new file mode 100755 index 0000000..8a048b9 --- /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(): + controller.init() # very important to initialize the controller + parseCommandline() + controller.end() # also to end it + +if __name__ == "__main__": + init() diff --git a/hue_remote/default-config.py b/hue_remote/default-config.py deleted file mode 100644 index 372cacb..0000000 --- a/hue_remote/default-config.py +++ /dev/null @@ -1,6 +0,0 @@ -################################## -# RENAME THIS FILE TO "config.py"# -################################## - -address = "" -username = "" 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..6e7119e --- /dev/null +++ b/modules/hue/config.py @@ -0,0 +1,4 @@ +# 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 new file mode 100644 index 0000000..db1a6ba --- /dev/null +++ b/modules/hue/default-config.py @@ -0,0 +1,8 @@ +################################## +# 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/hue_remote/hue_controller.py b/modules/hue/hue_controller.py similarity index 93% rename from hue_remote/hue_controller.py rename to modules/hue/hue_controller.py index 20bd133..629fd1e 100644 --- a/hue_remote/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -3,17 +3,17 @@ 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 +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 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/hue_remote/hue_remote.py b/modules/hue/hue_remote.py similarity index 82% rename from hue_remote/hue_remote.py rename to modules/hue/hue_remote.py index d0bf730..fc81abd 100755 --- a/hue_remote/hue_remote.py +++ b/modules/hue/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" @@ -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 ): - 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 ): @@ -82,26 +81,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 ) - else: + parseCommand( cmd, 2, displayHelp=needHelp ) + elif( needHelp ): 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/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/modules/speech/speech.py b/modules/speech/speech.py new file mode 100644 index 0000000..1043fc8 --- /dev/null +++ b/modules/speech/speech.py @@ -0,0 +1,37 @@ +import speech_recognition as sr + +class voiceInput(object): + recognizer = sr.Recognizer() + + muted = True + + # "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 ): + self.muted = setm + + def switchMute( self ): + self.setMuted( not self.muted ) + diff --git a/speech/speech.py b/speech/speech.py deleted file mode 100644 index fa70c3c..0000000 --- a/speech/speech.py +++ /dev/null @@ -1,36 +0,0 @@ -import speech_recognition as sr - -class sr_microphone(object): - recognizer = sr.Recognizer() - - 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 - - audio = self.recognizer.listen(src) - - # Make audio -> text - return (self.recognizer.recognize_google( audio )).lower() # use googles recognizer and lower its output - - 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 ) - - -# Small test -voice = sr_microphone() -voice.setMuted(False) -print( voice.getInput() ) diff --git a/speech_daemon.py b/speech_daemon.py new file mode 100755 index 0000000..f2f7a01 --- /dev/null +++ b/speech_daemon.py @@ -0,0 +1,29 @@ +#!/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"] + +class speech_daemon(object): + voiceInpObj = None + + def __init__(self, deviceIndex=30): + self.voiceInpObj = voiceInput() + self.voiceInpObj.setMuted(False) + + def start(self): + 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() + daemon.start()