-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
104 lines (93 loc) · 3.71 KB
/
main.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
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require("fs");
var path = require('path');
http.createServer(function(request, response) {
var headers = request.headers;
var method = request.method;
var theUrl = request.url;
var theUrlParts = url.parse(theUrl, true);
var theUrlParams = theUrlParts.query;
var theUrlPathname = theUrlParts.pathname;
body = '', reqInfo = {};
var body = [];
request.on('error', function(err) {
console.error(err);
}).on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
response.on('error', function(err) {
console.error(err);
});
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
var responseBody = {
headers: headers,
method: method,
url: theUrl,
body: body,
theUrlParams: theUrlParams,
theUrlPathname: theUrlPathname
};
if(theUrlPathname === '/list' || theUrlPathname === '/list/') {
console.log(theUrlPathname);
responseBody = [{
nukiId: 1,
name: "Eingangsbereich"
}, {
nukiId: 2,
name: "Vorraumtür"
}];
response.write(JSON.stringify(responseBody));
}
else if(theUrlPathname === '/lockState' || theUrlPathname === '/lockState/') {
if(!theUrlParams.nukiId) {
console.log(theUrlPathname + "--- no nuki id ---" + theUrlParams.nukiId);
response.statusCode = 404;
response.setHeader("Content-Type", "text/plain");
response.write("ERROR " + theUrlPathname);
}
else {
var nukiId = theUrlParams.nukiId;
var state = fs.readFileSync(path.join(__dirname, nukiId + ".state"), "UTF-8");
console.log(theUrlPathname + "---" + nukiId + "---" + state);
responseBody = {
state: parseInt(state),
stateName: "Status: " + state,
batteryCritical: theUrlParams.batteryCritical ? true : false,
success: true
};
response.write(JSON.stringify(responseBody));
}
}
else if(theUrlPathname === '/lockAction' || theUrlPathname === '/lockAction/') {
if(!theUrlParams.nukiId || !theUrlParams.action) {
console.log(theUrlPathname + "--- no nuki id or action ---" + theUrlParams.nukiId + "---" + theUrlParams.action);
response.statusCode = 404;
response.setHeader("Content-Type", "text/plain");
response.write("ERROR " + theUrlPathname);
}
else {
var nukiId = theUrlParams.nukiId;
var action = theUrlParams.action;
var newState = "unsupported";
if(action === "2") {
newState = 1;
}
else if(action === "1") {
newState = 3;
}
console.log(theUrlPathname + "---" + nukiId + "---" + action + "---" + newState);
var state = fs.writeFileSync(path.join(__dirname, nukiId + ".state"), newState, "UTF-8");
responseBody = {
batteryCritical: theUrlParams.batteryCritical ? true : false,
success: "true"
};
response.write(JSON.stringify(responseBody));
}
}
response.end();
});
}).listen(8881);