-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
388 lines (290 loc) · 12.1 KB
/
__init__.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
# Copyright 2019 S. M. Estiaque Ahmed
#
# 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.
import os
import sqlite3
from datetime import timedelta
from json2html import json2html
# For test.
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from adapt.intent import IntentBuilder
from mycroft.util.time import now_local
from mycroft.skills.core import MycroftSkill
from mycroft.skills.core import intent_handler
from mycroft.util.format import pronounce_number
class HealthSkill(MycroftSkill):
def __init__(self):
super(HealthSkill, self).__init__(name="HealthSkill")
self.log.error(self.root_dir)
self.home_dir = os.path.expanduser('~')
self.log.error(self.home_dir)
@intent_handler(IntentBuilder("PressureIntent").require("Track")
.require("Health").require("Pressure"))
def handle_pressure_intent(self, message):
top = self.get_response("top.mh")
if not top:
self.speak_dialog("error.input.mh")
return
try:
top = float(top)
except Exception:
self.speak_dialog("error.input.mh")
return
bottom = self.get_response("bottom.mh")
if not bottom:
self.speak_dialog("error.input.mh")
return
try:
bottom = float(bottom)
except Exception:
self.speak_dialog("error.input.mh")
return
person = self.get_response("person.mh")
person = "" if not person else person
self.log.error(top)
self.log.error(bottom)
self.log.error(person)
pressure_data = [(now_local(), "pressure", str(top), "", person),
(now_local(), "pressure", str(bottom), "", person)]
confirm = self.ask_yesno(
"confirm.pressure.mh",
{"top": pronounce_number(top),
"bottom": pronounce_number(bottom)})
if confirm == "yes":
if not self.insert_db(pressure_data):
self.speak_dialog("error.save.mh")
@intent_handler(IntentBuilder("SugarIntent").require("Track")
.require("Health").require("Sugar").require("Value"))
def handle_sugar_intent(self, message):
value = message.data.get("Value")
if not value:
self.speak_dialog("error.input.mh")
return
try:
value = float(value)
except Exception:
self.speak_dialog("error.input.mh")
return
meal_status = self.get_response("meal.mh")
if not meal_status:
return
person = self.get_response("person.mh")
person = "" if not person else person
self.log.error(value)
self.log.error(meal_status)
self.log.error(person)
sugar_data = [(now_local(), "diabetes", str(value),
meal_status, person)]
confirm = self.ask_yesno(
"confirm.sugar.mh",
{"meal": meal_status,
"value": pronounce_number(value)})
if confirm == "yes":
if not self.insert_db(sugar_data):
self.speak_dialog("error.save.mh")
@intent_handler(IntentBuilder("TemperatureIntent").require("Track")
.require("Health").require("Temperature").require("Value"))
def handle_temperature_intent(self, message):
value = message.data.get("Value")
if not value:
self.speak_dialog("error.input.mh")
return
try:
value = float(value)
except Exception:
self.speak_dialog("error.input.mh")
return
person = self.get_response("person.mh")
person = "" if not person else person
self.log.error(value)
self.log.error(person)
temperature_data = [(now_local(), "temperature",
str(value), "", person)]
confirm = self.ask_yesno(
"confirm.temperature.mh",
{"value": pronounce_number(value)})
if confirm == "yes":
if not self.insert_db(temperature_data):
self.speak_dialog("error.save.mh")
@intent_handler(IntentBuilder("PainIntent").require("Track")
.require("Health").require("Pain").require("Value"))
def handle_pain_intent(self, message):
value = message.data.get("Value")
if not value:
self.speak_dialog("error.input.mh")
return
person = self.get_response("person.mh")
person = "" if not person else person
self.log.error(value)
self.log.error(person)
pain_data = [(now_local(), "pain", value, "", person)]
confirm = self.ask_yesno(
"confirm.pain.mh",
{"value": value})
if confirm == "yes":
if not self.insert_db(pain_data):
self.speak_dialog("error.save.mh")
@intent_handler(IntentBuilder("HeartbeatIntent").require("Track")
.require("Health").require("Heartbeat").require("Value"))
def handle_heartbeat_intent(self, message):
value = message.data.get("Value")
if not value:
self.speak_dialog("error.input.mh")
return
try:
value = int(value)
except Exception:
self.speak_dialog("error.input.mh")
return
person = self.get_response("person.mh")
person = "" if not person else person
self.log.error(value)
self.log.error(person)
heartbeat_data = [(now_local(), "heartbeat", str(value),
"", person)]
confirm = self.ask_yesno(
"confirm.heartbeat.mh",
{"value": pronounce_number(value)})
if confirm == "yes":
if not self.insert_db(heartbeat_data):
self.speak_dialog("error.save.mh")
@intent_handler(IntentBuilder("GenerateIntent").require("Generate")
.require("Health").require("Report").require("Month"))
# @intent_handler(IntentBuilder("GenerateIntent").require("Generate")
# .require("Health").require("Report"))
def handle_generate_intent(self, message):
# month = message.data.get("Month")
# self.log.error(month)
# For test.
month = "this"
if not month:
self.speak_dialog("error.input.mh")
return
category = self.get_response("category.mh")
if not category or category not in ["pressure", "diabetes", "pain",
"temperature", "heartbeat"]:
self.speak_dialog("error.input.mh")
return
person = self.get_response("person.mh")
person = "" if not person else person
current_datetime = now_local()
# Multi languages support should be implemented here.
if month == "this" or month == "current":
to_datetime = current_datetime
elif month == "last" or month == "previous":
to_datetime = current_datetime.replace(
day=1,
hour=23,
minute=59,
second=59,
microsecond=0) - timedelta(days=1)
from_datetime = to_datetime.replace(
day=1,
hour=0,
minute=0,
second=0,
microsecond=0)
health_data_list = self.get_data(from_datetime, to_datetime,
category, person)
health_data = []
for data in health_data_list:
health_data.append({
'Datetime': data[0].strftime("%m/%d/%Y %H:%M:%S"),
'Category': data[1],
'Value': data[2],
'Parameter': data[3],
'Person': data[4]})
table = json2html.convert(json=health_data)
content = """
<!DOCTYPE html><html><head><style>table{font-family:arial,sans-serif;
border-collapse:collapse;width:100%;}td,th{border:1px solid #dddddd;
text-align:left;padding:8px;}tr:nth-child(even)
{background-color: #dddddd;}</style></head><body>"""
content = """
{0}<h2>Health Data: {1} to {2}</h2>
{3}</body></html>""".format(
content,
from_datetime.strftime("%m/%d/%Y %H:%M:%S"),
to_datetime.strftime("%m/%d/%Y %H:%M:%S"),
table)
# self.send_email(
# "Health Report - {0}".format(category.upper()),
# content)
# For test.
message = Mail(
from_email="Mycroft Health <health@mycroft.ai>",
to_emails="smearumi@gmail.com",
subject="Mycroft Health Report - {0}".format(category.upper()),
html_content=content)
# Please provide your own Api key for testing as it's not wise to
# expose Api key in public.
api_key = "<your sendgrid API key>"
try:
sg = SendGridAPIClient(api_key)
_ = sg.send(message)
except Exception:
pass
def db_connect(self):
self.log.error(self.root_dir)
try:
connection = sqlite3.connect(
"{0}/mycroft-health.db".format(
self.home_dir),
detect_types=sqlite3.PARSE_DECLTYPES |
sqlite3.PARSE_COLNAMES)
except Exception:
return None
try:
cursor = connection.cursor()
sql = """CREATE TABLE health_data (datetime TIMESTAMP NOT NULL,
category TEXT NOT NULL, value TEXT NOT NULL,
parameter TEXT, person TEXT)"""
cursor.execute(sql)
connection.commit()
# connection.close()
except Exception:
pass
return connection
def insert_db(self, data):
connection = self.db_connect()
if not connection:
return
sql = """INSERT INTO health_data VALUES (?, ?, ?, ?, ?)"""
with connection:
try:
cursor = connection.cursor()
cursor.executemany(sql, data)
connection.commit()
except Exception:
connection.rollback()
return False
return True
def get_data(self, from_datetime, to_datetime, category, person):
connection = self.db_connect()
if not connection:
return
sql = "SELECT * FROM health_data WHERE datetime > ? and datetime < ?"
sql = "{0} and category = ? and person = ?".format(sql)
data_tuple = (from_datetime, to_datetime, category, person)
with connection:
try:
cursor = connection.cursor()
cursor.execute(sql, data_tuple)
return cursor.fetchall()
except Exception:
return None
def stop(self):
pass
def create_skill():
return HealthSkill()