Browse Source

Add snips notifications and feedback

Rechner 1 year ago
parent
commit
d9dcb81dbb
2 changed files with 49 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 47
    0
      main.py

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.pyc
2
+*.swp

+ 47
- 0
main.py View File

@@ -2,9 +2,15 @@ import wx
2 2
 import wx.lib.newevent
3 3
 import paho.mqtt.client as paho
4 4
 import threading
5
+import json
6
+#import subprocess
5 7
 
6 8
 import printing
7 9
 
10
+import gi
11
+gi.require_version('Notify', '0.7')
12
+from gi.repository import Notify
13
+
8 14
 MqttMessageWaiting, EVT_MQTT_MESSAGE_WAITING = wx.lib.newevent.NewEvent()
9 15
 
10 16
 class Controls(wx.Frame):
@@ -15,6 +21,9 @@ class Controls(wx.Frame):
15 21
         self.Bind(EVT_MQTT_MESSAGE_WAITING, self.processMqtt)
16 22
 
17 23
         sizer = wx.BoxSizer(wx.VERTICAL)
24
+
25
+        self.snipsBtn = wx.Button(self, wx.ID_ANY, "Idle")
26
+
18 27
         self.printBtn = wx.Button(self, wx.ID_ANY, "Print Date Label")
19 28
         self.printBtn.SetFont(wx.Font(20, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD, False))
20 29
         self.printBtn.Bind(wx.EVT_BUTTON, self.print_date)
@@ -35,6 +44,7 @@ class Controls(wx.Frame):
35 44
         self.SetBackgroundColour("black")
36 45
         self.ShowFullScreen(True)
37 46
 
47
+        sizer.Add(self.snipsBtn, -1, wx.ALL|wx.EXPAND|wx.ALIGN_CENTRE, 0)
38 48
         sizer.Add(self.printBtn, 2, wx.ALL|wx.EXPAND|wx.ALIGN_CENTRE, 20)
39 49
 
40 50
         hsizer = wx.BoxSizer(wx.HORIZONTAL)
@@ -46,6 +56,8 @@ class Controls(wx.Frame):
46 56
 
47 57
         self.printer = printing.Main(False)
48 58
 
59
+        Notify.init("Snips")
60
+
49 61
         self.mqtt = paho.Client()
50 62
         self.mqtt.connect("10.0.10.11")
51 63
         self.mqtt.on_publish = Controls.on_publish
@@ -55,6 +67,7 @@ class Controls(wx.Frame):
55 67
         
56 68
         self.mqtt.subscribe("homeassistant/light/kitchen/state")
57 69
         self.mqtt.subscribe("homeassistant/light/dining_room/state")
70
+        self.mqtt.subscribe("hermes/dialogueManager/#")
58 71
 
59 72
         self.buttons = {
60 73
             'homeassistant/light/kitchen/state'     : self.kitchenBtn,
@@ -73,6 +86,10 @@ class Controls(wx.Frame):
73 86
     def on_publish(client, userdata, result):
74 87
         print("Published: {0}: {1}".format(userdata, result))
75 88
 
89
+    def send_notification(self, title, text, icon="dialog-information"):
90
+        #subprocess.check_call(['/usr/bin/notify-send', str(title), str(text)])
91
+        Notify.Notification.new(title, text).show()
92
+        
76 93
   
77 94
     def toggle_kitchen(self, event):
78 95
         print("Kitchen light toggle")
@@ -82,7 +99,36 @@ class Controls(wx.Frame):
82 99
         print("Dining light toggle")
83 100
         self.mqtt.publish("house/switch/dining_room", "toggle")
84 101
 
102
+    def snips_idle(self):
103
+        self.snipsBtn.SetBackgroundColour("white")
104
+        self.snipsBtn.SetForegroundColour("black")
105
+        self.snipsBtn.SetLabelText("Idle...")
106
+
107
+    def processHermes(self, event):
108
+        try:
109
+            payload = json.loads(event.payload)
110
+        except:
111
+            payload = {}
112
+        if event.topic == "hermes/dialogueManager/sessionStarted":
113
+            self.snipsBtn.SetBackgroundColour("green")
114
+            self.snipsBtn.SetForegroundColour("white")
115
+            self.snipsBtn.SetLabelText("Listening...")
116
+        if event.topic == "hermes/dialogueManager/sessionEnded":
117
+            if 'termination' in payload.keys():
118
+                if payload['termination']['reason'] in ('timeout', 'intentNotRecognized'):
119
+                    self.send_notification("Sorry, I didn't quite catch that")
120
+                self.snips_idle()
121
+
122
+        if event.topic == "hermes/dialogueManager/endSession":
123
+            if 'text' in payload.keys():
124
+                print(payload['text'])
125
+                self.send_notification(payload['text'], payload['text'])
126
+
85 127
     def processMqtt(self, event):
128
+        if event.topic.startswith("hermes"):
129
+            self.processHermes(event)
130
+            return
131
+
86 132
         try:
87 133
             button = self.buttons[event.topic]
88 134
         except KeyError:
@@ -108,6 +154,7 @@ class Controls(wx.Frame):
108 154
 
109 155
 
110 156
     def close(self, event):
157
+        Notify.uninit()
111 158
         self.MQTT_EXIT.set()
112 159
         self.thread.join()
113 160
         self.mqtt.disconnect()