thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
152 lines (135 loc) • 4.36 kB
JavaScript
module.exports = {
metadata: {
plugin: "doorSensor",
label: "EnOcean IP Magnetic contact Sensor",
role: "sensor",
family: "doorSensor",
deviceTypes: ["enocean-ip/gateway"],
events: [{
id: 'objectPresent',
label: 'Object Present',
},
{
id: 'objectNotPresent',
label: 'Object Not Present',
},
],
services: [],
state: [{
id: 'objectPresent',
label: 'Object Present',
type: {
id: 'boolean',
},
}
// {
// id: 'initialized', label: 'Initialized',
// type: {
// id: 'boolean',
// },
// },
// {
// id: 'signalStrength',
// label: 'Signal strength',
// type: {
// id: 'integer',
// },
// },
// {
// id: 'batteryPercentage',
// label: 'Battery (%)',
// type: {
// id: 'decimal', // float
// },
// },
],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new DoorSensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function DoorSensor() {
/**
*
*/
DoorSensor.prototype.start = function () {
var deferred = q.defer();
//this.logLevel = 'debug';
this.state = {};
if (this.isSimulated()) {
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 === 'contact') {
//console.log("contact");
if (body.state.functions[n].value === 'open') {
this.state.objectPresent = false;
// console.log("open");
} else if (body.state.functions[n].value === 'closed') {
this.state.objectPresent = true;
}
}
}
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) {
//console.log('Device ' + telegram.friendlyId + ' is processing ', telegram.functions);
for (var n in telegram.functions) {
if (telegram.functions[n].key === 'contact') {
//console.log("contact");
if (telegram.functions[n].value === 'open') {
this.state.objectPresent = false;
// console.log("open");
this.publishStateChange();
this.publishEvent('objectNotPresent', {});
} else if (telegram.functions[n].value === 'closed') {
this.state.objectPresent = true;
this.publishStateChange();
this.publishEvent('objectPresent', {});
}
break;
}
}
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
DoorSensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
DoorSensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
DoorSensor.prototype.stop = function () {
}
}