Merge branch 'feature/Harmony'

Conflicts:
	package.json
This commit is contained in:
Kraig McConaghy
2015-10-19 14:02:27 -05:00
2 changed files with 192 additions and 228 deletions

View File

@@ -20,7 +20,7 @@
"eibd": "^0.3.1", "eibd": "^0.3.1",
"elkington": "kevinohara80/elkington", "elkington": "kevinohara80/elkington",
"hap-nodejs": "^0.0.2", "hap-nodejs": "^0.0.2",
"harmonyhubjs-client": "^1.1.4", "harmonyhubjs-client": "enriquez/harmonyhubjs-client#patch-1",
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
"komponist": "0.1.0", "komponist": "0.1.0",
"lifx": "git+https://github.com/magicmonkey/lifxjs.git", "lifx": "git+https://github.com/magicmonkey/lifxjs.git",
@@ -30,7 +30,9 @@
"node-icontrol": "^0.1.5", "node-icontrol": "^0.1.5",
"node-milight-promise": "0.0.x", "node-milight-promise": "0.0.x",
"node-persist": "0.0.x", "node-persist": "0.0.x",
"node-xmpp-client": "1.0.0-alpha23",
"q": "1.4.x", "q": "1.4.x",
"queue": "^3.1.0",
"request": "2.49.x", "request": "2.49.x",
"sonos": "0.8.x", "sonos": "0.8.x",
"telldus-live": "^0.2.1", "telldus-live": "^0.2.1",

View File

@@ -24,6 +24,13 @@ var harmony = require('harmonyhubjs-client');
var _harmonyHubPort = 61991; var _harmonyHubPort = 61991;
var Service = require("hap-nodejs").Service;
var Characteristic = require("hap-nodejs").Characteristic;
var Accessory = require("hap-nodejs").Accessory;
var uuid = require("hap-nodejs").uuid;
var inherits = require('util').inherits;
var queue = require('queue');
function sortByKey (array, key) { function sortByKey (array, key) {
return array.sort(function(a, b) { return array.sort(function(a, b) {
@@ -41,30 +48,37 @@ function LogitechHarmonyPlatform (log, config) {
LogitechHarmonyPlatform.prototype = { LogitechHarmonyPlatform.prototype = {
// Find one Harmony remote hub (only support one for now) accessories: function (callback) {
locateHub: function (callback) { var plat = this;
var self = this; var foundAccessories = [];
var activityAccessories = [];
var hub = null;
var hubIP = null;
var hubQueue = queue();
hubQueue.concurrency = 1;
// Connect to a Harmony hub // Get the first hub
var createClient = function (ipAddress) { locateHub(function (err, client, clientIP) {
self.log("Connecting to Logitech Harmony remote hub..."); if (err) throw err;
harmony(ipAddress) plat.log("Fetching Logitech Harmony devices and activites...");
.then(function (client) {
self.log("Connected to Logitech Harmony remote hub");
callback(null, client); hub = client;
hubIP = clientIP;
//getDevices(hub);
getActivities();
}); });
};
// Find one Harmony remote hub (only support one for now)
function locateHub(callback) {
// Use the ip address in configuration if available // Use the ip address in configuration if available
if (this.ip_address) { if (plat.ip_address) {
console.log("Using Logitech Harmony hub ip address from configuration"); console.log("Using Logitech Harmony hub ip address from configuration");
return createClient(this.ip_address) return createClient(plat.ip_address, callback)
} }
this.log("Searching for Logitech Harmony remote hubs..."); plat.log("Searching for Logitech Harmony remote hubs...");
// Discover the harmony hub with bonjour // Discover the harmony hub with bonjour
var discover = new harmonyDiscover(_harmonyHubPort); var discover = new harmonyDiscover(_harmonyHubPort);
@@ -72,234 +86,182 @@ LogitechHarmonyPlatform.prototype = {
// TODO: Support update event with some way to add accessories // TODO: Support update event with some way to add accessories
// TODO: Have some kind of timeout with an error message. Right now this searches forever until it finds one hub. // TODO: Have some kind of timeout with an error message. Right now this searches forever until it finds one hub.
discover.on('online', function (hubInfo) { discover.on('online', function (hubInfo) {
self.log("Found Logitech Harmony remote hub: " + hubInfo.ip); plat.log("Found Logitech Harmony remote hub: " + hubInfo.ip);
// Stop looking for hubs once we find the first one // Stop looking for hubs once we find the first one
// TODO: Support multiple hubs // TODO: Support multiple hubs
discover.stop(); discover.stop();
createClient(hubInfo.ip); createClient(hubInfo.ip, callback);
}); });
// Start looking for hubs // Start looking for hubs
discover.start(); discover.start();
}, }
accessories: function (callback) { // Connect to a Harmony hub
var self = this; function createClient(ipAddress, callback) {
var foundAccessories = []; plat.log("Connecting to Logitech Harmony remote hub...");
harmony(ipAddress)
// Get the first hub .then(function (client) {
this.locateHub(function (err, hub) { plat.log("Connected to Logitech Harmony remote hub");
if (err) throw err; callback(null, client, ipAddress);
self.log("Fetching Logitech Harmony devices and activites...");
//getDevices(hub);
getActivities(hub);
}); });
}
// Get Harmony Devices
/*
var getDevices = function(hub) {
self.log("Fetching Logitech Harmony devices...");
hub.getDevices()
.then(function (devices) {
self.log("Found devices: ", devices);
var sArray = sortByKey(json['result'],"Name");
sArray.map(function(s) {
accessory = new LogitechHarmonyAccessory(self.log, self.server, self.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
foundAccessories.push(accessory);
});
callback(foundAccessories);
});
};
*/
// Get Harmony Activities // Get Harmony Activities
var getActivities = function(hub) { function getActivities() {
self.log("Fetching Logitech Harmony activities..."); plat.log("Fetching Logitech Harmony activities...");
hub.getActivities() hub.getActivities()
.then(function (activities) { .then(function (activities) {
self.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n")); plat.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n"));
var sArray = sortByKey(activities, "label");
sArray.map(function(s) {
var accessory = new LogitechHarmonyAccessory(self.log, hub, s, true);
// TODO: Update the initial power state
foundAccessories.push(accessory);
});
callback(foundAccessories);
});
};
}
};
function LogitechHarmonyAccessory (log, hub, details, isActivity) {
this.log = log;
this.hub = hub;
this.details = details;
this.id = details.id;
this.name = details.label;
this.isActivity = isActivity;
this.isActivityActive = false;
};
LogitechHarmonyAccessory.prototype = {
// TODO: Somehow make this event driven so that it tells the user what activity is on
getPowerState: function (callback) {
var self = this;
if (this.isActivity) {
hub.getCurrentActivity().then(function (currentActivity) { hub.getCurrentActivity().then(function (currentActivity) {
callback(currentActivity.id === self.id); var actAccessories = [];
}).except(function (err) { var sArray = sortByKey(activities, "label");
self.log('Unable to get current activity with error', err); sArray.map(function(s) {
callback(false); var accessory = createActivityAccessory(s);
}); if (accessory.id > 0) {
} else { accessory.updateActivityState(currentActivity);
// TODO: Support onRead for devices actAccessories.push(accessory);
this.log('TODO: Support onRead for devices'); foundAccessories.push(accessory);
}
});
activityAccessories = actAccessories;
keepAliveRefreshLoop();
callback(foundAccessories);
}).catch(function (err) {
plat.log('Unable to get current activity with error', err);
throw err;
});
});
} }
},
setPowerState: function (state, callback) { function createActivityAccessory(activity) {
var self = this; var accessory = new LogitechHarmonyActivityAccessory(plat.log, activity, changeCurrentActivity.bind(plat), -1);
return accessory;
}
if (this.isActivity) { var isChangingActivity = false;
this.log('Set activity ' + this.name + ' power state to ' + state); function changeCurrentActivity(nextActivity, callback) {
if (!nextActivity) {
// Activity id -1 is turn off all devices nextActivity = -1;
var id = state ? this.id : -1; }
plat.log('Queue activity to ' + nextActivity);
this.hub.startActivity(id) executeOnHub(function(h, cb) {
plat.log('Set activity to ' + nextActivity);
h.startActivity(nextActivity)
.then(function () { .then(function () {
self.log('Finished setting activity ' + self.name + ' power state to ' + state); cb();
callback(); isChangingActivity = false;
plat.log('Finished setting activity to ' + nextActivity);
updateCurrentActivity(nextActivity);
if (callback) callback(null, nextActivity);
}) })
.catch(function (err) { .catch(function (err) {
self.log('Failed setting activity ' + self.name + ' power state to ' + state + ' with error ' + err); cb();
callback(err); isChangingActivity = false;
plat.log('Failed setting activity to ' + nextActivity + ' with error ' + err);
if (callback) callback(err);
});
}, function(){
callback(Error("Set activity failed too many times"));
}); });
} else {
// TODO: Support setting device power
this.log('TODO: Support setting device power');
callback();
}
},
getServices: function () {
var self = this;
return [
{
sType: types.ACCESSORY_INFORMATION_STYPE,
characteristics: [
{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: self.name,
supportEvents: false,
supportBonjour: false,
manfDescription: "Name of the accessory",
designedMaxLength: 255
},
{
cType: types.MANUFACTURER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Logitech",
supportEvents: false,
supportBonjour: false,
manfDescription: "Manufacturer",
designedMaxLength: 255
},
{
cType: types.MODEL_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Harmony",
supportEvents: false,
supportBonjour: false,
manfDescription: "Model",
designedMaxLength: 255
},
{
cType: types.SERIAL_NUMBER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
// TODO: Add hub unique id to this for people with multiple hubs so that it is really a guid.
initialValue: self.id,
supportEvents: false,
supportBonjour: false,
manfDescription: "SN",
designedMaxLength: 255
},
{
cType: types.IDENTIFY_CTYPE,
onUpdate: null,
perms: ["pw"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Identify Accessory",
designedMaxLength: 1
}
]
},
{
sType: types.SWITCH_STYPE,
characteristics: [
{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: self.name,
supportEvents: true,
supportBonjour: false,
manfDescription: "Name of service",
designedMaxLength: 255
},
{
cType: types.POWER_STATE_CTYPE,
onUpdate: function (value) {
self.setPowerState(value)
},
onRead: self.getPowerState,
perms: ["pw","pr","ev"],
format: "bool",
initialValue: 0,
supportEvents: true,
supportBonjour: false,
manfDescription: "Change the power state",
designedMaxLength: 1
}
]
}
];
} }
function updateCurrentActivity(currentActivity) {
var actAccessories = activityAccessories;
if (actAccessories instanceof Array) {
actAccessories.map(function(a) { a.updateActivityState(currentActivity); });
}
}
// prevent connection from closing
function keepAliveRefreshLoop() {
setTimeout(function() {
setInterval(function() {
executeOnHub(function(h, cb) {
plat.log("Refresh Status");
h.getCurrentActivity()
.then(function(currentActivity){
cb();
updateCurrentActivity(currentActivity);
})
.catch(cb);
});
}, 20000);
}, 5000);
}
function executeOnHub(func, funcMaxTimeout)
{
if (!func) return;
hubQueue.push(function(cb) {
var tout = setTimeout(function(){
plat.log("Reconnecting to Hub " + hubIP);
createClient(hubIP, function(err, newHub){
if (err) throw err;
hub = newHub;
if (funcMaxTimeout) {
funcMaxTimeout();
}
cb();
});
}, 30000);
func(hub, function(){
clearTimeout(tout);
cb();
});
});
if (!hubQueue.running){
hubQueue.start();
}
}
}
};
function LogitechHarmonyActivityAccessory (log, details, changeCurrentActivity) {
this.log = log;
this.id = details.id;
this.name = details.label;
this.isOn = false;
this.changeCurrentActivity = changeCurrentActivity;
Accessory.call(this, this.name, uuid.generate(this.id));
var self = this;
this.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Logitech")
.setCharacteristic(Characteristic.Model, "Harmony")
// TODO: Add hub unique id to this for people with multiple hubs so that it is really a guid.
.setCharacteristic(Characteristic.SerialNumber, this.id);
this.addService(Service.Switch)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
// Refreshed automatically by platform
callback(null, self.isOn);
})
.on('set', this.setPowerState.bind(this));
}
inherits(LogitechHarmonyActivityAccessory, Accessory);
LogitechHarmonyActivityAccessory.prototype.parent = Accessory.prototype;
LogitechHarmonyActivityAccessory.prototype.getServices = function() {
return this.services;
};
LogitechHarmonyActivityAccessory.prototype.updateActivityState = function (currentActivity) {
this.isOn = (currentActivity === this.id);
// Force get to trigger 'change' if needed
this.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.getValue();
};
LogitechHarmonyActivityAccessory.prototype.setPowerState = function (state, callback) {
this.changeCurrentActivity(state ? this.id : null, callback);
}; };
module.exports.accessory = LogitechHarmonyAccessory;
module.exports.platform = LogitechHarmonyPlatform; module.exports.platform = LogitechHarmonyPlatform;