thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
115 lines (103 loc) • 2.99 kB
JavaScript
module.exports = {
metadata: {
plugin: "liquidDetection",
label: "Liquid Detector F6-05-01",
role: "sensor",
family: "liquidDetection",
deviceTypes: ["enocean-ip/gateway"],
services: [],
state: [{
label: 'Last Operation Timestamp',
id: 'lastOperationTimestamp',
type:{
id: "string"
}
}, {
label: 'Liquid Detected',
id: 'liquidDetected',
type:{
id: "boolean"
}
}],
events: [{
label: 'Liquid',
id: 'liquid'
}, {
label: 'No Liquid',
id: 'noLiquid'
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new LiquidDetection();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function LiquidDetection() {
/**
*
*/
LiquidDetection.prototype.start = function () {
var deferred = q.defer();
this.state = {};
if (this.isSimulated()) {
deferred.resolve();
}
else {
var dataArray = [];
this.device.adapter.listeners.push(telegram => {
if (telegram.deviceId === this.configuration.deviceId) {
this.logDebug('Device ' + telegram.friendlyId + ' is processing ', telegram.functions);
for (n in telegram.functions) {
if (telegram.functions[n].key === "liquidDetected") {
if (telegram.functions[n].value === "true") {
this.state.liquidDetected = true;
this.state.lastOperationTimestapm = moment().toISOString();
console.log(this.state);
this.publishEvent('liquid', {});
} else {
this.state.lastOperationTimestamp = moment().toISOString();
this.state.liquidDetected = false;
console.log(this.state);
this.publishEvent('noLiquid', {});
}
}
}
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
LiquidDetection.prototype.getState = function () {
return this.state;
};
/**
*
*/
LiquidDetection.prototype.setState = function (state) {
};
/**
*
*/
LiquidDetection.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}