-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (103 loc) · 2.87 KB
/
index.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
109
110
111
112
113
114
115
// content of index.js
const http = require("http");
const fs = require("fs");
const port = 3000;
const handlePostRequest = async (urlPath, body) => {
console.log("{body,urlPath}", JSON.stringify({ body, urlPath }, null, 2));
if (urlPath === "/v1/tse/device/000A8000FC0000001/tkorouter/thermostat") {
return { success: true };
}
if (urlPath === "/auth/token" && "username" in body && "password" in body) {
return {
accessToken: "{access_token}",
scope: "all",
expiresIn: 86400,
tokenType: "Bearer",
};
}
if (urlPath === "/devices/abcd1234/commands" && "attribute" in body) {
return { success: true };
}
return { success: true };
};
const handleGetRequest = (urlPath) => {
if (urlPath === "/devices/abcd1234") {
return {
success: true,
message: "",
result: {
name: "Bedroom ceiling light",
id: "124",
type: "DimmerSwitch",
attributes: [
{
attribute: "status",
value: true,
},
{
attribute: "dimLevel",
value: 100,
},
],
},
};
}
if (urlPath === "/v1/tse/device/000A8000FC0000001/tkorouter/thermostat") {
return {
"fanspeed-allowed": ["low", "medium", "high", "auto"],
"heat-setpoint-min": 18.88,
temperature: 21.66,
"fanspeed-state": "medium",
"mode-state": "heat",
"cool-setpoint": 22.22,
"heat-setpoint": 22.22,
"mode-allowed": ["cool", "off"],
"cool-setpoint-min": 18.88,
"heat-setpoint-max": 26.66,
address: "000A8000FC000001",
fanspeed: "medium",
"cool-setpoint-max": 26.66,
_link: {
_self: "/v1/tse/device/000A8000FC000001/tkorouter/thermostat",
},
occupied: true,
mode: "auto",
};
}
return null;
};
const server = http.createServer((request, response) => {
console.log(request.url);
switch (request.method.toUpperCase()) {
case "POST":
var reqBody = "";
request.on("data", function (chunk) {
reqBody += chunk;
});
request.on("end", async function () {
const body = JSON.parse(reqBody);
const data = await handlePostRequest(request.url, body);
response.writeHead(200, { "Content-Type": "application/json" });
response.write(JSON.stringify(data));
response.end();
return;
});
return;
case "GET":
const data = handleGetRequest(request.url);
const html = fs.readFileSync("./index.html");
const responseData = data ? JSON.stringify(data) : html;
response.writeHead(200, { "Content-Type": "text/html" });
response.write(responseData);
response.end();
default:
break;
}
});
server.listen(port, (err) => {
if (err) {
console.log("something bad happened", err);
throw err;
}
console.log(`server is listening on ${port}`);
});