User Tools

Site Tools


action_http

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
action_http [2018/07/11 18:43]
curlymo created
action_http [2018/08/11 14:13]
Niek
Line 1: Line 1:
-<code lua>+<file lua http.lua>
 -- --
 -- Copyright (C) 2018 CurlyMo & Niek -- Copyright (C) 2018 CurlyMo & Niek
Line 11: Line 11:
  
 function M.check(parameters) function M.check(parameters)
- local nr1 -1; + if parameters['​DEVICE'​] ​== nil then 
- local nr2 -1+ error("​http action requires a \"​DEVICE\"​ statement"​)
- local nr3 -1; + end 
 +  
 + if #​parameters['​DEVICE'​]['​value'​] ~= 1 or parameters['​DEVICE'​]['​value'​][2] ~= nil then 
 + error("​http action \"​DEVICE\"​ only takes one argument"​); 
 + 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
  
 + 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;
 +end
 +
 +function M.callback(http) ​
 + local data = http.getUserdata();​
 + 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  end
   
- if parameters['​DATA'​] ~= nil and (#​parameters['​DATA'​]['​value'​] ~1 or parameters['​DATA'​]['​value'​][2] ~nil) then + if devobj.setColor(info) == false then 
- error("​http action \"DATA\" ​only takes one argument");+ 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  end
  
- return 1;+ devobj.send(); 
 end end
  
-function M.callback(http+function M.thread(thread-- set color to "​busy",​ preserve old data 
- if http.getCode() == 200 then + local data = thread.getUserdata()
- error("​http action calling ".. http.getUrl() ." ​succeeded")+ local devname ​data['​device'​];​ 
- else + local devobj ​pilight.config.device(devname)
- error("​http action ​calling ​"​.. ​http.getUrl() ​.. " ​failed with code " ​.. tostring(http.getCode()));+ 
 + if devobj.setColor("busy"​) ​== false then 
 + error("​http action ​device \"" .. devname ​.. "\" color could not be set to \"​busy\"​")
  end  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 end
  
 function M.run(parameters) 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;
 +
 + 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 154:
  httpobj.post();​  httpobj.post();​
  end  end
- +
  return 1;  return 1;
 +
 end end
  
Line 114: Line 166:
  return {  return {
  name = "​http",​  name = "​http",​
- version = "0.1", + version = "0.2", 
- reqversion = "8.1.2",+ reqversion = "8.1.1",
  reqcommit = "​0"​  reqcommit = "​0"​
  }  }
 end end
  
-return M; +return M;</file>
-</code>+
action_http.txt · Last modified: 2018/08/15 12:28 by Niek