User Tools

Site Tools


action_http

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
action_http [2018/07/15 14:51]
Niek replaced <code> tag with <file> tag to enable download
action_http [2018/08/14 16:15]
Niek Fixed busy state at startup and made DEVICE optional again
Line 11: Line 11:
  
 function M.check(parameters) function M.check(parameters)
- local nr1 -1; + if parameters['​DEVICE'​] ~= nil then  
- local ​nr2 -1; + if #​parameters['​DEVICE'​]['​value'​] ~= 1 or parameters['​DEVICE'​]['​value'​][2] ~= nil then 
- local ​nr3 = -1+ error("​http action \"​DEVICE\"​ only takes one argument"​)
 + end 
 + local devname ​parameters['​DEVICE'​]['​value'​][1] 
 + 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 
 +
  if parameters['​GET'​] == nil and parameters['​POST'​] == nil then  if parameters['​GET'​] == nil and parameters['​POST'​] == nil then
  error("​http action is missing a \"​GET\"​ or \"​POST\"​ statement"​);​  error("​http action is missing a \"​GET\"​ or \"​POST\"​ statement"​);​
  end  end
 +
  if parameters['​GET'​] ~= nil and parameters['​POST'​] ~= nil then  if parameters['​GET'​] ~= nil and parameters['​POST'​] ~= nil then
- error("​http action cannot have \"​GET\"​ and \"​POST\"​ statements at the same time"​);​+ error("​http action cannot have \"​GET\"​ and \"​POST\"​ statements at the same time"​);​
  end  end
 +
  if parameters['​GET'​] ~= nil then  if parameters['​GET'​] ~= nil then
- if (#parameters['​GET'​]['​value'] ~= 1 or parameters['​GET'​]['​value'​][2] ~= nilthen + if parameters['​MIMETYPE'] ~= nil then 
- error("​http action \"​GET\" ​only takes one argument");+ error("​http action \"​GET\" ​doesn'​t take a \"​MIMETYPE\"​ statement");
  end  end
  
- if parameters['​MIMETYPE'] ~= nil + if parameters['​DATA'] ~= nil then 
- error("​http action \"​GET\" ​does not take a \"MIMETYPE\" statement"​);​+ error("​http action \"​GET\" ​doesn'​t ​take a \"DATA\" statement"​);​
  end  end
  
- if parameters['​DATA'] ~= nil + if (#parameters['​GET'​]['​value'] ~= 1 or parameters['​GET'​]['​value'​][2] ~= nil) then 
- error("​http action \"​GET\" ​does not take a \"​MIMETYPE\"​ statement");+ error("​http action \"​GET\" ​only takes one argument");
  end  end
  end  end
  
  if parameters['​POST'​] ~= nil then  if parameters['​POST'​] ~= nil then
- if (#parameters['​POST'​]['​value'​] ​~1 or parameters['​POST'​]['​value'​][2] ~= nilthen + if parameters['​MIMETYPE'] == nil then 
- error("​http action \"​POST\" ​only takes one argument");+ error("​http action \"​POST\" ​request requires a \"​MIMETYPE\"​ statement");
  end  end
 +
  if parameters['​DATA'​] == nil then  if parameters['​DATA'​] == nil then
  error("​http action \"​POST\"​ request requires a \"​DATA\"​ statement"​);​  error("​http action \"​POST\"​ request requires a \"​DATA\"​ statement"​);​
  end  end
  
- nr1 = parameters['​POST'​]['​order']+ if (#parameters['​POST'​]['​value'​] ​~1 or parameters['​POST'​]['​value'][2] ~= nil) then 
- nr2 = parameters['​DATA'​]['​order']+ error("​http ​action ​\"​POST\" ​only takes one argument");
- +
- if nr2 < nr1 then +
- error("​http ​actions are formatted as \"http POST ... DATA ...\""​)+
- return -1;+
  end  end
- end 
  
- if parameters['​MIMETYPE'​] ~= nil then + if (#​parameters['​MIMETYPE'​]['​value'​] ~= 1 or parameters['​MIMETYPE'​]['​value'​][2] ~= nil) then
- if(#​parameters['​MIMETYPE'​]['​value'​] ~= 1 or parameters['​MIMETYPE'​]['​value'​][2] ~= nil) then+
  error("​http action \"​MIMETYPE\"​ only takes one argument"​);​  error("​http action \"​MIMETYPE\"​ only takes one argument"​);​
  end  end
-  
- nr3 = parameters['​MIMETYPE'​]['​order'​];​ 
  
- if nr3 < nr2 or nr3 < nr1 then + if (#​parameters['​DATA'​]['​value'​] ~= 1 or parameters['​DATA'​]['​value'​][2] ~= nil) then 
- error("​http ​actions are formatted as \"http POST ... DATA ... MIMETYPE ...\""​)+ error("​http ​action ​\"​DATA\" ​only takes one argument");
- return -1;+
  end  end
- 
  end  end
   
- if parameters['​DATA'​] ~= nil and (#​parameters['​DATA'​]['​value'​] ~= 1 or parameters['​DATA'​]['​value'​][2] ~= nil) then 
- error("​http action \"​DATA\"​ only takes one argument"​);​ 
- end 
- 
  return 1;  return 1;
 end end
  
-function M.callback(http)+function M.callback(http) ​ 
 + local data = http.getUserdata();​ 
 + if data['​device'​] ~= nil then 
 + 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 
 + if devobj.setLabel(http.getData()) == false then 
 + error("​http action device \""​ .. devname .. "​\"​ label could not be set to  \""​ .. http.getData() .. "​\""​) 
 + end 
 + 
 + devobj.send();​ 
 + end  
  if http.getCode() == 200 then  if http.getCode() == 200 then
- error("​http action calling ".. http.getUrl() .. " succeeded"​);​+ error("​http action calling ".. http.getUrl() .. " succeeded, received \""​ .. http.getData() .. "​\"​");
  else  else
  error("​http action calling ".. http.getUrl() .. " failed with code " .. tostring(http.getCode()));​  error("​http action calling ".. http.getUrl() .. " failed with code " .. tostring(http.getCode()));​
Line 84: Line 107:
 end end
  
-function M.run(parameters)+function M.thread(thread-- only set color to new state, preserve old data 
 + local data = thread.getUserdata();​ 
 + local devname = data['​device'​];​ 
 + local devobj = pilight.config.device(devname);​
  
 + if devobj.setColor(data['​new_state'​]) == false then
 + error("​http action device \""​ .. devname .. "​\"​ color could not be set to \" .. data['​new_state'​].. \""​)
 + end
 +
 + if devobj.setLabel(data['​old_data'​]) == false then
 + error("​http action device \""​ .. devname .. "​\"​ label could not be set to \""​ .. data['​old_data'​] .. "​\""​)
 + end
 +
 + devobj.send();​
 +end
 +
 +function M.run(parameters)
  local httpobj = pilight.network.http();​  local httpobj = pilight.network.http();​
  httpobj.setCallback("​callback"​);​  httpobj.setCallback("​callback"​);​
 + local data = httpobj.getUserdata();​
 + data['​device'​] = nil;
  
- if parameters['​GET'] ~= nil then + if parameters['​DEVICE'] ~= nil then 
- if parameters['​DATA'​] ~=nil then + local devname ​= parameters['​DEVICE'​]['​value'​][1]
- httpobj.setUrl(parameters['​GET'​]['​value'​][1] ​.. "?"​ .. parameters['DATA']['​value'​][1]); + data['device'​] ​= devname; 
- else + local devobj = pilight.config.device(devname); 
- httpobj.setUrl(parameters['​GET'​]['​value'​][1]);+ if devobj.getColor() == "​busy"​ then 
 + error("​http action previous request for device \"" ​.. devname .. "​\"​ in progress, request skipped!"​)
  end  end
 +
 + local old_state = devobj.getColor();​
 + local new_state = nil;
 + local old_data = devobj.getLabel();​
 +
 + if old_state ~= nil then
 + local async = pilight.async.thread();​
 + local data = async.getUserdata();​
 + data['​device'​] = devname;
 + data['​old_data'​] = old_data;
 + data['​new_state'​] = "​busy"​
 +
 + async.setCallback("​thread"​);​
 + async.trigger();​
 + end
 + end
 +
 + if parameters['​GET'​] ~= nil then
 + httpobj.setUrl(parameters['​GET'​]['​value'​][1]);​
  httpobj.setMimetype(""​);​  httpobj.setMimetype(""​);​
  httpobj.get();​  httpobj.get();​
Line 103: Line 163:
  httpobj.post();​  httpobj.post();​
  end  end
- +
  return 1;  return 1;
 +
 end end
  
Line 114: Line 175:
  return {  return {
  name = "​http",​  name = "​http",​
- version = "0.1",+ version = "0.3",
  reqversion = "​8.1.1",​  reqversion = "​8.1.1",​
  reqcommit = "​0"​  reqcommit = "​0"​
action_http.txt · Last modified: 2018/08/15 12:28 by Niek