node-red-contrib-unifi-ultimate 1.1.18 → 1.1.19
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.
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { registerConnectionCheck } = require("./utils/unifi-connection-check");
|
|
4
4
|
const { actionGuidance } = require("./utils/unifi-action-guidance");
|
|
5
|
+
const { attachHeartbeat, stopHeartbeat, createReconnectBackoff } = require("./utils/unifi-websocket-resilience");
|
|
5
6
|
|
|
6
7
|
const { installSimpleMonitorScheduler } = require("./utils/unifi-simple-monitor");
|
|
7
8
|
|
|
@@ -82,6 +83,7 @@ module.exports = function(RED) {
|
|
|
82
83
|
node.nodeClients = [];
|
|
83
84
|
node.wsNotifications = null;
|
|
84
85
|
node.reconnectTimer = null;
|
|
86
|
+
node.reconnectBackoff = createReconnectBackoff();
|
|
85
87
|
node.isClosing = false;
|
|
86
88
|
node.activeDoorbells = new Map();
|
|
87
89
|
node.activeDoorbellRequests = new Map();
|
|
@@ -652,8 +654,8 @@ module.exports = function(RED) {
|
|
|
652
654
|
return;
|
|
653
655
|
}
|
|
654
656
|
|
|
655
|
-
//
|
|
656
|
-
//
|
|
657
|
+
// Back off exponentially to avoid a busy loop while the controller
|
|
658
|
+
// is restarting or upgrading.
|
|
657
659
|
node.reconnectTimer = setTimeout(() => {
|
|
658
660
|
node.reconnectTimer = null;
|
|
659
661
|
try {
|
|
@@ -661,7 +663,7 @@ module.exports = function(RED) {
|
|
|
661
663
|
} catch (error) {
|
|
662
664
|
node.warn(`Access websocket reconnect failed: ${error && error.message ? error.message : error}`);
|
|
663
665
|
}
|
|
664
|
-
},
|
|
666
|
+
}, node.reconnectBackoff.nextDelay());
|
|
665
667
|
};
|
|
666
668
|
|
|
667
669
|
node.ensureWebSocket = () => {
|
|
@@ -698,6 +700,11 @@ module.exports = function(RED) {
|
|
|
698
700
|
return;
|
|
699
701
|
}
|
|
700
702
|
|
|
703
|
+
ws.on("open", () => {
|
|
704
|
+
node.reconnectBackoff.reset();
|
|
705
|
+
});
|
|
706
|
+
attachHeartbeat(ws);
|
|
707
|
+
|
|
701
708
|
ws.on("message", (rawData) => {
|
|
702
709
|
try {
|
|
703
710
|
// Ignore malformed websocket frames rather than killing the
|
|
@@ -712,10 +719,12 @@ module.exports = function(RED) {
|
|
|
712
719
|
|
|
713
720
|
ws.on("close", () => {
|
|
714
721
|
try {
|
|
722
|
+
// A socket closed on purpose has already been detached and
|
|
723
|
+
// must not schedule a reconnect.
|
|
715
724
|
if (node.wsNotifications === ws) {
|
|
716
725
|
node.wsNotifications = null;
|
|
726
|
+
node.scheduleReconnect();
|
|
717
727
|
}
|
|
718
|
-
node.scheduleReconnect();
|
|
719
728
|
} catch (error) {
|
|
720
729
|
}
|
|
721
730
|
});
|
|
@@ -736,12 +745,15 @@ module.exports = function(RED) {
|
|
|
736
745
|
node.reconnectTimer = null;
|
|
737
746
|
}
|
|
738
747
|
|
|
739
|
-
|
|
748
|
+
const socket = node.wsNotifications;
|
|
749
|
+
if (socket) {
|
|
750
|
+
// Detach first so the close callback does not reconnect.
|
|
751
|
+
node.wsNotifications = null;
|
|
752
|
+
stopHeartbeat(socket);
|
|
740
753
|
try {
|
|
741
|
-
|
|
754
|
+
socket.close();
|
|
742
755
|
} catch (error) {
|
|
743
756
|
}
|
|
744
|
-
node.wsNotifications = null;
|
|
745
757
|
}
|
|
746
758
|
};
|
|
747
759
|
|
|
@@ -5,6 +5,7 @@ const { actionGuidance } = require("./utils/unifi-action-guidance");
|
|
|
5
5
|
|
|
6
6
|
const { installNetworkMonitorReads } = require("./utils/unifi-network-monitor");
|
|
7
7
|
const { installSimpleMonitorScheduler } = require("./utils/unifi-simple-monitor");
|
|
8
|
+
const { attachHeartbeat, stopHeartbeat, createReconnectBackoff } = require("./utils/unifi-websocket-resilience");
|
|
8
9
|
|
|
9
10
|
const fs = require("fs");
|
|
10
11
|
const os = require("os");
|
|
@@ -44,6 +45,9 @@ module.exports = function(RED) {
|
|
|
44
45
|
const POWER_OBSERVER_SCHEDULER_TICK_MS = 1000;
|
|
45
46
|
const PORT_OVERRIDE_CACHE_VERSION = 1;
|
|
46
47
|
const PORT_OVERRIDE_CACHE_SAVE_DEBOUNCE_MS = 1000;
|
|
48
|
+
// Short name of the site used by the legacy event stream when a subscriber
|
|
49
|
+
// has no resolvable site.
|
|
50
|
+
const UNOFFICIAL_NETWORK_DEFAULT_SITE = "default";
|
|
47
51
|
// Sentinel stored for a port that was enabled but had no override at all, so
|
|
48
52
|
// re-enabling knows to remove the override we added (instead of restoring one).
|
|
49
53
|
const PORT_OVERRIDE_NONE = { __noOverride: true };
|
|
@@ -122,8 +126,12 @@ module.exports = function(RED) {
|
|
|
122
126
|
node.rejectUnauthorized = config.rejectUnauthorized === true || config.rejectUnauthorized === "true";
|
|
123
127
|
node.powerObservationIntervalSeconds = normalizePowerObserverIntervalSeconds(config.powerObservationIntervalSeconds);
|
|
124
128
|
node.nodeClients = [];
|
|
125
|
-
|
|
129
|
+
// Legacy site name -> socket; the unofficial stream is scoped per site.
|
|
130
|
+
node.wsNetworkEvents = new Map();
|
|
131
|
+
node.unofficialNetworkSiteNames = new Map();
|
|
132
|
+
node.unofficialNetworkSync = Promise.resolve();
|
|
126
133
|
node.reconnectTimer = null;
|
|
134
|
+
node.reconnectBackoff = createReconnectBackoff();
|
|
127
135
|
node.isClosing = false;
|
|
128
136
|
node.powerObserverNodeState = new Map();
|
|
129
137
|
node.powerObserverTimer = null;
|
|
@@ -838,7 +846,7 @@ module.exports = function(RED) {
|
|
|
838
846
|
return getCapabilitiesForType(deviceType, selectedDevice);
|
|
839
847
|
};
|
|
840
848
|
|
|
841
|
-
node.buildUnofficialNetworkWebSocketUrl = () => {
|
|
849
|
+
node.buildUnofficialNetworkWebSocketUrl = (siteName = UNOFFICIAL_NETWORK_DEFAULT_SITE) => {
|
|
842
850
|
const normalizedHost = applyPortToHost(
|
|
843
851
|
String(node.host || "")
|
|
844
852
|
.trim()
|
|
@@ -850,30 +858,58 @@ module.exports = function(RED) {
|
|
|
850
858
|
throw new Error("The configured host is empty or invalid.");
|
|
851
859
|
}
|
|
852
860
|
|
|
853
|
-
const
|
|
861
|
+
const normalizedSiteName = normalizeString(siteName) || UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
862
|
+
const url = new URL(`https://${normalizedHost}/proxy/network/wss/s/${encodeURIComponent(normalizedSiteName)}/events`);
|
|
854
863
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
855
864
|
return url.toString();
|
|
856
865
|
};
|
|
857
866
|
|
|
858
|
-
|
|
867
|
+
const isUnofficialNetworkStreamSubscriber = (client) => {
|
|
859
868
|
try {
|
|
860
|
-
return client && typeof client.shouldReceiveUnofficialNetworkEvents === "function" && client.shouldReceiveUnofficialNetworkEvents();
|
|
869
|
+
return Boolean(client && typeof client.shouldReceiveUnofficialNetworkEvents === "function" && client.shouldReceiveUnofficialNetworkEvents());
|
|
861
870
|
} catch (error) {
|
|
862
871
|
return false;
|
|
863
872
|
}
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
const getUnofficialNetworkSubscriberSiteId = (client) => {
|
|
876
|
+
try {
|
|
877
|
+
return client && typeof client.getUnofficialNetworkSiteId === "function"
|
|
878
|
+
? normalizeString(client.getUnofficialNetworkSiteId())
|
|
879
|
+
: "";
|
|
880
|
+
} catch (error) {
|
|
881
|
+
return "";
|
|
882
|
+
}
|
|
883
|
+
};
|
|
870
884
|
|
|
871
|
-
|
|
885
|
+
// The legacy event stream is scoped to one site by its short name, so
|
|
886
|
+
// a subscriber without a known site falls back to the default site.
|
|
887
|
+
const getUnofficialNetworkSubscriberSiteName = (client) => {
|
|
888
|
+
const siteId = getUnofficialNetworkSubscriberSiteId(client);
|
|
889
|
+
if (!siteId) {
|
|
890
|
+
return UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
891
|
+
}
|
|
892
|
+
return node.unofficialNetworkSiteNames.get(siteId) || UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
node.hasUnofficialNetworkStreamSubscribers = () => node.nodeClients.some(isUnofficialNetworkStreamSubscriber);
|
|
896
|
+
node.isUnofficialNetworkWebSocketConnected = () => node.wsNetworkEvents.size > 0
|
|
897
|
+
&& Array.from(node.wsNetworkEvents.values()).every((socket) => (
|
|
898
|
+
socket
|
|
899
|
+
&& typeof socket.readyState === "number"
|
|
900
|
+
&& socket.readyState === 1
|
|
901
|
+
));
|
|
902
|
+
|
|
903
|
+
node.broadcastUnofficialNetworkEvent = (eventPayload, siteName) => {
|
|
872
904
|
node.nodeClients.forEach((client) => {
|
|
873
905
|
try {
|
|
874
|
-
if (client
|
|
875
|
-
|
|
906
|
+
if (!client || typeof client.handleNetworkEventUpdate !== "function") {
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
if (siteName && getUnofficialNetworkSubscriberSiteName(client) !== siteName) {
|
|
910
|
+
return;
|
|
876
911
|
}
|
|
912
|
+
client.handleNetworkEventUpdate(eventPayload);
|
|
877
913
|
} catch (error) {
|
|
878
914
|
}
|
|
879
915
|
});
|
|
@@ -886,19 +922,36 @@ module.exports = function(RED) {
|
|
|
886
922
|
|
|
887
923
|
node.reconnectTimer = setTimeout(() => {
|
|
888
924
|
node.reconnectTimer = null;
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
} catch (error) {
|
|
892
|
-
node.warn(`Network websocket reconnect failed: ${error && error.message ? error.message : error}`);
|
|
893
|
-
}
|
|
894
|
-
}, 5000);
|
|
925
|
+
node.ensureUnofficialNetworkWebSocket();
|
|
926
|
+
}, node.reconnectBackoff.nextDelay());
|
|
895
927
|
};
|
|
896
928
|
|
|
897
|
-
|
|
898
|
-
if (
|
|
899
|
-
return;
|
|
929
|
+
async function resolveUnofficialNetworkSiteName(siteId) {
|
|
930
|
+
if (!siteId) {
|
|
931
|
+
return UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
932
|
+
}
|
|
933
|
+
if (node.unofficialNetworkSiteNames.has(siteId)) {
|
|
934
|
+
return node.unofficialNetworkSiteNames.get(siteId);
|
|
900
935
|
}
|
|
901
936
|
|
|
937
|
+
let siteName = "";
|
|
938
|
+
try {
|
|
939
|
+
siteName = await fetchLegacySiteName(siteId);
|
|
940
|
+
} catch (error) {
|
|
941
|
+
// Not cached, so the next reconnect or subscriber retries.
|
|
942
|
+
node.warn(`Unable to resolve UniFi Network site '${siteId}' for the event stream, using '${UNOFFICIAL_NETWORK_DEFAULT_SITE}': ${error && error.message ? error.message : error}`);
|
|
943
|
+
return UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
if (!siteName) {
|
|
947
|
+
node.warn(`UniFi Network site '${siteId}' was not found for the event stream, using '${UNOFFICIAL_NETWORK_DEFAULT_SITE}'.`);
|
|
948
|
+
siteName = UNOFFICIAL_NETWORK_DEFAULT_SITE;
|
|
949
|
+
}
|
|
950
|
+
node.unofficialNetworkSiteNames.set(siteId, siteName);
|
|
951
|
+
return siteName;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
function openUnofficialNetworkSiteSocket(siteName) {
|
|
902
955
|
let WebSocket;
|
|
903
956
|
const apiKey = String(node.getApiKey() || "").trim();
|
|
904
957
|
if (!apiKey) {
|
|
@@ -914,7 +967,7 @@ module.exports = function(RED) {
|
|
|
914
967
|
|
|
915
968
|
let ws;
|
|
916
969
|
try {
|
|
917
|
-
ws = new WebSocket(node.buildUnofficialNetworkWebSocketUrl(), {
|
|
970
|
+
ws = new WebSocket(node.buildUnofficialNetworkWebSocketUrl(siteName), {
|
|
918
971
|
headers: buildRequestHeaders(apiKey),
|
|
919
972
|
rejectUnauthorized: node.rejectUnauthorized
|
|
920
973
|
});
|
|
@@ -924,6 +977,11 @@ module.exports = function(RED) {
|
|
|
924
977
|
return;
|
|
925
978
|
}
|
|
926
979
|
|
|
980
|
+
ws.on("open", () => {
|
|
981
|
+
node.reconnectBackoff.reset();
|
|
982
|
+
});
|
|
983
|
+
attachHeartbeat(ws);
|
|
984
|
+
|
|
927
985
|
ws.on("message", (rawData) => {
|
|
928
986
|
try {
|
|
929
987
|
const text = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : String(rawData);
|
|
@@ -931,9 +989,9 @@ module.exports = function(RED) {
|
|
|
931
989
|
return;
|
|
932
990
|
}
|
|
933
991
|
try {
|
|
934
|
-
node.broadcastUnofficialNetworkEvent(JSON.parse(text));
|
|
992
|
+
node.broadcastUnofficialNetworkEvent(JSON.parse(text), siteName);
|
|
935
993
|
} catch (error) {
|
|
936
|
-
node.broadcastUnofficialNetworkEvent({ raw: text });
|
|
994
|
+
node.broadcastUnofficialNetworkEvent({ raw: text }, siteName);
|
|
937
995
|
}
|
|
938
996
|
} catch (error) {
|
|
939
997
|
}
|
|
@@ -941,10 +999,12 @@ module.exports = function(RED) {
|
|
|
941
999
|
|
|
942
1000
|
ws.on("close", () => {
|
|
943
1001
|
try {
|
|
944
|
-
|
|
945
|
-
|
|
1002
|
+
// A socket closed on purpose has already been detached and
|
|
1003
|
+
// must not schedule a reconnect.
|
|
1004
|
+
if (node.wsNetworkEvents.get(siteName) === ws) {
|
|
1005
|
+
node.wsNetworkEvents.delete(siteName);
|
|
1006
|
+
node.scheduleNetworkReconnect();
|
|
946
1007
|
}
|
|
947
|
-
node.scheduleNetworkReconnect();
|
|
948
1008
|
} catch (error) {
|
|
949
1009
|
}
|
|
950
1010
|
});
|
|
@@ -956,7 +1016,58 @@ module.exports = function(RED) {
|
|
|
956
1016
|
}
|
|
957
1017
|
});
|
|
958
1018
|
|
|
959
|
-
node.wsNetworkEvents
|
|
1019
|
+
node.wsNetworkEvents.set(siteName, ws);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
function closeUnofficialNetworkSiteSocket(siteName) {
|
|
1023
|
+
const socket = node.wsNetworkEvents.get(siteName);
|
|
1024
|
+
if (!socket) {
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
node.wsNetworkEvents.delete(siteName);
|
|
1028
|
+
stopHeartbeat(socket);
|
|
1029
|
+
try {
|
|
1030
|
+
socket.close();
|
|
1031
|
+
} catch (error) {
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
async function syncUnofficialNetworkWebSockets() {
|
|
1036
|
+
if (node.isClosing) {
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
const siteIds = new Set(node.nodeClients
|
|
1041
|
+
.filter(isUnofficialNetworkStreamSubscriber)
|
|
1042
|
+
.map(getUnofficialNetworkSubscriberSiteId));
|
|
1043
|
+
const wantedSiteNames = new Set();
|
|
1044
|
+
for (const siteId of siteIds) {
|
|
1045
|
+
wantedSiteNames.add(await resolveUnofficialNetworkSiteName(siteId));
|
|
1046
|
+
}
|
|
1047
|
+
if (node.isClosing) {
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
Array.from(node.wsNetworkEvents.keys())
|
|
1052
|
+
.filter((siteName) => !wantedSiteNames.has(siteName))
|
|
1053
|
+
.forEach(closeUnofficialNetworkSiteSocket);
|
|
1054
|
+
wantedSiteNames.forEach((siteName) => {
|
|
1055
|
+
if (!node.wsNetworkEvents.has(siteName)) {
|
|
1056
|
+
openUnofficialNetworkSiteSocket(siteName);
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// Opens one stream per subscribed site and closes streams no longer
|
|
1062
|
+
// needed. Calls are serialized so overlapping requests cannot open
|
|
1063
|
+
// duplicate sockets while a site name is being resolved.
|
|
1064
|
+
node.ensureUnofficialNetworkWebSocket = () => {
|
|
1065
|
+
node.unofficialNetworkSync = node.unofficialNetworkSync
|
|
1066
|
+
.then(syncUnofficialNetworkWebSockets)
|
|
1067
|
+
.catch((error) => {
|
|
1068
|
+
node.warn(`Unable to initialize Network websocket: ${error && error.message ? error.message : error}`);
|
|
1069
|
+
});
|
|
1070
|
+
return node.unofficialNetworkSync;
|
|
960
1071
|
};
|
|
961
1072
|
|
|
962
1073
|
node.closeUnofficialNetworkWebSocket = () => {
|
|
@@ -965,13 +1076,7 @@ module.exports = function(RED) {
|
|
|
965
1076
|
node.reconnectTimer = null;
|
|
966
1077
|
}
|
|
967
1078
|
|
|
968
|
-
|
|
969
|
-
try {
|
|
970
|
-
node.wsNetworkEvents.close();
|
|
971
|
-
} catch (error) {
|
|
972
|
-
}
|
|
973
|
-
node.wsNetworkEvents = null;
|
|
974
|
-
}
|
|
1079
|
+
Array.from(node.wsNetworkEvents.keys()).forEach(closeUnofficialNetworkSiteSocket);
|
|
975
1080
|
};
|
|
976
1081
|
|
|
977
1082
|
function parsePowerNumber(value) {
|
|
@@ -3397,6 +3502,9 @@ module.exports = function(RED) {
|
|
|
3397
3502
|
}
|
|
3398
3503
|
if (!node.hasUnofficialNetworkStreamSubscribers()) {
|
|
3399
3504
|
node.closeUnofficialNetworkWebSocket();
|
|
3505
|
+
} else {
|
|
3506
|
+
// Close the stream of a site that no longer has subscribers.
|
|
3507
|
+
node.ensureUnofficialNetworkWebSocket();
|
|
3400
3508
|
}
|
|
3401
3509
|
node.refreshPowerObservationScheduler();
|
|
3402
3510
|
node.refreshPresenceObservationScheduler();
|
|
@@ -1008,6 +1008,8 @@ module.exports = function(RED) {
|
|
|
1008
1008
|
}
|
|
1009
1009
|
|
|
1010
1010
|
node.shouldReceiveUnofficialNetworkEvents = () => Boolean(node.isObserving && isUnofficialNetworkStreamCapabilitySelected());
|
|
1011
|
+
// The config node opens the event stream of the site this node belongs to.
|
|
1012
|
+
node.getUnofficialNetworkSiteId = () => resolveScopedIdentifiers(node.deviceType, node.deviceId, node.currentDevice).siteId;
|
|
1011
1013
|
|
|
1012
1014
|
function shouldAutoReceiveConfiguredDevice() {
|
|
1013
1015
|
return node.server
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { registerConnectionCheck } = require("./utils/unifi-connection-check");
|
|
4
4
|
const { actionGuidance } = require("./utils/unifi-action-guidance");
|
|
5
5
|
const { buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
6
|
+
const { attachHeartbeat, stopHeartbeat, createReconnectBackoff } = require("./utils/unifi-websocket-resilience");
|
|
6
7
|
|
|
7
8
|
const {
|
|
8
9
|
buildBaseUrlFromHost,
|
|
@@ -204,6 +205,7 @@ module.exports = function(RED) {
|
|
|
204
205
|
node.wsDevices = null;
|
|
205
206
|
node.wsEvents = null;
|
|
206
207
|
node.reconnectTimer = null;
|
|
208
|
+
node.reconnectBackoff = createReconnectBackoff();
|
|
207
209
|
node.isClosing = false;
|
|
208
210
|
node.knxAiCameraCache = { at: 0, cameras: [] };
|
|
209
211
|
node.knxAiCameraRefresh = null;
|
|
@@ -875,8 +877,8 @@ module.exports = function(RED) {
|
|
|
875
877
|
return;
|
|
876
878
|
}
|
|
877
879
|
|
|
878
|
-
// Back off
|
|
879
|
-
//
|
|
880
|
+
// Back off exponentially so temporary controller restarts do not
|
|
881
|
+
// cause a tight reconnect loop.
|
|
880
882
|
node.reconnectTimer = setTimeout(() => {
|
|
881
883
|
node.reconnectTimer = null;
|
|
882
884
|
try {
|
|
@@ -884,7 +886,7 @@ module.exports = function(RED) {
|
|
|
884
886
|
} catch (error) {
|
|
885
887
|
node.warn(`Protect websocket reconnect failed: ${error && error.message ? error.message : error}`);
|
|
886
888
|
}
|
|
887
|
-
},
|
|
889
|
+
}, node.reconnectBackoff.nextDelay());
|
|
888
890
|
};
|
|
889
891
|
|
|
890
892
|
node.attachSocket = (kind, path, handler) => {
|
|
@@ -918,6 +920,11 @@ module.exports = function(RED) {
|
|
|
918
920
|
return;
|
|
919
921
|
}
|
|
920
922
|
|
|
923
|
+
ws.on("open", () => {
|
|
924
|
+
node.reconnectBackoff.reset();
|
|
925
|
+
});
|
|
926
|
+
attachHeartbeat(ws);
|
|
927
|
+
|
|
921
928
|
ws.on("message", (rawData) => {
|
|
922
929
|
try {
|
|
923
930
|
// Protect streams send JSON messages; malformed frames are
|
|
@@ -972,6 +979,7 @@ module.exports = function(RED) {
|
|
|
972
979
|
// Clear ownership before close so the close callback knows this was
|
|
973
980
|
// intentional and does not schedule an unnecessary reconnect.
|
|
974
981
|
node[propertyName] = null;
|
|
982
|
+
stopHeartbeat(socket);
|
|
975
983
|
try {
|
|
976
984
|
socket.close();
|
|
977
985
|
} catch (error) {
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Shared resilience for UniFi WebSocket streams. A ping/pong heartbeat detects
|
|
4
|
+
// half-open connections that never emit "close", and an exponential backoff
|
|
5
|
+
// spaces out reconnects while a controller restarts or upgrades.
|
|
6
|
+
|
|
7
|
+
const HEARTBEAT_INTERVAL_MS = 30000;
|
|
8
|
+
const RECONNECT_BASE_DELAY_MS = 5000;
|
|
9
|
+
const RECONNECT_MAX_DELAY_MS = 60000;
|
|
10
|
+
|
|
11
|
+
const heartbeatTimers = new WeakMap();
|
|
12
|
+
|
|
13
|
+
function stopHeartbeat(ws) {
|
|
14
|
+
if (!ws || typeof ws !== "object") {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const timer = heartbeatTimers.get(ws);
|
|
18
|
+
if (timer) {
|
|
19
|
+
clearInterval(timer);
|
|
20
|
+
heartbeatTimers.delete(ws);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function attachHeartbeat(ws, options = {}) {
|
|
25
|
+
if (!ws || typeof ws.on !== "function") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
stopHeartbeat(ws);
|
|
30
|
+
const intervalMs = Number(options.intervalMs) > 0 ? Number(options.intervalMs) : HEARTBEAT_INTERVAL_MS;
|
|
31
|
+
let alive = true;
|
|
32
|
+
const markAlive = () => {
|
|
33
|
+
alive = true;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const timer = setInterval(() => {
|
|
37
|
+
if (!alive) {
|
|
38
|
+
// Nothing received since the previous ping: the peer is gone even
|
|
39
|
+
// though the socket still looks open. terminate() emits "close",
|
|
40
|
+
// which lets the owner schedule its normal reconnect.
|
|
41
|
+
stopHeartbeat(ws);
|
|
42
|
+
try {
|
|
43
|
+
ws.terminate();
|
|
44
|
+
} catch (error) {
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
alive = false;
|
|
49
|
+
try {
|
|
50
|
+
ws.ping();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
}
|
|
53
|
+
}, intervalMs);
|
|
54
|
+
if (typeof timer.unref === "function") {
|
|
55
|
+
timer.unref();
|
|
56
|
+
}
|
|
57
|
+
heartbeatTimers.set(ws, timer);
|
|
58
|
+
|
|
59
|
+
ws.on("pong", markAlive);
|
|
60
|
+
ws.on("message", markAlive);
|
|
61
|
+
ws.on("close", () => stopHeartbeat(ws));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function createReconnectBackoff(options = {}) {
|
|
65
|
+
const baseMs = Number(options.baseMs) > 0 ? Number(options.baseMs) : RECONNECT_BASE_DELAY_MS;
|
|
66
|
+
const maxMs = Number(options.maxMs) > 0 ? Number(options.maxMs) : RECONNECT_MAX_DELAY_MS;
|
|
67
|
+
const random = typeof options.random === "function" ? options.random : Math.random;
|
|
68
|
+
let attempts = 0;
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
nextDelay() {
|
|
72
|
+
const delay = Math.min(maxMs, baseMs * (2 ** attempts));
|
|
73
|
+
attempts += 1;
|
|
74
|
+
// Up to 20% jitter keeps several config nodes from reconnecting in
|
|
75
|
+
// lockstep without ever exceeding the maximum delay.
|
|
76
|
+
return Math.round(delay * (0.8 + (random() * 0.2)));
|
|
77
|
+
},
|
|
78
|
+
reset() {
|
|
79
|
+
attempts = 0;
|
|
80
|
+
},
|
|
81
|
+
get attempts() {
|
|
82
|
+
return attempts;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
HEARTBEAT_INTERVAL_MS,
|
|
89
|
+
RECONNECT_BASE_DELAY_MS,
|
|
90
|
+
RECONNECT_MAX_DELAY_MS,
|
|
91
|
+
attachHeartbeat,
|
|
92
|
+
stopHeartbeat,
|
|
93
|
+
createReconnectBackoff
|
|
94
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-unifi-ultimate",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.19",
|
|
4
4
|
"description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
|
|
5
5
|
"files": [
|
|
6
6
|
"nodes/",
|
|
@@ -66,12 +66,11 @@
|
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
|
-
"test": "node
|
|
69
|
+
"test": "node tests/check-syntax.js && jest",
|
|
70
70
|
"registry:discover:network": "node scripts/discover-unifi-network-registry.js",
|
|
71
71
|
"registry:discover:protect": "node scripts/discover-unifi-protect-registry.js",
|
|
72
72
|
"registry:discover:access": "node scripts/discover-unifi-access-registry.js"
|
|
73
73
|
},
|
|
74
|
-
"main": "index.js",
|
|
75
74
|
"directories": {
|
|
76
75
|
"example": "examples"
|
|
77
76
|
},
|