This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
action_http [2018/08/11 14:13] Niek |
action_http [2018/08/15 12:28] Niek Bugfix |
||
---|---|---|---|
Line 11: | Line 11: | ||
function M.check(parameters) | function M.check(parameters) | ||
- | if parameters['DEVICE'] == nil then | + | if parameters['DEVICE'] ~= nil then |
- | error("http action requires a \"DEVICE\" statement"); | + | if #parameters['DEVICE']['value'] ~= 1 or parameters['DEVICE']['value'][2] ~= nil then |
- | end | + | error("http action \"DEVICE\" only takes one argument"); |
- | + | end | |
- | if #parameters['DEVICE']['value'] ~= 1 or parameters['DEVICE']['value'][2] ~= nil then | + | local devname = parameters['DEVICE']['value'][1] |
- | error("http action \"DEVICE\" only takes one argument"); | + | local devobj = pilight.config.device(devname); |
+ | if devobj == nil then | ||
+ | error("http action device \"" .. devname .. "\" does not exist"); | ||
+ | end | ||
+ | if devobj.getLabel == nil or devobj.getColor == nil then | ||
+ | error("http action device \"" .. devname .. "\" isn't a generic_label device"); | ||
+ | end | ||
+ | if devobj.getColor() == "busy" then -- reset busy state | ||
+ | local async = pilight.async.thread(); | ||
+ | local data = async.getUserdata(); | ||
+ | data['device'] = devname; | ||
+ | data['old_data'] = devobj.getLabel(); | ||
+ | data['new_state'] = "ready" | ||
+ | async.setCallback("thread"); | ||
+ | async.trigger(); | ||
+ | end | ||
end | end | ||
Line 62: | Line 77: | ||
end | end | ||
end | end | ||
- | + | ||
- | local dev = nil | + | |
- | local nrdev = #parameters['DEVICE']['value']; | + | |
- | for i = 1, nrdev, 1 do | + | |
- | dev = pilight.config.device(parameters['DEVICE']['value'][i]); | + | |
- | if dev == nil then | + | |
- | error("http action device \"" .. parameters['DEVICE']['value'][i] .. "\" does not exist"); | + | |
- | end | + | |
- | if dev.getLabel == nil or dev.getColor == nil then | + | |
- | error("http action device \"" .. parameters['DEVICE']['value'][i] .. "\" isn't a generic_label device"); | + | |
- | end | + | |
- | end | + | |
return 1; | return 1; | ||
end | end | ||
Line 80: | Line 83: | ||
function M.callback(http) | function M.callback(http) | ||
local data = http.getUserdata(); | local data = http.getUserdata(); | ||
- | local devname = data['device']; | + | if data['device'] ~= nil then |
- | local devobj = pilight.config.device(devname); | + | local devname = data['device']; |
+ | local devobj = pilight.config.device(devname); | ||
+ | local info = "code=" .. tostring(http.getCode()) .. "&size=" .. tostring(http.getSize()) | ||
+ | if string.len(http.getMimetype()) > 0 then | ||
+ | info = info .."&mimetype=" .. http.getMimetype() | ||
+ | end | ||
+ | |||
+ | if devobj.setColor(info) == false then | ||
+ | error("http action device \"" .. devname .. "\" color could not be set to \"" .. info .. "\"") | ||
+ | end | ||
+ | local label = data['old_data']; | ||
+ | if http.getCode() == 200 then | ||
+ | label = http.getData(); | ||
+ | end | ||
+ | if devobj.setLabel(label) == false then | ||
+ | error("http action device \"" .. devname .. "\" label could not be set to \"" .. label .. "\"") | ||
+ | end | ||
- | local info = "code=" .. tostring(http.getCode()) .. "&size=" .. tostring(http.getSize()) | + | devobj.send(); |
- | if string.len(http.getMimetype()) > 0 then | + | end |
- | info = info .."&mimetype=" .. http.getMimetype() | + | |
- | end | + | |
- | + | ||
- | if devobj.setColor(info) == false then | + | |
- | error("http action device \"" .. devname .. "\" color could not be set to \"" .. info .. "\"") | + | |
- | end | + | |
- | if devobj.setLabel(http.getData()) == false then | + | |
- | error("http action device \"" .. devname .. "\" label could not be set to \"" .. http.getData() .. "\"") | + | |
- | end | + | |
- | + | ||
- | devobj.send(); | + | |
+ | if http.getCode() == 200 then | ||
+ | error("http action calling ".. http.getUrl() .. " succeeded, received \"" .. http.getData() .. "\""); | ||
+ | else | ||
+ | error("http action calling ".. http.getUrl() .. " failed with code " .. tostring(http.getCode())); | ||
+ | end | ||
end | end | ||
- | function M.thread(thread) -- set color to "busy", preserve old data | + | function M.thread(thread) -- only set color to new state, preserve old data |
local data = thread.getUserdata(); | local data = thread.getUserdata(); | ||
local devname = data['device']; | local devname = data['device']; | ||
local devobj = pilight.config.device(devname); | local devobj = pilight.config.device(devname); | ||
- | if devobj.setColor("busy") == false then | + | if devobj.setColor(data['new_state']) == false then |
- | error("http action device \"" .. devname .. "\" color could not be set to \"busy\"") | + | error("http action device \"" .. devname .. "\" color could not be set to \" .. data['new_state'].. \"") |
end | end | ||
Line 132: | Line 145: | ||
local new_state = nil; | local new_state = nil; | ||
local old_data = devobj.getLabel(); | local old_data = devobj.getLabel(); | ||
+ | data['old_data'] = old_data; | ||
if old_state ~= nil then | if old_state ~= nil then | ||
Line 138: | Line 152: | ||
data['device'] = devname; | data['device'] = devname; | ||
data['old_data'] = old_data; | data['old_data'] = old_data; | ||
+ | data['new_state'] = "busy" | ||
async.setCallback("thread"); | async.setCallback("thread"); | ||
Line 166: | Line 181: | ||
return { | return { | ||
name = "http", | name = "http", | ||
- | version = "0.2", | + | version = "0.3", |
reqversion = "8.1.1", | reqversion = "8.1.1", | ||
reqcommit = "0" | reqcommit = "0" | ||
Line 172: | Line 187: | ||
end | end | ||
- | return M;</file> | + | return M; |
+ | </file> |