-
Notifications
You must be signed in to change notification settings - Fork 28
/
__init__.py
169 lines (143 loc) · 6.39 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
# 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 time
from mycroft import AdaptIntent, MycroftSkill, intent_handler
from mycroft.audio import wait_while_speaking
from mycroft.configuration.config import Configuration
from mycroft.messagebus.message import Message
class NapTimeSkill(MycroftSkill):
"""Skill to handle mycroft speech client listener sleeping."""
def __init__(self) -> None:
"""Initialize NapTimeSkill"""
super().__init__()
self.started_by_skill = False
self.sleeping = False
self.old_brightness = 30
self.disabled_confirm_listening = False
def initialize(self) -> None:
"""Perform final initialization once Skill class has loaded."""
self.add_event('mycroft.awoken', self.handle_awoken)
self.platform = self.config_core.get(
"enclosure").get("platform", "unknown")
self.wake_word = Configuration.get()['listener']['wake_word']
@intent_handler(AdaptIntent("NapTimeIntent").require("SleepCommand"))
def handle_go_to_sleep(self, _) -> None:
"""Sends a message to the speech client putting the listener to sleep.
If the user has been told about the waking up process five times
already, it sends a shorter message.
"""
count = self.settings.get('Wake up count', 0)
count += 1
self.settings['Wake up count'] = count
if count <= 5:
self.speak_dialog('going.to.sleep', {'wake_word': self.wake_word})
else:
self.speak_dialog('going.to.sleep.short')
self.perform_sleep()
def handle_awoken(self, _) -> None:
"""Handler for the mycroft.awoken message
The message is sent when the listener hears 'Hey Mycroft, Wake Up',
this handles the user interaction upon wake up.
"""
started_by_skill = self.started_by_skill
self.perform_awaken()
if started_by_skill:
# Announce that the unit is awake
self.speak_dialog("i.am.awake")
self.display_waking_face()
@intent_handler("wake-up.intent")
def handle_already_awake(self, _) -> None:
"""Handle requests to wake up when the system is already awake.
If the user thought the device was in a sleep mode and tries to wake
it up this prevents that going to the Common Query fallbacks.
"""
self.log.info("User tried to wake device, but device was not asleep.")
self.speak_dialog('already.awake')
def display_sleep_face(self) -> None:
"""Display the sleeping face depending on the platform."""
if self.gui.connected:
self.gui.show_page("resting.qml", override_idle=True)
elif self.platform == "mycroft_mark_1":
self.display_sleep_animation()
def display_sleep_animation(self) -> None:
"""Dim and look downward to 'go to sleep'."""
# TODO: Get current brightness from somewhere
self.old_brightness = 30
for i in range(0, (self.old_brightness - 10) // 2):
self.enclosure.eyes_brightness(self.old_brightness - i * 2)
time.sleep(0.15)
self.enclosure.eyes_look("d")
def display_waking_face(self) -> None:
"""Display the waking face depending on the platform."""
if self.gui.connected:
self.gui.remove_page("resting.qml")
self.gui.show_page("awake.qml", override_idle=5)
# TODO Screen not reverting after the specified 5 seconds.
# The following 2 lines shouldn't be needed. Remove when fixed.
time.sleep(5)
self.gui.release()
elif self.platform == 'mycroft_mark_1':
self.display_wake_up_animation()
def display_wake_up_animation(self) -> None:
"""Mild animation to come out of sleep from voice command.
Pop open eyes and wait a sec.
"""
self.enclosure.eyes_reset()
time.sleep(1)
self.enclosure.eyes_blink('b')
time.sleep(1)
# brighten the rest of the way
self.enclosure.eyes_brightness(self.old_brightness)
def perform_awaken(self) -> None:
"""Perform actions to wake system up."""
if self.platform != "unknown":
self.bus.emit(Message('mycroft.volume.unmute',
data={"speak_message": False}))
elif self.disabled_confirm_listening:
self.enable_confirm_listening()
self.sleeping = False
self.started_by_skill = False
def perform_sleep(self) -> None:
"""Perform actions to put system to sleep."""
self.bus.emit(Message('recognizer_loop:sleep'))
self.sleeping = True
self.started_by_skill = True
# TODO - Work out why this is here...
wait_while_speaking()
time.sleep(2)
wait_while_speaking()
self.display_sleep_face()
if self.platform != "unknown":
self.bus.emit(Message('mycroft.volume.mute',
data={"speak_message": False}))
elif self.config_core['confirm_listening']:
self.disable_confirm_listening()
def disable_confirm_listening(self) -> None:
"""Patch active mycroft configuration to disable listening beep."""
msg = Message('configuration.patch',
data={'config': {'confirm_listening': False}}
)
self.bus.emit(msg)
self.disabled_confirm_listening = True
self.log.info('Disabled listening chirp')
def enable_confirm_listening(self) -> None:
"""Patch active mycroft configuration to enable listening beep."""
msg = Message('configuration.patch',
data={'config': {'confirm_listening': True}}
)
self.bus.emit(msg)
self.disabled_confirm_listening = False
self.log.info('Enabled listening chirp again')
def create_skill():
return NapTimeSkill()