wifi monitors

Added script for monitoring wifi routers ip address thru an http call and send results thru socket connection.

James Peret 7 years ago
parent
commit
3b92170e46
6 changed files with 120 additions and 10 deletions
  1. 13 0
      database.js
  2. 26 2
      external_temperature_sensor.js
  3. 4 1
      index.js
  4. 2 0
      package.json
  5. 29 7
      views/index.html
  6. 46 0
      wifi_routers.js

+ 13 - 0
database.js

@@ -0,0 +1,13 @@
1
+const Sequelize = require('sequelize');
2
+const sequelize = new Sequelize('postgres://postgres:postgres-pi@localhost:5432/home_automation');
3
+
4
+module.exports = function(){
5
+  sequelize
6
+    .authenticate()
7
+    .then(() => {
8
+        console.log('Connection has been established successfully.');
9
+    })
10
+    .catch(err => {
11
+        console.error('Unable to connect to the database:', err);
12
+     });
13
+}

+ 26 - 2
external_temperature_sensor.js

@@ -2,8 +2,17 @@ var logger = require('winston');
2 2
 var sensor = require("ds18x20")
3 3
 var devices;
4 4
 var channel;
5
+var current_temp;
5 6
 
6
-module.exports.start = function(channel_config){
7
+const Sequelize = require('sequelize');
8
+const sequelize = new Sequelize('postgres://postgres:postgres-pi@localhost:5432/home_automation');
9
+
10
+sequelize
11
+  .authenticate()
12
+  .then(() => { logger.info('Connection to database has been established successfully.'); })
13
+  .catch(err => { logger.info('Unable to connect to the database:', err); });
14
+
15
+module.exports.start = function(router, channel_config){
7 16
   channel = channel_config
8 17
   logger.info("Starting External temperature module");
9 18
   var isLoaded = sensor.isDriverLoaded();
@@ -14,11 +23,19 @@ module.exports.start = function(channel_config){
14 23
     setInterval(function () {
15 24
       var temperature = readSensor();
16 25
     }, 5000);
26
+    setInterval(function () {
27
+      saveData();
28
+    }, 300000);
17 29
   } else {
18 30
     logger.info("ds18x20 sensor drivers are not loaded.")
19 31
   }
32
+  router.get('/external_temperature', function (req, res) {
33
+    logger.info("Received request for external temperature data");
34
+    sequelize.query("SELECT * FROM conditions", { type: sequelize.QueryTypes.SELECT}).then(data => {
35
+      res.json(data);
36
+    })
20 37
 
21
-
38
+  });
22 39
 }
23 40
 
24 41
 function readSensor() {
@@ -31,5 +48,12 @@ function readSensor() {
31 48
   		console.log(err);
32 49
   	}
33 50
   	channel.emit('temperature_external', { temperature: temp });
51
+    current_temp = temp;
34 52
   });
35 53
 }
54
+
55
+function saveData(){
56
+  sequelize.query("INSERT INTO conditions(time, location, temperature) VALUES (NOW(), 'external', " + current_temp + ");").spread((results, metadata) => {
57
+      // Results will be an empty array and metadata will contain the number of affected rows.
58
+    })
59
+}

+ 4 - 1
index.js

@@ -23,12 +23,15 @@ router.get('/', function (req, res) {
23 23
 
24 24
 external_temperature_sensor = require('./external_temperature_sensor');
25 25
 relay_controller = require('./relay_controller');
26
+wifi_routers = require('./wifi_routers');
26 27
 
27 28
 var channel = io
28 29
   .of('/')
29 30
   .on('connection', function (socket) {
30 31
       relay_controller.monitor(socket, channel);
32
+      wifi_routers.monitor(socket, channel);
31 33
   });
32 34
 
33 35
 relay_controller.start(router, channel);
34
-external_temperature_sensor.start(channel);
36
+external_temperature_sensor.start(router, channel);
37
+wifi_routers.start();

+ 2 - 0
package.json

@@ -14,7 +14,9 @@
14 14
     "ejs": "^2.5.7",
15 15
     "express": "^4.15.4",
16 16
     "express-ejs-layouts": "^2.3.1",
17
+    "pg": "^7.2.0",
17 18
     "rpio": "^0.9.17",
19
+    "sequelize": "^4.7.5",
18 20
     "socket.io": "^2.0.3",
19 21
     "winston": "^2.3.1"
20 22
   }

+ 29 - 7
views/index.html

@@ -40,7 +40,7 @@
40 40
         <div class="statcard statcard-divided">
41 41
           <div class="statcard-header">
42 42
             <i class="fa fa-wifi icon-header" aria-hidden="true"></i>
43
-            <h3 class="statcard-number">ACTIVE</h3>
43
+            <h3 class="statcard-number" style="text-transform: uppercase;">{{wifi_routers_status.global}}</h3>
44 44
             <span class="statcard-title">Wifi Routers</span>
45 45
           </div>
46 46
           <div class="statcard-content">
@@ -48,9 +48,9 @@
48 48
               <dt class="col-sm-8">Number of devices</dt>
49 49
               <dd class="col-sm-4">2</dd>
50 50
               <dt class="col-sm-8">Router Main</dt>
51
-              <dd class="col-sm-4">OK</dd>
51
+              <dd class="col-sm-4">{{wifi_routers_status.main_router}}</dd>
52 52
               <dt class="col-sm-8 text-truncate">Repeater 1</dt>
53
-              <dd class="col-sm-4">OK</dd>
53
+              <dd class="col-sm-4">{{wifi_routers_status.repeater_1}}</dd>
54 54
             </dl>
55 55
             <div class="row spacer-sm">
56 56
               <div class="col-6">
@@ -141,7 +141,12 @@
141 141
       temperature_external: "--",
142 142
       light01 : "--",
143 143
       light01_status : undefined,
144
-      light01_btn : ""
144
+      light01_btn : "",
145
+      wifi_routers_status : {
146
+        global : "Unkown",
147
+        main_router : "--",
148
+        repeater_1 : "--"
149
+      }
145 150
     },
146 151
     methods: {
147 152
       switchLight01: function(){
@@ -152,9 +157,6 @@
152 157
           status = true;
153 158
         }
154 159
         http_post("/switch_relay", { code: "L01", status: status})
155
-      },
156
-      example: function(example_var){
157
-        app.example();
158 160
       }
159 161
     }
160 162
   })
@@ -164,6 +166,21 @@
164 166
     app.temperature_external = temp + "ºC";
165 167
   });
166 168
 
169
+  socket.on('wifi_routers', function (data) {
170
+    console.log(data);
171
+    app.wifi_routers_status.main_router = data[1].status;
172
+    app.wifi_routers_status.repeater_1 = data[2].status;
173
+    if(data[1].status == "Unkown" || data[2].status == "Unkown") {
174
+      app.wifi_routers_status.global = "Unkown"
175
+    } else if(data[1].status == "Inactive" && data[2].status == "Inactive") {
176
+      app.wifi_routers_status.global = "Offline"
177
+    } else if(data[1].status == "Inactive" || data[2].status == "Inactive") {
178
+      app.wifi_routers_status.global = "Degraded"
179
+    } else {
180
+      app.wifi_routers_status.global = "Active"
181
+    }
182
+  });
183
+
167 184
   socket.on('relay_switch', function (data) {
168 185
     console.log(data)
169 186
     var status = "ON";
@@ -186,5 +203,10 @@
186 203
     socket.emit('relay_switch_update', {});
187 204
   }, 3000000);
188 205
 
206
+  socket.emit('wifi_routers_update', {});
207
+  setInterval(function () {
208
+    socket.emit('wifi_routers_update', {});
209
+  }, 30000);
210
+
189 211
 
190 212
 </script>

+ 46 - 0
wifi_routers.js

@@ -0,0 +1,46 @@
1
+var logger = require('winston');
2
+var http = require('http');
3
+
4
+var wifi_routers = [
5
+  {
6
+    title : "Satelite Link",
7
+    status: "Unkown",
8
+    url: "http://192.168.0.1/",
9
+    device: "HT2000"
10
+  },
11
+  {
12
+    title : "Main Router",
13
+    status: "Unkown",
14
+    url: "http://192.168.100.1/",
15
+    device: "Wireless N 300M"
16
+  },
17
+  {
18
+    title : "Repeater 1",
19
+    status: "Unkown",
20
+    url: "http://192.168.100.100/",
21
+    device: "300M Wireless N Nano Router"
22
+  }
23
+]
24
+
25
+module.exports.start = function(){
26
+  logger.info("Starting wifi routers controller module");
27
+  setInterval(function () {
28
+    checkURL(wifi_routers[0].url, 0);
29
+    checkURL(wifi_routers[1].url, 1);
30
+    checkURL(wifi_routers[2].url, 2);
31
+  }, 60000);
32
+}
33
+
34
+module.exports.monitor = function(socket, channel){
35
+  socket.on('wifi_routers_update', function (data) {
36
+    socket.emit("wifi_routers", wifi_routers);
37
+  });
38
+}
39
+
40
+var checkURL = function(url, n){
41
+  http.get(url, function (res) {
42
+    wifi_routers[n].status = "Active"
43
+  }).on('error', function(e) {
44
+    wifi_routers[n].status = "Inactive"
45
+  });;
46
+}