UNPKG

thing-it-device-enocean-ip

Version:

[thing-it-node] Device Plugin for EnOcean IP products.

193 lines (179 loc) 6.1 kB
module.exports = { metadata: { plugin: "rockerSwitch2Rocker", label: "EnOcean IP Rocker Switch, 2 Rocker", role: "actor", family: "rockerSwitch2Rocker", deviceTypes: ["enocean-ip/gateway"], services: [], state: [{ label: 'Last Operation', id: 'lastOperation' }, { label: 'Last Operation Timestamp', id: 'lastOperationTimestamp' }, { label: 'Button A0', id: 'buttonA0', type:{ id: "boolean" } }, { label: 'Button AI', id: 'buttonAI', type:{ id: "boolean" } }, { label: 'Button B0', id: 'buttonB0', type:{ id: "boolean" } }, { label: 'Button BI', id: 'buttonBI', type:{ id: "boolean" } } ], events: [{ label: 'Button A0 Pressed', id: 'buttonA0Pressed' }, { label: 'Button A0 Released', id: 'buttonA0Released' }, { label: 'Button AI Pressed', id: 'buttonAIPressed' }, { label: 'Button AI Released', id: 'buttonAIReleased' }, { label: 'Button B0 Pressed', id: 'buttonB0Pressed' }, { label: 'Button B0 Released', id: 'buttonB0Released' }, { label: 'Button BI Pressed', id: 'buttonBIPressed' }, { label: 'Button BI Released', id: 'buttonBIReleased' }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }] }, create: function () { return new RockerSwitch2Rocker(); } }; var q = require('q'); var moment = require('moment'); function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } /** * */ function RockerSwitch2Rocker() { /** * */ RockerSwitch2Rocker.prototype.start = function () { var deferred = q.defer(); this.state = {}; if (this.isSimulated()) { this.interval = setInterval(function () { if (Math.random() > 0.5) { this.state.lastOperation = 'Button AI' + ' Pressed'; } else { this.state.lastOperation = 'Button A0 Pressed'; } this.publishStateChange(); }.bind(this), 10000); deferred.resolve(); } else { //Retrieve current state this.device.adapter.getDeviceState(this.configuration.deviceId).then(function(body){ for(let n in body.state.functions){ if (body.state.functions[n].key === 'buttonA0'){ this.state.buttonA0 = body.state.functions[n].value === 'pressed'; } if (body.state.functions[n].key === 'buttonAI'){ this.state.buttonAI = body.state.functions[n].value === 'pressed'; } if (body.state.functions[n].key === 'buttonB0'){ this.state.buttonB0 = body.state.functions[n].value === 'pressed'; } if (body.state.functions[n].key === 'buttonBI'){ this.state.buttonBI = body.state.functions[n].value === 'pressed'; } } this.publishStateChange(); this.logDebug(this.state); }.bind(this), function (err){ console.log(err) }.bind(this)); //Retrieve updated state this.device.adapter.listeners.push(telegram => { if (telegram.deviceId === this.configuration.deviceId) { this.logDebug('Device ' + telegram.friendlyId + ' is processing ', telegram.functions); for (var n in telegram.functions) { if (telegram.functions[n].key.indexOf('button') === 0) { if (telegram.functions[n].key === 'buttonA0'){ this.state.buttonA0 = telegram.functions[n].value === 'pressed'; } if (telegram.functions[n].key === 'buttonAI'){ this.state.buttonAI = telegram.functions[n].value === 'pressed'; } if (telegram.functions[n].key === 'buttonB0'){ this.state.buttonB0 = telegram.functions[n].value === 'pressed'; } if (telegram.functions[n].key === 'buttonBI'){ this.state.buttonBI = telegram.functions[n].value === 'pressed'; } this.publishEvent(telegram.functions[n].key + capitalizeFirstLetter(telegram.functions[n].value), {}); this.state.lastOperation = capitalizeFirstLetter(telegram.functions[n].key) + ' ' + capitalizeFirstLetter(telegram.functions[n].value); this.state.lastOperationTimestamp = moment().toISOString(); this.publishStateChange(); } } } }); deferred.resolve(); } return deferred.promise; }; /** * */ RockerSwitch2Rocker.prototype.getState = function () { return this.state; }; /** * */ RockerSwitch2Rocker.prototype.setState = function (state) { }; /** * */ RockerSwitch2Rocker.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } }