|
var logger = require('winston');
var http = require('http');
var channel;
var relay_controller;
var error_count_before_restart = 60;
var wifi_routers = [
{
title : "Satelite Link",
status: "Unkown",
url: "http://192.168.0.1/",
device: "HT2000",
errors: 0,
restarted: false,
code: "R01"
},
{
title : "Main Router",
status: "Unkown",
url: "http://192.168.100.1/",
device: "Wireless N 300M",
errors: 0,
restarted: false,
code: "R02"
},
{
title : "Repeater 1",
status: "Unkown",
url: "http://192.168.100.2/",
device: "300M Wireless N Nano Router",
errors: 0,
restarted: false
}
]
module.exports.start = function(relay_controller_config){
relay_controller = relay_controller_config;
logger.info("Starting wifi routers controller module");
checkRouters()
setInterval(function () {
checkRouters()
}, 60000);
}
module.exports.monitor = function(socket, channel_config){
channel = channel_config;
socket.on('wifi_routers_update', function (data) {
socket.emit("wifi_routers", wifi_routers);
});
}
module.exports.change_state = function(id, status){
wifi_routers[id].status = status;
channel.emit("wifi_routers", wifi_routers);
}
module.exports.check_status = function(id){
checkURL(wifi_routers[id].url, id);
}
module.exports.send_status = function(){
channel.emit("wifi_routers", wifi_routers);
}
var checkRouters = function(){
logger.info("Checking WIFI Routers...");
checkURL(wifi_routers[0].url, 0);
checkURL(wifi_routers[1].url, 1);
checkURL(wifi_routers[2].url, 2);
setInterval(function () {
checkRestart()
}, 5000);
}
var checkURL = function(url, n){
http.get(url, function (res) {
wifi_routers[n].status = "Active"
wifi_routers[n].errors = 0;
}).on('error', function(e) {
wifi_routers[n].status = "Inactive";
wifi_routers[n].errors += 1;
logger.info(wifi_routers[n].title + " is " + wifi_routers[n].status + " (" + wifi_routers[n].errors + " errors)");
});;
}
var checkRestart = function() {
for (var i = 0; i < wifi_routers.length; i++) {
if(wifi_routers[i].errors > error_count_before_restart) {
if(wifi_routers[i].restarted == false){
if(wifi_routers[i].code != undefined){
wifi_routers[i].restarted = true;
relay_controller.switchRelay(wifi_routers[i].code, true)
}
}
}
}
}
|