-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
685 lines (641 loc) · 21.6 KB
/
server.js
File metadata and controls
685 lines (641 loc) · 21.6 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// >npm start
//in terminal to start this site.
//IF new group created, website will need to know!
//Empty resultset.
/*
https://github.com/websockets/ws/tree/master
*/
const version = '1.34';
const http = require('http');
const express = require('express');
const handlebars = require('express-handlebars');
const path = require('path');
const mqtt = require('mqtt'); // require mqtt
const { WebSocketServer } = require('ws');
//var mdns = require('multicast-dns')();
const dayjs = require('dayjs');
var clsSettings = require('./g_settings.js'); //import clsSettings from './g_settings.js';
var Dbase = require('./database.js');
const db = new Dbase('db.sqlite');
var fs = require('fs');
let client;
//console.log(handlebars.create().handlebars.compile);
const app = express();
//app.engine(".hbs", handlebars({ extname: ".hbs" }));
app.set('view engine', 'hbs');
app.use(express.json());
app.use(express.static(__dirname));
app.engine(
'hbs',
handlebars.engine({
defaultView: 'default',
layoutsDir: __dirname + '/views/layouts',
//partialsDir: __dirname + "/views/partials",
extname: 'hbs',
})
);
var WSRunning = true;
var MQTTRunning = true;
app.set('MQTTRunning', MQTTRunning);
const hbs = handlebars.create();
hbs.handlebars.registerHelper('messagenumber', function (msg, dp, options) {
//msg = the topic message coming in as float.
//dp = decimal places for use in toFixed(dp)
var mf = parseFloat(msg);
// console.log(mf);
// console.log(msg);
if (isNaN(mf)) return msg;
else return mf.toFixed(parseInt(dp));
});
hbs.handlebars.registerHelper('dayjs', function (dt, fmt, td, options) {
//format the date string using dayjs.
//dt = dateinput
//fmt is the daysjs formating
//td = if today is the same as dt, 1=remove date and show time only. Not 1, dont change.
//console.log(dayjs().format("MM/DD/YYYY hh:mm:ss A"));
//console.log(`dayjs ${dt}`);
if (dt === null) {
return '';
}
var dati = dayjs(dt).format(fmt);
if (td == '1') {
//console.log(dayjs().isSame(dayjs(dt).format("MM/DD/YYYY"), "day"));
if (dayjs().isSame(dayjs(dt).format('MM/DD/YYYY'), 'day')) {
dati = dayjs(dt).format('hh:mm:ss A');
}
}
return dati; //dayjs(dt).format(fmt);
});
hbs.handlebars.registerHelper('ifdatebetween', function (v1, v2, options) {
//For sensores and the lastupdated, its possible the difference is a second. Which means they are not equal.
//This means the last updated row might not get highlighted since match is not exact.
//this function checks that the sensor is within 5 seconds +-.
// console.log("helper");
// console.log(v1);
// console.log(v2);
var strsensordt = Date.parse(v1);
var strlastupdated = Date.parse(v2);
var sensordt = new Date(strsensordt);
var lastupdated = new Date(strlastupdated);
var dp = new Date(lastupdated.getTime() + 10000);
var dm = new Date(lastupdated.getTime() - 10000);
// console.log(dp);
// console.log(dm);
if (sensordt < dp && sensordt > dm) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
//https://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional
hbs.handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return v1 == v2 ? options.fn(this) : options.inverse(this);
case '===':
return v1 === v2 ? options.fn(this) : options.inverse(this);
case '!=':
return v1 != v2 ? options.fn(this) : options.inverse(this);
case '!==':
return v1 !== v2 ? options.fn(this) : options.inverse(this);
case '<':
return v1 < v2 ? options.fn(this) : options.inverse(this);
case '<=':
return v1 <= v2 ? options.fn(this) : options.inverse(this);
case '>':
return v1 > v2 ? options.fn(this) : options.inverse(this);
case '>=':
return v1 >= v2 ? options.fn(this) : options.inverse(this);
case '&&':
return v1 && v2 ? options.fn(this) : options.inverse(this);
case '||':
return v1 || v2 ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
const sockserver = new WebSocketServer({ port: 3001 });
async function SetupWebSocket() {
const rows = await db.getSettings();
console.log('--SetupWebSocket---');
//console.log(rows);
if (rows === undefined) {
//sockserver.close();
return false;
}
sockserver.on('connection', (ws, req) => {
console.log('New client connected!');
//var rf = { refresh: true };
//ws.send(JSON.stringify(rf));
ws.send('connection established');
ws.on('close', () => console.log('Client has disconnected!'));
ws.on('message', (data) => {
console.log(`Client in setup Count: ${sockserver.clients.size}`);
sockserver.clients.forEach(async (client) => {
console.log(`distributing SEND: ${req.socket.remoteAddress}`);
//client.send(`${data}`);
//Could user SQL to grab the groups and compare the data is 1 of the groups before proceeding.
var theirip = req.socket.remoteAddress;
var cip = client._socket.remoteAddress;
if (
theirip.toString().replace('::ffff:', '') !==
cip.toString().replace('::ffff:', '')
) {
return;
}
const result = await db.getBasedonGrouo(data.toString());
console.log(`data : ${data.toString()}`);
//console.log(result);
if (result && result.length > 0) {
// get group here and add <group.tolower()>.hbs for the file read!
const filePath = path.join(
__dirname,
`/views/templates/${data.toString().toLocaleLowerCase()}.hbs`
);
//console.log(filePath);
if (!fs.existsSync(filePath)) {
return;
}
const source = fs.readFileSync(filePath, 'utf-8').toString();
//console.log(source);
const hbs = handlebars.create();
const template = hbs.handlebars.compile(source); //NO handlebars.engine.compile(source);
const now = new Date();
var mm = '0' + now.getMonth() + 1;
mm = mm.slice(-2);
var s = '0' + now.getSeconds();
s = s.slice(-2);
var datetime = '';
datetime = datetime.concat(
('0' + (now.getMonth() + 1)).slice(-2),
'/',
('0' + now.getDate()).slice(-2),
'/',
now.getFullYear().toString(),
' ',
('0' + now.getHours()).slice(-2),
':',
('0' + now.getMinutes()).slice(-2),
':',
('0' + now.getSeconds()).slice(-2)
);
const replacements = {
group: result[0].group,
updateddate: datetime,
topics: result,
};
const htmlToSend = template(replacements);
client.send(htmlToSend);
//console.log(`distributing message: ${client._socket.remoteAddress} `);
// console.log(htmlToSend);
// sockserver.clients.forEach((client) => {
// console.log(`distributing message: ${client._socket.remoteAddress} `);
// // client.send(JSON.stringify(result));
// client.send(htmlToSend);
// });
//ws.send("MQTT:New message");
}
});
});
ws.onerror = function () {
console.log('websocket error');
};
});
}
async function SetupMQTT() {
//const client = mqtt.connect(`mqtt://${cSettings.mip}`,{username:`${cSettings.mid}`,password:`${cSettings.mpw}`}) // create a client
//Get settings. mqtt.connect using settings, then when mqtt is connected, subscrivbe to topics. All in one pass.
const rows = await db.getSettings();
if (rows === undefined || WSRunning == false) {
return false;
}
// console.log('-----');
// console.log(rows);
if (rows !== undefined) {
// console.log(row);
//console.log(rows[0].topics_json);
var topics_json = JSON.parse(rows.topics_json);
cSettings = new clsSettings(
rows.mqtt_ip,
rows.mqtt_id,
rows.mqtt_pw,
topics_json
);
console.log('inside');
console.log(cSettings.mip);
client = mqtt
.connect(`mqtt://${cSettings.mip}`, {
username: `${cSettings.mid}`,
password: `${cSettings.mpw}`,
reconnectPeriod: 1000,
})
.on('connect', () => {
console.log('CONNECTED TO MQTT');
for (var i in topics_json) {
const tpc = topics_json[i];
//console.log(tpc.topic);
client.subscribe(tpc.topic, (err, granted) => {
if (err) {
console.log(err);
}
console.log(granted);
});
}
})
.on('message', async (topic, message) => {
// message is Buffer
dataupdated = true;
console.log(
`topic=> ${topic.toString()} < message=> ${message.toString()} <`
);
var data = {
topic: topic.toString(),
message: message.toString(),
};
db.Add_Topic(data.topic, data.message);
const result = await db.getBasedonTopic(data.topic);
//console.log(result);
// get group here and add <group.tolower()>.hbs for the file read!
const filePath = path.join(
__dirname,
`/views/templates/${result[0].group
.toString()
.toLocaleLowerCase()}.hbs`
);
if (!fs.existsSync(filePath)) {
return;
}
//C:\Users\treya\Documents\Dev\HAExpress\views\templates
//C:\Users\treya\Documents\Dev\HAExpress\templates\temperature.hbs
//console.log(filePath);
const source = fs.readFileSync(filePath, 'utf-8').toString();
//console.log(source);
const hbs = handlebars.create();
//const template = hbs.compile(source);
const template = hbs.handlebars.compile(source); //NO handlebars.engine.compile(source);
const now = new Date();
var mm = '0' + now.getMonth() + 1;
mm = mm.slice(-2);
var s = '0' + now.getSeconds();
s = s.slice(-2);
var datetime = '';
datetime = datetime.concat(
('0' + (now.getMonth() + 1)).slice(-2),
'/',
('0' + now.getDate()).slice(-2),
'/',
now.getFullYear().toString(),
' ',
('0' + now.getHours()).slice(-2),
':',
('0' + now.getMinutes()).slice(-2),
':',
('0' + now.getSeconds()).slice(-2)
);
const replacements = {
group: result[0].group,
updateddate: datetime,
topics: result,
};
const htmlToSend = template(replacements);
// console.log(htmlToSend);
sockserver.clients.forEach((client) => {
//console.log(`distributing message: ${JSON.stringify(client)} `);
// client.send(JSON.stringify(result));
client.send(htmlToSend);
});
});
return true;
}
}
app.get('/', async (req, res) => {
//let rTemplate = 'minimain';
let rTemplate = req.query.template;
if (rTemplate === undefined) {
rTemplate = 'main';
}
const filePath = path.join(
__dirname,
`/views/${rTemplate.toLocaleLowerCase()}.hbs`
);
if (!fs.existsSync(filePath)) {
res.send(`Your Layout Template does not exist.<br>${filePath}`);
return;
}
//Serves the body of the page aka "main.handlebars" to the container //aka "index.handlebars"
const settings = await db.getSettings();
if (settings == undefined) {
res.redirect('/setup');
return;
}
const result = await db.getGroups();
console.log(`app.get(/) ${rTemplate}`);
//console.log(result);
// result.forEach((result) => {
// console.log(result.value);
// });
//How to send a dynamic array of TestArray?
res.render(rTemplate, {
layout: 'index',
version: version,
Groups: JSON.stringify(result),
});
});
//USE THIS FOR A HEALTH CHECK. AJAX/FETCH check to refresh the site.
//is response is NOT OK. set global var const doRefresh = false; to true.
//THen when hc responds, reload the screen.
// app.get('/hc', (req, res) => {
// return res.send('OK');
// });
app.get('/setup/', (req, res) => {
//Serves the body of the page aka "main.handlebars" to the container //aka "index.handlebars"
//console.log(req);
//console.dir(req.originalUrl); // '/admin/new?a=b' (WARNING: beware query string)
//console.dir(req.baseUrl); // '/admin'
//console.log(req.path); // '/new'
//console.dir(req.baseUrl + req.path); // '/admin/new' (full path without query string)
//console.log(req.app.get('web3'));
var mRunning = req.app.get('MQTTRunning');
console.log(`mqtt running: ${mRunning}`);
res.render('setmain', {
layout: 'setup',
//setter: 'this is the setup page',
version: version,
mqttstatus: `mqtt active status = ${mRunning.toString()}`,
});
});
app.get('/loadt/', async (req, res) => {
let rFile = req.query.file;
let rPath = req.query.path;
if (rFile !== undefined) {
console.log(rFile);
}
if (rPath !== undefined) {
console.log(rPath);
}
let filePath = '';
if (rPath.toString() === 'templates') {
filePath = path.join(__dirname, `/views/templates/${rFile}`);
} else {
filePath = path.join(__dirname, `/views/${rFile}`);
}
//const filePath = path.join(__dirname, `/views/templates/${rFile}`);
//const viewpath = path.join(__dirname, `/views/${rFile}`);
if (!fs.existsSync(filePath)) {
return;
}
const source = fs.readFileSync(filePath, 'utf-8').toString();
res.send(source);
});
app.post('/savet/', async (req, res) => {
let rJsonBody = req.body;
let rPath = req.query.path;
if (rJsonBody === undefined) {
// console.log(rJsonBody);
res.send(JSON.stringify({ a: 'NOT ok' }));
return;
}
if (rPath !== undefined) {
console.log(rPath);
}
console.log(rJsonBody.path.toString()); //toLocaleLowerCase
if (rJsonBody.path.toString() === 'templates') {
const filePath = path.join(
__dirname,
`/views/templates/${rJsonBody.file.toString()}`
);
console.log(filePath);
fs.writeFileSync(filePath, rJsonBody.contents.toString());
} else {
const viewpath = path.join(
__dirname,
`/views/${rJsonBody.file.toString()}`
);
fs.writeFileSync(viewpath, rJsonBody.contents);
}
res.send(JSON.stringify({ a: 'ok' }));
});
app.get('/template/', async (req, res) => {
const filePath = path.join(__dirname, `/views/templates/`);
// fs.readdir(filePath, (err, files) => {
// files.forEach((file) => {
// console.log(file);
// });
// });
const viewpath = path.join(__dirname, `/views/`);
// fs.readdir(viewpath, (err, files) => {
// files.forEach((file) => {
// console.log(file);
// });
// });
//..var files = fs.readdirSync('C:/tmp').filter(fn => fn.endsWith('.csv'));
//changing templates detail,main and setmain can effect the site since. These files were included with HomeView and may get overwritten if you fetch.
//You can add /Templates/<Based on group name>.hbs <- PULL GROUP LIST AND HELP WITH FILENAME!!!
//
let rTemplate = req.query.edit;
if (rTemplate !== undefined) {
console.log(rTemplate);
}
fs.readdirSync(filePath)
.filter((fn) => fn.endsWith('.hbs'))
.forEach((file) => {
//console.log(file);
});
fs.readdirSync(viewpath)
.filter((fn) => fn.endsWith('.hbs'))
.forEach((file) => {
//// console.log(file);
});
let fviews = fs.readdirSync(viewpath).filter((fn) => fn.endsWith('.hbs'));
let ftemplates = fs.readdirSync(filePath).filter((fn) => fn.endsWith('.hbs'));
const groups = await db.getGroups();
let newgroup = [];
groups.forEach((g) => newgroup.push(`${g.value.toLocaleLowerCase()}.hbs`));
/// console.log(groups);
let file_json = { view: fviews, templates: ftemplates, groups: newgroup };
// console.log(file_json);
res.render('templatebody', {
layout: 'template',
//setter: 'this is the setup page',
filetemplates: JSON.stringify(file_json),
version: version,
});
});
/* //not being used
app.get('/setup/AT', (req, res) => {
//Serves the body of the page aka "main.handlebars" to the container //aka "index.handlebars"
var sql = datalayer.SQL_Get_All_Topics;
var params = [];
console.log('GO');
datalayer.db.all(sql, params, function (err, result) {
if (err) {
console.log('error' + err.message);
return;
}
//console.log(result);
// result.forEach((result) => {
// console.log(result);
// });
//How to send a dynamic array of TestArray?
res.send(JSON.stringify(result));
});
}); */
//The Setup Page pulls the data from here.
app.get('/data', async (req, res) => {
//Serves the body of the page aka "main.handlebars" to the container //aka "index.handlebars"
//console.log(req.url);
// console.dir(req.originalUrl); // '/admin/new?a=b' (WARNING: beware query string)
// console.dir(req.baseUrl); // '/admin'
// console.log(req.path); // '/new'
// console.dir(req.baseUrl + req.path); // '/admin/new' (full path without query string)
const settings = await db.getSettings();
const result = await db.getAllTopics();
console.log('app.get(/data');
var mtdata = { setting: settings, topic: result };
// console.log('---mtdata----');
// console.log(mtdata);
// console.log('----mtdata---');
// result.forEach((result) => {
// console.log(result.topic);
// });
//How to send a dynamic array of TestArray?
res.send(JSON.stringify(mtdata));
});
//the setup posts the settings and topics data here
app.post('/mtsettings', async (req, res) => {
console.log('-----------');
//console.log(req.body);
var jsonsetting = req.body;
// console.log(jsonsetting.setting.mqtt_ip);
db.setSettings(
jsonsetting.setting.mqtt_ip.toString(),
jsonsetting.setting.mqtt_id.toString(),
jsonsetting.setting.mqtt_pw.toString(),
JSON.stringify(jsonsetting.topic)
);
console.log('-----------');
res.send(req.body);
//sockserver.terminate();//close();
if (client !== undefined) {
console.log(`Clien Count: ${sockserver.clients.size}`);
sockserver.clients.forEach((client) => {
client.terminate();
client.close();
});
client.end(false, async () => {
//StartWSMQTT();
MQTTRunning = await SetupMQTT();
//console.log(`mqtt I running: ${MQTTRunning}`);
app.set('MQTTRunning', MQTTRunning);
});
}
// sockserver.clients.forEach((client) => {
// //console.log(`distributing message: ${JSON.stringify(client)} `);
// // client.send(JSON.stringify(result));
// var rf = { refresh: true };
// client.send(JSON.stringify(rf));
// });
});
/* app.get('/door', (req, res) => {
//Serves the body of the page aka "main.handlebars" to the container //aka "index.handlebars"
//res.render("main", { layout: "index", doors: "DOOORABLE" });
//console.log(req);
if (dataupdated == true) {
res.send(dataupdated);
dataupdated = false;
} else {
res.send(dataupdated);
}
}); */
/////////////
// When the topic group is sent, set a timer on the JS page to request groups.
//When the groups come in from a Fetch. Compare with the array in dooring. If they dont match. location.reload.
app.get('/gp', (req, res) => {
let groups = db.getGroups();
res.send(JSON.stringify(groups));
});
app.get('/detail', async (req, res) => {
// console.log(req.query.topic);
// console.log(req.query.limit);
let limit = req.query.limit;
if (limit === undefined) {
limit = 100;
}
const result = await db.getDetailsbyTopic(req.query.topic, limit);
const groups = await db.getGrouplist(result[0].group);
//console.log(groups);
//res.send(JSON.stringify(groups) + '<br><br>' + JSON.stringify(result));
res.render('details', {
layout: 'index',
//setter: 'this is the setup page',
version: version,
groups: groups,
topics: result,
});
});
/* app.get('/users', (req, res) => {
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'myapp'
});
connection.query('SELECT * FROM users', (error, results, fields) => {
if (error) throw error;
res.render('users', { users: results });
});
}); */
// app.use("/", function (req, res) {
// res.sendFile(path.join(__dirname + "/express/index.html"));
// //__dirname : It will resolve to your project folder.
// });
//app.set("view engine", "hbs"); //instead of app.engine('handlebars', handlebars({
//console.log(`mqtt root running: ${MQTTRunning}`);
const StartWSMQTT = async function () {
WSRunning = await SetupWebSocket();
MQTTRunning = await SetupMQTT();
//console.log(`mqtt I running: ${MQTTRunning}`);
app.set('MQTTRunning', MQTTRunning);
};
StartWSMQTT();
const server = http.createServer(app);
const port = 3000;
server.listen(port);
console.debug('Server listening on port ' + port);
/*
const os = require('os');
const interfaces = os.networkInterfaces();
const addresses = [];
for (const k in interfaces) {
for (const addr of interfaces[k]) {
if (addr.family === 'IPv4' && !addr.internal) {
addresses.push(addr.address);
}
}
}
//console.log(addresses[0]);
//console.log(server.address());
mdns.on('query', function (query) {
//console.log('got a query packet:', query);
//console.log('got a query packet:', server.address().address);
// iterate over all questions to check if we should respond
query.questions.forEach(function (q) {
if (q.type === 'A' && q.name === 'homeview.local') {
//console.log('got a query SSSSpacket:', query);
// send an A-record response for example.local
const ip = addresses[0].toString();
// console.log(ip);
mdns.respond({
answers: [
{
name: 'homeview.local',
type: 'A',
ttl: 300,
data: ip,
},
],
});
}
});
});
*/