forked from dmlogic/vera-GenericWebService
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L_GenericWebService.lua
62 lines (45 loc) · 2.03 KB
/
L_GenericWebService.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local http = require 'socket.http'
local deviceSettings = {}
local deviceSID = 'urn:dmlogic-com:serviceId:GenericWebService1'
-- http://192.168.1.104:3480/data_request?id=variableget&DeviceNum=13&Variable=Armed&serviceId=urn:micasaverde-com:serviceId:SecuritySensor1
-- Builds the request and sends off to our service
function SendRequest(lul_device, lul_settings)
local response_body = { }
local payload = deviceSettings.Payload .. "&"..lul_settings.Payload
-- luup.log("GenericWebService SendRequest payload "..payload,25)
local res, code, response_headers = socket.http.request
{
url = deviceSettings.ServiceUrl;
method = deviceSettings.Method;
headers =
{
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #payload;
};
source = ltn12.source.string(payload);
sink = ltn12.sink.table(respsonse_body);
}
end
-- Hat tip to the SMTP plugin for gettings vars like this
function gwsReadVariable(lul_device, devicetype, name, defaultValue)
local var = luup.variable_get(devicetype,name, lul_device)
if (var == nil) then
var = defaultValue
luup.variable_set(devicetype,name,var,lul_device)
end
-- watch for those ampersands run through the UIs
var = string.gsub(var,"&","&")
return var
end
-- Read in our settings variables. As at v1.0 there is no convenience interface
-- to enter these into Vera. Instead add them as custom variables in the
-- Advanced tab using:
-- urn:dmlogic-com:serviceId:GenericWebService1
-- as the "New Service" identifier
function gwsStartup(lul_device)
luup.task("Running Lua Startup", 1, "GenericWebService", -1)
-- luup.log("GenericWebService startup device "..lul_device,25)
deviceSettings.ServiceUrl = gwsReadVariable(lul_device,deviceSID,"ServiceUrl","http://requestb.in/ppw34epp")
deviceSettings.Method = gwsReadVariable(lul_device,deviceSID,"Method","POST")
deviceSettings.Payload = gwsReadVariable(lul_device,deviceSID,"Payload","empty")
end