Exemplo n.º 1
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))
Exemplo n.º 2
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()
Exemplo n.º 3
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))
Exemplo n.º 4
0
    def process_event(self, event):
        """Pretty prints events.
        Prints all events that occur with two spaces between each new
        conversation and a single space between turns of a conversation.
        Args:
            event(event.Event): The current event to process.
        """
        print(event)
        if event.type == EventType.ON_START_FINISHED:
            self.can_start_conversation = True
            self.t2.start()
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                assistantindicator('mute')
            if (configuration['Wakewords']['Ok_Google'] == 'Disabled'
                    or os.path.isfile("{}/.mute".format(USER_PATH))):
                self.assistant.set_mic_mute(True)
            if custom_wakeword:
                self.t1.start()

        if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
            self.can_start_conversation = False
            subprocess.Popen(
                ["aplay", "{}/sample-audio-files/Fb.wav".format(ROOT_PATH)],
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
            #Uncomment the following after starting the Kodi
            #status=mutevolstatus()
            #vollevel=status[1]
            #with open('{}/.volume.json'.format(USER_PATH), 'w') as f:
            #json.dump(vollevel, f)
            #kodi.Application.SetVolume({"volume": 0})
            assistantindicator('listening')
            if vlcplayer.is_vlc_playing():
                if os.path.isfile("{}/.mediavolume.json".format(USER_PATH)):
                    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)
            print()

        if (event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT
                or event.type == EventType.ON_NO_RESPONSE):
            self.can_start_conversation = True
            assistantindicator('off')
            #Uncomment the following after starting the Kodi
            #with open('{}/.volume.json'.format(USER_PATH), 'r') as f:
            #vollevel = json.load(f)
            #kodi.Application.SetVolume({"volume": vollevel})
            if (configuration['Wakewords']['Ok_Google'] == 'Disabled'
                    or os.path.isfile("{}/.mute".format(USER_PATH))):
                self.assistant.set_mic_mute(True)
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                assistantindicator('mute')
            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))

        if (event.type == EventType.ON_RESPONDING_STARTED and event.args
                and not event.args['is_error_response']):
            assistantindicator('speaking')

        if event.type == EventType.ON_RESPONDING_FINISHED:
            assistantindicator('off')

        if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
            assistantindicator('off')

        print(event)

        if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED
                and event.args and not event.args['with_follow_on_turn']):
            self.can_start_conversation = True
            assistantindicator('off')
            if (configuration['Wakewords']['Ok_Google'] == 'Disabled'
                    or os.path.isfile("{}/.mute".format(USER_PATH))):
                self.assistant.set_mic_mute(True)
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                assistantindicator('mute')
            #Uncomment the following after starting the Kodi
            #with open('{}/.volume.json'.format(USER_PATH), 'r') as f:
            #vollevel = json.load(f)
            #kodi.Application.SetVolume({"volume": vollevel})
            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))
            print()

        if event.type == EventType.ON_DEVICE_ACTION:
            for command, params in event.actions:
                print('Do command', command, 'with params', str(params))
Exemplo n.º 5
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", "/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()
Exemplo n.º 6
0
def process_event(event):
    """Pretty prints events.
    Prints all events that occur with two spaces between each new
    conversation and a single space between turns of a conversation.
    Args:
        event(event.Event): The current event to process.
    """
    if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        #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)
        print()



    if event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT:
      GPIO.output(5,GPIO.LOW)
      GPIO.output(6,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))


    if (event.type == EventType.ON_RESPONDING_STARTED and event.args and not event.args['is_error_response']):
       GPIO.output(5,GPIO.LOW)
       GPIO.output(6,GPIO.HIGH)
       led.ChangeDutyCycle(50)

    if event.type == EventType.ON_RESPONDING_FINISHED:
       GPIO.output(6,GPIO.LOW)
       GPIO.output(5,GPIO.HIGH)
       led.ChangeDutyCycle(100)

    if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
       GPIO.output (5, GPIO.LOW)
       GPIO.output (6, GPIO.LOW)
       led.ChangeDutyCycle (0)

    print(event)

    if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
            event.args and not event.args['with_follow_on_turn']):
        GPIO.output(5,GPIO.LOW)
        GPIO.output(6,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))


        print()

    if event.type == EventType.ON_DEVICE_ACTION:
        for command, params in event.actions:
            print('Do command', command, 'with params', str(params))
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    def process_event(self,event):
        """Pretty prints events.
        Prints all events that occur with two spaces between each new
        conversation and a single space between turns of a conversation.
        Args:
            event(event.Event): The current event to process.
        """
        print(event)
        print()
        if event.type == EventType.ON_MUTED_CHANGED:
            self.mutestatus=event.args["is_muted"]

        if event.type == EventType.ON_START_FINISHED:
            self.can_start_conversation = True
            if GPIOcontrol:
                self.t2.start()
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                assistantindicator('mute')
            if (configuration['Wakewords']['Ok_Google']=='Disabled' or os.path.isfile("{}/.mute".format(USER_PATH))):
                self.assistant.set_mic_mute(True)
            if custom_wakeword:
                self.t1.start()
            if configuration['MQTT']['MQTT_Control']=='Enabled':
                self.t3.start()
            if irreceiver!=None:
                self.t4.start()
            if configuration['ADAFRUIT_IO']['ADAFRUIT_IO_CONTROL']=='Enabled':
                self.t5.start()

        if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
            subprocess.Popen(["aplay", "{}/sample-audio-files/Fb.wav".format(ROOT_PATH)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            self.can_start_conversation = False
            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})
                    kodi.GUI.ShowNotification({"title": "", "message": ".....Listening.....", "image": "{}/GoogleAssistantImages/GoogleAssistantBarsTransparent.gif".format(ROOT_PATH)})
                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)

        if (event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT or event.type == EventType.ON_NO_RESPONSE):
            self.can_start_conversation = True
            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 (configuration['Wakewords']['Ok_Google']=='Disabled' or os.path.isfile("{}/.mute".format(USER_PATH))):
                  self.assistant.set_mic_mute(True)
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                if GPIOcontrol:
                    assistantindicator('mute')
            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))

        if (event.type == EventType.ON_RESPONDING_STARTED and event.args and not event.args['is_error_response']):
            if GPIOcontrol:
                assistantindicator('speaking')

        if event.type == EventType.ON_RESPONDING_FINISHED:
            if GPIOcontrol:
                assistantindicator('off')

        if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
            if GPIOcontrol:
                assistantindicator('off')
            if self.singleresposne:
                self.assistant.stop_conversation()
                self.singledetectedresponse= event.args["text"]
            else:
                usrcmd=event.args["text"]
                self.custom_command(usrcmd)
                if kodicontrol:
                    try:
                        kodi.GUI.ShowNotification({"title": "", "message": event.args["text"], "image": "{}/GoogleAssistantImages/GoogleAssistantDotsTransparent.gif".format(ROOT_PATH)})
                    except requests.exceptions.ConnectionError:
                        print("Kodi TV box not online")

        if event.type == EventType.ON_RENDER_RESPONSE:
            if GPIOcontrol:
                assistantindicator('off')
            if kodicontrol:
                try:
                    kodi.GUI.ShowNotification({"title": "", "message": event.args["text"], "image": "{}/GoogleAssistantImages/GoogleAssistantTransparent.gif".format(ROOT_PATH),"displaytime": 20000})
                except requests.exceptions.ConnectionError:
                    print("Kodi TV box not online")

        if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
                event.args and not event.args['with_follow_on_turn']):
            self.can_start_conversation = True
            if GPIOcontrol:
                assistantindicator('off')
            if (configuration['Wakewords']['Ok_Google']=='Disabled' or os.path.isfile("{}/.mute".format(USER_PATH))):
                self.assistant.set_mic_mute(True)
            if os.path.isfile("{}/.mute".format(USER_PATH)):
                if GPIOcontrol:
                    assistantindicator('mute')
            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))

        if event.type == EventType.ON_DEVICE_ACTION:
            for command, params in event.actions:
                print('Do command', command, 'with params', str(params))