├── .gitignore ├── README.md ├── index.js ├── lib ├── accessory.js ├── controller.js ├── model.js └── utils.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Mac 3 | .DS_Store 4 | 5 | # Node 6 | node_modules/ 7 | npm-debug.log 8 | .node-version 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # homebridge-mvc 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | 5 | [npm-image]: http://img.shields.io/npm/v/homebridge-mvc.svg 6 | [npm-url]: https://npmjs.org/package/homebridge-mvc 7 | 8 | Homebridge-mvc is a Plugin for Homebridge. This Example-plugin is based on MVC (Model-View-Controller) pattern. 9 | 10 | Have a look at [homebridge-mqtt](https://github.com/cflurin/homebridge-mqtt) for a practical implementation. 11 | 12 | ### Installation 13 | 14 | If you are new to homebridge, please follow the instructions in [homebridge](https://github.com/nfarina/homebridge) for the homebridge server installation. 15 | 16 | Install homebridge-mvc: 17 | ```sh 18 | sudo npm install -g homebridge-mvc 19 | ``` 20 | 21 | ### Configuration 22 | Add the platform in config.json in your home directory inside `.homebridge`. 23 | 24 | ```sh 25 | { 26 | "platform": "mvc", 27 | "name": "mvc" 28 | } 29 | 30 | ``` 31 | 32 | # 33 | # API 34 | 35 | * addAccessory() 36 | * addService() 37 | * removeAccessory() 38 | * removeService() 39 | * setValue() 40 | * getAccessories() 41 | * updateReachability() 42 | * setAccessoryInformation() 43 | * get() 44 | * set() 45 | * identify() 46 | 47 | 48 | ## Howto examples 49 | 50 | 1) Define your homebridge platform in index.js: 51 | 52 | ```js 53 | var platform_name = "myPlatform"; 54 | ``` 55 | 56 | **Note:** remeber to change `config.json` for your platform. 57 | 58 | 2) Modify the functions for your plugin in model.js 59 | 60 | ### add accessory 61 | 62 | ```sh 63 | accessory = {"name": "flex_lamp", "service_name": "light", "service": "Switch"}; 64 | result = addAccessory(accessory); 65 | ``` 66 | ### add service 67 | 68 | Note: an accessory with the same name must be added before. 69 | 70 | ```sh 71 | accessory = {"name": "multi_sensor", "service_name": "Humidity", "service": "HumiditySensor"}; 72 | result = addService(accessory); 73 | ``` 74 | 75 | ### remove accessory 76 | 77 | ```sh 78 | accessory = {"name": "flex_lamp"}; 79 | result = removeAccessory(accessory); 80 | ``` 81 | 82 | ### remove service 83 | 84 | ```sh 85 | accessory = {"name": "multi_sensor", "service_name": "Humidity"}; 86 | result = removeService(accessory); 87 | ``` 88 | 89 | ### get accessory/accessories 90 | 91 | The purpose of this function is to retrieve accessory definitions. 92 | 93 | ```sh 94 | accessory = {"name": "outdoor_temp"}; 95 | result = getAccessories(accessory); 96 | ``` 97 | 98 | ```sh 99 | accessory = {"name": "*"}; 100 | result = getAccessories(accessory); 101 | ``` 102 | 103 | ### set value 104 | 105 | ```sh 106 | accessory = {"name": "flex_lamp", "service_name": "light", "characteristic": "On", "value": true}; 107 | result = setValue(accessory); 108 | ``` 109 | 110 | ### update reachability 111 | 112 | ```sh 113 | 114 | accessory = {"name": "flex_lamp", "reachable": true}; 115 | or 116 | accessory = {"name": "flex_lamp", "reachable": false}; 117 | result = updateReachability(accessory); 118 | 119 | ``` 120 | 121 | ### set accessory information 122 | 123 | ```sh 124 | accessory = {"name": "flex_lamp", "manufacturer": "espressif", "model": "esp8266-12", "serialnumber": "4711"}; 125 | result = setAccessoryInformation(accessory); 126 | ``` 127 | 128 | ### get (from homebridge) 129 | 130 | ```sh 131 | Model.prototype.get = function(name, service_name, characteristic) {...} 132 | ``` 133 | 134 | ### set (from homebridge) 135 | 136 | ```sh 137 | Model.prototype.set = function(name, service_name, characteristic, value, callback) {...} 138 | ``` 139 | 140 | ### identify (from homebridge) 141 | 142 | ```sh 143 | Model.prototype.identify = function (name, manufacturer, model, serialnumber) {...} 144 | ``` 145 | 146 | ### define characterstic 147 | 148 | The required characteristics are added with the default properties. If you need to change the default, define the characteristic-name with the properties. e.g.: 149 | 150 | ```sh 151 | accessory = 152 | { 153 | "name": "living_temp", 154 | "service": "TemperatureSensor", 155 | "CurrentTemperature": {"minValue": -20, "maxValue": 60, "minStep": 1} 156 | }; 157 | result = addAccessory(accessory); 158 | ``` 159 | 160 | To add an optional charachteristic define the characteristic-name with "default" or with the properties. e.g.: 161 | 162 | ```sh 163 | accessory = 164 | { 165 | "name": "living_lamp", 166 | "service": "Lightbulb", 167 | "Brightness": "default" 168 | }; 169 | result = addAccessory(accessory); 170 | ``` 171 | 172 | ```sh 173 | accessory = 174 | { 175 | "name": "bathroom_blind", 176 | "service": "WindowCovering", 177 | "CurrentPosition": {"minStep": 5}, 178 | "TargetPosition": {"minStep": 5}, 179 | "CurrentHorizontalTiltAngle": {"minValue": 0, "minStep": 5}, 180 | "TargetHorizontalTiltAngle": {"minValue": 0, "minStep": 5} 181 | }; 182 | result = addAccessory(accessory); 183 | ``` 184 | 185 | [HomeKitTypes.js](https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js) describes all the predifined Services, Characteristcs, format and properties for the `value` e.g.: 186 | 187 | ``` 188 | /** 189 | * Service "Contact Sensor" 190 | */ 191 | 192 | Service.ContactSensor = function(displayName, subtype) { 193 | Service.call(this, displayName, '00000080-0000-1000-8000-0026BB765291', subtype); 194 | 195 | // Required Characteristics 196 | this.addCharacteristic(Characteristic.ContactSensorState); 197 | 198 | // Optional Characteristics 199 | this.addOptionalCharacteristic(Characteristic.StatusActive); 200 | this.addOptionalCharacteristic(Characteristic.StatusFault); 201 | this.addOptionalCharacteristic(Characteristic.StatusTampered); 202 | this.addOptionalCharacteristic(Characteristic.StatusLowBattery); 203 | this.addOptionalCharacteristic(Characteristic.Name); 204 | }; 205 | 206 | /** 207 | * Characteristic "Contact Sensor State" 208 | */ 209 | 210 | Characteristic.ContactSensorState = function() { 211 | Characteristic.call(this, 'Contact Sensor State', '0000006A-0000-1000-8000-0026BB765291'); 212 | this.setProps({ 213 | format: Characteristic.Formats.UINT8, 214 | perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY] 215 | }); 216 | this.value = this.getDefaultValue(); 217 | }; 218 | 219 | inherits(Characteristic.ContactSensorState, Characteristic); 220 | 221 | Characteristic.ContactSensorState.UUID = '0000006A-0000-1000-8000-0026BB765291'; 222 | 223 | // The value property of ContactSensorState must be one of the following: 224 | Characteristic.ContactSensorState.CONTACT_DETECTED = 0; 225 | Characteristic.ContactSensorState.CONTACT_NOT_DETECTED = 1; 226 | ``` 227 | 228 | Derived from this: 229 | 230 | ``` 231 | service = ContactSensor 232 | characteristic = ContactSensorState 233 | format = UINT8 234 | property = 0 or 1 235 | ``` 236 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Utils = require('./lib/utils.js').Utils; 4 | var Controller = require('./lib/controller.js').Controller; 5 | 6 | var HapAccessory, Service, Characteristic, UUIDGen; 7 | var cachedAccessories = 0; 8 | 9 | var platform_name = "mvc"; // define your platform_name 10 | var plugin_name = "homebridge-" + platform_name; 11 | var storagePath; 12 | var plugin_version; 13 | 14 | module.exports = function(homebridge) { 15 | console.log("homebridge API version: " + homebridge.version); 16 | 17 | HapAccessory = homebridge.platformAccessory; 18 | 19 | Service = homebridge.hap.Service; 20 | Characteristic = homebridge.hap.Characteristic; 21 | UUIDGen = homebridge.hap.uuid; // Universally Unique IDentifier 22 | storagePath = homebridge.user.storagePath(); 23 | 24 | homebridge.registerPlatform(plugin_name, platform_name, PluginPlatform, true); 25 | } 26 | 27 | function PluginPlatform(log, config, api) { 28 | 29 | this.log = log; 30 | 31 | if (typeof(config) === "undefined" || config === null) { 32 | this.log.error("config undefined or null!"); 33 | this.log("storagePath = %s", storagePath); 34 | process.exit(1); 35 | } 36 | 37 | plugin_version = Utils.readPluginVersion(); 38 | this.log("%s v%s", plugin_name, plugin_version); 39 | 40 | Utils.read_npmVersion(plugin_name, function(npm_version) { 41 | if (npm_version > plugin_version) { 42 | this.log("A new version %s is avaiable", npm_version); 43 | } 44 | }.bind(this)); 45 | 46 | this.log.debug("storagePath = %s", storagePath); 47 | this.log.debug("config = %s", JSON.stringify(config)); 48 | 49 | var c_parameters = { 50 | "config": config, 51 | "log": this.log, 52 | "plugin_name": plugin_name, 53 | "plugin_version": plugin_version, 54 | "platform_name": platform_name, 55 | "api": api, 56 | "HapAccessory": HapAccessory, 57 | "Characteristic": Characteristic, 58 | "Service": Service, 59 | "UUIDGen": UUIDGen 60 | } 61 | 62 | this.controller = new Controller(c_parameters); 63 | 64 | if (api) { 65 | 66 | api.on('didFinishLaunching', function() { 67 | this.log("Number of cached Accessories: %s", cachedAccessories); 68 | 69 | this.controller.start(); 70 | }.bind(this)); 71 | } 72 | } 73 | 74 | PluginPlatform.prototype.configureAccessory = function(accessory) { 75 | 76 | //this.log.debug("configureAccessory %s", JSON.stringify(accessory, null, 2)); 77 | cachedAccessories++; 78 | 79 | this.controller.configureAccessory(accessory); 80 | } 81 | -------------------------------------------------------------------------------- /lib/accessory.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Utils = require('./utils.js').Utils; 4 | 5 | var Service, Characteristic, platform_name, get, set, identify; 6 | 7 | Number.prototype.pad = function (len) { 8 | return (new Array(len+1).join("0") + this).slice(-len); 9 | } 10 | 11 | module.exports = { 12 | Accessory: Accessory 13 | } 14 | 15 | function Accessory(params) { 16 | 17 | this.log = params.log; 18 | Service = params.Service; 19 | Characteristic = params.Characteristic; 20 | platform_name = params.platform_name; 21 | get = params.get; 22 | set = params.set; 23 | identify = params.identify; 24 | 25 | this.name; // assigned by this.configureAccessory 26 | 27 | this.i_value = {}; 28 | this.i_label = {}; 29 | this.i_props = {}; 30 | 31 | this.reachable = true; 32 | 33 | this.services = {}; 34 | this.service_types = {}; 35 | this.service_names = {}; 36 | this.service_namesList = []; 37 | 38 | this.set_timeout; 39 | this.prec_c; 40 | } 41 | 42 | // 43 | // configuration 44 | // 45 | 46 | Accessory.prototype.addService = function(accessory, service_name, service_type) { 47 | 48 | accessory.addService(Service[service_type], service_name, service_name); 49 | 50 | this.service_types[service_name] = service_type; 51 | 52 | if (typeof accessory.context.service_types === "undefined") { 53 | accessory.context.service_types = {}; 54 | } 55 | accessory.context.service_types[service_name] = service_type; 56 | } 57 | 58 | Accessory.prototype.removeService = function(service_name) { 59 | 60 | this.log.debug("Accessory.removeService %s %s", this.name, service_name); 61 | 62 | delete this.services[service_name]; 63 | delete this.service_types[service_name]; 64 | delete this.service_names[service_name]; 65 | 66 | delete this.i_value[service_name]; 67 | delete this.i_props[service_name]; 68 | delete this.i_label[service_name]; 69 | this.service_namesList.pop(service_name); 70 | } 71 | 72 | Accessory.prototype.addService_name = function(service_name) { 73 | 74 | this.i_value[service_name] = {}; 75 | this.i_props[service_name] = {}; 76 | this.i_label[service_name] = {}; 77 | this.service_namesList.push(service_name); 78 | } 79 | 80 | Accessory.prototype.configureAccessory = function(accessory, api_accessory, service_name, service_type) { 81 | 82 | if (typeof api_accessory !== "undefined") { // new accessory 83 | this.name = api_accessory.name; 84 | 85 | this.log.debug("Accessory.configureAccessory '%s' '%s' '%s'", this.name, service_name, service_type); 86 | 87 | this.services[service_name] = accessory.getServiceByUUIDAndSubType(service_name, service_name); 88 | //this.services[service_name] = accessory.getService(service_name); 89 | 90 | this.addService_name(service_name); 91 | this.configureCharacteristics(accessory, api_accessory, service_name); 92 | this.configureOptionalCharacteristics(accessory, api_accessory, service_name); 93 | 94 | } else { // cachedAccessories 95 | this.name = accessory.displayName; 96 | 97 | for (var k in accessory.services) { 98 | if (typeof accessory.services[k].displayName !== "undefined") { 99 | service_name = accessory.services[k].displayName; 100 | 101 | this.services[service_name] = accessory.getServiceByUUIDAndSubType(service_name, service_name); 102 | 103 | // backwards compatible 104 | if (typeof this.services[service_name] === "undefined" ) { 105 | this.services[service_name] = accessory.getService(service_name); 106 | this.log.debug("Accessory.configureAccessory [compatible] '%s' '%s'", this.name, this.services[service_name].displayName); 107 | } else { 108 | this.log.debug("Accessory.configureAccessory '%s' '%s'", this.name, service_name); 109 | } 110 | 111 | this.addService_name(service_name); 112 | this.configureCharacteristics(accessory, api_accessory, service_name); 113 | //this.log.debug("Accessory.configureAccessory %s", JSON.stringify(this.services[service_name], null, 2)); 114 | } 115 | } 116 | 117 | if (typeof accessory.context.service_types !== "undefined") { 118 | for (var k in accessory.context.service_types) { 119 | this.service_types[k] = accessory.context.service_types[k]; 120 | } 121 | } else { 122 | // backwards compatible 123 | if (typeof accessory.context.service_name !== "undefined") { 124 | this.service_types[this.name] = accessory.context.service_name; 125 | } 126 | } 127 | } 128 | //this.log.debug("Accessory.configureAccessory %s", JSON.stringify(accessory, null, 2)); 129 | } 130 | 131 | Accessory.prototype.configureCharacteristics = function(accessory, api_accessory, service_name) { 132 | 133 | var c; 134 | var service = this.services[service_name]; 135 | 136 | for (var k in service.characteristics) { 137 | 138 | c = service.characteristics[k].displayName.replace(/\s/g, ""); 139 | //this.log.debug("Accessory.configureCharacteristics %s %s %s", this.name, service_name, c); 140 | 141 | if (c != "Name") { 142 | this.allocate(service, c); 143 | if (typeof api_accessory !== "undefined") { 144 | this.setProps(service, c, api_accessory); 145 | } 146 | this.i_value[service_name][c] = "blank"; 147 | this.i_props[service_name][c] = JSON.parse(JSON.stringify(service.getCharacteristic(Characteristic[c]).props)); 148 | //this.log.debug("Accessory.configureCharacteristics %s %s %s %s", this.name, service_name, c, JSON.stringify(this.i_props)); 149 | } 150 | } 151 | //this.log.debug("Accessory.configureCharacteristics %s", JSON.stringify(api_accessory[c])); 152 | } 153 | 154 | Accessory.prototype.configureOptionalCharacteristics = function(accessory, api_accessory, service_name) { 155 | 156 | var c; 157 | var service = this.services[service_name]; 158 | 159 | // note: if the accessories are restored from cachedAccessories, the optionalCharacteristics are stored in characteristics. 160 | for (var k in service.optionalCharacteristics) { 161 | 162 | c = service.optionalCharacteristics[k].displayName.replace(/\s/g, ""); 163 | 164 | if (typeof(api_accessory[c]) !== "undefined") { 165 | this.log.debug("Accessory.configureCharacteristics %s %s optional %s", this.name, service_name, c); 166 | 167 | if (c != "Name") { 168 | this.allocate(service, c); 169 | this.setProps(service, c, api_accessory); 170 | this.i_value[service_name][c] = "blank"; 171 | this.i_props[service_name][c] = JSON.parse(JSON.stringify(service.getCharacteristic(Characteristic[c]).props)); 172 | } 173 | } 174 | } 175 | } 176 | 177 | Accessory.prototype.allocate = function(service, c) { 178 | 179 | var self = this; 180 | var sc = service.getCharacteristic(Characteristic[c]); 181 | 182 | sc.on('get', function(callback, context) {self.get(callback, context, this.displayName, this.iid)}); 183 | if (sc.props.perms.indexOf("pw") > -1) { 184 | //this.log.debug("Accessory.allocate 'set' event %s %s", this.name, c); 185 | sc.on('set', function(value, callback, context) {self.set(value, callback, context, this.displayName, this.iid)}); 186 | } 187 | } 188 | 189 | Accessory.prototype.setProps = function(service, c, api_accessory) { 190 | 191 | // only for newAccessories 192 | if (typeof(api_accessory[c]) !== "undefined") { 193 | if (api_accessory[c] != "default") { 194 | service.getCharacteristic(Characteristic[c]).setProps(api_accessory[c]); 195 | } 196 | //this.log.debug("Accessory.setProps %s %s %s", this.name, c, api_accessory[c]); 197 | } 198 | } 199 | 200 | Accessory.prototype.getService_names = function() { 201 | 202 | var service_name; 203 | 204 | this.log.debug("Accessory.getService_names"); 205 | for (var k in this.services) { 206 | for (var i in this.services[k].characteristics) { 207 | if (this.services[k].characteristics[i].displayName !== "Name") { 208 | this.service_names[this.services[k].characteristics[i].iid] = this.services[k].displayName; 209 | this.log.debug("Accessory.getService_names [iid %s] %s '%s' '%s'", this.services[k].characteristics[i].iid, this.name, this.services[k].characteristics[i].displayName, this.services[k].displayName); 210 | } 211 | } 212 | } 213 | //this.log.debug("Accessory.getService_names \n%s", JSON.stringify(this.service_names)); 214 | } 215 | 216 | Accessory.prototype.configureIdentity = function(accessory) { 217 | 218 | accessory.on('identify', function(paired, callback) {this.identify(paired, callback)}.bind(this)); 219 | } 220 | 221 | // 222 | // from HomeKit functions 223 | // 224 | 225 | Accessory.prototype.get = function(callback, context, displayName, iid) { 226 | 227 | if (this.reachable) { 228 | var c = displayName.replace(/\s/g, ""); 229 | //this.log.debug("Accessory.get %s '%s' '%s'", this.name, iid, c); 230 | 231 | if (typeof this.service_names[iid] === "undefined") { 232 | this.getService_names(); 233 | } 234 | var service_name = this.service_names[iid]; 235 | 236 | this.log.debug("Accessory.get [iid %s] '%s' '%s' '%s'", iid, this.name, service_name, c); 237 | get(this.name, service_name, c, callback); 238 | 239 | var value; 240 | if (typeof(this.i_value[service_name][c]) !== "undefined" && this.i_value[service_name][c] !== "blank") { 241 | value = this.i_value[service_name][c]; 242 | } else { 243 | value = null; 244 | } 245 | //this.log.debug("Accessory.get %s %s %s", this.name, c, value); 246 | callback(null, value); 247 | } else { 248 | callback("no_response"); 249 | } 250 | } 251 | 252 | Accessory.prototype.set = function(value, callback, context, displayName, iid) { 253 | 254 | if (this.reachable) { 255 | var c = displayName.replace(/\s/g, ""); 256 | //this.log.debug("Accessory.set %s '%s' '%s' %s", this.name, iid, c, value); 257 | 258 | if (typeof this.service_names[iid] === "undefined") { 259 | this.getService_names(); 260 | } 261 | var service_name = this.service_names[iid]; 262 | 263 | if (c == "On") value = (value == 0 || value == false) ? false : true; 264 | this.i_value[service_name][c] = value; 265 | 266 | if (typeof(context) !== "undefined" && typeof(context.trigger) === "undefined") { 267 | this.setLabel("homekit", service_name, c); 268 | } 269 | 270 | if (typeof(context) !== "undefined" && typeof(context.trigger) !== "undefined" && context.trigger.includes(platform_name)) { 271 | callback(); 272 | } else { 273 | this.log.debug("Accessory.set [iid %s] '%s' '%s' '%s' %s", iid, this.name, service_name, c, value); 274 | switch (c) { 275 | case "Brightness": 276 | case "TargetPosition": 277 | case "TargetHorizontalTiltAngle": 278 | case "TargetVerticalTiltAngle": 279 | case "TargetRelativeHumidity": 280 | case "TargetTemperature": 281 | if (this.set_timeout && c === this.pre_c) { 282 | clearTimeout(this.set_timeout); 283 | } 284 | this.set_timeout = setTimeout(function() { 285 | set(this.name, service_name, c, value, callback); 286 | }.bind(this), 250); 287 | this.pre_c = c; 288 | break; 289 | 290 | default: 291 | set(this.name, service_name, c, value, callback); 292 | } 293 | } 294 | } else { 295 | callback("no_response"); 296 | } 297 | } 298 | 299 | Accessory.prototype.identify = function (paired, callback) { 300 | 301 | identify(this.name); 302 | callback(); 303 | } 304 | 305 | // 306 | // to HomeKit and value-handling functions 307 | // 308 | 309 | Accessory.prototype.save_and_setValue = function (trigger, service_name, c, value) { 310 | 311 | var result = {}; 312 | 313 | result = this.parseValue(service_name, c, value); 314 | 315 | if (result.isValid) { 316 | //this.log.debug("Accessory.save_and_setValue '%s' '%s' '%s' %s", this.name, service_name, c, value); 317 | this.i_value[service_name][c] = result.value; 318 | this.setLabel(trigger, service_name, c); 319 | 320 | var context = this.i_label[service_name][c]; 321 | //context is also used by the hap-server ('get' and 'set' event) - "context": {"keepalive":true, ... 322 | //this.log.debug("Accessory.save_and_setValue %s %s %s %s %s ", trigger, this.name, c, result.value, JSON.stringify(context)); 323 | 324 | // todo: to clarify 325 | // A function to only updating the remote value, but not firiring the 'set' event. 326 | // Service.prototype.updateCharacteristic = function(name, value) 327 | 328 | if (typeof(context) !== "undefined") { 329 | this.services[service_name].getCharacteristic(Characteristic[c]).setValue(result.value, null, context); 330 | } 331 | else { 332 | this.services[service_name].getCharacteristic(Characteristic[c]).setValue(result.value); 333 | } 334 | } 335 | 336 | return result; 337 | } 338 | 339 | Accessory.prototype.parseValue = function(service_name, c, value) { 340 | 341 | var isValid = true; 342 | 343 | //this.log.debug("Accessory.parseValue %s %s", service_name, c); 344 | 345 | var sc = this.services[service_name].getCharacteristic(Characteristic[c]); 346 | //this.log.debug("Accessory.parseValue %s %s", c, JSON.stringify(sc)); 347 | 348 | switch (sc.props.format) { 349 | case "bool": 350 | value = (value == 0 || value == false) ? false : true; 351 | break; 352 | 353 | case "int": 354 | case "uint8": 355 | case "uint16": 356 | case "unit32": 357 | value = parseInt(value); 358 | if (value < sc.props.minValue || value > sc.props.maxalue) { 359 | this.log.debug("Accessory.parseInt %s %s value '%s' outside range.", this.name, c, value); 360 | isValid = false; 361 | } 362 | break; 363 | 364 | case "float": 365 | value = parseFloat(value); 366 | if (value < sc.props.minValue || value > sc.props.maxalue) { 367 | this.log.debug("Accessory.parseFloat %s %s value '%s' outside range.", this.name, c, value); 368 | isValid = false; 369 | } 370 | break; 371 | 372 | default: 373 | // string, tlv8, 374 | value = undefined; 375 | this.log.warn("Accessory.parseValue %s %s %s %s", name, c, value, JSON.stringify(sc.props)); 376 | } 377 | return {isValid: isValid, value: value}; 378 | } 379 | 380 | Accessory.prototype.setLabel = function(trigger, service_name, c) { 381 | 382 | var now = new Date(); 383 | var timestamp = now.getHours().pad(2)+":"+now.getMinutes().pad(2)+":"+now.getSeconds().pad(2); 384 | // +","+now.getMilliseconds(); 385 | 386 | this.i_label[service_name][c] = { 387 | "timestamp": timestamp, 388 | "trigger": trigger 389 | }; 390 | } 391 | -------------------------------------------------------------------------------- /lib/controller.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Utils = require('./utils.js').Utils; 4 | var Accessory = require('./accessory.js').Accessory; 5 | var Model = require('./model.js').Model; 6 | 7 | var config, plugin_name, plugin_version, platform_name; 8 | var api, HapAccessory, Service, Characteristic, UUIDGen; 9 | 10 | var accessory_parameters; 11 | 12 | module.exports = { 13 | Controller: Controller 14 | } 15 | 16 | function Controller(params) { 17 | 18 | config = params.config; 19 | this.log = params.log; 20 | plugin_name = params.plugin_name; 21 | plugin_version = params.plugin_version; 22 | platform_name = params.platform_name; 23 | api = params.api; 24 | HapAccessory = params.HapAccessory; 25 | Service = params.Service; 26 | Characteristic = params.Characteristic; 27 | UUIDGen = params.UUIDGen; 28 | 29 | this.accessories = {}; 30 | this.hap_accessories = {}; 31 | 32 | var model_parameters = { 33 | "config": config, 34 | "log": this.log, 35 | "plugin_name": plugin_name, 36 | "Characteristic": Characteristic, 37 | "addAccessory": this.addAccessory.bind(this), 38 | "addService": this.addService.bind(this), 39 | "removeAccessory": this.removeAccessory.bind(this), 40 | "removeService": this.removeService.bind(this), 41 | "setValue": this.setValue.bind(this), 42 | "getAccessories": this.getAccessories.bind(this), 43 | "updateReachability": this.updateReachability.bind(this), 44 | "setAccessoryInformation": this.setAccessoryInformation.bind(this) 45 | }; 46 | 47 | this.createModel(model_parameters); 48 | 49 | accessory_parameters = { 50 | "log": this.log, 51 | "platform_name": platform_name, 52 | "Service": Service, 53 | "Characteristic": Characteristic, 54 | "get": this.get.bind(this), 55 | "set": this.set.bind(this), 56 | "identify": this.identify.bind(this) 57 | }; 58 | } 59 | 60 | Controller.prototype.addAccessory = function(api_accessory) { 61 | 62 | var name = api_accessory.name; 63 | var service_type = api_accessory.service; 64 | var service_name; 65 | var ack, message; 66 | 67 | // backwards compatible 68 | if (typeof api_accessory.service_name !== "undefined" ) { 69 | service_name = api_accessory.service_name; 70 | } else { 71 | service_name = name; 72 | } 73 | 74 | if (typeof name === "undefined") { 75 | ack = false; message = "name undefined."; 76 | 77 | } else if (typeof service_type === "undefined") { 78 | ack = false; message = "service undefined."; 79 | 80 | } else if (typeof service_name === "undefined") { 81 | ack = false; message = "service_name undefined."; 82 | 83 | } else if (typeof Service[service_type] === "undefined") { 84 | ack = false; message = "service '" + service_type + "' undefined."; 85 | 86 | } else if (typeof this.accessories[name] !== "undefined") { 87 | ack = false; message = "name '" + name + "' is already used."; 88 | 89 | } else { 90 | var uuid = UUIDGen.generate(name); 91 | 92 | var newAccessory = new HapAccessory(name, uuid); 93 | //this.log.debug("Controller.addAccessory UUID = %s", newAccessory.UUID); 94 | 95 | var i_accessory = new Accessory(accessory_parameters); 96 | 97 | i_accessory.addService(newAccessory, service_name, service_type); 98 | 99 | i_accessory.configureAccessory(newAccessory, api_accessory, service_name, service_type); 100 | 101 | i_accessory.configureIdentity(newAccessory); 102 | 103 | newAccessory.reachable = true; 104 | 105 | this.accessories[name] = i_accessory; 106 | this.hap_accessories[name] = newAccessory; 107 | api.registerPlatformAccessories(plugin_name, platform_name, [newAccessory]); 108 | 109 | ack = true; message = "accessory '" + name + "', service_name '" + service_name + "' is added."; 110 | } 111 | 112 | if (ack) { 113 | var now = new Date().toISOString().slice(0,16); 114 | var plugin_v = "v" + plugin_version; 115 | this.setAccessoryInformation({"name":name,"manufacturer":plugin_name,"model": plugin_v,"serialnumber":now}, false); 116 | } 117 | 118 | return {"topic": "addAccessory", "ack": ack, "message": message}; 119 | } 120 | 121 | Controller.prototype.addService = function(api_accessory) { 122 | 123 | var name= api_accessory.name; 124 | var service_type = api_accessory.service; 125 | var service_name = api_accessory.service_name; 126 | 127 | var ack, message; 128 | 129 | if (typeof this.hap_accessories[name] === "undefined") { 130 | ack = false; message = "accessory '" + name + "' undefined."; 131 | 132 | } else if (typeof service_name === "undefined") { 133 | ack = false; message = "service_name undefined."; 134 | 135 | } else if (typeof service_type === "undefined") { 136 | ack = false; message = "service undefined."; 137 | 138 | } else if (typeof Service[service_type] === "undefined") { 139 | ack = false; message = "service '" + service_type + "' undefined."; 140 | 141 | } else if (this.accessories[name].service_namesList.indexOf(service_name) > -1) { 142 | ack = false; message = "service_name '" + service_name + "' is already used."; 143 | 144 | } else if (typeof this.hap_accessories[name].context.service_types === "undefined") { 145 | ack = false; message = "Please remove the accessory '" + name + "'and add it again before adding multiple services"; 146 | 147 | } else { 148 | this.accessories[name].addService(this.hap_accessories[name], service_name, service_type); 149 | this.accessories[name].configureAccessory(this.hap_accessories[name], api_accessory, service_name, service_type); 150 | ack = true; message = "name '" + name + "', service_name '" + service_name + "', service '" + service_type + "' is added."; 151 | } 152 | 153 | return {"topic": "addService", "ack": ack, "message": message}; 154 | } 155 | 156 | Controller.prototype.configureAccessory = function(accessory) { 157 | 158 | //this.log.debug("Controller.configureAccessory %s", JSON.stringify(accessory, null, 2)); 159 | 160 | var name = accessory.displayName; 161 | var uuid = accessory.UUID; 162 | 163 | if (this.accessories[name]) { 164 | this.log.error("Controller.configureAccessory %s UUID %s already used.", name, uuid); 165 | process.exit(1); 166 | } 167 | 168 | accessory.reachable = true; 169 | 170 | var i_accessory = new Accessory(accessory_parameters); 171 | 172 | i_accessory.configureAccessory(accessory); 173 | i_accessory.configureIdentity(accessory); 174 | 175 | this.accessories[name] = i_accessory; 176 | this.hap_accessories[name] = accessory; 177 | } 178 | 179 | Controller.prototype.removeAccessory = function(name) { 180 | 181 | var ack, message; 182 | 183 | if (typeof(this.accessories[name]) === "undefined") { 184 | ack = false; message = "accessory '" + name + "' not found."; 185 | 186 | } else { 187 | this.log.debug("Controller.removeAccessory '%s'", name); 188 | 189 | api.unregisterPlatformAccessories(plugin_name, platform_name, [this.hap_accessories[name]]); 190 | delete this.accessories[name]; 191 | delete this.hap_accessories[name]; 192 | ack = true; message = "accessory '" + name + "' is removed."; 193 | } 194 | 195 | return {"topic": "removeAccessory", "ack": ack, "message": message}; 196 | } 197 | 198 | Controller.prototype.removeService = function(api_accessory) { 199 | 200 | var ack, message; 201 | var name = api_accessory.name; 202 | var service_name = api_accessory.service_name; 203 | 204 | if (typeof(this.accessories[name]) === "undefined") { 205 | ack = false; message = "accessory '" + name + "' not found."; 206 | 207 | } else if (typeof service_name === "undefined") { 208 | ack = false; message = "service_name undefined."; 209 | 210 | } else if (this.accessories[name].service_namesList.indexOf(service_name) < 0) { 211 | ack = false; message = "accessory '" + name + "', service_name '" + service_name + "' undefined."; 212 | 213 | } else if (typeof this.hap_accessories[name].getServiceByUUIDAndSubType(service_name, service_name) === "undefined") { 214 | ack = false; message = "accessory '" + name + "', service_name '" + service_name + "' not found."; 215 | 216 | } else { 217 | this.hap_accessories[name].removeService(this.accessories[name].services[service_name]); 218 | this.accessories[name].removeService(service_name); 219 | 220 | //this.log.debug("Controller.removeService '%s' '%s'", name, service_name); 221 | ack = true; message = "accessory '" + name + "' service_name '" + service_name + "' is removed."; 222 | } 223 | 224 | return {"topic": "addService", "ack": ack, "message": message}; 225 | } 226 | 227 | Controller.prototype.updateReachability = function(accessory) { 228 | 229 | var ack, message; 230 | var name = accessory.name; 231 | var reachable = accessory.reachable; 232 | //this.log.debug("Controller.updateReachability %s %s", name, reachable); 233 | 234 | if (typeof name === "undefined") { 235 | ack = false; message = "name undefined."; 236 | 237 | } else if (typeof reachable === "undefined") { 238 | ack = false; message = "reachable undefined."; 239 | 240 | } else if (typeof(this.accessories[name]) === "undefined") { 241 | ack = false; message = "accessory '" + name + "' not found."; 242 | 243 | } else { 244 | this.log.debug("Controller.updateReachability '%s'", name); 245 | 246 | this.accessories[name].reachable = reachable; 247 | this.hap_accessories[name].updateReachability(reachable); 248 | 249 | ack = true; message = "accessory '" + name + "' reachability set to " + reachable; 250 | } 251 | 252 | return {"topic": "updateReachability", "ack": ack, "message": message}; 253 | } 254 | 255 | Controller.prototype.setAccessoryInformation = function(accessory) { 256 | 257 | this.log.debug("Controller.setAccessoryInformation %s", JSON.stringify(accessory)); 258 | var message; 259 | var ack; 260 | var name = accessory.name; 261 | 262 | if (typeof this.hap_accessories[name] === "undefined") { 263 | ack = false; message = "accessory '" + name + "' undefined."; 264 | } else { 265 | var service = this.hap_accessories[name].getService(Service.AccessoryInformation); 266 | 267 | if (typeof accessory.manufacturer !== "undefined") { 268 | service.setCharacteristic(Characteristic.Manufacturer, accessory.manufacturer); 269 | ack = true; 270 | } 271 | if (typeof accessory.model !== "undefined") { 272 | service.setCharacteristic(Characteristic.Model, accessory.model); 273 | ack = true; 274 | } 275 | if (typeof accessory.serialnumber !== "undefined") { 276 | service.setCharacteristic(Characteristic.SerialNumber, accessory.serialnumber); 277 | ack = true; 278 | } 279 | 280 | if (ack) { 281 | message = "accessory '" + name + "', accessoryinformation is set."; 282 | } else { 283 | message = "accessory '" + name + "', accessoryinforrmation properties undefined."; 284 | } 285 | } 286 | 287 | return {"topic": "setAccessoryInformation", "ack": ack, "message": message}; 288 | } 289 | 290 | Controller.prototype.getAccessories = function(api_accessory) { 291 | 292 | var name; 293 | var accessories = {}; 294 | var service, characteristics; 295 | var ack, message; 296 | 297 | if (typeof api_accessory.name !== "undefined") { 298 | name = api_accessory.name; 299 | } else { 300 | name = "*"; 301 | } 302 | 303 | if (name !== "*" && typeof(this.accessories[name]) === "undefined") { 304 | ack = false; message = "name '" + name + "' undefined."; 305 | 306 | } else { 307 | switch (name) { 308 | case "*": 309 | case "all": 310 | for (var k in this.accessories) { 311 | //this.log.debug("Controller.getAccessories %s", JSON.stringify(this.accessories[k], null, 2)); 312 | service = this.accessories[k].service_types; 313 | characteristics = this.accessories[k].i_value; 314 | accessories[k] = {"services": service, "characteristics": characteristics}; 315 | } 316 | break; 317 | 318 | default: 319 | service = this.accessories[name].service_types; 320 | characteristics = this.accessories[name].i_value; 321 | accessories[name] = {"services": service, "characteristics": characteristics}; 322 | } 323 | ack = true; message = "name '" + name + "' valid."; 324 | } 325 | 326 | return {"topic": "getAccessories", "ack": ack, "message": message, "accessories": accessories}; 327 | } 328 | 329 | // 330 | // Model functions 331 | // 332 | 333 | Controller.prototype.createModel = function (model_parameters) { 334 | 335 | this.Model = new Model(model_parameters); 336 | } 337 | 338 | Controller.prototype.start = function () { 339 | 340 | this.log.debug("Controller.start"); 341 | this.Model.start(); 342 | } 343 | 344 | Controller.prototype.get = function (name, service_name, c, callback) { 345 | 346 | this.Model.get(name, service_name, c, callback); 347 | } 348 | 349 | Controller.prototype.set = function (name, service_name, c, value, callback) { 350 | 351 | this.Model.set(name, service_name, c, value, callback); 352 | } 353 | 354 | Controller.prototype.setValue = function (api_accessory) { 355 | 356 | var ack, message; 357 | var result = {}; 358 | 359 | result = this.validate(api_accessory); 360 | 361 | if (!result.isValid) { 362 | ack = false; message = result.message; 363 | this.log.debug("Controller.setValue %s", message); 364 | 365 | } else { 366 | result = this.accessories[api_accessory.name].save_and_setValue(platform_name, result.service_name, api_accessory.characteristic, result.value); 367 | 368 | if (!result.isValid) { 369 | ack = false; message = "name '" + api_accessory.name + "', value '" + result.value + "' outside range"; 370 | this.log.debug("Controller.setValue %s", message); 371 | 372 | } else { 373 | ack = true; message = "name '" + api_accessory.name + "' valid."; 374 | } 375 | } 376 | //this.log.debug("Controller.setValue %s %s", ack, message); 377 | return {"topic": "setValue", "ack": ack, "message": message}; 378 | } 379 | 380 | Controller.prototype.identify = function (name) { 381 | 382 | var manufacturer = this.hap_accessories[name].getService(Service.AccessoryInformation).getCharacteristic("Manufacturer").value; 383 | var model = this.hap_accessories[name].getService(Service.AccessoryInformation).getCharacteristic("Model").value; 384 | var serialnumber = this.hap_accessories[name].getService(Service.AccessoryInformation).getCharacteristic("Serial Number").value; 385 | 386 | this.log("identify name '%s' manufacturer '%s' model '%s' serialnumber '%s'", name, manufacturer, model, serialnumber); 387 | 388 | this.Model.identify(name, manufacturer, model, serialnumber); 389 | } 390 | 391 | Controller.prototype.validate = function(api_accessory) { 392 | 393 | var name = api_accessory.name; 394 | var service_name = api_accessory.service_name; 395 | var c = api_accessory.characteristic; 396 | var value = api_accessory.value; 397 | 398 | var ack; 399 | var message = ""; 400 | 401 | // backwards compatible 402 | if (typeof service_name === "undefined") { 403 | service_name = name; 404 | if (typeof this.accessories[name].services[service_name] === "undefined") { 405 | ack = false; message = "name '" + name + "', service_name '" + service_name + "' undefined."; 406 | this.log.debug("Controller.validate %s", message); 407 | return {isValid: ack, message: message, service_name: service_name, value: value}; 408 | } 409 | } 410 | 411 | if(typeof(this.accessories[name]) === "undefined") { 412 | ack = false; message = "name '" + name + "' undefined."; 413 | 414 | } else if (typeof(Characteristic[c]) !== "function") { 415 | ack = false; message = "characteristic '" + c + "' undefined."; 416 | 417 | } else if (typeof(api_accessory.value) === "undefined" || api_accessory.value === null) { 418 | ack = false; message = "name '" + name + "' value undefined."; 419 | 420 | } else if (typeof this.accessories[name].services[service_name] == "undefined") { 421 | ack = false; message = "name '" + name + "', service_name '" + service_name + "' undefined."; 422 | 423 | } else if (typeof(this.accessories[name].services[service_name].getCharacteristic(Characteristic[c])) === "undefined") { 424 | message = "name '" + name + "' service_name '" + service_name + "' characteristic do not match."; 425 | 426 | } else { 427 | ack = true; message = "name '" + name + "' valid."; 428 | } 429 | 430 | return {isValid: ack, message: message, service_name: service_name, value: value}; 431 | } 432 | -------------------------------------------------------------------------------- /lib/model.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Utils = require('./utils.js').Utils; 4 | 5 | var plugin_name, Characteristic; 6 | var addAccessory, addService, removeAccessory, removeService, setValue, getAccessories, updateReachability, setAccessoryInformation; 7 | 8 | module.exports = { 9 | Model: Model 10 | } 11 | 12 | function Model(params) { 13 | 14 | this.config = params.config; 15 | this.log = params.log; 16 | plugin_name = params.plugin_name; 17 | Characteristic = params.Characteristic; 18 | 19 | addAccessory = params.addAccessory; 20 | addService = params.addService; 21 | removeAccessory = params.removeAccessory; 22 | removeService = params.removeService; 23 | setValue = params.setValue; 24 | getAccessories = params.getAccessories; 25 | updateReachability = params.updateReachability; 26 | setAccessoryInformation = params.setAccessoryInformation; 27 | } 28 | 29 | Model.prototype.start = function() { 30 | 31 | var result; 32 | var accessory = {}; 33 | 34 | // addAccessory 35 | accessory = {"name":"mvc_lamp","service_name":"light","service":"Switch"}; 36 | 37 | result = addAccessory(accessory); 38 | this.log("result: %s", JSON.stringify(result)); 39 | 40 | 41 | // addAccessory 42 | accessory = {"name":"mvc_sensor","service_name":"temperature","service":"TemperatureSensor"}; 43 | 44 | result = addAccessory(accessory); 45 | this.log("result: %s", JSON.stringify(result)); 46 | 47 | 48 | // setValue 49 | accessory = {"name":"mvc_sensor","service_name":"temperature","characteristic":"CurrentTemperature","value": 20.5}; 50 | 51 | result = setValue(accessory); 52 | this.log("result: %s", JSON.stringify(result)); 53 | 54 | 55 | // updateReachability 56 | accessory = {"name":"mvc_lamp","reachable": true}; 57 | 58 | result = updateReachability(accessory); 59 | this.log("result: %s", JSON.stringify(result)); 60 | 61 | 62 | // setAccessoryInformation 63 | accessory = {"name": "mvc_lamp", "manufacturer": "espressif", "model": "esp8266-12", "serialnumber": "4711"} 64 | 65 | result = setAccessoryInformation(accessory); 66 | this.log("result: %s", JSON.stringify(result)); 67 | 68 | 69 | // getAccessories 70 | accessory = {"name":"*"}; // or accessory = {"name":"mvc_sensor"}; 71 | 72 | result = getAccessories(accessory); 73 | this.log("result: %s", JSON.stringify(result, null, 2)); 74 | 75 | 76 | /* uncomment if needed 77 | // removeAccessory 78 | accessory = {"name":"mvc_sensor"}; 79 | 80 | result = removeAccessory(accessory); 81 | this.log("result: %s", JSON.stringify(result)); 82 | 83 | 84 | // addService 85 | accessory = {"name":"mvc_sensor","service_name":"humidity","service":"HumiditySensor"}; 86 | 87 | result = addService(accessory); 88 | this.log("result: %s", JSON.stringify(result)); 89 | 90 | 91 | // removeService 92 | accessory = {"name":"mvc_sensor","service_name": "humidity"}; 93 | 94 | result = removeService(accessory); 95 | this.log("result: %s", JSON.stringify(result)); 96 | */ 97 | } 98 | 99 | 100 | Model.prototype.get = function(name, service_name, characteristic) { 101 | 102 | this.log("get '%s' '%s' '%s'", name, service_name, characteristic); 103 | } 104 | 105 | Model.prototype.set = function(name, service_name, characteristic, value, callback) { 106 | 107 | this.log("set '%s' '%s' '%s' %s", name, service_name, characteristic, value); 108 | 109 | callback(); 110 | } 111 | 112 | Model.prototype.identify = function (name, manufacturer, model, serialnumber) { 113 | 114 | this.log("identify, turn on/off a lamp or just log this message"); 115 | } 116 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var fs = require('fs'); 5 | var request = require('request'); 6 | 7 | var plugins_name = "plugins"; 8 | var package_json = "../package.json"; 9 | var plugin_version; 10 | var github_version; 11 | var npm_version; 12 | 13 | module.exports = { 14 | Utils: Utils 15 | } 16 | 17 | function Utils() { 18 | } 19 | 20 | Utils.loadConfig = function(storage_path, plugin_name, config_name) { 21 | 22 | var plugin_storage_path = path.join(storage_path, plugins_name, plugin_name); 23 | var config_path = path.join(plugin_storage_path, config_name); 24 | 25 | // Complain and exit if config_name doesn't exist yet 26 | if (!fs.existsSync(config_path)) { 27 | console.log("Couldn't find a %s file at %s.", config_name, plugin_storage_path); 28 | process.exit(1); 29 | } 30 | 31 | // Load up the configuration file 32 | var config; 33 | try { 34 | //console.log("Utils.loadConfig"); 35 | config = JSON.parse(fs.readFileSync(config_path)); 36 | } 37 | catch (err) { 38 | console.log("There was a problem reading your %s file.", config_name); 39 | console.log("Please try pasting your %s file here to validate it: http://jsonlint.com", config_name); 40 | console.log(""); 41 | throw err; 42 | } 43 | return config; 44 | } 45 | 46 | Utils.saveConfig = function(storage_path, plugin_name, config_name, data) { 47 | 48 | var plugin_storage_path = path.join(storage_path, plugins_name, plugin_name); 49 | var config_path = path.join(plugin_storage_path, config_name); // "config_test.json"); 50 | 51 | try { 52 | //console.log("Utils.saveConfig %s", data); 53 | fs.writeFileSync(config_path, JSON.stringify(data, null, 2)); 54 | } 55 | catch (err) { 56 | console.log("There was a problem writing your %s file.", config_name); 57 | throw err; 58 | } 59 | } 60 | 61 | Utils.getHomeKitTypes = function() { 62 | 63 | var uuid, items, service_type; 64 | var HomeKitTypes = {}; 65 | 66 | var types_path = path.join(__dirname, '../../hap-nodejs/lib/gen/HomeKitTypes.js'); 67 | var lines = fs.readFileSync(types_path).toString().split('\n'); 68 | var line; 69 | 70 | for(var k in lines) { 71 | line = lines[k]; 72 | 73 | if (line.includes("Service.") && line.includes("UUID")) { 74 | items = line.split("."); 75 | service_type = items[1]; 76 | uuid = items[2].slice(8,44); 77 | //console.log(service_type); 78 | //console.log(uuid); 79 | HomeKitTypes[uuid] = service_type; 80 | } 81 | } 82 | return HomeKitTypes; 83 | } 84 | 85 | Utils.getPluginVersion = function() { 86 | return plugin_version; 87 | } 88 | 89 | Utils.get_npmVersion = function(pkg) { 90 | // Update version for the next call 91 | this.read_npmVersion(pkg, function(version) { 92 | npm_version = version; 93 | }); 94 | return npm_version; 95 | } 96 | 97 | Utils.n2b = function (number) { 98 | return (number == 0 || number == false) ? false : true; 99 | } 100 | 101 | Utils.getGitHubVersion = function(pkg, github_url) { 102 | this.read_GitHubVersion(pkg, github_url); 103 | return github_version; 104 | } 105 | 106 | Utils.readPluginVersion = function() { 107 | 108 | var packageJSONPath = path.join(__dirname, package_json); 109 | var packageJSON = JSON.parse(fs.readFileSync(packageJSONPath)); 110 | plugin_version = packageJSON.version; 111 | return plugin_version; 112 | } 113 | 114 | Utils.read_npmVersion = function(pck, callback) { 115 | var exec = require('child_process').exec; 116 | var cmd = 'npm view '+pck+' version'; 117 | exec(cmd, function(error, stdout, stderr) { 118 | npm_version = stdout.trim(); 119 | //npm_version = stdout.replace(/(\r\n|\n|\r)/gm,""); 120 | callback(npm_version); 121 | //console.log("npm_version %s", npm_version); 122 | }); 123 | } 124 | 125 | Utils.readGitHubVersion = function (pkg, url) { 126 | 127 | request.get({url: url}, function(err, response, body) { 128 | 129 | if (!err && response.statusCode == 200) { 130 | var package_json = body.trim(); 131 | //console.log("package.json %s", JSON.stringify(package)); 132 | var packageJSON = JSON.parse(package_json); 133 | github_version = packageJSON.version; 134 | if (github_version > plugin_version) { 135 | console.log("%s a new version %s is avaiable", pkg, github_version); 136 | } 137 | } 138 | else { 139 | console.log(err); 140 | if (response) console.log("statusCode: %s Message: %s", response.statusCode, response.statusMessage); 141 | } 142 | }); 143 | } 144 | 145 | /** 146 | * Converts an HSV color value to RGB. Conversion formula 147 | * adapted from https://en.wikipedia.org/wiki/HSL_and_HSV 148 | * Assumes h, s, and v are contained in the set [0, 1] and 149 | * returns rgb (FFFFFF) 150 | * 151 | * @param Number h The hue 152 | * @param Number s The saturation 153 | * @param Number v The value (brigthness) 154 | * @return HexNumber The RGB representation 155 | */ 156 | 157 | Utils.hsv2rgb = function (h, s, v) { 158 | 159 | //console.log("h: %s s: %s v: %s", h, s, v); 160 | var r, g, b; 161 | 162 | if( s == 0 ) { 163 | r = v; g = v; b = v; 164 | } 165 | else { 166 | var i = Math.floor(h * 6); 167 | var f = h * 6 - i; 168 | var p = v * (1 - s); 169 | var q = v * (1 - f * s); 170 | var t = v * (1 - (1 - f) * s); 171 | 172 | switch(i % 6){ 173 | case 0: r = v; g = t; b = p; break; 174 | case 1: r = q; g = v; b = p; break; 175 | case 2: r = p; g = v; b = t; break; 176 | case 3: r = p; g = q; b = v; break; 177 | case 4: r = t; g = p; b = v; break; 178 | case 5: r = v; g = p; b = q; break; 179 | } 180 | } 181 | r = Math.round(r*255); 182 | g = Math.round(g*255); 183 | b = Math.round(b*255); 184 | //console.log("r: %s g: %s b: %s", r, g, b); 185 | return Number(0x1000000 + r*0x10000 + g*0x100 + b).toString(16).substring(1).toUpperCase(); 186 | } 187 | 188 | /** 189 | * Converts an RGB color value to HSV. Conversion formula 190 | * adapted from https://en.wikipedia.org/wiki/HSL_and_HSV 191 | * Assumes r, g, and b are contained in the set [0, 255] and 192 | * returns h, s, and v in the set [0, 1]. 193 | * 194 | * @param Number r The red color value 195 | * @param Number g The green color value 196 | * @param Number b The blue color value 197 | * @return Array The HSV representation 198 | */ 199 | 200 | Utils.rgb2hsv = function(r, g, b) { 201 | 202 | r = r/255, g = g/255, b = b/255; 203 | var max = Math.max(r, g, b), min = Math.min(r, g, b); 204 | var h, s, v = max; 205 | 206 | var d = max - min; 207 | s = max == 0 ? 0 : d / max; 208 | 209 | if(max == min){ 210 | h = 0; // achromatic 211 | } else { 212 | switch(max){ 213 | case r: h = (g - b) / d + (g < b ? 6 : 0); break; 214 | case g: h = (b - r) / d + 2; break; 215 | case b: h = (r - g) / d + 4; break; 216 | } 217 | h /= 6; 218 | } 219 | 220 | return [h, s, v]; 221 | } 222 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "homebridge-mvc", 3 | "version": "0.1.3", 4 | "description": "MVC Plugin for Homebridge", 5 | "license": "ISC", 6 | "author": { 7 | "name": "flurin" 8 | }, 9 | "keywords": [ 10 | "homebridge", 11 | "homebridge-plugin", 12 | "homekit", 13 | "siri", 14 | "node-RED" 15 | ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/cflurin/homebridge-mvc.git" 19 | }, 20 | "bugs": { 21 | "url": "http://github.com/cflurin/homebridge-mvc/issues" 22 | }, 23 | "engines": { 24 | "node": ">=4.3.2", 25 | "homebridge": ">=0.4.16" 26 | }, 27 | "dependencies": { 28 | "request": ">=2.79.0" 29 | } 30 | } 31 | --------------------------------------------------------------------------------