You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Actions = {
function(data)
print('Received data from iothub:', data)
local source = 'iothub-mqtt'
local device = 'device-uuid'
local dataT, err = rulexlib:J2T(data)
if (err ~= nil) then
print('parse json error:', err)
return false, data
end
local requestId = dataT['id']
-- Action:
-- {
-- "method":"action",
-- "id":"20a4ccfd",
-- "actionId":"actionid",
-- "timestamp":13124323543,
-- "data":{
-- "a":"1",
-- "b":"2"
-- }
-- }
if dataT['method'] == 'action' then
local actionId = dataT['actionId']
if actionId == 'Action get_state' then
local readData, err = rulexlib:ReadDevice(0, device)
if (err ~= nil) then
print('Action get_state error:', err)
return false, data
end
print('Action get_state:', readData)
local _, err = iothub:ActionSuccess(source, requestId, { code = 0 })
if (err ~= nil) then
print('Action Reply error:', err)
return false, data
end
end
end
-- Property:
-- {
-- "method":"property",
-- "id":"20a4ccfd",
-- "timestamp":13124323543,
-- "data":{
-- "a":"1"
-- }
-- }
if dataT['method'] == 'property' then
local _, err = iothub:PropertySuccess(source, requestId, { code = 0 })
if (err ~= nil) then
print('Property Reply error:', err)
return false, data
end
end
return true, data
end
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
在编写Lua代码的时候,我提倡“防御式”编程。也就是说直到最后结果正确才认为是正确的。其中每一个环节产生的不同分支必须被检查。例如以下示例:
在上面这个案例中 httpget 产生了两个可能:有异常或者有结果,我们必须对error进行处理:
类似于下面这个解析JSON的案例:
防御式编程是增加了代码的冗余度,但是几乎可以认为是最靠谱的做法。在IOT设备的控制和通信领域中,我们宁可麻烦点,也保证设备的靠谱性。
Beta Was this translation helpful? Give feedback.
All reactions