[fluksod] improved message arg checking

This commit is contained in:
Bart Van Der Meerssche 2011-01-28 10:41:55 +01:00
parent fd51388300
commit d870088415
1 changed files with 14 additions and 6 deletions

View File

@ -24,8 +24,8 @@ local nixio = require 'nixio'
local os, table, string = local os, table, string =
os, table, string os, table, string
local getfenv, setmetatable, tonumber = local getfenv, setmetatable, tonumber, type =
getfenv, setmetatable, tonumber getfenv, setmetatable, tonumber, type
module (...) module (...)
local modenv = getfenv() local modenv = getfenv()
@ -60,9 +60,11 @@ end
function parse(msg) function parse(msg)
msg.parsed = {} msg.parsed = {}
msg.parsed.cmd = msg.body:match('^%l%l') if(msg.body) then
for arg in msg.body:gmatch('%d+') do msg.parsed.cmd = msg.body:match('^%l%l') or ''
msg.parsed[#msg.parsed + 1] = tonumber(arg) -- returns nil when string does not contain a number for arg in msg.body:gmatch('%d+') do
msg.parsed[#msg.parsed + 1] = tonumber(arg) -- returns nil when string does not contain a number
end
end end
end end
@ -75,8 +77,14 @@ function encode(msg)
if argc ~= #msg.parsed then if argc ~= #msg.parsed then
return false return false
else else
return true for i = 1, #msg.parsed do
if type(msg.parsed[i]) ~= 'number' then
return false
end
end
end end
return true
end end
if msg.to == 'uart' then if msg.to == 'uart' then