@@ -44,7 +44,7 @@ def _init_state(self):
44
44
45
45
# stub
46
46
self .current_project = None
47
- self .project_list = ['Otto' , 'Fuffa' , 'Foobar' , 'Gah' ]
47
+ self .project_list = []
48
48
self .project_idx = 0
49
49
50
50
# hours to send to bacotto
@@ -66,8 +66,9 @@ def display_initializing(self):
66
66
67
67
def display_project_list (self ):
68
68
# TODO: scrolling
69
- for i , p in enumerate (self .project_list [:3 ], start = 1 ):
70
- self .display .text (p , 5 , 15 + i * 10 )
69
+ for i , prj in enumerate (self .project_list [:3 ], start = 1 ):
70
+ name = prj ['ShortName' ]
71
+ self .display .text (name , 5 , 15 + i * 10 )
71
72
72
73
def display_project_time (self ):
73
74
delta = utime .time () - self .project_start_time
@@ -76,7 +77,7 @@ def display_project_time(self):
76
77
project = self .current_project
77
78
78
79
self .display .text (
79
- "%s %s:%s" % (project , _pretty_digit (hours ), _pretty_digit (mins )),
80
+ "%s %s:%s" % (project [ 'ShortName' ] , _pretty_digit (hours ), _pretty_digit (mins )),
80
81
10 , 30 )
81
82
82
83
def display_body (self ):
@@ -108,6 +109,7 @@ def run(self):
108
109
self .wlan .connect ()
109
110
110
111
self .setup_sntp ()
112
+ self .fetch_projects ()
111
113
112
114
try :
113
115
self .debug ()
@@ -143,6 +145,18 @@ def setup_sntp(self):
143
145
else :
144
146
self .need_wifi_count += 1
145
147
148
+ def fetch_projects (self ):
149
+ # TODO: invalidate the list of projects
150
+ if self .project_list :
151
+ return
152
+
153
+ if self .wlan .is_connected ():
154
+ print ('fetching projects...' )
155
+ self .project_list = self .call_bacotto ('GET' , '/projects' , {}).json ()
156
+ self .need_wifi_count -= 1
157
+ else :
158
+ self .need_wifi_count += 1
159
+
146
160
def debug (self ):
147
161
if self .debug_sock :
148
162
print ('debug: sending display buffer to' , settings .DEBUG_HOST )
@@ -159,6 +173,7 @@ def send_hours(self):
159
173
160
174
if self .wlan .is_connected ():
161
175
print ('sending hours to bacotto' )
176
+ # self.call_bacotto('POST', 'register', {})
162
177
self .projects_hours = []
163
178
self .need_wifi_count -= 1
164
179
else :
@@ -169,13 +184,14 @@ def ping_bacotto(self):
169
184
if self .wlan .is_connected ():
170
185
self .bacotto_ping_counter = 0
171
186
self .need_wifi_count -= 1
187
+ self .call_bacotto ('GET' , '/ping' , {})
172
188
self .call_bacotto ()
173
189
else :
174
190
self .need_wifi_count += 1
175
191
else :
176
192
self .bacotto_ping_counter += 1
177
193
178
- def call_bacotto (self ):
194
+ def call_bacotto (self , method , path , params ):
179
195
if not self .wlan .is_connected ():
180
196
return
181
197
@@ -185,12 +201,20 @@ def call_bacotto(self):
185
201
print ('current timestamp:' , now )
186
202
totp_tok = self .otp_gen .totp (utime .time (), interval = 30 )
187
203
188
- resp = urequests . get ( settings . BACOTTO_URL + '/ping' , params = {
204
+ params . update ( {
189
205
'otp' : totp_tok ,
190
206
'serial' : settings .BACOTTO_SERIAL ,
191
207
})
208
+
209
+ if method in ('POST' , 'PUT' ):
210
+ resp = urequests .request (method , settings .BACOTTO_URL + path , json = params )
211
+ else :
212
+ resp = urequests .request (method , settings .BACOTTO_URL + path , params = params )
213
+
192
214
print ('Wow! Bacotto replied:' , resp .status_code , resp .text )
193
215
216
+ return resp
217
+
194
218
195
219
def _pretty_digit (d ):
196
220
if d <= 9 :
0 commit comments