Added RPI Status

The client dashboard page continuously querys system data thru sockets.

James Peret 7 年之前
父節點
當前提交
53a222ca39
共有 6 個文件被更改,包括 124 次插入18 次删除
  1. 5 2
      index.js
  2. 1 0
      package.json
  3. 2 0
      relay_controller.js
  4. 16 0
      rpi_monitor.js
  5. 55 6
      views/index.html
  6. 45 10
      wifi_routers.js

+ 5 - 2
index.js

@@ -13,8 +13,8 @@ router.use(bodyParser.urlencoded({ extended: true }))
13 13
 router.use(bodyParser.json());
14 14
 router.use(express.static('public'))
15 15
 
16
-server.listen(3100, function () {
17
-  winston.info('Listening on port 3100');
16
+server.listen(8080, function () {
17
+  winston.info('Listening on port 8080');
18 18
 })
19 19
 
20 20
 router.get('/', function (req, res) {
@@ -24,14 +24,17 @@ router.get('/', function (req, res) {
24 24
 external_temperature_sensor = require('./external_temperature_sensor');
25 25
 relay_controller = require('./relay_controller');
26 26
 wifi_routers = require('./wifi_routers');
27
+rpi_monitor = require('./rpi_monitor');
27 28
 
28 29
 var channel = io
29 30
   .of('/')
30 31
   .on('connection', function (socket) {
31 32
       wifi_routers.monitor(socket, channel);
32 33
       relay_controller.monitor(socket, channel);
34
+      rpi_monitor.monitor(socket, channel);
33 35
   });
34 36
 
35 37
 external_temperature_sensor.start(router, channel);
36 38
 wifi_routers.start();
37 39
 relay_controller.start(router, channel, wifi_routers);
40
+rpi_monitor.start();

+ 1 - 0
package.json

@@ -18,6 +18,7 @@
18 18
     "rpio": "^0.9.17",
19 19
     "sequelize": "^4.7.5",
20 20
     "socket.io": "^2.0.3",
21
+    "systeminformation": "^3.26.2",
21 22
     "winston": "^2.3.1"
22 23
   }
23 24
 }

+ 2 - 0
relay_controller.js

@@ -89,3 +89,5 @@ var relay_restart = function(code, time, wifi_id, relay_id){
89 89
     }, 15000);
90 90
   }, time);
91 91
 }
92
+
93
+module.exports.switchRelay = switchRelay

+ 16 - 0
rpi_monitor.js

@@ -0,0 +1,16 @@
1
+var logger = require('winston');
2
+const sys = require('systeminformation');
3
+var channel;
4
+
5
+module.exports.start = function(){
6
+  logger.info("Starting RPI monitor module");
7
+}
8
+
9
+module.exports.monitor = function(socket, channel_config){
10
+  channel = channel_config;
11
+  socket.on('rpi_status_update', function (data) {
12
+    sys.getDynamicData(function(data) {
13
+        socket.emit("rpi_status", data);
14
+    })
15
+  });
16
+}

+ 55 - 6
views/index.html

@@ -21,12 +21,12 @@
21 21
           </div>
22 22
           <div class="statcard-content">
23 23
             <dl class="row">
24
-              <dt class="col-sm-3">CPU</dt>
25
-              <dd class="col-sm-9">45%</dd>
26
-              <dt class="col-sm-3">RAM</dt>
27
-              <dd class="col-sm-9">560M of 1024M</dd>
28
-              <dt class="col-sm-3 text-truncate">Disk</dt>
29
-              <dd class="col-sm-9">13.5Gb of 100Gb</dd>
24
+              <dt class="col-sm-4">CPU</dt>
25
+              <dd class="col-sm-8">{{rpi_status.current_load}}</dd>
26
+              <dt class="col-sm-4">RAM</dt>
27
+              <dd class="col-sm-8">{{rpi_status.memory_usage}}</dd>
28
+              <dt class="col-sm-4 text-truncate">Uptime</dt>
29
+              <dd class="col-sm-8">{{rpi_status.uptime}}</dd>
30 30
             </dl>
31 31
             <div class="row spacer-sm">
32 32
               <div class="col-12">
@@ -147,6 +147,13 @@
147 147
         main_router : "--",
148 148
         repeater_1 : "--",
149 149
         satelite: "Unkown"
150
+      },
151
+      rpi_status : {
152
+        current_load : "--",
153
+        ram_used : "--",
154
+        ram_total : "--",
155
+        memory_usage : "--",
156
+        uptime : "--"
150 157
       }
151 158
     },
152 159
     methods: {
@@ -192,6 +199,15 @@
192 199
 
193 200
   });
194 201
 
202
+  socket.on('rpi_status', function (data) {
203
+    console.log(data);
204
+    app.rpi_status.current_load = (Math.round( data.currentLoad.currentload * 10 ) / 10) + "%";
205
+    app.rpi_status.ram_used = bytesToSize(data.mem.used);
206
+    app.rpi_status.ram_total = bytesToSize(data.mem.total);
207
+    app.rpi_status.memory_usage = app.rpi_status.ram_used + " of " + app.rpi_status.ram_total
208
+    app.rpi_status.uptime = secondsToString(data.time.uptime);
209
+  });
210
+
195 211
   socket.on('relay_switch', function (data) {
196 212
     console.log(data)
197 213
     var status = "ON";
@@ -219,5 +235,38 @@
219 235
     socket.emit('wifi_routers_update', {});
220 236
   }, 30000);
221 237
 
238
+  socket.emit('rpi_status_update', {});
239
+  setInterval(function () {
240
+    socket.emit('rpi_status_update', {});
241
+  }, 5000);
242
+
243
+
244
+  function bytesToSize(bytes) {
245
+   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
246
+   if (bytes == 0) return '0 Byte';
247
+   var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
248
+   return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
249
+  };
250
+
251
+  function secondsToString(seconds) {
252
+    var numyears = Math.floor(seconds / 31536000);
253
+    var numdays = Math.floor((seconds % 31536000) / 86400);
254
+    var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
255
+    var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
256
+    var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
257
+    if(numyears != 0){
258
+      return numyears + " years " +  numdays + " days ";
259
+    }
260
+    if(numdays != 0){
261
+      return numdays + " days " + numhours + " hours ";
262
+    }
263
+    if(numhours != 0){
264
+      return numhours + " hours " + numminutes + " minutes "
265
+    }
266
+    if(numminutes != 0) {
267
+      return numminutes + " minutes ";
268
+    }
269
+    return numseconds + " seconds"
270
+  }
222 271
 
223 272
 </script>

+ 45 - 10
wifi_routers.js

@@ -1,35 +1,44 @@
1 1
 var logger = require('winston');
2 2
 var http = require('http');
3 3
 var channel;
4
+var relay_controller;
5
+var error_count_before_restart = 60;
4 6
 
5 7
 var wifi_routers = [
6 8
   {
7 9
     title : "Satelite Link",
8 10
     status: "Unkown",
9 11
     url: "http://192.168.0.1/",
10
-    device: "HT2000"
12
+    device: "HT2000",
13
+    errors: 0,
14
+    restarted: false,
15
+    code: "R01"
11 16
   },
12 17
   {
13 18
     title : "Main Router",
14 19
     status: "Unkown",
15 20
     url: "http://192.168.100.1/",
16
-    device: "Wireless N 300M"
21
+    device: "Wireless N 300M",
22
+    errors: 0,
23
+    restarted: false,
24
+    code: "R02"
17 25
   },
18 26
   {
19 27
     title : "Repeater 1",
20 28
     status: "Unkown",
21
-    url: "http://192.168.100.100/",
22
-    device: "300M Wireless N Nano Router"
29
+    url: "http://192.168.100.2/",
30
+    device: "300M Wireless N Nano Router",
31
+    errors: 0,
32
+    restarted: false
23 33
   }
24 34
 ]
25 35
 
26
-module.exports.start = function(){
36
+module.exports.start = function(relay_controller_config){
37
+  relay_controller = relay_controller_config;
27 38
   logger.info("Starting wifi routers controller module");
39
+  checkRouters()
28 40
   setInterval(function () {
29
-    logger.info("Checking WIFI Routers...");
30
-    checkURL(wifi_routers[0].url, 0);
31
-    checkURL(wifi_routers[1].url, 1);
32
-    checkURL(wifi_routers[2].url, 2);
41
+    checkRouters()
33 42
   }, 60000);
34 43
 }
35 44
 
@@ -53,10 +62,36 @@ module.exports.send_status = function(){
53 62
   channel.emit("wifi_routers", wifi_routers);
54 63
 }
55 64
 
65
+var checkRouters = function(){
66
+  logger.info("Checking WIFI Routers...");
67
+  checkURL(wifi_routers[0].url, 0);
68
+  checkURL(wifi_routers[1].url, 1);
69
+  checkURL(wifi_routers[2].url, 2);
70
+  setInterval(function () {
71
+    checkRestart()
72
+  }, 5000);
73
+}
74
+
56 75
 var checkURL = function(url, n){
57 76
   http.get(url, function (res) {
58 77
     wifi_routers[n].status = "Active"
78
+    wifi_routers[n].errors = 0;
59 79
   }).on('error', function(e) {
60
-    wifi_routers[n].status = "Inactive"
80
+    wifi_routers[n].status = "Inactive";
81
+    wifi_routers[n].errors += 1;
82
+    logger.info(wifi_routers[n].title + " is " + wifi_routers[n].status + " (" + wifi_routers[n].errors + " errors)");
61 83
   });;
62 84
 }
85
+
86
+var checkRestart = function() {
87
+  for (var i = 0; i < wifi_routers.length; i++) {
88
+    if(wifi_routers[i].errors > error_count_before_restart) {
89
+      if(wifi_routers[i].restarted == false){
90
+        if(wifi_routers[i].code != undefined){
91
+          wifi_routers[i].restarted = true;
92
+          relay_controller.switchRelay(wifi_routers[i].code, true)
93
+        }
94
+      }
95
+    }
96
+  }
97
+}