1 Commits 3b92170e46 ... d9c99c6556

Autor SHA1 Mensaje Fecha
  James Peret d9c99c6556 Restart routers commands %!s(int64=7) %!d(string=hace) años
Se han modificado 4 ficheros con 89 adiciones y 28 borrados
  1. 2 2
      index.js
  2. 55 21
      relay_controller.js
  3. 15 4
      views/index.html
  4. 17 1
      wifi_routers.js

+ 2 - 2
index.js

@@ -28,10 +28,10 @@ wifi_routers = require('./wifi_routers');
28 28
 var channel = io
29 29
   .of('/')
30 30
   .on('connection', function (socket) {
31
-      relay_controller.monitor(socket, channel);
32 31
       wifi_routers.monitor(socket, channel);
32
+      relay_controller.monitor(socket, channel);
33 33
   });
34 34
 
35
-relay_controller.start(router, channel);
36 35
 external_temperature_sensor.start(router, channel);
37 36
 wifi_routers.start();
37
+relay_controller.start(router, channel, wifi_routers);

+ 55 - 21
relay_controller.js

@@ -1,26 +1,29 @@
1 1
 var logger = require('winston');
2 2
 var rpio = require("rpio")
3
+var channel;
4
+var wifi_routers;
3 5
 
4 6
 var relay_status = [
5
-  {
6
-    code: "L01",
7
-    status: true,
8
-    pin: 35
9
-  }
7
+  { code: "L01", status: false, pin: 35 }, // Light - plug 4
8
+  { code: "R01", status: true, pin: 33 }, // Satelite Link router - plug 3
9
+  { code: "R02", status: true, pin: 31 }, // Wifi Router - plug 2
10
+  { code: "???", status: true, pin: 29 } // Unused - plug 1
10 11
 ]
11 12
 
12
-module.exports.start = function(router, channel){
13
+module.exports.start = function(router, channel_config, wifi_routers_config){
14
+  channel = channel_config;
15
+  wifi_routers = wifi_routers_config;
13 16
   logger.info("Starting Relay controller module");
14 17
   rpio.open(relay_status[0].pin, rpio.OUTPUT, rpio.LOW);
18
+  rpio.open(relay_status[1].pin, rpio.OUTPUT, rpio.HIGH);
19
+  rpio.open(relay_status[2].pin, rpio.OUTPUT, rpio.HIGH);
20
+  rpio.open(relay_status[3].pin, rpio.OUTPUT, rpio.HIGH);
15 21
   router.post('/switch_relay', function (req, res) {
16 22
     logger.info("Received request for switching relays", req.body);
17 23
     if(req.body != undefined){
18 24
       var relay = req.body;
19 25
       if(relay.code != undefined && relay.status != undefined){
20
-        var done = switchRelay(relay.code, relay.status);
21
-        if(done){
22
-            channel.emit('relay_switch', { code: relay.code, status: relay.status });
23
-        }
26
+        switchRelay(relay.code, relay.status);
24 27
       }
25 28
     }
26 29
     res.sendStatus(200);
@@ -40,18 +43,49 @@ module.exports.monitor = function(socket, channel){
40 43
 var switchRelay = function(code, status){
41 44
   switch (code) {
42 45
     case "L01":
43
-      // switch on light relay
44
-      if(status == true) {
45
-        rpio.write(relay_status[0].pin, rpio.HIGH);
46
-        logger.debug("Switched L01 ON");
47
-      } else {
48
-        rpio.write(relay_status[0].pin, rpio.LOW);
49
-        logger.debug("Switched L01 OFF");
50
-      }
51
-      relay_status[0].status = status;
52
-      return true;
46
+      relay_L01(status)
47
+      break;
48
+    case "R01":
49
+      relay_restart(code, 30000, 0, 1)
50
+      break;
51
+    case "R02":
52
+      relay_restart(code, 5000, 1, 2)
53 53
       break;
54 54
     default:
55
-      return false;
55
+
56 56
   }
57 57
 }
58
+
59
+var relay_L01 = function(status){
60
+  // switch on light relay
61
+  if(status == true) {
62
+    rpio.write(relay_status[0].pin, rpio.HIGH);
63
+    logger.info("Switched L01 ON");
64
+  } else {
65
+    rpio.write(relay_status[0].pin, rpio.LOW);
66
+    logger.info("Switched L01 OFF");
67
+  }
68
+  relay_status[0].status = status;
69
+  channel.emit('relay_switch', { code: "L01", status: status });
70
+}
71
+
72
+var relay_restart = function(code, time, wifi_id, relay_id){
73
+  // Restart router
74
+  channel.emit('relay_switch', { code: code, status: false });
75
+  wifi_routers.change_state(wifi_id, "Restarting");
76
+  logger.info("Switched " + code + " OFF");
77
+  relay_status[relay_id].status = false;
78
+  rpio.write(relay_status[relay_id].pin, rpio.LOW);
79
+  setTimeout(function () {
80
+    channel.emit('relay_switch', { code: code, status: true });
81
+    logger.info("Switched " + code + " ON");
82
+    relay_status[relay_id].status = true;
83
+    rpio.write(relay_status[relay_id].pin, rpio.HIGH);
84
+    setTimeout(function () {
85
+      wifi_routers.check_status(wifi_id);
86
+      setTimeout(function () {
87
+        wifi_routers.send_status();
88
+      }, 3000);
89
+    }, 15000);
90
+  }, time);
91
+}

+ 15 - 4
views/index.html

@@ -54,7 +54,7 @@
54 54
             </dl>
55 55
             <div class="row spacer-sm">
56 56
               <div class="col-6">
57
-                <button type="button" class="btn btn-outline-secondary btn-sm btn-block disabled"><i class="fa fa-refresh" aria-hidden="true"></i> Main Router</button>
57
+                <button type="button" v-on:click="restartRouter02()" class="btn btn-outline-secondary btn-sm btn-block"><i class="fa fa-refresh" aria-hidden="true"></i> Main Router</button>
58 58
               </div>
59 59
               <div class="col-6">
60 60
                 <button type="button" class="btn btn-outline-secondary btn-sm btn-block disabled"><i class="fa fa-refresh" aria-hidden="true"></i> Repeater 1</button>
@@ -67,7 +67,7 @@
67 67
         <div class="statcard statcard-divided">
68 68
           <div class="statcard-header">
69 69
             <i class="fa fa-wifi icon-header" aria-hidden="true"></i>
70
-            <h3 class="statcard-number">ACTIVE</h3>
70
+            <h3 class="statcard-number"  style="text-transform: uppercase;">{{wifi_routers_status.satelite}}</h3>
71 71
             <span class="statcard-title">Sattelite Connection</span>
72 72
           </div>
73 73
           <div class="statcard-content">
@@ -81,7 +81,7 @@
81 81
             </dl>
82 82
             <div class="row spacer-sm">
83 83
               <div class="col-12">
84
-                <button type="button" class="btn btn-outline-secondary btn-sm btn-block disabled"><i class="fa fa-refresh" aria-hidden="true"></i> Reconect</button>
84
+                <button type="button" v-on:click="restartRouter01()" class="btn btn-outline-secondary btn-sm btn-block"><i class="fa fa-refresh" aria-hidden="true"></i> Reconect</button>
85 85
               </div>
86 86
             </div>
87 87
           </div>
@@ -145,7 +145,8 @@
145 145
       wifi_routers_status : {
146 146
         global : "Unkown",
147 147
         main_router : "--",
148
-        repeater_1 : "--"
148
+        repeater_1 : "--",
149
+        satelite: "Unkown"
149 150
       }
150 151
     },
151 152
     methods: {
@@ -157,6 +158,12 @@
157 158
           status = true;
158 159
         }
159 160
         http_post("/switch_relay", { code: "L01", status: status})
161
+      },
162
+      restartRouter01: function(){
163
+        http_post("/switch_relay", { code: "R01", status: true})
164
+      },
165
+      restartRouter02: function(){
166
+        http_post("/switch_relay", { code: "R02", status: true})
160 167
       }
161 168
     }
162 169
   })
@@ -168,6 +175,7 @@
168 175
 
169 176
   socket.on('wifi_routers', function (data) {
170 177
     console.log(data);
178
+    app.wifi_routers_status.satelite = data[0].status;
171 179
     app.wifi_routers_status.main_router = data[1].status;
172 180
     app.wifi_routers_status.repeater_1 = data[2].status;
173 181
     if(data[1].status == "Unkown" || data[2].status == "Unkown") {
@@ -176,9 +184,12 @@
176 184
       app.wifi_routers_status.global = "Offline"
177 185
     } else if(data[1].status == "Inactive" || data[2].status == "Inactive") {
178 186
       app.wifi_routers_status.global = "Degraded"
187
+    } else if(data[1].status == "Restarting" || data[2].status == "Restarting") {
188
+      app.wifi_routers_status.global = "Degraded"
179 189
     } else {
180 190
       app.wifi_routers_status.global = "Active"
181 191
     }
192
+
182 193
   });
183 194
 
184 195
   socket.on('relay_switch', function (data) {

+ 17 - 1
wifi_routers.js

@@ -1,5 +1,6 @@
1 1
 var logger = require('winston');
2 2
 var http = require('http');
3
+var channel;
3 4
 
4 5
 var wifi_routers = [
5 6
   {
@@ -25,18 +26,33 @@ var wifi_routers = [
25 26
 module.exports.start = function(){
26 27
   logger.info("Starting wifi routers controller module");
27 28
   setInterval(function () {
29
+    logger.info("Checking WIFI Routers...");
28 30
     checkURL(wifi_routers[0].url, 0);
29 31
     checkURL(wifi_routers[1].url, 1);
30 32
     checkURL(wifi_routers[2].url, 2);
31 33
   }, 60000);
32 34
 }
33 35
 
34
-module.exports.monitor = function(socket, channel){
36
+module.exports.monitor = function(socket, channel_config){
37
+  channel = channel_config;
35 38
   socket.on('wifi_routers_update', function (data) {
36 39
     socket.emit("wifi_routers", wifi_routers);
37 40
   });
38 41
 }
39 42
 
43
+module.exports.change_state = function(id, status){
44
+  wifi_routers[id].status = status;
45
+  channel.emit("wifi_routers", wifi_routers);
46
+}
47
+
48
+module.exports.check_status = function(id){
49
+  checkURL(wifi_routers[id].url, id);
50
+}
51
+
52
+module.exports.send_status = function(){
53
+  channel.emit("wifi_routers", wifi_routers);
54
+}
55
+
40 56
 var checkURL = function(url, n){
41 57
   http.get(url, function (res) {
42 58
     wifi_routers[n].status = "Active"