Skip to content

Commit 20abe94

Browse files
committed
ports: esp32: otto: call ping
1 parent 201cf26 commit 20abe94

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

ports/esp32/dump_settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
def _str_setting(val):
1212
return "'''{}'''".format(val)
1313

14+
def _bytes_setting(val):
15+
return "b'''{}'''".format(val)
16+
1417
def _int_setting(val):
1518
return str(int(val))
1619

@@ -26,6 +29,8 @@ def _int_setting(val):
2629
)
2730

2831
BACOTTO_SETTINGS = (
32+
('BACOTTO_OTP_SECRET', _bytes_setting),
33+
('BACOTTO_SERIAL', _str_setting),
2934
('BACOTTO_URL', _str_setting),
3035
)
3136

ports/esp32/modules/otp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class OTP(object):
88
"""
99
Base class for OTP handlers.
1010
"""
11-
def __init__(self, s, digits=6, digest=hashlib.sha256):
11+
def __init__(self, s, digits=6, digest=hashlib.sha1):
1212
"""
1313
:param s: secret in base32 format
1414
:type s: str

ports/esp32/modules/otto.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import settings
22
import network
3+
import otp
34
import urequests
45
import utime
56

@@ -32,12 +33,18 @@ def wlan_disconnect():
3233
return sta_if.isconnected()
3334

3435

35-
def call_bacotto():
36-
resp = urequests.get(settings.BACOTTO_URL)
37-
print('Wow! Bacotto replied:', resp.text)
36+
def call_bacotto(otp_gen):
37+
totp_tok = otp_gen.totp(utime.time(), interval=30)
38+
39+
resp = urequests.get(settings.BACOTTO_URL + '/ping', params={
40+
'otp': totp_tok,
41+
'serial': settings.BACOTTO_SERIAL,
42+
})
43+
print('Wow! Bacotto replied:', resp.status_code, resp.text)
3844

3945

4046
def run():
47+
otp_gen = otp.OTP(settings.BACOTTO_OTP_SECRET)
4148
sntp_setup = False
4249

4350
while True:
@@ -51,7 +58,7 @@ def run():
5158
sntp_setup = True
5259

5360
print('current timestamp:', utime.time())
54-
call_bacotto()
61+
call_bacotto(otp_gen)
5562
except Exception as e:
5663
print(e)
5764
utime.sleep_ms(1000)

ports/esp32/modules/urequests.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def json(self):
3434
return ujson.loads(self.content)
3535

3636

37-
def request(method, url, data=None, json=None, headers={}, stream=None):
37+
def request(method, url, params={}, data=None, json=None, headers={}):
3838
try:
3939
proto, dummy, host, path = url.split("/", 3)
4040
except ValueError:
@@ -55,43 +55,53 @@ def request(method, url, data=None, json=None, headers={}, stream=None):
5555
ai = usocket.getaddrinfo(host, port)
5656
addr = ai[0][-1]
5757

58+
if len(params) > 0:
59+
# TODO: add proper serialization
60+
path += '?' + '&'.join([k + '=' + params[k] for k in params.keys()])
61+
5862
s = usocket.socket()
5963
try:
6064
s.connect(addr)
6165
if proto == "https:":
6266
s = ussl.wrap_socket(s, server_hostname=host)
67+
6368
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
6469
if not "Host" in headers:
6570
s.write(b"Host: %s\r\n" % host)
71+
6672
# Iterate over keys to avoid tuple alloc
6773
for k in headers:
6874
s.write(k)
6975
s.write(b": ")
7076
s.write(headers[k])
7177
s.write(b"\r\n")
78+
7279
if json is not None:
7380
assert data is None
7481
import ujson
7582
data = ujson.dumps(json)
83+
7684
if data:
7785
s.write(b"Content-Length: %d\r\n" % len(data))
86+
7887
s.write(b"\r\n")
7988
if data:
8089
s.write(data)
8190

8291
l = s.readline()
8392
protover, status, msg = l.split(None, 2)
8493
status = int(status)
85-
#print(protover, status, msg)
94+
8695
while True:
8796
l = s.readline()
8897
if not l or l == b"\r\n":
8998
break
90-
#print(l)
99+
91100
if l.startswith(b"Transfer-Encoding:"):
92101
if b"chunked" in l:
93102
raise ValueError("Unsupported " + l)
94-
elif l.startswith(b"Location:") and not 200 <= status <= 299:
103+
104+
elif not (200 <= status <= 299) and l.startswith(b"Location:"):
95105
raise NotImplementedError("Redirects not yet supported")
96106
except OSError:
97107
s.close()

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