Skip to content

Commit

Permalink
add is-port-reachable
Browse files Browse the repository at this point in the history
  • Loading branch information
lopelex committed Jun 24, 2019
1 parent c4ac850 commit 1bd0a1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion harmony/harmony-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const events = require('events');
const netstat = require('node-netstat');
const util = require('util');

const debug = true;
const debug = false;

module.exports = function(RED) {

Expand Down Expand Up @@ -56,6 +56,7 @@ module.exports = function(RED) {
.catch(err => {
if (debug) console.error(err);
});
delete(node.hub);
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions harmony/harmony.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function(RED) {

const debug = false;

function HarmonySendCommand(config) {
RED.nodes.createNode(this, config);
var node = this;
Expand Down Expand Up @@ -30,7 +32,7 @@ module.exports = function(RED) {
var action = node.server.hub.getAction(id || node.activity, command);

} catch (err) {
console.log('Error: ' + err);
if (debug) console.log('Error: ' + err);
}

if (!action) {
Expand All @@ -48,7 +50,7 @@ module.exports = function(RED) {
node.send({
payload: false
});
console.log('Error: ' + err);
if (debug) console.log('Error: ' + err);
});
}
});
Expand Down Expand Up @@ -116,14 +118,12 @@ module.exports = function(RED) {
node.send({
payload: false
});
console.log('Error: ' + err);
if (debug) console.log('Error: ' + err);
});
});
}
RED.nodes.registerType('HWS activity', HarmonyActivity);



function HarmonyObserve(config) {
var node = this;
RED.nodes.createNode(this, config);
Expand Down
18 changes: 13 additions & 5 deletions harmony/hub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const EventEmitter = require('events');
const Harmony = require('harmony-websocket');
const util = require('util');
const isPortReachable = require('is-port-reachable');

DEFAULT_HUB_PORT = 8088;


class Hub extends EventEmitter {
Expand All @@ -24,10 +26,16 @@ class Hub extends EventEmitter {
this.harmony.on('stateDigest', digest => this._onStateDigest(digest));
}

// if(this.isConnected()) {
// return Promise.resolve();
// }
return this.harmony.connect(this.ip)
return isPortReachable(DEFAULT_HUB_PORT, {
host: this.ip,
timeout: 2000
})
.then((result) => {
if(!result) {
throw new Error('Hub not reachable');
}
})
.then(() => this.harmony.connect(this.ip))
.then(() => this.harmony.getCurrentActivity())
.then(activityId => {
this.activityId = activityId;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"websocket": "^1.0.28",
"websocket-as-promised": "^0.9.0",
"node-netstat": "^1.6.0",
"@harmonyhub/discover": "^1.0.8"
"@harmonyhub/discover": "^1.0.8",
"is-port-reachable": "^2.0.1"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 1bd0a1b

Please sign in to comment.