-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm_server.py
executable file
·157 lines (136 loc) · 4.14 KB
/
alarm_server.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import Pyro4
import time
from threading import Thread
class clock(Thread):
def __init__(self):
Thread.__init__(self)
self.alarm_on = False
self.alarm_set = False
self.snoozing = False
self.ns = Pyro4.locateNS(host='muffin',port=9090)
self.uri_relay = self.ns.lookup('relay')
self.uri_itunes = self.ns.lookup('itunes')
self.counter = 0
def set_time(self, settime):
self.alarm_set = True
self.time = settime
def run(self):
while True:
if self.alarm_set and not self.alarm_on and not self.snoozing:
if self.check_time():
self.wake()
if self.alarm_on:
self.counter += 1
#safety feature
if self.counter > 100:
self.stop_alarm()
time.sleep(10)
def check_time(self):
return self.time == [time.localtime().tm_hour,time.localtime().tm_min]
def wake(self):
self.counter = 0
#set system volume
os.system('osascript -e "set volume 2.5"')
with Pyro4.Proxy(self.uri_itunes) as itunes:
itunes.pause()
print 'good morning!'
self.alarm_on = True
with Pyro4.Proxy(self.uri_relay) as relay:
relay.set_state(True)
time.sleep(3)
with Pyro4.Proxy(self.uri_itunes) as itunes:
self.vol = int(itunes.volume)
itunes.volume = 0
itunes.play_playlist('morning')
for i in range(0,100):
itunes.volume = int(i)
time.sleep(0.05)
def snooze(self, minutes):
self.counter = 0
self.alarm_set = False
self.snoozing = True
print 'snoozing -.-'
for i in range(int(minutes*60)):
if self.alarm_on:
time.sleep(1)
else:
return False
with Pyro4.Proxy(self.uri_relay) as relay:
relay.set_state(True)
with Pyro4.Proxy(self.uri_itunes) as itunes:
itunes.volume
itunes.volume = 0
itunes.play()
for i in range(0,100):
itunes.volume = int(i)
time.sleep(0.02)
self.alarm_on = True
self.snoozing = False
def stop_alarm(self):
self.counter = 0
if self.alarm_on:
print 'alarm off'
with Pyro4.Proxy(self.uri_itunes) as itunes:
itunes.pause()
with Pyro4.Proxy(self.uri_relay) as relay:
relay.set_state(False)
with Pyro4.Proxy(self.uri_itunes) as itunes:
itunes.volume
itunes.volume = self.vol
self.alarm_on = False
def stop(self):
self.stopped = True
class alarm(object):
def __init__(self):
self._set = False
self._playing = False
self._snoozing = False
self._time = None
self.clock = clock()
self.clock.start()
def reset(self):
self.clock.stop_alarm()
self.clock.alarm_set = False
self.clock.stop()
del self.clock
self.__init__()
def stop(self):
self.clock.stop()
@Pyro4.expose
@property
def playing(self):
return self.clock.alarm_on
@Pyro4.expose
@property
def set(self):
return self._set
@Pyro4.expose
@property
def snoozing(self):
return self._snoozing
@Pyro4.expose
@property
def time(self):
return self._time
def snooze(self):
self._snoozing = True
self.clock.snooze(7)
def stop(self):
self._playing = False
self._snoozing = False
self.clock.stop_alarm()
def set_time(self, settime):
self._set = True
self._time = [int(i) for i in settime.split(':')]
self.clock.set_time(self._time)
print 'waking at %s:%s'%(self.time[0],self.time[1])
daemon = Pyro4.Daemon(host='muffin')
ns = Pyro4.locateNS()
uri = daemon.register(alarm())
ns.register("alarm", uri)
print("server object uri:", uri)
print("attributes server running.")
daemon.requestLoop()