node-red-node-physical-web: Added uid mode

This commit is contained in:
Nathanaël Lécaudé 2016-03-14 16:24:22 -04:00
parent 7b72ecd99c
commit 71f9d3ad98
2 changed files with 64 additions and 7 deletions

View File

@ -78,12 +78,27 @@
<script type="text/x-red" data-template-name="PhysicalWeb out">
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-wrench"></i> <span>Mode</span></label>
<select id="node-input-mode" style='width:250px'>
<option value="url">url</option>
<option value="uid">uid</option>
</select>
</div>
<div class="form-row" id="node-url">
<label for="node-input-url"><i class="fa fa-link"></i> URL</label>
<input type="text" id="node-input-url" style="width:250px;" placeholder="http://...">
<!--<button type="button" id="node-input-url-shorten" disabled=true>Shorten</button> -->
<p style="margin-left:100px; width:70%">This URL needs to be shorter than 18 bytes
in length to meet Eddystone spec</p>
</div>
<div class="form-row" id="node-namespace">
<label for="node-input-namespace"><i class="fa fa-bluetooth"></i> Namespace</label>
<input type="text" id="node-input-namespace" style="width:250px;">
</div>
<div class="form-row" id="node-instance">
<label for="node-input-instance"><i class="fa fa-bluetooth"></i> Instance</label>
<input type="text" id="node-input-instance" style="width:250px;">
</div>
<div class="form-row">
<label for="node-input-period"><i class="fa fa-repeat"></i> Period (S)</label>
<input type="text" id="node-input-period" style="width:80px;" placeholder="Period">
@ -114,7 +129,10 @@
category: 'output',
defaults: {
name: {value:""},
mode: {value:"url"},
url: {value:"",validate:function(v) {return v.length<19; }},
namespace: {value:""},
instance: {value:""},
power: {value:"-21",validate:RED.validators.number()},
period: {value:"1",validate:RED.validators.number()}
},
@ -131,6 +149,17 @@
oneditprepare: function() {
$( "#node-input-period" ).spinner({min:1});
$( "#node-input-power" ).spinner({min:-30,max:100});
$("#node-input-mode").on("change",function() {
if ($("#node-input-mode").val() === "uid") {
$("#node-url").hide();
$("#node-namespace").show();
$("#node-instance").show();
} else {
$("#node-url").show();
$("#node-namespace").hide();
$("#node-instance").hide();
}
});
}
});
</script>

View File

@ -23,9 +23,12 @@ module.exports = function(RED) {
function Beacon(n) {
RED.nodes.createNode(this,n);
var node = this;
node.mode = n.mode;
node.power = n.power;
node.period = n.period * 10;
node.url = n.url;
node.namespace = n.namespace;
node.instance = n.instance;
node.options = {
txPowerLevel: node.power,
@ -33,7 +36,7 @@ module.exports = function(RED) {
tlmCount: 2
};
if (node.url) {
if (node.mode === "url" && node.url) {
if (!eddyBeacon) {
eddyBeacon = true;
try {
@ -46,13 +49,38 @@ module.exports = function(RED) {
else {node.warn('Beacon already in use');}
}
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) {
try {
eddystoneBeacon.advertiseUrl(msg.payload, node.options);
node.status({fill:"green",shape:"dot",text:node.url.toString()});
} catch(e) {
node.status({fill:"red",shape:"circle",text:"URL too long"});
node.error('error updating beacon URL', e);
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);
}
}
});