-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
234 lines (209 loc) · 8.1 KB
/
server.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//http://stackoverflow.com/questions/24468459/sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery
//http://stackoverflow.com/questions/15427220/how-to-handle-post-request-in-node-js
var http = require('http');
var url = require("url");
var fs = require('fs');
var ChildProcess = require('child_process');
var re_date = /^\/\d{4}-\d{2}-\d{2}/;
function getPathFromUrl(url) {
return url.split("?")[0];
//return url.split("?")[0].split("#")[0];
}
var updatePlotVars = function(LogData) {
//console.log("updating plotedit.json");
plotVars = JSON.parse(fs.readFileSync('./plotedit.json')).plotVars;
// Add new variables to plotVars
var changes = false;
for (var i = 0; i < LogData.length; i++) {
d = LogData[i];
//console.log(d);
if (!(plotVars.hasOwnProperty(d))) {
//console.log(d);
changes = true;
plotVars[d] = {
Axis: 'y',
Label: d,
isBool: false,
Options: ''
};
//console.log(plotVars);
}
}
// Remove old variables from plotVars
for (var v in plotVars) {
if (LogData.indexOf(v) < 0) {
//console.log(d);
changes = true;
//console.log('removing: ');
//console.log(v);
delete plotVars[v]
//console.log(plotVars);
}
}
// Write if any changes
if (changes) {
console.log("updating plotedit.json");
// Write file
var toWrite = JSON.stringify({"plotVars": plotVars}, null, ' ');
fs.writeFileSync('./plotedit.json', toWrite, 'utf8');
}
}
var ConvertCSV = function(fn) {
// Data are saved in CSV format space efficiency and for easy appending of
// new values. A request for a CSV data file will open one and convert it
// to Json.
var lines = fn.split('\n');
var names = lines[0].split(',');
dat = {};
for (var j=0; j<names.length; j++){
dat[names[j]] = [];
}
for (var i=1; i<lines.length; i++) {
if (lines[i].length > 0) {
var vals = lines[i].split(',');
for (var j=0; j<names.length; j++) {
dat[names[j]].push(vals[j]);
}
}
}
sdat = 'data = ' + JSON.stringify(dat);
return sdat;
}
var GenLogList = function() {
// Generates a list of of log files based on filenames that have a date format
// e.g. 2017-01-01.csv
// This list is used to populate a dropdown menu in loghistory.html
var f = fs.readdirSync('./data/');
f = f.filter(function(i) { return /^\d{4}-\d{2}-\d{2}/.test(i); });
return "loglist = " + JSON.stringify(f);
}
var GenDSSensorList = function() {
r = ChildProcess.execSync('cat /sys/devices/w1_bus_master1/w1_master_slaves').toString().trim().split('\n');
return "dslist = " + JSON.stringify(r);
}
var serverPort = 8182;
http.createServer(function (request, response) {
var params = url.parse(request.url,true);
//console.log(request.url);
if(request.method === "GET") {
if (request.url === "/favicon.ico") {
// Don't have an icon for any pages yet, won't actually be requested.
response.writeHead(404, {'Content-Type': 'text/html'});
response.write('<!doctype html><html><head><title>404</title></head><body>404: Resource Not Found</body></html>');
response.end();
} else if (re_date.test(request.url)) {
// if a log file is requested, convert it to Json and return it in the
// response.
//console.log(request.url);
//console.log('json');
fs.readFile('./data/'+request.url, 'utf-8', function(error,data){
//console.log(request.url);
if(error){
response.writeHead(404, {"Content-type":"text/html"});
response.end("Sorry the page was not found.. ..");
}else{
response.writeHead(200, {"Content-type":"text/html"});
response.end(ConvertCSV(data));
}
});
} else if (request.url === '/loglist.json') {
// The list of existing log files needs to be generated by GenLogList()
response.writeHead(200, {"Content-type":"text/html"});
response.end(GenLogList());
} else if (request.url === '/dslist.json') {
// The list of ds devices needs to be generated by GenDSSensorList()
response.writeHead(200, {"Content-type":"text/html"});
response.end(GenDSSensorList());
} else {
// This is the normal page request
fs.readFile('.'+getPathFromUrl(request.url), function(error,data){
//console.log(params.path);
//console.log(request.url);
//console.log(getPathFromUrl(request.url));
if(error){
response.writeHead(404, {"Content-type":"text/html"});
response.end("Sorry the page was not found....");
}else{
//response.setHeader("Content-Type", "text/html");
response.writeHead(200, {"Content-type":"text/html"});
response.end(data);
}
});
}
} else if (request.method === "POST") {
console.log('post!');
if (request.url === "/setup.html") {
// Data submitted via POST from setup.html is to replace the init.json
// file used by the controller backend.
var requestBody = '';
request.on('data', function(data) {
requestBody += data;
if(requestBody.length > 1e7) {
response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
response.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
}
});
request.on('end', function() {
//console.log(requestBody);
var formData = JSON.parse(requestBody);
//console.log(formData);
// Write new data
fs.writeFileSync('init.json', requestBody, 'utf8');
// Update plotedit.json
updatePlotVars(formData.init.DataLog);
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Done!');
});
} else if (request.url === "/statusedit.html") {
// Data submitted via POST from statusedit.html is to replace the status.json
// file used by status.html.
var requestBody = '';
request.on('data', function(data) {
requestBody += data;
if(requestBody.length > 1e7) {
response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
response.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
}
});
request.on('end', function() {
//console.log(requestBody);
var formData = JSON.parse(requestBody);
//console.log(formData);
fs.writeFile('status.json', requestBody, 'utf8');
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Done!');
});
}
else if (request.url === "/plotedit.html") {
// Data submitted via POST from plotedit.html is to replace the plotedit.json
// file used by makeplot.js
var requestBody = '';
request.on('data', function(data) {
requestBody += data;
if(requestBody.length > 1e7) {
response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
response.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
}
});
request.on('end', function() {
//console.log(requestBody);
var formData = JSON.parse(requestBody);
//console.log(formData);
fs.writeFile('plotedit.json', requestBody, 'utf8');
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Done!');
});
}
else if (request.url === '/switches.html') {
// handle events to switch io pins
}
else {
response.writeHead(404, 'Resource Not Found', {'Content-Type': 'text/html'});
response.end('<!doctype html><html><head><title>404</title></head><body>404: Resource Not Found</body></html>');
}
} else {
response.writeHead(405, 'Method Not Supported', {'Content-Type': 'text/html'});
return response.end('<!doctype html><html><head><title>405</title></head><body>405: Method Not Supported</body></html>');
}
}).listen(serverPort);
console.log('Server running at localhost:'+serverPort);