Skip to content

Commit

Permalink
Merge pull request #380 from prey/connection-state-fix
Browse files Browse the repository at this point in the history
Connection state fix + 2 action fixes
  • Loading branch information
javo authored Jun 26, 2018
2 parents db2b137 + 67f4b9a commit 7cca852
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 68 deletions.
2 changes: 1 addition & 1 deletion lib/agent/actions/alarm/windows.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var join = require('path').join,
unmute = join(__dirname, 'bin', 'unmuter.exe'),
voladjust = join(__dirname, 'bin', 'voldjust.exe');
voladjust = join(__dirname, 'bin', 'voladjust.exe');

exports.play = join(__dirname, 'bin', 'mpg123.exe');
exports.raise_volume = unmute + ' & ' + voladjust + ' 100';
2 changes: 1 addition & 1 deletion lib/agent/plugins/control-panel/long-polling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var request = function(re_schedule) {
host: host,
username: api_key,
password: 'x',
timeout: 1000 * 120,
timeout: 1000 * 60 * 5,
user_agent: user_agent
};

Expand Down
9 changes: 5 additions & 4 deletions lib/agent/providers/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var network = require('network'),
exp = module.exports,
common = require('../../common'),
logger = common.logger.prefix('network'),
config = common.config,
needle = require('needle');

var ap_list_callbacks = [],
Expand Down Expand Up @@ -168,10 +169,10 @@ exp.get_open_access_points_list = function(callback) {

};

exp.get_connection_status = function(args, cb) {
var proxy = args.proxy,
protocol = args.protocol,
host = args.host,
exp.get_connection_status = function(cb) {
var proxy = config.get('try_proxy'),
protocol = config.get('control-panel').protocol,
host = config.get('control-panel').host,
opts = {};

if (proxy) opts.proxy = proxy;
Expand Down
10 changes: 5 additions & 5 deletions lib/agent/triggers/auto-connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var connected = false;

var restart_reconnection = function() {
stop_waiting();
reconnect_time = 60000; // One minute
reconnect_time = 1000 * 60 * 3; // Three minutes
wait_normal_reconnection();
}

Expand Down Expand Up @@ -54,7 +54,7 @@ var stop_waiting = function() {
}

exports.start = function(opts, cb) {
hooks.on('connected_internet', function() {
hooks.on('connected', function() {
network.get_active_access_point(function(err, ap) {
if (was_disconnected) {
logger.info("Connection achieved! " + (ap ? ap.ssid || "" : ""));
Expand All @@ -66,7 +66,7 @@ exports.start = function(opts, cb) {
stop_waiting();
});

hooks.on('disconnected_internet', function() {
hooks.on('disconnected', function() {
was_disconnected = true;
connected = false;
if (config.get('auto_connect')) {
Expand All @@ -80,8 +80,8 @@ exports.start = function(opts, cb) {
}

exports.stop = function() {
hooks.remove('connected_internet');
hooks.remove('disconnected_internet');
hooks.remove('connected');
hooks.remove('disconnected');

if (emitter) {
emitter.removeAllListeners();
Expand Down
69 changes: 16 additions & 53 deletions lib/agent/triggers/connection/index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,43 @@
var join = require('path').join,
is_online = require('internet-available'),
base_path = join(__dirname, '..', '..'),
hooks = require(join(base_path, 'hooks')),
network = require(join(base_path, 'providers', 'network')),
common = require('./../../common'),
config = common.config,
Emitter = require('events').EventEmitter;

var emitter,
status = null, // either connected or disconnected
internet_status = null,
status, // either connected or disconnected
checking = false,
interval_checker;

var args = {
proxy: config.get('try_proxy'),
protocol: config.get('control-panel').protocol,
host: config.get('control-panel').host
};

var args_net = {
proxy: args.proxy,
protocol: args.protocol,
host: 'www.google.com'
};

var check = function(opts, cb) {
var get_current_status = function(cb) {
is_online({ retries: 0 })
.then(cb('connected'))
.catch(function() {
network.get_connection_status(function(new_status) {
cb(new_status);
});
})
}

var check_status = function() {
if (checking) return;
checking = true;

network.get_connection_status(opts, function(new_status) {
var current_status = status;

if (opts.host == 'www.google.com') {
current_status = internet_status;
new_status = new_status.concat('_internet');
internet_status = new_status;
} else {
status = new_status;
}

if (current_status != new_status) {
checking = true;
get_current_status(function(new_status) {
if (status != new_status) {
// trigger directly the event instead of emitting it to the actions manager
hooks.trigger(new_status);
}

status = new_status;
checking = false;
if (cb) return cb();
});
}

var check_status = function() {
check(args, function() {
if (status == 'disconnected') check(args_net);
})
}

exports.start = function(opts, cb) {
check_status();
hooks.on('network_state_changed', check_status);

hooks.on('connected', function() {
if (internet_status != 'connected_internet') {
internet_status = 'connected_internet';
hooks.trigger(internet_status);
}
});

hooks.on('disconnected_internet', function() {
network.reset_active_access_point();
});

// Connection Heartbeat
// todo @lemavri Dinamically change interval from
// 15-60 seconds if status remains. Reset otherwise
Expand All @@ -83,11 +50,7 @@ exports.start = function(opts, cb) {

exports.stop = function(cb) {
hooks.remove('network_state_changed', check_status);
hooks.remove('disconnected_internet');
hooks.remove('connected');

clearInterval(interval_checker);
interval_checker = null;
if (emitter) {
emitter.removeAllListeners();
emitter = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ system.spawn_as_admin_user = function(command, args, opts, cb) {
var options = { command: command, args: args, opts: opts, cb: cb };

if (os_name == 'windows') {
system.check_service(function(err, data) { // An error means the new service isn't available
system.check_service(options, function(err, data) { // An error means the new service isn't available
if (err) return as('logged_user', 'spawn', data.command, data.args, data.opts, data.cb);

var cb = data.cb;
Expand Down
10 changes: 10 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"folder": "0.0.4",
"getset": "^0.7.1",
"graceful-fs": "^3.0.5",
"internet-available": "1.0.0",
"linus": "0.0.5",
"memorize": "0.0.1",
"mime": "https://registry.npmjs.org/mime/-/mime-1.2.5.tgz",
Expand Down Expand Up @@ -68,7 +69,8 @@
"wink": "0.0.1",
"winssh": "",
"wmic": "0.0.7",
"winattr": "1.1.0"
"winattr": "1.1.0",
"xml2js": "0.4.19"
},
"devDependencies": {
"mkdirp": "",
Expand All @@ -80,8 +82,7 @@
"send": "",
"shelljs": "",
"should": "8.3.1",
"sinon": "1.17.2",
"winattr": "^1.1.0"
"sinon": "1.17.2"
},
"main": "./lib/common",
"bin": {
Expand Down

0 comments on commit 7cca852

Please sign in to comment.