Enhance command polling: implement deduplication of processed commands and acknowledgment of command IDs

This commit is contained in:
MayaTheShy
2026-03-22 01:29:05 -04:00
parent 98f8157bea
commit c47ec63e50

View File

@@ -196,17 +196,27 @@ local function stateForwarder()
end
-- Task 3: Poll web server for commands
local lastProcessedId = 0 -- track highest processed command ID for dedup
local function commandPoller()
while running do
local ok, err = pcall(function()
local result = httpGet("/api/bridge/commands")
if result and result.commands and #result.commands > 0 then
-- Process each command
local maxId = lastProcessedId
-- Process each command, skipping already-processed ones
for _, cmd in ipairs(result.commands) do
pcall(processCommand, cmd)
local cmdId = cmd.id or 0
if cmdId > lastProcessedId then
pcall(processCommand, cmd)
if cmdId > maxId then maxId = cmdId end
end
end
-- Acknowledge up to the highest processed ID
if maxId > lastProcessedId then
lastProcessedId = maxId
httpPost("/api/bridge/commands/ack", { lastProcessedId = lastProcessedId })
end
-- Acknowledge
httpPost("/api/bridge/commands/ack", {})
end
end)
sleep(POLL_INTERVAL)