-
Notifications
You must be signed in to change notification settings - Fork 0
/
sima.py
252 lines (215 loc) · 7.9 KB
/
sima.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
#!/usr/bin/env python3
"""
sima.py
Навык Робот Сима
https://github.com/hjltu/sima
author: hjltu@ya.ru, license: MIT/X11
yandex:
https://tech.yandex.ru/dialogs/alice
https://dialogs.yandex.ru/developer/
ssl crt:
openssl req -new -keyout crt.pem -out crt.pem -x509 -days 365 -nodes -subj '/CN=site.com/O=user/C=RU'
lib/sn.py RPi serial to password
clients/my.csv Home automation hjmqtt
https://github.com/hjltu/hjmqtt
lib/mysend.py send MQTT command to hjconnect
https://github.com/hjltu/hjconnect
usage:
python3 sima.py [-t],[-test]
last change: 24-feb-19
"""
# coding: utf-8
# from __future__ import unicode_literals
import json
import cgi
import ssl
import random
import time
import os
import sys
import threading
from lib import mysend
from lib import mytext
from lib import accessory
from lib import mydb
from lib import dialog
from lib import sn
# http
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
import threading
# create and test db
mydb.create()
if mydb.select() == 2:
mydb.insert()
# user_id and sn, dict
users = mydb.select_all()
if type(users) is not dict:
print("ERR in db, exit")
sys.exit(1)
# load clients data
# client's dirname, string
MYDIR = mytext.clientsDir
# list filenames in client's dir
isfile = os.path.isfile
join = os.path.join
myfiles = [f for f in os.listdir(MYDIR) if isfile(join(MYDIR, f))]
print(myfiles)
# list RPi serial numbers
mysn = dialog.get_sn(MYDIR, myfiles)
if "ERR" in mysn:
sys.exit(1)
print(mysn)
# create accessories
Lt = accessory.Light()
Rl = accessory.Rele()
Dm = accessory.Dimmer()
Ct = accessory.Curtains()
Tr = accessory.Term()
#sys.exit(0)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
print("get")
self.send_response(200)
self.end_headers()
message = threading.currentThread().getName()
message += '\nHi! I am Robot Sima, i live here.'
self.wfile.write(message.encode())
self.wfile.write('\n'.encode())
return
def do_POST(self):
# print("post")
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
# refuse if not json
if ctype != 'application/json':
self.send_response(400)
self.end_headers()
print("ERR: json header")
return
# check input json
try:
# json to dict
length = int(self.headers.get('content-length'))
msg = json.loads(self.rfile.read(length).decode("utf-8"))
# command, str
comm = msg["request"]["command"].lower()
# device ID, str
userId = msg["session"]["user_id"]
except:
print("ERR: not json")
return
try:
# default answer
msg = {"response": {"text": mytext.ans,
"tts": mytext.ans,
"end_session": False},
"session": {"session_id": msg["session"]["session_id"],
"message_id": msg["session"]["message_id"],
"user_id": msg["session"]["user_id"]},
"version": "1.0"}
#msg["response"]["tts"] = mytext.ans
except:
print("ERR: wrong json")
return
# when json is OK, general Q&A session
if len(comm) == 0:
comm = mytext.welcome
msg = dialog.common(msg, comm)
# global variables defines out of class
global users
global myfiles
global mysn
global MYDIR
# check new user device,
# add new ID in db and users, check result
if userId not in users:
out = mydb.insert(name=userId)
if out == 0:
users[userId] = None
print("add new id")
msg["response"]["text"] = random.choice(mytext.done)
msg["response"]["tts"] = random.choice(mytext.done)
else:
print("ERR: mydb.insert return",out)
msg["response"]["text"] = random.choice(mytext.err)
msg["response"]["tts"] = random.choice(mytext.err)
# add or remove RPi serial
if mytext.myPass in comm or mytext.myDel in comm:
# check password
passwd = dialog.check_passwd(comm)
if passwd:
for s in mysn:
if sn.main(s) == passwd: # selial to password
# add new password
if mytext.myPass in comm:
out = mydb.update(name=userId, stat=passwd)
if out == 0:
users = mydb.select_all()
print("add sn")
msg["response"]["text"] = random.choice(mytext.done)
msg["response"]["tts"] = mytext.harp + random.choice(mytext.done)
else:
msg["response"]["text"] = random.choice(mytext.err)
msg["response"]["tts"] = random.choice(mytext.err)
# remove password
if mytext.myDel in comm:
out = mydb.update(name=userId)
if out == 0:
users[userId] = None
print("remove sn")
msg["response"]["text"] = random.choice(mytext.done)
msg["response"]["tts"] = random.choice(mytext.done)
else:
msg["response"]["text"] = random.choice(mytext.err)
msg["response"]["tts"] = random.choice(mytext.err)
# welcome new user
msg = dialog.my_welcome(msg, users[userId], comm)
# for registered users
if users[userId]:
serNum = None
passwd = mydb.select(name=userId)
for s in mysn:
if sn.main(s) == passwd:
serNum = s
# Hooray! Home automation section
if serNum:
accessories = dialog.get_accessories(serNum, MYDIR, myfiles)
print(accessories)
for acc in accessories:
if acc[1] == "rele":
out = Rl.action(comm, acc[0], acc[2])
if acc[1] == "light":
out = Lt.action(comm, acc[0], acc[2])
if acc[1] == "dimm":
out = Dm.action(comm, acc[0], acc[2])
if acc[1] == "curt":
out = Ct.action(comm, acc[0], acc[2])
if acc[1] == "term":
out = Tr.action(comm, acc[0], acc[2])
if type(out) == dict:
msg["response"]["text"] = random.choice(mytext.done)
msg["response"]["tts"] = mytext.sw + random.choice(mytext.done)
for k in out:
threading.Thread(target=mysend.main, args=(serNum, k, out[k])).start()
# We did it!
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(msg).encode())
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
#def my_user(msg, userId):
# return msg
if __name__ == "__main__":
print("Start ",time.strftime("%d-%b-%y_%H:%M:%S"),"\n")
try:
if sys.argv[1] == "-t" or sys.argv[1] == "-test":
server = ThreadedHTTPServer(('localhost', 8000), Handler)
except:
server = HTTPServer(("0.0.0.0", 8000), Handler)
server.socket = ssl.wrap_socket( server.socket, certfile='./crt.pem', server_side=True )
try:
server.serve_forever()
except KeyboardInterrupt:
pass
print("exit")