-
Notifications
You must be signed in to change notification settings - Fork 16
/
__init__.py
executable file
·418 lines (370 loc) · 14.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Copyright 2017 Mycroft AI Inc.
#
# 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 astral
import time
import arrow
from mycroft.skills.core import MycroftSkill
from mycroft.util.log import LOG
from mycroft.audio import wait_while_speaking
from mycroft import intent_file_handler
from ast import literal_eval as parse_tuple
from threading import Timer
from pytz import timezone
from datetime import datetime
class Mark1(MycroftSkill):
def __init__(self):
super(Mark1, self).__init__("Mark1")
self.should_converse = False
self._settings_loaded = False
self.converse_context = None
self.custom_rgb = []
# colors in (r, g, b)
self.color_dict = {
'red': (255, 0, 0),
'green': (0, 128, 0),
'yellow': (255, 255, 0),
'blue': (0, 0, 255),
'orange': (245, 130, 48),
'purple': (128, 0, 128),
'cyan': (0, 255, 255),
'magenta': (255, 0, 255),
'lime': (0, 255, 0),
'pink': (250, 190, 190),
'teal': (0, 128, 128),
'lavendar': (230, 190, 255),
'brown': (170, 110, 40),
'beige': (255, 250, 200),
'maroon': (128, 0, 0),
'mint': (170, 255, 195),
'olive': (128, 128, 0),
'coral': (255, 215, 180),
'navy': (0, 0, 128),
'grey': (128, 128, 128),
'gray': (128, 128, 128),
'white': (255, 255, 255),
'black': (0, 0, 0),
'default': (34, 167, 240) # mycroft blue
}
def initialize(self):
self.register_entity_file('color.entity')
if self.settings.get('auto_brightness') is None:
self.settings['auto_brightness'] = False
def set_eye_color(self, color=None, rgb=None, speak=True):
""" function to set eye color
Args:
custom (bool): user inputed rgb
speak (bool): to have success speak on change
"""
if color is not None:
color_rgb = self.color_dict.get(color, None)
if color_rgb is not None:
(r, g, b) = color_rgb
self.enclosure.eyes_color(r, g, b)
elif rgb is not None:
(r, g, b) = rgb
self.enclosure.eyes_color(r, g, b)
self._current_color = (r, g, b)
if speak is True:
self.speak_dialog('set.color.success')
@intent_file_handler('custom.eye.color.intent')
def handle_custom_eye_color(self, message):
""" Intent Callback to set custom eye colors in rgb
Args:
message (dict): messagebus message from intent parser
"""
# reset list
self.custom_rgb = []
self.set_converse('set.custom.color')
self.speak_dialog('set.custom.color')
wait_while_speaking()
self.speak_dialog(
'need.custom',
data={'color': 'red'},
expect_response=True)
@intent_file_handler('eye.color.intent')
def handle_eye_color(self, message):
""" Callback to set eye color from list
Args:
message (dict): messagebus message from intent parser
"""
if 'color' in message.data:
color = message.data.get('color', None)
if color is not None:
self.set_eye_color(color=color)
else:
self.set_converse('need.color')
self.speak_dialog(
'color.not.found.expect.response', expect_response=True)
else:
self.set_converse('need.color')
self.speak_dialog(
'color.not.found.expect.response', expect_response=True)
def is_rgb_format_correct(self, rgb):
""" checks for correct rgb format and value
Args:
rgb (tuple): tuple with integer values
return:
(bool): for correct rgb value
"""
(r, g, b) = rgb
if 0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255:
return True
else:
return False
def parse_to_rgb(self, color):
""" parse the color and returns rgb. color can be
Hex, RGB, or color from color_dict
Args:
color (str): RGB, Hex, or color from color_dict
returns:
(r, g, b) (tuple): rgb from 0 - 255
"""
# color exist in dict
if color in self.color_dict:
return self.color_dict[color]
# color is rgb
try:
(r, g, b) = parse_tuple(color)
return (r, g, b)
except:
LOG.info('RGB format is incorrect')
# color is hex
try:
if color[0] is '#':
color = color[1:]
if len(color) != 6:
raise
(r, g, b) = \
int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16)
return (r, g, b)
except:
LOG.info('Hex format is incorrect')
# color is None of the above
return None
# TODO: allow skills to update the web ui settings with
# rgb values. This will require some mycroft-core changes
@intent_file_handler('get.color.web.intent')
def handle_web_settings(self):
""" Callback to set eye color to web settings
Args:
message (dict): messagebus message from intent parser
"""
_color = self.settings.get('eye color', "")
rgb = self.parse_to_rgb(_color)
if rgb is not None:
correct = self.is_rgb_format_correct(rgb)
if correct:
self.set_eye_brightness(rgb)
else:
self.speak_dialog('error.format')
else:
self.speak_dialog('error.format')
def set_eye_brightness(self, brightness, speak=True):
""" set eye brightness
"""
self.enclosure.eyes_brightness(brightness)
if speak is True:
self.speak_dialog('brightness.set', data={'val': brightness})
@intent_file_handler('brightness.intent')
def handle_brightness(self, message):
""" Intent Callback to set custom eye colors in rgb
Args:
message (dict): messagebus message from intent parser
"""
LOG.info(message.data)
self.auto_brightness = False
if 'brightness' in message.data:
try:
brightness = int(message.data.get('brightness'))
if brightness > 30:
raise
self.set_eye_brightness(brightness)
except Exception as e:
LOG.error(e)
self.set_converse('need.brightness')
self.speak_dialog('brightness.not.found', expect_response=True)
else:
self.set_converse('need.brightness')
self.speak_dialog('brightness.not.found', expect_response=True)
def _get_auto_time(self):
""" get dawn, sunrise, noon, sunset, and dusk time
returns:
times (dict): dict with associated (datetime, brightnes)
"""
tz = self.location['timezone']['code']
lat = self.location['coordinate']['latitude']
lon = self.location['coordinate']['longitude']
ast_loc = astral.Location()
ast_loc.timezone = tz
ast_loc.lattitude = lat
ast_loc.longitude = lon
user_set_tz = \
timezone(tz).localize(datetime.now()).strftime('%Z')
device_tz = time.tzname
if user_set_tz in device_tz:
sunrise = ast_loc.sun()['sunrise']
noon = ast_loc.sun()['noon']
sunset = ast_loc.sun()['sunset']
else:
secs = int(self.location['timezone']['offset']) / -1000
sunrise = arrow.get(
ast_loc.sun()['sunrise']).shift(
seconds=secs).replace(tzinfo='UTC').datetime
noon = arrow.get(
ast_loc.sun()['noon']).shift(
seconds=secs).replace(tzinfo='UTC').datetime
sunset = arrow.get(
ast_loc.sun()['sunset']).shift(
seconds=secs).replace(tzinfo='UTC').datetime
return {
'Sunrise': (sunrise, 20),
'Noon': (noon, 30),
'Sunset': (sunset, 5)
}
def schedule_brightness(self, time_of_day, pair):
""" shcedule auto bright ness with the event scheduler
Args:
time_of_day (str): Sunrise, Noon, Sunset
pair (tuple): (datetime, brightness)
"""
d_time = pair[0]
brightness = pair[1]
now = arrow.now()
arw_d_time = arrow.get(d_time)
data = (time_of_day, brightness)
if now.timestamp > arw_d_time.timestamp:
d_time = arrow.get(d_time).shift(hours=+24)
self.schedule_event(self._handle_eye_brightness_event, d_time,
data=data, name=time_of_day)
else:
self.schedule_event(self._handle_eye_brightness_event, d_time,
data=data, name=time_of_day)
# TODO: this is currently set by voice.
# allow setting from faceplate and web ui
@intent_file_handler('brightness.auto.intent')
def handle_auto_brightness(self, message):
""" brightness varies depending on time of day
Args:
message (dict): messagebus message from intent parser
"""
self.auto_brightness = True
auto_time = self._get_auto_time()
nearest_time_to_now = (float('inf'), None, None)
for time_of_day, pair in auto_time.iteritems():
self.schedule_brightness(time_of_day, pair)
now = arrow.now().timestamp
t = arrow.get(pair[0]).timestamp
if abs(now - t) < nearest_time_to_now[0]:
nearest_time_to_now = (abs(now - t), pair[1], time_of_day)
LOG.info(nearest_time_to_now)
self.set_eye_brightness(nearest_time_to_now[1], speak=False)
tod = nearest_time_to_now[2]
if tod == 'Sunrise':
self.speak_dialog('auto.sunrise')
elif tod == 'Sunset':
self.speak_dialog('auto.sunset')
elif tod == 'Noon':
self.speak_dialog('auto.noon')
def _handle_eye_brightness_event(self, message):
""" wrapper for setting eye brightness from
eventscheduler
Args:
message (dict): messagebus message
"""
if self.auto_brightness is True:
time_of_day = message.data[0]
brightness = message.data[1]
self.cancel_scheduled_event(time_of_day)
self.set_eye_brightness(brightness, speak=False)
pair = self._get_auto_time()[time_of_day]
self.schedule_brightness(time_of_day, pair)
def need_custom_dialog(self, error=""):
""" handles dialog to retrieve custom color
Args:
error (str): to set speak_dialog to error
"""
if len(self.custom_rgb) == 0:
self.speak_dialog(
'{}need.custom'.format(error),
data={'color': 'red'},
expect_response=True)
elif len(self.custom_rgb) == 1:
self.speak_dialog(
'{}need.custom'.format(error),
data={'color': 'green'},
expect_response=True)
elif len(self.custom_rgb) == 2:
self.speak_dialog(
'{}need.custom'.format(error),
data={'color': 'blue'},
expect_response=True)
def set_converse(self, context):
""" to invoke the converse method
Args:
context (str): string to set context for converse
"""
self.should_converse = True
self.converse_context = context
def reset_converse(self):
""" set converse to false and empty converse_context """
self.should_converse = False
self.converse_context = None
def converse(self, utterances, lang='en-us'):
""" overrides MycroftSkill converse method. when return value is True,
any utterances after will be sent through the conveerse method
to be handled.
Args:
utterances (str): utterances said to mycroft
lang (str): languge of utterance (currently not used)
"""
utt = utterances[0]
if self.converse_context == 'need.color':
found_color = False
for color in self.color_dict:
if color in utt.lower():
self.set_eye_color(color=color)
found_color = True
if found_color is False:
self.speak_dialog('color.not.found')
self.reset_converse()
return True
elif self.converse_context == 'set.custom.color':
try:
value = int(utt)
if 0 <= value <= 255:
self.custom_rgb.append(value)
self.need_custom_dialog()
if len(self.custom_rgb) == 3:
self.set_eye_color(rgb=self.custom_rgb)
self.reset_converse()
return True
else:
raise
except Exception as e:
LOG.error(e)
self.need_custom_dialog('error.')
elif self.converse_context == 'need.brightness':
try:
brightness = int(utt.lower())
self.set_eye_brightness(brightness)
self.reset_converse()
return True
except Exception as e:
LOG.error(e)
self.speak_dialog('brightness.error', expect_response=True)
return self.should_converse
def stop(self):
pass
def create_skill():
return Mark1()