-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathhttp-handlers.js
108 lines (101 loc) · 2.88 KB
/
http-handlers.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Shelly script example of using HTTP handlers
* Implementing a simple HTTP server with action handlers
*
* Usage:
* call the setup action on your shelly
* curl 'http://<SHELLY-IP>/script/<SCRIPT-ID>/api?action=setup'
*
* call the profile action, pass the profile parameter
* see handleProfile method
* curl 'http://<SHELLY-IP>/script/<SCRIPT-ID>/api?action=profile&profile=<local|remote>'
*/
//as hoisting is not supported in Shelly Scripting
//we introduce a registration method
let CONFIG = {
url_segment: "api",
action_param: "action",
actions: {},
registerActionHandler: function (actionParamValue, handler) {
this.actions[actionParamValue] = handler;
},
};
//action handlers below
//this is just a dummy handler, nothing interesting
let handleSetup = function (qsParams, response) {
response.code = 200;
response.body = "Setup: successful";
response.send();
};
//switch between detached and follow mode for the first switch
let handleProfile = function (qsParams, response) {
if (typeof qsParams.profile === "undefined") {
response.code = 424;
response.body = "Missing parameter profile";
response.send();
return;
}
let _config = {
in_mode: "detached",
};
if (qsParams.profile === "local") {
_config.in_mode = "follow";
_config.initial_state = "off";
}
Shelly.call(
"switch.setconfig",
{
id: 0,
config: _config,
},
function (res, err_code, err_msg) {
if (err_code !== 0) {
response.code = 400;
response.body = err_msg;
} else {
response.code = 200;
response.body = "Successful profile setup";
}
response.send();
}
);
};
CONFIG.registerActionHandler("setup", handleSetup);
CONFIG.registerActionHandler("profile", handleProfile);
//No need to adapt anything below
function parseQS(qs) {
let params = {};
if (qs.length === 0) return params;
let paramsArray = qs.split("&");
for (let idx in paramsArray) {
let kv = paramsArray[idx].split("=");
params[kv[0]] = kv[1] || null;
}
return params;
}
function httpServerHandler(request, response) {
let responseCode = 200;
let responseBody = "";
let params = parseQS(request.query);
let actionParam = params[CONFIG.action_param];
console.log("Action param", actionParam);
console.log(JSON.stringify(params));
if (
typeof actionParam === "undefined" ||
typeof CONFIG.actions[actionParam] === "undefined" ||
CONFIG.actions[actionParam] === null
) {
response.code = 400;
response.body =
"No " +
CONFIG.action_param +
" parameter in query string or no action defined";
response.send();
} else {
CONFIG.actions[actionParam](params, response);
console.log("Handler called");
}
}
//we don't handle failure of this method
//as according to Shelly Gen2 API docs it aborts the script
HTTPServer.registerEndpoint(CONFIG.url_segment, httpServerHandler);