node-red-nodes/hardware/physical-web/physical-web.js

226 lines
7.3 KiB
JavaScript
Raw Normal View History

module.exports = function(RED) {
2016-02-20 18:50:37 +01:00
"use strict";
var eddystoneBeacon = require('eddystone-beacon');
var EddystoneBeaconScanner = require('@abandonware/eddystone-beacon-scanner');
var eddyBeacon = false;
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;
}
}
2017-01-29 18:45:44 +01:00
switch (true) {
case /.*\.info\/.*/.test(text): {
l -= 5;
break;
}
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;
}
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;
}
}
return l;
}
function Beacon(n) {
2016-02-20 18:50:37 +01:00
RED.nodes.createNode(this,n);
var node = this;
node.mode = n.mode;
2016-02-20 18:50:37 +01:00
node.power = n.power;
node.period = n.period;
node.count = n.count;
2016-02-20 18:50:37 +01:00
node.url = n.url;
node.namespace = n.namespace;
node.instance = n.instance;
2016-02-20 18:50:37 +01:00
node.options = {
txPowerLevel: node.power,
tlmPeriod: node.period,
tlmCount: node.count
};
if (node.mode === "url" && node.url) {
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) {
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
}
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) {
node.error('Error setting beacon information', e);
}
}
else {node.warn('Beacon already in use');}
}
node.on('input', function(msg) {
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) {
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) {
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) {
node.error('Error setting beacon information', e);
}
return;
}
}
// url mode
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"});
}
}
// 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
}
});
node.on('close', function(done) {
eddyBeacon = false;
2016-02-20 18:50:37 +01:00
try {
node.status({});
2016-02-20 18:50:37 +01:00
eddystoneBeacon.stop();
done();
2017-01-29 18:45:44 +01:00
}
catch(e) {
node.error('error shutting down beacon', e);
2016-02-20 18:50:37 +01:00
}
});
2016-02-20 18:50:37 +01:00
}
RED.nodes.registerType("PhysicalWeb out", Beacon);
function Scanner(n) {
2016-02-20 18:50:37 +01:00
RED.nodes.createNode(this,n);
var node = this;
node.topic = n.topic;
node.duplicates = n.duplicates;
2016-02-20 18:50:37 +01:00
function onFound(beacon) {
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
}
2016-02-20 18:50:37 +01:00
EddystoneBeaconScanner.on('found', onFound);
EddystoneBeaconScanner.on('updated', onUpdated);
EddystoneBeaconScanner.on('lost', onLost);
node.on('close',function(done) {
2016-02-20 18:50:37 +01:00
EddystoneBeaconScanner.removeListener('found', onFound);
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() {
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);
};