示例#1
0
def getBirthdays(update, context):
    if (checkPrivate(update)):
        say(update, context, "Essa funcionalidade é exclusiva para grupos")
    else:
        response_message = "Aniversariantes:\n"
        aniversariantes = birthdays.find()
        niverArr = []
        for i in aniversariantes:
            niverArr.append(i)
            i["month"] = int(i["dayMonth"][2] + i["dayMonth"][3])
            i["day"] = int(i["dayMonth"][0] + i["dayMonth"][1])
        niverArr.sort(key=day)
        niverArr.sort(key=month)
        previousMonth = 0
        for j in niverArr:
            if (j["month"] != previousMonth):
                response_message += newMonth(j["month"]) + "\n"
            previousMonth = j["month"]
            if (len(str(j["day"])) == 1):
                response_message += "0" + str(
                    j["day"]) + " - " + j["userName"] + "\n"
            else:
                response_message += str(
                    j["day"]) + " - " + j["userName"] + "\n"
        say(update, context, response_message)
示例#2
0
文件: main.py 项目: vfciot/GassistPi
 def interpreter_mode_trigger(self,switch):
     if configuration['Speechtotext']['Google_Cloud_Speech']['Cloud_Speech_Control']=='Disabled':
         say("Cloud speech has not been enabled")
     else:
         if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
             if configuration['Speechtotext']['Google_Cloud_Speech']['Google_Cloud_Speech_Credentials_Path']!="ENTER THE PATH TO YOUR CLOUD SPEECH CREDENTIALS FILE HERE":
                 os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = configuration['Speechtotext']['Google_Cloud_Speech']['Google_Cloud_Speech_Credentials_Path']
                 if switch=="Start":
                     self.interpreter=True
                     say("Starting interpreter.")
                     self.interpreter_speech_recorder()
                 elif switch=="Stop":
                     self.interpreter=False
                     self.interpconvcounter=0
                     say("Stopping interpreter.")
                 else:
                     self.interpreter=False
         else:
             if switch=="Start":
                 self.interpreter=True
                 say("Starting interpreter.")
                 self.interpreter_speech_recorder()
             elif switch=="Stop":
                 self.interpreter=False
                 self.interpconvcounter=0
                 say("Stopping interpreter.")
             else:
                 self.interpreter=False
示例#3
0
 def __text_action(self, text=None):
     if text is not None:
         # requests.get("http://localhost:8080/statement?text=%s" % text)
         # self.speech.synthesize_text(text)
         if self.bot_vn in ['ty', 'be', 'xuka']:
             say(text, 'vi', self.bot_vn)
         else:
             say(text)
示例#4
0
文件: main.py 项目: vfciot/GassistPi
 def interpreter_mode_tts(self,text,count):
     self.interpconvcounter=self.interpconvcounter+1
     if (count % 2)==0:
         say(text,self.interpttslang1,self.interpttslang2)
         self.interpreter_speech_recorder()
     else:
         say(text,self.interpttslang2,self.interpttslang1)
         self.interpreter_speech_recorder()
示例#5
0
文件: main.py 项目: vfciot/GassistPi
 def single_user_response(self,prompt):
     self.singledetectedresponse=''
     self.singleresposne=True
     say(prompt)
     self.assistant.start_conversation()
     while self.singledetectedresponse=='':
         time.sleep(.1)
     self.singleresposne=False
     return self.singledetectedresponse
示例#6
0
def getHelp(update, context):
    response_message = "Olá sou o iSpirito, por enquanto em grupos eu apenas registro e lembro os" + \
        " aniversários de todo mundo, os comandos são os seguintes:\n/mybirthday <DD/MM/AAAA> Para registrar" + \
        " seu aniversário\n/birthdaylist Lista os aniversários registrados\n No privado tenho algumas funcionalidades" + \
        " extras como:\n - Enviar email de justificativa pro RH\n - FAQ, com perguntas frequentes sobre diversos temas" + \
        " pertinentes à empresa\n - Segfault, por meio do bot é possível fazer reclamações 100% anônimas pro RH" + \
        " (espero que ninguem precise usar esse recurso)\n - Sugestões, onde vc pode fazer a sugestão de ideias" + \
        " à empresa, podendo ser direcionado ou com o escopo geral\nPra usar essas funcionalidades basta me chamar" + \
        " no privado!\n Espero ser útil! 👻"
    say(update, context, response_message)
示例#7
0
    def __maps_action(self, nlu_entities=None):

        location = None
        map_type = None
        origin = None
        destination = None
        maps_url = None
        if self.bot_vn in ['ty', 'be', 'xuka']:
            if nlu_entities is not None:
                if 'city' in nlu_entities:
                    location = nlu_entities['city'][0]["value"]
                if 'country' in nlu_entities:
                    country = nlu_entities['country'][0]["value"]
                if 'from' in nlu_entities:
                    origin = nlu_entities['from'][0]["value"]
                if 'to' in nlu_entities:
                    destination = nlu_entities['to'][0]["value"]

            if origin is not None and destination is not None:
                maps_url = self.knowledge.get_direction_map(
                    origin, destination)
                location = 'đường đi'
            else:
                if location is not None:
                    maps_url = self.knowledge.get_map_url(location, map_type)
                else:
                    self.__text_action(
                        "Em xin lỗi, em không biết nơi anh muốn tìm")
            if maps_url is not None:
                maps_action = "chắc chắn rồi, đây là bản đồ %s." % location
                body = {'url': maps_url}
                # requests.post("http://localhost:8080/image", data=json.dumps(body))
                say(maps_action, 'vi', self.bot_vn)
        else:
            if nlu_entities is not None:
                if 'location' in nlu_entities:
                    location = nlu_entities['location'][0]["value"]
                if "Map_Type" in nlu_entities:
                    map_type = nlu_entities['Map_Type'][0]["value"]

            if location is not None:
                maps_url = self.knowledge.get_map_url(location, map_type)
                maps_action = "Sure. Here's a map of %s." % location
                body = {'url': maps_url}
                # requests.post("http://localhost:8080/image", data=json.dumps(body))
                self.speech.synthesize_text(maps_action)
            else:
                self.__text_action(
                    "I'm sorry, I couldn't understand what location you wanted."
                )
示例#8
0
文件: main.py 项目: vfciot/GassistPi
 def interpreter_speech_recorder(self):
     if self.interpreter:
         interpreteraudio='/tmp/interpreter.wav'
         subprocess.Popen(["aplay", "{}/sample-audio-files/Fb.wav".format(ROOT_PATH)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         while not record_to_file(interpreteraudio):
             time.sleep(.1)
         if (self.interpconvcounter % 2)==0:
             text=self.cloud_speech_transcribe(interpreteraudio,self.interpcloudlang1)
             print("Local Speaker: "+text)
             if 'stop' in text.lower():
                 self.interpreter_mode_trigger('Stop')
         elif (self.interpconvcounter % 2)==1:
             text=self.cloud_speech_transcribe(interpreteraudio,self.interpcloudlang2)
             print("Foreign Speaker: "+text)
         self.interpreter_mode_tts(text,self.interpconvcounter)
     else:
         say("Interpreter not active.")
示例#9
0
    def __news_action(self, nlu_entities=None, language='vi', oldintent=None):
        country = None
        intent = None
        action = None
        if nlu_entities is not None:
            # get entites from API
            if 'country' in nlu_entities:
                country = nlu_entities['country'][0]['value']
            if 'intent' in nlu_entities:
                intent = nlu_entities['intent'][0]['value']

            # doc tin tuc cua nc nao
            if country is not None and 'việt' in country.lower():
                self.__text_action('tin việt nam ạ, chờ em tý!')
                # todo:get_vnex_news
                news_vn = self.knowledge.get_news('vi')
                if news_vn:
                    # requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines}))
                    interest = self.nlg.article_interest(news_vn)
                    if interest is not None:
                        self.__text_action(interest)
            elif country is not None and 'nhật bản' in country.lower():
                self.__text_action(
                    'tin nhật bản ạ, chờ tý em bảo chị gu gồ đọc cho %s nghe' %
                    my_name_vn)
                news_jp = self.knowledge.get_news('jp')
                if news_jp:
                    # requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines}))
                    interest = self.nlg.article_interest(news_jp)
                    if interest is not None:
                        say(interest, 'ja')
                        self.decide_action(langague=language,
                                           oldintent=intent,
                                           pre_message=interest)
                else:
                    self.__text_action('không tìm thấy tin nào hết, %s ạ' %
                                       my_name_vn)
            elif oldintent is None:
                self.__text_action(
                    'Anh muốn nghe tin gì, nước nào nhật bản hay việt nam hả  %s ?'
                    % my_name_vn)
                self.decide_action(langague=language, oldintent=intent)
示例#10
0
    def __alarm(self, nlu_entities=None):
        __datetime = None
        __action = None
        current_ts = time.time()
        if nlu_entities is not None:
            if 'datetime' in nlu_entities:
                __datetime = nlu_entities['datetime'][0]['value']

        if __datetime is not None:
            dtime = dateutil.parser.parse(__datetime)
            str = '%s giờ, %s, Ngày %s, Tháng %s' % (dtime.hour, dtime.minute,
                                                     dtime.day, dtime.month)
            timestamp2 = time.mktime(dtime.timetuple())
            delay = timestamp2 - current_ts
            if delay > 0:
                _thread.start_new_thread(self.__start_alarm, (
                    "alarm",
                    delay,
                ))
                say('đã cài báo thức %s', str)
示例#11
0
def tasmota_control(phrase,devname,devip):
    if 'on' in phrase:
        try:
            rq=requests.head("http://"+devip+"/cm?cmnd=Power%20on")
            say("Tunring on "+devname)
        except requests.exceptions.ConnectionError:
            say("Device not online")
    elif 'off' in phrase:
        try:
            rq=requests.head("http://"+devip+"/cm?cmnd=Power%20off")
            say("Tunring off "+devname)
        except requests.exceptions.ConnectionError:
            say("Device not online")
示例#12
0
def tasmota_control(phrase,devname,devip):
    if custom_action_keyword['Dict']['On'] in phrase:
        try:
            rq=requests.head("http://"+devip+"/cm?cmnd=Power%20on")
            say("Tunring on "+devname)
        except requests.exceptions.ConnectionError:
            say("Device not online")
    elif custom_action_keyword['Dict']['Off'] in phrase:
        try:
            rq=requests.head("http://"+devip+"/cm?cmnd=Power%20off")
            say("Tunring off "+devname)
        except requests.exceptions.ConnectionError:
            say("Device not online")
示例#13
0
def birthdayToday(update, context):
    all = checkBirthday()
    if (all):
        if (len(all) == 1):
            response_message = "Hoje temos um aniversariante!!!!!\nParabéns " + \
                all[0]["userName"] + " pelos " + \
                str(all[0]["idade"]) + " aninhos!"
        else:
            response_message = "Hoje temos alguns aniversariantes!!!!!\n"
            for i in all:
                response_message += "Parabéns " + \
                    i["userName"] + " pelos " + str(i["idade"]) + " aninhos!\n"
        say(update, context, "🥳")
        say(update, context, "🥳")
        say(update, context, "🥳")
        say(update, context, response_message)
示例#14
0
def setBirthday(update, context):
    userID = update.message.from_user.id
    checkdb = birthdays.count_documents({"userID": userID})
    if (checkPrivate(update)):
        say(update, context, "Essa funcionalidade é exclusiva para grupos")
    else:
        if (validateDate(context)):
            birthday = context.args[0].split("/")
            dayMonth = birthday[0] + birthday[1]
            year = birthday[2]
            name = str(update.message.from_user.first_name)
            if update.message.from_user.last_name:
                name = name + " " + update.message.from_user.last_name
            newBirthday = {
                "userID": update.message.from_user.id,
                "userName": name,
                "dayMonth": dayMonth,
                "year": year
            }
            if (checkdb == 0):
                birthdays.insert_one(newBirthday)
                msgs = [
                    "Vou me lembrar! (É sério)", "Anotei aqui!",
                    "Seu aniversário agora está registrado"
                ]
                index = random.randint(0, len(msgs) - 1)
                say(update, context, msgs[index])
            else:
                birthdays.replace_one({"userID": update.message.from_user.id},
                                      newBirthday)
                msgs = ["Atualizei aqui!", "Seu aniversário foi atualizado!"]
                index = random.randint(0, len(msgs) - 1)
                say(update, context, msgs[index])

        else:
            userExists = birthdays.find_one(
                {"userID": update.message.from_user.id})
            if (userExists):
                response_message = "Seu aniversário já está registrado, caso queira modificar a data insira sua data de aniversário no seguinte formato:'/mybirthday DD/MM/YYYY'"
            else:
                response_message = "Por favor insira sua data de aniversário no seguinte formato:'/mybirthday DD/MM/YYYY'"
            say(update, context, response_message)
示例#15
0
文件: main.py 项目: capic/GassistPi
    def main(self):
        parser = argparse.ArgumentParser(
            formatter_class=argparse.RawTextHelpFormatter)
        parser.add_argument('--device-model-id',
                            '--device_model_id',
                            type=str,
                            metavar='DEVICE_MODEL_ID',
                            required=False,
                            help='the device model ID registered with Google')
        parser.add_argument('--project-id',
                            '--project_id',
                            type=str,
                            metavar='PROJECT_ID',
                            required=False,
                            help='the project ID used to register this device')
        parser.add_argument('--device-config',
                            type=str,
                            metavar='DEVICE_CONFIG_FILE',
                            default=os.path.join(
                                os.path.expanduser('~/.config'),
                                'googlesamples-assistant',
                                'device_config_library.json'),
                            help='path to store and read device configuration')
        parser.add_argument('--credentials',
                            type=existing_file,
                            metavar='OAUTH2_CREDENTIALS_FILE',
                            default=os.path.join(
                                os.path.expanduser('~/.config'),
                                'google-oauthlib-tool', 'credentials.json'),
                            help='path to store and read OAuth2 credentials')
        parser.add_argument('-v',
                            '--version',
                            action='version',
                            version='%(prog)s ' + Assistant.__version_str__())

        args = parser.parse_args()
        with open(args.credentials, 'r') as f:
            credentials = google.oauth2.credentials.Credentials(token=None,
                                                                **json.load(f))

        device_model_id = None
        last_device_id = None
        try:
            with open(args.device_config) as f:
                device_config = json.load(f)
                device_model_id = device_config['model_id']
                last_device_id = device_config.get('last_device_id', None)
        except FileNotFoundError:
            pass

        if not args.device_model_id and not device_model_id:
            raise Exception('Missing --device-model-id option')

        # Re-register if "device_model_id" is given by the user and it differs
        # from what we previously registered with.
        should_register = (args.device_model_id
                           and args.device_model_id != device_model_id)

        device_model_id = args.device_model_id or device_model_id
        with Assistant(credentials, device_model_id) as assistant:
            self.assistant = assistant
            subprocess.Popen(
                ["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"],
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
            events = assistant.start()
            device_id = assistant.device_id
            print('device_model_id:', device_model_id)
            print('device_id:', device_id + '\n')

            # Re-register if "device_id" is different from the last "device_id":
            if should_register or (device_id != last_device_id):
                if args.project_id:
                    register_device(args.project_id, credentials,
                                    device_model_id, device_id)
                    pathlib.Path(os.path.dirname(
                        args.device_config)).mkdir(exist_ok=True)
                    with open(args.device_config, 'w') as f:
                        json.dump(
                            {
                                'last_device_id': device_id,
                                'model_id': device_model_id,
                            }, f)
                else:
                    print(WARNING_NOT_REGISTERED)

            for event in events:
                self.process_event(event)
                usrcmd = event.args
                with open('/home/pi/GassistPi/src/diyHue/config.json',
                          'r') as config:
                    hueconfig = json.load(config)
                for i in range(1, len(hueconfig['lights']) + 1):
                    try:
                        if str(hueconfig['lights'][str(i)]
                               ['name']).lower() in str(usrcmd).lower():
                            assistant.stop_conversation()
                            hue_control(
                                str(usrcmd).lower(), str(i),
                                str(hueconfig['lights_address'][str(i)]['ip']))
                            break
                    except Keyerror:
                        say('Unable to help, please check your config file')

                for num, name in enumerate(tasmota_devicelist):
                    if name.lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        tasmota_control(
                            str(usrcmd).lower(), name.lower(),
                            tasmota_deviceip[num])
                        break
                if commands['magic_mirror']['name'].lower() in str(
                        usrcmd).lower():
                    assistant.stop_conversation()
                    self.magic_mirror_treatment(usrcmd)
                if 'ingredients'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    ingrequest = str(usrcmd).lower()
                    ingredientsidx = ingrequest.find('for')
                    ingrequest = ingrequest[ingredientsidx:]
                    ingrequest = ingrequest.replace('for', "", 1)
                    ingrequest = ingrequest.replace("'}", "", 1)
                    ingrequest = ingrequest.strip()
                    ingrequest = ingrequest.replace(" ", "%20", 1)
                    getrecipe(ingrequest)
                if 'kickstarter'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    kickstarter_tracker(str(usrcmd).lower())
                if 'trigger'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    Action(str(usrcmd).lower())
                if 'stream'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                if 'stop'.lower() in str(usrcmd).lower():
                    stop()
                if 'radio'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    radio(str(usrcmd).lower())
                if 'wireless'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    ESP(str(usrcmd).lower())
                if 'parcel'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    track()
                if 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower(
                ) in str(usrcmd).lower():
                    assistant.stop_conversation()
                    feed(str(usrcmd).lower())
                if 'on kodi'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    kodiactions(str(usrcmd).lower())
                # Google Assistant now comes built in with chromecast control, so custom function has been commented
                # if 'chromecast'.lower() in str(usrcmd).lower():
                #     assistant.stop_conversation()
                #     if 'play'.lower() in str(usrcmd).lower():
                #         chromecast_play_video(str(usrcmd).lower())
                #     else:
                #         chromecast_control(usrcmd)
                if 'pause music'.lower() in str(usrcmd).lower(
                ) or 'resume music'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing():
                        if 'pause music'.lower() in str(usrcmd).lower():
                            vlcplayer.pause_vlc()
                    if checkvlcpaused():
                        if 'resume music'.lower() in str(usrcmd).lower():
                            vlcplayer.play_vlc()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if 'play next'.lower() in str(usrcmd).lower(
                ) or 'play next track'.lower() in str(usrcmd).lower(
                ) or 'play next song'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() or checkvlcpaused() == True:
                        vlcplayer.stop_vlc()
                        vlcplayer.change_media_next()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if 'play previous'.lower() in str(usrcmd).lower(
                ) or 'play previous track'.lower() in str(usrcmd).lower(
                ) or 'play previous song'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() or checkvlcpaused() == True:
                        vlcplayer.stop_vlc()
                        vlcplayer.change_media_previous()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if 'music volume'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() == True or checkvlcpaused(
                    ) == True:
                        if 'set'.lower() in str(usrcmd).lower(
                        ) or 'change'.lower() in str(usrcmd).lower():
                            if 'hundred'.lower() in str(usrcmd).lower(
                            ) or 'maximum' in str(usrcmd).lower():
                                settingvollevel = 100
                                with open('/home/pi/.mediavolume.json',
                                          'w') as vol:
                                    json.dump(settingvollevel, vol)
                            elif 'zero'.lower() in str(usrcmd).lower(
                            ) or 'minimum' in str(usrcmd).lower():
                                settingvollevel = 0
                                with open('/home/pi/.mediavolume.json',
                                          'w') as vol:
                                    json.dump(settingvollevel, vol)
                            else:
                                for settingvollevel in re.findall(
                                        r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                    with open('/home/pi/.mediavolume.json',
                                              'w') as vol:
                                        json.dump(settingvollevel, vol)
                            print('Setting volume to: ' + str(settingvollevel))
                            vlcplayer.set_vlc_volume(int(settingvollevel))
                        elif 'increase'.lower() in str(usrcmd).lower(
                        ) or 'decrease'.lower() in str(usrcmd).lower(
                        ) or 'reduce'.lower() in str(usrcmd).lower():
                            if os.path.isfile("/home/pi/.mediavolume.json"):
                                with open('/home/pi/.mediavolume.json',
                                          'r') as vol:
                                    oldvollevel = json.load(vol)
                                    for oldvollevel in re.findall(
                                            r'\b\d+\b', str(oldvollevel)):
                                        oldvollevel = int(oldvollevel)
                            else:
                                oldvollevel = vlcplayer.get_vlc_volume
                                for oldvollevel in re.findall(
                                        r"[-+]?\d*\.\d+|\d+", str(output)):
                                    oldvollevel = int(oldvollevel)
                            if 'increase'.lower() in str(usrcmd).lower():
                                if any(char.isdigit() for char in str(usrcmd)):
                                    for changevollevel in re.findall(
                                            r'\b\d+\b', str(usrcmd)):
                                        changevollevel = int(changevollevel)
                                else:
                                    changevollevel = 10
                                newvollevel = oldvollevel + changevollevel
                                print(newvollevel)
                                if int(newvollevel) > 100:
                                    settingvollevel == 100
                                elif int(newvollevel) < 0:
                                    settingvollevel == 0
                                else:
                                    settingvollevel = newvollevel
                                with open('/home/pi/.mediavolume.json',
                                          'w') as vol:
                                    json.dump(settingvollevel, vol)
                                print('Setting volume to: ' +
                                      str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                            if 'decrease'.lower() in str(usrcmd).lower(
                            ) or 'reduce'.lower() in str(usrcmd).lower():
                                if any(char.isdigit() for char in str(usrcmd)):
                                    for changevollevel in re.findall(
                                            r'\b\d+\b', str(usrcmd)):
                                        changevollevel = int(changevollevel)
                                else:
                                    changevollevel = 10
                                newvollevel = oldvollevel - changevollevel
                                print(newvollevel)
                                if int(newvollevel) > 100:
                                    settingvollevel == 100
                                elif int(newvollevel) < 0:
                                    settingvollevel == 0
                                else:
                                    settingvollevel = newvollevel
                                with open('/home/pi/.mediavolume.json',
                                          'w') as vol:
                                    json.dump(settingvollevel, vol)
                                print('Setting volume to: ' +
                                      str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                        else:
                            say("Sorry I could not help you")
                    else:
                        say("Sorry nothing is playing right now")
                if 'refresh'.lower() in str(usrcmd).lower() and 'music'.lower(
                ) in str(usrcmd).lower():
                    assistant.stop_conversation()
                    refreshlists()
                if 'google music'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    gmusicselect(str(usrcmd).lower())
                if 'spotify'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    spotify_playlist_select(str(usrcmd).lower())
        if custom_wakeword:
            self.detector.terminate()
示例#16
0
文件: main.py 项目: capic/GassistPi
    def magic_mirror_treatment(usrcmd):
        magic_mirror_commands = commands['magic_mirror']
        magic_mirror_messages = messages['magic_mirror']

        try:
            mmmcommand = str(usrcmd).lower()

            if magic_mirror_commands['modules']['weather']['name'].lower(
            ) in mmmcommand:
                mmmcommand_accurate = get_best_accurate_command(
                    magic_mirror_commands['modules']['weather']['actions'],
                    mmmcommand)
                if mmmcommand_accurate is not None:
                    if magic_mirror_commands['actions']['show'].lower(
                    ) == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['show'] + ' ' + \
                                         magic_mirror_messages['commands']['generic']['module'] + ' ' + \
                                         magic_mirror_messages['commands']['generic']['modules']['weather']
                        say(message_to_say)
                        mmreq_one = requests.get(
                            "http://" + mmmip +
                            ":8080/remote?action=SHOW&module=module_2_currentweather"
                        )
                        mmreq_two = requests.get(
                            "http://" + mmmip +
                            ":8080/remote?action=SHOW&module=module_3_currentweather"
                        )
                    if magic_mirror_commands['actions']['hide'].lower(
                    ) == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['hide'] + ' ' + \
                                         magic_mirror_messages['commands']['generic']['module'] + ' ' + \
                                         magic_mirror_messages['commands']['generic']['modules']['weather']
                        say(message_to_say)
                        mmreq_one = requests.get(
                            "http://" + mmmip +
                            ":8080/remote?action=HIDE&module=module_2_currentweather"
                        )
                        mmreq_two = requests.get(
                            "http://" + mmmip +
                            ":8080/remote?action=HIDE&module=module_3_currentweather"
                        )
            else:
                mmmcommand_accurate = get_best_accurate_command(
                    magic_mirror_commands['generic']['actions'], mmmcommand)
                if mmmcommand_accurate is not None:
                    if magic_mirror_commands['generic']['actions'][
                            'power_off'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['power_off'] + ' ' + \
                                         magic_mirror_messages['name']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/remote?action=SHUTDOWN")
                    if magic_mirror_commands['generic']['actions'][
                            'reboot'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands'][
                            'generic']['reboot'] + ' ' + magic_mirror_messages[
                                'name']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/remote?action=REBOOT")
                    if magic_mirror_commands['generic']['actions'][
                            'restart'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['restart'] + ' ' + \
                                         magic_mirror_messages['name']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/remote?action=RESTART")
                    if magic_mirror_commands['generic']['actions'][
                            'display_on'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['display_on'] + ' ' + \
                                         magic_mirror_messages['name']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/remote?action=MONITORON")
                    if magic_mirror_commands['generic']['actions'][
                            'display_off'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands']['generic']['display_off'] + ' ' + \
                                         magic_mirror_messages['name']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/remote?action=MONITOROFF")
                    if magic_mirror_commands['generic']['actions'][
                            'hide_all'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands'][
                            'generic']['hide_all']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/get?data=modules")
                        data = mmreq.json()
                        for module in data:
                            mmreq = requests.get(
                                "http://" + mmmip +
                                ":8080/remote?action=HIDE&module=" +
                                module['identifier'])
                    if magic_mirror_commands['generic']['actions'][
                            'show_all'].lower() == mmmcommand_accurate:
                        message_to_say = magic_mirror_messages['commands'][
                            'generic']['show_all']
                        say(message_to_say)
                        mmreq = requests.get("http://" + mmmip +
                                             ":8080/get?data=modules")
                        data = mmreq.json()
                        for module in data:
                            mmreq = requests.get(
                                "http://" + mmmip +
                                ":8080/remote?action=SHOW&module=" +
                                module['identifier'])
                else:
                    message_to_say = magic_mirror_messages['error'][
                        'no_command']
                    say(message_to_say)
        except requests.exceptions.ConnectionError:
            message_to_say = magic_mirror_messages[
                'name'] + ' ' + magic_mirror_messages['error']['not_online']
            say(message_to_say)
示例#17
0
def main():
    init_pubnub()
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials', type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('~/.config'),
                            'google-oauthlib-tool',
                            'credentials.json'
                        ),
                        help='Path to store and read OAuth2 credentials')
    parser.add_argument('--device_model_id', type=str,
                        metavar='DEVICE_MODEL_ID', required=True,
                        help='The device model ID registered with Google.')
    parser.add_argument(
        '--project_id',
        type=str,
        metavar='PROJECT_ID',
        required=False,
        help='The project ID used to register device instances.')
    parser.add_argument(
        '-v',
        '--version',
        action='version',
        version='%(prog)s ' +
        Assistant.__version_str__())

    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))
    with Assistant(credentials, args.device_model_id) as assistant:
        subprocess.Popen(["aplay", "/home/pi/GassistPi-IT/sample-audio-files/Startup.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        events = assistant.start()
        print('device_model_id:', args.device_model_id + '\n' +
              'device_id:', assistant.device_id + '\n')
        if args.project_id:
            register_device(args.project_id, credentials,
                            args.device_model_id, assistant.device_id)
        for event in events:
            process_event(event, assistant.device_id)
            usrcmd=event.args
            with open('/home/pi/GassistPi-IT/src/diyHue/config.json', 'r') as config:
                 hueconfig = json.load(config)
            for i in range(1,len(hueconfig['lights'])+1):
                try:
                    if str(hueconfig['lights'][str(i)]['name']).lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        hue_control(str(usrcmd).lower(),str(i),str(hueconfig['lights_address'][str(i)]['ip']))
                        break
                except Keyerror:
                    say('Unable to help, please check your config file')

            for num, name in enumerate(tasmota_devicelist):
                if name.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    tasmota_control(str(usrcmd).lower(), name.lower(),tasmota_deviceip[num])
                    break
            if 'lampadina'.lower() in str(usrcmd).lower():		
                assistant.stop_conversation()
                if 'accendi'.lower() in str(usrcmd).lower():
                    url = ''
                    r = requests.post(url, data="")
                    say("Lampadina Accesa")
                elif 'spegni'.lower() in str(usrcmd).lower():
                    url = ''
                    r = requests.post(url, data="")
                    say("Lampadina Spenta")        
            if 'magic mirror'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                try:
                    mmmcommand=str(usrcmd).lower()
                    if 'weather'.lower() in mmmcommand:
                        if 'show'.lower() in mmmcommand:
                            mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_2_currentweather")
                            mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_3_currentweather")
                        if 'hide'.lower() in mmmcommand:
                            mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_2_currentweather")
                            mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_3_currentweather")
                    if 'power off'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=SHUTDOWN")
                    if 'reboot'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=REBOOT")
                    if 'restart'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=RESTART")
                    if 'display on'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITORON")
                    if 'display off'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITOROFF")
                except requests.exceptions.ConnectionError:
                    say("Magic mirror not online")
            if 'ingredients'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ingrequest=str(usrcmd).lower()
                ingredientsidx=ingrequest.find('for')
                ingrequest=ingrequest[ingredientsidx:]
                ingrequest=ingrequest.replace('for',"",1)
                ingrequest=ingrequest.replace("'}","",1)
                ingrequest=ingrequest.strip()
                ingrequest=ingrequest.replace(" ","%20",1)
                getrecipe(ingrequest)
            if 'kickstarter'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kickstarter_tracker(str(usrcmd).lower())
            if 'trigger'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                Action(str(usrcmd).lower())
            if 'avvia'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi-IT/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi-IT/src/trackchange.py')
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi-IT/src/trackchange.py')
                        os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi-IT/src/trackchange.py')
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                else:
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi-IT/src/trackchange.py')
                        os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi-IT/src/trackchange.py')
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())

            if 'ferma'.lower() in str(usrcmd).lower():
                stop()
            if 'radio'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                radio(str(usrcmd).lower())
            if 'wireless'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ESP(str(usrcmd).lower())
            if 'parcel'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                track()
            if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                feed(str(usrcmd).lower())
            if 'on kodi'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kodiactions(str(usrcmd).lower())
            if 'chromecast'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if 'play'.lower() in str(usrcmd).lower():
                    chromecast_play_video(str(usrcmd).lower())
                else:
                    chromecast_control(usrcmd)
            if 'pause music'.lower() in str(usrcmd).lower() or 'resume music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if ismpvplaying():
                    if 'pause music'.lower() in str(usrcmd).lower():
                        playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", True]})+"' | socat - /tmp/mpvsocket")
                    elif 'resume music'.lower() in str(usrcmd).lower():
                        playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", False]})+"' | socat - /tmp/mpvsocket")
                else:
                    say("Sorry nothing is playing right now")
            if 'music volume'.lower() in str(usrcmd).lower():
                if ismpvplaying():
                    if 'set'.lower() in str(usrcmd).lower() or 'change'.lower() in str(usrcmd).lower():
                        if 'hundred'.lower() in str(usrcmd).lower() or 'maximum' in str(usrcmd).lower():
                            settingvollevel=100
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        elif 'zero'.lower() in str(usrcmd).lower() or 'minimum' in str(usrcmd).lower():
                            settingvollevel=0
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        else:
                            for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                with open('/home/pi/.mediavolume.json', 'w') as vol:
                                    json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                    elif 'increase'.lower() in str(usrcmd).lower() or 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.mediavolume.json"):
                            with open('/home/pi/.mediavolume.json', 'r') as vol:
                                oldvollevel = json.load(vol)
                                for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                    oldvollevel=int(oldvollevel)
                        else:
                            mpvgetvol=subprocess.Popen([("echo '"+json.dumps({ "command": ["get_property", "volume"]})+"' | socat - /tmp/mpvsocket")],shell=True, stdout=subprocess.PIPE)
                            output=mpvgetvol.communicate()[0]
                            for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                oldvollevel=int(oldvollevel)

                        if 'increase'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                    changevollevel=int(changevollevel)
                            else:
                                changevollevel=10
                            newvollevel= oldvollevel+ changevollevel
                            print(newvollevel)
                            if newvollevel>100:
                                settingvollevel==100
                            elif newvollevel<0:
                                settingvollevel==0
                            else:
                                settingvollevel=newvollevel
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        if 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                    changevollevel=int(changevollevel)
                            else:
                                changevollevel=10
                            newvollevel= oldvollevel - changevollevel
                            print(newvollevel)
                            if newvollevel>100:
                                settingvollevel==100
                            elif newvollevel<0:
                                settingvollevel==0
                            else:
                                settingvollevel=newvollevel
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                    else:
                        say("Sorry I could not help you")
                else:
                    say("Sorry nothing is playing right now")

            if 'refresh'.lower() in str(usrcmd).lower() and 'music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                refreshlists()
            if 'google music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi-IT/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi-IT/src/trackchange.py')
                    gmusicselect(str(usrcmd).lower())
                else:
                    gmusicselect(str(usrcmd).lower())
示例#18
0
    def assist(self):
        """Send a voice request to the Assistant and playback the response.

        Returns: True if conversation should continue.
        """
        continue_conversation = False
        device_actions_futures = []
        subprocess.Popen(
            ["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        self.conversation_stream.start_recording()
        #Uncomment the following after starting the Kodi
        #status=mutevolstatus()
        #vollevel=status[1]
        #with open('/home/pi/.volume.json', 'w') as f:
        #json.dump(vollevel, f)
        #kodi.Application.SetVolume({"volume": 0})
        GPIO.output(5, GPIO.HIGH)
        led.ChangeDutyCycle(100)
        if ismpvplaying():
            if os.path.isfile("/home/pi/.mediavolume.json"):
                mpvsetvol = os.system(
                    "echo '" +
                    json.dumps({"command": ["set_property", "volume", "10"]}) +
                    "' | socat - /tmp/mpvsocket")
            else:
                mpvgetvol = subprocess.Popen(
                    [("echo '" +
                      json.dumps({"command": ["get_property", "volume"]}) +
                      "' | socat - /tmp/mpvsocket")],
                    shell=True,
                    stdout=subprocess.PIPE)
                output = mpvgetvol.communicate()[0]
                for currntvol in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                        json.dump(currntvol, vol)
                mpvsetvol = os.system(
                    "echo '" +
                    json.dumps({"command": ["set_property", "volume", "10"]}) +
                    "' | socat - /tmp/mpvsocket")

        logging.info('Recording audio request.')

        def iter_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            self.conversation_stream.start_playback()

        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected')
                GPIO.output(5, GPIO.LOW)
                led.ChangeDutyCycle(0)
                self.conversation_stream.stop_recording()

            if resp.speech_results:
                logging.info(
                    'Transcript of user request: "%s".',
                    ' '.join(r.transcript for r in resp.speech_results))

                for r in resp.speech_results:
                    usercommand = str(r)

                if "stability: 1.0" in usercommand.lower():
                    usrcmd = str(usercommand).lower()
                    idx = usrcmd.find('stability')
                    usrcmd = usrcmd[:idx]
                    usrcmd = usrcmd.replace("stability", "", 1)
                    usrcmd = usrcmd.strip()
                    usrcmd = usrcmd.replace('transcript: "', '', 1)
                    usrcmd = usrcmd.replace('"', '', 1)
                    usrcmd = usrcmd.strip()
                    print(str(usrcmd))
                    if 'trigger'.lower() in str(usrcmd).lower():
                        Action(str(usrcmd).lower())
                        return continue_conversation
                    if 'stream'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile(
                                "/home/pi/GassistPi/src/trackchange.py"):
                            os.system(
                                'rm /home/pi/GassistPi/src/trackchange.py')
                            os.system(
                                'echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            os.system(
                                'echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        else:
                            os.system(
                                'echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            os.system(
                                'echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        return continue_conversation
                    if 'stop'.lower() in str(usrcmd).lower():
                        stop()
                        return continue_conversation
                    if 'radio'.lower() in str(usrcmd).lower():
                        radio(str(usrcmd).lower())
                        return continue_conversation
                    if 'wireless'.lower() in str(usrcmd).lower():
                        ESP(str(usrcmd).lower())
                        return continue_conversation
                    if 'parcel'.lower() in str(usrcmd).lower():
                        track()
                        return continue_conversation
                    if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower(
                    ) in str(usrcmd).lower() or 'quote'.lower() in str(
                            usrcmd).lower():
                        feed(str(usrcmd).lower())
                        return continue_conversation
                    if 'on kodi'.lower() in str(usrcmd).lower():
                        kodiactions(str(usrcmd).lower())
                        return continue_conversation
                    if 'chromecast'.lower() in str(usrcmd).lower():
                        if 'play'.lower() in str(usrcmd).lower():
                            chromecast_play_video(str(usrcmd).lower())
                        else:
                            chromecast_control(usrcmd)
                        return continue_conversation
                    if 'pause music'.lower() in str(usrcmd).lower(
                    ) or 'resume music'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'pause music'.lower() in str(usrcmd).lower():
                                playstatus = os.system("echo '" + json.dumps({
                                    "command": ["set_property", "pause", True]
                                }) + "' | socat - /tmp/mpvsocket")
                            elif 'resume music'.lower() in str(usrcmd).lower():
                                playstatus = os.system("echo '" + json.dumps({
                                    "command":
                                    ["set_property", "pause", False]
                                }) + "' | socat - /tmp/mpvsocket")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if 'music volume'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'set'.lower() in str(usrcmd).lower(
                            ) or 'change'.lower() in str(usrcmd).lower():
                                if 'hundred'.lower() in str(usrcmd).lower(
                                ) or 'maximum' in str(usrcmd).lower():
                                    settingvollevel = 100
                                    with open('/home/pi/.mediavolume.json',
                                              'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                elif 'zero'.lower() in str(usrcmd).lower(
                                ) or 'minimum' in str(usrcmd).lower():
                                    settingvollevel = 0
                                    with open('/home/pi/.mediavolume.json',
                                              'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                else:
                                    for settingvollevel in re.findall(
                                            r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                        with open('/home/pi/.mediavolume.json',
                                                  'w') as vol:
                                            json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                            elif 'increase'.lower() in str(usrcmd).lower(
                            ) or 'decrease'.lower() in str(usrcmd).lower(
                            ) or 'reduce'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        "/home/pi/.mediavolume.json"):
                                    with open('/home/pi/.mediavolume.json',
                                              'r') as vol:
                                        oldvollevel = json.load(vol)
                                        for oldvollevel in re.findall(
                                                r'\b\d+\b', str(oldvollevel)):
                                            oldvollevel = int(oldvollevel)
                                else:
                                    mpvgetvol = subprocess.Popen(
                                        [("echo '" + json.dumps({
                                            "command":
                                            ["get_property", "volume"]
                                        }) + "' | socat - /tmp/mpvsocket")],
                                        shell=True,
                                        stdout=subprocess.PIPE)
                                    output = mpvgetvol.communicate()[0]
                                    for oldvollevel in re.findall(
                                            r"[-+]?\d*\.\d+|\d+", str(output)):
                                        oldvollevel = int(oldvollevel)

                                if 'increase'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit()
                                           for char in str(usrcmd)):
                                        for changevollevel in re.findall(
                                                r'\b\d+\b', str(usrcmd)):
                                            changevollevel = int(
                                                changevollevel)
                                    else:
                                        changevollevel = 10
                                    newvollevel = oldvollevel + changevollevel
                                    print(newvollevel)
                                    if newvollevel > 100:
                                        settingvollevel == 100
                                    elif newvollevel < 0:
                                        settingvollevel == 0
                                    else:
                                        settingvollevel = newvollevel
                                    with open('/home/pi/.mediavolume.json',
                                              'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                if 'decrease'.lower() in str(usrcmd).lower(
                                ) or 'reduce'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit()
                                           for char in str(usrcmd)):
                                        for changevollevel in re.findall(
                                                r'\b\d+\b', str(usrcmd)):
                                            changevollevel = int(
                                                changevollevel)
                                    else:
                                        changevollevel = 10
                                    newvollevel = oldvollevel - changevollevel
                                    print(newvollevel)
                                    if newvollevel > 100:
                                        settingvollevel == 100
                                    elif newvollevel < 0:
                                        settingvollevel == 0
                                    else:
                                        settingvollevel = newvollevel
                                    with open('/home/pi/.mediavolume.json',
                                              'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                            else:
                                say("Sorry I could not help you")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation

                    if 'refresh'.lower() in str(usrcmd).lower(
                    ) and 'music'.lower() in str(usrcmd).lower():
                        refreshlists()
                        return continue_conversation
                    if 'google music'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile(
                                "/home/pi/GassistPi/src/trackchange.py"):
                            os.system(
                                'rm /home/pi/GassistPi/src/trackchange.py')
                            os.system(
                                'echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            if 'all the songs'.lower() in str(usrcmd).lower():
                                os.system(
                                    'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                                )
                                say("Playing all your songs")
                                play_songs()

                            if 'playlist'.lower() in str(usrcmd).lower():
                                if 'first'.lower() in str(usrcmd).lower(
                                ) or 'one'.lower() in str(usrcmd).lower(
                                ) or '1'.lower() in str(usrcmd).lower():
                                    os.system(
                                        'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                                    )
                                    say("Playing songs from your playlist")
                                    play_playlist(0)
                                else:
                                    say("Sorry I am unable to help")

                            if 'album'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        "/home/pi/.gmusicalbumplayer.json"):
                                    os.system(
                                        "rm /home/pi/.gmusicalbumplayer.json")

                                req = str(usrcmd).lower()
                                idx = (req).find('album')
                                album = req[idx:]
                                album = album.replace("'}", "", 1)
                                album = album.replace('album', '', 1)
                                if 'from'.lower() in req:
                                    album = album.replace('from', '', 1)
                                    album = album.replace(
                                        'google music', '', 1)
                                else:
                                    album = album.replace(
                                        'google music', '', 1)

                                album = album.strip()
                                print(album)
                                albumstr = ('"' + album + '"')
                                f = open(
                                    '/home/pi/GassistPi/src/trackchange.py',
                                    'a+')
                                f.write('play_album(' + albumstr + ')')
                                f.close()
                                say("Looking for songs from the album")
                                play_album(album)

                            if 'artist'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        "/home/pi/.gmusicartistplayer.json"):
                                    os.system(
                                        "rm /home/pi/.gmusicartistplayer.json")

                                req = str(usrcmd).lower()
                                idx = (req).find('artist')
                                artist = req[idx:]
                                artist = artist.replace("'}", "", 1)
                                artist = artist.replace('artist', '', 1)
                                if 'from'.lower() in req:
                                    artist = artist.replace('from', '', 1)
                                    artist = artist.replace(
                                        'google music', '', 1)
                                else:
                                    artist = artist.replace(
                                        'google music', '', 1)

                                artist = artist.strip()
                                print(artist)
                                artiststr = ('"' + artist + '"')
                                f = open(
                                    '/home/pi/GassistPi/src/trackchange.py',
                                    'a+')
                                f.write('play_artist(' + artiststr + ')')
                                f.close()
                                say("Looking for songs rendered by the artist")
                                play_artist(artist)
                        else:
                            os.system(
                                'echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            if 'all the songs'.lower() in str(usrcmd).lower():
                                os.system(
                                    'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                                )
                                say("Playing all your songs")
                                play_songs()

                            if 'playlist'.lower() in str(usrcmd).lower():
                                if 'first'.lower() in str(usrcmd).lower(
                                ) or 'one'.lower() in str(usrcmd).lower(
                                ) or '1'.lower() in str(usrcmd).lower():
                                    os.system(
                                        'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                                    )
                                    say("Playing songs from your playlist")
                                    play_playlist(0)
                                else:
                                    say("Sorry I am unable to help")

                            if 'album'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        "/home/pi/.gmusicalbumplayer.json"):
                                    os.system(
                                        "rm /home/pi/.gmusicalbumplayer.json")

                                req = str(usrcmd).lower()
                                idx = (req).find('album')
                                album = req[idx:]
                                album = album.replace("'}", "", 1)
                                album = album.replace('album', '', 1)
                                if 'from'.lower() in req:
                                    album = album.replace('from', '', 1)
                                    album = album.replace(
                                        'google music', '', 1)
                                else:
                                    album = album.replace(
                                        'google music', '', 1)

                                album = album.strip()
                                print(album)
                                albumstr = ('"' + album + '"')
                                f = open(
                                    '/home/pi/GassistPi/src/trackchange.py',
                                    'a+')
                                f.write('play_album(' + albumstr + ')')
                                f.close()
                                say("Looking for songs from the album")
                                play_album(album)

                            if 'artist'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        "/home/pi/.gmusicartistplayer.json"):
                                    os.system(
                                        "rm /home/pi/.gmusicartistplayer.json")

                                req = str(usrcmd).lower()
                                idx = (req).find('artist')
                                artist = req[idx:]
                                artist = artist.replace("'}", "", 1)
                                artist = artist.replace('artist', '', 1)
                                if 'from'.lower() in req:
                                    artist = artist.replace('from', '', 1)
                                    artist = artist.replace(
                                        'google music', '', 1)
                                else:
                                    artist = artist.replace(
                                        'google music', '', 1)

                                artist = artist.strip()
                                print(artist)
                                artiststr = ('"' + artist + '"')
                                f = open(
                                    '/home/pi/GassistPi/src/trackchange.py',
                                    'a+')
                                f.write('play_artist(' + artiststr + ')')
                                f.close()
                                say("Looking for songs rendered by the artist")
                                play_artist(artist)
                        return continue_conversation

                else:
                    continue
                GPIO.output(5, GPIO.LOW)
                GPIO.output(6, GPIO.HIGH)
                led.ChangeDutyCycle(50)
                logging.info('Playing assistant response.')
            if len(resp.audio_out.audio_data) > 0:
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                GPIO.output(6, GPIO.LOW)
                GPIO.output(5, GPIO.HIGH)
                led.ChangeDutyCycle(100)
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json)
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        GPIO.output(6, GPIO.LOW)
        GPIO.output(5, GPIO.LOW)
        led.ChangeDutyCycle(0)
        #Uncomment the following, after starting Kodi
        #with open('/home/pi/.volume.json', 'r') as f:
        #vollevel = json.load(f)
        #kodi.Application.SetVolume({"volume": vollevel})
        if ismpvplaying():
            if os.path.isfile("/home/pi/.mediavolume.json"):
                with open('/home/pi/.mediavolume.json', 'r') as vol:
                    oldvollevel = json.load(vol)
                print(oldvollevel)
                mpvsetvol = os.system("echo '" + json.dumps(
                    {"command": ["set_property", "volume",
                                 str(oldvollevel)]}) +
                                      "' | socat - /tmp/mpvsocket")
        self.conversation_stream.stop_playback()
        return continue_conversation
示例#19
0
        print(e)
        return False

esng = ESpeakNG()
esng.voice = 'korean'
esng.say("시스템 초기화 중입니다. 약 이십초 정도 걸립니다.")
i = 0
while i<20:
    print('Waiting for WIFI(',i+1,"s/20s)", sep='', end='\r')
    sleep(1)
    i += 1
try:
    if internet_on():
        from actions import say
        print("Loading online script...")
        say("인공지능 서버와 연결중입니다")
        from online import main
    else:
        esng.say("인공지능 서버와 연결할 수 없습니다. 오프라인 모드로 전환합니다.")
        print("Loading offline script...")
        from offline import main
except Exception as e:
    print(e)
    try:
        print("Error occured while importing. try to load offline script...")
        from offline import main
    except Exception as e:
        print(e)
        exit("Cannot import voice script")

main()
    def main(self):
        parser = argparse.ArgumentParser(
            formatter_class=argparse.RawTextHelpFormatter)
        parser.add_argument('--device-model-id',
                            '--device_model_id',
                            type=str,
                            metavar='DEVICE_MODEL_ID',
                            required=False,
                            help='the device model ID registered with Google')
        parser.add_argument('--project-id',
                            '--project_id',
                            type=str,
                            metavar='PROJECT_ID',
                            required=False,
                            help="the project ID used to register this device")
        parser.add_argument('--nickname',
                            type=str,
                            metavar='NICKNAME',
                            required=False,
                            help="the nickname used to register this device")
        parser.add_argument('--device-config',
                            '--device_config',
                            type=str,
                            metavar='DEVICE_CONFIG_FILE',
                            default="/data/client.json",
                            help="path to store and read OAuth2 credentials")
        parser.add_argument('--credentials',
                            type=existing_file,
                            metavar='OAUTH2_CREDENTIALS_FILE',
                            default="/data/cred.json",
                            help="path to store and read OAuth2 credentials")
        parser.add_argument(
            '--query',
            type=str,
            metavar='QUERY',
            help='query to send as soon as the Assistant starts')
        parser.add_argument('-v',
                            '--version',
                            action='version',
                            version='%(prog)s ' + Assistant.__version_str__())
        args = parser.parse_args()
        with open(args.credentials, 'r') as f:
            credentials = google.oauth2.credentials.Credentials(token=None,
                                                                **json.load(f))
        device_model_id = None
        last_device_id = None
        try:
            with open(args.device_config) as f:
                device_config = json.load(f)
                device_model_id = device_config['model_id']
                last_device_id = device_config.get('last_device_id', None)
        except FileNotFoundError:
            print("Device config file not found.")

        if not args.device_model_id and not device_model_id:
            raise Exception("Missing --device-model-id option")

        # Re-register if 'device_model_id" is given by the user and it differs from what we previously registered with.
        should_register = (args.device_model_id
                           and args.device_model_id != device_model_id)
        device_model_id = args.device_model_id or device_model_id

        with Assistant(credentials, device_model_id) as assistant:
            self.assistant = assistant
            if gender == 'Male':
                subprocess.Popen([
                    'aplay', "/resources/sample-audio-files/Startup-Male.wav"
                ],
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            else:
                subprocess.Popen([
                    'aplay', "/resources/sample-audio-files/Startup-Female.wav"
                ],
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            events = assistant.start()
            device_id = assistant.device_id
            print('device_model_id: ', device_model_id)
            print('device_id: ', device_id + '\n')

            # Re-register if 'device_id' is different from the last 'device_id':
            if should_register or (device_id != last_device_id):
                if args.project_id:
                    register_device(args.project_id, credentials,
                                    device_model_id, device_id, args.nickname)
                    pathlib.Path(os.path.dirname(
                        args.device_config)).mkdir(exist_ok=True)
                    with open(args.device_config, 'w') as f:
                        json.dump(
                            {
                                'last_device_id': device_id,
                                'model_id': device_model_id,
                            }, f)
                else:
                    print(WARNING_NOT_REGISTERED)
            for event in events:
                if event.type == EventType.ON_START_FINISHED and args.query:
                    assistant.send_text_query(args.query)
                self.process_event(event)
                usr_cmd = event.args
                if configuration['Conversation'][
                        'Conversation_Control'] == 'Enabled':
                    for i in range(1, num_ques + 1):
                        try:
                            if str(configuration['Conversation']['question'][i]
                                   [0]).lower() in str(usr_cmd).lower():
                                assistant.stop_conversation()
                                selected_ans = random.sample(
                                    configuration['Conversation']['answer'][i],
                                    1)
                                say(selected_ans[0])
                                break
                        except KeyError:
                            say('Please check if the number of questions matches the number of answers in your config file'
                                )
                if (custom_action_keyword['Keywords']['Magic_mirror'][0]
                    ).lower() in str(usr_cmd).lower():
                    assistant.stop_conversation()
                    try:
                        mmcommand = str(usr_cmd).lower()
                        if 'weather'.lower() in mmcommand:
                            if 'show'.lower() in mmcommand:
                                mmreq1 = requests.get(
                                    "http://" + mmip +
                                    ":8080/remote?action=SHOW&module=module_2_currentweather"
                                )
                                mmreq2 = requests.get(
                                    "http://" + mmip +
                                    ":8080/remote?action=SHOW&module=module_3_currentweather"
                                )
                            if 'hide'.lower() in mmcommand:
                                mmreq1 = requests.get(
                                    "http://" + mmip +
                                    ":8080/remote?action=HIDE&module=module_2_currentweather"
                                )
                                mmreq2 = requests.get(
                                    "http://" + mmip +
                                    ":8080/remote?action=HIDE&module=module_3_currentweather"
                                )
                        if 'power off'.lower() in mmcommand:
                            mmreq = requests.get(
                                "http://" + mmip +
                                ":8080/remote?/action=SHUTDOWN")
                        if 'reboot'.lower() in mmcommand:
                            mmreq = requests.get('http://' + mmip +
                                                 ":8080/remote?action=REBOOT")
                        if 'restart'.lower() in mmcommand:
                            mmreq = requests.get('http://' + mmip +
                                                 ":8080/remote?action=RESTART")
                        if 'display on'.lower() in mmcommand:
                            mmreq = requests.get(
                                'http://' + mmip +
                                ":8080/remote?action=MONITORON")
                        if 'display off'.lower() in mmcommand:
                            mmreq = requests.get(
                                'http://' + mmip +
                                ":8080/remote?action=MONITOROFF")
                    except requests.exceptions.ConnectionError:
                        say("Magic Mirror is offline")
                if configuration['Raspberrypi_GPIO_Control'][
                        'GPIO_Control'] == 'Enabled':
                    if (custom_action_keyword['Keywords']['Pi_GPIO_control'][0]
                        ).lower() in str(usr_cmd).lower():
                        assistant.stop_conversation()
                        Action(str(usr_cmd).lower())

        if custom_wakeword:
            self.detector.terminate()
示例#21
0
    def assist(self):
        """Send a voice request to the Assistant and playback the response.

        Returns: True if conversation should continue.
        """
        continue_conversation = False
        device_actions_futures = []
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.conversation_stream.start_recording()
        #Uncomment the following after starting the Kodi
        #status=mutevolstatus()
        #vollevel=status[1]
        #with open('/home/pi/.volume.json', 'w') as f:
               #json.dump(vollevel, f)
        #kodi.Application.SetVolume({"volume": 0})
        GPIO.output(5,GPIO.HIGH)
        led.ChangeDutyCycle(100)
        if ismpvplaying():
            if os.path.isfile("/home/pi/.mediavolume.json"):
                mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume","10"]})+"' | socat - /tmp/mpvsocket")
            else:
                mpvgetvol=subprocess.Popen([("echo '"+json.dumps({ "command": ["get_property", "volume"]})+"' | socat - /tmp/mpvsocket")],shell=True, stdout=subprocess.PIPE)
                output=mpvgetvol.communicate()[0]
                for currntvol in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                        json.dump(currntvol, vol)
                mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume","10"]})+"' | socat - /tmp/mpvsocket")

        logging.info('Recording audio request.')

        def iter_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            self.conversation_stream.start_playback()

        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected')
                GPIO.output(5,GPIO.LOW)
                led.ChangeDutyCycle(0)
                self.conversation_stream.stop_recording()

            if resp.speech_results:
                logging.info('Transcript of user request: "%s".',
                             ' '.join(r.transcript
                                      for r in resp.speech_results))

                for r in resp.speech_results:
                    usercommand=str(r)

                if "stability: 1.0" in usercommand.lower():
                    usrcmd=str(usercommand).lower()
                    idx=usrcmd.find('stability')
                    usrcmd=usrcmd[:idx]
                    usrcmd=usrcmd.replace("stability","",1)
                    usrcmd=usrcmd.strip()
                    usrcmd=usrcmd.replace('transcript: "','',1)
                    usrcmd=usrcmd.replace('"','',1)
                    usrcmd=usrcmd.strip()
                    print(str(usrcmd))
                    if 'trigger'.lower() in str(usrcmd).lower():
                        Action(str(usrcmd).lower())
                        return continue_conversation
                    if 'stream'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                            os.system('rm /home/pi/GassistPi/src/trackchange.py')
                            os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                            os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py')
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        else:
                            os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                            os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py')
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        return continue_conversation
                    if 'stop'.lower() in str(usrcmd).lower():
                        stop()
                        return continue_conversation
                    if 'radio'.lower() in str(usrcmd).lower():
                        radio(str(usrcmd).lower())
                        return continue_conversation
                    if 'wireless'.lower() in str(usrcmd).lower():
                        ESP(str(usrcmd).lower())
                        return continue_conversation
                    if 'parcel'.lower() in str(usrcmd).lower():
                        track()
                        return continue_conversation
                    if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                        feed(str(usrcmd).lower())
                        return continue_conversation
                    if 'on kodi'.lower() in str(usrcmd).lower():
                        kodiactions(str(usrcmd).lower())
                        return continue_conversation
                    if 'chromecast'.lower() in str(usrcmd).lower():
                        if 'play'.lower() in str(usrcmd).lower():
                            chromecast_play_video(str(usrcmd).lower())
                        else:
                            chromecast_control(usrcmd)
                        return continue_conversation
                    if 'pause music'.lower() in str(usrcmd).lower() or 'resume music'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'pause music'.lower() in str(usrcmd).lower():
                                playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", True]})+"' | socat - /tmp/mpvsocket")
                            elif 'resume music'.lower() in str(usrcmd).lower():
                                playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", False]})+"' | socat - /tmp/mpvsocket")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if 'music volume'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'set'.lower() in str(usrcmd).lower() or 'change'.lower() in str(usrcmd).lower():
                                if 'hundred'.lower() in str(usrcmd).lower() or 'maximum' in str(usrcmd).lower():
                                    settingvollevel=100
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                                elif 'zero'.lower() in str(usrcmd).lower() or 'minimum' in str(usrcmd).lower():
                                    settingvollevel=0
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                                else:
                                    for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                        with open('/home/pi/.mediavolume.json', 'w') as vol:
                                            json.dump(settingvollevel, vol)
                                    mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                            elif 'increase'.lower() in str(usrcmd).lower() or 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.mediavolume.json"):
                                    with open('/home/pi/.mediavolume.json', 'r') as vol:
                                        oldvollevel = json.load(vol)
                                        for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                            oldvollevel=int(oldvollevel)
                                else:
                                    mpvgetvol=subprocess.Popen([("echo '"+json.dumps({ "command": ["get_property", "volume"]})+"' | socat - /tmp/mpvsocket")],shell=True, stdout=subprocess.PIPE)
                                    output=mpvgetvol.communicate()[0]
                                    for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                        oldvollevel=int(oldvollevel)

                                if 'increase'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel+ changevollevel
                                    print(newvollevel)
                                    if newvollevel>100:
                                        settingvollevel==100
                                    elif newvollevel<0:
                                        settingvollevel==0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                                if 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel - changevollevel
                                    print(newvollevel)
                                    if newvollevel>100:
                                        settingvollevel==100
                                    elif newvollevel<0:
                                        settingvollevel==0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                            else:
                                say("Sorry I could not help you")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation

                    if 'refresh'.lower() in str(usrcmd).lower() and 'music'.lower() in str(usrcmd).lower():
                        refreshlists()
                        return continue_conversation
                    if 'google music'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                            os.system('rm /home/pi/GassistPi/src/trackchange.py')
                            os.system('echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                            if 'all the songs'.lower() in str(usrcmd).lower():
                                os.system('echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py')
                                say("Playing all your songs")
                                play_songs()

                            if 'playlist'.lower() in str(usrcmd).lower():
                                if 'first'.lower() in str(usrcmd).lower() or 'one'.lower() in str(usrcmd).lower()  or '1'.lower() in str(usrcmd).lower():
                                    os.system('echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py')
                                    say("Playing songs from your playlist")
                                    play_playlist(0)
                                else:
                                    say("Sorry I am unable to help")

                            if 'album'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                                    os.system("rm /home/pi/.gmusicalbumplayer.json")

                                req=str(usrcmd).lower()
                                idx=(req).find('album')
                                album=req[idx:]
                                album=album.replace("'}", "",1)
                                album = album.replace('album','',1)
                                if 'from'.lower() in req:
                                    album = album.replace('from','',1)
                                    album = album.replace('google music','',1)
                                else:
                                    album = album.replace('google music','',1)

                                album=album.strip()
                                print(album)
                                albumstr=('"'+album+'"')
                                f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                                f.write('play_album('+albumstr+')')
                                f.close()
                                say("Looking for songs from the album")
                                play_album(album)

                            if 'artist'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                                    os.system("rm /home/pi/.gmusicartistplayer.json")

                                req=str(usrcmd).lower()
                                idx=(req).find('artist')
                                artist=req[idx:]
                                artist=artist.replace("'}", "",1)
                                artist = artist.replace('artist','',1)
                                if 'from'.lower() in req:
                                    artist = artist.replace('from','',1)
                                    artist = artist.replace('google music','',1)
                                else:
                                    artist = artist.replace('google music','',1)

                                artist=artist.strip()
                                print(artist)
                                artiststr=('"'+artist+'"')
                                f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                                f.write('play_artist('+artiststr+')')
                                f.close()
                                say("Looking for songs rendered by the artist")
                                play_artist(artist)
                        else:
                            os.system('echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                            if 'all the songs'.lower() in str(usrcmd).lower():
                                os.system('echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py')
                                say("Playing all your songs")
                                play_songs()

                            if 'playlist'.lower() in str(usrcmd).lower():
                                if 'first'.lower() in str(usrcmd).lower() or 'one'.lower() in str(usrcmd).lower()  or '1'.lower() in str(usrcmd).lower():
                                    os.system('echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py')
                                    say("Playing songs from your playlist")
                                    play_playlist(0)
                                else:
                                    say("Sorry I am unable to help")

                            if 'album'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                                    os.system("rm /home/pi/.gmusicalbumplayer.json")

                                req=str(usrcmd).lower()
                                idx=(req).find('album')
                                album=req[idx:]
                                album=album.replace("'}", "",1)
                                album = album.replace('album','',1)
                                if 'from'.lower() in req:
                                    album = album.replace('from','',1)
                                    album = album.replace('google music','',1)
                                else:
                                    album = album.replace('google music','',1)

                                album=album.strip()
                                print(album)
                                albumstr=('"'+album+'"')
                                f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                                f.write('play_album('+albumstr+')')
                                f.close()
                                say("Looking for songs from the album")
                                play_album(album)

                            if 'artist'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                                    os.system("rm /home/pi/.gmusicartistplayer.json")

                                req=str(usrcmd).lower()
                                idx=(req).find('artist')
                                artist=req[idx:]
                                artist=artist.replace("'}", "",1)
                                artist = artist.replace('artist','',1)
                                if 'from'.lower() in req:
                                    artist = artist.replace('from','',1)
                                    artist = artist.replace('google music','',1)
                                else:
                                    artist = artist.replace('google music','',1)

                                artist=artist.strip()
                                print(artist)
                                artiststr=('"'+artist+'"')
                                f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                                f.write('play_artist('+artiststr+')')
                                f.close()
                                say("Looking for songs rendered by the artist")
                                play_artist(artist)
                        return continue_conversation

                else:
                    continue
                GPIO.output(5,GPIO.LOW)
                GPIO.output(6,GPIO.HIGH)
                led.ChangeDutyCycle(50)
                logging.info('Playing assistant response.')
            if len(resp.audio_out.audio_data) > 0:
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                GPIO.output(6,GPIO.LOW)
                GPIO.output(5,GPIO.HIGH)
                led.ChangeDutyCycle(100)
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json
                )
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        GPIO.output(6,GPIO.LOW)
        GPIO.output(5,GPIO.LOW)
        led.ChangeDutyCycle(0)
        #Uncomment the following, after starting Kodi
        #with open('/home/pi/.volume.json', 'r') as f:
               #vollevel = json.load(f)
               #kodi.Application.SetVolume({"volume": vollevel})
        if ismpvplaying():
            if os.path.isfile("/home/pi/.mediavolume.json"):
                with open('/home/pi/.mediavolume.json', 'r') as vol:
                    oldvollevel = json.load(vol)
                print(oldvollevel)
                mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(oldvollevel)]})+"' | socat - /tmp/mpvsocket")
        self.conversation_stream.stop_playback()
        return continue_conversation
示例#22
0
    def assist(self):
        """Send a voice request to the Assistant and playback the response.

        Returns: True if conversation should continue.
        """
        continue_conversation = False
        device_actions_futures = []
        play_audio_file(resources['fb'])
        self.conversation_stream.start_recording()
        #Uncomment the following after starting the Kodi
        #status=mutevolstatus()
        #vollevel=status[1]
        #with open(os.path.expanduser('~/.volume.json'), 'w') as f:
        #json.dump(vollevel, f)
        #kodi.Application.SetVolume({"volume": 0})
        if GPIO != None:
            GPIO.output(5, GPIO.HIGH)
            led.ChangeDutyCycle(100)
        if ismpvplaying():
            if os.path.isfile(os.path.expanduser("~/.mediavolume.json")):
                mpvsetvol = os.system(
                    "echo '" +
                    json.dumps({"command": ["set_property", "volume", "10"]}) +
                    "' | socat - /tmp/mpvsocket")
            else:
                mpvgetvol = subprocess.Popen(
                    [("echo '" +
                      json.dumps({"command": ["get_property", "volume"]}) +
                      "' | socat - /tmp/mpvsocket")],
                    shell=True,
                    stdout=subprocess.PIPE)
                output = mpvgetvol.communicate()[0]
                for currntvol in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                    with open(os.path.expanduser('~/.mediavolume.json'),
                              'w') as vol:
                        json.dump(currntvol, vol)
                mpvsetvol = os.system(
                    "echo '" +
                    json.dumps({"command": ["set_property", "volume", "10"]}) +
                    "' | socat - /tmp/mpvsocket")

        logging.info('Recording audio request.')

        def iter_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            self.conversation_stream.start_playback()

        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected')
                if GPIO != None:
                    GPIO.output(5, GPIO.LOW)
                    led.ChangeDutyCycle(0)
                self.conversation_stream.stop_recording()
                print('Full Speech Result ' + str(resp.speech_results))
            if resp.speech_results:
                logging.info(
                    'Transcript of user request: "%s".',
                    ' '.join(r.transcript for r in resp.speech_results))

                for r in resp.speech_results:
                    usercommand = str(r)

                if "stability: 1.0" in usercommand.lower():
                    usrcmd = str(usercommand).lower()
                    idx = usrcmd.find('stability')
                    usrcmd = usrcmd[:idx]
                    usrcmd = usrcmd.replace("stability", "", 1)
                    usrcmd = usrcmd.strip()
                    usrcmd = usrcmd.replace('transcript: "', '', 1)
                    usrcmd = usrcmd.replace('"', '', 1)
                    usrcmd = usrcmd.strip()
                    for item in tasmota_devicelist:
                        if item['friendly-name'] in str(usrcmd).lower():
                            tasmota_control(str(usrcmd).lower(), item)
                            return continue_conversation
                            break
                    with open('{}/src/diyHue/config.json'.format(ROOT_PATH),
                              'r') as config:
                        hueconfig = json.load(config)
                    for i in range(1, len(hueconfig['lights']) + 1):
                        try:
                            if str(hueconfig['lights'][str(i)]
                                   ['name']).lower() in str(usrcmd).lower():
                                assistant.stop_conversation()
                                hue_control(
                                    str(usrcmd).lower(), str(i),
                                    str(hueconfig['lights_address'][str(i)]
                                        ['ip']))
                                break
                        except Keyerror:
                            say('Unable to help, please check your config file'
                                )

                    if 'magic mirror'.lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        try:
                            mmmcommand = str(usrcmd).lower()
                            if 'weather'.lower() in mmmcommand:
                                if 'show'.lower() in mmmcommand:
                                    mmreq_one = requests.get(
                                        "http://" + mmmip +
                                        ":8080/remote?action=SHOW&module=module_2_currentweather"
                                    )
                                    mmreq_two = requests.get(
                                        "http://" + mmmip +
                                        ":8080/remote?action=SHOW&module=module_3_currentweather"
                                    )
                                if 'hide'.lower() in mmmcommand:
                                    mmreq_one = requests.get(
                                        "http://" + mmmip +
                                        ":8080/remote?action=HIDE&module=module_2_currentweather"
                                    )
                                    mmreq_two = requests.get(
                                        "http://" + mmmip +
                                        ":8080/remote?action=HIDE&module=module_3_currentweather"
                                    )
                            if 'power off'.lower() in mmmcommand:
                                mmreq = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=SHUTDOWN")
                            if 'reboot'.lower() in mmmcommand:
                                mmreq = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=REBOOT")
                            if 'restart'.lower() in mmmcommand:
                                mmreq = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=RESTART")
                            if 'display on'.lower() in mmmcommand:
                                mmreq = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=MONITORON")
                            if 'display off'.lower() in mmmcommand:
                                mmreq = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=MONITOROFF")
                        except requests.exceptions.ConnectionError:
                            say("Magic mirror not online")
                    if 'ingredients'.lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        ingrequest = str(usrcmd).lower()
                        ingredientsidx = ingrequest.find('for')
                        ingrequest = ingrequest[ingredientsidx:]
                        ingrequest = ingrequest.replace('for', "", 1)
                        ingrequest = ingrequest.replace("'}", "", 1)
                        ingrequest = ingrequest.strip()
                        ingrequest = ingrequest.replace(" ", "%20", 1)
                        getrecipe(ingrequest)
                    if 'kickstarter'.lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        kickstarter_tracker(str(usrcmd).lower())
                    if 'trigger'.lower() in str(usrcmd).lower():
                        Action(str(usrcmd).lower())
                        return continue_conversation
                    if 'stream'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile(
                                "{}/src/trackchange.py".format(ROOT_PATH)):
                            os.system(
                                'rm {}/src/trackchange.py'.format(ROOT_PATH))
                            os.system(
                                'echo "from actions import youtubeplayer\n\n" >> {}/src/trackchange.py'
                                .format(ROOT_PATH))
                            os.system(
                                'echo "youtubeplayer()\n" >> {}/src/trackchange.py'
                                .format(ROOT_PATH))
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        else:
                            os.system(
                                'echo "from actions import youtubeplayer\n\n" >> {}/src/trackchange.py'
                                .format(ROOT_PATH))
                            os.system(
                                'echo "youtubeplayer()\n" >> {}/src/trackchange.py'
                                .format(ROOT_PATH))
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                        return continue_conversation
                    if 'stop'.lower() in str(usrcmd).lower():
                        stop()
                        return continue_conversation
                    if 'radio'.lower() in str(usrcmd).lower():
                        radio(str(usrcmd).lower())
                        return continue_conversation
                    if 'wireless'.lower() in str(usrcmd).lower():
                        ESP(str(usrcmd).lower())
                        return continue_conversation
                    if 'parcel'.lower() in str(usrcmd).lower():
                        track()
                        return continue_conversation
                    if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower(
                    ) in str(usrcmd).lower() or 'quote'.lower() in str(
                            usrcmd).lower():
                        feed(str(usrcmd).lower())
                        return continue_conversation
                    if 'on kodi'.lower() in str(usrcmd).lower():
                        kodiactions(str(usrcmd).lower())
                        return continue_conversation
                    if 'chromecast'.lower() in str(usrcmd).lower():
                        if 'play'.lower() in str(usrcmd).lower():
                            chromecast_play_video(str(usrcmd).lower())
                        else:
                            chromecast_control(usrcmd)
                        return continue_conversation
                    if 'pause music'.lower() in str(usrcmd).lower(
                    ) or 'resume music'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'pause music'.lower() in str(usrcmd).lower():
                                playstatus = os.system("echo '" + json.dumps({
                                    "command": ["set_property", "pause", True]
                                }) + "' | socat - /tmp/mpvsocket")
                            elif 'resume music'.lower() in str(usrcmd).lower():
                                playstatus = os.system("echo '" + json.dumps({
                                    "command":
                                    ["set_property", "pause", False]
                                }) + "' | socat - /tmp/mpvsocket")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if 'music volume'.lower() in str(usrcmd).lower():
                        if ismpvplaying():
                            if 'set'.lower() in str(usrcmd).lower(
                            ) or 'change'.lower() in str(usrcmd).lower():
                                if 'hundred'.lower() in str(usrcmd).lower(
                                ) or 'maximum' in str(usrcmd).lower():
                                    settingvollevel = 100
                                    with open(
                                            os.path.expanduser(
                                                '~/.mediavolume.json'),
                                            'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                elif 'zero'.lower() in str(usrcmd).lower(
                                ) or 'minimum' in str(usrcmd).lower():
                                    settingvollevel = 0
                                    with open(
                                            os.path.expanduser(
                                                '~/.mediavolume.json'),
                                            'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                else:
                                    for settingvollevel in re.findall(
                                            r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                        with open(
                                                os.path.expanduser(
                                                    '~/.mediavolume.json'),
                                                'w') as vol:
                                            json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                            elif 'increase'.lower() in str(usrcmd).lower(
                            ) or 'decrease'.lower() in str(usrcmd).lower(
                            ) or 'reduce'.lower() in str(usrcmd).lower():
                                if os.path.isfile(
                                        os.path.expanduser(
                                            "~/.mediavolume.json")):
                                    with open(
                                            os.path.expanduser(
                                                '~/.mediavolume.json'),
                                            'r') as vol:
                                        oldvollevel = json.load(vol)
                                        for oldvollevel in re.findall(
                                                r'\b\d+\b', str(oldvollevel)):
                                            oldvollevel = int(oldvollevel)
                                else:
                                    mpvgetvol = subprocess.Popen(
                                        [("echo '" + json.dumps({
                                            "command":
                                            ["get_property", "volume"]
                                        }) + "' | socat - /tmp/mpvsocket")],
                                        shell=True,
                                        stdout=subprocess.PIPE)
                                    output = mpvgetvol.communicate()[0]
                                    for oldvollevel in re.findall(
                                            r"[-+]?\d*\.\d+|\d+", str(output)):
                                        oldvollevel = int(oldvollevel)

                                if 'increase'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit()
                                           for char in str(usrcmd)):
                                        for changevollevel in re.findall(
                                                r'\b\d+\b', str(usrcmd)):
                                            changevollevel = int(
                                                changevollevel)
                                    else:
                                        changevollevel = 10
                                    newvollevel = oldvollevel + changevollevel
                                    print(newvollevel)
                                    if newvollevel > 100:
                                        settingvollevel == 100
                                    elif newvollevel < 0:
                                        settingvollevel == 0
                                    else:
                                        settingvollevel = newvollevel
                                    with open(
                                            os.path.expanduser(
                                                '~/.mediavolume.json'),
                                            'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                                if 'decrease'.lower() in str(usrcmd).lower(
                                ) or 'reduce'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit()
                                           for char in str(usrcmd)):
                                        for changevollevel in re.findall(
                                                r'\b\d+\b', str(usrcmd)):
                                            changevollevel = int(
                                                changevollevel)
                                    else:
                                        changevollevel = 10
                                    newvollevel = oldvollevel - changevollevel
                                    print(newvollevel)
                                    if newvollevel > 100:
                                        settingvollevel == 100
                                    elif newvollevel < 0:
                                        settingvollevel == 0
                                    else:
                                        settingvollevel = newvollevel
                                    with open(
                                            os.path.expanduser(
                                                '~/.mediavolume.json'),
                                            'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    mpvsetvol = os.system(
                                        "echo '" + json.dumps({
                                            "command": [
                                                "set_property", "volume",
                                                str(settingvollevel)
                                            ]
                                        }) + "' | socat - /tmp/mpvsocket")
                            else:
                                say("Sorry I could not help you")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation

                    if 'refresh'.lower() in str(usrcmd).lower(
                    ) and 'music'.lower() in str(usrcmd).lower():
                        refreshlists()
                        return continue_conversation
                    if 'google music'.lower() in str(usrcmd).lower():
                        os.system('pkill mpv')
                        if os.path.isfile(
                                "{}/src/trackchange.py".format(ROOT_PATH)):
                            os.system(
                                'rm {}/src/trackchange.py'.format(ROOT_PATH))

                            gmusicselect(str(usrcmd).lower())
                        else:

                            gmusicselect(str(usrcmd).lower())
                        return continue_conversation

                    else:
                        continue
                if GPIO != None:
                    GPIO.output(5, GPIO.LOW)
                    GPIO.output(6, GPIO.HIGH)
                    led.ChangeDutyCycle(50)
                logging.info('Playing assistant response.')
            if len(resp.audio_out.audio_data) > 0:
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                if GPIO != None:
                    GPIO.output(6, GPIO.LOW)
                    GPIO.output(5, GPIO.HIGH)
                    led.ChangeDutyCycle(100)
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:

                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json)
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        self.conversation_stream.stop_playback()
        return continue_conversation
示例#23
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--device-model-id', '--device_model_id', type=str,
                        metavar='DEVICE_MODEL_ID', required=False,
                        help='the device model ID registered with Google')
    parser.add_argument('--project-id', '--project_id', type=str,
                        metavar='PROJECT_ID', required=False,
                        help='the project ID used to register this device')
    parser.add_argument('--device-config', type=str,
                        metavar='DEVICE_CONFIG_FILE',
                        default=os.path.join(
                            os.path.expanduser('~/.config'),
                            'googlesamples-assistant',
                            'device_config_library.json'
                        ),
                        help='path to store and read device configuration')
    parser.add_argument('--credentials', type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('~/.config'),
                            'google-oauthlib-tool',
                            'credentials.json'
                        ),
                        help='path to store and read OAuth2 credentials')
    parser.add_argument('-v', '--version', action='version',
                        version='%(prog)s ' + Assistant.__version_str__())

    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))

    device_model_id = None
    last_device_id = None
    try:
        with open(args.device_config) as f:
            device_config = json.load(f)
            device_model_id = device_config['model_id']
            last_device_id = device_config.get('last_device_id', None)
    except FileNotFoundError:
        pass

    if not args.device_model_id and not device_model_id:
        raise Exception('Missing --device-model-id option')

    # Re-register if "device_model_id" is given by the user and it differs
    # from what we previously registered with.
    should_register = (
        args.device_model_id and args.device_model_id != device_model_id)

    device_model_id = args.device_model_id or device_model_id
    with Assistant(credentials, device_model_id) as assistant:
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        events = assistant.start()

        device_id = assistant.device_id
        print('device_model_id:', device_model_id)
        print('device_id:', device_id + '\n')

        # Re-register if "device_id" is different from the last "device_id":
        if should_register or (device_id != last_device_id):
            if args.project_id:
                register_device(args.project_id, credentials,
                                device_model_id, device_id)
                pathlib.Path(os.path.dirname(args.device_config)).mkdir(
                    exist_ok=True)
                with open(args.device_config, 'w') as f:
                    json.dump({
                        'last_device_id': device_id,
                        'model_id': device_model_id,
                    }, f)
            else:
                print(WARNING_NOT_REGISTERED)

        for event in events:
            process_event(event)
            usrcmd=event.args
            with open('/home/pi/GassistPi/src/diyHue/config.json', 'r') as config:
                 hueconfig = json.load(config)
            for i in range(1,len(hueconfig['lights'])+1):
                try:
                    if str(hueconfig['lights'][str(i)]['name']).lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        hue_control(str(usrcmd).lower(),str(i),str(hueconfig['lights_address'][str(i)]['ip']))
                        break
                except KeyError:
                    say('Unable to help, please check your config file')

            for num, name in enumerate(tasmota_devicelist):
                if name.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    tasmota_control(str(usrcmd).lower(), name.lower(),tasmota_deviceip[num])
                    break
            if 'magic mirror'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                try:
                    mmmcommand=str(usrcmd).lower()
                    if 'weather'.lower() in mmmcommand:
                        if 'show'.lower() in mmmcommand:
                            mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_2_currentweather")
                            mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_3_currentweather")
                        if 'hide'.lower() in mmmcommand:
                            mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_2_currentweather")
                            mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_3_currentweather")
                    if 'power off'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=SHUTDOWN")
                    if 'reboot'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=REBOOT")
                    if 'restart'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=RESTART")
                    if 'display on'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITORON")
                    if 'display off'.lower() in mmmcommand:
                        mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITOROFF")
                except requests.exceptions.ConnectionError:
                    say("Magic mirror not online")
            if 'ingredients'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ingrequest=str(usrcmd).lower()
                ingredientsidx=ingrequest.find('for')
                ingrequest=ingrequest[ingredientsidx:]
                ingrequest=ingrequest.replace('for',"",1)
                ingrequest=ingrequest.replace("'}","",1)
                ingrequest=ingrequest.strip()
                ingrequest=ingrequest.replace(" ","%20",1)
                getrecipe(ingrequest)
            if 'kickstarter'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kickstarter_tracker(str(usrcmd).lower())
            if 'trigger'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                Action(str(usrcmd).lower())
            if 'wireless'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ESP(str(usrcmd).lower())
            if 'parcel'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                track()
            if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                feed(str(usrcmd).lower())
            if 'update'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if 'magic mirror'.lower() in str(usrcmd).lower():
                    # update magic mirror also
                    pass
                say("Päivitetään", "fi")
                subprocess.Popen(["sudo systemctl stop gassistpi-ok-google && git -C /home/pi/GassistPi pull "
                                  "&& sudo systemctl start gassistpi-ok-google"], stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
示例#24
0
    def __weather_action(self, nlu_entities=None):

        current_dtime = datetime.datetime.now()
        skip_weather = False  # used if we decide that current weather is not important

        weather_obj = self.knowledge.find_weather()
        temperature = weather_obj['temperature']
        icon = weather_obj['icon']
        wind_speed = weather_obj['windSpeed']

        weather_speech = self.nlg.weather(temperature, current_dtime,
                                          "present")
        forecast_speech = None

        if nlu_entities is not None:
            if 'datetime' in nlu_entities:
                if 'grain' in nlu_entities['datetime'][0] and nlu_entities[
                        'datetime'][0]['grain'] == 'day':
                    dtime_str = nlu_entities['datetime'][0][
                        'value']  # 2016-09-26T00:00:00.000-07:00
                    dtime = dateutil.parser.parse(dtime_str)
                    if current_dtime.date() == dtime.date():  # hourly weather
                        forecast_obj = {
                            'forecast_type': 'hourly',
                            'forecast': weather_obj['daily_forecast']
                        }
                        forecast_speech = self.nlg.forecast(forecast_obj)
                    elif current_dtime.date() < dtime.date(
                    ):  # sometime in the future ... get the weekly forecast/ handle specific days
                        forecast_obj = {
                            'forecast_type': 'daily',
                            'forecast': weather_obj['weekly_forecast']
                        }
                        forecast_speech = self.nlg.forecast(forecast_obj)
                        skip_weather = True
            if 'Weather_Type' in nlu_entities:
                weather_type = nlu_entities['Weather_Type'][0]['value']
                print(weather_type)
                if weather_type == "current":
                    forecast_obj = {
                        'forecast_type': 'current',
                        'forecast': weather_obj['current_forecast']
                    }
                    forecast_speech = self.nlg.forecast(forecast_obj)
                elif weather_type == 'today':
                    forecast_obj = {
                        'forecast_type': 'hourly',
                        'forecast': weather_obj['daily_forecast']
                    }
                    forecast_speech = self.nlg.forecast(forecast_obj)
                elif weather_type == 'tomorrow' or weather_type == '3 day' or weather_type == '7 day':
                    forecast_obj = {
                        'forecast_type': 'daily',
                        'forecast': weather_obj['weekly_forecast']
                    }
                    forecast_speech = self.nlg.forecast(forecast_obj)
                    skip_weather = True

        weather_data = {
            "temperature": temperature,
            "icon": icon,
            'windSpeed': wind_speed,
            "hour": datetime.datetime.now().hour
        }
        requests.post("http://localhost:8080/weather",
                      data=json.dumps(weather_data))

        if not skip_weather:
            # self.speech.synthesize_text(weather_speech)
            say(weather_speech)

        if forecast_speech is not None:
            # self.speech.synthesize_text(forecast_speech)
            say(forecast_speech)
示例#25
0
 def __text_action(self, text=None):
     if text is not None:
         requests.get("http://localhost:8080/statement?text=%s" % text)
         # self.speech.synthesize_text(text)
         say(text, language)
示例#26
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials',
                        type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(os.path.expanduser('~/.config'),
                                             'google-oauthlib-tool',
                                             'credentials.json'),
                        help='Path to store and read OAuth2 credentials')
    parser.add_argument('--device_model_id',
                        type=str,
                        metavar='DEVICE_MODEL_ID',
                        required=True,
                        help='The device model ID registered with Google.')
    parser.add_argument('--project_id',
                        type=str,
                        metavar='PROJECT_ID',
                        required=False,
                        help='The project ID used to register device ' +
                        'instances.')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))
    with Assistant(credentials, args.device_model_id) as assistant:
        subprocess.Popen(
            ["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        events = assistant.start()
        print('device_model_id:', args.device_model_id + '\n' + 'device_id:',
              assistant.device_id + '\n')
        if args.project_id:
            register_device(args.project_id, credentials, args.device_model_id,
                            assistant.device_id)
        for event in events:
            process_event(event, assistant.device_id)
            usrcmd = event.args
            if 'trigger'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                Action(str(usrcmd).lower())
            if 'stream'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi/src/trackchange.py')
                    os.system(
                        'echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    os.system(
                        'echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                else:
                    os.system(
                        'echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    os.system(
                        'echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())

            if 'stop'.lower() in str(usrcmd).lower():
                stop()
            if 'radio'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                radio(str(usrcmd).lower())
            if 'wireless'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ESP(str(usrcmd).lower())
            if 'parcel'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                track()
            if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(
                    usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                feed(str(usrcmd).lower())
            if 'on kodi'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kodiactions(str(usrcmd).lower())
            if 'chromecast'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if 'play'.lower() in str(usrcmd).lower():
                    chromecast_play_video(str(usrcmd).lower())
                else:
                    chromecast_control(usrcmd)
            if 'pause music'.lower() in str(usrcmd).lower(
            ) or 'resume music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if ismpvplaying():
                    if 'pause music'.lower() in str(usrcmd).lower():
                        playstatus = os.system("echo '" + json.dumps(
                            {"command": ["set_property", "pause", True]}) +
                                               "' | socat - /tmp/mpvsocket")
                    elif 'resume music'.lower() in str(usrcmd).lower():
                        playstatus = os.system("echo '" + json.dumps(
                            {"command": ["set_property", "pause", False]}) +
                                               "' | socat - /tmp/mpvsocket")
                else:
                    say("Sorry nothing is playing right now")
            if 'music volume'.lower() in str(usrcmd).lower():
                if ismpvplaying():
                    if 'set'.lower() in str(usrcmd).lower() or 'change'.lower(
                    ) in str(usrcmd).lower():
                        if 'hundred'.lower() in str(usrcmd).lower(
                        ) or 'maximum' in str(usrcmd).lower():
                            settingvollevel = 100
                            with open('/home/pi/.mediavolume.json',
                                      'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol = os.system("echo '" + json.dumps({
                                "command": [
                                    "set_property", "volume",
                                    str(settingvollevel)
                                ]
                            }) + "' | socat - /tmp/mpvsocket")
                        elif 'zero'.lower() in str(usrcmd).lower(
                        ) or 'minimum' in str(usrcmd).lower():
                            settingvollevel = 0
                            with open('/home/pi/.mediavolume.json',
                                      'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol = os.system("echo '" + json.dumps({
                                "command": [
                                    "set_property", "volume",
                                    str(settingvollevel)
                                ]
                            }) + "' | socat - /tmp/mpvsocket")
                        else:
                            for settingvollevel in re.findall(
                                    r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                with open('/home/pi/.mediavolume.json',
                                          'w') as vol:
                                    json.dump(settingvollevel, vol)
                            mpvsetvol = os.system("echo '" + json.dumps({
                                "command": [
                                    "set_property", "volume",
                                    str(settingvollevel)
                                ]
                            }) + "' | socat - /tmp/mpvsocket")
                    elif 'increase'.lower() in str(usrcmd).lower(
                    ) or 'decrease'.lower() in str(usrcmd).lower(
                    ) or 'reduce'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.mediavolume.json"):
                            with open('/home/pi/.mediavolume.json',
                                      'r') as vol:
                                oldvollevel = json.load(vol)
                                for oldvollevel in re.findall(
                                        r'\b\d+\b', str(oldvollevel)):
                                    oldvollevel = int(oldvollevel)
                        else:
                            mpvgetvol = subprocess.Popen(
                                [("echo '" + json.dumps(
                                    {"command": ["get_property", "volume"]}) +
                                  "' | socat - /tmp/mpvsocket")],
                                shell=True,
                                stdout=subprocess.PIPE)
                            output = mpvgetvol.communicate()[0]
                            for oldvollevel in re.findall(
                                    r"[-+]?\d*\.\d+|\d+", str(output)):
                                oldvollevel = int(oldvollevel)

                        if 'increase'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(
                                        r'\b\d+\b', str(usrcmd)):
                                    changevollevel = int(changevollevel)
                            else:
                                changevollevel = 10
                            newvollevel = oldvollevel + changevollevel
                            print(newvollevel)
                            if newvollevel > 100:
                                settingvollevel == 100
                            elif newvollevel < 0:
                                settingvollevel == 0
                            else:
                                settingvollevel = newvollevel
                            with open('/home/pi/.mediavolume.json',
                                      'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol = os.system("echo '" + json.dumps({
                                "command": [
                                    "set_property", "volume",
                                    str(settingvollevel)
                                ]
                            }) + "' | socat - /tmp/mpvsocket")
                        if 'decrease'.lower() in str(usrcmd).lower(
                        ) or 'reduce'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(
                                        r'\b\d+\b', str(usrcmd)):
                                    changevollevel = int(changevollevel)
                            else:
                                changevollevel = 10
                            newvollevel = oldvollevel - changevollevel
                            print(newvollevel)
                            if newvollevel > 100:
                                settingvollevel == 100
                            elif newvollevel < 0:
                                settingvollevel == 0
                            else:
                                settingvollevel = newvollevel
                            with open('/home/pi/.mediavolume.json',
                                      'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol = os.system("echo '" + json.dumps({
                                "command": [
                                    "set_property", "volume",
                                    str(settingvollevel)
                                ]
                            }) + "' | socat - /tmp/mpvsocket")
                    else:
                        say("Sorry I could not help you")
                else:
                    say("Sorry nothing is playing right now")

            if 'refresh'.lower() in str(
                    usrcmd).lower() and 'music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                refreshlists()
            if 'google music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi/src/trackchange.py')
                    os.system(
                        'echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system(
                            'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                        )
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower(
                        ) or 'one'.lower() in str(usrcmd).lower() or '1'.lower(
                        ) in str(usrcmd).lower():
                            os.system(
                                'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('album')
                        album = req[idx:]
                        album = album.replace("'}", "", 1)
                        album = album.replace('album', '', 1)
                        if 'from'.lower() in req:
                            album = album.replace('from', '', 1)
                            album = album.replace('google music', '', 1)
                        else:
                            album = album.replace('google music', '', 1)

                        album = album.strip()
                        print(album)
                        albumstr = ('"' + album + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album(' + albumstr + ')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('artist')
                        artist = req[idx:]
                        artist = artist.replace("'}", "", 1)
                        artist = artist.replace('artist', '', 1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from', '', 1)
                            artist = artist.replace('google music', '', 1)
                        else:
                            artist = artist.replace('google music', '', 1)

                        artist = artist.strip()
                        print(artist)
                        artiststr = ('"' + artist + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist(' + artiststr + ')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)
                else:
                    os.system(
                        'echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system(
                            'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                        )
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower(
                        ) or 'one'.lower() in str(usrcmd).lower() or '1'.lower(
                        ) in str(usrcmd).lower():
                            os.system(
                                'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('album')
                        album = req[idx:]
                        album = album.replace("'}", "", 1)
                        album = album.replace('album', '', 1)
                        if 'from'.lower() in req:
                            album = album.replace('from', '', 1)
                            album = album.replace('google music', '', 1)
                        else:
                            album = album.replace('google music', '', 1)

                        album = album.strip()
                        print(album)
                        albumstr = ('"' + album + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album(' + albumstr + ')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('artist')
                        artist = req[idx:]
                        artist = artist.replace("'}", "", 1)
                        artist = artist.replace('artist', '', 1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from', '', 1)
                            artist = artist.replace('google music', '', 1)
                        else:
                            artist = artist.replace('google music', '', 1)

                        artist = artist.strip()
                        print(artist)
                        artiststr = ('"' + artist + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist(' + artiststr + ')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)
示例#27
0
    def main(self):
        parser = argparse.ArgumentParser(
            formatter_class=argparse.RawTextHelpFormatter)
        parser.add_argument('--device-model-id',
                            '--device_model_id',
                            type=str,
                            metavar='DEVICE_MODEL_ID',
                            required=False,
                            help='the device model ID registered with Google')
        parser.add_argument('--project-id',
                            '--project_id',
                            type=str,
                            metavar='PROJECT_ID',
                            required=False,
                            help='the project ID used to register this device')
        parser.add_argument('--device-config',
                            type=str,
                            metavar='DEVICE_CONFIG_FILE',
                            default=os.path.join(
                                os.path.expanduser('~/.config'),
                                'googlesamples-assistant',
                                'device_config_library.json'),
                            help='path to store and read device configuration')
        parser.add_argument('--credentials',
                            type=existing_file,
                            metavar='OAUTH2_CREDENTIALS_FILE',
                            default=os.path.join(
                                os.path.expanduser('~/.config'),
                                'google-oauthlib-tool', 'credentials.json'),
                            help='path to store and read OAuth2 credentials')
        parser.add_argument('-v',
                            '--version',
                            action='version',
                            version='%(prog)s ' + Assistant.__version_str__())

        args = parser.parse_args()
        with open(args.credentials, 'r') as f:
            credentials = google.oauth2.credentials.Credentials(token=None,
                                                                **json.load(f))

        device_model_id = None
        last_device_id = None
        try:
            with open(args.device_config) as f:
                device_config = json.load(f)
                device_model_id = device_config['model_id']
                last_device_id = device_config.get('last_device_id', None)
        except FileNotFoundError:
            pass

        if not args.device_model_id and not device_model_id:
            raise Exception('Missing --device-model-id option')

        # Re-register if "device_model_id" is given by the user and it differs
        # from what we previously registered with.
        should_register = (args.device_model_id
                           and args.device_model_id != device_model_id)

        device_model_id = args.device_model_id or device_model_id
        with Assistant(credentials, device_model_id) as assistant:
            self.assistant = assistant
            subprocess.Popen([
                "aplay", "{}/sample-audio-files/Startup.wav".format(ROOT_PATH)
            ],
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
            events = assistant.start()
            device_id = assistant.device_id
            print('device_model_id:', device_model_id)
            print('device_id:', device_id + '\n')

            # Re-register if "device_id" is different from the last "device_id":
            if should_register or (device_id != last_device_id):
                if args.project_id:
                    register_device(args.project_id, credentials,
                                    device_model_id, device_id)
                    pathlib.Path(os.path.dirname(
                        args.device_config)).mkdir(exist_ok=True)
                    with open(args.device_config, 'w') as f:
                        json.dump(
                            {
                                'last_device_id': device_id,
                                'model_id': device_model_id,
                            }, f)
                else:
                    print(WARNING_NOT_REGISTERED)

            for event in events:
                self.process_event(event)
                usrcmd = event.args
                with open('{}/src/diyHue/config.json'.format(ROOT_PATH),
                          'r') as config:
                    hueconfig = json.load(config)
                for i in range(1, len(hueconfig['lights']) + 1):
                    try:
                        if str(hueconfig['lights'][str(i)]
                               ['name']).lower() in str(usrcmd).lower():
                            assistant.stop_conversation()
                            hue_control(
                                str(usrcmd).lower(), str(i),
                                str(hueconfig['lights_address'][str(i)]['ip']))
                            break
                    except Keyerror:
                        say('Unable to help, please check your config file')

                for num, name in enumerate(tasmota_devicelist):
                    if name.lower() in str(usrcmd).lower():
                        assistant.stop_conversation()
                        tasmota_control(
                            str(usrcmd).lower(), name.lower(),
                            tasmota_deviceip[num], tasmota_deviceportid[num])
                        break
                for i in range(1, numques + 1):
                    try:
                        if str(configuration['Conversation']['question'][i]
                               [0]).lower() in str(usrcmd).lower():
                            assistant.stop_conversation()
                            selectedans = random.sample(
                                configuration['Conversation']['answer'][i], 1)
                            say(selectedans[0])
                            break
                    except Keyerror:
                        say('Please check if the number of questions matches the number of answers'
                            )

                if Domoticz_Device_Control == True and len(
                        domoticz_devices['result']) > 0:
                    for i in range(0, len(domoticz_devices['result'])):
                        if str(
                                domoticz_devices['result'][i]
                            ['HardwareName']).lower() in str(usrcmd).lower():
                            assistant.stop_conversation()
                            domoticz_control(
                                i,
                                str(usrcmd).lower(),
                                domoticz_devices['result'][i]['idx'],
                                domoticz_devices['result'][i]['HardwareName'])
                            break

                if (custom_action_keyword['Keywords']['Magic_mirror'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    try:
                        mmmcommand = str(usrcmd).lower()
                        if 'weather'.lower() in mmmcommand:
                            if 'show'.lower() in mmmcommand:
                                mmreq_one = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=SHOW&module=module_2_currentweather"
                                )
                                mmreq_two = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=SHOW&module=module_3_currentweather"
                                )
                            if 'hide'.lower() in mmmcommand:
                                mmreq_one = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=HIDE&module=module_2_currentweather"
                                )
                                mmreq_two = requests.get(
                                    "http://" + mmmip +
                                    ":8080/remote?action=HIDE&module=module_3_currentweather"
                                )
                        if 'power off'.lower() in mmmcommand:
                            mmreq = requests.get(
                                "http://" + mmmip +
                                ":8080/remote?action=SHUTDOWN")
                        if 'reboot'.lower() in mmmcommand:
                            mmreq = requests.get("http://" + mmmip +
                                                 ":8080/remote?action=REBOOT")
                        if 'restart'.lower() in mmmcommand:
                            mmreq = requests.get("http://" + mmmip +
                                                 ":8080/remote?action=RESTART")
                        if 'display on'.lower() in mmmcommand:
                            mmreq = requests.get(
                                "http://" + mmmip +
                                ":8080/remote?action=MONITORON")
                        if 'display off'.lower() in mmmcommand:
                            mmreq = requests.get(
                                "http://" + mmmip +
                                ":8080/remote?action=MONITOROFF")
                    except requests.exceptions.ConnectionError:
                        say("Magic mirror not online")
                if (custom_action_keyword['Keywords']['Recipe_pushbullet'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    ingrequest = str(usrcmd).lower()
                    ingredientsidx = ingrequest.find('for')
                    ingrequest = ingrequest[ingredientsidx:]
                    ingrequest = ingrequest.replace('for', "", 1)
                    ingrequest = ingrequest.replace("'}", "", 1)
                    ingrequest = ingrequest.strip()
                    ingrequest = ingrequest.replace(" ", "%20", 1)
                    getrecipe(ingrequest)
                if (custom_action_keyword['Keywords']['Kickstarter_tracking']
                    [0]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    kickstarter_tracker(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Pi_GPIO_control'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    Action(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['YouTube_music_stream']
                    [0]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Stop_music'][0]
                    ).lower() in str(usrcmd).lower():
                    stop()
                if 'radio'.lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    radio(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['ESP_control'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    ESP(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Parcel_tracking'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    track()
                if (custom_action_keyword['Keywords']['RSS'][0]
                    ).lower() in str(usrcmd).lower() or (
                        custom_action_keyword['Keywords']['RSS'][1]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    feed(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Kodi_actions'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    kodiactions(str(usrcmd).lower())
                # Google Assistant now comes built in with chromecast control, so custom function has been commented
                # if 'chromecast'.lower() in str(usrcmd).lower():
                #     assistant.stop_conversation()
                #     if 'play'.lower() in str(usrcmd).lower():
                #         chromecast_play_video(str(usrcmd).lower())
                #     else:
                #         chromecast_control(usrcmd)
                if (custom_action_keyword['Keywords']['Pause_resume'][0]
                    ).lower() in str(usrcmd).lower() or (
                        custom_action_keyword['Keywords']['Pause_resume'][1]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing():
                        if (custom_action_keyword['Keywords']['Pause_resume']
                            [0]).lower() in str(usrcmd).lower():
                            vlcplayer.pause_vlc()
                    if checkvlcpaused():
                        if (custom_action_keyword['Keywords']['Pause_resume']
                            [1]).lower() in str(usrcmd).lower():
                            vlcplayer.play_vlc()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if (custom_action_keyword['Keywords']['Track_change']['Next']
                    [0]).lower() in str(usrcmd).lower() or (
                        custom_action_keyword['Keywords']['Track_change']
                        ['Next'][1]).lower() in str(usrcmd).lower() or (
                            custom_action_keyword['Keywords']['Track_change']
                            ['Next'][2]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() or checkvlcpaused() == True:
                        vlcplayer.stop_vlc()
                        vlcplayer.change_media_next()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if (custom_action_keyword['Keywords']['Track_change']
                    ['Previous'][0]).lower() in str(usrcmd).lower() or (
                        custom_action_keyword['Keywords']['Track_change']
                        ['Previous'][1]).lower() in str(usrcmd).lower() or (
                            custom_action_keyword['Keywords']['Track_change']
                            ['Previous'][2]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() or checkvlcpaused() == True:
                        vlcplayer.stop_vlc()
                        vlcplayer.change_media_previous()
                    elif vlcplayer.is_vlc_playing(
                    ) == False and checkvlcpaused() == False:
                        say("Sorry nothing is playing right now")
                if (custom_action_keyword['Keywords']['VLC_music_volume'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    if vlcplayer.is_vlc_playing() == True or checkvlcpaused(
                    ) == True:
                        if 'set'.lower() in str(usrcmd).lower(
                        ) or 'change'.lower() in str(usrcmd).lower():
                            if 'hundred'.lower() in str(usrcmd).lower(
                            ) or 'maximum' in str(usrcmd).lower():
                                settingvollevel = 100
                                with open(
                                        '{}/.mediavolume.json'.format(
                                            USER_PATH), 'w') as vol:
                                    json.dump(settingvollevel, vol)
                            elif 'zero'.lower() in str(usrcmd).lower(
                            ) or 'minimum' in str(usrcmd).lower():
                                settingvollevel = 0
                                with open(
                                        '{}/.mediavolume.json'.format(
                                            USER_PATH), 'w') as vol:
                                    json.dump(settingvollevel, vol)
                            else:
                                for settingvollevel in re.findall(
                                        r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                    with open(
                                            '{}/.mediavolume.json'.format(
                                                USER_PATH), 'w') as vol:
                                        json.dump(settingvollevel, vol)
                            print('Setting volume to: ' + str(settingvollevel))
                            vlcplayer.set_vlc_volume(int(settingvollevel))
                        elif 'increase'.lower() in str(usrcmd).lower(
                        ) or 'decrease'.lower() in str(usrcmd).lower(
                        ) or 'reduce'.lower() in str(usrcmd).lower():
                            if os.path.isfile(
                                    "{}/.mediavolume.json".format(USER_PATH)):
                                with open(
                                        '{}/.mediavolume.json'.format(
                                            USER_PATH), 'r') as vol:
                                    oldvollevel = json.load(vol)
                                    for oldvollevel in re.findall(
                                            r'\b\d+\b', str(oldvollevel)):
                                        oldvollevel = int(oldvollevel)
                            else:
                                oldvollevel = vlcplayer.get_vlc_volume
                                for oldvollevel in re.findall(
                                        r"[-+]?\d*\.\d+|\d+", str(output)):
                                    oldvollevel = int(oldvollevel)
                            if 'increase'.lower() in str(usrcmd).lower():
                                if any(char.isdigit() for char in str(usrcmd)):
                                    for changevollevel in re.findall(
                                            r'\b\d+\b', str(usrcmd)):
                                        changevollevel = int(changevollevel)
                                else:
                                    changevollevel = 10
                                newvollevel = oldvollevel + changevollevel
                                print(newvollevel)
                                if int(newvollevel) > 100:
                                    settingvollevel == 100
                                elif int(newvollevel) < 0:
                                    settingvollevel == 0
                                else:
                                    settingvollevel = newvollevel
                                with open(
                                        '{}/.mediavolume.json'.format(
                                            USER_PATH), 'w') as vol:
                                    json.dump(settingvollevel, vol)
                                print('Setting volume to: ' +
                                      str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                            if 'decrease'.lower() in str(usrcmd).lower(
                            ) or 'reduce'.lower() in str(usrcmd).lower():
                                if any(char.isdigit() for char in str(usrcmd)):
                                    for changevollevel in re.findall(
                                            r'\b\d+\b', str(usrcmd)):
                                        changevollevel = int(changevollevel)
                                else:
                                    changevollevel = 10
                                newvollevel = oldvollevel - changevollevel
                                print(newvollevel)
                                if int(newvollevel) > 100:
                                    settingvollevel == 100
                                elif int(newvollevel) < 0:
                                    settingvollevel == 0
                                else:
                                    settingvollevel = newvollevel
                                with open(
                                        '{}/.mediavolume.json'.format(
                                            USER_PATH), 'w') as vol:
                                    json.dump(settingvollevel, vol)
                                print('Setting volume to: ' +
                                      str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                        else:
                            say("Sorry I could not help you")
                    else:
                        say("Sorry nothing is playing right now")
                if (custom_action_keyword['Keywords']['Music_index_refresh'][0]
                    ).lower() in str(usrcmd).lower() and (
                        custom_action_keyword['Keywords']
                        ['Music_index_refresh'][1]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    refreshlists()
                if (custom_action_keyword['Keywords']['Google_music_streaming']
                    [0]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    gmusicselect(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']
                    ['Spotify_music_streaming'][0]
                    ).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    spotify_playlist_select(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Gaana_music_streaming']
                    [0]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    gaana_playlist_select(str(usrcmd).lower())
                if (custom_action_keyword['Keywords']['Deezer_music_streaming']
                    [0]).lower() in str(usrcmd).lower():
                    assistant.stop_conversation()
                    vlcplayer.stop_vlc()
                    deezer_playlist_select(str(usrcmd).lower())

        if custom_wakeword:
            self.detector.terminate()
示例#28
0
文件: main.py 项目: vfciot/GassistPi
    def custom_command(self,usrcmd):
        if configuration['DIYHUE']['DIYHUE_Control']=='Enabled':
            if os.path.isfile('/opt/hue-emulator/config.json'):
                with open('/opt/hue-emulator/config.json', 'r') as config:
                     hueconfig = json.load(config)
                for i in range(1,len(hueconfig['lights'])+1):
                    try:
                        if str(hueconfig['lights'][str(i)]['name']).lower() in str(usrcmd).lower():
                            self.assistant.stop_conversation()
                            hue_control(str(usrcmd).lower(),str(i),str(hueconfig['lights_address'][str(i)]['ip']))
                            break
                    except Keyerror:
                        say('Unable to help, please check your config file')
        if configuration['Tasmota_devicelist']['Tasmota_Control']=='Enabled':
            for num, name in enumerate(tasmota_devicelist):
                if name.lower() in str(usrcmd).lower():
                    self.assistant.stop_conversation()
                    tasmota_control(str(usrcmd).lower(), name.lower(),tasmota_deviceip[num],tasmota_deviceportid[num])
        if configuration['Conversation']['Conversation_Control']=='Enabled':
            for i in range(1,numques+1):
                try:
                    if str(configuration['Conversation']['question'][i][0]).lower() in str(usrcmd).lower():
                        self.assistant.stop_conversation()
                        selectedans=random.sample(configuration['Conversation']['answer'][i],1)
                        say(selectedans[0])
                        break
                except Keyerror:
                    say('Please check if the number of questions matches the number of answers')

        if Domoticz_Device_Control==True and len(domoticz_devices['result'])>0:
            if len(configuration['Domoticz']['Devices']['Name'])==len(configuration['Domoticz']['Devices']['Id']):
                for i in range(0,len(configuration['Domoticz']['Devices']['Name'])):
                    if str(configuration['Domoticz']['Devices']['Name'][i]).lower() in str(usrcmd).lower():
                        self.assistant.stop_conversation()
                        domoticz_control(str(usrcmd).lower(),configuration['Domoticz']['Devices']['Id'][i],configuration['Domoticz']['Devices']['Name'][i])
                        break
            else:
                say("Number of devices and the number of ids given in config file do not match")

        if (custom_action_keyword['Keywords']['Magic_mirror'][0]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            try:
                mmmcommand=str(usrcmd).lower()
                if 'weather'.lower() in mmmcommand:
                    if 'show'.lower() in mmmcommand:
                        mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_2_currentweather")
                        mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_3_currentweather")
                    if 'hide'.lower() in mmmcommand:
                        mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_2_currentweather")
                        mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_3_currentweather")
                if 'power off'.lower() in mmmcommand:
                    mmreq=requests.get("http://"+mmmip+":8080/remote?action=SHUTDOWN")
                if 'reboot'.lower() in mmmcommand:
                    mmreq=requests.get("http://"+mmmip+":8080/remote?action=REBOOT")
                if 'restart'.lower() in mmmcommand:
                    mmreq=requests.get("http://"+mmmip+":8080/remote?action=RESTART")
                if 'display on'.lower() in mmmcommand:
                    mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITORON")
                if 'display off'.lower() in mmmcommand:
                    mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITOROFF")
            except requests.exceptions.ConnectionError:
                say("Magic mirror not online")
        if (custom_action_keyword['Keywords']['Recipe_pushbullet'][0]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            ingrequest=str(usrcmd).lower()
            ingredientsidx=ingrequest.find('for')
            ingrequest=ingrequest[ingredientsidx:]
            ingrequest=ingrequest.replace('for',"",1)
            ingrequest=ingrequest.replace("'}","",1)
            ingrequest=ingrequest.strip()
            ingrequest=ingrequest.replace(" ","%20",1)
            getrecipe(ingrequest)
        if configuration['Pushbullet']['Pushbullet_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Send_Message'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                say("What is your message?")
                self.voicenote_recording()
        if (custom_action_keyword['Keywords']['Kickstarter_tracking'][0]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            kickstarter_tracker(str(usrcmd).lower())
        if configuration['Raspberrypi_GPIO_Control']['GPIO_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Pi_GPIO_control'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                Action(str(usrcmd).lower())
        if configuration['YouTube']['YouTube_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['YouTube_music_stream'][0]).lower() in str(usrcmd).lower() and 'kodi' not in str(usrcmd).lower() and 'chromecast' not in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                if not Youtube_credentials:
                    say("Hey, you need to enter your google cloud api in the config file first.")
                else:
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
        if (custom_action_keyword['Keywords']['Stop_music'][0]).lower() in str(usrcmd).lower():
            stop()
        if configuration['Notify_TTS']['Notify_TTS_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['notify_TTS'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                notify_tts(str(usrcmd).lower())
        if configuration['Radio_stations']['Radio_Control']=='Enabled':
            if 'radio'.lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                radio(str(usrcmd).lower())
        if configuration['ESP']['ESP_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['ESP_control'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                ESP(str(usrcmd).lower())
        if (custom_action_keyword['Keywords']['Parcel_tracking'][0]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            track()
        if (custom_action_keyword['Keywords']['RSS'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['RSS'][1]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            feed(str(usrcmd).lower())
        if kodicontrol:
            try:
                if (custom_action_keyword['Keywords']['Kodi_actions'][0]).lower() in str(usrcmd).lower():
                    self.assistant.stop_conversation()
                    kodiactions(str(usrcmd).lower())
            except requests.exceptions.ConnectionError:
                say("Kodi TV box not online")
        # Google Assistant now comes built in with chromecast control, so custom function has been commented
        # if 'chromecast'.lower() in str(usrcmd).lower():
        #     self.assistant.stop_conversation()
        #     if 'play'.lower() in str(usrcmd).lower():
        #         chromecast_play_video(str(usrcmd).lower())
        #     else:
        #         chromecast_control(usrcmd)
        if (custom_action_keyword['Keywords']['Pause_resume'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Pause_resume'][1]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            if vlcplayer.is_vlc_playing():
                if (custom_action_keyword['Keywords']['Pause_resume'][0]).lower() in str(usrcmd).lower():
                    vlcplayer.pause_vlc()
            if checkvlcpaused():
                if (custom_action_keyword['Keywords']['Pause_resume'][1]).lower() in str(usrcmd).lower():
                    vlcplayer.play_vlc()
            elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                say("Sorry nothing is playing right now")
        if (custom_action_keyword['Keywords']['Track_change']['Next'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Next'][1]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Next'][2]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            if vlcplayer.is_vlc_playing() or checkvlcpaused()==True:
                vlcplayer.stop_vlc()
                vlcplayer.change_media_next()
            elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                say("Sorry nothing is playing right now")
        if (custom_action_keyword['Keywords']['Track_change']['Previous'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Previous'][1]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Previous'][2]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            if vlcplayer.is_vlc_playing() or checkvlcpaused()==True:
                vlcplayer.stop_vlc()
                vlcplayer.change_media_previous()
            elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                say("Sorry nothing is playing right now")
        if (custom_action_keyword['Keywords']['VLC_music_volume'][0]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            if vlcplayer.is_vlc_playing()==True or checkvlcpaused()==True:
                if (custom_action_keyword['Dict']['Set']).lower() in str(usrcmd).lower() or (custom_action_keyword['Dict']['Change']).lower() in str(usrcmd).lower():
                    if 'hundred'.lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Maximum'] in str(usrcmd).lower():
                        settingvollevel=100
                        with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                            json.dump(settingvollevel, vol)
                    elif 'zero'.lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Minimum'] in str(usrcmd).lower():
                        settingvollevel=0
                        with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                            json.dump(settingvollevel, vol)
                    else:
                        for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                            with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                json.dump(settingvollevel, vol)
                    print('Setting volume to: '+str(settingvollevel))
                    vlcplayer.set_vlc_volume(int(settingvollevel))
                elif (custom_action_keyword['Dict']['Increase']).lower() in str(usrcmd).lower() or (custom_action_keyword['Dict']['Decrease']).lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                    if os.path.isfile("{}/.mediavolume.json".format(USER_PATH)):
                        try:
                            with open('{}/.mediavolume.json'.format(USER_PATH), 'r') as vol:
                                oldvollevel = json.load(vol)
                                for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                    oldvollevel=int(oldvollevel)
                        except json.decoder.JSONDecodeError:
                            oldvollevel=vlcplayer.get_vlc_volume
                            for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                oldvollevel=int(oldvollevel)
                    else:
                        oldvollevel=vlcplayer.get_vlc_volume
                        for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                            oldvollevel=int(oldvollevel)
                    if (custom_action_keyword['Dict']['Increase']).lower() in str(usrcmd).lower():
                        if any(char.isdigit() for char in str(usrcmd)):
                            for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                changevollevel=int(changevollevel)
                        else:
                            changevollevel=10
                        newvollevel= oldvollevel+ changevollevel
                        print(newvollevel)
                        if int(newvollevel)>100:
                            settingvollevel=100
                        elif int(newvollevel)<0:
                            settingvollevel=0
                        else:
                            settingvollevel=newvollevel
                        with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                            json.dump(settingvollevel, vol)
                        print('Setting volume to: '+str(settingvollevel))
                        vlcplayer.set_vlc_volume(int(settingvollevel))
                    if (custom_action_keyword['Dict']['Decrease']).lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                        if any(char.isdigit() for char in str(usrcmd)):
                            for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                changevollevel=int(changevollevel)
                        else:
                            changevollevel=10
                        newvollevel= oldvollevel - changevollevel
                        print(newvollevel)
                        if int(newvollevel)>100:
                            settingvollevel=100
                        elif int(newvollevel)<0:
                            settingvollevel=0
                        else:
                            settingvollevel=newvollevel
                        with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                            json.dump(settingvollevel, vol)
                        print('Setting volume to: '+str(settingvollevel))
                        vlcplayer.set_vlc_volume(int(settingvollevel))
                else:
                    say("Sorry I could not help you")
            else:
                say("Sorry nothing is playing right now")
        if (custom_action_keyword['Keywords']['Music_index_refresh'][0]).lower() in str(usrcmd).lower() and (custom_action_keyword['Keywords']['Music_index_refresh'][1]).lower() in str(usrcmd).lower():
            self.assistant.stop_conversation()
            refreshlists()
        if configuration['Gmusicapi']['Gmusic_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Google_music_streaming'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                gmusicselect(str(usrcmd).lower())
        if configuration['Spotify']['Spotify_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Spotify_music_streaming'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                if not Spotify_credentials:
                    say("Hey, you need to enter your spotify credentials in the config file first.")
                else:
                    spotify_playlist_select(str(usrcmd).lower())
        if configuration['Gaana']['Gaana_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Gaana_music_streaming'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                gaana_playlist_select(str(usrcmd).lower())
        if configuration['Deezer']['Deezer_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Deezer_music_streaming'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                vlcplayer.stop_vlc()
                deezer_playlist_select(str(usrcmd).lower())
        if configuration['Clickatell']['Clickatell_Control']=='Enabled':
            if (custom_action_keyword['Keywords']['Send_sms_clickatell'][0]).lower() in str(usrcmd).lower():
                self.assistant.stop_conversation()
                sendSMS(str(usrcmd).lower())
        if 'interpreter' in str(usrcmd).lower():
            self.assistant.stop_conversation()
            reqlang=str(usrcmd).lower()
            reqlang=reqlang.replace('stop','',1)
            reqlang=reqlang.replace('start','',1)
            reqlang=reqlang.replace('interpreter','',1)
            reqlang=reqlang.strip()
            for i in range(0,len(langlist['Languages'])):
                if str(langlist['Languages'][i][i][0]).lower()==reqlang:
                    self.interpcloudlang2=langlist['Languages'][i][i][1]
                    self.interpttslang2=langlist['Languages'][i][i][2]
                    if 'start' in str(usrcmd).lower():
                        self.interpreter_mode_trigger('Start')
                    else:
                        self.interpreter_mode_trigger('Stop')
                    break
示例#29
0
    def assist(self):
        """Send a voice request to the Assistant and playback the response.

        Returns: True if conversation should continue.
        """
        continue_conversation = False
        device_actions_futures = []
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.conversation_stream.start_recording()
        #Uncomment the following after starting the Kodi
        #status=mutevolstatus()
        #vollevel=status[1]
        #with open('/home/pi/.volume.json', 'w') as f:
               #json.dump(vollevel, f)
        #kodi.Application.SetVolume({"volume": 0})
        GPIO.output(5,GPIO.HIGH)
        led.ChangeDutyCycle(100)
        if vlcplayer.is_vlc_playing():
            if os.path.isfile("/home/pi/.mediavolume.json"):
                vlcplayer.set_vlc_volume(15)
            else:
                currentvolume=vlcplayer.get_vlc_volume()
                print(currentvolume)
                with open('/home/pi/.mediavolume.json', 'w') as vol:
                   json.dump(currentvolume, vol)
                vlcplayer.set_vlc_volume(15)

        logging.info('Recording audio request.')

        def iter_log_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            logging.debug('Reached end of AssistRequest iteration.')

        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_log_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected.')
                logging.info('Stopping recording.')
                self.conversation_stream.stop_recording()
            if resp.speech_results:
                logging.info('Transcript of user request: "%s".',
                             ' '.join(r.transcript
                                      for r in resp.speech_results))
                for r in resp.speech_results:
                    usercommand=str(r)

                if "stability: 1.0" in usercommand.lower():
                    usrcmd=str(usercommand).lower()
                    idx=usrcmd.find('stability')
                    usrcmd=usrcmd[:idx]
                    usrcmd=usrcmd.replace("stability","",1)
                    usrcmd=usrcmd.strip()
                    usrcmd=usrcmd.replace('transcript: "','',1)
                    usrcmd=usrcmd.replace('"','',1)
                    usrcmd=usrcmd.strip()
                    print(str(usrcmd))
                    with open('/home/pi/GassistPi/src/diyHue/config.json', 'r') as config:
                         hueconfig = json.load(config)
                    for i in range(1,len(hueconfig['lights'])+1):
                        try:
                            if str(hueconfig['lights'][str(i)]['name']).lower() in str(usrcmd).lower():
                                hue_control(str(usrcmd).lower(),str(i),str(hueconfig['lights_address'][str(i)]['ip']))
                                return continue_conversation
                                break
                        except Keyerror:
                            say('Unable to help, please check your config file')
                    for num, name in enumerate(tasmota_devicelist):
                        if name.lower() in str(usrcmd).lower():
                            tasmota_control(str(usrcmd).lower(), name.lower(),tasmota_deviceip[num])
                            return continue_conversation
                            break
                    if 'magic mirror'.lower() in str(usrcmd).lower():
                        try:
                            mmmcommand=str(usrcmd).lower()
                            if 'weather'.lower() in mmmcommand:
                                if 'show'.lower() in mmmcommand:
                                    mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_2_currentweather")
                                    mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_3_currentweather")
                                if 'hide'.lower() in mmmcommand:
                                    mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_2_currentweather")
                                    mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_3_currentweather")
                            if 'power off'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=SHUTDOWN")
                            if 'reboot'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=REBOOT")
                            if 'restart'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=RESTART")
                            if 'display on'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITORON")
                            if 'display off'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITOROFF")
                        except requests.exceptions.ConnectionError:
                            say("Magic mirror not online")
                        return continue_conversation
                    if 'ingredients'.lower() in str(usrcmd).lower():
                        ingrequest=str(usrcmd).lower()
                        ingredientsidx=ingrequest.find('for')
                        ingrequest=ingrequest[ingredientsidx:]
                        ingrequest=ingrequest.replace('for',"",1)
                        ingrequest=ingrequest.replace("'}","",1)
                        ingrequest=ingrequest.strip()
                        ingrequest=ingrequest.replace(" ","%20",1)
                        getrecipe(ingrequest)
                        return continue_conversation
                    if 'kickstarter'.lower() in str(usrcmd).lower():
                        kickstarter_tracker(str(usrcmd).lower())
                        return continue_conversation
                    if 'trigger'.lower() in str(usrcmd).lower():
                        Action(str(usrcmd).lower())
                        return continue_conversation
                    if 'stream'.lower() in str(usrcmd).lower():
                        vlcplayer.stop_vlc()
                        if 'autoplay'.lower() in str(usrcmd).lower():
                            YouTube_Autoplay(str(usrcmd).lower())
                        else:
                            YouTube_No_Autoplay(str(usrcmd).lower())
                        return continue_conversation
                    if 'stop'.lower() in str(usrcmd).lower():
                        stop()
                    if 'radio'.lower() in str(usrcmd).lower():
                        radio(str(usrcmd).lower())
                        return continue_conversation
                    if 'wireless'.lower() in str(usrcmd).lower():
                        ESP(str(usrcmd).lower())
                        return continue_conversation
                    if 'parcel'.lower() in str(usrcmd).lower():
                        track()
                        return continue_conversation
                    if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                        feed(str(usrcmd).lower())
                        return continue_conversation
                    if 'on kodi'.lower() in str(usrcmd).lower():
                        kodiactions(str(usrcmd).lower())
                        return continue_conversation
                    # Google Assistant now comes built in with chromecast control, so custom function has been commented
                    # if 'chromecast'.lower() in str(usrcmd).lower():
                    #     if 'play'.lower() in str(usrcmd).lower():
                    #         chromecast_play_video(str(usrcmd).lower())
                    #     else:
                    #         chromecast_control(usrcmd)
                    #     return continue_conversation
                    if 'pause music'.lower() in str(usrcmd).lower() or 'resume music'.lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing():
                            if 'pause music'.lower() in str(usrcmd).lower():
                                vlcplayer.pause_vlc()
                        if checkvlcpaused():
                            if 'resume music'.lower() in str(usrcmd).lower():
                                vlcplayer.play_vlc()
                        elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if 'music volume'.lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing()==True or checkvlcpaused()==True:
                            if 'set'.lower() in str(usrcmd).lower() or 'change'.lower() in str(usrcmd).lower():
                                if 'hundred'.lower() in str(usrcmd).lower() or 'maximum' in str(usrcmd).lower():
                                    settingvollevel=100
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                elif 'zero'.lower() in str(usrcmd).lower() or 'minimum' in str(usrcmd).lower():
                                    settingvollevel=0
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                else:
                                    for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                        with open('/home/pi/.mediavolume.json', 'w') as vol:
                                            json.dump(settingvollevel, vol)
                                print('Setting volume to: '+str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                            elif 'increase'.lower() in str(usrcmd).lower() or 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                if os.path.isfile("/home/pi/.mediavolume.json"):
                                    with open('/home/pi/.mediavolume.json', 'r') as vol:
                                        oldvollevel = json.load(vol)
                                        for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                            oldvollevel=int(oldvollevel)
                                else:
                                    oldvollevel=vlcplayer.get_vlc_volume
                                    for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                        oldvollevel=int(oldvollevel)
                                if 'increase'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel+ changevollevel
                                    print(newvollevel)
                                    if int(newvollevel)>100:
                                        settingvollevel==100
                                    elif int(newvollevel)<0:
                                        settingvollevel==0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    print('Setting volume to: '+str(settingvollevel))
                                    vlcplayer.set_vlc_volume(int(settingvollevel))
                                if 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel - changevollevel
                                    print(newvollevel)
                                    if int(newvollevel)>100:
                                        settingvollevel==100
                                    elif int(newvollevel)<0:
                                        settingvollevel==0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('/home/pi/.mediavolume.json', 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    print('Setting volume to: '+str(settingvollevel))
                                    vlcplayer.set_vlc_volume(int(settingvollevel))
                            else:
                                say("Sorry I could not help you")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if 'refresh'.lower() in str(usrcmd).lower() and 'music'.lower() in str(usrcmd).lower():
                        refreshlists()
                        return continue_conversation
                    if 'google music'.lower() in str(usrcmd).lower():
                        vlcplayer.stop_vlc()
                        gmusicselect(str(usrcmd).lower())
                        return continue_conversation
                    if 'spotify'.lower() in str(usrcmd).lower():
                        vlcplayer.stop_vlc()
                        spotify_playlist_select(str(usrcmd).lower())
                        return continue_conversation
                    else:
                        continue
                GPIO.output(5,GPIO.LOW)
                GPIO.output(6,GPIO.HIGH)
                led.ChangeDutyCycle(50)

            if len(resp.audio_out.audio_data) > 0:
                if not self.conversation_stream.playing:
                    self.conversation_stream.stop_recording()
                    self.conversation_stream.start_playback()
                    logging.info('Playing assistant response.')
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                GPIO.output(6,GPIO.LOW)
                GPIO.output(5,GPIO.HIGH)
                led.ChangeDutyCycle(100)
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                GPIO.output(6,GPIO.LOW)
                GPIO.output(5,GPIO.LOW)
                led.ChangeDutyCycle(0)
                #Uncomment the following after starting the Kodi
                #with open('/home/pi/.volume.json', 'r') as f:
                       #vollevel = json.load(f)
                       #kodi.Application.SetVolume({"volume": vollevel})
                if vlcplayer.is_vlc_playing():
                    with open('/home/pi/.mediavolume.json', 'r') as vol:
                        oldvolume= json.load(vol)
                    vlcplayer.set_vlc_volume(int(oldvolume))
                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json
                )
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)
            if self.display and resp.screen_out.data:
                system_browser = browser_helpers.system_browser
                system_browser.display(resp.screen_out.data)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        self.conversation_stream.stop_playback()
        return continue_conversation
        GPIO.output(6,GPIO.LOW)
        GPIO.output(5,GPIO.LOW)
        led.ChangeDutyCycle(0)
        #Uncomment the following after starting the Kodi
        #with open('/home/pi/.volume.json', 'r') as f:
               #vollevel = json.load(f)
               #kodi.Application.SetVolume({"volume": vollevel})
        if vlcplayer.is_vlc_playing():
            with open('/home/pi/.mediavolume.json', 'r') as vol:
                oldvolume= json.load(vol)
            vlcplayer.set_vlc_volume(int(oldvolume))
示例#30
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials', type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('~/.config'),
                            'google-oauthlib-tool',
                            'credentials.json'
                        ),
                        help='Path to store and read OAuth2 credentials')
    parser.add_argument('--device_model_id', type=str,
                        metavar='DEVICE_MODEL_ID', required=True,
                        help='The device model ID registered with Google.')
    parser.add_argument('--project_id', type=str,
                        metavar='PROJECT_ID', required=False,
                        help='The project ID used to register device '
                        + 'instances.')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))
    with Assistant(credentials, args.device_model_id) as assistant:
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        events = assistant.start()
        print('device_model_id:', args.device_model_id + '\n' +
              'device_id:', assistant.device_id + '\n')
        if args.project_id:
            register_device(args.project_id, credentials,
                            args.device_model_id, assistant.device_id)
        for event in events:
            process_event(event, assistant.device_id)
            usrcmd=event.args
            if 'trigger'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                Action(str(usrcmd).lower())
            if 'stream'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi/src/trackchange.py')
                    os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                    os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py')
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                else:
                    os.system('echo "from actions import youtubeplayer\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                    os.system('echo "youtubeplayer()\n" >> /home/pi/GassistPi/src/trackchange.py')
                    if 'autoplay'.lower() in str(usrcmd).lower():
                        YouTube_Autoplay(str(usrcmd).lower())
                    else:
                        YouTube_No_Autoplay(str(usrcmd).lower())
                
            if 'stop'.lower() in str(usrcmd).lower():
                stop()
            if 'radio'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                radio(str(usrcmd).lower())
            if 'wireless'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ESP(str(usrcmd).lower())
            if 'parcel'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                track()
            if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                feed(str(usrcmd).lower())
            if 'on kodi'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kodiactions(str(usrcmd).lower())
            if 'chromecast'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if 'play'.lower() in str(usrcmd).lower():
                    chromecast_play_video(str(usrcmd).lower())
                else:
                    chromecast_control(usrcmd)
            if 'pause music'.lower() in str(usrcmd).lower() or 'resume music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if ismpvplaying():
                    if 'pause music'.lower() in str(usrcmd).lower():
                        playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", True]})+"' | socat - /tmp/mpvsocket")
                    elif 'resume music'.lower() in str(usrcmd).lower():
                        playstatus=os.system("echo '"+json.dumps({ "command": ["set_property", "pause", False]})+"' | socat - /tmp/mpvsocket")
                else:
                    say("Sorry nothing is playing right now")
            if 'music volume'.lower() in str(usrcmd).lower():
                if ismpvplaying():
                    if 'set'.lower() in str(usrcmd).lower() or 'change'.lower() in str(usrcmd).lower():
                        if 'hundred'.lower() in str(usrcmd).lower() or 'maximum' in str(usrcmd).lower():
                            settingvollevel=100
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        elif 'zero'.lower() in str(usrcmd).lower() or 'minimum' in str(usrcmd).lower():
                            settingvollevel=0
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        else:
                            for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                with open('/home/pi/.mediavolume.json', 'w') as vol:
                                    json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                    elif 'increase'.lower() in str(usrcmd).lower() or 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.mediavolume.json"):
                            with open('/home/pi/.mediavolume.json', 'r') as vol:
                                oldvollevel = json.load(vol)
                                for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                    oldvollevel=int(oldvollevel)
                        else:
                            mpvgetvol=subprocess.Popen([("echo '"+json.dumps({ "command": ["get_property", "volume"]})+"' | socat - /tmp/mpvsocket")],shell=True, stdout=subprocess.PIPE)
                            output=mpvgetvol.communicate()[0]
                            for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                oldvollevel=int(oldvollevel)

                        if 'increase'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                    changevollevel=int(changevollevel)
                            else:
                                changevollevel=10
                            newvollevel= oldvollevel+ changevollevel
                            print(newvollevel)
                            if newvollevel>100:
                                settingvollevel==100
                            elif newvollevel<0:
                                settingvollevel==0
                            else:
                                settingvollevel=newvollevel
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                        if 'decrease'.lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                            if any(char.isdigit() for char in str(usrcmd)):
                                for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                    changevollevel=int(changevollevel)
                            else:
                                changevollevel=10
                            newvollevel= oldvollevel - changevollevel
                            print(newvollevel)
                            if newvollevel>100:
                                settingvollevel==100
                            elif newvollevel<0:
                                settingvollevel==0
                            else:
                                settingvollevel=newvollevel
                            with open('/home/pi/.mediavolume.json', 'w') as vol:
                                json.dump(settingvollevel, vol)
                            mpvsetvol=os.system("echo '"+json.dumps({ "command": ["set_property", "volume",str(settingvollevel)]})+"' | socat - /tmp/mpvsocket")
                    else:
                        say("Sorry I could not help you")
                else:
                    say("Sorry nothing is playing right now")

            if 'refresh'.lower() in str(usrcmd).lower() and 'music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                refreshlists()
            if 'google music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi/src/trackchange.py')
                    os.system('echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system('echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py')
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower() or 'one'.lower() in str(usrcmd).lower()  or '1'.lower() in str(usrcmd).lower():
                            os.system('echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py')
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req=str(usrcmd).lower()
                        idx=(req).find('album')
                        album=req[idx:]
                        album=album.replace("'}", "",1)
                        album = album.replace('album','',1)
                        if 'from'.lower() in req:
                            album = album.replace('from','',1)
                            album = album.replace('google music','',1)
                        else:
                            album = album.replace('google music','',1)

                        album=album.strip()
                        print(album)
                        albumstr=('"'+album+'"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album('+albumstr+')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req=str(usrcmd).lower()
                        idx=(req).find('artist')
                        artist=req[idx:]
                        artist=artist.replace("'}", "",1)
                        artist = artist.replace('artist','',1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from','',1)
                            artist = artist.replace('google music','',1)
                        else:
                            artist = artist.replace('google music','',1)

                        artist=artist.strip()
                        print(artist)
                        artiststr=('"'+artist+'"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist('+artiststr+')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)
                else:
                    os.system('echo "from actions import play_playlist\nfrom actions import play_songs\nfrom actions import play_album\nfrom actions import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py')
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system('echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py')
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower() or 'one'.lower() in str(usrcmd).lower()  or '1'.lower() in str(usrcmd).lower():
                            os.system('echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py')
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req=str(usrcmd).lower()
                        idx=(req).find('album')
                        album=req[idx:]
                        album=album.replace("'}", "",1)
                        album = album.replace('album','',1)
                        if 'from'.lower() in req:
                            album = album.replace('from','',1)
                            album = album.replace('google music','',1)
                        else:
                            album = album.replace('google music','',1)

                        album=album.strip()
                        print(album)
                        albumstr=('"'+album+'"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album('+albumstr+')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req=str(usrcmd).lower()
                        idx=(req).find('artist')
                        artist=req[idx:]
                        artist=artist.replace("'}", "",1)
                        artist = artist.replace('artist','',1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from','',1)
                            artist = artist.replace('google music','',1)
                        else:
                            artist = artist.replace('google music','',1)

                        artist=artist.strip()
                        print(artist)
                        artiststr=('"'+artist+'"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist('+artiststr+')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)
示例#31
0
    def assist(self):
        """Send a voice request to the Assistant and playback the response.

        Returns: True if conversation should continue.
        """
        continue_conversation = False
        device_actions_futures = []
        subprocess.Popen(["aplay", "{}/sample-audio-files/Fb.wav".format(ROOT_PATH)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.conversation_stream.start_recording()
        if kodicontrol:
            try:
                status=mutevolstatus()
                vollevel=status[1]
                with open('{}/.volume.json'.format(USER_PATH), 'w') as f:
                       json.dump(vollevel, f)
                kodi.Application.SetVolume({"volume": 0})
            except requests.exceptions.ConnectionError:
                print("Kodi TV box not online")
        if GPIOcontrol:
            assistantindicator('listening')
        if vlcplayer.is_vlc_playing():
            if os.path.isfile("{}/.mediavolume.json".format(USER_PATH)):
                try:
                    with open('{}/.mediavolume.json'.format(USER_PATH), 'r') as vol:
                        volume = json.load(vol)
                    vlcplayer.set_vlc_volume(15)
                except json.decoder.JSONDecodeError:
                    currentvolume=vlcplayer.get_vlc_volume()
                    print(currentvolume)
                    with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                       json.dump(currentvolume, vol)
                    vlcplayer.set_vlc_volume(15)
            else:
                currentvolume=vlcplayer.get_vlc_volume()
                print(currentvolume)
                with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                   json.dump(currentvolume, vol)
                vlcplayer.set_vlc_volume(15)

        logging.info('Recording audio request.')

        def iter_log_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            logging.debug('Reached end of AssistRequest iteration.')

        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_log_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected.')
                logging.info('Stopping recording.')
                self.conversation_stream.stop_recording()
            if resp.speech_results:
                logging.info('Transcript of user request: "%s".',
                             ' '.join(r.transcript
                                      for r in resp.speech_results))
                for r in resp.speech_results:
                    usercommand=str(r)

                if "stability: 1.0" in usercommand.lower():
                    usrcmd=str(usercommand).lower()
                    idx=usrcmd.find('stability')
                    usrcmd=usrcmd[:idx]
                    usrcmd=usrcmd.replace("stability","",1)
                    usrcmd=usrcmd.strip()
                    usrcmd=usrcmd.replace('transcript: "','',1)
                    usrcmd=usrcmd.replace('"','',1)
                    usrcmd=usrcmd.strip()
                    print(str(usrcmd))
                    if configuration['DIYHUE']['DIYHUE_Control']=='Enabled':
                        if os.path.isfile('/opt/hue-emulator/config.json'):
                            with open('/opt/hue-emulator/config.json', 'r') as config:
                                 hueconfig = json.load(config)
                            for i in range(1,len(hueconfig['lights'])+1):
                                try:
                                    if str(hueconfig['lights'][str(i)]['name']).lower() in str(usrcmd).lower():
                                        hue_control(str(usrcmd).lower(),str(i),str(hueconfig['lights_address'][str(i)]['ip']))
                                        return continue_conversation
                                        break
                                except Keyerror:
                                    say('Unable to help, please check your config file')
                    if configuration['Tasmota_devicelist']['Tasmota_Control']=='Enabled':
                        for num, name in enumerate(tasmota_devicelist):
                            if name.lower() in str(usrcmd).lower():
                                tasmota_control(str(usrcmd).lower(), name.lower(),tasmota_deviceip[num])
                                return continue_conversation
                                break
                    if configuration['Conversation']['Conversation_Control']=='Enabled':
                        for i in range(1,numques+1):
                            try:
                                if str(configuration['Conversation']['question'][i][0]).lower() in str(usrcmd).lower():
                                    selectedans=random.sample(configuration['Conversation']['answer'][i],1)
                                    say(selectedans[0])
                                    return continue_conversation
                                    break
                            except Keyerror:
                                say('Please check if the number of questions matches the number of answers')
                    if Domoticz_Device_Control==True and len(domoticz_devices['result'])>0:
                        for i in range(0,len(domoticz_devices['result'])):
                            if str(domoticz_devices['result'][i]['HardwareName']).lower() in str(usrcmd).lower():
                                domoticz_control(i,str(usrcmd).lower(),domoticz_devices['result'][i]['idx'],domoticz_devices['result'][i]['HardwareName'])
                                return continue_conversation
                                break
                    if (custom_action_keyword['Keywords']['Magic_mirror'][0]).lower() in str(usrcmd).lower():
                        try:
                            mmmcommand=str(usrcmd).lower()
                            if 'weather'.lower() in mmmcommand:
                                if 'show'.lower() in mmmcommand:
                                    mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_2_currentweather")
                                    mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=SHOW&module=module_3_currentweather")
                                if 'hide'.lower() in mmmcommand:
                                    mmreq_one=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_2_currentweather")
                                    mmreq_two=requests.get("http://"+mmmip+":8080/remote?action=HIDE&module=module_3_currentweather")
                            if 'power off'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=SHUTDOWN")
                            if 'reboot'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=REBOOT")
                            if 'restart'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=RESTART")
                            if 'display on'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITORON")
                            if 'display off'.lower() in mmmcommand:
                                mmreq=requests.get("http://"+mmmip+":8080/remote?action=MONITOROFF")
                        except requests.exceptions.ConnectionError:
                            say("Magic mirror not online")
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['Recipe_pushbullet'][0]).lower() in str(usrcmd).lower():
                        ingrequest=str(usrcmd).lower()
                        ingredientsidx=ingrequest.find('for')
                        ingrequest=ingrequest[ingredientsidx:]
                        ingrequest=ingrequest.replace('for',"",1)
                        ingrequest=ingrequest.replace("'}","",1)
                        ingrequest=ingrequest.strip()
                        ingrequest=ingrequest.replace(" ","%20",1)
                        getrecipe(ingrequest)
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['Kickstarter_tracking'][0]).lower() in str(usrcmd).lower():
                        kickstarter_tracker(str(usrcmd).lower())
                        return continue_conversation
                    if configuration['Raspberrypi_GPIO_Control']['GPIO_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['Pi_GPIO_control'][0]).lower() in str(usrcmd).lower():
                            Action(str(usrcmd).lower())
                            return continue_conversation
                    if configuration['YouTube']['YouTube_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['YouTube_music_stream'][0]).lower() in str(usrcmd).lower():
                            vlcplayer.stop_vlc()
                            if 'autoplay'.lower() in str(usrcmd).lower():
                                YouTube_Autoplay(str(usrcmd).lower())
                            else:
                                YouTube_No_Autoplay(str(usrcmd).lower())
                            return continue_conversation
                    if (custom_action_keyword['Keywords']['Stop_music'][0]).lower() in str(usrcmd).lower():
                        stop()
                    if configuration['Radio_stations']['Radio_Control']=='Enabled':
                        if 'radio'.lower() in str(usrcmd).lower():
                            radio(str(usrcmd).lower())
                            return continue_conversation
                    if configuration['ESP']['ESP_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['ESP_control'][0]).lower() in str(usrcmd).lower():
                            ESP(str(usrcmd).lower())
                            return continue_conversation

                    if (custom_action_keyword['Keywords']['Parcel_tracking'][0]).lower() in str(usrcmd).lower():
                        track()
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['RSS'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['RSS'][1]).lower() in str(usrcmd).lower():
                        feed(str(usrcmd).lower())
                        return continue_conversation
                    if kodicontrol:
                        try:
                            if (custom_action_keyword['Keywords']['Kodi_actions'][0]).lower() in str(usrcmd).lower():
                                kodiactions(str(usrcmd).lower())
                        except requests.exceptions.ConnectionError:
                            say("Kodi TV box not online")
                        return continue_conversation
                    # Google Assistant now comes built in with chromecast control, so custom function has been commented
                    # if 'chromecast'.lower() in str(usrcmd).lower():
                    #     if 'play'.lower() in str(usrcmd).lower():
                    #         chromecast_play_video(str(usrcmd).lower())
                    #     else:
                    #         chromecast_control(usrcmd)
                    #     return continue_conversation
                    if (custom_action_keyword['Keywords']['Pause_resume'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Pause_resume'][1]).lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing():
                            if (custom_action_keyword['Keywords']['Pause_resume'][0]).lower() in str(usrcmd).lower():
                                vlcplayer.pause_vlc()
                        if checkvlcpaused():
                            if (custom_action_keyword['Keywords']['Pause_resume'][1]).lower() in str(usrcmd).lower():
                                vlcplayer.play_vlc()
                        elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['Track_change']['Next'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Next'][1]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Next'][2]).lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing() or checkvlcpaused()==True:
                            vlcplayer.stop_vlc()
                            vlcplayer.change_media_next()
                        elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['Track_change']['Previous'][0]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Previous'][1]).lower() in str(usrcmd).lower() or (custom_action_keyword['Keywords']['Track_change']['Previous'][2]).lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing() or checkvlcpaused()==True:
                            vlcplayer.stop_vlc()
                            vlcplayer.change_media_previous()
                        elif vlcplayer.is_vlc_playing()==False and checkvlcpaused()==False:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['VLC_music_volume'][0]).lower() in str(usrcmd).lower():
                        if vlcplayer.is_vlc_playing()==True or checkvlcpaused()==True:
                            if (custom_action_keyword['Dict']['Set']).lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Change'].lower() in str(usrcmd).lower():
                                if 'hundred'.lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Maximum'] in str(usrcmd).lower():
                                    settingvollevel=100
                                    with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                elif 'zero'.lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Minimum'] in str(usrcmd).lower():
                                    settingvollevel=0
                                    with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                else:
                                    for settingvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(usrcmd)):
                                        with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                            json.dump(settingvollevel, vol)
                                print('Setting volume to: '+str(settingvollevel))
                                vlcplayer.set_vlc_volume(int(settingvollevel))
                            elif custom_action_keyword['Dict']['Increase'].lower() in str(usrcmd).lower() or custom_action_keyword['Dict']['Decrease'].lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                if os.path.isfile("{}/.mediavolume.json".format(USER_PATH)):
                                    try:
                                        with open('{}/.mediavolume.json'.format(USER_PATH), 'r') as vol:
                                            oldvollevel = json.load(vol)
                                            for oldvollevel in re.findall(r'\b\d+\b', str(oldvollevel)):
                                                oldvollevel=int(oldvollevel)
                                    except json.decoder.JSONDecodeError:
                                        oldvollevel=vlcplayer.get_vlc_volume
                                        for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                            oldvollevel=int(oldvollevel)
                                else:
                                    oldvollevel=vlcplayer.get_vlc_volume
                                    for oldvollevel in re.findall(r"[-+]?\d*\.\d+|\d+", str(output)):
                                        oldvollevel=int(oldvollevel)
                                if custom_action_keyword['Dict']['Increase'].lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel+ changevollevel
                                    print(newvollevel)
                                    if int(newvollevel)>100:
                                        settingvollevel=100
                                    elif int(newvollevel)<0:
                                        settingvollevel=0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    print('Setting volume to: '+str(settingvollevel))
                                    vlcplayer.set_vlc_volume(int(settingvollevel))
                                if custom_action_keyword['Dict']['Decrease'].lower() in str(usrcmd).lower() or 'reduce'.lower() in str(usrcmd).lower():
                                    if any(char.isdigit() for char in str(usrcmd)):
                                        for changevollevel in re.findall(r'\b\d+\b', str(usrcmd)):
                                            changevollevel=int(changevollevel)
                                    else:
                                        changevollevel=10
                                    newvollevel= oldvollevel - changevollevel
                                    print(newvollevel)
                                    if int(newvollevel)>100:
                                        settingvollevel=100
                                    elif int(newvollevel)<0:
                                        settingvollevel=0
                                    else:
                                        settingvollevel=newvollevel
                                    with open('{}/.mediavolume.json'.format(USER_PATH), 'w') as vol:
                                        json.dump(settingvollevel, vol)
                                    print('Setting volume to: '+str(settingvollevel))
                                    vlcplayer.set_vlc_volume(int(settingvollevel))
                            else:
                                say("Sorry I could not help you")
                        else:
                            say("Sorry nothing is playing right now")
                        return continue_conversation
                    if (custom_action_keyword['Keywords']['Music_index_refresh'][0]).lower() in str(usrcmd).lower() and (custom_action_keyword['Keywords']['Music_index_refresh'][1]).lower() in str(usrcmd).lower():
                        refreshlists()
                        return continue_conversation
                    if configuration['Gmusicapi']['Gmusic_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['Google_music_streaming'][0]).lower() in str(usrcmd).lower():
                            vlcplayer.stop_vlc()
                            gmusicselect(str(usrcmd).lower())
                            return continue_conversation
                    if configuration['Spotify']['Spotify_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['Spotify_music_streaming'][0]).lower() in str(usrcmd).lower():
                            vlcplayer.stop_vlc()
                            spotify_playlist_select(str(usrcmd).lower())
                            return continue_conversation
                    if configuration['Gaana']['Gaana_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['Gaana_music_streaming'][0]).lower() in str(usrcmd).lower():
                            vlcplayer.stop_vlc()
                            gaana_playlist_select(str(usrcmd).lower())
                            return continue_conversation
                    if configuration['Deezer']['Deezer_Control']=='Enabled':
                        if (custom_action_keyword['Keywords']['Deezer_music_streaming'][0]).lower() in str(usrcmd).lower():
                            vlcplayer.stop_vlc()
                            deezer_playlist_select(str(usrcmd).lower())
                            return continue_conversation
                    else:
                        continue
                if GPIOcontrol:
                    assistantindicator('speaking')

            if len(resp.audio_out.audio_data) > 0:
                if not self.conversation_stream.playing:
                    self.conversation_stream.stop_recording()
                    self.conversation_stream.start_playback()
                    logging.info('Playing assistant response.')
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                if GPIOcontrol:
                    assistantindicator('listening')
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                if GPIOcontrol:
                    assistantindicator('off')
                if kodicontrol:
                    try:
                        with open('{}/.volume.json'.format(USER_PATH), 'r') as f:
                            vollevel = json.load(f)
                            kodi.Application.SetVolume({"volume": vollevel})
                    except requests.exceptions.ConnectionError:
                        print("Kodi TV box not online")

                if vlcplayer.is_vlc_playing():
                    with open('{}/.mediavolume.json'.format(USER_PATH), 'r') as vol:
                        oldvolume= json.load(vol)
                    vlcplayer.set_vlc_volume(int(oldvolume))
                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json
                )
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)
            if self.display and resp.screen_out.data:
                system_browser = browser_helpers.system_browser
                system_browser.display(resp.screen_out.data)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        self.conversation_stream.stop_playback()
        return continue_conversation
        if GPIOcontrol:
            assistantindicator('off')
        if kodicontrol:
            try:
                with open('{}/.volume.json'.format(USER_PATH), 'r') as f:
                    vollevel = json.load(f)
                    kodi.Application.SetVolume({"volume": vollevel})
            except requests.exceptions.ConnectionError:
                print("Kodi TV box not online")

        if vlcplayer.is_vlc_playing():
            with open('{}/.mediavolume.json'.format(USER_PATH), 'r') as vol:
                oldvolume= json.load(vol)
            vlcplayer.set_vlc_volume(int(oldvolume))
示例#32
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials',
                        type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(os.path.expanduser('~/.config'),
                                             'google-oauthlib-tool',
                                             'credentials.json'),
                        help='Path to store and read OAuth2 credentials')
    parser.add_argument('--device_model_id',
                        type=str,
                        metavar='DEVICE_MODEL_ID',
                        required=True,
                        help='The device model ID registered with Google.')
    parser.add_argument('--project_id',
                        type=str,
                        metavar='PROJECT_ID',
                        required=False,
                        help='The project ID used to register device ' +
                        'instances.')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))
    with Assistant(credentials, args.device_model_id) as assistant:
        subprocess.Popen(
            ["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        events = assistant.start()
        MY_CYCLE = colorschemes.TheaterChase(num_led=NUM_LED,
                                             pause_value=0.04,
                                             num_steps_per_cycle=15,
                                             num_cycles=4)
        MY_CYCLE.start()
        print('device_model_id:', args.device_model_id + '\n' + 'device_id:',
              assistant.device_id + '\n')
        if args.project_id:
            register_device(args.project_id, credentials, args.device_model_id,
                            assistant.device_id)
        for event in events:
            process_event(event, assistant.device_id)
            usrcmd = event.args
            if 'trigger'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                Action(str(usrcmd).lower())
            if 'stream'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                if 'autoplay'.lower() in str(usrcmd).lower():
                    os.system("mpsyt rmp mylist,exit")
                    YouTube_Autoplay(str(usrcmd).lower())
                else:
                    YouTube_No_Autoplay(str(usrcmd).lower())
            if 'stop'.lower() in str(usrcmd).lower():
                stop()
            if 'tune into'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                radio(str(usrcmd).lower())
            if 'wireless'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                ESP(str(usrcmd).lower())
            if 'parcel'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                track()
            if 'news'.lower() in str(usrcmd).lower() or 'feed'.lower() in str(
                    usrcmd).lower() or 'quote'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                feed(str(usrcmd).lower())
            if 'on kodi'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                kodiactions(str(usrcmd).lower())
            if 'google music'.lower() in str(usrcmd).lower():
                assistant.stop_conversation()
                os.system('pkill mpv')
                if os.path.isfile("/home/pi/GassistPi/src/trackchange.py"):
                    os.system('rm /home/pi/GassistPi/src/trackchange.py')
                    os.system(
                        'echo "from gmusic import play_playlist\nfrom gmusic import play_songs\nfrom gmusic import play_album\nfrom gmusic import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system(
                            'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                        )
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower(
                        ) or 'one'.lower() in str(usrcmd).lower() or '1'.lower(
                        ) in str(usrcmd).lower():
                            os.system(
                                'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('album')
                        album = req[idx:]
                        album = album.replace("'}", "", 1)
                        album = album.replace('album', '', 1)
                        if 'from'.lower() in req:
                            album = album.replace('from', '', 1)
                            album = album.replace('google music', '', 1)
                        else:
                            album = album.replace('google music', '', 1)

                        album = album.strip()
                        print(album)
                        albumstr = ('"' + album + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album(' + albumstr + ')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('artist')
                        artist = req[idx:]
                        artist = artist.replace("'}", "", 1)
                        artist = artist.replace('artist', '', 1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from', '', 1)
                            artist = artist.replace('google music', '', 1)
                        else:
                            artist = artist.replace('google music', '', 1)

                        artist = artist.strip()
                        print(artist)
                        artiststr = ('"' + artist + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist(' + artiststr + ')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)
                else:
                    os.system(
                        'echo "from gmusic import play_playlist\nfrom gmusic import play_songs\nfrom gmusic import play_album\nfrom gmusic import play_artist\n\n" >> /home/pi/GassistPi/src/trackchange.py'
                    )
                    if 'all the songs'.lower() in str(usrcmd).lower():
                        os.system(
                            'echo "play_songs()\n" >> /home/pi/GassistPi/src/trackchange.py'
                        )
                        say("Playing all your songs")
                        play_songs()

                    if 'playlist'.lower() in str(usrcmd).lower():
                        if 'first'.lower() in str(usrcmd).lower(
                        ) or 'one'.lower() in str(usrcmd).lower() or '1'.lower(
                        ) in str(usrcmd).lower():
                            os.system(
                                'echo "play_playlist(0)\n" >> /home/pi/GassistPi/src/trackchange.py'
                            )
                            say("Playing songs from your playlist")
                            play_playlist(0)
                        else:
                            say("Sorry I am unable to help")

                    if 'album'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicalbumplayer.json"):
                            os.system("rm /home/pi/.gmusicalbumplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('album')
                        album = req[idx:]
                        album = album.replace("'}", "", 1)
                        album = album.replace('album', '', 1)
                        if 'from'.lower() in req:
                            album = album.replace('from', '', 1)
                            album = album.replace('google music', '', 1)
                        else:
                            album = album.replace('google music', '', 1)

                        album = album.strip()
                        print(album)
                        albumstr = ('"' + album + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_album(' + albumstr + ')')
                        f.close()
                        say("Looking for songs from the album")
                        play_album(album)

                    if 'artist'.lower() in str(usrcmd).lower():
                        if os.path.isfile("/home/pi/.gmusicartistplayer.json"):
                            os.system("rm /home/pi/.gmusicartistplayer.json")

                        req = str(usrcmd).lower()
                        idx = (req).find('artist')
                        artist = req[idx:]
                        artist = artist.replace("'}", "", 1)
                        artist = artist.replace('artist', '', 1)
                        if 'from'.lower() in req:
                            artist = artist.replace('from', '', 1)
                            artist = artist.replace('google music', '', 1)
                        else:
                            artist = artist.replace('google music', '', 1)

                        artist = artist.strip()
                        print(artist)
                        artiststr = ('"' + artist + '"')
                        f = open('/home/pi/GassistPi/src/trackchange.py', 'a+')
                        f.write('play_artist(' + artiststr + ')')
                        f.close()
                        say("Looking for songs rendered by the artist")
                        play_artist(artist)