diff --git a/inventoryWebBridge.lua b/inventoryWebBridge.lua index 2c34a11..cbb3919 100644 --- a/inventoryWebBridge.lua +++ b/inventoryWebBridge.lua @@ -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)