-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (107 loc) · 3.42 KB
/
index.js
File metadata and controls
120 lines (107 loc) · 3.42 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
const serveStatic = require('serve-static')
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const request = require('request');
const browserify = require('browserify');
const fs = require('fs');
const moment = require('moment');
const colors = require('colors');
module.exports = {
all_tiles: [],
polling:{},
start: function (config, host, port) {
let check_result = this._checkConfig(config);
if (check_result !== true) {
this.log("error", "Error in config: " + check_result.bold);
process.exit(1);
}
fs.writeFileSync(__dirname + '/src/config.json',JSON.stringify(config))
let b = browserify();
b.add('./src/main.js');
b.bundle().pipe(fs.createWriteStream(__dirname + '/dist/main.js'));
this.all_tiles = config.dashboards.map(d => d.tiles).reduce((a, b) => a.concat(b), [])
this.all_tiles.forEach(t => {
if (t.url) {
let poll_rate = t.poll_rate || 60000;
this.log('info', "Registering polling for tile "+t.id.blue+" (every "+(poll_rate/1000).toString().blue+" seconds)")
this.polling[t.id] = setInterval(this.updateTile, poll_rate, t);
this.updateTile(t, true);
}
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/dist/index.html');
});
app.post('/push/:tile_id', function (req, res) {
let tile_id = req.params.tile_id;
let tile = config.tiles.find(t => t.id === tile_id);
let data = req.body;
io.emit('update_tile', { ...tile, data });
res.send("OK");
});
app.use(serveStatic('./dist'));
app.use(express.json());
port = port || 5000;
host = host || "0.0.0.0";
http.listen(port, host, () => this.log('info', "Rijola is listening on " + host.blue.bold + ":" + port.toString().blue.bold));
io.on('connection', (socket) => {
this.log('info', 'new client connected');
this.all_tiles.forEach(t => {
if (t.data)
io.emit('update_tile', t)
});
setInterval(() => {
socket.emit('ping')
}, 10000);
});
},
updateTile: function(tile, avoid_log) {
let url = tile.url;
let self = this;
if (!avoid_log)
this.log('info', 'polling ' + tile.url.blue + " for tile " + tile.id.blue);
request.get({
url,
rejectUnauthorized: false,
}, function (error, response, body) {
if (error) {
self.log("error", error);
return;
}
try {
let data = JSON.parse(body)
tile.data = data;
io.emit('update_tile', tile)
}
catch (e) {
self.log("error", e.message);
}
})
},
_checkConfig: function (config) {
if (!config.dashboards)
return 'missing root element "dashboard"';
if (typeof config.dashboards !== typeof [])
return 'root element "dashboard" must be array';
this.log('info','Config check is OK!')
return true;
},
log: function (level, txt) {
let date = moment().format('YYYY-MM-DD HH:mm:ss').toString().cyan
level = level.toUpperCase()
if (level === 'INFO') {
level = level.green;
txt = txt.green
}
else if (level === 'WARN') {
level = level.yellow;
txt = txt.yellow.underline
}
else if (level === 'ERROR') {
level = level.red;
txt = txt.red.bold
}
console.log("["+date+" "+level+"] "+txt)
}
}