Skip to content

Commit f935b20

Browse files
committed
otto: fetch projects from bacotto
1 parent b38c8d1 commit f935b20

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

ports/esp32/modules/otto.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _init_state(self):
4444

4545
# stub
4646
self.current_project = None
47-
self.project_list = ['Otto', 'Fuffa', 'Foobar', 'Gah']
47+
self.project_list = []
4848
self.project_idx = 0
4949

5050
# hours to send to bacotto
@@ -66,8 +66,9 @@ def display_initializing(self):
6666

6767
def display_project_list(self):
6868
# 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)
7172

7273
def display_project_time(self):
7374
delta = utime.time() - self.project_start_time
@@ -76,7 +77,7 @@ def display_project_time(self):
7677
project = self.current_project
7778

7879
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)),
8081
10, 30)
8182

8283
def display_body(self):
@@ -108,6 +109,7 @@ def run(self):
108109
self.wlan.connect()
109110

110111
self.setup_sntp()
112+
self.fetch_projects()
111113

112114
try:
113115
self.debug()
@@ -143,6 +145,18 @@ def setup_sntp(self):
143145
else:
144146
self.need_wifi_count += 1
145147

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+
146160
def debug(self):
147161
if self.debug_sock:
148162
print('debug: sending display buffer to', settings.DEBUG_HOST)
@@ -159,6 +173,7 @@ def send_hours(self):
159173

160174
if self.wlan.is_connected():
161175
print('sending hours to bacotto')
176+
# self.call_bacotto('POST', 'register', {})
162177
self.projects_hours = []
163178
self.need_wifi_count -= 1
164179
else:
@@ -169,13 +184,14 @@ def ping_bacotto(self):
169184
if self.wlan.is_connected():
170185
self.bacotto_ping_counter = 0
171186
self.need_wifi_count -= 1
187+
self.call_bacotto('GET', '/ping', {})
172188
self.call_bacotto()
173189
else:
174190
self.need_wifi_count += 1
175191
else:
176192
self.bacotto_ping_counter += 1
177193

178-
def call_bacotto(self):
194+
def call_bacotto(self, method, path, params):
179195
if not self.wlan.is_connected():
180196
return
181197

@@ -185,12 +201,20 @@ def call_bacotto(self):
185201
print('current timestamp:', now)
186202
totp_tok = self.otp_gen.totp(utime.time(), interval=30)
187203

188-
resp = urequests.get(settings.BACOTTO_URL + '/ping', params={
204+
params.update({
189205
'otp': totp_tok,
190206
'serial': settings.BACOTTO_SERIAL,
191207
})
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+
192214
print('Wow! Bacotto replied:', resp.status_code, resp.text)
193215

216+
return resp
217+
194218

195219
def _pretty_digit(d):
196220
if d <= 9:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy