1
+ from __future__ import print_function , division
2
+
3
+ # imports
4
+ from simplejson import load as load_json
5
+ from importlib import import_module
6
+ from datetime import datetime , timedelta
7
+ from bottle import route , request , run , static_file
8
+ from arrow import utcnow
9
+
10
+ from events import scheduler , emitter , ms
11
+
12
+ # load config.json data
13
+ with open ("config.json" ) as data :
14
+ config = load_json (data )
15
+
16
+ if "kit" in config :
17
+ print ("loading board config \" " , config ["kit" ], "\" from config.json" , sep = "" )
18
+ board = import_module (config ["kit" ])
19
+ else :
20
+ print ("loading default board config" )
21
+ board = import_module ("grove" )
22
+
23
+ # clock functions
24
+ current_time = utcnow ().replace (seconds = - 1 )
25
+ alarm_time = utcnow ().replace (days = - 1 )
26
+
27
+ def after (t , c ):
28
+ return t .floor ("second" ) > c .floor ("second" )
29
+
30
+ def same (t , c ):
31
+ return t .floor ("second" ) == c .floor ("second" )
32
+
33
+ def start_clock ():
34
+ global current_time
35
+ time = utcnow ()
36
+ if after (time , current_time ):
37
+ board .write_message (str (time .format ("h:mm:ss A" )))
38
+ if same (time , alarm_time ):
39
+ start_alarm ()
40
+
41
+ current_time = time
42
+
43
+ scheduler .add_job (start_clock , "interval" , seconds = ms (50 ))
44
+
45
+ def start_alarm ():
46
+ global alarm_time
47
+
48
+ alarm_state = {
49
+ "tick" : False
50
+ }
51
+
52
+ board .start_buzzer ()
53
+ board .set_screen_background ("red" )
54
+
55
+ def alarm_actions ():
56
+ tick = alarm_state ["tick" ]
57
+ board .set_screen_background ("white" if tick else "red" )
58
+ if tick :
59
+ board .stop_buzzer ()
60
+ else :
61
+ board .start_buzzer ()
62
+ alarm_state ["tick" ] = not tick
63
+
64
+ print ("schedule alarm_actions" )
65
+ alarm_interval = scheduler .add_job (alarm_actions , "interval" , seconds = ms (250 ))
66
+
67
+ def stop_alarm ():
68
+ global alarm_time
69
+ alarm_interval .remove ()
70
+ alarm_time = alarm_time .replace (days = 1 )
71
+ board .set_screen_background ("white" )
72
+ board .stop_buzzer ()
73
+
74
+ emitter .once ("button-pressed" , stop_alarm )
75
+
76
+ # server setup
77
+ alarm_duration = {
78
+ "hour" : 0 ,
79
+ "minute" : 0 ,
80
+ "second" : 0
81
+ }
82
+
83
+ @route ('/' )
84
+ def serve_index ():
85
+ global alarm_time
86
+ global alarm_duration
87
+ print ("query:" , request .query )
88
+ if { "hour" , "minute" , "second" } <= set (request .query ):
89
+ duration = {
90
+ "hour" : int (request .query .get ("hour" )) or 0 ,
91
+ "minute" : int (request .query .get ("minute" )) or 0 ,
92
+ "second" : int (request .query .get ("second" )) or 0
93
+ }
94
+ alarm_time = utcnow ().replace (hours = duration ["hour" ], minutes = duration ["minute" ], seconds = duration ["second" ])
95
+ alarm_duration = duration
96
+ print ("alarm set to go off at" , alarm_time .format ("h:mm:ss A" ))
97
+ return static_file ('index.html' , root = "" )
98
+
99
+ @route ('/alarm.json' )
100
+ def serve_json ():
101
+ payload = {
102
+ "hour" : alarm_duration ["hour" ],
103
+ "minute" : alarm_duration ["minute" ],
104
+ "second" : alarm_duration ["second" ]
105
+ }
106
+ return payload
107
+
108
+ def main ():
109
+ board .init_hardware (config )
110
+ board .stop_buzzer ()
111
+ start_clock ()
112
+ run (host = "0.0.0.0" , port = 5000 )
113
+
114
+ if __name__ == "__main__" :
115
+ main ()
0 commit comments