From 988d2ada35e461f8bf55d2eb817d91f67fe68cfc Mon Sep 17 00:00:00 2001 From: Sebastian Maier Date: Tue, 13 Oct 2020 04:08:26 +0200 Subject: [PATCH] Fix whitespace --- SmartHomeNGConnection.js | 41 +++++----- index.js | 166 +++++++++++++++++++-------------------- 2 files changed, 103 insertions(+), 104 deletions(-) diff --git a/SmartHomeNGConnection.js b/SmartHomeNGConnection.js index d85b472..5e97b42 100755 --- a/SmartHomeNGConnection.js +++ b/SmartHomeNGConnection.js @@ -5,38 +5,37 @@ var colorOn = "\x1b[30;47m"; var colorOff = "\x1b[0m"; function SmartHomeNGConnection(platform, log, host, port) { - this.log = log; - this.platform = platform; - this.connected = false; - this.updateCallback = undefined; - this.tomonitor = []; - this.retryTimer = 10; - - this.shng_host = host; - this.shng_port = port; - + this.log = log; + this.platform = platform; + this.connected = false; + this.updateCallback = undefined; + this.tomonitor = []; + this.retryTimer = 10; + + this.shng_host = host; + this.shng_port = port; } SmartHomeNGConnection.prototype.init = function () { var that = this; this.shng_ws = new WebSocketClient(); this.shng_ws.on('connect', function(connection) { - that.log('[SmartHomeNGConnection] connected to server!'); + that.log('[SmartHomeNGConnection] connected to server!'); that.connected = true; that.connection = connection; that.idenfityMyself(); that.startMonitoring(); - - connection.on('message', function(message) { that.receive(message); }); - connection.on('error', function(error) { - that.log(colorOn + '[SmartHomeNGConnection] WebSocket error: ' + error.toString() + colorOff) + + connection.on('message', function(message) { that.receive(message); }); + connection.on('error', function(error) { + that.log(colorOn + '[SmartHomeNGConnection] WebSocket error: ' + error.toString() + colorOff) }); connection.on('close', function(code, description) { that.log(colorOn + '[SmartHomeNGConnection] Connection to smarthome lost, retrying in ' + that.retryTimer + ' seconds. ' + description + colorOff); that.connected = false; setTimeout(that.connect.bind(that), that.retryTimer * 1000); }); - + }); this.shng_ws.on('connectFailed', function(errorDescription) { @@ -49,13 +48,13 @@ SmartHomeNGConnection.prototype.init = function () { } SmartHomeNGConnection.prototype.connect = function(host, ip) { - this.log("[SmartHomeNGConnection] Connecting to SmartHomeNG @ " + this.shng_host); + this.log("[SmartHomeNGConnection] Connecting to SmartHomeNG @ " + this.shng_host); this.shng_ws.connect('ws://' + this.shng_host + ':' + this.shng_port + '/'); - + } SmartHomeNGConnection.prototype.receive = function(message) { - var msg = JSON.parse(message.utf8Data); + var msg = JSON.parse(message.utf8Data); //this.log(msg); if (msg.items) { for (int = 0; int < msg.items.length; int++) { @@ -91,7 +90,7 @@ SmartHomeNGConnection.prototype.idenfityMyself = function() { } } -SmartHomeNGConnection.prototype.startMonitoring = function() { +SmartHomeNGConnection.prototype.startMonitoring = function() { if (this.connected && this.tomonitor.length > 0) { var buffer = {}; this.log("[SmartHomeNGConnection] Start monitoring " + this.tomonitor); @@ -105,6 +104,6 @@ SmartHomeNGConnection.prototype.startMonitoring = function() { } module.exports = { - SmartHomeNGConnection: SmartHomeNGConnection + SmartHomeNGConnection: SmartHomeNGConnection } diff --git a/index.js b/index.js index 461e858..6862319 100755 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ /***** - * SmartHomeNG platform shim for use with nfarina's homebridge plugin system + * SmartHomeNG platform shim for use with nfarina's homebridge plugin system * This work has been inspired by the homebridge-knx platform shim. Credits to snowdd1 ! - * + * */ var Accessory, Service, Characteristic, UUIDGen; @@ -22,7 +22,7 @@ module.exports = function(homebridge) { Service = homebridge.hap.Service; Characteristic = homebridge.hap.Characteristic; UUIDGen = homebridge.hap.uuid; - + homebridge.registerPlatform("homebridge-smarthomeng", "SmartHomeNG", SmartHomeNGPlatform); } @@ -32,26 +32,26 @@ function SmartHomeNGPlatform(log, config, api) { this.config = config; this.accessoriesCache = []; this.supportedFunctions = ['onoff', 'brightness', 'hue', 'saturation', 'currentposition', 'targetposition', 'motiondetected']; - + 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.version = this.getVersion(); this.log("SmartHomeNG Platform Plugin Version " + this.version) 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() { @@ -63,7 +63,7 @@ function SmartHomeNGPlatform(log, config, api) { var device = monitoring[i]; if(tomonitor.indexOf(device.item) == -1) { tomonitor.push(device.item); - } + } } this.shngcon.tomonitor = tomonitor; }.bind(this)); @@ -85,27 +85,27 @@ function SmartHomeNGAccessory(log, config, shngcon) { } 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++) { @@ -127,14 +127,14 @@ SmartHomeNGPlatform.prototype = { } 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 !"); @@ -143,17 +143,17 @@ SmartHomeNGAccessory.prototype = { // construct service and characteristics according to device type var serial = "unknown"; - switch (this.config.type.toLowerCase()) { + switch (this.config.type.toLowerCase()) { // Lightbulb service case 'fan': myServices.push(this.getFanService(this.config)); serial = this.config.onoff; break; - + case 'temperaturesensor': myServices.push(this.getTemperatureSensorService(this.config)); break; - + case 'thermostat': myServices.push(this.getThermostatService(this.config)); break; @@ -162,7 +162,7 @@ SmartHomeNGAccessory.prototype = { myServices.push(this.getLightbulbService(this.config)); serial = this.config.onoff; break; - + case 'window': myServices.push(this.getWindowService(this.config)); break; @@ -193,14 +193,14 @@ SmartHomeNGAccessory.prototype = { myServices.push(this.getContactSensorService(this.config)); serial = this.config.contactsensorstate; break; - + // If no supported type is found warn user and return empty services default: this.log("Ignoring '" + this.name + "' because device type '" + this.config.type + "' is not supported !"); return []; break; } - + // device information service var informationService = new Service.AccessoryInformation(); informationService @@ -208,18 +208,18 @@ SmartHomeNGAccessory.prototype = { .setCharacteristic(Characteristic.Model, "SmartHomeNG device") .setCharacteristic(Characteristic.SerialNumber, serial); myServices.push(informationService); - + return myServices; }, // Respond to identify request - identify: function(callback) { + identify: function(callback) { this.log("Identify request for '" + this.name + "'."); callback(); }, /** Registering routines - * + * */ shngregister_int: function(name, shngitem, characteristic) { this.log("[" + name + "] Registering callback for '" + shngitem + "'."); @@ -229,7 +229,7 @@ SmartHomeNGAccessory.prototype = { }.bind(this); monitoring.push({name: name, characteristic: characteristic.displayName, item: shngitem, callback: callback, inverted: false}); }, - + shngregister_float: function(name, shngitem, characteristic) { this.log("[" + name + "] Registering callback for '" + shngitem + "'."); var callback = function (shngitem, value) { @@ -247,7 +247,7 @@ SmartHomeNGAccessory.prototype = { }.bind(this); monitoring.push({name: name, characteristic: characteristic.displayName, 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) { @@ -256,7 +256,7 @@ SmartHomeNGAccessory.prototype = { }.bind(this); monitoring.push({name: name, characteristic: characteristic.displayName, item: shngitem, callback: callback, inverted: inverted}); }, - + shngregister_angle: function(name, shngitem, characteristic, inverted) { this.log("[" + name + "] Registering callback for '" + shngitem + "'."); var callback = function (shngitem, value, inverted) { @@ -266,8 +266,8 @@ SmartHomeNGAccessory.prototype = { }.bind(this); monitoring.push({name: name, characteristic: characteristic.displayName, item: shngitem, callback: callback, inverted: inverted}); }, - - /** get methods + +/** get methods * */ getState: function(callback, shngitem, inverted) { @@ -305,17 +305,17 @@ SmartHomeNGAccessory.prototype = { if (callback) callback(); } }, - + setPercentage: function(value, callback, context, shngitem, inverted) { if (context === 'fromSHNG') { if (callback) { callback(); } - } else { + } else { var numericValue = 0; value = ( value>=0 ? (value<=100 ? value:100):0 ); //ensure range 0..100 if (inverted) { - numericValue = 100 - value; + numericValue = 100 - value; } else { numericValue = value; } @@ -330,11 +330,11 @@ SmartHomeNGAccessory.prototype = { if (callback) { callback(); } - } else { + } else { var numericValue = 0; value = ( value>=0 ? (value<=360 ? value:360):0 ); //ensure range 0..360 if (inverted) { - numericValue = 360 - value; + numericValue = 360 - value; } else { numericValue = value; } @@ -348,40 +348,40 @@ SmartHomeNGAccessory.prototype = { }, setInt: function(value, callback, context, shngitem) { - if (context === 'fromSHNG') { - if (callback) { - callback(); - } - } else { - var numericValue = 0; - if (value && value>=0) { - numericValue = value; - } - this.log("["+ this.name +"] Setting " + shngitem + " int to %s", numericValue); + if (context === 'fromSHNG') { + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (value && value>=0) { + numericValue = value; + } + this.log("["+ this.name +"] Setting " + shngitem + " int to %s", numericValue); this.shngcon.setValue(shngitem, numericValue); if (callback) callback(); - } + } }, setFloat: function(value, callback, context, shngitem) { - if (context === 'fromSHNG') { - if (callback) { - callback(); - } - } else { - var numericValue = 0; - if (value && value>=0) { - numericValue = value; - } - this.log("["+ this.name +"] Setting " + shngitem + " float to %s", numericValue); + if (context === 'fromSHNG') { + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (value && value>=0) { + numericValue = value; + } + this.log("["+ this.name +"] Setting " + shngitem + " float 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); @@ -398,7 +398,7 @@ SmartHomeNGAccessory.prototype = { this.getState(callback, shngitem, inverted); }.bind(this)); this.shngregister_int(this.name, shngitem, myCharacteristic); - break; + break; case "Float": myCharacteristic.on('set', function(value, callback, context) { this.setFloat(value, callback, context, shngitem); @@ -407,7 +407,7 @@ SmartHomeNGAccessory.prototype = { this.getState(callback, shngitem, inverted); }.bind(this)); this.shngregister_float(this.name, shngitem, myCharacteristic); - break; + break; case "Bool": myCharacteristic.on('set', function(value, callback, context) { this.setBooleanState(value, callback, context, shngitem, inverted); @@ -416,12 +416,12 @@ SmartHomeNGAccessory.prototype = { this.getState(callback, shngitem, inverted); }.bind(this)); this.shngregister_bool(this.name, shngitem, myCharacteristic, inverted); - break; + break; case "Percent": myCharacteristic.on('set', function(value, callback, context) { this.setPercentage(value, callback, context, shngitem, inverted); //myCharacteristic.timeout = Date.now()+milliTimeout; - }.bind(this)); + }.bind(this)); myCharacteristic.on('get', function(callback, context) { this.getState(callback, shngitem, inverted); }.bind(this)); @@ -431,7 +431,7 @@ SmartHomeNGAccessory.prototype = { myCharacteristic.on('set', function(value, callback, context) { this.setAngle(value, callback, context, shngitem, inverted); //myCharacteristic.timeout = Date.now()+milliTimeout; - }.bind(this)); + }.bind(this)); myCharacteristic.on('get', function(callback, context) { this.getState(callback, shngitem, inverted); }.bind(this)); @@ -439,7 +439,7 @@ SmartHomeNGAccessory.prototype = { break; default: this.log(colorOn + "[ERROR] unknown type passed: [" + valueType+"]"+ colorOff); - } + } return myCharacteristic; }, @@ -447,7 +447,7 @@ SmartHomeNGAccessory.prototype = { * function getXXXXXXXService(config) * returns a configured service object to the caller (accessory/device) * - */ + */ // Create Temperature Sensor service getTemperatureSensorService: function(config) { var myService = new Service.TemperatureSensor(config.name,config.name); @@ -471,10 +471,10 @@ SmartHomeNGAccessory.prototype = { // Target temperature if (config.targettemperature) { this.log("["+ this.name +"] TemperatureSensor TargetTemperature characteristic enabled"); - myService.getCharacteristic(Characteristic.TargetTemperature).setProps({ - minValue: config.targettemperatureminimum || 0, - maxValue: config.targettemperaturemaximum || 40 - }); + myService.getCharacteristic(Characteristic.TargetTemperature).setProps({ + minValue: config.targettemperatureminimum || 0, + maxValue: config.targettemperaturemaximum || 40 + }); this.bindCharacteristic(myService, Characteristic.TargetTemperature, "Float", config.targettemperature, false); } /* @@ -485,7 +485,7 @@ SmartHomeNGAccessory.prototype = { }*/ return myService; }, - + // Create Fan service getFanService: function(config) { var myService = new Service.Fan(config.name,config.name); @@ -500,7 +500,7 @@ SmartHomeNGAccessory.prototype = { } return myService; }, - + // Create Lightbulb service getLightbulbService: function(config) { var myService = new Service.Lightbulb(config.name,config.name); @@ -533,7 +533,7 @@ SmartHomeNGAccessory.prototype = { } return myService; }, - + // Create Window service getWindowService: function(config) { //this.log(config); @@ -545,11 +545,11 @@ SmartHomeNGAccessory.prototype = { if (config.currentposition) { this.log("["+ this.name +"] Window CurrentPosition characteristic enabled"); this.bindCharacteristic(myService, Characteristic.CurrentPosition, "Percent", config.currentposition, inverted); - } + } if (config.targetposition) { this.log("["+ this.name +"] Window TargetPosition characteristic enabled"); this.bindCharacteristic(myService, Characteristic.TargetPosition, "Percent", config.targetposition, inverted); - } + } this.log("["+ this.name +"] Window PositionState characteristic enabled"); this.bindCharacteristic(myService, Characteristic.PositionState, "Int", config.positionstate, inverted, Characteristic.PositionState.STOPPED); return myService; @@ -566,16 +566,16 @@ SmartHomeNGAccessory.prototype = { 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, Characteristic.PositionState.STOPPED); return myService; }, - + // Create OccupancySensor service getOccupancySensorService: function(config) { var myService = new Service.OccupancySensor(config.name,config.name); @@ -586,7 +586,7 @@ SmartHomeNGAccessory.prototype = { if (config.motiondetected) { this.log("["+ this.name +"] OccupancySensor OccupancyDetected characteristic enabled"); this.bindCharacteristic(myService, Characteristic.OccupancyDetected, "Bool", config.motiondetected, inverted); - } + } return myService; }, @@ -600,10 +600,10 @@ SmartHomeNGAccessory.prototype = { if (config.motiondetected) { this.log("["+ this.name +"] MotionSensor MotionDetected characteristic enabled"); this.bindCharacteristic(myService, Characteristic.MotionDetected, "Bool", config.motiondetected, inverted); - } + } return myService; }, - + // Create ContactSensor service getContactSensorService: function(config) { var myService = new Service.ContactSensor(config.name,config.name); @@ -614,7 +614,7 @@ SmartHomeNGAccessory.prototype = { if (config.contactsensorstate) { this.log("["+ this.name +"] ContactSensor ContactSensorState characteristic enabled"); this.bindCharacteristic(myService, Characteristic.ContactSensorState, "Bool", config.contactsensorstate, inverted); - } + } return myService; },