2015-11-23 16:24:26 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2015 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
node.power = n.power;
|
2016-03-04 14:10:00 +01:00
|
|
|
node.period = n.period * 10;
|
2016-02-20 18:50:37 +01:00
|
|
|
node.url = n.url;
|
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,
|
|
|
|
tlmCount: 2
|
2016-03-03 10:18:57 +01:00
|
|
|
};
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
if (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});
|
|
|
|
} catch(e) {
|
|
|
|
node.error('Error setting beacon URL', e);
|
|
|
|
}
|
2016-02-20 18:50:37 +01:00
|
|
|
}
|
2016-03-04 14:10:00 +01:00
|
|
|
else {node.warn('Beacon node already in use');}
|
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('input', function(msg) {
|
2016-02-20 18:50:37 +01:00
|
|
|
try {
|
|
|
|
eddystoneBeacon.advertiseUrl(msg.payload, node.options);
|
2016-03-04 14:10:00 +01:00
|
|
|
node.status({fill:"green",shape:"dot",text:node.url.toString()});
|
2016-03-02 22:57:53 +01:00
|
|
|
} catch(e) {
|
2016-03-04 14:10:00 +01:00
|
|
|
node.status({fill:"red",shape:"circle",text:"URL too long"});
|
2016-02-20 18:50:37 +01:00
|
|
|
node.error('error updating beacon URL', e);
|
|
|
|
}
|
|
|
|
});
|
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();
|
2016-03-02 22:57:53 +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;
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
function onFound(beacon) {
|
|
|
|
node.send({
|
|
|
|
topic: node.topic,
|
|
|
|
payload: beacon
|
|
|
|
});
|
|
|
|
}
|
2015-11-23 16:24:26 +01:00
|
|
|
|
2016-02-20 18:50:37 +01:00
|
|
|
EddystoneBeaconScanner.on('found', onFound);
|
|
|
|
EddystoneBeaconScanner.on('updated', onFound);
|
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);
|
|
|
|
EddystoneBeaconScanner.removeListener('updated', onFound);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("PhysicalWeb in", Scanner);
|
|
|
|
};
|