Skip to content

Commit

Permalink
Refactored event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
docbobo committed Jun 7, 2017
1 parent c5c184c commit d443ae2
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"use strict";

var debug = require('debug')('roon-extension-harmony'),
debug_keepalive = require('debug')('roon-extension-harmony:keepalive'),
RoonApi = require('node-roon-api'),
RoonApiStatus = require('node-roon-api-status'),
RoonApiSettings = require('node-roon-api-settings'),
RoonApiSourceControl = require('node-roon-api-source-control'),
Discover = require('harmonyhubjs-discover'),
Harmony = require('harmonyhubjs-client');

var util = require('util');
var EventEmitter = require('events').EventEmitter;

const STATE_HUB_OFF = 0;
const STATE_ACTIVITY_STARTING = 1;
const STATE_ACTIVITY_STARTED = 2;
Expand All @@ -21,6 +25,8 @@ const STATE_HUB_TURNING_OFF = 3;
function HarmonyHub(name, address) {
this.name = name;
this.address = address;

EventEmitter.call(this)
}

/**
Expand All @@ -29,11 +35,17 @@ function HarmonyHub(name, address) {
HarmonyHub.prototype.createClient = function() {
debug("HarmonyHub.createClient: Creating Harmony client for %s (%s)", this.name, this.address);

var self = this;

var result = Harmony(this.address);
return result.then((client) => {
client._xmppClient.connection.socket.setTimeout(0);
client._xmppClient.connection.socket.setKeepAlive(true, 10000);

client.on('stateDigest', stateDigest => {
self.emit('stateDigest', stateDigest);
});

return result;
});
}
Expand All @@ -42,6 +54,8 @@ HarmonyHub.prototype.toString = function() {
return this.name;
}

util.inherits(HarmonyHub, EventEmitter);

/**
* Creates a new HarmonyDiscovery intances. This class will keep track of all the Harmony hubs that are discovered on
* the networks.
Expand Down Expand Up @@ -198,13 +212,33 @@ function setup_harmony_connection(harmonyHub) {
}
};

harmonyHub.on('stateDigest', stateDigest => {
debug("stateDigest[%s]: state for '%s' => %s", activity.label, stateDigest.activityId, stateDigest.activityStatus);

var status;
switch (stateDigest.activityStatus) {
case STATE_ACTIVITY_STARTING:
case STATE_ACTIVITY_STARTED:
status = stateDigest.activityId == activity.id ? "selected" : "standby";
break;

default:
status = "standby";
}

if (device.status != status) {
device.status = status;
harmony.activities[activity.id].update_state({ status: status });
}
});

harmony.activities[activity.id] = svc_source_control.new_device(device);
}
});

harmonyClient.keepalive = setInterval(() => {
harmonyClient.getCurrentActivity().then((val) => {
debug("Keep-Alive: getCurrentActivity() == %s", val);
debug_keepalive("Keep-Alive: getCurrentActivity() == %s", val);
});
}, 30000);

Expand All @@ -213,42 +247,6 @@ function setup_harmony_connection(harmonyHub) {
setup_harmony_connection(harmonyHub);
});

harmonyClient.on('stateDigest', (val) => {
debug("stateDigest: state for '%s' => %s", val.activityId, val.activityStatus);
for (var key in harmony.activities) {
if (key == val.activityId) {
switch (val.activityStatus) {
case STATE_ACTIVITY_STARTING:
case STATE_ACTIVITY_STARTED:
harmony.activities[key].update_state({ status: "selected" });
break;

case STATE_HUB_OFF:
harmony.activities[key].update_state({ status: "standby" });
break;

default:
// Ignoring
break;
}
} else {
switch (val.activityStatus) {
case STATE_ACTIVITY_STARTED:
harmony.activities[key].update_state({ status: "standby" });
break;

case STATE_HUB_OFF:
harmony.activities[key].update_state({ status: "standby" });
break;

default:
// Ignoring
break;
}
}
}
});

svc_status.set_status("Connected to Harmony Hub " + harmonyHub.name, false);
});
});
Expand Down

0 comments on commit d443ae2

Please sign in to comment.