343 lines
13 KiB
JavaScript
Executable File
343 lines
13 KiB
JavaScript
Executable File
/*****
|
|
* SmartHomeNG platform shim for use with nfarina's homebridge plugin system
|
|
* This work has been inspired by the homebridge-knx platform shim. Credits to the autor !
|
|
*
|
|
*/
|
|
|
|
var Accessory, Service, Characteristic, UUIDGen;
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
|
|
var SmartHomeNGConnection = require("./SmartHomeNGConnection.js").SmartHomeNGConnection;
|
|
var milliTimeout = 300; // used to block responses while swiping
|
|
var monitoring = [];
|
|
var colorOn = "\x1b[30;47m";
|
|
var colorOff = "\x1b[0m";
|
|
|
|
module.exports = function(homebridge) {
|
|
// Accessory must be created from PlatformAccessory Constructor
|
|
Accessory = homebridge.platformAccessory;
|
|
|
|
// Service and Characteristic are from hap-nodejs
|
|
Service = homebridge.hap.Service;
|
|
Characteristic = homebridge.hap.Characteristic;
|
|
UUIDGen = homebridge.hap.uuid;
|
|
|
|
homebridge.registerPlatform("homebridge-smarthomeng", "SmartHomeNG", SmartHomeNGPlatform);
|
|
}
|
|
|
|
// Platform constructor
|
|
function SmartHomeNGPlatform(log, config, api) {
|
|
this.log = log;
|
|
this.config = config;
|
|
this.accessoriesCache = [];
|
|
this.supportedFunctions = ['onoff', 'brightness', 'currentposition', 'targetposition'];
|
|
|
|
if (this.config["host"] != undefined) {
|
|
this.shng_host = this.config["host"];
|
|
} else {
|
|
this.shng_host = "localhost";
|
|
}
|
|
|
|
if (this.config["port"] != undefined) {
|
|
this.shng_port = this.config["port"];
|
|
} else {
|
|
this.shng_port = 2424;
|
|
}
|
|
|
|
var that = this
|
|
this.log("SmartHomeNG Platform Plugin Version " + this.getVersion())
|
|
|
|
this.shngcon = new SmartHomeNGConnection(this, this.log, this.shng_host, this.shng_port);
|
|
this.shngcon.updateCallback = this.update;
|
|
|
|
if (api) {
|
|
this.api = api;
|
|
this.api.on('didFinishLaunching', function() {
|
|
this.log("Finished loading " + this.accessoriesCache.length + " accessories");
|
|
this.log(monitoring);
|
|
// Add supported SHNG items to monitoring
|
|
var tomonitor = [];
|
|
for(i = 0; i < monitoring.length; i++) {
|
|
var device = monitoring[i];
|
|
if(tomonitor.indexOf(device.item) == -1) {
|
|
tomonitor.push(device.item);
|
|
}
|
|
}
|
|
this.shngcon.tomonitor = tomonitor;
|
|
}.bind(this));
|
|
}
|
|
this.shngcon.init();
|
|
}
|
|
|
|
// Accessory constructor
|
|
function SmartHomeNGAccessory(log, config, shngcon) {
|
|
this.name = config.name;
|
|
this.config = config;
|
|
this.log = log;
|
|
this.shngcon = shngcon;
|
|
this.value = undefined;
|
|
this.manufacturername = 'SmartHomeNG';
|
|
/*this.log("CONSTRUCTOR: ");
|
|
this.log(device);
|
|
this.log("------")*/
|
|
}
|
|
|
|
SmartHomeNGPlatform.prototype = {
|
|
|
|
// Return our accessories to homebridge
|
|
accessories: function(callback) {
|
|
var that = this;
|
|
var foundAccessories = [];
|
|
|
|
this.log("Building list of accessories");
|
|
|
|
var configAccessories = this.config.accessories;
|
|
for (var i = 0; i < configAccessories.length; i++) {
|
|
this.log("Parsing accessory: '" + configAccessories[i].name + "'.");
|
|
|
|
var accessory = new SmartHomeNGAccessory(that.log, configAccessories[i], that.shngcon);
|
|
foundAccessories.push(accessory);
|
|
}
|
|
|
|
//this.log(foundAccessories)
|
|
this.accessoriesCache = foundAccessories;
|
|
callback(foundAccessories);
|
|
},
|
|
|
|
update: function (item, value) {
|
|
//this.log("CALLBACK: item " + item + " with value " + value);
|
|
for (var i = 0; i < monitoring.length; i++) {
|
|
// iterate through all registered addresses
|
|
if (monitoring[i].item == item) {
|
|
this.log("[" + item + "] Got update from SmartHomeNG with value " + value);
|
|
monitoring[i].lastValue = value;
|
|
monitoring[i].callback(item, value, monitoring[i].inverted);
|
|
}
|
|
}
|
|
},
|
|
|
|
// Get version info from package.json file
|
|
getVersion: function() {
|
|
var pjPath = path.join(__dirname, './package.json');
|
|
var pj = JSON.parse(fs.readFileSync(pjPath));
|
|
return pj.version;
|
|
}
|
|
}
|
|
|
|
SmartHomeNGAccessory.prototype = {
|
|
|
|
// Enumerate accessory services and characteristics
|
|
getServices: function() {
|
|
var that = this;
|
|
var myServices = [];
|
|
|
|
this.log("["+ this.name +"] Setting up services.");
|
|
|
|
// check if device type is set in config
|
|
if (!this.config.type) {
|
|
this.log("Ignoring '" + this.name + "' because no device type found, make sure to have the 'type' parameter in your config.json !");
|
|
return [];
|
|
}
|
|
|
|
// construct service and characteristics according to device type
|
|
switch (this.config.type.toLowerCase()) {
|
|
// Lightbulb service
|
|
case 'lightbulb':
|
|
myServices.push(this.getLightbulbService(this.config));
|
|
break;
|
|
|
|
case 'windowcovering':
|
|
myServices.push(this.getWindowCoveringService(this.config));
|
|
break;
|
|
|
|
// If no supported type is found warn user and return empty services
|
|
default:
|
|
this.log("Ignoring '" + this.name + "' because device type '" + this.device.type + "' is not supported !");
|
|
return [];
|
|
break;
|
|
}
|
|
|
|
// device information service
|
|
var informationService = new Service.AccessoryInformation();
|
|
informationService
|
|
.setCharacteristic(Characteristic.Manufacturer, "Opensource Community")
|
|
.setCharacteristic(Characteristic.Model, "SmartHomeNG device")
|
|
.setCharacteristic(Characteristic.SerialNumber, "beta");
|
|
myServices.push(informationService);
|
|
|
|
return myServices;
|
|
},
|
|
|
|
// Respond to identify request
|
|
identify: function(callback) {
|
|
this.log("Identify request for '" + this.device.name + "'.");
|
|
callback();
|
|
},
|
|
|
|
/** Registering routines
|
|
*
|
|
*/
|
|
// boolean: get 0 or 1 from the bus, write boolean
|
|
shngregister_bool: function(name, shngitem, characteristic, inverted) {
|
|
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
|
var callback = function (shngitem, value, inverted) {
|
|
//this.log("[" + this.name + "] callback for " + characteristic.displayName);
|
|
characteristic.setValue(value ? (inverted ? 0:1) : (inverted ? 1:0), undefined, 'fromSHNG');
|
|
}.bind(this);
|
|
monitoring.push({item: shngitem, callback: callback, inverted: inverted});
|
|
},
|
|
|
|
shngregister_percent: function(name, shngitem, characteristic, inverted) {
|
|
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
|
var callback = function (shngitem, value, inverted) {
|
|
//this.log("[" + this.name + "] callback for " + characteristic.displayName);
|
|
characteristic.setValue(inverted ? 100 - value : value, undefined, 'fromSHNG');
|
|
}.bind(this);
|
|
monitoring.push({item: shngitem, callback: callback, inverted: inverted});
|
|
},
|
|
|
|
/** get methods
|
|
*
|
|
*/
|
|
getState: function(callback, shngitem, inverted) {
|
|
this.log("[" + this.name + "] Get value from cache for item " + shngitem + ".");
|
|
for (var i = 0; i < monitoring.length; i++) {
|
|
if (monitoring[i].item == shngitem) {
|
|
if (monitoring[i].lastValue != undefined) {
|
|
value = monitoring[i].lastValue;
|
|
this.log("[" + this.name + "] Found value " + value + " in cache.");
|
|
//monitoring[i].callback(item, value, monitoring[i].inverted);
|
|
callback(null, value);
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
callback();
|
|
},
|
|
|
|
/** set methods used for creating callbacks
|
|
*
|
|
*/
|
|
setBooleanState: function(value, callback, context, shngitem, inverted) {
|
|
if (context === 'fromSHNG') {
|
|
if (callback) {
|
|
callback();
|
|
}
|
|
} else {
|
|
var numericValue = inverted ? 1:0;
|
|
if (value) {
|
|
numericValue = inverted ? 0:1;
|
|
}
|
|
this.log("[" + this.name + "] Setting " + shngitem + (inverted ? " (inverted)":"") + " boolean to %s", numericValue);
|
|
this.shngcon.setValue(shngitem, numericValue);
|
|
if (callback) callback();
|
|
}
|
|
},
|
|
|
|
setPercentage: function(value, callback, context, shngitem, inverted) {
|
|
if (context === 'fromSHNG') {
|
|
if (callback) {
|
|
callback();
|
|
}
|
|
} else {
|
|
var numericValue = 0;
|
|
value = ( value>=0 ? (value<=100 ? value:100):0 ); //ensure range 0..100
|
|
if (inverted) {
|
|
numericValue = 100 - value;
|
|
} else {
|
|
numericValue = value;
|
|
}
|
|
this.log("[" + this.name + "] Setting " + shngitem + " percentage to %s", numericValue);
|
|
this.shngcon.setValue(shngitem, numericValue);
|
|
if (callback) callback();
|
|
}
|
|
},
|
|
|
|
/** bindCharacteristic
|
|
* initializes callbacks for 'set' events (from HK) and for SmartHomeNG monitoring events (to HK)
|
|
*/
|
|
// Bind characteristic to service during startup
|
|
bindCharacteristic: function(myService, characteristicType, valueType, shngitem, inverted, defaultValue) {
|
|
var myCharacteristic = myService.getCharacteristic(characteristicType);
|
|
this.log("SHNGITEM: " + shngitem)
|
|
if (defaultValue) {
|
|
myCharacteristic.setValue(defaultValue);
|
|
}
|
|
switch (valueType) {
|
|
case "Bool":
|
|
myCharacteristic.on('set', function(value, callback, context) {
|
|
this.setBooleanState(value, callback, context, shngitem, inverted);
|
|
}.bind(this));
|
|
myCharacteristic.on('get', function(callback, context) {
|
|
this.getState(callback, shngitem, inverted);
|
|
}.bind(this));
|
|
this.shngregister_bool(this.name, shngitem, myCharacteristic, inverted);
|
|
break;
|
|
case "Percent":
|
|
myCharacteristic.on('set', function(value, callback, context) {
|
|
this.setPercentage(value, callback, context, shngitem, inverted);
|
|
//myCharacteristic.timeout = Date.now()+milliTimeout;
|
|
}.bind(this));
|
|
myCharacteristic.on('get', function(callback, context) {
|
|
this.getState(callback, shngitem, inverted);
|
|
}.bind(this));
|
|
this.shngregister_percent(this.name, shngitem, myCharacteristic, inverted);
|
|
break;
|
|
default:
|
|
this.log(colorOn + "[ERROR] unknown type passed: [" + valueType+"]"+ colorOff);
|
|
}
|
|
return myCharacteristic;
|
|
},
|
|
|
|
/**
|
|
* function getXXXXXXXService(config)
|
|
* returns a configured service object to the caller (accessory/device)
|
|
*
|
|
*/
|
|
// Create Lightbulb service
|
|
getLightbulbService: function(config) {
|
|
var myService = new Service.Lightbulb(config.name,config.name);
|
|
var inverted = false;
|
|
if (config.inverted) {
|
|
inverted = true;
|
|
}
|
|
// On (and Off)
|
|
if (config.onoff) {
|
|
this.log("["+ this.name +"] Lightbulb on/off characteristic enabled");
|
|
this.bindCharacteristic(myService, Characteristic.On, "Bool", config.onoff, inverted);
|
|
}
|
|
// Brightness if available
|
|
if (config.brightness) {
|
|
this.log("["+ this.name +"] Lightbulb Brightness characteristic enabled");
|
|
myService.addCharacteristic(Characteristic.Brightness); // it's an optional
|
|
this.bindCharacteristic(myService, Characteristic.Brightness, "Percent", config.brightness, inverted);
|
|
}
|
|
return myService;
|
|
},
|
|
|
|
// Create WindowCovering service
|
|
getWindowCoveringService: function(config) {
|
|
this.log(config);
|
|
var myService = new Service.WindowCovering(config.name,config.name);
|
|
var inverted = false;
|
|
if (config.inverted) {
|
|
inverted = true;
|
|
}
|
|
if (config.currentposition) {
|
|
this.log("["+ this.name +"] WindowCovering CurrentPosition characteristic enabled");
|
|
this.bindCharacteristic(myService, Characteristic.CurrentPosition, "Percent", config.currentposition, inverted);
|
|
}
|
|
if (config.targetposition) {
|
|
this.log("["+ this.name +"] WindowCovering TargetPosition characteristic enabled");
|
|
this.bindCharacteristic(myService, Characteristic.TargetPosition, "Percent", config.targetposition, inverted);
|
|
}
|
|
this.log("["+ this.name +"] WindowCovering PositionState characteristic enabled");
|
|
this.bindCharacteristic(myService, Characteristic.PositionState, "Int", config.positionstate, inverted, 2);
|
|
return myService;
|
|
},
|
|
|
|
|
|
}
|