let ti sensor tag try to reconnect

to close #196
This commit is contained in:
Dave Conway-Jones 2016-04-01 16:02:49 +01:00
parent 68bd4cc036
commit 98d44004c9
3 changed files with 90 additions and 86 deletions

View File

@ -51,12 +51,11 @@
<p>Node to read from the Ti SensorTag</p>
<p>The UUID field is the bluetooth mac address of the sensor tag, this is optional
and can be used to bind to a specific SensorTag if you have more than one
active in range at the same time. <b>Note</b>:you can only have one SensorTag
per Node-RED instance at the moment.</p>
active in range at the same time.
<p>The <i>topic</i> setting is a prefix that will be pre-pended to the name of the
sensor that creates the reading. e.g. <i>sensorTag/temperature</i>. If
blank it will be set to the UUID of the sensor tag.</p>
<p><strong>NOTE:</strong> Only 1 sensorTag can be read from at a time,
<p><strong>Note:</strong> Currently the node can only read from 1 sensorTag at a time,
if you add more than one to the canvas then only the first to connect
will work.</p>
<p>Linux users should <a href="https://github.com/sandeepmistry/bleno#running-on-linux" target="_new">READ THIS</a>.</p>
@ -67,7 +66,7 @@
category:'advanced-function',
color:"GoldenRod",
defaults:{
name:{value:"sensorTag"},
name:{value:""},
topic:{value:"sensorTag"},
uuid:{value:undefined},
temperature:{value:true},
@ -89,15 +88,15 @@
return this.name?"node_label_italic":"";
},
oneditsave:function() {
var mac = $("#node-input-uuid").val();
mac = mac.toLowerCase();
// nasty hack as I can't get global replace to work
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.toLowerCase();
$("#node-input-uuid").val(mac);
var mac = $("#node-input-uuid").val();
mac = mac.toLowerCase();
// nasty hack as I can't get global replace to work
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.replace(/:/gi,'');
mac = mac.toLowerCase();
$("#node-input-uuid").val(mac);
}
});
</script>

View File

@ -33,90 +33,95 @@ module.exports = function(RED) {
this.keys = n.keys;
if (this.uuid === "") { this.uuid = undefined; }
var node = this;
node.discovering = false;
if (typeof node.stag === "undefined") {
node.status({fill:"blue", shape:"dot", text:"discovering..."});
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"});
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"});
sensorTag.on('disconnect', function() {
node.discovering = false;
node.status({fill:"red", shape:"ring", text:"disconnected"});
node.log("disconnected ",node.uuid);
});
sensorTag.on('disconnect', function() {
node.status({fill:"red", shape:"ring", text:"disconnected"});
node.log("disconnected ",node.uuid);
});
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)
};
node.send(msg);
});
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.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.on('luxometerChange', function(lux) {
var msg = {'topic': node.topic + '/luxometer'};
msg.payload = {'lux': parseInt(lux)};
node.send(msg);
});
enable(node);
});
});
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)
};
node.send(msg);
});
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);
});
});
},node.uuid);
},node.uuid);
}
},15000);
} else {
console.log("reconfig",node.uuid);
enable(node);
}
this.on("close", function() {
if (node.stag) {
node.stag.disconnect(function() {});
}
if (node.loop) { clearInterval(node.loop); }
if (node.stag) { node.stag.disconnect(function() {}); }
});
}

View File

@ -1,7 +1,7 @@
{
"name": "node-red-node-sensortag",
"description": "A Node-RED node to read data from a TI SensorTag",
"version": "0.0.12",
"version": "0.0.13",
"keywords": [
"node-red",
"sensortag",