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

View File

@ -33,90 +33,95 @@ module.exports = function(RED) {
this.keys = n.keys; this.keys = n.keys;
if (this.uuid === "") { this.uuid = undefined; } if (this.uuid === "") { this.uuid = undefined; }
var node = this; var node = this;
node.discovering = false;
if (typeof node.stag === "undefined") { 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) { sensorTag.on('disconnect', function() {
node.status({fill:"blue", shape:"dot", text:"connecting"}); node.discovering = false;
node.stag = sensorTag; node.status({fill:"red", shape:"ring", text:"disconnected"});
node.log("found sensor tag: " + sensorTag._peripheral.uuid); node.log("disconnected ",node.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() { sensorTag.discoverServicesAndCharacteristics(function() {
node.status({fill:"red", shape:"ring", text:"disconnected"}); sensorTag.enableIrTemperature(function() {});
node.log("disconnected ",node.uuid); 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.on('luxometerChange', function(lux) {
sensorTag.enableIrTemperature(function() {}); var msg = {'topic': node.topic + '/luxometer'};
sensorTag.on('irTemperatureChange', msg.payload = {'lux': parseInt(lux)};
function(objectTemperature, ambientTemperature) { node.send(msg);
var msg = {'topic': node.topic + '/temperature'}; });
msg.payload = {'object': +objectTemperature.toFixed(1), enable(node);
'ambient': +ambientTemperature.toFixed(1) });
};
node.send(msg);
}); });
sensorTag.enableBarometricPressure(function() {}); },node.uuid);
sensorTag.on('barometricPressureChange', function(pressure) { }
var msg = {'topic': node.topic + '/pressure'}; },15000);
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);
} else { } else {
console.log("reconfig",node.uuid); console.log("reconfig",node.uuid);
enable(node); enable(node);
} }
this.on("close", function() { this.on("close", function() {
if (node.stag) { if (node.loop) { clearInterval(node.loop); }
node.stag.disconnect(function() {}); if (node.stag) { node.stag.disconnect(function() {}); }
}
}); });
} }

View File

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