thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
117 lines (107 loc) • 3.27 kB
JavaScript
module.exports = {
metadata: {
plugin: "vibrationSensor_A5-12-00",
label: "EnOcean IP Vibration Sensor A5-12-00",
role: "sensor",
family: "vibrationSensor_A5-12-00",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [],
events: [{
id: "vibrationDetected",
label: "Vibration Detected"
}],
state: [{
label: "Last intrusion Timestamp",
id: "lastIntrusionTimestamp",
type: {
id: "string"
}
},
{
label: "Cumulative counter",
id: "counter",
type: {
id: "integer"
}
}
],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new VibriationSensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function VibriationSensor() {
/**
*
*/
VibriationSensor.prototype.start = function () {
var deferred = q.defer();
this.state = {};
if (this.isSimulated()) {
this.publishStateChange();
} else {
/*this.device.adapter.getDeviceState(this.configuration.deviceId).then(function (body) {
for (var n in body.state.functions) {
if (body.state.functions[n].key === "cumulativeCounter") {
this.state.counter = body.state.functions[n].value;
}
}
this.publishStateChange();
}.bind(this), function (err) {
console.log(err)
}.bind(this));*/
// Retrieve current state
this.device.adapter.listeners.push(telegram => {
if (telegram.deviceId === this.configuration.deviceId) {
if (telegram.telegramInfo.data.match(/.{1,2}/g)[0] != "00") return;
for (var n in telegram.functions) {
if (telegram.functions[n].key === "cumulativeCounter") {
this.state.counter = telegram.functions[n].value;
this.state.lastIntrusionTimestamp = moment().toISOString();
this.publishEvent('vibrationDetected', {});
this.logDebug("Last intrusion alarm: " + this.state.lastIntrusionTimestamp);
}
}
this.publishStateChange();
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
VibriationSensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
VibriationSensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
VibriationSensor.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}