[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 =
os, table, string
local getfenv, setmetatable, tonumber =
getfenv, setmetatable, tonumber
local getfenv, setmetatable, tonumber, type =
getfenv, setmetatable, tonumber, type
module (...)
local modenv = getfenv()
@ -60,10 +60,12 @@ end
function parse(msg)
msg.parsed = {}
msg.parsed.cmd = msg.body:match('^%l%l')
if(msg.body) then
msg.parsed.cmd = msg.body:match('^%l%l') or ''
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
function encode(msg)
@ -75,9 +77,15 @@ function encode(msg)
if argc ~= #msg.parsed then
return false
else
return true
for i = 1, #msg.parsed do
if type(msg.parsed[i]) ~= 'number' then
return false
end
end
end
return true
end
if msg.to == 'uart' then
msg.encoded = nixio.bin.hexlify(msg.body or '')