mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 22:02:17 +01:00
/controller, /discovery, /rooms, /shades, /groups, /room, /shade and /group now take a SnapshotSlots slot, queue a fill on the LoopQueue and send a chunked response whose body waits (RESPONSE_TRY_AGAIN) until loop() has copied the shade model into the slot. SnapshotWriter then streams the document from that copy a unit at a time, so each document describes one moment. At most two reads run at once; a third gets 503. A slot's 5.6 KB snapshot is allocated only while a read holds it, and its atomic state decides whether loop() or the read frees it when the read goes away first. The JSON of each item is unchanged and now lives in ModelSnapshot.cpp, where the native tests check it. Replies still written on the web server task copy their one item with SomfySnapshot.cpp, so serializing a group no longer calls updateFlags(), which emitted groupState and published MQTT from async_tcp. Also: every read route is chunked and leaves out an item too big for its 3 KB unit, like /controller did; /discovery drops its timing log; the SOMFY_MAX_* limits move to ShadeTypes.h. 21 new native tests, 292 in all. Flash: esp32devdbg +440 bytes, esp32dev +2,064 bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4.1 KiB
C++
102 lines
4.1 KiB
C++
#include <string.h>
|
|
#include "SomfySnapshot.h"
|
|
|
|
extern SomfyShadeController somfy;
|
|
|
|
static void copyName(char *out, const char *name) {
|
|
strncpy(out, name, 20);
|
|
out[20] = '\0';
|
|
}
|
|
|
|
void snapshotRoom(SomfyRoom &room, room_snapshot_t &out) {
|
|
out.roomId = room.roomId;
|
|
copyName(out.name, room.name);
|
|
out.sortOrder = room.sortOrder;
|
|
}
|
|
void snapshotShade(SomfyShade &shade, shade_snapshot_t &out) {
|
|
out.shadeId = shade.getShadeId();
|
|
out.roomId = shade.roomId;
|
|
copyName(out.name, shade.name);
|
|
out.remoteAddress = shade.getRemoteAddress();
|
|
out.upTime = shade.upTime;
|
|
out.downTime = shade.downTime;
|
|
out.tiltTime = shade.tiltTime;
|
|
out.stepSize = shade.stepSize;
|
|
out.paired = shade.paired;
|
|
out.lastRollingCode = shade.lastRollingCode;
|
|
out.position = shade.transformPosition(shade.currentPos);
|
|
out.target = shade.transformPosition(shade.target);
|
|
out.myPos = shade.transformPosition(shade.myPos);
|
|
out.direction = shade.direction;
|
|
out.tiltType = static_cast<uint8_t>(shade.tiltType);
|
|
out.tiltPosition = shade.transformPosition(shade.currentTiltPos);
|
|
out.tiltTarget = shade.transformPosition(shade.tiltTarget);
|
|
out.myTiltPos = shade.transformPosition(shade.myTiltPos);
|
|
out.tiltDirection = shade.tiltDirection;
|
|
out.shadeType = static_cast<uint8_t>(shade.shadeType);
|
|
out.bitLength = shade.bitLength;
|
|
out.proto = static_cast<uint8_t>(shade.proto);
|
|
out.flags = shade.flags;
|
|
out.flipCommands = shade.flipCommands;
|
|
out.flipPosition = shade.flipPosition;
|
|
out.inGroup = shade.isInGroup();
|
|
out.sunSensor = shade.hasSunSensor();
|
|
out.light = shade.hasLight();
|
|
out.simMy = shade.simMy();
|
|
out.repeats = shade.repeats;
|
|
out.sortOrder = shade.sortOrder;
|
|
out.gpioUp = shade.gpioUp;
|
|
out.gpioDown = shade.gpioDown;
|
|
out.gpioMy = shade.gpioMy;
|
|
out.gpioLLTrigger = (shade.gpioFlags & static_cast<uint8_t>(gpio_flags_t::LowLevelTrigger)) != 0;
|
|
out.linkedRemoteCount = 0;
|
|
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
|
|
SomfyLinkedRemote &remote = shade.linkedRemotes[i];
|
|
if(remote.getRemoteAddress() == 0) continue;
|
|
linked_remote_snapshot_t &l = out.linkedRemotes[out.linkedRemoteCount++];
|
|
l.remoteAddress = remote.getRemoteAddress();
|
|
l.lastRollingCode = remote.lastRollingCode;
|
|
}
|
|
}
|
|
void snapshotGroup(SomfyGroup &group, group_snapshot_t &out) {
|
|
out.groupId = group.getGroupId();
|
|
out.roomId = group.roomId;
|
|
copyName(out.name, group.name);
|
|
out.remoteAddress = group.getRemoteAddress();
|
|
out.lastRollingCode = group.lastRollingCode;
|
|
out.bitLength = group.bitLength;
|
|
out.proto = static_cast<uint8_t>(group.proto);
|
|
out.sunSensor = group.hasSunSensor();
|
|
out.flipCommands = group.flipCommands;
|
|
out.repeats = group.repeats;
|
|
out.sortOrder = group.sortOrder;
|
|
// The same sum as SomfyGroup::updateFlags, without its event.
|
|
out.flags = 0;
|
|
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES && group.linkedShades[i] != 0; i++) {
|
|
SomfyShade *shade = somfy.getShadeById(group.linkedShades[i]);
|
|
if(shade) out.flags |= shade->flags;
|
|
}
|
|
out.linkedShadeCount = 0;
|
|
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
|
|
const uint8_t shadeId = group.linkedShades[i];
|
|
if(shadeId > 0 && shadeId < 255) out.linkedShades[out.linkedShadeCount++] = shadeId;
|
|
}
|
|
}
|
|
void fillSnapshot(model_snapshot_t &snap) {
|
|
snap.startingAddress = somfy.startingAddress;
|
|
for(uint8_t i = 0; i < SOMFY_MAX_ROOMS; i++)
|
|
if(somfy.rooms[i].roomId != 0) snapshotRoom(somfy.rooms[i], snap.rooms[snap.roomCount++]);
|
|
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++)
|
|
if(somfy.shades[i].getShadeId() != 255) snapshotShade(somfy.shades[i], snap.shades[snap.shadeCount++]);
|
|
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++)
|
|
if(somfy.groups[i].getGroupId() != 255) snapshotGroup(somfy.groups[i], snap.groups[snap.groupCount++]);
|
|
for(uint8_t i = 0; i < SOMFY_MAX_REPEATERS; i++)
|
|
if(somfy.repeaters[i] != 0) snap.repeaters[snap.repeaterCount++] = somfy.repeaters[i];
|
|
}
|
|
const shade_snapshot_t *liveShade(uint8_t shadeId, void *ctx) {
|
|
SomfyShade *shade = somfy.getShadeById(shadeId);
|
|
if(!shade) return nullptr;
|
|
shade_snapshot_t *out = static_cast<shade_snapshot_t *>(ctx);
|
|
snapshotShade(*shade, *out);
|
|
return out;
|
|
}
|