Added handling to keep track of activity state

This commit is contained in:
Kraig McConaghy
2015-10-18 16:00:02 -05:00
parent 6008374b9e
commit e30b504583

View File

@@ -63,7 +63,7 @@ LogitechHarmonyPlatform.prototype = {
setTimeout(function() { setTimeout(function() {
setInterval(function() { setInterval(function() {
self.log("Sending command to prevent timeout"); self.log("Sending command to prevent timeout");
client.getCurrentActivity(); client.getCurrentActivity().then(self.updateCurrentActivity.bind(self));
}, 20000); }, 20000);
}, 5000); }, 5000);
@@ -99,6 +99,13 @@ LogitechHarmonyPlatform.prototype = {
discover.start(); discover.start();
}, },
updateCurrentActivity: function(currentActivity) {
var actAccessories = this.activityAccessories;
if (actAccessories instanceof Array) {
actAccessories.map(function(a) { a.updateActivityState(currentActivity); });
}
},
accessories: function (callback) { accessories: function (callback) {
var self = this; var self = this;
var foundAccessories = []; var foundAccessories = [];
@@ -142,30 +149,36 @@ LogitechHarmonyPlatform.prototype = {
.then(function (activities) { .then(function (activities) {
self.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n")); self.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n"));
var sArray = sortByKey(activities, "label"); hub.getCurrentActivity().then(function (currentActivity) {
var actAccessories = [];
sArray.map(function(s) { var sArray = sortByKey(activities, "label");
var accessory = new LogitechHarmonyActivityAccessory(self.log, hub, s); sArray.map(function(s) {
// TODO: Update the initial power state var accessory = new LogitechHarmonyActivityAccessory(self.log, hub, s, self.updateCurrentActivity.bind(self));
foundAccessories.push(accessory); accessory.updateActivityState(currentActivity);
actAccessories.push(accessory);
foundAccessories.push(accessory);
});
self.activityAccessories = actAccessories;
callback(foundAccessories);
}).catch(function (err) {
self.log('Unable to get current activity with error', err);
callback(false);
}); });
callback(foundAccessories);
}); });
}; };
} }
}; };
function LogitechHarmonyActivityAccessory (log, hub, details, updateCurrentActivity) {
function LogitechHarmonyActivityAccessory (log, hub, details) {
this.log = log; this.log = log;
this.hub = hub; this.hub = hub;
this.details = details; this.details = details;
this.id = details.id; this.id = details.id;
this.name = details.label; this.name = details.label;
this.isOn = false;
this.updateCurrentActivity = updateCurrentActivity;
Accessory.call(this, this.name, uuid.generate(this.id)); Accessory.call(this, this.name, uuid.generate(this.id));
var self = this;
this.getService(Service.AccessoryInformation) this.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Logitech") .setCharacteristic(Characteristic.Manufacturer, "Logitech")
@@ -175,8 +188,12 @@ function LogitechHarmonyActivityAccessory (log, hub, details) {
this.addService(Service.Switch) this.addService(Service.Switch)
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('get', this.getPowerState) .on('get', function(callback) {
.on('set', this.setPowerState); // Refreshed automatically by platform
callback(null, self.isOn);
})
.on('set', this.setPowerState.bind(this));
}; };
inherits(LogitechHarmonyActivityAccessory, Accessory); inherits(LogitechHarmonyActivityAccessory, Accessory);
LogitechHarmonyActivityAccessory.prototype.parent = Accessory.prototype; LogitechHarmonyActivityAccessory.prototype.parent = Accessory.prototype;
@@ -184,31 +201,32 @@ LogitechHarmonyActivityAccessory.prototype.getServices = function() {
return this.services; return this.services;
}; };
// TODO: Somehow make this event driven so that it tells the user what activity is on LogitechHarmonyActivityAccessory.prototype.updateActivityState = function (currentActivity) {
LogitechHarmonyActivityAccessory.prototype.getPowerState = function (callback) { this.isOn = (currentActivity === this.id);
var self = this; // Force get to trigger 'change' if needed
this.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.getValue();
};
this.hub.getCurrentActivity().then(function (currentActivity) { LogitechHarmonyActivityAccessory.prototype.setPowerState = function (state, callback) {
callback(currentActivity === self.id);
}).catch(function (err) {
self.log('Unable to get current activity with error', err);
callback(false);
});
};
LogitechHarmonyActivityAccessory.prototype.setPowerState = function (state) { var self = this;
var self = this;
this.log('Set activity ' + this.name + ' power state to ' + state); this.log('Set activity ' + this.name + ' power state to ' + state);
this.hub.startActivity(self.id) var nextActivity = self.id;
.then(function () { this.hub.startActivity(nextActivity)
self.log('Finished setting activity ' + self.name + ' power state to ' + state); .then(function () {
}) self.log('Finished setting activity ' + self.name + ' power state to ' + state);
.catch(function (err) { self.updateCurrentActivity(nextActivity);
self.log('Failed setting activity ' + self.name + ' power state to ' + state + ' with error ' + err); if (callback) callback(null, state);
}); })
}; .catch(function (err) {
self.log('Failed setting activity ' + self.name + ' power state to ' + state + ' with error ' + err);
if (callback) callback(err);
});
};
module.exports.platform = LogitechHarmonyPlatform; module.exports.platform = LogitechHarmonyPlatform;