From 2f3ccf14db7e716b7e66bfe3626eb161ac74d65e Mon Sep 17 00:00:00 2001 From: Daniel Svitan Date: Sun, 1 Jun 2025 12:30:42 +0200 Subject: [PATCH] :bug: Fixes cannot update state --- .gitignore | 1 + peripheral/.gitignore | 2 ++ peripheral/main.py | 14 ++++++++++++-- server/main.go | 1 - 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c595809..ca91d34 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,4 @@ flake.lock # Before adding new lines, see the comment at the top. .env* +.idea/ diff --git a/peripheral/.gitignore b/peripheral/.gitignore index b7869dd..89e08f3 100644 --- a/peripheral/.gitignore +++ b/peripheral/.gitignore @@ -1 +1,3 @@ +__pycache__/ + .env.json diff --git a/peripheral/main.py b/peripheral/main.py index b77137e..2f57e11 100644 --- a/peripheral/main.py +++ b/peripheral/main.py @@ -90,6 +90,14 @@ class App: print(f"Connected with IP {wlan.ifconfig()[0]}") self.update_server() + def health_check_server(self): + print("Health checking server...", end="\r") + try: + r = urequests.get(f"{self.server}/") + print(f"Server healthy [{r.status_code}]{" " * 8}") + except Exception as e: + print(f"Error occurred: {e}") + def update_server(self): print("Updating state...", end="\r") data = {"opened": self.opened} @@ -101,7 +109,7 @@ class App: headers={"Authorization": self.token, "Content-Type": "application/json"}, data=raw ) - print(f"State updated [{r.status_code}]") + print(f"State updated [{r.status_code}] {r.content.decode()}") except Exception as e: print(f"Error occurred: {e}") @@ -136,7 +144,7 @@ class App: distance = self.measure_distance() self.opened = distance >= THRESHOLD_DISTANCE - if not opened: + if not self.opened: self.led.low() self.ultra_opened_counter = 0 @@ -154,7 +162,9 @@ class App: utime.sleep_ms(100) self.led.toggle() utime.sleep_ms(400) + i = 0 + self.health_check_server() else: utime.sleep_ms(500) diff --git a/server/main.go b/server/main.go index ec6bd60..02a8e00 100644 --- a/server/main.go +++ b/server/main.go @@ -130,7 +130,6 @@ func main() { } if data.Opened == opened { - mut.Unlock() return c.NoContent(http.StatusOK) }