-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path__init__.py
110 lines (92 loc) · 3.68 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
# 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 time
from alsaaudio import Mixer
from datetime import datetime, timedelta
import yaml
from adapt.intent import IntentBuilder
from os.path import dirname, join
from mycroft.skills.scheduled_skills import ScheduledCRUDSkill
from mycroft.util import play_mp3
__author__ = 'jdorleans'
# TODO - Localization
class AlarmSkill(ScheduledCRUDSkill):
def __init__(self):
super(AlarmSkill, self).__init__("AlarmSkill", None)
self.alarm_on = False
self.max_delay = self.config.get('max_delay')
self.repeat_time = self.config.get('repeat_time')
self.extended_delay = self.config.get('extended_delay')
self.file_path = join(dirname(__file__), self.config.get('filename'))
def initialize(self):
super(AlarmSkill, self).initialize()
intent = IntentBuilder(
'AlarmSkillStopIntent').require('AlarmSkillStopVerb') \
.require('AlarmSkillKeyword').build()
self.register_intent(intent, self.__handle_stop)
def load_data(self):
try:
with self.file_system.open(self.PENDING_TASK, 'r') as f:
self.data = yaml.safe_load(f)
assert self.data
except:
self.data = {}
def load_repeat_data(self):
try:
with self.file_system.open(self.REPEAT_TASK, 'r') as f:
self.repeat_data = yaml.safe_load(f)
assert self.repeat_data
except:
self.repeat_data = {}
def __handle_stop(self, message):
if self.alarm_on:
self.speak_dialog('alarm.off')
self.alarm_on = False
def notify(self, timestamp):
with self.LOCK:
if self.data.__contains__(timestamp):
volume = None
self.alarm_on = True
delay = self.__calculate_delay(self.max_delay)
while self.alarm_on and datetime.now() < delay:
play_mp3(self.file_path).communicate()
self.speak_dialog('alarm.stop')
time.sleep(self.repeat_time + 2)
if not volume and datetime.now() >= delay:
mixer = Mixer()
volume = mixer.getvolume()[0]
mixer.setvolume(100)
delay = self.__calculate_delay(self.extended_delay)
if volume:
Mixer().setvolume(volume)
self.remove(timestamp)
self.alarm_on = False
self.save()
@staticmethod
def __calculate_delay(seconds):
return datetime.now() + timedelta(seconds=seconds)
def save(self):
with self.file_system.open(self.PENDING_TASK, 'w') as f:
yaml.safe_dump(self.data, f)
with self.file_system.open(self.REPEAT_TASK, 'w') as f:
yaml.safe_dump(self.repeat_data, f)
if not self.alarm_on:
self.schedule()
def stop(self):
self.__handle_stop(None)
def create_skill():
return AlarmSkill()