-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarm.py
More file actions
192 lines (165 loc) · 6.63 KB
/
Alarm.py
File metadata and controls
192 lines (165 loc) · 6.63 KB
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
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import Tkinter
from Tkinter import Label, Button, Frame
import time
import sys
import AlarmAPI
import AlarmGUI
import AlarmModel
from lazylights import *
from subprocess import Popen, PIPE
from threading import Timer
class SmartAlarm(Tkinter.Tk):
def __init__(self, root):
Tkinter.Tk.__init__(self, root)
self.root = root
self.settings = AlarmModel.Settings()
self.API = AlarmAPI.APIMethods()
self.view = AlarmGUI.AlarmView(root, self.settings, self)
self.alarmOn = False
self.sleepOn = False
self.sound = None
try:
self.bulbs = find_bulbs(expected_bulbs=1)
except:
print "No internet connection."
self.SUNSET_RED = ('SUNSET_RED', 0, 0.20, 0.95, 2000) #in HSV
self.MORNING_BLUE = ('MORNING_BLUE', 210, 0.20, 0.90, 2000) #in HSV
self.DAYLIGHT = ('DAYLIGHT', 0, 0, 1, 4500) #in Kelvin
self.COLORS = [self.SUNSET_RED, self.MORNING_BLUE, self.DAYLIGHT]
self.setLightColor('DAYLIGHT')
self.initialize()
self.attributes('-fullscreen', True)
#self.overrideredirect(True) #Gets rid of title bar
def initialize(self):
self.updateCalendar()
self.updateWeather()
alarmTuple = self.API.setAlarm(self.events, self.raining)
self.alarm = alarmTuple[0]
self.sleep = alarmTuple[1]
self.view.lAlarm.configure(text="Alarm Set For: " + str(self.alarm))
self.currTime = ''
self.GUITimer()
def GUITimer(self):
# get the current local time from the PC
newTime = time.strftime("%H:%M")
# if currTime string has changed, update it
if newTime != self.currTime:
#if '00' == newTime[3:]:
if True:
self.updateCalendar()
self.updateWeather()
alarmTuple = self.API.setAlarm(self.events, self.raining)
self.alarm = alarmTuple[0]
self.sleep = alarmTuple[1]
self.view.lAlarm.configure(text="Alarm Set For: " + str(self.alarm))
if newTime == self.alarm:
self.API.soundAlarm(self)
if newTime == self.sleep:
self.API.setLightColor('SUNSET_RED')
self.currTime = newTime
self.view.lTime.configure(text=self.currTime)
if self.alarmOn is True and self.sound is not None and self.sound.poll() is not None:
self.sound = Popen(['omxplayer', 'WakeMeUp.ogg'], stdin=PIPE)
if self.alarmOn is not True and self.sleepOn is not True and self.view.bLights['text'] is 'Alarm Off':
self.view.bLights['text']= 'Lights'
#calls itself after 5 seconds
self.after(5000, self.GUITimer)
def updateCalendar(self):
self.events = self.API.pullCalendar()
i = 0
for event in self.events:
if i == 0:
self.view.lEvent1.configure(text=event)
i += 1
elif i == 1:
self.view.lEvent2.configure(text=event)
i += 1
elif i == 2:
self.view.lEvent3.configure(text=event)
def updateWeather(self):
try:
weather = self.API.pullWeather()
self.view.lTempCurr.config(text = ('Temp: ' + weather["current_temp"] + 'F'))
self.view.lTempHigh.config(text = ('Hi: ' + weather["high_temp"] + 'F'))
self.view.lTempLow.config(text = ('Lo: ' + weather["low_temp"] + 'F'))
self.view.lPrecip.config(text = ('Precip: ' + weather["precip"] + '%'))
if int(weather["precip"]) > 70:
self.raining = True
else:
self.raining = False
except:
self.raining = False
def soundAlarm(self):
self.alarmOn = True
self.view.bLights.config(text='Alarm Off')
if self.sleepOn:
self.sleepOn = False
self.setLightColor('MORNING_BLUE')
if self.sound is not None:
self.sound.communicate('q')
self.sound = None
self.sound = Popen(['omxplayer', self.settings.alarmSound], stdin=PIPE)
def sleep(self):
if self.alarmOn:
self.alarmOn = False
set_power(self.bulbs, False)
self.bulbs_power = False
self.sleepOn = True
if self.sound is not None:
self.sound.communicate('q')
self.sound = None
self.sleepCallback = Timer(self.settings.sleepTimer, self.soundAlarm)
self.sleepCallback.start()
else:
self.soundAlarm()
def lights(self):
if self.alarmOn:
self.alarmOn = False
if self.sound is not None:
self.sound.communicate('q')
self.sound = None
elif self.bulbs_power:
set_power(self.bulbs, False)
self.bulbs_power = False
else:
if self.sleepOn:
self.setLightColor('MORNING_BLUE')
self.view.bLights.config(text='Lights')
self.sleepCallback.cancel()
self.sleepOn = False
else:
self.setLightColor('DAYLIGHT')
def setLightColor(self, color):
set_power(self.bulbs, True)
self.bulbs_power = True
lColor = self.COLORS[2]
for item in [item for item in self.COLORS if item[0] == color]:
lColor = item
set_state(self.bulbs, lColor[1], lColor[2], lColor[3], lColor[4], 0, raw=False)
self.bulbs_color = lColor[0]
def resetBulbs(self):
try:
self.bulbs = find_bulbs(expected_bulbs=1)
except:
print "No internet connection."
def saveSettings(self):
defaultHour = self.view.sDefaultHour.get()
defaultMinute = self.view.sDefaultMinute.get()
if not len(defaultHour) > 1:
defaultHour = '0' + defaultHour
if not len(defaultMinute) > 1:
defaultMinute = '0' + defaultMinute
self.settings.defaultAlarm = defaultHour + ':' + defaultMinute
self.settings.alarmSound = self.view.eDefaultSound.get()
self.settings.zipCode = self.view.eDefaultZipCode.get()
self.settings.save()
if __name__ == "__main__":
white = '#%02x%02x%02x' % (255, 255, 255)
app = SmartAlarm(None)
app.title('Alarm')
app.configure(bg = white)
app.tk_setPalette(background=white)
app.geometry("320x240+0+0")
app.mainloop()