-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction-multidetection.py
executable file
·60 lines (47 loc) · 1.91 KB
/
action-multidetection.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
#!/usr/bin/env python3
import threading
import paho.mqtt.client as paho
import json
import mqtt_client
from pprint import pprint
global _multiDetectionsHolder
_multiDetectionsHolder = []
_sessions = {}
def handleMultiDetection():
# print('handleMultiDetection')
global _multiDetectionsHolder
global _sessions
# pprint(_multiDetectionsHolder)
if len(_multiDetectionsHolder) <= 1:
_multiDetectionsHolder = []
return
for sessionId in _sessions.keys():
message = _sessions[sessionId]
payload = json.loads(str(message.payload.decode("utf-8", "ignore")))
if payload['siteId'] != _multiDetectionsHolder[0]:
# print('endSession ' + sessionId)
client.publish('hermes/dialogueManager/endSession', json.dumps({'sessionId': sessionId}))
_multiDetectionsHolder = []
_sessions = {}
def onHotwordDetected(self, data, msg):
# print('onHotwordDetected')
global _multiDetectionsHolder
# pprint(_multiDetectionsHolder)
payload = json.loads(str(msg.payload.decode("utf-8", "ignore")))
if len(_multiDetectionsHolder) == 0:
threading.Timer(interval=0.3, function=handleMultiDetection).start()
_multiDetectionsHolder.append(payload['siteId'])
def onSessionStarted(self, data, msg):
# print('onSessionStarted')
global _multiDetectionsHolder
global _sessions
# pprint(_multiDetectionsHolder)
sessionId = json.loads(str(msg.payload.decode("utf-8", "ignore")))['sessionId']
_sessions[sessionId] = msg
client = paho.Client("multi")
client.username_pw_set(mqtt_client.get_user(), mqtt_client.get_pass())
client.connect(mqtt_client.get_addr(), mqtt_client.get_port(), 60)
client.subscribe([("hermes/hotword/default/detected", 0), ("hermes/dialogueManager/sessionStarted", 0)])
client.message_callback_add('hermes/hotword/default/detected', onHotwordDetected)
client.message_callback_add('hermes/dialogueManager/sessionStarted', onSessionStarted)
client.loop_forever()