node-red-contrib-unifi-ultimate 1.0.1 → 1.0.2
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 +36 -6
- package/nodes/unifi-access-config.html +4 -3
- package/nodes/unifi-access-device.html +43 -5
- package/nodes/unifi-access-device.js +77 -4
- package/nodes/unifi-network-config.html +15 -5
- package/nodes/unifi-network-config.js +288 -0
- package/nodes/unifi-network-control-poe.html +2 -7
- package/nodes/unifi-network-control-poe.js +2 -1
- package/nodes/unifi-network-device.html +79 -7
- package/nodes/unifi-network-device.js +439 -19
- package/nodes/unifi-network-presence.html +2 -7
- package/nodes/unifi-network-presence.js +2 -1
- package/nodes/unifi-protect-config.html +4 -3
- package/nodes/unifi-protect-device.html +43 -5
- package/nodes/unifi-protect-device.js +77 -4
- package/nodes/utils/unifi-network-device-registry.js +95 -0
- package/package.json +1 -1
|
@@ -10,6 +10,7 @@ const {
|
|
|
10
10
|
const { extractNetworkData } = require("./utils/unifi-network-utils");
|
|
11
11
|
const UNOFFICIAL_NETWORK_STREAM_CAPABILITY = "observeUnofficialEvents";
|
|
12
12
|
const UNOFFICIAL_POLL_INTERVAL_MS = 3000;
|
|
13
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
13
14
|
|
|
14
15
|
function resolveDeviceType(configuredDeviceType) {
|
|
15
16
|
return String(configuredDeviceType || "").trim();
|
|
@@ -46,6 +47,91 @@ function resolveCapabilityConfig(configuredCapabilityConfig) {
|
|
|
46
47
|
return parseCapabilityConfig(configuredCapabilityConfig);
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
function parseBoolean(value) {
|
|
51
|
+
return value === true || value === "true" || value === 1 || value === "1";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function parseIntervalSeconds(value, fallback) {
|
|
55
|
+
const numeric = Number(value);
|
|
56
|
+
if (!Number.isFinite(numeric) || numeric < 5) {
|
|
57
|
+
return fallback;
|
|
58
|
+
}
|
|
59
|
+
return Math.trunc(numeric);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function normalizeIdentifierKey(value) {
|
|
63
|
+
return String(value || "").trim().toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isTruthyOnlineValue(value) {
|
|
67
|
+
if (value === true) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const normalized = String(value || "").trim().toLowerCase();
|
|
72
|
+
return normalized === "online" || normalized === "connected" || normalized === "active";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isFalsyOnlineValue(value) {
|
|
76
|
+
if (value === false) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const normalized = String(value || "").trim().toLowerCase();
|
|
81
|
+
return normalized === "offline" || normalized === "disconnected" || normalized === "inactive";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function resolveClientOnlineState(client) {
|
|
85
|
+
const item = client && typeof client === "object" && !Array.isArray(client)
|
|
86
|
+
? client
|
|
87
|
+
: {};
|
|
88
|
+
|
|
89
|
+
if (item.__unifiUltimateOnline === true) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (item.__unifiUltimateOnline === false) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (item.offline === true) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const negativeCandidates = [
|
|
100
|
+
item.isOnline,
|
|
101
|
+
item.online,
|
|
102
|
+
item.connected,
|
|
103
|
+
item.isConnected,
|
|
104
|
+
item.connectionState,
|
|
105
|
+
item.status,
|
|
106
|
+
item.state
|
|
107
|
+
];
|
|
108
|
+
if (negativeCandidates.some(isFalsyOnlineValue)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const positiveCandidates = [
|
|
113
|
+
item.isOnline,
|
|
114
|
+
item.online,
|
|
115
|
+
item.connected,
|
|
116
|
+
item.isConnected,
|
|
117
|
+
item.connectionState,
|
|
118
|
+
item.status,
|
|
119
|
+
item.state
|
|
120
|
+
];
|
|
121
|
+
return positiveCandidates.some(isTruthyOnlineValue);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function getFirstNonEmptyString(values) {
|
|
125
|
+
const list = Array.isArray(values) ? values : [];
|
|
126
|
+
for (const value of list) {
|
|
127
|
+
const normalized = String(value === undefined || value === null ? "" : value).trim();
|
|
128
|
+
if (normalized) {
|
|
129
|
+
return normalized;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
|
|
49
135
|
function buildNodeStatus(deviceType, payload) {
|
|
50
136
|
const normalizedType = String(deviceType || "").trim();
|
|
51
137
|
const item = payload && typeof payload === "object" ? payload : {};
|
|
@@ -481,8 +567,12 @@ module.exports = function(RED) {
|
|
|
481
567
|
node.deviceId = config.deviceId || "";
|
|
482
568
|
node.capability = config.capability || "observe";
|
|
483
569
|
node.capabilityConfig = config.capabilityConfig || "{}";
|
|
570
|
+
node.autoEmit = parseBoolean(config.autoEmit);
|
|
571
|
+
node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
|
|
572
|
+
node.autoEmitTimer = null;
|
|
573
|
+
node.autoEmitInFlight = false;
|
|
484
574
|
node.deviceName = resolveDeviceName(config.deviceName);
|
|
485
|
-
node.timeout =
|
|
575
|
+
node.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
486
576
|
node.currentDevice = null;
|
|
487
577
|
node.isObserving = false;
|
|
488
578
|
node.unofficialLastFingerprint = "";
|
|
@@ -572,7 +662,273 @@ module.exports = function(RED) {
|
|
|
572
662
|
send(outputMsg);
|
|
573
663
|
}
|
|
574
664
|
|
|
575
|
-
async function
|
|
665
|
+
async function fetchDeviceTemperatures(deviceType, deviceId, capabilityId, send) {
|
|
666
|
+
if (deviceType !== "device" || typeof node.server.fetchDeviceTemperatures !== "function") {
|
|
667
|
+
throw new Error("Switch temperature reading is only available for UniFi device resources.");
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
const result = await node.server.fetchDeviceTemperatures(deviceId);
|
|
671
|
+
const payload = result && result.payload && typeof result.payload === "object"
|
|
672
|
+
? result.payload
|
|
673
|
+
: {};
|
|
674
|
+
const sourceDevice = result && result.device && typeof result.device === "object"
|
|
675
|
+
? result.device
|
|
676
|
+
: payload;
|
|
677
|
+
node.currentDevice = sourceDevice;
|
|
678
|
+
if (payload.deviceName) {
|
|
679
|
+
node.deviceName = resolveDeviceName(payload.deviceName);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const temperatures = Array.isArray(payload.temperatures) ? payload.temperatures : [];
|
|
683
|
+
const temperatureC = Number(payload.temperatureC);
|
|
684
|
+
const maxTemperatureC = Number(payload.maxTemperatureC);
|
|
685
|
+
const outputMsg = {};
|
|
686
|
+
outputMsg.payload = Number.isFinite(temperatureC) ? temperatureC : null;
|
|
687
|
+
attachDetails(outputMsg, {
|
|
688
|
+
device: sourceDevice,
|
|
689
|
+
temperature: payload,
|
|
690
|
+
temperatureSources: result && result.sources,
|
|
691
|
+
unifiNetwork: buildBaseMetadata(deviceType, deviceId, capabilityId, {
|
|
692
|
+
source: "request",
|
|
693
|
+
temperatureC: Number.isFinite(temperatureC) ? temperatureC : undefined,
|
|
694
|
+
maxTemperatureC: Number.isFinite(maxTemperatureC) ? maxTemperatureC : undefined,
|
|
695
|
+
temperatureCount: temperatures.length,
|
|
696
|
+
hasTemperatures: temperatures.length > 0
|
|
697
|
+
})
|
|
698
|
+
});
|
|
699
|
+
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
700
|
+
|
|
701
|
+
const statusTemperatureC = Number.isFinite(temperatureC) ? temperatureC : maxTemperatureC;
|
|
702
|
+
const statusText = Number.isFinite(statusTemperatureC)
|
|
703
|
+
? `${statusTemperatureC} C`
|
|
704
|
+
: "no temperature";
|
|
705
|
+
setNodeStatus({ fill: temperatures.length > 0 ? "green" : "yellow", shape: "dot", text: statusText });
|
|
706
|
+
send(outputMsg);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
async function executeConfiguredCapabilityRequest(deviceType, deviceId, capabilityId, capabilityConfig) {
|
|
710
|
+
const execution = composeCapabilityExecution(deviceType, capabilityId, capabilityConfig);
|
|
711
|
+
const request = buildCapabilityRequest(deviceType, capabilityId, deviceId, execution.params, node.currentDevice);
|
|
712
|
+
const response = await node.server.executeNetworkRequest({
|
|
713
|
+
path: request.path,
|
|
714
|
+
method: request.method,
|
|
715
|
+
query: execution.query,
|
|
716
|
+
headers: execution.headers,
|
|
717
|
+
payload: execution.payload,
|
|
718
|
+
timeout: node.timeout
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
722
|
+
setNodeStatus({ fill: "yellow", shape: "ring", text: `${response.statusCode}` });
|
|
723
|
+
throw new Error(`UniFi Network request failed with status ${response.statusCode}`);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
return {
|
|
727
|
+
request,
|
|
728
|
+
response,
|
|
729
|
+
responseData: extractNetworkData(response.payload)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
async function fetchClientOnlineState(deviceType, deviceId, capabilityId, send) {
|
|
734
|
+
if (deviceType !== "client") {
|
|
735
|
+
throw new Error("Client online state is only available for Client resources.");
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
const scoped = resolveScopedIdentifiers(deviceType, deviceId);
|
|
739
|
+
let client = await node.server.fetchDeviceByTypeAndId("client", deviceId);
|
|
740
|
+
let selectedFromList = null;
|
|
741
|
+
try {
|
|
742
|
+
const clients = await node.server.fetchDevices("client");
|
|
743
|
+
const selectedKeys = new Set([
|
|
744
|
+
scoped.resourceId,
|
|
745
|
+
client && client.id,
|
|
746
|
+
client && client.clientId,
|
|
747
|
+
client && client.macAddress,
|
|
748
|
+
client && client.mac
|
|
749
|
+
].map(normalizeIdentifierKey).filter(Boolean));
|
|
750
|
+
selectedFromList = Array.isArray(clients)
|
|
751
|
+
? clients.find((candidate) => {
|
|
752
|
+
const candidateScoped = resolveScopedIdentifiers("client", "", candidate);
|
|
753
|
+
if (scoped.siteId && candidateScoped.siteId && scoped.siteId !== candidateScoped.siteId) {
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
return [
|
|
757
|
+
candidateScoped.resourceId,
|
|
758
|
+
candidate && candidate.id,
|
|
759
|
+
candidate && candidate.clientId,
|
|
760
|
+
candidate && candidate.macAddress,
|
|
761
|
+
candidate && candidate.mac
|
|
762
|
+
].some((value) => selectedKeys.has(normalizeIdentifierKey(value)));
|
|
763
|
+
}) || null
|
|
764
|
+
: null;
|
|
765
|
+
} catch (error) {
|
|
766
|
+
selectedFromList = null;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
client = {
|
|
770
|
+
...(client && typeof client === "object" && !Array.isArray(client) ? client : {}),
|
|
771
|
+
...(selectedFromList && typeof selectedFromList === "object" && !Array.isArray(selectedFromList) ? selectedFromList : {})
|
|
772
|
+
};
|
|
773
|
+
node.currentDevice = client;
|
|
774
|
+
|
|
775
|
+
const online = resolveClientOnlineState(client);
|
|
776
|
+
const outputMsg = {};
|
|
777
|
+
outputMsg.payload = online;
|
|
778
|
+
attachDetails(outputMsg, {
|
|
779
|
+
client,
|
|
780
|
+
unifiNetwork: buildBaseMetadata(deviceType, deviceId, capabilityId, {
|
|
781
|
+
source: "request",
|
|
782
|
+
online
|
|
783
|
+
})
|
|
784
|
+
});
|
|
785
|
+
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
786
|
+
|
|
787
|
+
setNodeStatus({ fill: online ? "green" : "yellow", shape: "dot", text: online ? "online" : "offline" });
|
|
788
|
+
send(outputMsg);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
async function countOnlineClients(deviceType, deviceId, capabilityId, send) {
|
|
792
|
+
if (deviceType !== "site") {
|
|
793
|
+
throw new Error("Client count is only available for Site resources.");
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
const site = await node.server.fetchDeviceByTypeAndId("site", deviceId);
|
|
797
|
+
node.currentDevice = site;
|
|
798
|
+
const scoped = resolveScopedIdentifiers("site", deviceId, site);
|
|
799
|
+
const clients = await node.server.fetchDevices("client");
|
|
800
|
+
const siteClients = Array.isArray(clients)
|
|
801
|
+
? clients.filter((client) => {
|
|
802
|
+
const clientScoped = resolveScopedIdentifiers("client", "", client);
|
|
803
|
+
return !scoped.siteId || clientScoped.siteId === scoped.siteId;
|
|
804
|
+
})
|
|
805
|
+
: [];
|
|
806
|
+
const onlineClients = siteClients.filter(resolveClientOnlineState);
|
|
807
|
+
|
|
808
|
+
const outputMsg = {};
|
|
809
|
+
outputMsg.payload = onlineClients.length;
|
|
810
|
+
attachDetails(outputMsg, {
|
|
811
|
+
site,
|
|
812
|
+
clients: siteClients,
|
|
813
|
+
onlineClients,
|
|
814
|
+
unifiNetwork: buildBaseMetadata(deviceType, deviceId, capabilityId, {
|
|
815
|
+
source: "request",
|
|
816
|
+
onlineClientCount: onlineClients.length,
|
|
817
|
+
clientCount: siteClients.length
|
|
818
|
+
})
|
|
819
|
+
});
|
|
820
|
+
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
821
|
+
|
|
822
|
+
setNodeStatus({ fill: "green", shape: "dot", text: `${onlineClients.length} online` });
|
|
823
|
+
send(outputMsg);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
async function fetchDeviceSimpleStatistic(deviceType, deviceId, capabilityId, capabilityConfig, send) {
|
|
827
|
+
if (deviceType !== "device") {
|
|
828
|
+
throw new Error("Device statistics are only available for UniFi Device resources.");
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
const selectedDevice = await node.server.fetchDeviceByTypeAndId("device", deviceId);
|
|
832
|
+
node.currentDevice = selectedDevice;
|
|
833
|
+
const { request, response, responseData } = await executeConfiguredCapabilityRequest(deviceType, deviceId, capabilityId, capabilityConfig);
|
|
834
|
+
const statistics = responseData && typeof responseData === "object" && !Array.isArray(responseData)
|
|
835
|
+
? responseData
|
|
836
|
+
: {};
|
|
837
|
+
const metricMap = {
|
|
838
|
+
readDeviceCpu: {
|
|
839
|
+
key: "cpuUtilizationPct",
|
|
840
|
+
label: "CPU",
|
|
841
|
+
unit: "%"
|
|
842
|
+
},
|
|
843
|
+
readDeviceMemory: {
|
|
844
|
+
key: "memoryUtilizationPct",
|
|
845
|
+
label: "Memory",
|
|
846
|
+
unit: "%"
|
|
847
|
+
},
|
|
848
|
+
readDeviceUptime: {
|
|
849
|
+
key: "uptimeSec",
|
|
850
|
+
label: "Uptime",
|
|
851
|
+
unit: "s"
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
const metric = metricMap[capabilityId];
|
|
855
|
+
const value = Number(statistics[metric.key]);
|
|
856
|
+
const payloadValue = Number.isFinite(value) ? value : null;
|
|
857
|
+
|
|
858
|
+
const outputMsg = {};
|
|
859
|
+
outputMsg.payload = payloadValue;
|
|
860
|
+
attachDetails(outputMsg, {
|
|
861
|
+
device: selectedDevice,
|
|
862
|
+
statistics,
|
|
863
|
+
response: {
|
|
864
|
+
statusCode: response.statusCode,
|
|
865
|
+
headers: response.headers,
|
|
866
|
+
method: request.method,
|
|
867
|
+
path: request.path
|
|
868
|
+
},
|
|
869
|
+
unifiNetwork: buildBaseMetadata(deviceType, deviceId, capabilityId, {
|
|
870
|
+
source: "request",
|
|
871
|
+
method: request.method,
|
|
872
|
+
path: request.path,
|
|
873
|
+
value: payloadValue
|
|
874
|
+
})
|
|
875
|
+
});
|
|
876
|
+
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
877
|
+
|
|
878
|
+
const statusText = payloadValue === null
|
|
879
|
+
? `${metric.label} n/a`
|
|
880
|
+
: `${metric.label} ${payloadValue}${metric.unit}`;
|
|
881
|
+
setNodeStatus({ fill: payloadValue === null ? "yellow" : "green", shape: "dot", text: statusText });
|
|
882
|
+
send(outputMsg);
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
async function createGuestVoucher(deviceType, deviceId, capabilityId, capabilityConfig, send) {
|
|
886
|
+
if (deviceType !== "site") {
|
|
887
|
+
throw new Error("Guest vouchers are only available for Site resources.");
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
const site = await node.server.fetchDeviceByTypeAndId("site", deviceId);
|
|
891
|
+
node.currentDevice = site;
|
|
892
|
+
const { request, response, responseData } = await executeConfiguredCapabilityRequest(deviceType, deviceId, capabilityId, capabilityConfig);
|
|
893
|
+
const vouchers = Array.isArray(responseData && responseData.vouchers)
|
|
894
|
+
? responseData.vouchers
|
|
895
|
+
: Array.isArray(responseData)
|
|
896
|
+
? responseData
|
|
897
|
+
: [];
|
|
898
|
+
const codes = vouchers
|
|
899
|
+
.map((voucher) => getFirstNonEmptyString([
|
|
900
|
+
voucher && voucher.code,
|
|
901
|
+
voucher && voucher.voucherCode,
|
|
902
|
+
voucher && voucher.id
|
|
903
|
+
]))
|
|
904
|
+
.filter(Boolean);
|
|
905
|
+
|
|
906
|
+
const outputMsg = {};
|
|
907
|
+
outputMsg.payload = codes.length <= 1 ? (codes[0] || null) : codes;
|
|
908
|
+
attachDetails(outputMsg, {
|
|
909
|
+
site,
|
|
910
|
+
vouchers,
|
|
911
|
+
voucherCodes: codes,
|
|
912
|
+
response: {
|
|
913
|
+
statusCode: response.statusCode,
|
|
914
|
+
headers: response.headers,
|
|
915
|
+
method: request.method,
|
|
916
|
+
path: request.path
|
|
917
|
+
},
|
|
918
|
+
unifiNetwork: buildBaseMetadata(deviceType, deviceId, capabilityId, {
|
|
919
|
+
source: "request",
|
|
920
|
+
method: request.method,
|
|
921
|
+
path: request.path,
|
|
922
|
+
voucherCount: vouchers.length
|
|
923
|
+
})
|
|
924
|
+
});
|
|
925
|
+
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
926
|
+
|
|
927
|
+
setNodeStatus({ fill: vouchers.length > 0 ? "green" : "yellow", shape: "dot", text: `${vouchers.length} voucher` });
|
|
928
|
+
send(outputMsg);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
async function invokeCapability(send, triggerSource) {
|
|
576
932
|
if (!node.server) {
|
|
577
933
|
throw new Error("Unifi Network configuration is missing.");
|
|
578
934
|
}
|
|
@@ -601,31 +957,38 @@ module.exports = function(RED) {
|
|
|
601
957
|
deviceId,
|
|
602
958
|
capabilityId,
|
|
603
959
|
send,
|
|
604
|
-
capability.mode === "fetch" ? "fetch" : "manual-refresh"
|
|
960
|
+
triggerSource || (capability.mode === "fetch" ? "fetch" : "manual-refresh")
|
|
605
961
|
);
|
|
606
962
|
return;
|
|
607
963
|
}
|
|
608
964
|
|
|
609
|
-
|
|
610
|
-
|
|
965
|
+
if (capabilityId === "readSwitchTemperatures") {
|
|
966
|
+
await fetchDeviceTemperatures(deviceType, deviceId, capabilityId, send);
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
611
969
|
|
|
612
|
-
|
|
970
|
+
if (capabilityId === "isClientOnline") {
|
|
971
|
+
await fetchClientOnlineState(deviceType, deviceId, capabilityId, send);
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
613
974
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
headers: execution.headers,
|
|
619
|
-
payload: execution.payload,
|
|
620
|
-
timeout: node.timeout
|
|
621
|
-
});
|
|
975
|
+
if (capabilityId === "countOnlineClients") {
|
|
976
|
+
await countOnlineClients(deviceType, deviceId, capabilityId, send);
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
622
979
|
|
|
623
|
-
if (
|
|
624
|
-
|
|
625
|
-
|
|
980
|
+
if (capabilityId === "readDeviceCpu" || capabilityId === "readDeviceMemory" || capabilityId === "readDeviceUptime") {
|
|
981
|
+
await fetchDeviceSimpleStatistic(deviceType, deviceId, capabilityId, capabilityConfig, send);
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
if (capabilityId === "createGuestVoucher") {
|
|
986
|
+
await createGuestVoucher(deviceType, deviceId, capabilityId, capabilityConfig, send);
|
|
987
|
+
return;
|
|
626
988
|
}
|
|
627
989
|
|
|
628
|
-
|
|
990
|
+
setNodeStatus({ fill: "blue", shape: "dot", text: `${capability.label}` });
|
|
991
|
+
const { request, response, responseData } = await executeConfiguredCapabilityRequest(deviceType, deviceId, capabilityId, capabilityConfig);
|
|
629
992
|
if (responseData && typeof responseData === "object" && !Array.isArray(responseData)) {
|
|
630
993
|
node.currentDevice = responseData;
|
|
631
994
|
}
|
|
@@ -665,6 +1028,15 @@ module.exports = function(RED) {
|
|
|
665
1028
|
return Boolean(capability && capability.opensEventStream === true);
|
|
666
1029
|
}
|
|
667
1030
|
|
|
1031
|
+
function configuredCapabilitySupportsAutoEmit() {
|
|
1032
|
+
const capability = resolveConfiguredCapabilityDefinition();
|
|
1033
|
+
return Boolean(
|
|
1034
|
+
capability
|
|
1035
|
+
&& capability.opensEventStream !== true
|
|
1036
|
+
&& String(capability.method || "").trim().toUpperCase() !== "POST"
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
668
1040
|
function isUnofficialNetworkStreamCapabilitySelected() {
|
|
669
1041
|
return resolveCapabilityId(node.capability) === UNOFFICIAL_NETWORK_STREAM_CAPABILITY;
|
|
670
1042
|
}
|
|
@@ -717,13 +1089,58 @@ module.exports = function(RED) {
|
|
|
717
1089
|
refreshUnofficialPollingFallback();
|
|
718
1090
|
}
|
|
719
1091
|
|
|
1092
|
+
function stopAutoEmitTimer() {
|
|
1093
|
+
if (node.autoEmitTimer) {
|
|
1094
|
+
clearInterval(node.autoEmitTimer);
|
|
1095
|
+
node.autoEmitTimer = null;
|
|
1096
|
+
}
|
|
1097
|
+
node.autoEmitInFlight = false;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
function shouldStartAutoEmitTimer() {
|
|
1101
|
+
return Boolean(
|
|
1102
|
+
node.server
|
|
1103
|
+
&& node.autoEmit
|
|
1104
|
+
&& node.deviceType
|
|
1105
|
+
&& node.deviceId
|
|
1106
|
+
&& configuredCapabilitySupportsAutoEmit()
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
function runAutoEmit() {
|
|
1111
|
+
if (node.autoEmitInFlight || !shouldStartAutoEmitTimer()) {
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
node.autoEmitInFlight = true;
|
|
1116
|
+
invokeCapability(node.send.bind(node), "interval")
|
|
1117
|
+
.catch((error) => {
|
|
1118
|
+
setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
|
|
1119
|
+
node.error(error);
|
|
1120
|
+
})
|
|
1121
|
+
.finally(() => {
|
|
1122
|
+
node.autoEmitInFlight = false;
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
function startAutoEmitTimer() {
|
|
1127
|
+
stopAutoEmitTimer();
|
|
1128
|
+
if (!shouldStartAutoEmitTimer()) {
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const intervalMs = node.autoEmitIntervalSeconds * 1000;
|
|
1133
|
+
node.autoEmitTimer = setInterval(runAutoEmit, intervalMs);
|
|
1134
|
+
setNodeStatus({ fill: "grey", shape: "ring", text: `every ${node.autoEmitIntervalSeconds}s` });
|
|
1135
|
+
}
|
|
1136
|
+
|
|
720
1137
|
node.on("input", async function(_msg, send, done) {
|
|
721
1138
|
send = send || function() {
|
|
722
1139
|
node.send.apply(node, arguments);
|
|
723
1140
|
};
|
|
724
1141
|
|
|
725
1142
|
try {
|
|
726
|
-
await invokeCapability(send);
|
|
1143
|
+
await invokeCapability(send, "manual-refresh");
|
|
727
1144
|
if (typeof done === "function") {
|
|
728
1145
|
done();
|
|
729
1146
|
}
|
|
@@ -865,6 +1282,8 @@ module.exports = function(RED) {
|
|
|
865
1282
|
startAutoReceive();
|
|
866
1283
|
} else if (configuredCapabilityOpensEventStream() && (!node.deviceType || !node.deviceId)) {
|
|
867
1284
|
setNodeStatus({ fill: "grey", shape: "ring", text: "set device" });
|
|
1285
|
+
} else if (shouldStartAutoEmitTimer()) {
|
|
1286
|
+
startAutoEmitTimer();
|
|
868
1287
|
} else {
|
|
869
1288
|
setNodeStatus({ fill: "grey", shape: "ring", text: "ready" });
|
|
870
1289
|
}
|
|
@@ -872,6 +1291,7 @@ module.exports = function(RED) {
|
|
|
872
1291
|
node.on("close", function(done) {
|
|
873
1292
|
try {
|
|
874
1293
|
node.isObserving = false;
|
|
1294
|
+
stopAutoEmitTimer();
|
|
875
1295
|
if (node.server && typeof node.server.removeClient === "function") {
|
|
876
1296
|
node.server.removeClient(node);
|
|
877
1297
|
}
|
|
@@ -12,8 +12,7 @@
|
|
|
12
12
|
deviceName: { value: "" },
|
|
13
13
|
pollInterval: { value: 10, required: true, validate: RED.validators.number() },
|
|
14
14
|
onlineHysteresis: { value: 15, required: true, validate: RED.validators.number() },
|
|
15
|
-
offlineHysteresis: { value: 30, required: true, validate: RED.validators.number() }
|
|
16
|
-
timeout: { value: 8000, required: true, validate: RED.validators.number() }
|
|
15
|
+
offlineHysteresis: { value: 30, required: true, validate: RED.validators.number() }
|
|
17
16
|
},
|
|
18
17
|
inputs: 0,
|
|
19
18
|
outputs: 1,
|
|
@@ -513,10 +512,6 @@
|
|
|
513
512
|
<label for="node-input-offlineHysteresis"><i class="fa fa-hourglass-half"></i> Hysteresis (s)</label>
|
|
514
513
|
<input type="text" id="node-input-offlineHysteresis" placeholder="30">
|
|
515
514
|
</div>
|
|
516
|
-
<div class="form-row">
|
|
517
|
-
<label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout</label>
|
|
518
|
-
<input type="text" id="node-input-timeout" placeholder="8000">
|
|
519
|
-
</div>
|
|
520
515
|
<input type="hidden" id="node-input-deviceName">
|
|
521
516
|
</div>
|
|
522
517
|
</script>
|
|
@@ -532,8 +527,8 @@
|
|
|
532
527
|
<p><b>Poll (s)</b> sets this node cadence for presence checks. Scheduling and fetch execution are handled by the shared Network config node.</p>
|
|
533
528
|
<p><b>Connect Hysteresis (s)</b> delays the <code>true</code> output so brief reconnects do not flap.</p>
|
|
534
529
|
<p><b>Hysteresis (s)</b> delays the <code>false</code> output to avoid flapping when the client disconnects briefly.</p>
|
|
530
|
+
<p>Request timeout is handled automatically by the node.</p>
|
|
535
531
|
<p><b>Name</b> is the node name shown in the editor and is updated only when you manually select a different client from the list.</p>
|
|
536
|
-
<p><b>Timeout</b> is the HTTP timeout for each check.</p>
|
|
537
532
|
|
|
538
533
|
<h3>Input</h3>
|
|
539
534
|
<ul>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { decodeScopedDeviceId } = require("./utils/unifi-network-device-registry");
|
|
4
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 8000;
|
|
4
5
|
|
|
5
6
|
function parsePositiveSeconds(value, fallbackValue) {
|
|
6
7
|
const numeric = Number(value);
|
|
@@ -87,7 +88,7 @@ module.exports = function(RED) {
|
|
|
87
88
|
node.pollIntervalSeconds = parsePositiveSeconds(config.pollInterval, 10);
|
|
88
89
|
node.onlineHysteresisSeconds = parseNonNegativeSeconds(config.onlineHysteresis, 15);
|
|
89
90
|
node.offlineHysteresisSeconds = parseNonNegativeSeconds(config.offlineHysteresis, 30);
|
|
90
|
-
node.timeout =
|
|
91
|
+
node.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
91
92
|
node.deviceName = resolveDeviceName(config.deviceName);
|
|
92
93
|
|
|
93
94
|
node.isObserving = false;
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
return this.name || this.host || "Unifi Protect Config";
|
|
18
18
|
},
|
|
19
19
|
oneditprepare: function () {
|
|
20
|
-
const
|
|
20
|
+
const node = this;
|
|
21
|
+
const $hostInput = $("#node-config-input-host");
|
|
21
22
|
const $apiKey = $("#node-config-input-apiKey");
|
|
22
23
|
const $rejectUnauthorized = $("#node-config-input-rejectUnauthorized");
|
|
23
24
|
const $apiKeyRow = $apiKey.closest(".form-row");
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
const $advancedRow = $("#node-config-row-advanced");
|
|
26
27
|
|
|
27
28
|
function syncProgressiveRows() {
|
|
28
|
-
const hasHost = String($
|
|
29
|
+
const hasHost = String($hostInput.val() || node.host || "").trim() !== "";
|
|
29
30
|
$apiKeyRow.toggle(hasHost);
|
|
30
31
|
$securityRow.toggle(hasHost);
|
|
31
32
|
$advancedRow.toggle(hasHost);
|
|
@@ -40,7 +41,7 @@
|
|
|
40
41
|
|
|
41
42
|
syncProgressiveRows();
|
|
42
43
|
|
|
43
|
-
$
|
|
44
|
+
$hostInput.on("change input", function () {
|
|
44
45
|
syncProgressiveRows();
|
|
45
46
|
});
|
|
46
47
|
|