2015-11-23 16:24:26 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
2016-02-20 18:50:37 +01:00
|
|
|
"use strict";
|
|
|
|
var eddystoneBeacon = require('eddystone-beacon');
|
|
|
|
var EddystoneBeaconScanner = require('eddystone-beacon-scanner');
|
2016-03-04 14:10:00 +01:00
|
|
|
var eddyBeacon = false;
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-04-16 11:08:21 +02:00
|
|
|
var checkLength = function(text) {
|
|
|
|
var l = text.length;
|
2017-01-29 18:45:44 +01:00
|
|
|
switch (true) {
|
|
|
|
case /^http:\/\/www./.test(text): {
|
|
|
|
l -= 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case /^https:\/\/www./.test(text): {
|
|
|
|
l -= 11;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case /^http:\/\//.test(text): {
|
|
|
|
l -= 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case /^https:\/\//.test(text): {
|
|
|
|
l -= 7;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-16 11:08:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 18:45:44 +01:00
|
|
|
switch (true) {
|
|
|
|
case /.*\.info\/.*/.test(text): {
|
|
|
|
l -= 5;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-16 11:08:21 +02:00
|
|
|
case /.*\.com\/.*/.test(text):
|
|
|
|
case /.*\.net\/.*/.test(text):
|
|
|
|
case /.*\.org\/.*/.test(text):
|
|
|
|
case /.*\.edu\/.*/.test(text):
|
|
|
|
case /.*\.biz\/.*/.test(text):
|
|
|
|
case /.*\.gov\/.*/.test(text):
|
2017-01-29 18:45:44 +01:00
|
|
|
case /.*\.info.*/.test(text): {
|
|
|
|
l -= 4;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-16 11:08:21 +02:00
|
|
|
case /.*\.com.*/.test(text):
|
|
|
|
case /.*\.net.*/.test(text):
|
|
|
|
case /.*\.org.*/.test(text):
|
|
|
|
case /.*\.edu.*/.test(text):
|
|
|
|
case /.*\.biz.*/.test(text):
|
2017-01-29 18:45:44 +01:00
|
|
|
case /.*\.gov.*/.test(text): {
|
|
|
|
l -= 3;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-16 11:08:21 +02:00
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:57:53 +01:00
|
|
|
function Beacon(n) {
|
2016-02-20 18:50:37 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
2016-03-14 21:24:22 +01:00
|
|
|
node.mode = n.mode;
|
2016-02-20 18:50:37 +01:00
|
|
|
node.power = n.power;
|
2016-03-14 22:30:50 +01:00
|
|
|
node.period = n.period;
|
|
|
|
node.count = n.count;
|
2016-02-20 18:50:37 +01:00
|
|
|
node.url = n.url;
|
2016-03-14 21:24:22 +01:00
|
|
|
node.namespace = n.namespace;
|
|
|
|
node.instance = n.instance;
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
node.options = {
|
|
|
|
txPowerLevel: node.power,
|
2016-02-21 14:56:56 +01:00
|
|
|
tlmPeriod: node.period,
|
2016-03-14 22:30:50 +01:00
|
|
|
tlmCount: node.count
|
2016-03-03 10:18:57 +01:00
|
|
|
};
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-03-14 21:24:22 +01:00
|
|
|
if (node.mode === "url" && node.url) {
|
2016-03-04 14:10:00 +01:00
|
|
|
if (!eddyBeacon) {
|
|
|
|
eddyBeacon = true;
|
|
|
|
try {
|
|
|
|
eddystoneBeacon.advertiseUrl(node.url, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:node.url});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-03-04 14:10:00 +01:00
|
|
|
node.error('Error setting beacon URL', e);
|
|
|
|
}
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
2016-03-08 21:13:30 +01:00
|
|
|
else {node.warn('Beacon already in use');}
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-03-14 21:24:22 +01:00
|
|
|
if (node.mode === "uid") {
|
|
|
|
if (!eddyBeacon) {
|
|
|
|
eddyBeacon = true;
|
|
|
|
try {
|
|
|
|
eddystoneBeacon.advertiseUid(node.namespace, node.instance, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:node.namespace});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-03-14 21:24:22 +01:00
|
|
|
node.error('Error setting beacon information', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {node.warn('Beacon already in use');}
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:57:53 +01:00
|
|
|
node.on('input', function(msg) {
|
2016-05-02 22:58:40 +02:00
|
|
|
if (msg.advertising === false) {
|
|
|
|
if (eddyBeacon) {
|
|
|
|
try {
|
|
|
|
eddystoneBeacon.stop();
|
|
|
|
node.status({fill:"red",shape:"dot",text:"Stopped"});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-05-02 22:58:40 +02:00
|
|
|
node.error('error shutting down beacon', e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg.advertising === true) {
|
|
|
|
if (node.mode === "url") {
|
|
|
|
try {
|
|
|
|
eddystoneBeacon.advertiseUrl(node.url, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:node.url});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-05-02 22:58:40 +02:00
|
|
|
node.error('Error setting beacon URL', e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node.mode === "uid") {
|
|
|
|
try {
|
|
|
|
eddystoneBeacon.advertiseUid(node.namespace, node.instance, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:node.namespace});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-05-02 22:58:40 +02:00
|
|
|
node.error('Error setting beacon information', e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// url mode
|
2016-03-14 21:24:22 +01:00
|
|
|
if (node.mode === "url") {
|
2017-01-29 18:45:44 +01:00
|
|
|
if (checkLength(msg.payload) <= 18) {
|
|
|
|
try {
|
|
|
|
node.url = msg.payload;
|
|
|
|
eddystoneBeacon.advertiseUrl(node.url, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:node.url});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
node.status({fill:"red",shape:"dot",text:"Error setting URL"});
|
|
|
|
node.error('error updating beacon URL', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.status({fill:"red",shape:"dot",text:"URL too long"});
|
|
|
|
}
|
2016-03-14 21:24:22 +01:00
|
|
|
}
|
|
|
|
// uid mode
|
|
|
|
else {
|
2017-01-29 18:45:44 +01:00
|
|
|
try {
|
|
|
|
node.namespace = msg.payload;
|
|
|
|
node.instance = msg.topic;
|
|
|
|
eddystoneBeacon.advertiseUid(node.namespace, node.instance, node.options);
|
|
|
|
node.status({fill:"green",shape:"dot",text:msg.payload});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
node.status({fill:"red",shape:"dot",text:"Error setting beacon information"});
|
|
|
|
node.error('Error setting beacon information', e);
|
|
|
|
}
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
|
|
|
});
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-03-02 22:57:53 +01:00
|
|
|
node.on('close', function(done) {
|
2016-03-04 14:10:00 +01:00
|
|
|
eddyBeacon = false;
|
2016-02-20 18:50:37 +01:00
|
|
|
try {
|
2016-03-04 14:10:00 +01:00
|
|
|
node.status({});
|
2016-02-20 18:50:37 +01:00
|
|
|
eddystoneBeacon.stop();
|
|
|
|
done();
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2016-03-03 10:18:57 +01:00
|
|
|
node.error('error shutting down beacon', e);
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
|
|
|
});
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("PhysicalWeb out", Beacon);
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-03-02 22:57:53 +01:00
|
|
|
function Scanner(n) {
|
2016-02-20 18:50:37 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
|
|
|
node.topic = n.topic;
|
2016-05-20 17:00:52 +02:00
|
|
|
node.duplicates = n.duplicates;
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
function onFound(beacon) {
|
2016-05-20 17:00:52 +02:00
|
|
|
node.send({topic: node.topic || 'found', payload: beacon});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onUpdated(beacon) {
|
|
|
|
node.send({topic: node.topic || 'updated', payload: beacon});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLost(beacon) {
|
|
|
|
node.send({topic: node.topic || 'lost', payload: beacon});
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
EddystoneBeaconScanner.on('found', onFound);
|
2016-05-20 17:00:52 +02:00
|
|
|
EddystoneBeaconScanner.on('updated', onUpdated);
|
|
|
|
EddystoneBeaconScanner.on('lost', onLost);
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-03-02 22:57:53 +01:00
|
|
|
node.on('close',function(done) {
|
2016-02-20 18:50:37 +01:00
|
|
|
EddystoneBeaconScanner.removeListener('found', onFound);
|
2016-05-20 17:00:52 +02:00
|
|
|
EddystoneBeaconScanner.removeListener('updated', onUpdated);
|
|
|
|
EddystoneBeaconScanner.removeListener('lost', onLost);
|
2016-02-20 18:50:37 +01:00
|
|
|
done();
|
|
|
|
});
|
2016-03-08 21:13:30 +01:00
|
|
|
|
|
|
|
var tout = setTimeout(function() {
|
2016-05-20 17:00:52 +02:00
|
|
|
EddystoneBeaconScanner.startScanning(node.duplicates);
|
|
|
|
}, 2000);
|
2016-03-08 21:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
node.on("close", function(done) {
|
|
|
|
if (tout) { clearTimeout(tout); }
|
|
|
|
EddystoneBeaconScanner.stopScanning();
|
|
|
|
done();
|
|
|
|
});
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("PhysicalWeb in", Scanner);
|
|
|
|
};
|