2013-10-25 14:06:29 +02:00
|
|
|
|
2015-07-02 00:45:34 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2015-07-05 16:29:24 +02:00
|
|
|
var SensorTag = require("sensortag");
|
2013-10-25 14:06:29 +02:00
|
|
|
|
2015-07-05 16:29:24 +02:00
|
|
|
function SensorTagNode(n) {
|
2015-07-02 00:45:34 +02:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.name = n.name;
|
|
|
|
this.topic = n.topic;
|
|
|
|
this.uuid = n.uuid;
|
|
|
|
this.temperature = n.temperature;
|
|
|
|
this.pressure = n.pressure;
|
|
|
|
this.humidity = n.humidity;
|
|
|
|
this.accelerometer = n.accelerometer;
|
|
|
|
this.magnetometer = n.magnetometer;
|
|
|
|
this.gyroscope = n.gyroscope;
|
|
|
|
this.luxometer = n.luxometer;
|
|
|
|
this.keys = n.keys;
|
|
|
|
if (this.uuid === "") { this.uuid = undefined; }
|
|
|
|
var node = this;
|
2016-04-01 17:02:49 +02:00
|
|
|
node.discovering = false;
|
2013-11-22 14:58:41 +01:00
|
|
|
|
2016-03-04 14:10:00 +01:00
|
|
|
if (typeof node.stag === "undefined") {
|
2016-04-01 17:02:49 +02:00
|
|
|
node.loop = setInterval(function() {
|
|
|
|
if (!node.discovering) {
|
|
|
|
node.status({fill:"blue", shape:"dot", text:"discovering..."});
|
|
|
|
node.discovering = true;
|
|
|
|
SensorTag.discover(function(sensorTag) {
|
|
|
|
node.status({fill:"blue", shape:"dot", text:"connecting"});
|
|
|
|
node.stag = sensorTag;
|
|
|
|
node.log("found sensor tag: " + sensorTag._peripheral.uuid);
|
|
|
|
node.topic = node.topic || sensorTag._peripheral.uuid;
|
|
|
|
sensorTag.connect(function() {
|
|
|
|
node.log("connected to sensor tag: " + sensorTag._peripheral.uuid);
|
|
|
|
node.status({fill:"green", shape:"dot", text:"connected"});
|
2016-01-18 19:16:18 +01:00
|
|
|
|
2016-04-01 17:41:58 +02:00
|
|
|
sensorTag.once('disconnect', function() {
|
2016-04-01 17:02:49 +02:00
|
|
|
node.discovering = false;
|
|
|
|
node.status({fill:"red", shape:"ring", text:"disconnected"});
|
|
|
|
node.log("disconnected ",node.uuid);
|
|
|
|
});
|
2016-01-18 19:16:18 +01:00
|
|
|
|
2016-04-01 17:02:49 +02:00
|
|
|
sensorTag.discoverServicesAndCharacteristics(function() {
|
|
|
|
sensorTag.enableIrTemperature(function() {});
|
|
|
|
sensorTag.on('irTemperatureChange',
|
|
|
|
function(objectTemperature, ambientTemperature) {
|
|
|
|
var msg = {'topic': node.topic + '/temperature'};
|
|
|
|
msg.payload = {'object': +objectTemperature.toFixed(1),
|
|
|
|
'ambient': +ambientTemperature.toFixed(1)
|
|
|
|
};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.enableBarometricPressure(function() {});
|
|
|
|
sensorTag.on('barometricPressureChange', function(pressure) {
|
|
|
|
var msg = {'topic': node.topic + '/pressure'};
|
|
|
|
msg.payload = {'pressure': parseInt(pressure)};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.enableHumidity(function() {});
|
|
|
|
sensorTag.on('humidityChange', function(temp, humidity) {
|
|
|
|
var msg = {'topic': node.topic + '/humidity'};
|
|
|
|
msg.payload = {'temperature': +temp.toFixed(1),
|
|
|
|
'humidity': +humidity.toFixed(1)
|
|
|
|
};
|
2016-08-10 11:26:20 +02:00
|
|
|
if ((temp !== -40) || (humidity !== 100)) {
|
|
|
|
node.send(msg);
|
|
|
|
}
|
2016-04-01 17:02:49 +02:00
|
|
|
});
|
|
|
|
sensorTag.enableAccelerometer(function() {});
|
|
|
|
sensorTag.on('accelerometerChange', function(x,y,z) {
|
|
|
|
var msg = {'topic': node.topic + '/accelerometer'};
|
|
|
|
msg.payload = {'x': +x.toFixed(2), 'y': +y.toFixed(2), 'z': +z.toFixed(2)};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.enableMagnetometer(function() {});
|
|
|
|
sensorTag.on('magnetometerChange', function(x,y,z) {
|
|
|
|
var msg = {'topic': node.topic + '/magnetometer'};
|
|
|
|
msg.payload = {'x': +x.toFixed(2), 'y': +y.toFixed(2), 'z': +z.toFixed(2)};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.enableGyroscope(function() {});
|
|
|
|
sensorTag.on('gyroscopeChange', function(x,y,z) {
|
|
|
|
var msg = {'topic': node.topic + '/gyroscope'};
|
|
|
|
msg.payload = {'x': +x.toFixed(2), 'y': +y.toFixed(2), 'z': +z.toFixed(2)};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.on('simpleKeyChange', function(left, right, mag) {
|
|
|
|
var msg = {'topic': node.topic + '/keys'};
|
|
|
|
msg.payload = {'left': left, 'right': right, 'magnet': mag};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
sensorTag.on('luxometerChange', function(lux) {
|
|
|
|
var msg = {'topic': node.topic + '/luxometer'};
|
|
|
|
msg.payload = {'lux': parseInt(lux)};
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
enable(node);
|
|
|
|
});
|
2015-07-02 00:45:34 +02:00
|
|
|
});
|
2016-04-01 17:02:49 +02:00
|
|
|
},node.uuid);
|
|
|
|
}
|
|
|
|
},15000);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
console.log("reconfig",node.uuid);
|
|
|
|
enable(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on("close", function() {
|
2016-04-01 17:02:49 +02:00
|
|
|
if (node.loop) { clearInterval(node.loop); }
|
|
|
|
if (node.stag) { node.stag.disconnect(function() {}); }
|
2013-10-25 14:06:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-05 16:29:24 +02:00
|
|
|
var enable = function(node) {
|
2015-07-02 00:45:34 +02:00
|
|
|
if (node.temperature) {
|
|
|
|
node.stag.notifyIrTemperature(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyIrTemperature(function() {});
|
|
|
|
}
|
|
|
|
if (node.pressure) {
|
|
|
|
node.stag.notifyBarometricPressure(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyBarometricPressure(function() {});
|
|
|
|
}
|
|
|
|
if (node.humidity) {
|
|
|
|
node.stag.notifyHumidity(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyHumidity(function() {});
|
|
|
|
}
|
|
|
|
if (node.accelerometer) {
|
|
|
|
node.stag.notifyAccelerometer(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyAccelerometer(function() {});
|
|
|
|
}
|
|
|
|
if (node.magnetometer) {
|
|
|
|
node.stag.notifyMagnetometer(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyMagnetometer(function() {});
|
|
|
|
}
|
|
|
|
if (node.gyroscope) {
|
|
|
|
node.stag.notifyGyroscope(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifyGyroscope(function() {});
|
|
|
|
}
|
2015-07-21 10:17:31 +02:00
|
|
|
if (node.stag.type === "cc2650") {
|
2015-07-02 14:29:13 +02:00
|
|
|
if (node.luxometer) {
|
|
|
|
node.stag.enableLuxometer(function() {});
|
|
|
|
node.stag.notifyLuxometer(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 14:29:13 +02:00
|
|
|
node.stag.unnotifyLuxometer(function() {});
|
|
|
|
node.stag.disableLuxometer(function() {});
|
|
|
|
}
|
2015-07-02 00:45:34 +02:00
|
|
|
}
|
|
|
|
if (node.keys) {
|
|
|
|
node.stag.notifySimpleKey(function() {});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-02 00:45:34 +02:00
|
|
|
node.stag.unnotifySimpleKey(function() {});
|
|
|
|
}
|
2014-02-25 12:18:45 +01:00
|
|
|
}
|
2015-07-04 23:21:00 +02:00
|
|
|
|
2015-07-05 16:29:24 +02:00
|
|
|
RED.nodes.registerType("sensorTag",SensorTagNode);
|
2013-10-25 14:06:29 +02:00
|
|
|
}
|