feat: ping drivers and push groups from the devices modal
POST /devices/ping broadcasts an ESP-NOW PING_REQ, collects the drivers' PING_RSP for timeout_s and registers or renames the ones that answer. A reply also counts as the driver being heard, so group updates go to it again. POST /devices/groups sends every ESP-NOW driver its groups and reports the acks. The devices modal gets the Ping drivers and Update groups buttons from main, with per-driver ping dots. UI files are now served with Cache-Control: no-cache and the firmware build hash as ETag, so a browser picks up a new UI right after an update instead of a day later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7f238aebc
commit
19fea0b0a1
10 files changed
+559
-29
No files matched your search
+67
-10
@@ -814,31 +814,40 @@ static esp_err_t send_cmd(const uint8_t mac[6], const uint8_t *env, size_t len,
|
||||
// GROUPS packet: the ids of every zone whose driver names include `name`. An empty list replaces
|
||||
// whatever the driver stored, so it is only sent when allow_empty is set (a zone or device was edited);
|
||||
// a driver that merely announced itself and is in no zone keeps the groups it already has.
|
||||
static void send_groups(const uint8_t mac[6], const char *name, bool allow_empty)
|
||||
// GROUPS packet for a driver: [4C][02][count] then per zone [len][zone id]. Returns its length;
|
||||
// *count gets the number of zones.
|
||||
static size_t build_groups(uint8_t pkt[REL_FRAG_MAX], const char *name, int *count)
|
||||
{
|
||||
uint8_t pkt[REL_FRAG_MAX];
|
||||
size_t off = 3;
|
||||
int count = 0;
|
||||
*count = 0;
|
||||
const cJSON *z;
|
||||
cJSON_ArrayForEach(z, store_coll("zone"))
|
||||
{
|
||||
const cJSON *names = cJSON_GetObjectItem(z, "names");
|
||||
size_t gl = strlen(z->string);
|
||||
if (cJSON_IsArray(names) && name_seen(names, cJSON_GetArraySize(names), name) && off + 1 + gl <= sizeof(pkt) &&
|
||||
if (cJSON_IsArray(names) && name_seen(names, cJSON_GetArraySize(names), name) && off + 1 + gl <= REL_FRAG_MAX &&
|
||||
gl < 16) {
|
||||
pkt[off++] = gl;
|
||||
memcpy(pkt + off, z->string, gl);
|
||||
off += gl;
|
||||
count++;
|
||||
(*count)++;
|
||||
}
|
||||
}
|
||||
if (count == 0 && !allow_empty) {
|
||||
return;
|
||||
}
|
||||
pkt[0] = 0x4C;
|
||||
pkt[1] = 0x02; // GROUPS
|
||||
pkt[2] = count;
|
||||
rel_send(mac, pkt, off, false, 1);
|
||||
pkt[2] = *count;
|
||||
return off;
|
||||
}
|
||||
|
||||
static void send_groups(const uint8_t mac[6], const char *name, bool allow_empty)
|
||||
{
|
||||
uint8_t pkt[REL_FRAG_MAX];
|
||||
int count;
|
||||
size_t len = build_groups(pkt, name, &count);
|
||||
if (count == 0 && !allow_empty) {
|
||||
return;
|
||||
}
|
||||
rel_send(mac, pkt, len, false, 1);
|
||||
}
|
||||
|
||||
static bool hex_mac(const char *s, uint8_t mac[6])
|
||||
@@ -902,6 +911,54 @@ void engine_groups_changed(void)
|
||||
store_unlock();
|
||||
}
|
||||
|
||||
#define PUSH_MAX 32
|
||||
|
||||
void engine_groups_push_all(int *total, int *sent, int *failed)
|
||||
{
|
||||
struct {
|
||||
uint8_t mac[6];
|
||||
uint16_t seq;
|
||||
bool queued;
|
||||
} out[PUSH_MAX];
|
||||
int n = 0;
|
||||
store_lock();
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
const cJSON *name = cJSON_GetObjectItem(d, "name"), *addr = cJSON_GetObjectItem(d, "address");
|
||||
const cJSON *tr = cJSON_GetObjectItem(d, "transport");
|
||||
const char *hex = cJSON_IsString(addr) ? addr->valuestring : d->string;
|
||||
if (n < PUSH_MAX && cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") &&
|
||||
hex_mac(hex, out[n].mac)) {
|
||||
uint8_t pkt[REL_FRAG_MAX];
|
||||
int count;
|
||||
size_t len = build_groups(pkt, name->valuestring, &count);
|
||||
out[n].queued = false;
|
||||
for (int i = 0; i < 400 && !out[n].queued; i++) { // wait for room, as tx() does
|
||||
esp_err_t err = rel_send_seq(out[n].mac, pkt, len, false, 1, &out[n].seq);
|
||||
out[n].queued = err == ESP_OK;
|
||||
if (err != ESP_ERR_NO_MEM) {
|
||||
break;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(5));
|
||||
}
|
||||
n++;
|
||||
}
|
||||
}
|
||||
store_unlock();
|
||||
// All are in flight at once; then collect each outcome (rel gives up after about 2 s)
|
||||
*total = n;
|
||||
*sent = *failed = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
bool ok = out[i].queued && rel_wait(out[i].mac, out[i].seq, 3000) == ESP_OK;
|
||||
if (ok) {
|
||||
(*sent)++;
|
||||
} else {
|
||||
(*failed)++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A driver said hello at boot: register it, then tell it which zones it belongs to
|
||||
static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user