diff --git a/hugvey/central_command.py b/hugvey/central_command.py index a21ebb7..8fab0ef 100644 --- a/hugvey/central_command.py +++ b/hugvey/central_command.py @@ -13,9 +13,9 @@ import asyncio from hugvey.communication import getTopic, zmqSend, zmqReceive from hugvey.panopticon import Panopticon from hugvey.story import Story -from hugvey.voice.google import GoogleVoiceClient -from hugvey.voice.player import Player -from hugvey.voice.streamer import AudioStreamer +from hugvey.speech.google import GoogleVoiceClient +from hugvey.speech.player import Player +from hugvey.speech.streamer import AudioStreamer import json import logging import queue @@ -45,7 +45,7 @@ logger = logging.getLogger("command") class CentralCommand(object): """docstring for CentralCommand.""" - def __init__(self, debug_mode=False): + def __init__(self, args = {}, debug_mode=False): self.debug = debug_mode self.eventQueue = asyncio.Queue() self.commandQueue = asyncio.Queue() @@ -55,6 +55,7 @@ class CentralCommand(object): self.hugveyLock = asyncio.Lock() self.start_time = time.time() self.languageFiles = {} + self.args = args # cli args def loadConfig(self, filename): if hasattr(self, 'config'): @@ -63,6 +64,10 @@ class CentralCommand(object): with open(filename, 'r') as fp: logger.debug('Load config from {}'.format(filename)) self.config = yaml.safe_load(fp) + for arg in vars(self.args): + if arg in self.config: + logger.debug("Override argument {}".format(arg)) + self.config[arg] = getattr(self.args,arg) self.hugvey_ids = [i + 1 for i in range(self.config['hugveys'])] @@ -127,12 +132,6 @@ class CentralCommand(object): def _queueCommand(self, hv_id, msg): self.commandQueue.put_nowait((hv_id, msg)) -# if msg['action'] == 'play': -# self.commandQueue.put_nowait((hv_id, { -# 'action': 'play', -# 'msg': "This is an interrption", -# 'id': 'test', -# })) def commandAllHugveys(self, msg): for hv_id in self.hugvey_ids: @@ -354,19 +353,27 @@ class HugveyState(object): if event['event'] == 'change_language': self.setLanguage(event['lang_code']) if event['event'] == 'play_msg': - self.logger.info("DO PLAY :-)") + self.logger.info(f"Play given message {event['msg_id']}") if not self.story: self.logger.critical("No story to play message in") else: #restart first so that story loads the new json - self.restart() +# self.restart() + if self.story is None: + return + +# self.story.stop() + self.pause() # wait a tat for the restart loops to complete await asyncio.sleep(.1) + self.logger.debug('restarted') msg = self.story.get(event['msg_id']) if not msg: self.logger.critical("Invalid ID to play: {}".format(event['msg_id'])) else: self.story.setCurrentMessage(msg) + + self.resume() self.eventQueue = None @@ -438,7 +445,7 @@ class HugveyState(object): self.ip, int(self.command.config['voice']['port'])) - if self.command.debug: + if self.command.config['voyeur']: self.logger.warn("Debug on: Connecting Audio player") self.player = Player( self.command.config['voice']['src_rate'], self.command.config['voice']['out_rate']) diff --git a/hugvey/voice/google.py b/hugvey/speech/google.py similarity index 99% rename from hugvey/voice/google.py rename to hugvey/speech/google.py index c925e5e..7af35e2 100644 --- a/hugvey/voice/google.py +++ b/hugvey/speech/google.py @@ -18,7 +18,7 @@ import queue import uuid -logger = logging.getLogger("voice") +logger = logging.getLogger("speech") class RequireRestart(Exception): pass diff --git a/hugvey/voice/player.py b/hugvey/speech/player.py similarity index 100% rename from hugvey/voice/player.py rename to hugvey/speech/player.py diff --git a/hugvey/voice/streamer.py b/hugvey/speech/streamer.py similarity index 100% rename from hugvey/voice/streamer.py rename to hugvey/speech/streamer.py diff --git a/hugvey/story.py b/hugvey/story.py index 2098ba9..86f523f 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -37,6 +37,7 @@ class Message(object): self.interruptCount = 0 self.afterrunTime = 0. # the time after this message to allow for interrupts self.finishTime = None # message can be finished without finished utterance (with instant replycontains) + self.parseForVariables() @classmethod def initFromJson(message, data, story): @@ -47,6 +48,12 @@ class Message(object): msg.audioFile = data['audio']['file'] return msg + def parseForVariables(self): + """ + Find variables in text + """ + self.variables = re.findall('\$(\w+)', self.text) + def setReply(self, reply): self.reply = reply @@ -188,7 +195,7 @@ class Condition(object): logger.debug('Got match on {}'.format(self.vars['regex'])) results = result.groupdict() for captureGroup in results: - story.variables[captureGroup] = results[captureGroup] + story.variableValues[captureGroup] = results[captureGroup] # TODO: implement 'instant match' -> don't wait for isFinished() @@ -353,6 +360,7 @@ class Story(object): self.currentReply = None self.timer = Stopwatch() self.isRunning = False + self.variables = {} def pause(self): logger.debug('pause hugvey') @@ -371,6 +379,12 @@ class Story(object): } # print(self.log) return summary + + def registerVariable(self, variableName, message): + if variableName not in self.variables: + self.variables[variableName] = [message] + else: + self.variables[variableName].append(message) def setStoryData(self, story_data): """ @@ -385,6 +399,7 @@ class Story(object): self.diversions = [] self.directionsPerMsg = {} self.startMessage = None # The entrypoint to the graph + self.variables = {} self.reset() for el in self.data: @@ -412,7 +427,7 @@ class Story(object): self.lastMsgTime = None self.lastSpeechStartTime = None self.lastSpeechEndTime = None - self.variables = {} # captured variables from replies + self.variableValues = {} # captured variables from replies self.finish_time = False self.events = [] # queue of received events diff --git a/hugvey_server.py b/hugvey_server.py index c866679..d190c64 100644 --- a/hugvey_server.py +++ b/hugvey_server.py @@ -21,6 +21,11 @@ if __name__ == '__main__': '-v', action='count', default=0 ) + argParser.add_argument( + '--voyeur', + action='store_true', + help="Listen in on what is being said'." + ) args = argParser.parse_args() # print(coloredlogs.DEFAULT_LOG_FORMAT) @@ -33,6 +38,6 @@ if __name__ == '__main__': fmt="%(asctime)s %(hostname)s %(name)s[%(process)d,%(threadName)s] %(levelname)s %(message)s" ) - command = CentralCommand(debug_mode=args.verbose > 0) + command = CentralCommand(args=args, debug_mode=args.verbose > 0) command.loadConfig(args.config) command.start() diff --git a/requirements.server.txt b/requirements.server.txt index 931fba4..c654d26 100644 --- a/requirements.server.txt +++ b/requirements.server.txt @@ -3,3 +3,4 @@ pyaudio coloredlogs pyyaml google-cloud-speech +requests-threads