simplified listen node

This commit is contained in:
Niko Vujić
2025-12-31 10:46:52 +01:00
parent 84797d50c4
commit 89e42295c5
2 changed files with 121 additions and 174 deletions

View File

@@ -5,7 +5,10 @@ RED.nodes.registerType('xilica-listen', {
defaults: { defaults: {
name: { value: "" }, name: { value: "" },
connection: { value: "", type: "xilica-connection" }, connection: { value: "", type: "xilica-connection" },
rules: { value: [] } mode: { value: "list" },
targets: { value: "" },
startIndex: { value: 1 },
endIndex: { value: 1 }
}, },
inputs: 1, inputs: 1,
outputs: 1, outputs: 1,
@@ -15,110 +18,6 @@ RED.nodes.registerType('xilica-listen', {
return this.name; return this.name;
} }
return "xilica listen"; return "xilica listen";
},
oneditprepare: function () {
var node = this;
var rules = node.rules || [];
if (typeof rules === "string") {
try {
rules = JSON.parse(rules);
} catch (e) {
rules = [];
}
}
if (!Array.isArray(rules)) {
rules = [];
}
$("#node-input-rules").editableList({
addItem: function (container, index, rule) {
rule = rule || {};
var row = $('<div/>').appendTo(container);
var typeField = $('<select/>', {
class: "node-input-rule-type",
style: "width: 120px; margin-right: 5px;"
}).appendTo(row);
$('<option/>', { value: "eq", text: "==" }).appendTo(typeField);
$('<option/>', { value: "idx", text: "Indexed" }).appendTo(typeField);
var valueField = $('<input/>', {
class: "node-input-rule-value",
type: "text",
style: "width: 180px; margin-right: 5px;"
}).appendTo(row);
var fromField = $('<input/>', {
class: "node-input-rule-from",
type: "number",
style: "width: 80px; margin-right: 5px;",
placeholder: "From"
}).appendTo(row);
var toField = $('<input/>', {
class: "node-input-rule-to",
type: "number",
style: "width: 80px;",
placeholder: "To"
}).appendTo(row);
function updateVisibility() {
var t = typeField.val();
if (t === "idx") {
fromField.show();
toField.show();
} else {
fromField.hide();
toField.hide();
}
}
typeField.on("change", updateVisibility);
typeField.val(rule.t || "eq");
valueField.val(rule.v || "");
if (rule.from !== undefined) {
fromField.val(rule.from);
}
if (rule.to !== undefined) {
toField.val(rule.to);
}
updateVisibility();
},
removable: true,
sortable: true
});
(rules || []).forEach(function (r) {
$("#node-input-rules").editableList('addItem', r);
});
},
oneditsave: function () {
var rules = [];
$("#node-input-rules").editableList('items').each(function () {
var row = $(this);
var type = $(".node-input-rule-type", row).val() || "eq";
var value = $(".node-input-rule-value", row).val() || "";
var from = $(".node-input-rule-from", row).val();
var to = $(".node-input-rule-to", row).val();
var rule = { t: type, v: value };
if (type === "idx") {
if (from !== "") {
rule.from = parseInt(from, 10);
}
if (to !== "") {
rule.to = parseInt(to, 10);
}
}
rules.push(rule);
});
this.rules = rules;
} }
}); });
</script> </script>
@@ -133,20 +32,39 @@ RED.nodes.registerType('xilica-listen', {
<input type="text" id="node-input-connection"> <input type="text" id="node-input-connection">
</div> </div>
<div class="form-row"> <div class="form-row">
<label>Rules</label> <label for="node-input-mode">Mode</label>
<ol id="node-input-rules"></ol> <select id="node-input-mode">
<option value="list">Explicit list</option>
<option value="startsWith">Starts with</option>
<option value="endsWith">Ends with</option>
<option value="startsAndEnds">Starts with and ends with</option>
</select>
</div>
<div class="form-row">
<label for="node-input-targets">Targets</label>
<textarea id="node-input-targets" rows="5" style="width: 100%;"></textarea>
</div>
<div class="form-row">
<label for="node-input-startIndex">Start index</label>
<input type="number" id="node-input-startIndex" min="0">
</div>
<div class="form-row">
<label for="node-input-endIndex">End index</label>
<input type="number" id="node-input-endIndex" min="0">
</div> </div>
</script> </script>
<script type="text/x-red" data-help-name="xilica-listen"> <script type="text/x-red" data-help-name="xilica-listen">
<p>Parses Xilica Solaro notification/value lines and filters them by control name.</p> <p>Parses Xilica Solaro notification/value lines and filters them by control name.</p>
<p>Wire the output of a <code>tcp in</code> or <code>udp in</code> node (connected to the Solaro subscription stream) into this node.</p> <p>Wire the output of a <code>tcp in</code> or <code>udp in</code> node (connected to the Solaro subscription stream) into this node. The Connection field indicates which Xilica device this listener is for.</p>
<p><strong>Rules</strong> define which controls to pass through, similar to the Switch node:</p> <p><strong>Mode</strong> and <strong>Targets</strong> define which controls to pass through:</p>
<ul> <ul>
<li><strong>Type ==</strong>: the Value field is a full control name (e.g. <code>MASTER_GAIN</code>).</li> <li><strong>Explicit list</strong>: each non-empty line in Targets is used as a full control name (e.g. <code>MASTER_GAIN</code>).</li>
<li><strong>Type Indexed</strong>: the Value field is a base string (e.g. <code>CH</code>), and From/To define a numeric range (e.g. 148 <code>CH1</code><code>CH48</code>).</li> <li><strong>Starts with</strong>: each line in Targets is treated as a prefix (e.g. <code>CH</code>), and Start/End index define the numeric range (e.g. 148 <code>CH1</code><code>CH48</code>).</li>
<li><strong>Ends with</strong>: each line in Targets is treated as a suffix (e.g. <code>_OUT</code>), and Start/End index define the numeric range (e.g. 14 <code>1_OUT</code><code>4_OUT</code>).</li>
<li><strong>Starts with and ends with</strong>: each line in Targets is interpreted as <code>prefix|suffix</code> (for example <code>CH|OUT</code>), and Start/End index define the numeric range (e.g. 428 <code>CH4OUT</code><code>CH28OUT</code>).</li>
</ul> </ul>
<p>If no rules are defined, all notification/value lines are passed through.</p> <p>If no targets are defined, all notification/value lines are passed through.</p>
<p>For each matching line, the node outputs a message with:</p> <p>For each matching line, the node outputs a message with:</p>
<ul> <ul>
<li><code>msg.payload</code> = <code>{ type, control, data }</code></li> <li><code>msg.payload</code> = <code>{ type, control, data }</code></li>

View File

@@ -1,68 +1,87 @@
module.exports = function (RED) { module.exports = function (RED) {
function buildControlNamesFromRules(rules) { function buildNamesFromList(targets) {
return targets
.split(/\r?\n/)
.map((l) => l.trim())
.filter((l) => l.length > 0);
}
function buildNamesStartsWith(targets, startIndex, endIndex) {
const prefixes = buildNamesFromList(targets);
const names = []; const names = [];
if (!Array.isArray(rules)) { if (startIndex > endIndex) {
return names; const tmp = startIndex;
} startIndex = endIndex;
endIndex = tmp;
rules.forEach((rule) => {
if (!rule) {
return;
}
const type = (rule.t || rule.type || "eq").toString();
if (type === "idx") {
const base = (rule.v || rule.base || "").trim();
if (!base) {
return;
}
let from = parseInt(rule.from, 10);
let to = parseInt(rule.to, 10);
if (Number.isNaN(from) && Number.isNaN(to)) {
return;
}
if (Number.isNaN(from)) {
from = to;
}
if (Number.isNaN(to)) {
to = from;
}
if (from > to) {
const tmp = from;
from = to;
to = tmp;
}
for (let i = from; i <= to; i += 1) {
names.push(base + i);
}
} else {
const name = (rule.v || rule.value || "").trim();
if (name) {
names.push(name);
} }
prefixes.forEach((prefix) => {
for (let i = startIndex; i <= endIndex; i += 1) {
names.push(prefix + i);
} }
}); });
return names; return names;
} }
function buildNamesEndsWith(targets, startIndex, endIndex) {
const suffixes = buildNamesFromList(targets);
const names = [];
if (startIndex > endIndex) {
const tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
suffixes.forEach((suffix) => {
for (let i = startIndex; i <= endIndex; i += 1) {
names.push(String(i) + suffix);
}
});
return names;
}
function buildNamesStartsAndEnds(targets, startIndex, endIndex) {
const patterns = buildNamesFromList(targets);
const names = [];
if (startIndex > endIndex) {
const tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
patterns.forEach((pattern) => {
const parts = pattern.split("|");
const prefix = (parts[0] || "").trim();
const suffix = (parts[1] || "").trim();
if (!prefix && !suffix) {
return;
}
for (let i = startIndex; i <= endIndex; i += 1) {
names.push(prefix + i + suffix);
}
});
return names;
}
function buildControlNames(mode, targets, startIndex, endIndex) {
if (mode === "startsWith") {
return buildNamesStartsWith(targets, startIndex, endIndex);
}
if (mode === "endsWith") {
return buildNamesEndsWith(targets, startIndex, endIndex);
}
if (mode === "startsAndEnds") {
return buildNamesStartsAndEnds(targets, startIndex, endIndex);
}
return buildNamesFromList(targets);
}
function XilicaListen(config) { function XilicaListen(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
const node = this; const node = this;
let initialRules = config.rules; node.connectionConfig = RED.nodes.getNode(config.connection);
if (typeof initialRules === "string") { node.mode = config.mode || "list";
try { node.targets = config.targets || "";
initialRules = JSON.parse(initialRules); node.startIndex = parseInt(config.startIndex, 10) || 1;
} catch (e) { node.endIndex = parseInt(config.endIndex, 10) || node.startIndex;
initialRules = [];
}
}
node.rules = Array.isArray(initialRules) ? initialRules : [];
node.on("input", (msg, send, done) => { node.on("input", (msg, send, done) => {
send = send =
@@ -84,18 +103,28 @@ module.exports = function (RED) {
return; return;
} }
let rules = msg.rules || node.rules; const mode = (msg.mode || node.mode || "list").toString();
if (typeof rules === "string") { const targets =
try { typeof msg.targets === "string" && msg.targets.trim().length
rules = JSON.parse(rules); ? msg.targets
} catch (e) { : node.targets;
rules = [];
let startIndex = node.startIndex;
let endIndex = node.endIndex;
if (Object.prototype.hasOwnProperty.call(msg, "startIndex")) {
const v = parseInt(msg.startIndex, 10);
if (!Number.isNaN(v)) {
startIndex = v;
} }
} }
if (!Array.isArray(rules)) { if (Object.prototype.hasOwnProperty.call(msg, "endIndex")) {
rules = []; const v = parseInt(msg.endIndex, 10);
if (!Number.isNaN(v)) {
endIndex = v;
} }
const names = buildControlNamesFromRules(rules); }
const names = buildControlNames(mode, targets || "", startIndex, endIndex);
const matchAll = names.length === 0; const matchAll = names.length === 0;
const allowed = new Set(names); const allowed = new Set(names);