node-red-contrib-unifi-ultimate 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/nodes/unifi-access-config.js +20 -4
- package/nodes/unifi-access-device.html +20 -4
- package/nodes/unifi-network-config.js +156 -8
- package/nodes/unifi-network-device.html +34 -8
- package/nodes/unifi-network-presence.html +159 -34
- package/nodes/unifi-network-presence.js +26 -0
- package/nodes/unifi-network-restart.html +43 -11
- package/nodes/unifi-protect-device.html +20 -4
- package/nodes/utils/unifi-access-device-registry.js +100 -4
- package/nodes/utils/unifi-network-presence-utils.js +117 -0
- package/nodes/utils/unifi-protect-device-registry.js +225 -0
- package/package.json +2 -2
|
@@ -17,6 +17,18 @@
|
|
|
17
17
|
return (self && self.watchBy) ? String(self.watchBy).trim() : "presence";
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function currentPresenceMatchByValue(self) {
|
|
21
|
+
try {
|
|
22
|
+
const live = $("#node-input-matchBy").val();
|
|
23
|
+
if (live) {
|
|
24
|
+
return String(live).trim();
|
|
25
|
+
}
|
|
26
|
+
} catch (error) {
|
|
27
|
+
// jQuery/DOM not available in this context.
|
|
28
|
+
}
|
|
29
|
+
return (self && self.matchBy) ? String(self.matchBy).trim() : "id";
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
RED.nodes.registerType("unifi-network-presence", {
|
|
21
33
|
category: "Unifi Ultimate",
|
|
22
34
|
color: "#CFE8FB",
|
|
@@ -24,6 +36,7 @@
|
|
|
24
36
|
name: { value: "" },
|
|
25
37
|
server: { value: "", type: "unifi-network-config", required: true },
|
|
26
38
|
watchBy: { value: "presence", required: true },
|
|
39
|
+
matchBy: { value: "id", required: true },
|
|
27
40
|
clientId: {
|
|
28
41
|
value: "",
|
|
29
42
|
validate: function (v) {
|
|
@@ -31,6 +44,16 @@
|
|
|
31
44
|
return mode === "network" ? true : String(v || "").trim() !== "";
|
|
32
45
|
}
|
|
33
46
|
},
|
|
47
|
+
clientName: {
|
|
48
|
+
value: "",
|
|
49
|
+
validate: function (v) {
|
|
50
|
+
const mode = currentWatchByValue(this);
|
|
51
|
+
const matchBy = currentPresenceMatchByValue(this);
|
|
52
|
+
return mode !== "presence" || matchBy !== "name"
|
|
53
|
+
? true
|
|
54
|
+
: String(v || this.deviceName || "").trim() !== "";
|
|
55
|
+
}
|
|
56
|
+
},
|
|
34
57
|
deviceName: { value: "" },
|
|
35
58
|
pollInterval: { value: 10, required: true, validate: RED.validators.number() },
|
|
36
59
|
onlineHysteresis: { value: 15, required: true, validate: RED.validators.number() },
|
|
@@ -54,12 +77,12 @@
|
|
|
54
77
|
paletteLabel: "Presence Detection",
|
|
55
78
|
outputLabels: ["result", "error"],
|
|
56
79
|
label: function () {
|
|
57
|
-
if (this.name) {
|
|
58
|
-
return this.name;
|
|
59
|
-
}
|
|
60
80
|
if (this.watchBy === "network" && this.networkName) {
|
|
61
81
|
return this.networkName;
|
|
62
82
|
}
|
|
83
|
+
if (this.watchBy !== "network" && (this.clientName || this.deviceName)) {
|
|
84
|
+
return this.clientName || this.deviceName;
|
|
85
|
+
}
|
|
63
86
|
return "Presence Detection";
|
|
64
87
|
},
|
|
65
88
|
oneditsave: function () {
|
|
@@ -70,28 +93,42 @@
|
|
|
70
93
|
const $selected = $("#node-input-networkId option:selected");
|
|
71
94
|
this.networkName = String($selected.attr("data-network-name") || "").trim();
|
|
72
95
|
$("#node-input-networkName").val(this.networkName);
|
|
96
|
+
} else {
|
|
97
|
+
const selectedClientName = String($("#node-input-clientId option:selected").attr("data-client-name") || "").trim();
|
|
98
|
+
const configuredClientName = String($("#node-input-clientName").val() || "").trim();
|
|
99
|
+
if (selectedClientName) {
|
|
100
|
+
this.deviceName = selectedClientName;
|
|
101
|
+
$("#node-input-deviceName").val(selectedClientName);
|
|
102
|
+
}
|
|
103
|
+
this.clientName = configuredClientName || selectedClientName;
|
|
104
|
+
$("#node-input-clientName").val(this.clientName);
|
|
73
105
|
}
|
|
74
106
|
},
|
|
75
107
|
oneditprepare: function () {
|
|
76
108
|
const node = this;
|
|
77
|
-
const $name = $("#node-input-name");
|
|
78
109
|
const $server = $("#node-input-server");
|
|
79
110
|
const $watchBy = $("#node-input-watchBy");
|
|
111
|
+
const $matchBy = $("#node-input-matchBy");
|
|
80
112
|
const $clientId = $("#node-input-clientId");
|
|
113
|
+
const $clientName = $("#node-input-clientName");
|
|
81
114
|
const $deviceName = $("#node-input-deviceName");
|
|
82
115
|
const $refresh = $("#node-input-refreshClients");
|
|
83
116
|
const $clientRow = $clientId.closest(".form-row");
|
|
117
|
+
const $clientNameRow = $("#node-row-clientName");
|
|
84
118
|
const $advancedRow = $("#node-row-advanced");
|
|
85
119
|
// Network-mode controls.
|
|
86
120
|
const $networkId = $("#node-input-networkId");
|
|
87
121
|
const $refreshNetworks = $("#node-input-refreshNetworks");
|
|
88
122
|
const $watchByRow = $("#node-row-watchBy");
|
|
123
|
+
const $matchByRow = $("#node-row-matchBy");
|
|
89
124
|
const $networkRow = $("#node-row-networkId");
|
|
90
125
|
const $networkStatus = $("#unifi-presence-network-status");
|
|
91
126
|
let suspendChangeHandling = false;
|
|
92
127
|
let loadedNetworksServerId = "";
|
|
93
128
|
|
|
94
129
|
$watchBy.val(this.watchBy || "presence");
|
|
130
|
+
$matchBy.val(this.matchBy || "id");
|
|
131
|
+
$clientName.val(this.clientName || this.deviceName || "");
|
|
95
132
|
|
|
96
133
|
function currentMode() {
|
|
97
134
|
return String($watchBy.val() || "presence").trim();
|
|
@@ -108,7 +145,9 @@
|
|
|
108
145
|
const hasTarget = isNetwork ? hasSelection($networkId) : hasSelection($clientId);
|
|
109
146
|
|
|
110
147
|
$watchByRow.toggle(hasServer);
|
|
148
|
+
$matchByRow.toggle(hasServer && !isNetwork);
|
|
111
149
|
$clientRow.toggle(hasServer && !isNetwork);
|
|
150
|
+
$clientNameRow.toggle(hasServer && !isNetwork && hasSelection($clientId));
|
|
112
151
|
$networkRow.toggle(hasServer && isNetwork);
|
|
113
152
|
$advancedRow.toggle(hasServer && hasTarget);
|
|
114
153
|
// Tuning rows depend on the active mode.
|
|
@@ -137,7 +176,48 @@
|
|
|
137
176
|
$("<option></option>").val(id).text(name + suffix).attr("data-network-name", name)
|
|
138
177
|
);
|
|
139
178
|
});
|
|
140
|
-
|
|
179
|
+
const hasCurrentValue = currentValue && $networkId.find("option").filter(function () {
|
|
180
|
+
return String($(this).val() || "") === currentValue;
|
|
181
|
+
}).length > 0;
|
|
182
|
+
if (hasCurrentValue) {
|
|
183
|
+
$networkId.val(currentValue);
|
|
184
|
+
} else if (currentValue) {
|
|
185
|
+
const savedName = String(node.networkName || currentValue).trim();
|
|
186
|
+
$networkId.append(
|
|
187
|
+
$("<option></option>")
|
|
188
|
+
.val(currentValue)
|
|
189
|
+
.text(savedName + " (saved; currently unavailable)")
|
|
190
|
+
.attr("data-network-name", savedName)
|
|
191
|
+
.attr("data-unavailable", "true")
|
|
192
|
+
);
|
|
193
|
+
$networkId.val(currentValue);
|
|
194
|
+
} else {
|
|
195
|
+
$networkId.val("");
|
|
196
|
+
}
|
|
197
|
+
} finally {
|
|
198
|
+
suspendChangeHandling = false;
|
|
199
|
+
}
|
|
200
|
+
syncProgressiveRows();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function renderNetworksLoading() {
|
|
204
|
+
const savedValue = String($networkId.val() || node.networkId || "").trim();
|
|
205
|
+
const savedName = String(node.networkName || savedValue).trim();
|
|
206
|
+
suspendChangeHandling = true;
|
|
207
|
+
try {
|
|
208
|
+
$networkId.empty();
|
|
209
|
+
if (savedValue) {
|
|
210
|
+
$networkId.append(
|
|
211
|
+
$("<option></option>")
|
|
212
|
+
.val(savedValue)
|
|
213
|
+
.text(savedName + " (loading...)")
|
|
214
|
+
.attr("data-network-name", savedName)
|
|
215
|
+
);
|
|
216
|
+
$networkId.val(savedValue);
|
|
217
|
+
} else {
|
|
218
|
+
$networkId.append($('<option></option>').val("").text("Loading networks..."));
|
|
219
|
+
$networkId.val("");
|
|
220
|
+
}
|
|
141
221
|
} finally {
|
|
142
222
|
suspendChangeHandling = false;
|
|
143
223
|
}
|
|
@@ -156,6 +236,7 @@
|
|
|
156
236
|
}
|
|
157
237
|
$networkStatus.text("Loading networks...");
|
|
158
238
|
$refreshNetworks.find("i").addClass("fa-spin");
|
|
239
|
+
renderNetworksLoading();
|
|
159
240
|
loadedNetworksServerId = serverId;
|
|
160
241
|
return requestJson("unifiNetwork/networks?serverId=" + encodeURIComponent(serverId))
|
|
161
242
|
.then(function (networks) {
|
|
@@ -446,7 +527,9 @@
|
|
|
446
527
|
|
|
447
528
|
if (serverId && serverId !== "_ADD_") {
|
|
448
529
|
summary = "Choose the client to monitor.";
|
|
449
|
-
detail =
|
|
530
|
+
detail = $matchBy.val() === "name"
|
|
531
|
+
? "The active client is matched by alias or hostname on every poll."
|
|
532
|
+
: "The selected UniFi client ID is checked on every poll.";
|
|
450
533
|
}
|
|
451
534
|
|
|
452
535
|
if (clientName) {
|
|
@@ -456,22 +539,16 @@
|
|
|
456
539
|
syncProgressiveRows();
|
|
457
540
|
}
|
|
458
541
|
|
|
459
|
-
function
|
|
460
|
-
const clientName = getSelectedClientName();
|
|
461
|
-
if (!clientName) {
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
$name.val(clientName);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
function rememberSelectedClientName() {
|
|
542
|
+
function rememberSelectedClientName(forceTargetName) {
|
|
469
543
|
const clientName = getSelectedClientName();
|
|
470
544
|
if (!clientName) {
|
|
471
545
|
return;
|
|
472
546
|
}
|
|
473
547
|
|
|
474
548
|
$deviceName.val(clientName);
|
|
549
|
+
if (forceTargetName || !String($clientName.val() || "").trim()) {
|
|
550
|
+
$clientName.val(clientName);
|
|
551
|
+
}
|
|
475
552
|
}
|
|
476
553
|
|
|
477
554
|
function renderClients(items) {
|
|
@@ -500,7 +577,28 @@
|
|
|
500
577
|
$clientId.append($option);
|
|
501
578
|
});
|
|
502
579
|
|
|
503
|
-
|
|
580
|
+
const hasCurrentValue = currentValue && $clientId.find("option").filter(function () {
|
|
581
|
+
return String($(this).val() || "") === currentValue;
|
|
582
|
+
}).length > 0;
|
|
583
|
+
if (hasCurrentValue) {
|
|
584
|
+
$clientId.val(currentValue);
|
|
585
|
+
const loadedClientName = getSelectedClientName();
|
|
586
|
+
if (loadedClientName) {
|
|
587
|
+
$clientName.val(loadedClientName);
|
|
588
|
+
$deviceName.val(loadedClientName);
|
|
589
|
+
}
|
|
590
|
+
} else if (currentValue) {
|
|
591
|
+
// Never discard a saved target just because the controller
|
|
592
|
+
// omitted it from this refresh. Keeping a synthetic option
|
|
593
|
+
// prevents opening/saving the editor from erasing clientId.
|
|
594
|
+
const savedName = String($clientName.val() || node.clientName || node.deviceName || currentValue).trim();
|
|
595
|
+
$clientId.append(
|
|
596
|
+
$("<option></option>")
|
|
597
|
+
.val(currentValue)
|
|
598
|
+
.text(savedName + " (saved; currently unavailable)")
|
|
599
|
+
.attr("data-client-name", savedName)
|
|
600
|
+
.attr("data-unavailable", "true")
|
|
601
|
+
);
|
|
504
602
|
$clientId.val(currentValue);
|
|
505
603
|
} else {
|
|
506
604
|
$clientId.val("");
|
|
@@ -510,20 +608,32 @@
|
|
|
510
608
|
}
|
|
511
609
|
|
|
512
610
|
makeSelectSearchable($clientId, "Search clients");
|
|
513
|
-
rememberSelectedClientName();
|
|
611
|
+
rememberSelectedClientName(false);
|
|
514
612
|
refreshSummary();
|
|
515
613
|
}
|
|
516
614
|
|
|
517
615
|
function renderClientsLoading() {
|
|
616
|
+
const savedValue = String($clientId.val() || node.clientId || "").trim();
|
|
617
|
+
const savedName = String($clientName.val() || node.clientName || node.deviceName || savedValue).trim();
|
|
518
618
|
suspendChangeHandling = true;
|
|
519
619
|
try {
|
|
520
620
|
$clientId.empty();
|
|
521
|
-
|
|
522
|
-
$(
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
621
|
+
if (savedValue) {
|
|
622
|
+
$clientId.append(
|
|
623
|
+
$("<option></option>")
|
|
624
|
+
.val(savedValue)
|
|
625
|
+
.text(savedName + " (loading...)")
|
|
626
|
+
.attr("data-client-name", savedName)
|
|
627
|
+
);
|
|
628
|
+
$clientId.val(savedValue);
|
|
629
|
+
} else {
|
|
630
|
+
$clientId.append(
|
|
631
|
+
$("<option></option>")
|
|
632
|
+
.val("")
|
|
633
|
+
.text("Loading clients, please wait...")
|
|
634
|
+
);
|
|
635
|
+
$clientId.val("");
|
|
636
|
+
}
|
|
527
637
|
} finally {
|
|
528
638
|
suspendChangeHandling = false;
|
|
529
639
|
}
|
|
@@ -580,12 +690,8 @@
|
|
|
580
690
|
if (suspendChangeHandling) {
|
|
581
691
|
return;
|
|
582
692
|
}
|
|
583
|
-
rememberSelectedClientName();
|
|
584
|
-
const isUserSelection = $clientId.data("unifi-user-selection") === true;
|
|
693
|
+
rememberSelectedClientName(true);
|
|
585
694
|
$clientId.removeData("unifi-user-selection");
|
|
586
|
-
if (isUserSelection) {
|
|
587
|
-
syncNameFromSelection();
|
|
588
|
-
}
|
|
589
695
|
refreshSummary();
|
|
590
696
|
});
|
|
591
697
|
|
|
@@ -603,6 +709,14 @@
|
|
|
603
709
|
}
|
|
604
710
|
});
|
|
605
711
|
|
|
712
|
+
$matchBy.on("change", function () {
|
|
713
|
+
refreshSummary();
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
$clientName.on("input change", function () {
|
|
717
|
+
refreshSummary();
|
|
718
|
+
});
|
|
719
|
+
|
|
606
720
|
$networkId.on("change", function () {
|
|
607
721
|
if (suspendChangeHandling) {
|
|
608
722
|
return;
|
|
@@ -650,6 +764,13 @@
|
|
|
650
764
|
<option value="network">Network (join/leave)</option>
|
|
651
765
|
</select>
|
|
652
766
|
</div>
|
|
767
|
+
<div class="form-row" id="node-row-matchBy" style="display:none;">
|
|
768
|
+
<label for="node-input-matchBy"><i class="fa fa-id-card-o"></i> Identify by</label>
|
|
769
|
+
<select id="node-input-matchBy">
|
|
770
|
+
<option value="id">Client ID</option>
|
|
771
|
+
<option value="name">Client name</option>
|
|
772
|
+
</select>
|
|
773
|
+
</div>
|
|
653
774
|
<div class="form-row" id="node-row-clientId">
|
|
654
775
|
<label for="node-input-clientId"><i class="fa fa-wifi"></i> Client</label>
|
|
655
776
|
<select id="node-input-clientId" style="width:62%;"></select>
|
|
@@ -666,9 +787,10 @@
|
|
|
666
787
|
<div id="unifi-presence-network-status" class="unifi-presence-network-status"></div>
|
|
667
788
|
</div>
|
|
668
789
|
<div id="node-row-advanced" class="unifi-ultimate-advanced">
|
|
669
|
-
<div class="form-row">
|
|
670
|
-
<label for="node-input-
|
|
671
|
-
<input type="text" id="node-input-
|
|
790
|
+
<div class="form-row" id="node-row-clientName" style="display:none;">
|
|
791
|
+
<label for="node-input-clientName"><i class="fa fa-font"></i> Match name</label>
|
|
792
|
+
<input type="text" id="node-input-clientName" readonly>
|
|
793
|
+
<div class="form-tips">Read-only value copied from the selected client. It is used when Identify by is set to Client name.</div>
|
|
672
794
|
</div>
|
|
673
795
|
<div id="node-presence-tuning">
|
|
674
796
|
<div class="form-row">
|
|
@@ -694,6 +816,7 @@
|
|
|
694
816
|
<input type="checkbox" id="node-input-emitInitial" style="display:inline-block; width:auto; vertical-align:top;">
|
|
695
817
|
</div>
|
|
696
818
|
</div>
|
|
819
|
+
<input type="hidden" id="node-input-name">
|
|
697
820
|
<input type="hidden" id="node-input-deviceName">
|
|
698
821
|
<input type="hidden" id="node-input-networkName">
|
|
699
822
|
<input type="hidden" id="node-input-joinConfirmPolls">
|
|
@@ -712,6 +835,8 @@
|
|
|
712
835
|
|
|
713
836
|
<h4>Single client (presence)</h4>
|
|
714
837
|
<p><b>Client</b> selects the client to track. The selector is searchable and opens with the complete list before filtering. Each entry shows the user-defined client name (e.g. "iPhone di Massimo") when set, otherwise the device hostname. Known-but-offline clients are included too and marked with an <code>(offline)</code> label, so you can pick a target even while it is disconnected.</p>
|
|
838
|
+
<p><b>Identify by</b> controls how the selected client is found. <b>Client ID</b> (default) tracks the exact UniFi record and is the safest choice for most devices. <b>Client name</b> searches the currently connected clients by alias, display name, or hostname on every poll, so presence continues to work when a privacy-enabled device rotates its MAC address and UniFi assigns it a new client ID. The read-only <b>Match name</b> value is copied from the client selected above. Name matching is exact but case-insensitive; give devices unique names because any online client with the same name counts as present.</p>
|
|
839
|
+
<p>If a saved client is temporarily missing from the controller response, the editor keeps it as <code>saved; currently unavailable</code> instead of clearing the configuration.</p>
|
|
715
840
|
<p><b>Connect Hysteresis (s)</b> delays the <code>true</code> output so brief reconnects do not flap. <b>Hysteresis (s)</b> delays the <code>false</code> output. <b>Resend (s)</b> optionally re-emits the last saved value on a fixed cadence (<code>0</code> = off); resent messages carry <code>msg.eventName = "repeat"</code>.</p>
|
|
716
841
|
|
|
717
842
|
<h4>Network (join/leave)</h4>
|
|
@@ -726,7 +851,7 @@
|
|
|
726
851
|
</ul>
|
|
727
852
|
<p>The node reports the controller's data faithfully and applies a small built-in debounce, but it cannot fully remove this upstream noise. If duplicates are a problem, debounce in your flow — for example an <code>rbe</code> ("block unless value changes") node, or a <code>delay</code> node in rate-limit mode, keyed on <code>msg.deviceName</code>/<code>msg.payload.mac</code>.</p>
|
|
728
853
|
|
|
729
|
-
<p>In <b>presence</b> mode, <b>Poll (s)</b> sets the check cadence.
|
|
854
|
+
<p>In <b>presence</b> mode, <b>Poll (s)</b> sets the check cadence. Request timeout is handled automatically.</p>
|
|
730
855
|
|
|
731
856
|
<h3>Input</h3>
|
|
732
857
|
<ul>
|
|
@@ -794,4 +919,4 @@
|
|
|
794
919
|
font-size: 0.85em;
|
|
795
920
|
color: var(--red-ui-text-color-error, #b30000);
|
|
796
921
|
}
|
|
797
|
-
</style>
|
|
922
|
+
</style>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { decodeScopedDeviceId } = require("./utils/unifi-network-device-registry");
|
|
4
|
+
const { normalizePresenceMatchBy } = require("./utils/unifi-network-presence-utils");
|
|
4
5
|
const {
|
|
5
6
|
buildStatusTimestampText,
|
|
6
7
|
appendStatusTimestamp,
|
|
@@ -277,6 +278,11 @@ module.exports = function(RED) {
|
|
|
277
278
|
}
|
|
278
279
|
|
|
279
280
|
node.clientId = config.clientId || "";
|
|
281
|
+
// Existing flows keep the historical ID-based behavior. Name matching
|
|
282
|
+
// deliberately resolves the active client again on every poll, allowing
|
|
283
|
+
// privacy-addressed devices to rotate their MAC/resource id.
|
|
284
|
+
node.matchBy = normalizePresenceMatchBy(config.matchBy);
|
|
285
|
+
node.clientName = normalizeString(config.clientName || config.deviceName);
|
|
280
286
|
node.pollIntervalSeconds = parsePositiveSeconds(config.pollInterval, 10);
|
|
281
287
|
node.onlineHysteresisSeconds = parseNonNegativeSeconds(config.onlineHysteresis, 15);
|
|
282
288
|
node.offlineHysteresisSeconds = parseNonNegativeSeconds(config.offlineHysteresis, 30);
|
|
@@ -293,6 +299,8 @@ module.exports = function(RED) {
|
|
|
293
299
|
node.firstOnlineDetectedAt = 0;
|
|
294
300
|
node.firstOfflineDetectedAt = 0;
|
|
295
301
|
node.lastKnownClient = null;
|
|
302
|
+
node.lastMatchedClientId = "";
|
|
303
|
+
node.lastMatchCount = 0;
|
|
296
304
|
|
|
297
305
|
function setNodeStatus(status) {
|
|
298
306
|
if (!status || typeof status !== "object" || Array.isArray(status)) {
|
|
@@ -341,6 +349,10 @@ module.exports = function(RED) {
|
|
|
341
349
|
name: nodeName || undefined,
|
|
342
350
|
deviceName: resolvedDeviceName || undefined,
|
|
343
351
|
clientId,
|
|
352
|
+
matchBy: node.matchBy,
|
|
353
|
+
clientName: node.matchBy === "name" ? node.clientName || undefined : undefined,
|
|
354
|
+
matchedClientId: node.lastMatchedClientId || undefined,
|
|
355
|
+
matchCount: node.matchBy === "name" ? node.lastMatchCount : undefined,
|
|
344
356
|
siteId: scoped.siteId || undefined,
|
|
345
357
|
resourceId: scoped.resourceId || undefined,
|
|
346
358
|
source,
|
|
@@ -469,6 +481,14 @@ module.exports = function(RED) {
|
|
|
469
481
|
const client = snapshot && snapshot.client && typeof snapshot.client === "object" && !Array.isArray(snapshot.client)
|
|
470
482
|
? snapshot.client
|
|
471
483
|
: null;
|
|
484
|
+
if (node.matchBy === "name") {
|
|
485
|
+
node.lastMatchCount = Number.isFinite(Number(snapshot && snapshot.matchCount))
|
|
486
|
+
? Math.max(0, Math.trunc(Number(snapshot.matchCount)))
|
|
487
|
+
: 0;
|
|
488
|
+
if (normalizeString(snapshot && snapshot.matchedClientId)) {
|
|
489
|
+
node.lastMatchedClientId = normalizeString(snapshot.matchedClientId);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
472
492
|
if (client) {
|
|
473
493
|
node.lastKnownClient = client;
|
|
474
494
|
}
|
|
@@ -507,6 +527,8 @@ module.exports = function(RED) {
|
|
|
507
527
|
}
|
|
508
528
|
return node.server.requestPresenceObservationNow({
|
|
509
529
|
clientId,
|
|
530
|
+
matchBy: node.matchBy,
|
|
531
|
+
clientName: node.clientName,
|
|
510
532
|
timeout: node.timeout,
|
|
511
533
|
source: source || "manual-input"
|
|
512
534
|
});
|
|
@@ -587,6 +609,8 @@ module.exports = function(RED) {
|
|
|
587
609
|
}
|
|
588
610
|
return {
|
|
589
611
|
clientId,
|
|
612
|
+
matchBy: node.matchBy,
|
|
613
|
+
clientName: node.clientName,
|
|
590
614
|
pollIntervalSeconds: node.pollIntervalSeconds,
|
|
591
615
|
timeout: node.timeout
|
|
592
616
|
};
|
|
@@ -606,6 +630,8 @@ module.exports = function(RED) {
|
|
|
606
630
|
setNodeStatus({ fill: "red", shape: "ring", text: "no config" });
|
|
607
631
|
} else if (!node.clientId) {
|
|
608
632
|
setNodeStatus({ fill: "grey", shape: "ring", text: "set client" });
|
|
633
|
+
} else if (node.matchBy === "name" && !node.clientName) {
|
|
634
|
+
setNodeStatus({ fill: "grey", shape: "ring", text: "set client name" });
|
|
609
635
|
} else {
|
|
610
636
|
if (node.server && typeof node.server.addClient === "function") {
|
|
611
637
|
node.server.addClient(node);
|
|
@@ -58,6 +58,15 @@
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
function parseStoredNames() {
|
|
62
|
+
try {
|
|
63
|
+
const parsed = JSON.parse(String(node.deviceNames || "{}"));
|
|
64
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
61
70
|
function hasSelection($field) {
|
|
62
71
|
return String($field.val() || "").trim() !== "";
|
|
63
72
|
}
|
|
@@ -101,16 +110,12 @@
|
|
|
101
110
|
const hasStoredSelection = storedIds.length > 0;
|
|
102
111
|
const storedSet = {};
|
|
103
112
|
storedIds.forEach(function (id) { storedSet[id] = true; });
|
|
113
|
+
const storedNames = parseStoredNames();
|
|
114
|
+
const renderedSet = {};
|
|
104
115
|
|
|
105
116
|
$list.empty();
|
|
106
117
|
|
|
107
|
-
|
|
108
|
-
$list.append($("<div class='unifi-restart-empty'></div>").text("No UniFi Network devices found."));
|
|
109
|
-
updateSelectedCount();
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
items
|
|
118
|
+
(Array.isArray(items) ? items : [])
|
|
114
119
|
.slice()
|
|
115
120
|
.sort(function (a, b) {
|
|
116
121
|
return String(a.name || a.id).localeCompare(String(b.name || b.id));
|
|
@@ -120,6 +125,7 @@
|
|
|
120
125
|
if (!id) {
|
|
121
126
|
return;
|
|
122
127
|
}
|
|
128
|
+
renderedSet[id] = true;
|
|
123
129
|
const name = String(item.name || id).trim();
|
|
124
130
|
const model = String(item.state || "").trim();
|
|
125
131
|
const checked = hasStoredSelection ? Boolean(storedSet[id]) : !isGatewayLike(item);
|
|
@@ -139,6 +145,30 @@
|
|
|
139
145
|
$list.append($row);
|
|
140
146
|
});
|
|
141
147
|
|
|
148
|
+
// Keep configured devices that are missing from this controller
|
|
149
|
+
// response. Otherwise opening and saving the editor would silently
|
|
150
|
+
// delete an offline or temporarily unavailable target.
|
|
151
|
+
storedIds.forEach(function (id) {
|
|
152
|
+
if (renderedSet[id]) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const name = String(storedNames[id] || id).trim();
|
|
156
|
+
const $row = $("<label class='unifi-restart-device-row unifi-restart-device-unavailable'></label>");
|
|
157
|
+
const $checkbox = $("<input type='checkbox'>")
|
|
158
|
+
.attr("data-id", id)
|
|
159
|
+
.attr("data-name", name)
|
|
160
|
+
.prop("checked", true);
|
|
161
|
+
$checkbox.on("change", updateSelectedCount);
|
|
162
|
+
$row.append($checkbox);
|
|
163
|
+
$row.append($("<span class='unifi-restart-device-name'></span>").text(name));
|
|
164
|
+
$row.append($("<span class='unifi-restart-device-model'></span>").text("saved; currently unavailable"));
|
|
165
|
+
$list.append($row);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if ($list.find("input[type='checkbox']").length === 0) {
|
|
169
|
+
$list.append($("<div class='unifi-restart-empty'></div>").text("No UniFi Network devices found."));
|
|
170
|
+
}
|
|
171
|
+
|
|
142
172
|
applyFilter();
|
|
143
173
|
updateSelectedCount();
|
|
144
174
|
}
|
|
@@ -158,9 +188,8 @@
|
|
|
158
188
|
function loadDevices(force) {
|
|
159
189
|
const serverId = String($server.val() || "").trim();
|
|
160
190
|
if (!serverId || serverId === "_ADD_") {
|
|
161
|
-
|
|
191
|
+
renderDevices([]);
|
|
162
192
|
$status.text("");
|
|
163
|
-
updateSelectedCount();
|
|
164
193
|
return Promise.resolve();
|
|
165
194
|
}
|
|
166
195
|
|
|
@@ -169,6 +198,9 @@
|
|
|
169
198
|
}
|
|
170
199
|
|
|
171
200
|
$status.text("Loading devices...");
|
|
201
|
+
if ($list.find("input[type='checkbox']").length === 0) {
|
|
202
|
+
renderDevices([]);
|
|
203
|
+
}
|
|
172
204
|
loadedServerId = serverId;
|
|
173
205
|
loadingPromise = requestJson("unifiNetwork/devices?serverId=" + encodeURIComponent(serverId) + "&deviceType=device")
|
|
174
206
|
.then(function (devices) {
|
|
@@ -176,7 +208,7 @@
|
|
|
176
208
|
$status.text("");
|
|
177
209
|
})
|
|
178
210
|
.catch(function (xhr) {
|
|
179
|
-
|
|
211
|
+
renderDevices([]);
|
|
180
212
|
const message = xhr && xhr.responseJSON && xhr.responseJSON.error
|
|
181
213
|
? xhr.responseJSON.error
|
|
182
214
|
: "Unable to load devices.";
|
|
@@ -278,7 +310,7 @@
|
|
|
278
310
|
<li><code>Restart devices</code> — reboots each selected device (UniFi <code>RESTART</code> action).</li>
|
|
279
311
|
<li><code>Power cycle PoE ports</code> — power-cycles every port currently delivering PoE on each selected device (UniFi <code>POWER_CYCLE</code> action). Devices with no active PoE ports are skipped.</li>
|
|
280
312
|
</ul>
|
|
281
|
-
<p><b>Devices</b> lists every adopted UniFi Network device. By default switches and access points are pre-selected; gateways, consoles and cloud keys are listed but unchecked. Add or remove devices freely — the selection is saved with the node. Use <code>All</code>/<code>None</code> and the filter box to manage long lists.</p>
|
|
313
|
+
<p><b>Devices</b> lists every adopted UniFi Network device. By default switches and access points are pre-selected; gateways, consoles and cloud keys are listed but unchecked. Add or remove devices freely — the selection is saved with the node. A configured device missing from the current controller response remains checked and is marked as <code>saved; currently unavailable</code>. Use <code>All</code>/<code>None</code> and the filter box to manage long lists.</p>
|
|
282
314
|
|
|
283
315
|
<h3>Input</h3>
|
|
284
316
|
<ul>
|
|
@@ -861,7 +861,20 @@
|
|
|
861
861
|
);
|
|
862
862
|
});
|
|
863
863
|
|
|
864
|
-
|
|
864
|
+
const hasCurrentValue = currentValue && $deviceId.find("option").filter(function () {
|
|
865
|
+
return String($(this).val() || "") === currentValue;
|
|
866
|
+
}).length > 0;
|
|
867
|
+
if (hasCurrentValue) {
|
|
868
|
+
$deviceId.val(currentValue);
|
|
869
|
+
} else if (currentValue) {
|
|
870
|
+
const savedName = String(node.deviceName || currentValue).trim();
|
|
871
|
+
$deviceId.append(
|
|
872
|
+
$("<option></option>")
|
|
873
|
+
.val(currentValue)
|
|
874
|
+
.text(savedName + " (saved; currently unavailable)")
|
|
875
|
+
.attr("data-device-name", savedName)
|
|
876
|
+
.attr("data-unavailable", "true")
|
|
877
|
+
);
|
|
865
878
|
$deviceId.val(currentValue);
|
|
866
879
|
} else {
|
|
867
880
|
$deviceId.val("");
|
|
@@ -1080,6 +1093,7 @@
|
|
|
1080
1093
|
});
|
|
1081
1094
|
|
|
1082
1095
|
refreshSelectionSummary();
|
|
1096
|
+
renderDevices([]);
|
|
1083
1097
|
ensureDefaultServerSelection();
|
|
1084
1098
|
refreshEditor();
|
|
1085
1099
|
}
|
|
@@ -1146,10 +1160,12 @@
|
|
|
1146
1160
|
|
|
1147
1161
|
<h3>Configuration</h3>
|
|
1148
1162
|
<p><b>Connection</b> selects the shared UniFi Protect config node.</p>
|
|
1149
|
-
<p><b>Control</b> chooses the Protect device category. <b>Device</b> chooses the specific item. Device selectors are searchable and open with the complete list before filtering.</p>
|
|
1163
|
+
<p><b>Control</b> chooses the Protect device category. <b>Device</b> chooses the specific item. Device selectors are searchable and open with the complete list before filtering. A saved selection that is temporarily unavailable is preserved instead of being cleared.</p>
|
|
1150
1164
|
<p><b>Action</b> only shows actions supported by the selected device.</p>
|
|
1151
1165
|
<p>Additional fields appear only when the selected action needs extra input. For example, events, PTZ presets, live views, or discovered properties.</p>
|
|
1152
|
-
<p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, and <code>Read Application Info</code> on <code>NVR</code>.</p>
|
|
1166
|
+
<p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, siren play/stop/test, relay output control, speaker sound test, and <code>Read Application Info</code> on <code>NVR</code>.</p>
|
|
1167
|
+
<p>Protect resources include cameras, sensors, lights, chimes, viewers, live views, NVRs, bridges, Link Stations, alarm hubs, key fobs, relays, sirens, and speakers. The newer device families expose read state and events without the generic raw-update action.</p>
|
|
1168
|
+
<p><code>Play Siren</code> uses a duration in seconds. <code>Control Relay Output</code> requires an explicit output and on/off state; auto-off applies only when turning an output on. Sound-test actions use a configured volume and never read control values from the incoming message.</p>
|
|
1153
1169
|
<p><code>Receive Events</code> performs an initial fetch and then subscribes to streams managed by the shared Protect config node. The config node keeps websocket channels and forwards matching updates to runtime nodes. In this mode, the input pin is hidden and listening starts automatically after deploy/restart.</p>
|
|
1154
1170
|
<p>For <code>Sensor</code>, <code>Camera</code>, and <code>Light</code> observables, <code>All</code> emits every event for the selected device. Choosing a specific observable normalizes the main output to a derived value and keeps the original context in <code>msg.details.raw</code>. Event observables emit <code>true/false</code>; sensor metrics such as <code>Temperature</code>, <code>Humidity</code>, <code>Light Level</code>, and <code>Battery Level</code> emit numeric values.</p>
|
|
1155
1171
|
<p><b>Emit periodically</b> repeats read-only actions at the selected interval and sends each result to the flow. It is hidden for <code>Receive Events</code> and write actions.</p>
|
|
@@ -1217,4 +1233,4 @@
|
|
|
1217
1233
|
"payload": null,
|
|
1218
1234
|
"error": { "message": "Request timed out" }
|
|
1219
1235
|
}</code></pre>
|
|
1220
|
-
</script>
|
|
1236
|
+
</script>
|