-
Notifications
You must be signed in to change notification settings - Fork 0
/
wakeup.py
executable file
·132 lines (102 loc) · 3.94 KB
/
wakeup.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
#!/usr/bin/env python
"""
Get the events that match a search query from the nominated google calendar,
and schedule a wake sequence.
Adapted from Simple Google Calendar Alarm Clock by Bart Bania
"""
from pprint import pprint
from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta
from dateutil import parser
import httplib2
from oauth2client.file import Storage
import pytz
from lcdui import STATUS
from lib.net import wait_until_network
from presets import alarm_cycle
import settings
import logging
logging.basicConfig()
local = pytz.timezone(settings.TIMEZONE)
if settings.DEBUG:
CHECK_CALENDAR_EVERY = 60 #secs
else:
CHECK_CALENDAR_EVERY = 60*10 #secs
scheduler = Scheduler()
# set up a google calendar connection
from apiclient.discovery import build
from oauth2client import client
def establish_service():
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://www.googleapis.com/auth/calendar.readonly',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
storage = Storage('credentials.json')
credentials = storage.get()
if not credentials:
auth_uri = flow.step1_get_authorize_url()
import webbrowser
webbrowser.open_new(auth_uri)
auth_code = raw_input('Enter the auth code: ')
credentials = flow.step2_exchange(auth_code)
storage.put(credentials)
http_auth = credentials.authorize(httplib2.Http())
return build('calendar', 'v3', http_auth)
def check_calendar(service):
print 'Query for "%s" events on %s' % (settings.CALENDAR_QUERY, settings.CALENDAR_NAME)
request = service.events().list(
calendarId=settings.CALENDAR_NAME,
q=settings.CALENDAR_QUERY,
timeMax = local.localize(datetime.now() + timedelta(days=7)).isoformat(),
timeMin = local.localize(datetime.now()).isoformat(),
singleEvents=True)
response = request.execute()
#clear out current schedule of alarms - just in case any are deleted or changed
try:
scheduler.unschedule_func(do_alarm)
except KeyError:
pass
local_now = local.localize(datetime.now())
for event in response['items']:
parse_when = parser.parse(event['start']['dateTime'])
t = parse_when - timedelta(seconds=settings.PRE_WAKEUP_TIME)
if t > local_now:
if (STATUS.next_alarm is None) or (parse_when < STATUS.next_alarm):
STATUS.next_alarm = parse_when
print "%s: Scheduled alarm for %s ('%s') (starting %s)" % (datetime.now(), event['start']['dateTime'], event['summary'], t)
scheduler.add_cron_job(do_alarm, month=t.month, day=t.day, hour=t.hour, minute=t.minute, second=t.second)
def do_alarm():
STATUS.next_alarm = None
# local_now = local.localize(datetime.now())
# print "%s: ALARM" % local_now
[alarm_cycle(B) for B in settings.BULBS]
# #play a random mp3 file
# fn = random.choice(os.listdir(settings.AUDIO_PATH))
# print "Playing %s" % fn
# f = os.path.join(settings.AUDIO_PATH, fn)
#
# player = subprocess.Popen(["mplayer", f], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# player.stdin.write("volume 0 1\n")
#
# def _ramp_volume(volume):
# volume += 10
# volume = min(settings.MAX_VOLUME, volume)
# player.stdin.write("volume %s 1\n" % volume)
# if volume < settings.MAX_VOLUME:
# print volume
# t = Timer(0.5, _ramp_volume, (volume, ))
# t.start()
# _ramp_volume(0)
#
# player.wait()
def start_calendar_scheduler(service):
def cc():
check_calendar(service)
scheduler.add_interval_job(cc, seconds=CHECK_CALENDAR_EVERY) # define refresh rate.
scheduler.start() # runs the program indefinitely on an interval of x seconds
if __name__ == '__main__':
wait_until_network()
service = establish_service()
start_calendar_scheduler(service)
while True:
pass