fix rgb exception and wait before catch record

This commit is contained in:
2026-08-18 18:25:02 +05:00
parent f10f9de524
commit 34164ffc33
2 changed files with 16 additions and 8 deletions
+9 -6
View File
@@ -1,8 +1,11 @@
import wifi_server try:
from machine import Pin from machine import Pin
from neopixel import NeoPixel from neopixel import NeoPixel
np = NeoPixel(Pin(8), 1) np = NeoPixel(Pin(8), 1)
np[0] = (0, 0, 0) np[0] = (0, 0, 0)
np.write() np.write()
except Exception as e:
print("rgb off failed", e)
import wifi_server
wifi_server.main() wifi_server.main()
+7 -2
View File
@@ -53,6 +53,9 @@ def delete_code(name, filename=CODES_FILE):
def capture_and_save(name, timeout_ms=5000, idle_us=8000, recv_pin=None): def capture_and_save(name, timeout_ms=5000, idle_us=8000, recv_pin=None):
from capture import capture_raw, RECV_PIN from capture import capture_raw, RECV_PIN
pin = recv_pin if recv_pin is not None else RECV_PIN pin = recv_pin if recv_pin is not None else RECV_PIN
time.sleep_ms(200)
timings = capture_raw(pin_num=pin, timeout_ms=timeout_ms, idle_us=idle_us) timings = capture_raw(pin_num=pin, timeout_ms=timeout_ms, idle_us=idle_us)
if not timings: if not timings:
return False, 0 return False, 0
@@ -142,8 +145,10 @@ def handle_request(path, headers, api_key):
ok, count = capture_and_save(name, timeout_ms=timeout_ms) ok, count = capture_and_save(name, timeout_ms=timeout_ms)
if ok: if ok:
return 200, {"status": "ok", "saved": name, "pulses": count} return 200, {"status": "ok", "saved": name, "pulses": count}
else: elif count == 0:
return 408, {"error": "сигнал не обнаружен (таймаут)"} return 408, {"error": "сигнал не обнаружен (таймаут)"}
else:
return 422, {"error": f"сигнал похож на помеху ({count} импульсов, ожидается от 6)"}
except Exception as e: except Exception as e:
return 500, {"error": str(e)} return 500, {"error": str(e)}
@@ -209,7 +214,7 @@ def run_server(ip, api_key=None):
body_bytes = body_json.encode("utf-8") body_bytes = body_json.encode("utf-8")
status_text = {200: "OK", 400: "Bad Request", 401: "Unauthorized", 404: "Not Found", status_text = {200: "OK", 400: "Bad Request", 401: "Unauthorized", 404: "Not Found",
408: "Request Timeout", 500: "Internal Server Error"}.get(status, "OK") 408: "Request Timeout", 422: "Unprocessable Entity", 500: "Internal Server Error"}.get(status, "OK")
response_headers = ( response_headers = (
f"HTTP/1.1 {status} {status_text}\r\n" f"HTTP/1.1 {status} {status_text}\r\n"