From 9778f5d43ffca26e3def45f21a163c1a83feb7be Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Oct 2015 22:56:24 +0200 Subject: wip --- mqtt-bridge/inter.lua | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'mqtt-bridge/inter.lua') diff --git a/mqtt-bridge/inter.lua b/mqtt-bridge/inter.lua index 7fc9f73..3b117a9 100644 --- a/mqtt-bridge/inter.lua +++ b/mqtt-bridge/inter.lua @@ -3,36 +3,42 @@ function inter_parse(line) line = string.sub(line, 1, string.find(line, "\r")) - local cmd = nil; + local cmd; local i = string.find(line, ":") - if i == nil then + if not i then -- print("inter_parse: invalid line: no command, line="..line) return end local cmd = string.sub(line, 1, i - 1) -- print("inter_parse: cmd="..cmd) + local last = i + 1 local args = {} - last = i+1 - while i ~= nil do + while i do local key local value i = string.find(line, " ", last) - arg = string.sub(line, last, i) - print("inter_parse: arg: "..arg) - j = string.find(arg, "=") - if j == nil then - key = arg - args[key] = true - -- print("inter_parse: key="..key.."=true") - else - key = string.sub(arg, 1, j-1) - value = string.sub(arg, j+1) - -- print("inter_parse: key="..key..", value="..value) + arg = arg:gsub("%s+", "") + + if #arg > 0 then + print("inter_parse: arg: "..arg) + j = string.find(arg, "=") + if not j then + if #arg > 0 then + key = arg + args[key] = true + print("inter_parse: key="..key.."=true") + end + else + key = string.sub(arg, 1, j-1) + value = string.sub(arg, j+1) + print("inter_parse: key="..key..", value="..value) + end end - if i == nil then + + if not i then break end last = i + 1 @@ -46,7 +52,7 @@ function inter_on_line(line) print("line:"..line) local cmd, args = inter_parse(line) - if cmd == nil then + if not cmd then return end @@ -61,7 +67,5 @@ function inter_init(callback) -- uart.setup(0, 9600, 8, 0, 1, 0) cb = callback - tmr.alarm(0, 1000, 0, function() print("still alive!"); end) uart.on("data", "\r", inter_on_line, 0) - print("inter ready: callback.."..tostring(cb)) end -- cgit v1.2.3