node-red-contrib-unifi-ultimate 1.0.1 → 1.0.3
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 +59 -208
- package/nodes/unifi-access-config.html +4 -3
- package/nodes/unifi-access-device.html +77 -17
- package/nodes/unifi-access-device.js +79 -68
- package/nodes/unifi-network-config.html +15 -5
- package/nodes/unifi-network-config.js +288 -0
- package/nodes/unifi-network-control-poe.html +40 -22
- package/nodes/unifi-network-control-poe.js +13 -68
- package/nodes/unifi-network-device.html +113 -19
- package/nodes/unifi-network-device.js +437 -82
- package/nodes/unifi-network-presence.html +40 -21
- package/nodes/unifi-network-presence.js +18 -55
- package/nodes/unifi-protect-config.html +4 -3
- package/nodes/unifi-protect-device.html +77 -17
- package/nodes/unifi-protect-device.js +79 -68
- package/nodes/utils/common-utils.js +104 -0
- package/nodes/utils/unifi-network-device-registry.js +95 -0
- package/package.json +5 -3
|
@@ -9,6 +9,19 @@ const {
|
|
|
9
9
|
matchesObservable
|
|
10
10
|
} = require("./utils/unifi-access-device-registry");
|
|
11
11
|
const { extractAccessData } = require("./utils/unifi-access-utils");
|
|
12
|
+
const {
|
|
13
|
+
parseBoolean,
|
|
14
|
+
parseIntervalSeconds,
|
|
15
|
+
buildStatusTimestampText,
|
|
16
|
+
appendStatusTimestamp,
|
|
17
|
+
resolveNodeName,
|
|
18
|
+
resolveDeviceName,
|
|
19
|
+
extractDeviceNameFromPayload,
|
|
20
|
+
attachDeviceNameToPayload,
|
|
21
|
+
attachDetails,
|
|
22
|
+
buildErrorOutputMessage
|
|
23
|
+
} = require("./utils/common-utils");
|
|
24
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
12
25
|
|
|
13
26
|
function resolveDeviceType(configuredDeviceType) {
|
|
14
27
|
return String(configuredDeviceType || "").trim();
|
|
@@ -64,70 +77,6 @@ function requiresDeviceSpecificCapabilityValidation(deviceType, capabilityId) {
|
|
|
64
77
|
return String(deviceType || "").trim() === "device" && ["triggerDoorbell", "cancelDoorbell"].includes(String(capabilityId || "").trim());
|
|
65
78
|
}
|
|
66
79
|
|
|
67
|
-
function resolveNodeName(value) {
|
|
68
|
-
return String(value || "").trim();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function resolveDeviceName(value) {
|
|
72
|
-
return String(value || "").trim();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function extractDeviceNameFromPayload(payload) {
|
|
76
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
77
|
-
return "";
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return resolveDeviceName(
|
|
81
|
-
payload.name
|
|
82
|
-
|| payload.displayName
|
|
83
|
-
|| payload.alias
|
|
84
|
-
|| payload.full_name
|
|
85
|
-
|| payload.id
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function attachDeviceNameToPayload(payload, deviceName) {
|
|
90
|
-
if (!deviceName) {
|
|
91
|
-
return payload;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
95
|
-
return {
|
|
96
|
-
...payload,
|
|
97
|
-
deviceName
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return payload;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function attachDetails(outputMsg, details) {
|
|
105
|
-
if (!details || typeof details !== "object" || Array.isArray(details)) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const currentDetails = outputMsg.details && typeof outputMsg.details === "object" && !Array.isArray(outputMsg.details)
|
|
110
|
-
? outputMsg.details
|
|
111
|
-
: {};
|
|
112
|
-
|
|
113
|
-
outputMsg.details = {
|
|
114
|
-
...currentDetails,
|
|
115
|
-
...details
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function buildStatusTimestampText() {
|
|
120
|
-
const now = new Date();
|
|
121
|
-
const time = now.toTimeString().split(" ")[0];
|
|
122
|
-
return `(day ${now.getDate()}, ${time})`;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function appendStatusTimestamp(text) {
|
|
126
|
-
const normalized = String(text === undefined || text === null ? "" : text).trim();
|
|
127
|
-
const suffix = buildStatusTimestampText();
|
|
128
|
-
return normalized ? `${normalized} ${suffix}` : suffix;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
80
|
function resolveConfiguredObservable(capabilityConfig) {
|
|
132
81
|
return String(
|
|
133
82
|
capabilityConfig && capabilityConfig.observable !== undefined
|
|
@@ -147,8 +96,12 @@ module.exports = function(RED) {
|
|
|
147
96
|
node.deviceId = config.deviceId || "";
|
|
148
97
|
node.capability = config.capability || "observe";
|
|
149
98
|
node.capabilityConfig = config.capabilityConfig || "{}";
|
|
99
|
+
node.autoEmit = parseBoolean(config.autoEmit);
|
|
100
|
+
node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
|
|
101
|
+
node.autoEmitTimer = null;
|
|
102
|
+
node.autoEmitInFlight = false;
|
|
150
103
|
node.deviceName = resolveDeviceName(config.deviceName);
|
|
151
|
-
node.timeout =
|
|
104
|
+
node.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
152
105
|
node.currentDevice = null;
|
|
153
106
|
node.isObserving = false;
|
|
154
107
|
|
|
@@ -229,7 +182,7 @@ module.exports = function(RED) {
|
|
|
229
182
|
sendOutputs(send, stateMsg, null);
|
|
230
183
|
}
|
|
231
184
|
|
|
232
|
-
async function invokeCapability(send) {
|
|
185
|
+
async function invokeCapability(send, triggerSource) {
|
|
233
186
|
if (!node.server) {
|
|
234
187
|
throw new Error("Unifi Access configuration is missing.");
|
|
235
188
|
}
|
|
@@ -314,7 +267,7 @@ module.exports = function(RED) {
|
|
|
314
267
|
deviceId,
|
|
315
268
|
capabilityId,
|
|
316
269
|
send,
|
|
317
|
-
capability.mode === "fetch" ? "fetch" : "manual-refresh"
|
|
270
|
+
triggerSource || (capability.mode === "fetch" ? "fetch" : "manual-refresh")
|
|
318
271
|
);
|
|
319
272
|
return;
|
|
320
273
|
}
|
|
@@ -399,6 +352,16 @@ module.exports = function(RED) {
|
|
|
399
352
|
return resolveCapabilityId(node.capability) === "observe";
|
|
400
353
|
}
|
|
401
354
|
|
|
355
|
+
function configuredCapabilitySupportsAutoEmit() {
|
|
356
|
+
const capability = resolveConfiguredCapabilityDefinition();
|
|
357
|
+
if (!capability || capability.opensEventStream === true) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const method = String(capability.method || "").trim().toUpperCase();
|
|
362
|
+
return !method || method === "GET";
|
|
363
|
+
}
|
|
364
|
+
|
|
402
365
|
function shouldObserveConfiguredDevice() {
|
|
403
366
|
return node.server && configuredCapabilityOpensEventStream() && node.deviceType && node.deviceId;
|
|
404
367
|
}
|
|
@@ -416,18 +379,64 @@ module.exports = function(RED) {
|
|
|
416
379
|
});
|
|
417
380
|
}
|
|
418
381
|
|
|
382
|
+
function stopAutoEmitTimer() {
|
|
383
|
+
if (node.autoEmitTimer) {
|
|
384
|
+
clearInterval(node.autoEmitTimer);
|
|
385
|
+
node.autoEmitTimer = null;
|
|
386
|
+
}
|
|
387
|
+
node.autoEmitInFlight = false;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function shouldStartAutoEmitTimer() {
|
|
391
|
+
return Boolean(
|
|
392
|
+
node.server
|
|
393
|
+
&& node.autoEmit
|
|
394
|
+
&& node.deviceType
|
|
395
|
+
&& node.deviceId
|
|
396
|
+
&& configuredCapabilitySupportsAutoEmit()
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function runAutoEmit() {
|
|
401
|
+
if (node.autoEmitInFlight || !shouldStartAutoEmitTimer()) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
node.autoEmitInFlight = true;
|
|
406
|
+
invokeCapability(node.send.bind(node), "interval")
|
|
407
|
+
.catch((error) => {
|
|
408
|
+
setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
|
|
409
|
+
node.send([null, buildErrorOutputMessage(error, node.name)]);
|
|
410
|
+
node.error(error);
|
|
411
|
+
})
|
|
412
|
+
.finally(() => {
|
|
413
|
+
node.autoEmitInFlight = false;
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function startAutoEmitTimer() {
|
|
418
|
+
stopAutoEmitTimer();
|
|
419
|
+
if (!shouldStartAutoEmitTimer()) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
node.autoEmitTimer = setInterval(runAutoEmit, node.autoEmitIntervalSeconds * 1000);
|
|
424
|
+
setNodeStatus({ fill: "grey", shape: "ring", text: `every ${node.autoEmitIntervalSeconds}s` });
|
|
425
|
+
}
|
|
426
|
+
|
|
419
427
|
node.on("input", async function(_msg, send, done) {
|
|
420
428
|
send = send || function() {
|
|
421
429
|
node.send.apply(node, arguments);
|
|
422
430
|
};
|
|
423
431
|
|
|
424
432
|
try {
|
|
425
|
-
await invokeCapability(send);
|
|
433
|
+
await invokeCapability(send, "manual-refresh");
|
|
426
434
|
if (typeof done === "function") {
|
|
427
435
|
done();
|
|
428
436
|
}
|
|
429
437
|
} catch (error) {
|
|
430
438
|
setNodeStatus({ fill: "red", shape: "ring", text: "error" });
|
|
439
|
+
node.send([null, buildErrorOutputMessage(error, node.name)]);
|
|
431
440
|
if (typeof done === "function") {
|
|
432
441
|
done(error);
|
|
433
442
|
} else {
|
|
@@ -481,10 +490,12 @@ module.exports = function(RED) {
|
|
|
481
490
|
node.server.addClient(node);
|
|
482
491
|
}
|
|
483
492
|
startObservation();
|
|
493
|
+
startAutoEmitTimer();
|
|
484
494
|
}
|
|
485
495
|
|
|
486
496
|
node.on("close", function(done) {
|
|
487
497
|
try {
|
|
498
|
+
stopAutoEmitTimer();
|
|
488
499
|
if (node.server && typeof node.server.removeClient === "function") {
|
|
489
500
|
node.server.removeClient(node);
|
|
490
501
|
}
|
|
@@ -8,7 +8,16 @@
|
|
|
8
8
|
name: { value: "Unifi Network Config" },
|
|
9
9
|
host: { value: "", required: true },
|
|
10
10
|
rejectUnauthorized: { value: false },
|
|
11
|
-
powerObservationIntervalSeconds: {
|
|
11
|
+
powerObservationIntervalSeconds: {
|
|
12
|
+
value: 15,
|
|
13
|
+
required: false,
|
|
14
|
+
validate: function (value) {
|
|
15
|
+
return value === undefined
|
|
16
|
+
|| value === null
|
|
17
|
+
|| value === ""
|
|
18
|
+
|| RED.validators.number()(value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
12
21
|
},
|
|
13
22
|
credentials: {
|
|
14
23
|
apiKey: { type: "password", required: true }
|
|
@@ -17,7 +26,8 @@
|
|
|
17
26
|
return this.name || this.host || "Unifi Network Config";
|
|
18
27
|
},
|
|
19
28
|
oneditprepare: function () {
|
|
20
|
-
const
|
|
29
|
+
const node = this;
|
|
30
|
+
const $hostInput = $("#node-config-input-host");
|
|
21
31
|
const $apiKey = $("#node-config-input-apiKey");
|
|
22
32
|
const $rejectUnauthorized = $("#node-config-input-rejectUnauthorized");
|
|
23
33
|
const $powerObservationIntervalSeconds = $("#node-config-input-powerObservationIntervalSeconds");
|
|
@@ -27,7 +37,7 @@
|
|
|
27
37
|
const $advancedRow = $("#node-config-row-advanced");
|
|
28
38
|
|
|
29
39
|
function syncProgressiveRows() {
|
|
30
|
-
const hasHost = String($
|
|
40
|
+
const hasHost = String($hostInput.val() || node.host || "").trim() !== "";
|
|
31
41
|
$apiKeyRow.toggle(hasHost);
|
|
32
42
|
$securityRow.toggle(hasHost);
|
|
33
43
|
$powerObservationRow.toggle(hasHost);
|
|
@@ -42,7 +52,7 @@
|
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
syncProgressiveRows();
|
|
45
|
-
$
|
|
55
|
+
$hostInput.on("change input", function () {
|
|
46
56
|
syncProgressiveRows();
|
|
47
57
|
});
|
|
48
58
|
}
|
|
@@ -93,7 +103,7 @@
|
|
|
93
103
|
<p><b>Controller</b> accepts the UniFi OS host or IP address. The node builds the local UniFi Network proxy URL automatically.</p>
|
|
94
104
|
<p><b>API Key</b> is required for authentication and is sent as <code>X-API-Key</code>.</p>
|
|
95
105
|
<p><b>Security</b> controls certificate verification. Disable it only when your controller uses a self-signed certificate.</p>
|
|
96
|
-
<p><b>Power Poll (s)</b> is the global polling interval used by all <code>Control POE</code> nodes when action is <code>Emit Power Consumption</code
|
|
106
|
+
<p><b>Power Poll (s)</b> is the global polling interval used by all <code>Control POE</code> nodes when action is <code>Emit Power Consumption</code>. It is optional and defaults to 15 seconds.</p>
|
|
97
107
|
<p><b>Name</b> is the config node name shown in the editor.</p>
|
|
98
108
|
|
|
99
109
|
<h3>Usage</h3>
|
|
@@ -1422,6 +1422,294 @@ module.exports = function(RED) {
|
|
|
1422
1422
|
};
|
|
1423
1423
|
}
|
|
1424
1424
|
|
|
1425
|
+
function parseTemperatureNumber(value) {
|
|
1426
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1427
|
+
return value;
|
|
1428
|
+
}
|
|
1429
|
+
if (typeof value === "string") {
|
|
1430
|
+
const normalized = value.trim().replace(",", ".");
|
|
1431
|
+
if (!normalized) {
|
|
1432
|
+
return undefined;
|
|
1433
|
+
}
|
|
1434
|
+
const parsed = Number.parseFloat(normalized);
|
|
1435
|
+
if (Number.isFinite(parsed)) {
|
|
1436
|
+
return parsed;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
return undefined;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
function normalizeTemperatureCelsius(value, unit) {
|
|
1443
|
+
const parsed = parseTemperatureNumber(value);
|
|
1444
|
+
if (!Number.isFinite(parsed)) {
|
|
1445
|
+
return undefined;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
const normalizedUnit = normalizeString(unit).toLowerCase();
|
|
1449
|
+
let celsius = parsed;
|
|
1450
|
+
if (["f", "fahrenheit"].includes(normalizedUnit)) {
|
|
1451
|
+
celsius = (parsed - 32) * 5 / 9;
|
|
1452
|
+
} else if (["k", "kelvin"].includes(normalizedUnit)) {
|
|
1453
|
+
celsius = parsed - 273.15;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
return Math.round(celsius * 10) / 10;
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
function normalizeTemperatureLabel(key, fallbackLabel) {
|
|
1460
|
+
const explicit = normalizeString(fallbackLabel);
|
|
1461
|
+
const raw = explicit || normalizeString(key) || "temperature";
|
|
1462
|
+
const compact = raw
|
|
1463
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
1464
|
+
.replace(/[_-]+/g, " ")
|
|
1465
|
+
.replace(/\b(temp|temperature|celsius|fahrenheit|kelvin)\b/ig, " ")
|
|
1466
|
+
.replace(/\s{2,}/g, " ")
|
|
1467
|
+
.trim();
|
|
1468
|
+
|
|
1469
|
+
if (!compact) {
|
|
1470
|
+
return "Temperature";
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
return compact
|
|
1474
|
+
.split(" ")
|
|
1475
|
+
.map((part) => {
|
|
1476
|
+
const upper = part.toUpperCase();
|
|
1477
|
+
if (["CPU", "PHY", "SFP", "PSU", "PCB"].includes(upper)) {
|
|
1478
|
+
return upper;
|
|
1479
|
+
}
|
|
1480
|
+
return part.charAt(0).toUpperCase() + part.slice(1);
|
|
1481
|
+
})
|
|
1482
|
+
.join(" ");
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
function isTemperatureKey(key) {
|
|
1486
|
+
const normalized = normalizeString(key);
|
|
1487
|
+
return /(^|[_\-.])temp(erature)?($|[_\-.])/i.test(normalized)
|
|
1488
|
+
|| /Temperature$/.test(normalized)
|
|
1489
|
+
|| /^temp(erature)?$/i.test(normalized);
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
function collectTemperatureReadings(source, sourceName) {
|
|
1493
|
+
const readings = [];
|
|
1494
|
+
const seen = new Set();
|
|
1495
|
+
|
|
1496
|
+
function addReading(name, rawValue, unit, key, path) {
|
|
1497
|
+
const valueC = normalizeTemperatureCelsius(rawValue, unit);
|
|
1498
|
+
if (!Number.isFinite(valueC)) {
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
const label = normalizeTemperatureLabel(key, name);
|
|
1503
|
+
const normalizedPath = normalizeString(path || key || label);
|
|
1504
|
+
const dedupeKey = `${sourceName}:${label.toLowerCase()}:${valueC}:${normalizedPath.toLowerCase()}`;
|
|
1505
|
+
if (seen.has(dedupeKey)) {
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
seen.add(dedupeKey);
|
|
1509
|
+
readings.push({
|
|
1510
|
+
name: label,
|
|
1511
|
+
valueC,
|
|
1512
|
+
unit: "C",
|
|
1513
|
+
source: sourceName,
|
|
1514
|
+
key: normalizedPath || undefined
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
function walk(value, path, depth) {
|
|
1519
|
+
if (!value || typeof value !== "object" || depth > 5) {
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
if (Array.isArray(value)) {
|
|
1524
|
+
value.forEach((entry, index) => walk(entry, `${path}[${index}]`, depth + 1));
|
|
1525
|
+
return;
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
const objectName = firstNonEmptyString([
|
|
1529
|
+
value.name,
|
|
1530
|
+
value.label,
|
|
1531
|
+
value.type,
|
|
1532
|
+
value.sensor,
|
|
1533
|
+
value.description
|
|
1534
|
+
]);
|
|
1535
|
+
const objectDescriptor = firstNonEmptyString([
|
|
1536
|
+
value.type,
|
|
1537
|
+
value.sensor,
|
|
1538
|
+
value.category,
|
|
1539
|
+
value.metric,
|
|
1540
|
+
value.kind,
|
|
1541
|
+
objectName
|
|
1542
|
+
]);
|
|
1543
|
+
const objectUnit = firstNonEmptyString([
|
|
1544
|
+
value.unit,
|
|
1545
|
+
value.units,
|
|
1546
|
+
value.scale
|
|
1547
|
+
]);
|
|
1548
|
+
if ((objectName && isTemperatureKey(objectName)) || isTemperatureKey(objectDescriptor) || isTemperatureKey(path)) {
|
|
1549
|
+
[
|
|
1550
|
+
value.value,
|
|
1551
|
+
value.current,
|
|
1552
|
+
value.currentValue,
|
|
1553
|
+
value.temperature,
|
|
1554
|
+
value.temp
|
|
1555
|
+
].forEach((candidate) => addReading(objectName || objectDescriptor, candidate, objectUnit, objectDescriptor || objectName, path));
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
Object.entries(value).forEach(([key, entryValue]) => {
|
|
1559
|
+
const entryPath = path ? `${path}.${key}` : key;
|
|
1560
|
+
const keyLooksLikeTemperature = isTemperatureKey(key);
|
|
1561
|
+
if (keyLooksLikeTemperature) {
|
|
1562
|
+
if (entryValue && typeof entryValue === "object") {
|
|
1563
|
+
const nestedUnit = firstNonEmptyString([
|
|
1564
|
+
entryValue.unit,
|
|
1565
|
+
entryValue.units,
|
|
1566
|
+
entryValue.scale,
|
|
1567
|
+
objectUnit
|
|
1568
|
+
]);
|
|
1569
|
+
[
|
|
1570
|
+
entryValue.value,
|
|
1571
|
+
entryValue.current,
|
|
1572
|
+
entryValue.currentValue,
|
|
1573
|
+
entryValue.celsius,
|
|
1574
|
+
entryValue.c,
|
|
1575
|
+
entryValue.temperature,
|
|
1576
|
+
entryValue.temp
|
|
1577
|
+
].forEach((candidate) => addReading(objectName || key, candidate, nestedUnit, key, entryPath));
|
|
1578
|
+
} else {
|
|
1579
|
+
addReading(objectName || key, entryValue, objectUnit, key, entryPath);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
if (entryValue && typeof entryValue === "object") {
|
|
1584
|
+
walk(entryValue, entryPath, depth + 1);
|
|
1585
|
+
}
|
|
1586
|
+
});
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
walk(source, "", 0);
|
|
1590
|
+
return readings;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
function buildSelectedDeviceKeySet(deviceId, device) {
|
|
1594
|
+
const scopedDevice = resolveScopedIdentifiers("device", deviceId, device);
|
|
1595
|
+
const selectedDevice = device && typeof device === "object" && !Array.isArray(device)
|
|
1596
|
+
? device
|
|
1597
|
+
: {};
|
|
1598
|
+
const selectedDeviceKeys = new Set();
|
|
1599
|
+
[
|
|
1600
|
+
scopedDevice.resourceId,
|
|
1601
|
+
selectedDevice.id,
|
|
1602
|
+
selectedDevice.deviceId,
|
|
1603
|
+
selectedDevice.device_id,
|
|
1604
|
+
selectedDevice._id,
|
|
1605
|
+
selectedDevice.macAddress,
|
|
1606
|
+
selectedDevice.mac,
|
|
1607
|
+
selectedDevice.mac_address,
|
|
1608
|
+
selectedDevice.serial,
|
|
1609
|
+
selectedDevice.serialNumber
|
|
1610
|
+
].forEach((value) => addIdentifierKeys(selectedDeviceKeys, value));
|
|
1611
|
+
return {
|
|
1612
|
+
scopedDevice,
|
|
1613
|
+
selectedDevice,
|
|
1614
|
+
selectedDeviceKeys
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
function findMatchingLegacyDevice(legacyDevices, selectedDeviceKeys) {
|
|
1619
|
+
return (Array.isArray(legacyDevices) ? legacyDevices : []).find((legacyDevice) => {
|
|
1620
|
+
return [
|
|
1621
|
+
legacyDevice && legacyDevice.device_id,
|
|
1622
|
+
legacyDevice && legacyDevice.id,
|
|
1623
|
+
legacyDevice && legacyDevice._id,
|
|
1624
|
+
legacyDevice && legacyDevice.mac,
|
|
1625
|
+
legacyDevice && legacyDevice.macAddress,
|
|
1626
|
+
legacyDevice && legacyDevice.serial,
|
|
1627
|
+
legacyDevice && legacyDevice.serialNumber
|
|
1628
|
+
].some((value) => {
|
|
1629
|
+
const rawKey = normalizeString(value).toLowerCase();
|
|
1630
|
+
const compactKey = normalizeIdentifierKey(value);
|
|
1631
|
+
return selectedDeviceKeys.has(rawKey) || selectedDeviceKeys.has(compactKey);
|
|
1632
|
+
});
|
|
1633
|
+
}) || null;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
node.fetchDeviceTemperatures = async (deviceId) => {
|
|
1637
|
+
const device = await node.fetchDeviceByTypeAndId("device", deviceId);
|
|
1638
|
+
const {
|
|
1639
|
+
scopedDevice,
|
|
1640
|
+
selectedDevice,
|
|
1641
|
+
selectedDeviceKeys
|
|
1642
|
+
} = buildSelectedDeviceKeySet(deviceId, device);
|
|
1643
|
+
|
|
1644
|
+
const sources = [];
|
|
1645
|
+
const officialReadings = collectTemperatureReadings(device, "official");
|
|
1646
|
+
if (officialReadings.length > 0) {
|
|
1647
|
+
sources.push("official");
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
let legacyDevice = null;
|
|
1651
|
+
let legacyReadings = [];
|
|
1652
|
+
if (scopedDevice.siteId) {
|
|
1653
|
+
try {
|
|
1654
|
+
const legacyDevices = await fetchLegacyDevices(scopedDevice.siteId);
|
|
1655
|
+
legacyDevice = findMatchingLegacyDevice(legacyDevices, selectedDeviceKeys);
|
|
1656
|
+
legacyReadings = collectTemperatureReadings(legacyDevice, "legacy");
|
|
1657
|
+
if (legacyReadings.length > 0) {
|
|
1658
|
+
sources.push("legacy");
|
|
1659
|
+
}
|
|
1660
|
+
} catch (error) {
|
|
1661
|
+
legacyDevice = null;
|
|
1662
|
+
legacyReadings = [];
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
const dedupe = new Set();
|
|
1667
|
+
const temperatures = officialReadings.concat(legacyReadings).filter((reading) => {
|
|
1668
|
+
const key = `${normalizeString(reading.name).toLowerCase()}:${reading.valueC}`;
|
|
1669
|
+
if (dedupe.has(key)) {
|
|
1670
|
+
return false;
|
|
1671
|
+
}
|
|
1672
|
+
dedupe.add(key);
|
|
1673
|
+
return true;
|
|
1674
|
+
});
|
|
1675
|
+
const maxTemperatureC = temperatures.reduce((max, reading) => {
|
|
1676
|
+
const value = Number(reading && reading.valueC);
|
|
1677
|
+
if (!Number.isFinite(value)) {
|
|
1678
|
+
return max;
|
|
1679
|
+
}
|
|
1680
|
+
return Number.isFinite(max) ? Math.max(max, value) : value;
|
|
1681
|
+
}, NaN);
|
|
1682
|
+
const primaryTemperatureC = temperatures.length > 0
|
|
1683
|
+
? Number(temperatures[0].valueC)
|
|
1684
|
+
: NaN;
|
|
1685
|
+
const deviceName = firstNonEmptyString([
|
|
1686
|
+
selectedDevice.name,
|
|
1687
|
+
selectedDevice.displayName,
|
|
1688
|
+
selectedDevice.hostname,
|
|
1689
|
+
legacyDevice && legacyDevice.name,
|
|
1690
|
+
legacyDevice && legacyDevice.hostname,
|
|
1691
|
+
scopedDevice.resourceId
|
|
1692
|
+
]);
|
|
1693
|
+
|
|
1694
|
+
return {
|
|
1695
|
+
device: {
|
|
1696
|
+
...selectedDevice,
|
|
1697
|
+
...(legacyDevice ? { legacyDevice } : {})
|
|
1698
|
+
},
|
|
1699
|
+
sources,
|
|
1700
|
+
payload: {
|
|
1701
|
+
deviceId,
|
|
1702
|
+
siteId: scopedDevice.siteId || undefined,
|
|
1703
|
+
resourceId: scopedDevice.resourceId || undefined,
|
|
1704
|
+
deviceName: deviceName || undefined,
|
|
1705
|
+
hasTemperatures: temperatures.length > 0,
|
|
1706
|
+
temperatureC: Number.isFinite(primaryTemperatureC) ? primaryTemperatureC : undefined,
|
|
1707
|
+
maxTemperatureC: Number.isFinite(maxTemperatureC) ? maxTemperatureC : undefined,
|
|
1708
|
+
temperatures
|
|
1709
|
+
}
|
|
1710
|
+
};
|
|
1711
|
+
};
|
|
1712
|
+
|
|
1425
1713
|
node.fetchDevicePorts = async (deviceId) => {
|
|
1426
1714
|
const device = await node.fetchDeviceByTypeAndId("device", deviceId);
|
|
1427
1715
|
const ports = extractDevicePorts(device);
|
|
@@ -13,14 +13,13 @@
|
|
|
13
13
|
deviceId: { value: "", required: true },
|
|
14
14
|
deviceName: { value: "" },
|
|
15
15
|
portIdx: { value: 1, required: true, validate: RED.validators.number() },
|
|
16
|
-
action: { value: "msgPayload", required: true }
|
|
17
|
-
timeout: { value: 15000, required: true, validate: RED.validators.number() }
|
|
16
|
+
action: { value: "msgPayload", required: true }
|
|
18
17
|
},
|
|
19
18
|
inputs: 1,
|
|
20
|
-
outputs:
|
|
19
|
+
outputs: 2,
|
|
21
20
|
icon: "unifi-u-logo-light-network.svg",
|
|
22
21
|
paletteLabel: "Control POE",
|
|
23
|
-
outputLabels: ["result"],
|
|
22
|
+
outputLabels: ["result", "error"],
|
|
24
23
|
label: function () {
|
|
25
24
|
return this.name || "Control POE";
|
|
26
25
|
},
|
|
@@ -1095,10 +1094,6 @@
|
|
|
1095
1094
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
1096
1095
|
<input type="text" id="node-input-name" placeholder="Node name">
|
|
1097
1096
|
</div>
|
|
1098
|
-
<div class="form-row">
|
|
1099
|
-
<label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout</label>
|
|
1100
|
-
<input type="text" id="node-input-timeout" placeholder="15000">
|
|
1101
|
-
</div>
|
|
1102
1097
|
<input type="hidden" id="node-input-deviceName">
|
|
1103
1098
|
</div>
|
|
1104
1099
|
</script>
|
|
@@ -1116,8 +1111,8 @@
|
|
|
1116
1111
|
<p><b>Port</b> selects the physical port index. Port labels include connected clients or downstream UniFi devices when available.</p>
|
|
1117
1112
|
<p><b>Action</b> executes the configured PoE operation. <code>POE controlled by msg.payload</code> maps incoming payloads to PoE mode (<code>true</code> enables PoE, <code>false</code> disables PoE). Enable and Disable update the UniFi port override (<code>poe_mode</code> auto/off). Power Cycle uses the official Network port action. <code>Emit Power Consumption</code> emits power values from the shared observer in the config node.</p>
|
|
1118
1113
|
<p><b>Power poll interval</b> for emit action is configured globally in <code>Unifi Network Config</code>. This node does not run its own timer.</p>
|
|
1114
|
+
<p>Request timeout is handled automatically by the node.</p>
|
|
1119
1115
|
<p><b>Name</b> is the node name shown in the editor and is updated only when you manually select a different switch or client target.</p>
|
|
1120
|
-
<p><b>Timeout</b> controls the request timeout in milliseconds.</p>
|
|
1121
1116
|
|
|
1122
1117
|
<h3>Input</h3>
|
|
1123
1118
|
<ul>
|
|
@@ -1127,18 +1122,35 @@
|
|
|
1127
1122
|
<li>For all other actions, input message properties are ignored.</li>
|
|
1128
1123
|
</ul>
|
|
1129
1124
|
|
|
1130
|
-
<h3>
|
|
1131
|
-
<
|
|
1132
|
-
<
|
|
1133
|
-
<
|
|
1134
|
-
<
|
|
1135
|
-
<
|
|
1136
|
-
<
|
|
1137
|
-
<
|
|
1138
|
-
<
|
|
1139
|
-
<
|
|
1140
|
-
<
|
|
1141
|
-
|
|
1125
|
+
<h3>Outputs</h3>
|
|
1126
|
+
<dl class="message-properties">
|
|
1127
|
+
<dt>Output 1 — result</dt>
|
|
1128
|
+
<dd>Emitted after the configured action completes successfully.</dd>
|
|
1129
|
+
<dt class="optional">msg.payload</dt>
|
|
1130
|
+
<dd>API response payload.</dd>
|
|
1131
|
+
<dt class="optional">msg.topic</dt>
|
|
1132
|
+
<dd>Node name from the editor.</dd>
|
|
1133
|
+
<dt class="optional">msg.deviceName</dt>
|
|
1134
|
+
<dd>Remembered name of the selected switch or client.</dd>
|
|
1135
|
+
<dt class="optional">msg.eventName</dt>
|
|
1136
|
+
<dd>Action that produced the message.</dd>
|
|
1137
|
+
<dt class="optional">msg.details.unifiNetworkPoe</dt>
|
|
1138
|
+
<dd>Metadata with device, site, action, and port details. Includes <code>portPowerW</code> for the selected port when available.</dd>
|
|
1139
|
+
<dt class="optional">msg.payload.powerConsumptionSwitchTotal</dt>
|
|
1140
|
+
<dd>Sum of PoE power draw for all switch ports (watts) when available.</dd>
|
|
1141
|
+
<dt class="optional">msg.payload.portPowerW / msg.payload.powerChanged</dt>
|
|
1142
|
+
<dd>Available in power-consumption emit messages.</dd>
|
|
1143
|
+
</dl>
|
|
1144
|
+
<dl class="message-properties">
|
|
1145
|
+
<dt>Output 2 — error</dt>
|
|
1146
|
+
<dd>Emitted when the action fails (API error, timeout, or configuration problem). The node status also turns red. Errors are additionally forwarded to any connected <code>catch</code> node.</dd>
|
|
1147
|
+
<dt class="optional">msg.payload</dt>
|
|
1148
|
+
<dd><code>null</code></dd>
|
|
1149
|
+
<dt class="optional">msg.topic</dt>
|
|
1150
|
+
<dd>Node name from the editor.</dd>
|
|
1151
|
+
<dt class="optional">msg.error.message</dt>
|
|
1152
|
+
<dd>Human-readable error description.</dd>
|
|
1153
|
+
</dl>
|
|
1142
1154
|
<p><b>Power fields (guaranteed vs optional)</b></p>
|
|
1143
1155
|
<table style="width:100%; border-collapse:collapse;">
|
|
1144
1156
|
<thead>
|
|
@@ -1171,7 +1183,7 @@
|
|
|
1171
1183
|
</tr>
|
|
1172
1184
|
</tbody>
|
|
1173
1185
|
</table>
|
|
1174
|
-
<p>Example output:</p>
|
|
1186
|
+
<p>Example output (pin 1):</p>
|
|
1175
1187
|
<pre><code>{
|
|
1176
1188
|
"topic": "POE Soffitta Port 2",
|
|
1177
1189
|
"deviceName": "Soffitta",
|
|
@@ -1193,5 +1205,11 @@
|
|
|
1193
1205
|
"powerConsumptionSwitchTotal": 36.4
|
|
1194
1206
|
}
|
|
1195
1207
|
}
|
|
1208
|
+
}</code></pre>
|
|
1209
|
+
<p>Example output (pin 2 — error):</p>
|
|
1210
|
+
<pre><code>{
|
|
1211
|
+
"topic": "POE Soffitta Port 2",
|
|
1212
|
+
"payload": null,
|
|
1213
|
+
"error": { "message": "Missing switch device id." }
|
|
1196
1214
|
}</code></pre>
|
|
1197
1215
|
</script>
|