forked from corouteam/Assistant-Candy-Dispenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
667 lines (607 loc) · 33.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/usr/bin/env python
# Copyright (C) 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from kodijson import Kodi, PLAYER_VIDEO
import RPi.GPIO as GPIO
import argparse
import json
import os.path
import pathlib2 as pathlib
import os
import subprocess
import re
import psutil
import logging
import time
import random
import snowboydecoder
import sys
import signal
import requests
import google.oauth2.credentials
from google.assistant.library import Assistant
from google.assistant.library.event import EventType
from google.assistant.library.file_helpers import existing_file
from google.assistant.library.device_helpers import register_device
from actions import say
from actions import Action
from actions import YouTube_No_Autoplay
from actions import YouTube_Autoplay
from actions import stop
from actions import radio
from actions import ESP
from actions import track
from actions import feed
from actions import kodiactions
from actions import mutevolstatus
from actions import gmusicselect
from actions import refreshlists
from actions import chromecast_play_video
from actions import chromecast_control
from actions import kickstarter_tracker
from actions import getrecipe
from actions import hue_control
from actions import vlcplayer
from actions import spotify_playlist_select
from actions import configuration
from actions import custom_action_keyword
from threading import Thread
from indicator import assistantindicator
from indicator import stoppushbutton
from pathlib import Path
from actions import Domoticz_Device_Control
from actions import domoticz_control
from actions import domoticz_devices
from actions import gaana_playlist_select
from actions import deezer_playlist_select
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
WARNING_NOT_REGISTERED = """
This device is not registered. This means you will not be able to use
Device Actions or see your device in Assistant Settings. In order to
register this device follow instructions at:
https://developers.google.com/assistant/sdk/guides/library/python/embed/register-device
"""
logging.basicConfig(filename='/tmp/GassistPi.log', level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
ROOT_PATH = os.path.realpath(os.path.join(__file__, '..', '..'))
USER_PATH = os.path.realpath(os.path.join(__file__, '..', '..','..'))
#Login with default kodi/kodi credentials
#kodi = Kodi("http://localhost:8080/jsonrpc")
#Login with custom credentials
# Kodi("http://IP-ADDRESS-OF-KODI:8080/jsonrpc", "username", "password")
kodiurl=("http://"+str(configuration['Kodi']['ip'])+":"+str(configuration['Kodi']['port'])+"/jsonrpc")
kodi = Kodi(kodiurl, configuration['Kodi']['username'], configuration['Kodi']['password'])
mutestopbutton=True
#Sonoff-Tasmota Declarations
#Make sure that the device name assigned here does not overlap any of your smart device names in the google home app
tasmota_devicelist=configuration['Tasmota_devicelist']['friendly-names']
tasmota_deviceip=configuration['Tasmota_devicelist']['ipaddresses']
tasmota_deviceportid=configuration['Tasmota_devicelist']['portID']
#Magic Mirror Remote Control Declarations
mmmip=configuration['Mmmip']
# Check if VLC is paused
def checkvlcpaused():
state=vlcplayer.state()
if str(state)=="State.Paused":
currentstate=True
else:
currentstate=False
return currentstate
#Function to control Sonoff Tasmota Devices
def tasmota_control(phrase,devname,devip,devportid):
try:
if 'on' in phrase:
rq=requests.head("http://"+devip+"/cm?cmnd=Power"+devportid+"%20on")
say("Tunring on "+devname)
elif 'off' in phrase:
rq=requests.head("http://"+devip+"/cm?cmnd=Power"+devportid+"%20off")
say("Tunring off "+devname)
except requests.exceptions.ConnectionError:
say("Device not online")
#Check if custom wakeword has been enabled
if configuration['Wakewords']['Custom_Wakeword']=='Enabled':
custom_wakeword=True
else:
custom_wakeword=False
models=configuration['Wakewords']['Custom_wakeword_models']
#Custom Conversation
numques=len(configuration['Conversation']['question'])
numans=len(configuration['Conversation']['answer'])
class Myassistant():
def __init__(self):
self.interrupted=False
self.can_start_conversation=False
self.assistant=None
self.sensitivity = [0.5]*len(models)
self.callbacks = [self.detected]*len(models)
self.detector = snowboydecoder.HotwordDetector(models, sensitivity=self.sensitivity)
self.t1 = Thread(target=self.start_detector)
self.t2 = Thread(target=self.pushbutton)
def signal_handler(self,signal, frame):
self.interrupted = True
def interrupt_callback(self,):
return self.interrupted
def buttonsinglepress(self):
if os.path.isfile("{}/.mute".format(USER_PATH)):
os.system("sudo rm {}/.mute".format(USER_PATH))
assistantindicator('unmute')
if configuration['Wakewords']['Ok_Google']=='Disabled':
self.assistant.set_mic_mute(True)
else:
self.assistant.set_mic_mute(False)
# if custom_wakeword:
# self.t1.start()
subprocess.Popen(["aplay", "{}/sample-audio-files/Mic-On.wav".format(ROOT_PATH)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Turning on the microphone")
else:
open('{}/.mute'.format(USER_PATH), 'a').close()
assistantindicator('mute')
self.assistant.set_mic_mute(True)
# if custom_wakeword:
# self.thread_end(t1)
subprocess.Popen(["aplay", "{}/sample-audio-files/Mic-Off.wav".format(ROOT_PATH)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Turning off the microphone")
def buttondoublepress(self):
print('Stopped')
stop()
def buttontriplepress(self):
print("Create your own action for button triple press")
def pushbutton(self):
while mutestopbutton:
if GPIO.event_detected(stoppushbutton):
GPIO.remove_event_detect(stoppushbutton)
now = time.time()
count = 1
GPIO.add_event_detect(stoppushbutton,GPIO.RISING)
while time.time() < now + 1:
if GPIO.event_detected(stoppushbutton):
count +=1
time.sleep(.25)
if count == 2:
self.buttonsinglepress()
GPIO.remove_event_detect(stoppushbutton)
GPIO.add_event_detect(stoppushbutton,GPIO.FALLING)
elif count == 3:
self.buttondoublepress()
GPIO.remove_event_detect(stoppushbutton)
GPIO.add_event_detect(stoppushbutton,GPIO.FALLING)
elif count == 4:
self.buttontriplepress()
GPIO.remove_event_detect(stoppushbutton)
GPIO.add_event_detect(stoppushbutton,GPIO.FALLING)
def process_device_actions(self,event, device_id):
if 'inputs' in event.args:
for i in event.args['inputs']:
if i['intent'] == 'action.devices.EXECUTE':
for c in i['payload']['commands']:
for device in c['devices']:
if device['id'] == device_id:
if 'execution' in c:
for e in c['execution']:
if 'params' in e:
yield e['command'], e['params']
else:
yield e['command'], None
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))
def register_device(self,project_id, credentials, device_model_id, device_id):
"""Register the device if needed.
Registers a new assistant device if an instance with the given id
does not already exists for this model.
Args:
project_id(str): The project ID used to register device instance.
credentials(google.oauth2.credentials.Credentials): The Google
OAuth2 credentials of the user to associate the device
instance with.
device_model_id: The registered device model ID.
device_id: The device ID of the new instance.
"""
base_url = '/'.join([DEVICE_API_URL, 'projects', project_id, 'devices'])
device_url = '/'.join([base_url, device_id])
session = google.auth.transport.requests.AuthorizedSession(credentials)
r = session.get(device_url)
print(device_url, r.status_code)
if r.status_code == 404:
print('Registering....')
r = session.post(base_url, data=json.dumps({
'id': device_id,
'model_id': device_model_id,
'client_type': 'SDK_LIBRARY'
}))
if r.status_code != 200:
raise Exception('failed to register device: ' + r.text)
print('\rDevice registered.')
def detected(self):
if self.can_start_conversation == True:
self.assistant.set_mic_mute(False)
self.assistant.start_conversation()
print('Assistant is listening....')
def start_detector(self):
self.detector.start(detected_callback=self.callbacks,
interrupt_check=self.interrupt_callback,
sleep_time=0.03)
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:
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():
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():
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()
if __name__ == '__main__':
try:
Myassistant().main()
except Exception as error:
logger.exception(error)