1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/hardware/physical-web/physical-web.js

136 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* 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');
var eddyBeacon = false;
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});
} 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});
} catch(e) {
node.error('Error setting beacon information', e);
}
}
else {node.warn('Beacon already in use');}
}
node.on('input', function(msg) {
if (node.mode === "url") {
try {
eddystoneBeacon.advertiseUrl(msg.payload, node.options);
node.status({fill:"green",shape:"dot",text:msg.payload});
} catch(e) {
node.status({fill:"red",shape:"dot",text:"URL too long"});
node.error('error updating beacon URL', e);
}
}
// uid mode
else {
try {
eddystoneBeacon.advertiseUid(msg.payload, msg.topic, 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();
} 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;
2016-02-20 18:50:37 +01:00
function onFound(beacon) {
node.send({
topic: node.topic,
payload: beacon
});
}
2016-02-20 18:50:37 +01:00
EddystoneBeaconScanner.on('found', onFound);
EddystoneBeaconScanner.on('updated', onFound);
node.on('close',function(done) {
2016-02-20 18:50:37 +01:00
EddystoneBeaconScanner.removeListener('found', onFound);
EddystoneBeaconScanner.removeListener('updated', onFound);
done();
});
2016-03-08 21:13:30 +01:00
var tout = setTimeout(function() {
EddystoneBeaconScanner.startScanning(true);
},2000);
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);
};