forked from jcasoft/Mycroft-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
199 lines (142 loc) · 4.92 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
# Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
import tornado.ioloop as ioloop
import tornado.ioloop
import tornado.web as web
from mycroft.configuration import ConfigurationManager
from mycroft.messagebus.service.ws import WebsocketEventHandler
from mycroft.util import validate_param
from mycroft.lock import Lock # creates/supports PID locking file
__author__ = 'seanfitz', 'jdorleans'
settings = {
'debug': True
}
# --------------------------------------
#__co-author__ = 'jcasoft' # Only for Web Client
from datetime import date
import tornado.escape
#import tornado.web
import os
import json
import time
import tornado.httpserver
import tornado.websocket
import tornado.gen
from tornado.options import define, options
import multiprocessing
from netifaces import interfaces, ifaddresses, AF_INET
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from threading import Thread
ws = None
# --------------------------------------
clients = []
for ifaceName in interfaces():
ip = addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )][0]
input_queue = multiprocessing.Queue()
output_queue = multiprocessing.Queue()
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html',ip=ip)
class StaticFileHandler(tornado.web.RequestHandler):
def get(self):
self.render('main.js')
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
clients.append(self)
self.write_message("Mycroft connected OK")
self.write_message("Welcome to Mycroft")
def on_message(self, message):
utterance = json.dumps(message)
msg_recv = "- " + utterance
if utterance:
self.write_message(msg_recv)
lang = 'en-us'
data = {"lang": lang, "session": "", "utterances": [utterance]}
ws.emit(Message('recognizer_loop:utterance', data))
t = Thread(target = self.newThread)
t.start()
def newThread(self):
global wait_response
global skill_response
timeout = 0
while wait_response:
wait_response = True
time.sleep(1)
timeout = timeout + 1
skill_response = skill_response.replace("Checking for Updates","")
skill_response = skill_response.replace("Skills Updated. Mycroft is ready","")
self.write_message(skill_response)
skill_response = ""
wait_response = True
timeout = 0
while timeout < 10 or wait_response:
time.sleep(1)
timeout = timeout + 1
if len(skill_response) > 0:
self.write_message(skill_response)
wait_response = True
skill_response = ""
def on_close(self):
clients.remove(self)
def connect():
ws.run_forever()
def handle_speak(event):
response = event.data['utterance']
global skill_response, wait_response
skill_response = ""
wait_response = False
skill_response = response
# --------------------------------------
def main():
global skill_response, wait_response
wait_response = True
skill_response = ""
global ws
ws = WebsocketClient()
event_thread = Thread(target=connect)
event_thread.setDaemon(True)
event_thread.start()
ws.on('speak', handle_speak)
import tornado.options
lock = Lock("service")
tornado.options.parse_command_line()
config = ConfigurationManager.get().get("websocket")
host = config.get("host")
port = config.get("port")
route = config.get("route")
validate_param(host, "websocket.host")
validate_param(port, "websocket.port")
validate_param(route, "websocket.route")
routes = [
(route, WebsocketEventHandler),
tornado.web.url(r"/", IndexHandler),
tornado.web.url(r"/static/(.*)", tornado.web.StaticFileHandler, {'path': './'}),
tornado.web.url(r"/ws", WebSocketHandler)
]
settings = {
"debug": True,
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
application = web.Application(routes, **settings)
httpServer = tornado.httpserver.HTTPServer(application)
tornado.options.parse_command_line()
httpServer.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()