-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.py
301 lines (243 loc) · 10.8 KB
/
alarm.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
import configparser
import datetime
import time
import ledenet_api
import webcolors
import os
from mplayer import Player
class alarmset:
def __init__(self):
self.list = []
def __iter__(self):
return iter(self.list)
def __len__(self):
return len(self.list)
def __getitem__(self,index):
if isinstance (index, int):
return self.list[index]
elif isinstance (index, str):
return self[tuple(a.name for a in self).index(index)]
else:
return None
def add(self, alarm):
if self.exists(alarm.name):
raise DuplicateNameError(alarm.name, ': this name allready exists')
self.list.append(alarm)
def remove(self, alarm):
if self.exists(alarm.name):
self.list.remove(alarm)
def exists(self, name):
return name in (a.name for a in self)
def generate_name(self):
# .generate_name() generates a name in the format alarm# with the first
# integer available
alarm_exists = True
i = 0
while alarm_exists:
i += 1
name ='alarm' + str(i)
alarm_exists = self.exists(name)
return name
def run(self):
''' run checks the time every 10 secs and starts the alarm and the wake-up light
if the current time matches the alarm time'''
format = "%Y-%m-%d %H:%M"
while True:
now = datetime.datetime.now()
nowstring = now.strftime(format)
for a in self:
next = a.next_alarm(now)
if next != None:
# correct the time of the next alarm for the onset time of the color
#led strip
next -= datetime.timedelta(seconds=a.color_onset)
if next.strftime(format) == nowstring:
a.wakeup_light()
a.play()
time.sleep(10)
def gettrack(self):
''' gettrack() return the name of the track that is currently playing'''
track = None
for a in self:
if a.track != None:
track = a.track
return track
def next_alarm(self, now):
''' next(now) returns the alarm object that is the
nearest next alarm time'''
newlist = []
for a in self:
if a.next_alarm(now) != None:
newlist.append(a)
newlist.sort(key= lambda a: a.next_alarm(now))
return newlist[0] if len(newlist) >0 else None
def load_alarms (self, filename = 'alarms.conf'):
'''load all the alarms in an ini file
'''
# first open the file alarms using the module configparser
# and collects a set of calendar recurrences
file = configparser.ConfigParser()
file.read(filename)
# now loop through each section and determine what the next alarm is based
# on the recurrences set (if any). Note that a recurrence set in the past
# returns a date in the past.
for section in file.sections():
name = section
time = datetime.datetime.strptime(file.get(section, 'time'),'%H:%M').time()
days = tuple(file.get(section, 'days').replace(' ', '').upper().split(','))
path = file.get(section, 'path')
volume = file.getint(section, 'volume')
if file.has_option(section, 'date'):
date = datetime.datetime.strptime(file.get(section, 'date'),'%d-%b-%Y').date()
else:
date = None
active = file.getboolean(section, 'active')
color_onset = file.getint(section, 'color_onset')
duration = file.getint(section, 'duration')
color = file.get(section, 'color')
#bulb_ip = file.get(section, 'bulb_ip')
self.add(alarm(section, time, days, date, path, volume, active, color_onset, duration, color))
def save_alarms (self, filename):
'''save all the alarms to the ini file'''
file = configparser.ConfigParser()
for a in self:
file.add_section(a.name)
file.set(a.name,'time', a.time.strftime('%H:%M'))
file.set(a.name,'days', ', '.join(a.days))
if a.date != None: file.set(a.name,'date', a.date.strftime('%d-%b-%Y'))
active = 'True' if a.active else 'False'
file.set(a.name,'active', active)
file.set(a.name,'path', a.path)
file.set(a.name,'volume', str(a.volume))
file.set(a.name,'color_onset', str(a.color_onset))
file.set(a.name,'duration', str(a.duration))
file.set(a.name,'color', a.color)
file.set(a.name,'bulb_ip', a.bulb_ip)
with open(filename,'w', encoding='utf-8') as fileout:
file.write(fileout)
def stop(self):
for alarm in self:
alarm.stop()
class alarm:
dowlist = 'MO,TU,WE,TH,FR,SA,SU'.split(',')
dow = {d:i for i,d in enumerate(dowlist)}
def __init__(self, name, time, days = ('MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'), date = None, path = './playlist/',
volume = 40, active = True, color_onset = 300, duration = -1, color = 'FFFFFF' ):
'''define an alarm. at the time of the alarm, a media file is played
required parameters is time (datetime.time)
optional parameters days, the days of the week for the alarm.
days is one or more of 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'
date is the date for a specific alarm. date overrides the days parameter
path is the path to media files
if active is True, the alarm will play otherwise it will not. '''
self.name = name
self.time = time
self.days = days
self.date = date
self.repeating = True if date == None else False
self.active = active
self.volume = volume
self.path = path +'/'if path[-1] != '/' else path # add a / to the end of the path if not present
self.color_onset = color_onset # onset is the time before the alarm for sunrise
self.duration = duration # duration of the alarm. -1 is manual off only
self.color = color
self.bulb_ip = '192.168.1.162'
# these variables are not passed when initiating the alarm, but are used for housekeeping
self.chkbox = {d:b for b,d in zip(('checked' if t in self.days else '' for t in self.dowlist),self.dowlist)}
# used by the module web.py to format the ceckboxes for the recurrence
self.playing = False
self.track = None
def update_alarm(self, **kwargs):
'''update the the alarm. the function takes 0 or more inputs from the list
name, time, days, date, path and repeating
'''
self.name = kwargs.get('name', self.name)
self.time = kwargs.get('time', self.time)
self.days = kwargs.get('days', self.days)
self.date = kwargs.get('date', self.date)
self.repeating = kwargs.get('repeating', self.repeating)
self.path = kwargs.get('path', self.path)
self.volume = kwargs.get('volume', self.volume)
self.active = kwargs.get ('active', self.active)
self.color = kwargs.get('color', self.color)
self.color_onset = kwargs.get('color_onset', self.color_onset)
self.duration = kwargs.get('duration', self.duration)
# housekeeping
if self.path[-1] != '/':
self.path += '/'
self.chkbox = {d:b for b,d in zip(('checked' if t in self.days else '' for t in self.dowlist),self.dowlist)}
self.volume = max(min(100, self.volume), 0) # volume should be between 0 and 100
def next_alarm(self, now):
'''alarm.next(now) returns the next occurence of an alarm after the input
eg alarm(time(10,0,0)).next(datetime(2015,1,1,11,0,0))
-> the next alarm at 10am if today is 11:00AM on Jan 1st 2015
will return
datetime(2015,1,2,10,0,0) -> 10:00 AM on Jan 2nd 2015'''
#offset now by 60 seconds so that next(now) = now
now += datetime.timedelta(0,-60)
# first determine the start date...
# which is either a specific date (ie self.date != None
# tomorrow (if now.time is past alarm.time
# or today (in all other cases)
if not self.active:
return None
if not self.repeating:
date = self.date
if self.date == None or datetime.datetime.combine(date, self.time) < now:
return None
elif now.time() > self.time:
date = now.date() + datetime.timedelta(1)
else:
date = now.date()
next_date = datetime.datetime.combine(date, self.time)
#map the weekday numbers to the days in alarm.days, so that 'MO' = 0, TU = 1
#then increment the current date until the current dow matches a day in alarm.days
wkdays = set(alarm.dow[x] for x in self.days)
while next_date.weekday() not in wkdays:
next_date += datetime.timedelta(1)
return next_date
def generate_playlist(self, path=None, ext = ('.mp3','.wav','.flac')):
'''generates a list of files in path
that ends with extension ext (default = .mp3, .wav and .flac'''
if path == None:
path = self.path
return sorted(list(filter(lambda x: x.endswith(ext), os.listdir(path))))
def play(self):
playlist = self.generate_playlist()
path = self.path
self.player = Player()
self.playing = True
while len(playlist) >0:
track = playlist.pop(0)
self.player.loadfile(path+track)
i=0
while self.player.time_pos == None:
time.sleep(0.1)
print ('loading', path+track, i, end='\r')
i+=1
print()
self.player.volume = self.volume
while self.player_active():
print (path+track, self.player.time_pos, end='\r')
time.sleep(1)
self.stop()
def wakeup_light(self):
r, g, b = webcolors.hex_to_rgb('#'+self.color)
bulb = ledenet_api.bulb(self.bulb_ip)
bulb.gradual(r,g,b,self.color_onset)
def player_active(self):
if self.playing:
return self.player.time_pos != None
else:
return False
def stop(self):
self.turn_off_light()
self.track = None
if self.playing:
self.playing = False
self.player.quit()
def turn_off_light(self):
bulb = ledenet_api.bulb(self.bulb_ip)
bulb.turn_off()
class DuplicateNameError(LookupError):
pass