feat: ESP-NOW link, beat sync and control messages in the C++ driver
- espnow_link, ctrl_msg and beat_sync: the reliable link, envelopes and beat-synced timing. - ctrl_msg: accept an envelope with the trailing save byte. - patterns_frame() and espnow_link_poll() split out of their tasks, so the WebAssembly simulator can drive one frame and one link pass from a timer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94e300f6ad
commit
24dd5ffb15
30 files changed
+2159
-158
No files matched your search
Executable
+112
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Move bars to another Wi-Fi network (SSID), one at a time, checking each comes back.
|
||||
|
||||
Usage:
|
||||
tools/move_bars.py --ssid=lighting-b 109 110 111 112 113 114 115 116
|
||||
tools/move_bars.py --ssid=lighting-b --password-env=WIFI_PASS 109 # password from an environment variable
|
||||
tools/move_bars.py --ssid=lighting-b --dry-run 109 # show what would change, send nothing
|
||||
|
||||
Bars are the last octet (109) or a full address (192.168.50.109).
|
||||
|
||||
The password is asked for at a prompt (hidden), or read from the environment variable named by
|
||||
--password-env. It is never taken from the command line, so it does not land in the shell history or the
|
||||
process list. The same password is used for the new network and, if a bar does not come back, for putting
|
||||
it back on the network it was on, so both networks need the same password.
|
||||
|
||||
Each bar is changed, then polled until it reports the new SSID and is connected. The first bar that does
|
||||
not come back is put back on its old network and the run stops, so a mistake costs one bar, not all of them.
|
||||
Run this from a machine that is on the LED subnet.
|
||||
"""
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
PREFIX = "192.168.50."
|
||||
COME_BACK_S = 45 # how long a bar has to join the new network
|
||||
POLL_S = 2
|
||||
|
||||
|
||||
def call(host, path, body=None, timeout=5):
|
||||
req = urllib.request.Request(f"http://{host}{path}")
|
||||
if body is not None:
|
||||
req.add_header("Content-Type", "application/json")
|
||||
req.data = json.dumps(body).encode()
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode() or "{}")
|
||||
|
||||
|
||||
def wifi_state(host):
|
||||
try:
|
||||
return call(host, "/api/wifi", timeout=3)
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def switch(host, ssid, password):
|
||||
"""Tell a bar to join ssid and wait for it to report that it has. True if it did."""
|
||||
try:
|
||||
call(host, "/api/wifi", {"ssid": ssid, "password": password})
|
||||
except OSError:
|
||||
pass # the bar may drop the connection as it switches; the poll below tells
|
||||
end = time.time() + COME_BACK_S
|
||||
time.sleep(POLL_S)
|
||||
while time.time() < end:
|
||||
st = wifi_state(host)
|
||||
if st and st.get("ssid") == ssid and st.get("connected"):
|
||||
return True
|
||||
time.sleep(POLL_S)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
args = sys.argv[1:]
|
||||
opts = {a.split("=", 1)[0]: (a.split("=", 1)[1] if "=" in a else True) for a in args if a.startswith("--")}
|
||||
names = [a for a in args if not a.startswith("--")]
|
||||
ssid = opts.get("--ssid")
|
||||
if not isinstance(ssid, str) or not names:
|
||||
print(__doc__)
|
||||
return 2
|
||||
hosts = [n if "." in n else PREFIX + n for n in names]
|
||||
dry = "--dry-run" in opts
|
||||
|
||||
password = ""
|
||||
if not dry:
|
||||
env = opts.get("--password-env")
|
||||
password = os.environ.get(env, "") if isinstance(env, str) else getpass.getpass(f"Wi-Fi password for {ssid} (hidden): ")
|
||||
if len(password) != 0 and not 8 <= len(password) <= 64:
|
||||
print("The password must be empty or 8-64 characters.")
|
||||
return 2
|
||||
|
||||
moved = 0
|
||||
for h in hosts:
|
||||
st = wifi_state(h)
|
||||
if st is None:
|
||||
print(f"{h}: no answer, skipped")
|
||||
continue
|
||||
old = st.get("ssid", "")
|
||||
if old == ssid and st.get("connected"):
|
||||
print(f"{h}: already on {ssid}")
|
||||
continue
|
||||
if dry:
|
||||
print(f"{h}: would move from {old!r} to {ssid!r}")
|
||||
continue
|
||||
print(f"{h}: {old!r} -> {ssid!r} ...", end=" ", flush=True)
|
||||
if switch(h, ssid, password):
|
||||
print("ok")
|
||||
moved += 1
|
||||
continue
|
||||
print("did not come back; putting it back on", repr(old))
|
||||
if old and switch(h, old, password):
|
||||
print(f"{h}: back on {old!r}. Stopping, the other bars were not touched.")
|
||||
else:
|
||||
print(f"{h}: NOT reachable. Power-cycle it; it keeps the last network it was given.")
|
||||
return 1
|
||||
print(f"{moved} bar(s) moved")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user