Lua FIFO / QUEUE file

2k views Asked by At

I'm quite new to Lua and Embedded programming. I'm working on a project:

IoT node that can be abstractedinto two parts: sensor and a board that runs Open WRT with Lua 5.1. I'm writing script that would be called from crontab every minute.

  1. In my script I'm accessing data from the sensor via package written with C. The result of reading data from sensor is 'hexadecimal numbers returned in string:

4169999a4180cccd41c9851f424847ae4508e0003ddb22d141700000418e666641c87ae14248147b450800003dc8b439 
  1. Then convert it (string) to values I need and POST it to API.

Problem:

Sometimes API is not reachable due to poor network connection. So I need to implement system where I would read a data from a sensor and then if API is not responding, I would save it to a FIFO queue (buffer). And then next time when script is called to read it would be sending 'old' records first and the newest one and the end.

1

There are 1 answers

3
Egor   Skriptunoff On BEST ANSWER
local queue_filespec = [[/path/to/your/queue/file]]
-- Initially your "queue file" (regular file!) must contain single line:
-- return {}

local function operation_with_queue(func)
  local queue = dofile(queue_filespec)
  local result = func(queue)
  for k, v in ipairs(queue) do
    queue[k] = ("%q,\n"):format(v)
  end
  table.insert(queue, "}\n")
  queue[0] = "return {\n"
  queue = table.concat(queue, "", 0)
  local f = assert(io.open(queue_filespec, "w"))
  f:write(queue)
  f:close()
  return result
end

function add_to_queue(some_data)
  operation_with_queue(
    function(queue)
      table.insert(queue, some_data)
    end
  )
end

function extract_from_queue()
  -- returns nil if queue is empty
  return operation_with_queue(
    function(queue)
      return table.remove(queue, 1)
    end
  )
end

Usage example:

add_to_queue(42)
add_to_queue("Hello")
print(extract_from_queue()) --> 42
print(extract_from_queue()) --> Hello
print(extract_from_queue()) --> nil