node-red-contrib-mysensors
Version:
Mysensors related nodes, for decoding / encoding mysensors serial protocol and MQTT topic, and wrapping arbitrary messages into mysensors compatible messages
241 lines (228 loc) • 9.7 kB
HTML
<script type="text/html" data-template-name="mysencap">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-msgtype"><i class="fa fa-tag"></i> Message Type</label>
<select id="node-input-msgtype">
<option value="1">Set</option>
<option value="2">Request</option>
<option value="3">Internal</option>
</select>
</div>
<div class="form-row">
<label for="node-input-nodeid"><i class="fa fa-tag"></i> Node ID</label>
<input type="text" id="node-input-nodeid" placeholder="NodeId">
</div>
<div class="form-row node-childid">
<label for="node-input-childid"><i class="fa fa-tag"></i> Child ID</label>
<input type="text" id="node-input-childid" placeholder="Child id">
</div>
<div class="form-row" id="node-input-row-subtype">
<label for="node-input-subtype"><i class="fa fa-tag"></i> Child type hide</label>
<input type="text" id="node-input-subtype">
</div>
<div class="form-row" id="node-input-row-subtype-select">
<label for="node-input-subtype-select"><i class="fa fa-tag"></i> Child type</label>
<select id="node-input-subtype-select">
<select>
</div>
<div class="form-row" id="node-input-row-internal">
<label for="node-input-internal"><i class="fa fa-tag"></i> Internal msg hidden</label>
<input type="text" id="node-input-internal">
</div>
<div class="form-row" id="node-input-row-internal-select">
<label for="node-input-internal-select"><i class="fa fa-tag"></i> Internal msg</label>
<select id="node-input-internal-select">
</select>
</div>
<div class="form-row node-ack">
<label for="node-input-ack"><i class="fa fa-tag"></i> Ack</label>
<input type="checkbox" id="node-input-ack">
</div>
<div class="form-row node-presentation">
<label for="node-input-presentation"><i class="fa fa-tag"></i> Presentation</label>
<input type="checkbox" id="node-input-presentation">
</div>
<div class="form-row node-presentation-opts">
<label for="node-input-presentationtext"><i class="fa fa-tag"></i> Text</label>
<input type="text" id="node-input-presentationtext">
</div>
<div class="form-row" id="node-input-row-presentation">
<label for="node-input-presentationtype"><i class="fa fa-tag"></i> Type hide</label>
<input type="text" id="node-input-presentationtype">
</div>
<div class="form-row node-presentation-opts">
<label for="node-input-presentationtype-select"><i class="fa fa-tag"></i> Type</label>
<select id="node-input-presentationtype-select">
</select>
</div>
<div class="form-row node-presentation-opts">
<label for="node-input-fullpresentation"><i class="fa fa-tag"></i> Present firmware</label>
<input type="checkbox" id="node-input-fullpresentation">
</div>
<div class="form-row node-presentation-full-opts">
<label for="node-input-firmwarename"><i class="fa fa-tag"></i> Firmware</label>
<input type="text" id="node-input-firmwarename">
</div>
<div class="form-row node-presentation-full-opts">
<label for="node-input-firmwareversion"><i class="fa fa-tag"></i> Version</label>
<input type="text" id="node-input-firmwareversion">
</div>
</script>
<script type="text/html" data-help-name="mysencap">
<p>
Adds extra propperties to the message object, so it conforms to mysensor data. Send the resulting message through mysencode to convert
into mysensors serial / mqtt topic format, that can be sent to either a mysensors node, or sent to another controller that understands
mysensors serial or mqtt topic formats
</p>
<h3>Input</h3>
<dl clas="message-properties">
</dl>
<h3>Output</h3>
<dl class="message-properties">
All parts, except payload, is set in the node configuration page
<dt>nodeId <span class="property-type">number</span></dt>
<dd></dd>
<dt>childSensorId <span class="property-type">number</span></dt>
<dd></dd>
<dt>messageType <span class="property-type">number</span></dt>
<dd></dd>
<dt>ack <span class="property-type">0 or 1</span></dt>
<dd></dd>
<dt>subType <span class="property-type">number</span></dt>
<dd></dd>
<dt>payload <span class="property-type">string</span></dt>
<dd></dd>
</dl>
<h3>Details</h3>
<p>
if "presentation" is checked, a presentation message will be sent at startup of node-red (or a new flow is deployed).
This can be used to generate presentation of dummy nodes, that you create.
</p>
<h3>References</h3>
<ul>
<li><a href="https://www.mysensors.org/download/serial_api_20">MySensors Serial API</a></li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('mysencap',{
category: 'mysensors',
color: '#a6bbcf',
defaults: {
name: {value:""},
nodeid: {value:0, required:true},
childid: {value:0, required:true},
subtype: {value:0},
internal: {value:0},
ack : {value:false, required:true},
msgtype: {value:1, required:true},
presentation: {value : false, required:false},
presentationtype: {value:0},
presentationtext: {value:""},
fullpresentation: {value:false, required:false},
firmwarename : {value:""},
firmwareversion : {value:""}
},
inputs:1,
outputs:1,
icon: "envelope.png",
label: function() {
return this.name|| "encapsulate: " + this.nodeid + ":" + this.childid + ":" +
this.msgtype + ":0:" + this.subtype+"<payload>";
},
oneditprepare: function() {
$('#node-input-row-internal').hide();
$('#node-input-row-subtype').hide();
$('#node-input-row-presentation').hide();
loadJsonTypes('subtype', 'node-input-subtype-select', 'node-input-subtype');
loadJsonTypes('presentation','node-input-presentationtype-select','node-input-presentationtype');
loadJsonTypes('internal','node-input-internal-select','node-input-internal');
if (!$('#node-input-presentation').is(':checked')) {
$('.node-presentation-opts').hide();
}
if (!$('#node-input-fullpresentation').is(':checked')) {
$('.node-presentation-full-opts').hide();
}
if ($('#node-input-msgtype option:selected').val() == "3") {
showInternal();
} else {
showSetReq();
}
$("#node-input-msgtype").change(function() {
var id = $("#node-input-msgtype option:selected").val();
if (id == "3") {
showInternal();
} else {
showSetReq();
}
});
$('#node-input-presentation').click(function() {
if (this.checked) {
$('.node-presentation-opts').show();
} else {
$('.node-presentation-opts').hide();
}
if (!$('#node-input-fullpresentation').is(':checked')) $('.node-presentation-full-opts').hide();
else $('.node-presentation-full-opts').show();
});
$('#node-input-fullpresentation').click(function() {
if (this.checked) {
$('.node-presentation-full-opts').show();
} else {
$('.node-presentation-full-opts').hide();
}
});
},
oneditsave : function() {
$('#node-input-internal').val($('#node-input-internal-select').val());
$('#node-input-subtype').val($('#node-input-subtype-select').val());
$('#node-input-presentationtype').val($('#node-input-presentationtype-select').val());
}
});
function showSetReq() {
$("#node-input-row-subtype-select").show();
$("#node-input-row-internal-select").hide();
$(".node-childid").show();
$(".node-presentation").show();
}
function showInternal() {
$("#node-input-row-subtype-select").hide();
$("#node-input-row-internal-select").show();
$(".node-childid").hide();
$(".node-presentation").hide();
}
function sortOptions(selectTag) {
var selected = $('#'+selectTag).val();
var options = $('#'+selectTag+' option');
var arr = options.map(function(_, o) { return { t:
$(o).text(), v: o.value }; }).get();
arr.sort((o1, o2) => {
if (o1.t > o2.t) {
return 1;
}
if (o1.t < o2.t) {
return -1;
}
return 0;
});
options.each(function(i, o) {
o.value = arr[i].v;
$(o).text(arr[i].t);
});
$('#'+selectTag).val(selected);
}
function loadJsonTypes(type, selectId, selection) {
$.getJSON('mysensordefs/'+type, function(data) {
var html = '';
data = JSON.parse(data);
Object.keys(data).forEach((item) => {
html = html + '<option value='+data[item]+'>'+item+'</option>';
});
$('#'+selectId).html(html);
$('#'+selectId).val($('#'+selection).val());
});
sortOptions(selectId);
}
</script>