matter-all-in-one-addon 1.8.21

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.
Files changed (244) hide show
  1. package/README.md +183 -0
  2. package/dist/camera/ai/camera-ai-detector.d.ts +44 -0
  3. package/dist/camera/ai/camera-ai-detector.js +329 -0
  4. package/dist/camera/ai/camera-ai-detector.js.map +1 -0
  5. package/dist/camera/camera-capabilities.d.ts +13 -0
  6. package/dist/camera/camera-capabilities.js +186 -0
  7. package/dist/camera/camera-capabilities.js.map +1 -0
  8. package/dist/camera/camera-source-resolver.d.ts +18 -0
  9. package/dist/camera/camera-source-resolver.js +187 -0
  10. package/dist/camera/camera-source-resolver.js.map +1 -0
  11. package/dist/camera/camera-types.d.ts +124 -0
  12. package/dist/camera/camera-types.js +8 -0
  13. package/dist/camera/camera-types.js.map +1 -0
  14. package/dist/camera/cameraui/cameraui-client.d.ts +54 -0
  15. package/dist/camera/cameraui/cameraui-client.js +698 -0
  16. package/dist/camera/cameraui/cameraui-client.js.map +1 -0
  17. package/dist/camera/cameraui/cameraui-homekit-bridge.d.ts +21 -0
  18. package/dist/camera/cameraui/cameraui-homekit-bridge.js +343 -0
  19. package/dist/camera/cameraui/cameraui-homekit-bridge.js.map +1 -0
  20. package/dist/camera/cameraui/cameraui-storage.d.ts +17 -0
  21. package/dist/camera/cameraui/cameraui-storage.js +916 -0
  22. package/dist/camera/cameraui/cameraui-storage.js.map +1 -0
  23. package/dist/camera/cameraui/cameraui-types.d.ts +89 -0
  24. package/dist/camera/cameraui/cameraui-types.js +5 -0
  25. package/dist/camera/cameraui/cameraui-types.js.map +1 -0
  26. package/dist/camera/homekit/ffmpeg-helper.d.ts +88 -0
  27. package/dist/camera/homekit/ffmpeg-helper.js +673 -0
  28. package/dist/camera/homekit/ffmpeg-helper.js.map +1 -0
  29. package/dist/camera/homekit/fmp4-parser.d.ts +48 -0
  30. package/dist/camera/homekit/fmp4-parser.js +156 -0
  31. package/dist/camera/homekit/fmp4-parser.js.map +1 -0
  32. package/dist/camera/homekit/hevc-eligibility.d.ts +16 -0
  33. package/dist/camera/homekit/hevc-eligibility.js +153 -0
  34. package/dist/camera/homekit/hevc-eligibility.js.map +1 -0
  35. package/dist/camera/homekit/homekit-camera-recording.delegate.d.ts +73 -0
  36. package/dist/camera/homekit/homekit-camera-recording.delegate.js +445 -0
  37. package/dist/camera/homekit/homekit-camera-recording.delegate.js.map +1 -0
  38. package/dist/camera/homekit/homekit-camera-stream.delegate.d.ts +47 -0
  39. package/dist/camera/homekit/homekit-camera-stream.delegate.js +833 -0
  40. package/dist/camera/homekit/homekit-camera-stream.delegate.js.map +1 -0
  41. package/dist/camera/homekit/homekit-camera.accessory.d.ts +53 -0
  42. package/dist/camera/homekit/homekit-camera.accessory.js +697 -0
  43. package/dist/camera/homekit/homekit-camera.accessory.js.map +1 -0
  44. package/dist/camera/matter/camera-endpoint.builder.d.ts +7 -0
  45. package/dist/camera/matter/camera-endpoint.builder.js +127 -0
  46. package/dist/camera/matter/camera-endpoint.builder.js.map +1 -0
  47. package/dist/camera/matter/camera-session-manager.d.ts +46 -0
  48. package/dist/camera/matter/camera-session-manager.js +120 -0
  49. package/dist/camera/matter/camera-session-manager.js.map +1 -0
  50. package/dist/camera/matter/camera-webrtc-adapter.d.ts +62 -0
  51. package/dist/camera/matter/camera-webrtc-adapter.js +298 -0
  52. package/dist/camera/matter/camera-webrtc-adapter.js.map +1 -0
  53. package/dist/camera/motion/ffmpeg-motion-detector.d.ts +46 -0
  54. package/dist/camera/motion/ffmpeg-motion-detector.js +200 -0
  55. package/dist/camera/motion/ffmpeg-motion-detector.js.map +1 -0
  56. package/dist/camera/nest/nest-camera-adapter.d.ts +38 -0
  57. package/dist/camera/nest/nest-camera-adapter.js +109 -0
  58. package/dist/camera/nest/nest-camera-adapter.js.map +1 -0
  59. package/dist/camera/scrypted/scrypted-camera-card.d.ts +39 -0
  60. package/dist/camera/scrypted/scrypted-camera-card.js +38 -0
  61. package/dist/camera/scrypted/scrypted-camera-card.js.map +1 -0
  62. package/dist/camera/scrypted/scrypted-camera-detail.d.ts +33 -0
  63. package/dist/camera/scrypted/scrypted-camera-detail.js +34 -0
  64. package/dist/camera/scrypted/scrypted-camera-detail.js.map +1 -0
  65. package/dist/camera/scrypted/scrypted-camera-discovery-adapter.d.ts +17 -0
  66. package/dist/camera/scrypted/scrypted-camera-discovery-adapter.js +24 -0
  67. package/dist/camera/scrypted/scrypted-camera-discovery-adapter.js.map +1 -0
  68. package/dist/camera/scrypted/scrypted-camera-normalizer.d.ts +44 -0
  69. package/dist/camera/scrypted/scrypted-camera-normalizer.js +44 -0
  70. package/dist/camera/scrypted/scrypted-camera-normalizer.js.map +1 -0
  71. package/dist/camera/scrypted/scrypted-camera-policy.d.ts +11 -0
  72. package/dist/camera/scrypted/scrypted-camera-policy.js +13 -0
  73. package/dist/camera/scrypted/scrypted-camera-policy.js.map +1 -0
  74. package/dist/camera/scrypted/scrypted-camera-presentation.d.ts +13 -0
  75. package/dist/camera/scrypted/scrypted-camera-presentation.js +23 -0
  76. package/dist/camera/scrypted/scrypted-camera-presentation.js.map +1 -0
  77. package/dist/camera/scrypted/scrypted-client-discovery-adapter.d.ts +6 -0
  78. package/dist/camera/scrypted/scrypted-client-discovery-adapter.js +12 -0
  79. package/dist/camera/scrypted/scrypted-client-discovery-adapter.js.map +1 -0
  80. package/dist/camera/scrypted/scrypted-client.d.ts +152 -0
  81. package/dist/camera/scrypted/scrypted-client.js +1170 -0
  82. package/dist/camera/scrypted/scrypted-client.js.map +1 -0
  83. package/dist/camera/scrypted/scrypted-crypto.d.ts +21 -0
  84. package/dist/camera/scrypted/scrypted-crypto.js +90 -0
  85. package/dist/camera/scrypted/scrypted-crypto.js.map +1 -0
  86. package/dist/camera/scrypted/scrypted-discovery-provider.d.ts +9 -0
  87. package/dist/camera/scrypted/scrypted-discovery-provider.js +25 -0
  88. package/dist/camera/scrypted/scrypted-discovery-provider.js.map +1 -0
  89. package/dist/camera/scrypted/scrypted-homekit-bridge.d.ts +12 -0
  90. package/dist/camera/scrypted/scrypted-homekit-bridge.js +214 -0
  91. package/dist/camera/scrypted/scrypted-homekit-bridge.js.map +1 -0
  92. package/dist/camera/scrypted/scrypted-matter-bridge.d.ts +36 -0
  93. package/dist/camera/scrypted/scrypted-matter-bridge.js +226 -0
  94. package/dist/camera/scrypted/scrypted-matter-bridge.js.map +1 -0
  95. package/dist/camera/scrypted/scrypted-reconnect-manager.d.ts +40 -0
  96. package/dist/camera/scrypted/scrypted-reconnect-manager.js +353 -0
  97. package/dist/camera/scrypted/scrypted-reconnect-manager.js.map +1 -0
  98. package/dist/camera/scrypted/scrypted-runtime-connector.d.ts +7 -0
  99. package/dist/camera/scrypted/scrypted-runtime-connector.js +17 -0
  100. package/dist/camera/scrypted/scrypted-runtime-connector.js.map +1 -0
  101. package/dist/camera/scrypted/scrypted-runtime-facade.d.ts +13 -0
  102. package/dist/camera/scrypted/scrypted-runtime-facade.js +29 -0
  103. package/dist/camera/scrypted/scrypted-runtime-facade.js.map +1 -0
  104. package/dist/camera/scrypted/scrypted-runtime-ingest.d.ts +5 -0
  105. package/dist/camera/scrypted/scrypted-runtime-ingest.js +54 -0
  106. package/dist/camera/scrypted/scrypted-runtime-ingest.js.map +1 -0
  107. package/dist/camera/scrypted/scrypted-storage.d.ts +95 -0
  108. package/dist/camera/scrypted/scrypted-storage.js +698 -0
  109. package/dist/camera/scrypted/scrypted-storage.js.map +1 -0
  110. package/dist/camera/scrypted/scrypted-stream-validator.d.ts +46 -0
  111. package/dist/camera/scrypted/scrypted-stream-validator.js +412 -0
  112. package/dist/camera/scrypted/scrypted-stream-validator.js.map +1 -0
  113. package/dist/camera/scrypted/scrypted-types.d.ts +354 -0
  114. package/dist/camera/scrypted/scrypted-types.js +6 -0
  115. package/dist/camera/scrypted/scrypted-types.js.map +1 -0
  116. package/dist/converters/binary_sensor.converter.d.ts +18 -0
  117. package/dist/converters/binary_sensor.converter.js +21 -0
  118. package/dist/converters/binary_sensor.converter.js.map +1 -0
  119. package/dist/converters/camera.converter.d.ts +18 -0
  120. package/dist/converters/camera.converter.js +24 -0
  121. package/dist/converters/camera.converter.js.map +1 -0
  122. package/dist/converters/climate.converter.d.ts +21 -0
  123. package/dist/converters/climate.converter.js +55 -0
  124. package/dist/converters/climate.converter.js.map +1 -0
  125. package/dist/converters/cover.converter.d.ts +14 -0
  126. package/dist/converters/cover.converter.js +22 -0
  127. package/dist/converters/cover.converter.js.map +1 -0
  128. package/dist/converters/energy_tariff.converter.d.ts +10 -0
  129. package/dist/converters/energy_tariff.converter.js +12 -0
  130. package/dist/converters/energy_tariff.converter.js.map +1 -0
  131. package/dist/converters/fan.converter.d.ts +233 -0
  132. package/dist/converters/fan.converter.js +353 -0
  133. package/dist/converters/fan.converter.js.map +1 -0
  134. package/dist/converters/index.d.ts +14 -0
  135. package/dist/converters/index.js +15 -0
  136. package/dist/converters/index.js.map +1 -0
  137. package/dist/converters/light.converter.d.ts +26 -0
  138. package/dist/converters/light.converter.js +36 -0
  139. package/dist/converters/light.converter.js.map +1 -0
  140. package/dist/converters/lock.converter.d.ts +20 -0
  141. package/dist/converters/lock.converter.js +29 -0
  142. package/dist/converters/lock.converter.js.map +1 -0
  143. package/dist/converters/sensor.converter.d.ts +18 -0
  144. package/dist/converters/sensor.converter.js +33 -0
  145. package/dist/converters/sensor.converter.js.map +1 -0
  146. package/dist/converters/soil_sensor.converter.d.ts +14 -0
  147. package/dist/converters/soil_sensor.converter.js +21 -0
  148. package/dist/converters/soil_sensor.converter.js.map +1 -0
  149. package/dist/converters/vacuum.converter.d.ts +168 -0
  150. package/dist/converters/vacuum.converter.js +288 -0
  151. package/dist/converters/vacuum.converter.js.map +1 -0
  152. package/dist/device-profiles.d.ts +14 -0
  153. package/dist/device-profiles.js +245 -0
  154. package/dist/device-profiles.js.map +1 -0
  155. package/dist/device-registry.d.ts +58 -0
  156. package/dist/device-registry.js +288 -0
  157. package/dist/device-registry.js.map +1 -0
  158. package/dist/entities/base.entity.d.ts +40 -0
  159. package/dist/entities/base.entity.js +826 -0
  160. package/dist/entities/base.entity.js.map +1 -0
  161. package/dist/entities/camera.entity.d.ts +48 -0
  162. package/dist/entities/camera.entity.js +127 -0
  163. package/dist/entities/camera.entity.js.map +1 -0
  164. package/dist/entities/closure.entity.d.ts +9 -0
  165. package/dist/entities/closure.entity.js +52 -0
  166. package/dist/entities/closure.entity.js.map +1 -0
  167. package/dist/entities/composite-device.entity.d.ts +61 -0
  168. package/dist/entities/composite-device.entity.js +986 -0
  169. package/dist/entities/composite-device.entity.js.map +1 -0
  170. package/dist/entities/cooktop.entity.d.ts +20 -0
  171. package/dist/entities/cooktop.entity.js +59 -0
  172. package/dist/entities/cooktop.entity.js.map +1 -0
  173. package/dist/entities/energy_tariff.entity.d.ts +7 -0
  174. package/dist/entities/energy_tariff.entity.js +32 -0
  175. package/dist/entities/energy_tariff.entity.js.map +1 -0
  176. package/dist/entities/humidifier.entity.d.ts +27 -0
  177. package/dist/entities/humidifier.entity.js +187 -0
  178. package/dist/entities/humidifier.entity.js.map +1 -0
  179. package/dist/entities/lock.entity.d.ts +12 -0
  180. package/dist/entities/lock.entity.js +58 -0
  181. package/dist/entities/lock.entity.js.map +1 -0
  182. package/dist/entities/media-player.entity.d.ts +13 -0
  183. package/dist/entities/media-player.entity.js +52 -0
  184. package/dist/entities/media-player.entity.js.map +1 -0
  185. package/dist/entities/oven.entity.d.ts +20 -0
  186. package/dist/entities/oven.entity.js +112 -0
  187. package/dist/entities/oven.entity.js.map +1 -0
  188. package/dist/entities/pet_feeder.entity.d.ts +28 -0
  189. package/dist/entities/pet_feeder.entity.js +67 -0
  190. package/dist/entities/pet_feeder.entity.js.map +1 -0
  191. package/dist/entities/soil_sensor.entity.d.ts +7 -0
  192. package/dist/entities/soil_sensor.entity.js +40 -0
  193. package/dist/entities/soil_sensor.entity.js.map +1 -0
  194. package/dist/entities/vacuum.entity.d.ts +37 -0
  195. package/dist/entities/vacuum.entity.js +352 -0
  196. package/dist/entities/vacuum.entity.js.map +1 -0
  197. package/dist/frontend/assets/index-DPXu3al2.js +26 -0
  198. package/dist/frontend/assets/index-DalMl4eS.css +1 -0
  199. package/dist/frontend/assets/logo-B_gEBugV.png +0 -0
  200. package/dist/frontend/index.html +14 -0
  201. package/dist/frontend/logo.png +0 -0
  202. package/dist/homeAssistant.d.ts +1268 -0
  203. package/dist/homeAssistant.js +1623 -0
  204. package/dist/homeAssistant.js.map +1 -0
  205. package/dist/homekit.compat.d.ts +40 -0
  206. package/dist/homekit.compat.js +43 -0
  207. package/dist/homekit.compat.js.map +1 -0
  208. package/dist/index.d.ts +7 -0
  209. package/dist/index.js +31 -0
  210. package/dist/index.js.map +1 -0
  211. package/dist/mqtt/mqtt-client.d.ts +33 -0
  212. package/dist/mqtt/mqtt-client.js +138 -0
  213. package/dist/mqtt/mqtt-client.js.map +1 -0
  214. package/dist/mqtt/mqtt.entity.d.ts +30 -0
  215. package/dist/mqtt/mqtt.entity.js +286 -0
  216. package/dist/mqtt/mqtt.entity.js.map +1 -0
  217. package/dist/platform.d.ts +318 -0
  218. package/dist/platform.js +6144 -0
  219. package/dist/platform.js.map +1 -0
  220. package/dist/proxy.d.ts +1 -0
  221. package/dist/proxy.js +226 -0
  222. package/dist/proxy.js.map +1 -0
  223. package/dist/utils/ha-discovery.d.ts +30 -0
  224. package/dist/utils/ha-discovery.js +175 -0
  225. package/dist/utils/ha-discovery.js.map +1 -0
  226. package/dist/utils/ha-state.d.ts +48 -0
  227. package/dist/utils/ha-state.js +21 -0
  228. package/dist/utils/ha-state.js.map +1 -0
  229. package/dist/utils/light-color.d.ts +63 -0
  230. package/dist/utils/light-color.js +213 -0
  231. package/dist/utils/light-color.js.map +1 -0
  232. package/dist/utils/log-buffer.d.ts +13 -0
  233. package/dist/utils/log-buffer.js +50 -0
  234. package/dist/utils/log-buffer.js.map +1 -0
  235. package/dist/utils/matter-attributes.d.ts +13 -0
  236. package/dist/utils/matter-attributes.js +35 -0
  237. package/dist/utils/matter-attributes.js.map +1 -0
  238. package/dist/utils/matter-device-identity.d.ts +22 -0
  239. package/dist/utils/matter-device-identity.js +70 -0
  240. package/dist/utils/matter-device-identity.js.map +1 -0
  241. package/dist/utils/thread-credentials.d.ts +22 -0
  242. package/dist/utils/thread-credentials.js +29 -0
  243. package/dist/utils/thread-credentials.js.map +1 -0
  244. package/package.json +80 -0
@@ -0,0 +1,698 @@
1
+ import net from "node:net";
2
+ /**
3
+ * Fast TCP socket check to verify if a camera's RTSP or HTTP stream port is reachable.
4
+ */
5
+ export async function isCameraStreamReachable(streamUrl, timeoutMs = 2000) {
6
+ if (!streamUrl || typeof streamUrl !== "string")
7
+ return false;
8
+ try {
9
+ const parsed = new URL(streamUrl);
10
+ const host = parsed.hostname;
11
+ let port = parsed.port ? Number(parsed.port) : undefined;
12
+ if (!port) {
13
+ if (parsed.protocol === "rtsp:")
14
+ port = 554;
15
+ else if (parsed.protocol === "rtsps:")
16
+ port = 322;
17
+ else if (parsed.protocol === "http:")
18
+ port = 80;
19
+ else if (parsed.protocol === "https:")
20
+ port = 443;
21
+ else
22
+ port = 554;
23
+ }
24
+ return await new Promise((resolve) => {
25
+ const socket = net.createConnection({ host, port, timeout: timeoutMs }, () => {
26
+ socket.destroy();
27
+ resolve(true);
28
+ });
29
+ socket.on("error", () => {
30
+ socket.destroy();
31
+ resolve(false);
32
+ });
33
+ socket.on("timeout", () => {
34
+ socket.destroy();
35
+ resolve(false);
36
+ });
37
+ });
38
+ }
39
+ catch {
40
+ return false;
41
+ }
42
+ }
43
+ /**
44
+ * Universal extractor to parse camera lists from any Camera.UI version response format:
45
+ * - Direct array [...]
46
+ * - Camera.UI v5 standard paginated { result: [...] }
47
+ * - Legacy { cameras: [...] }
48
+ * - Object dictionary { cameras: { "cam1": { ... } } }
49
+ * - Object dictionary { result: { "cam1": { ... } } }
50
+ * - Generic envelope { data: [...] } or { items: [...] }
51
+ * - Top-level dictionary of camera objects { "patio": { name: "patio", ... } }
52
+ */
53
+ export function extractRawCameras(json) {
54
+ if (!json)
55
+ return [];
56
+ if (Array.isArray(json))
57
+ return json;
58
+ if (typeof json !== "object")
59
+ return [];
60
+ // Check array properties
61
+ if (Array.isArray(json.result))
62
+ return json.result;
63
+ if (Array.isArray(json.cameras))
64
+ return json.cameras;
65
+ if (Array.isArray(json.data))
66
+ return json.data;
67
+ if (Array.isArray(json.items))
68
+ return json.items;
69
+ // Check dictionary inside json.cameras
70
+ if (json.cameras && typeof json.cameras === "object" && !Array.isArray(json.cameras)) {
71
+ const dictValues = Object.entries(json.cameras)
72
+ .map(([key, val]) => {
73
+ if (val && typeof val === "object") {
74
+ return { name: val.name || key, ...val };
75
+ }
76
+ return null;
77
+ })
78
+ .filter(Boolean);
79
+ if (dictValues.length > 0)
80
+ return dictValues;
81
+ }
82
+ // Check dictionary inside json.result
83
+ if (json.result && typeof json.result === "object" && !Array.isArray(json.result)) {
84
+ const dictValues = Object.entries(json.result)
85
+ .map(([key, val]) => {
86
+ if (val && typeof val === "object") {
87
+ return { name: val.name || key, ...val };
88
+ }
89
+ return null;
90
+ })
91
+ .filter(Boolean);
92
+ if (dictValues.length > 0)
93
+ return dictValues;
94
+ }
95
+ // Check if json itself is a dictionary of camera definitions
96
+ const candidateValues = Object.entries(json)
97
+ .map(([key, val]) => {
98
+ if (val &&
99
+ typeof val === "object" &&
100
+ (val.sources || val.videoConfig || val.name || val.id || val._id || val.uuid)) {
101
+ return { name: val.name || key, ...val };
102
+ }
103
+ return null;
104
+ })
105
+ .filter(Boolean);
106
+ if (candidateValues.length > 0)
107
+ return candidateValues;
108
+ return [];
109
+ }
110
+ export class CameraUiClient {
111
+ config;
112
+ accessToken;
113
+ resolvedBaseUrl;
114
+ loginError;
115
+ constructor(config) {
116
+ this.config = config;
117
+ if (this.config.allowSelfSignedCertificate !== false) {
118
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
119
+ }
120
+ }
121
+ getBaseUrl() {
122
+ let url = (this.config.serverUrl || "").trim();
123
+ if (!url)
124
+ return "";
125
+ if (!url.startsWith("http://") && !url.startsWith("https://")) {
126
+ url = `http://${url}`;
127
+ }
128
+ return url.replace(/\/+$/, "");
129
+ }
130
+ getCandidateUrls() {
131
+ const configured = this.getBaseUrl();
132
+ const candidates = [];
133
+ if (this.resolvedBaseUrl) {
134
+ candidates.push(this.resolvedBaseUrl);
135
+ }
136
+ if (configured) {
137
+ candidates.push(configured);
138
+ try {
139
+ const u = new URL(configured);
140
+ // Prioritize IPv4 127.0.0.1 if localhost was configured, avoiding IPv6 ::1 ECONNREFUSED in containers
141
+ if (u.hostname === "localhost") {
142
+ candidates.push(`${u.protocol}//127.0.0.1:${u.port || "8181"}`);
143
+ }
144
+ else if (u.hostname === "127.0.0.1") {
145
+ candidates.push(`${u.protocol}//localhost:${u.port || "8181"}`);
146
+ }
147
+ }
148
+ catch { }
149
+ }
150
+ // Common Home Assistant add-on and local network locations
151
+ candidates.push("http://127.0.0.1:8181");
152
+ candidates.push("http://localhost:8181");
153
+ candidates.push("https://192.168.110.46:3543");
154
+ candidates.push("http://192.168.110.46:3543");
155
+ candidates.push("http://192.168.110.46:8181");
156
+ candidates.push("https://127.0.0.1:3543");
157
+ candidates.push("http://127.0.0.1:3543");
158
+ candidates.push("https://localhost:3543");
159
+ candidates.push("http://localhost:3543");
160
+ candidates.push("http://a0d7b954-camera-ui:8181");
161
+ candidates.push("http://homeassistant:8181");
162
+ return Array.from(new Set(candidates.filter(Boolean)));
163
+ }
164
+ getHeaders() {
165
+ const headers = {
166
+ Accept: "application/json",
167
+ };
168
+ if (this.accessToken) {
169
+ headers["Authorization"] = `Bearer ${this.accessToken}`;
170
+ }
171
+ else if (this.config.username && this.config.password) {
172
+ const creds = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64");
173
+ headers["Authorization"] = `Basic ${creds}`;
174
+ }
175
+ return headers;
176
+ }
177
+ /**
178
+ * Attempts authentication with Camera.UI.
179
+ * Modern Camera.UI may use POST /api/auth/login or session auth.
180
+ * Universal HTTP Basic Auth is also handled transparently via getHeaders().
181
+ */
182
+ async login(targetBaseUrl) {
183
+ if (!this.config.username || !this.config.password) {
184
+ return { ok: true, skipped: true };
185
+ }
186
+ const baseUrl = targetBaseUrl || this.getBaseUrl();
187
+ if (!baseUrl) {
188
+ return { ok: false, message: "URL del servidor Camera.UI no configurada" };
189
+ }
190
+ if (this.config.allowSelfSignedCertificate !== false) {
191
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
192
+ }
193
+ const authEndpoints = ["/api/auth/login", "/api/login", "/auth/login"];
194
+ for (const ep of authEndpoints) {
195
+ try {
196
+ const res = await fetch(`${baseUrl}${ep}`, {
197
+ method: "POST",
198
+ headers: {
199
+ "Content-Type": "application/json",
200
+ Accept: "application/json",
201
+ ...this.getHeaders(),
202
+ },
203
+ body: JSON.stringify({
204
+ username: this.config.username,
205
+ password: this.config.password,
206
+ kind: "web",
207
+ persistent: true,
208
+ device: { id: "matter-all-in-one", name: "Matter All-in-One Bridge" },
209
+ }),
210
+ signal: AbortSignal.timeout(5000),
211
+ });
212
+ if (res.ok) {
213
+ const data = await res.json();
214
+ const token = data?.access_token ||
215
+ data?.tokens?.access ||
216
+ data?.tokens?.access_token ||
217
+ data?.token ||
218
+ data?.accessToken;
219
+ if (token) {
220
+ this.accessToken = String(token);
221
+ this.loginError = undefined;
222
+ return { ok: true };
223
+ }
224
+ }
225
+ if (res.status === 401 || res.status === 403) {
226
+ this.loginError = `Credenciales incorrectas: usuario o contraseña rechazados por Camera.UI en ${baseUrl} (HTTP ${res.status}).`;
227
+ }
228
+ }
229
+ catch { }
230
+ }
231
+ return { ok: true, skipped: true };
232
+ }
233
+ /**
234
+ * Tests connection to the Camera.UI instance across candidate endpoints and hosts.
235
+ */
236
+ async testConnection() {
237
+ const candidates = this.getCandidateUrls();
238
+ if (candidates.length === 0) {
239
+ return { ok: false, message: "URL del servidor Camera.UI no configurada" };
240
+ }
241
+ if (this.config.allowSelfSignedCertificate !== false) {
242
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
243
+ }
244
+ let lastError = "";
245
+ let authFailureMessage;
246
+ for (const baseUrl of candidates) {
247
+ this.loginError = undefined;
248
+ if (this.config.username && this.config.password) {
249
+ await this.login(baseUrl);
250
+ }
251
+ const testEndpoints = [
252
+ "/api/cameras?page=1&pageSize=-1",
253
+ "/api/cameras",
254
+ "/cameras?page=1&pageSize=-1",
255
+ "/cameras",
256
+ "/api/config",
257
+ "/config",
258
+ "/api/system/version",
259
+ "/api/system",
260
+ "/api/ping",
261
+ "/",
262
+ ];
263
+ for (const endpoint of testEndpoints) {
264
+ try {
265
+ const res = await fetch(`${baseUrl}${endpoint}`, {
266
+ method: "GET",
267
+ headers: this.getHeaders(),
268
+ signal: AbortSignal.timeout(4000),
269
+ });
270
+ if (res.ok) {
271
+ let version = "active";
272
+ let count = 0;
273
+ try {
274
+ const data = await res.json();
275
+ if (data?.version)
276
+ version = String(data.version);
277
+ else if (data?.system?.version)
278
+ version = String(data.system.version);
279
+ const discovered = extractRawCameras(data);
280
+ if (discovered.length > 0) {
281
+ count = discovered.length;
282
+ }
283
+ }
284
+ catch { }
285
+ this.resolvedBaseUrl = baseUrl;
286
+ const countMsg = count > 0 ? ` (${count} cámara${count === 1 ? "" : "s"} detectada${count === 1 ? "" : "s"})` : "";
287
+ return {
288
+ ok: true,
289
+ message: `Conexión exitosa con Camera.UI en ${baseUrl}${countMsg}`,
290
+ version,
291
+ activeUrl: baseUrl,
292
+ };
293
+ }
294
+ if (res.status === 401 || res.status === 403) {
295
+ authFailureMessage = `Credenciales incorrectas: Camera.UI en ${baseUrl} denegó el acceso (HTTP ${res.status}). Verifica el usuario y contraseña.`;
296
+ break;
297
+ }
298
+ lastError = `HTTP ${res.status} ${res.statusText}`;
299
+ }
300
+ catch (err) {
301
+ lastError = err.message || String(err);
302
+ }
303
+ }
304
+ if (this.loginError && !authFailureMessage) {
305
+ authFailureMessage = this.loginError;
306
+ }
307
+ }
308
+ if (authFailureMessage) {
309
+ return {
310
+ ok: false,
311
+ message: authFailureMessage,
312
+ };
313
+ }
314
+ const primaryUrl = this.getBaseUrl() || candidates[0];
315
+ return {
316
+ ok: false,
317
+ message: `No se pudo conectar a Camera.UI en ${primaryUrl}: ${lastError || "Servicio no disponible"}. Verifica que el servicio esté activo y el certificado SSL sea aceptado.`,
318
+ };
319
+ }
320
+ /**
321
+ * Fetches and normalizes camera definitions from Camera.UI.
322
+ */
323
+ async fetchCameras() {
324
+ const candidates = this.getCandidateUrls();
325
+ if (candidates.length === 0)
326
+ return [];
327
+ if (this.config.allowSelfSignedCertificate !== false) {
328
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
329
+ }
330
+ let rawList = [];
331
+ let successfulBaseUrl = "";
332
+ const endpointsToTry = [
333
+ "/api/cameras?page=1&pageSize=-1",
334
+ "/api/cameras",
335
+ "/cameras?page=1&pageSize=-1",
336
+ "/cameras",
337
+ "/api/config",
338
+ "/config",
339
+ ];
340
+ for (const baseUrl of candidates) {
341
+ if (this.config.username && this.config.password && !this.accessToken) {
342
+ await this.login(baseUrl);
343
+ }
344
+ for (const ep of endpointsToTry) {
345
+ try {
346
+ let res = await fetch(`${baseUrl}${ep}`, {
347
+ headers: this.getHeaders(),
348
+ signal: AbortSignal.timeout(6000),
349
+ });
350
+ // If 401, token might have expired, re-login once and retry
351
+ if (res.status === 401 && this.config.username && this.config.password) {
352
+ const loginRes = await this.login(baseUrl);
353
+ if (loginRes.ok) {
354
+ res = await fetch(`${baseUrl}${ep}`, {
355
+ headers: this.getHeaders(),
356
+ signal: AbortSignal.timeout(6000),
357
+ });
358
+ }
359
+ }
360
+ if (res.ok) {
361
+ const json = await res.json();
362
+ const items = extractRawCameras(json);
363
+ if (items.length > 0) {
364
+ rawList = items;
365
+ successfulBaseUrl = baseUrl;
366
+ this.resolvedBaseUrl = baseUrl;
367
+ break;
368
+ }
369
+ }
370
+ }
371
+ catch { }
372
+ }
373
+ if (rawList.length > 0)
374
+ break;
375
+ }
376
+ if (rawList.length === 0)
377
+ return [];
378
+ const effectiveBaseUrl = successfulBaseUrl || this.getBaseUrl() || candidates[0];
379
+ const results = [];
380
+ let parsedHostname = "localhost";
381
+ try {
382
+ parsedHostname = new URL(effectiveBaseUrl).hostname;
383
+ }
384
+ catch { }
385
+ for (let i = 0; i < rawList.length; i++) {
386
+ const item = rawList[i];
387
+ if (!item)
388
+ continue;
389
+ const name = String(item.name || item.camera || item.title || `Camera-${i + 1}`).trim();
390
+ const rawId = item._id || item.id || item.uuid || item.name || `cam_${i + 1}`;
391
+ const safeId = String(rawId)
392
+ .toLowerCase()
393
+ .replace(/[^a-z0-9_-]/g, "_");
394
+ const videoConfig = item.videoConfig || {};
395
+ const sources = Array.isArray(item.sources) ? item.sources : [];
396
+ // Look for sources in modern Camera.UI schema
397
+ let mainSourceUrl;
398
+ let subSourceUrl;
399
+ let snapshotSourceUrl;
400
+ let isMuted = false;
401
+ if (sources.length > 0) {
402
+ // High resolution or primary stream (Stream 1) - strictly prioritize Stream 1
403
+ const highRes = sources.find((s) => s.role === "high-resolution" ||
404
+ s.role === "high" ||
405
+ s.role === "main" ||
406
+ s.role === "stream1" ||
407
+ (typeof s.name === "string" && /main|stream1|hq|primary/i.test(s.name))) ||
408
+ sources.find((s) => {
409
+ const u = Array.isArray(s.urls) ? s.urls[0] : s.url || s.stream || "";
410
+ return /stream1|main|ch0|h264preview.*main/i.test(u);
411
+ }) ||
412
+ sources.find((s) => s.role !== "sub" && s.role !== "low" && s.role !== "snapshot") ||
413
+ sources[0];
414
+ if (highRes) {
415
+ if (Array.isArray(highRes.urls) && highRes.urls.length > 0) {
416
+ mainSourceUrl = this.cleanStreamUrl(highRes.urls[0]);
417
+ }
418
+ else if (typeof highRes.url === "string") {
419
+ mainSourceUrl = this.cleanStreamUrl(highRes.url);
420
+ }
421
+ else if (typeof highRes.stream === "string") {
422
+ mainSourceUrl = this.cleanStreamUrl(highRes.stream);
423
+ }
424
+ if (highRes.muted === true) {
425
+ isMuted = true;
426
+ }
427
+ }
428
+ // Sub stream / mid-low resolution
429
+ const subRes = sources.find((s) => s.role === "mid-resolution" ||
430
+ s.role === "mid" ||
431
+ s.role === "low-resolution" ||
432
+ s.role === "low" ||
433
+ s.role === "sub");
434
+ if (subRes) {
435
+ if (Array.isArray(subRes.urls) && subRes.urls.length > 0) {
436
+ subSourceUrl = this.cleanStreamUrl(subRes.urls[0]);
437
+ }
438
+ else if (typeof subRes.url === "string") {
439
+ subSourceUrl = this.cleanStreamUrl(subRes.url);
440
+ }
441
+ else if (typeof subRes.stream === "string") {
442
+ subSourceUrl = this.cleanStreamUrl(subRes.stream);
443
+ }
444
+ }
445
+ // Snapshot source
446
+ const snap = sources.find((s) => s.role === "snapshot" || s.useForSnapshot === true);
447
+ if (snap) {
448
+ if (Array.isArray(snap.urls) && snap.urls.length > 0) {
449
+ snapshotSourceUrl = this.cleanStreamUrl(snap.urls[0]);
450
+ }
451
+ else if (typeof snap.url === "string") {
452
+ snapshotSourceUrl = this.cleanStreamUrl(snap.url);
453
+ }
454
+ }
455
+ }
456
+ // Legacy fallback to videoConfig
457
+ let rtspUrl = mainSourceUrl || this.cleanStreamUrl(videoConfig.source);
458
+ let subRtspUrl = subSourceUrl || this.cleanStreamUrl(videoConfig.subSource);
459
+ let snapshotUrl = snapshotSourceUrl ||
460
+ this.cleanStreamUrl(videoConfig.stillImageSource);
461
+ // Ensure rtspUrl strictly prioritizes Stream 1 / Main (Full 100% Quality / Native Max Stream)
462
+ if (rtspUrl) {
463
+ const isWyze = /wyze/i.test(name) ||
464
+ /wyze/i.test(videoConfig.source || "") ||
465
+ /wyze/i.test(rtspUrl);
466
+ if (isWyze) {
467
+ // Wyze cameras: /stream0 is 1080p/2K High Quality, /stream1 is 360p low-res
468
+ if (rtspUrl.includes("/stream1")) {
469
+ rtspUrl = rtspUrl.replace("/stream1", "/stream0");
470
+ }
471
+ if (subRtspUrl && subRtspUrl.includes("/stream0") && !rtspUrl.includes("/stream0")) {
472
+ const temp = rtspUrl;
473
+ rtspUrl = subRtspUrl;
474
+ subRtspUrl = temp;
475
+ }
476
+ }
477
+ else {
478
+ // Standard cameras (Tapo, ONVIF, Reolink): /stream1, /main, /ch0 is High Quality, /stream2 or /sub is 360p
479
+ if ((rtspUrl.includes("/stream2") || rtspUrl.includes("_sub") || rtspUrl.includes("/sub/")) &&
480
+ subRtspUrl &&
481
+ (subRtspUrl.includes("/stream1") || subRtspUrl.includes("_main") || subRtspUrl.includes("/main/"))) {
482
+ const temp = rtspUrl;
483
+ rtspUrl = subRtspUrl;
484
+ subRtspUrl = temp;
485
+ }
486
+ else if (rtspUrl.includes("/stream2")) {
487
+ rtspUrl = rtspUrl.replace("/stream2", "/stream1");
488
+ }
489
+ }
490
+ }
491
+ // Camera.UI owns the stream endpoint. Never substitute an address from
492
+ // another installation into a discovered camera URL.
493
+ const rtspRestreamHost = parsedHostname;
494
+ if (!rtspUrl) {
495
+ const safeName = encodeURIComponent(name.toLowerCase().replace(/\s+/g, "_"));
496
+ rtspUrl = `rtsp://${rtspRestreamHost}:8554/${safeName}`;
497
+ }
498
+ // Resolve only Camera.UI's localhost restream against its configured
499
+ // host. A real camera address must remain exactly as Camera.UI returned it.
500
+ if (parsedHostname !== "localhost" && parsedHostname !== "127.0.0.1") {
501
+ if (rtspUrl) {
502
+ rtspUrl = rtspUrl
503
+ .replace("://localhost:", `://${rtspRestreamHost}:`)
504
+ .replace("://127.0.0.1:", `://${rtspRestreamHost}:`)
505
+ .replace("://localhost/", `://${rtspRestreamHost}/`)
506
+ .replace("://127.0.0.1/", `://${rtspRestreamHost}/`);
507
+ }
508
+ if (subRtspUrl) {
509
+ subRtspUrl = subRtspUrl
510
+ .replace("://localhost:", `://${rtspRestreamHost}:`)
511
+ .replace("://127.0.0.1:", `://${rtspRestreamHost}:`)
512
+ .replace("://localhost/", `://${rtspRestreamHost}/`)
513
+ .replace("://127.0.0.1/", `://${rtspRestreamHost}/`);
514
+ }
515
+ }
516
+ if (snapshotUrl) {
517
+ snapshotUrl = snapshotUrl
518
+ .replace("://localhost:", `://${parsedHostname}:`)
519
+ .replace("://127.0.0.1:", `://${parsedHostname}:`)
520
+ .replace("://localhost/", `://${parsedHostname}/`)
521
+ .replace("://127.0.0.1/", `://${parsedHostname}/`);
522
+ }
523
+ // Default snapshot from Camera.UI feed if stillImageSource not present
524
+ if (!snapshotUrl) {
525
+ snapshotUrl = `${effectiveBaseUrl}/api/cameras/${encodeURIComponent(name)}/snapshot`;
526
+ }
527
+ // Extract width & height from sources, videoConfig, resolution string, or model heuristics
528
+ let width = Number(videoConfig.maxWidth || videoConfig.width || item.width || (sources[0] && sources[0].width) || 0);
529
+ let height = Number(videoConfig.maxHeight || videoConfig.height || item.height || (sources[0] && sources[0].height) || 0);
530
+ const resString = String(sources[0]?.resolution || item.resolution || videoConfig.resolution || "");
531
+ if ((!width || !height) && resString.includes("x")) {
532
+ const [rw, rh] = resString.split("x").map((s) => Number(s.trim()));
533
+ if (rw && rh) {
534
+ width = rw;
535
+ height = rh;
536
+ }
537
+ }
538
+ const modelName = String(item.info?.model || item.model || "").toLowerCase();
539
+ const cameraTitle = name.toLowerCase();
540
+ const combinedTitleAndModel = `${cameraTitle} ${modelName}`;
541
+ if (!width || width <= 1920) {
542
+ if (/4k|uhd|8mp/i.test(combinedTitleAndModel)) {
543
+ width = 3840;
544
+ height = 2160;
545
+ }
546
+ else if (/2k|qhd|c402|c420|c425|c520|c325|tc72|3mp|4mp|5mp/i.test(combinedTitleAndModel)) {
547
+ width = 2560;
548
+ height = 1440;
549
+ }
550
+ else {
551
+ width = width || 1920;
552
+ height = height || 1080;
553
+ }
554
+ }
555
+ if (!height)
556
+ height = Math.round((width * 9) / 16);
557
+ const fps = Number(videoConfig.maxFPS || item.fps || (sources[0] && sources[0].fps) || 30);
558
+ const hasAudio = !isMuted && videoConfig.audio !== false && item.muted !== true;
559
+ // MQTT topics & OpenCV detection support
560
+ const mqttConfig = item.mqtt || {};
561
+ const safeSlug = name.toLowerCase().replace(/[^a-z0-9_-]/g, "_");
562
+ const motionTopic = mqttConfig.motionTopic ||
563
+ item.motionTopic ||
564
+ `camera.ui/${safeSlug}/motion`;
565
+ const doorbellTopic = mqttConfig.doorbellTopic ||
566
+ item.doorbellTopic ||
567
+ (item.doorbell ? `camera.ui/${safeSlug}/doorbell` : undefined);
568
+ // Detect video codec: check videoConfig, sources, or known Tapo 2K/HEVC camera models
569
+ const rawCodec = String(videoConfig.vcodec ||
570
+ videoConfig.codec ||
571
+ item.vcodec ||
572
+ item.codec ||
573
+ (sources[0] && (sources[0].codec || sources[0].vcodec)) ||
574
+ "").toLowerCase();
575
+ const isHevcDetected = rawCodec.includes("hevc") ||
576
+ rawCodec.includes("265") ||
577
+ /c402|c420|c425|c520|c320|c325|tc72/i.test(modelName) ||
578
+ /c402|c420|c425|c520|c320|c325|tc72/i.test(cameraTitle) ||
579
+ (width >= 2304 && (/tapo/i.test(cameraTitle) || /tapo/i.test(modelName)));
580
+ const videoCodec = isHevcDetected ? "hevc" : rawCodec.includes("h264") ? "h264" : undefined;
581
+ const strategy = isHevcDetected ? "passthrough_hevc" : "passthrough_h264";
582
+ results.push({
583
+ id: `cameraui_${safeId}`,
584
+ name,
585
+ manufacturer: item.info?.manufacturer || item.manufacturer || "Camera.UI",
586
+ model: item.info?.model || item.model || "Network Camera",
587
+ serialNumber: item.info?.serialNumber || item.serialNumber || `CUI-${safeId.toUpperCase()}`,
588
+ rtspUrl,
589
+ subRtspUrl,
590
+ snapshotUrl,
591
+ hasAudio,
592
+ width,
593
+ height,
594
+ fps,
595
+ videoCodec,
596
+ strategy,
597
+ motionTopic,
598
+ doorbellTopic,
599
+ motionActive: false,
600
+ doorbellActive: false,
601
+ status: "online",
602
+ homeKitEnabled: true,
603
+ });
604
+ }
605
+ return results;
606
+ }
607
+ cleanStreamUrl(raw) {
608
+ if (!raw || typeof raw !== "string")
609
+ return undefined;
610
+ let trimmed = raw.trim();
611
+ if (trimmed.startsWith("-i ")) {
612
+ trimmed = trimmed.substring(3).trim();
613
+ }
614
+ // Remove surrounding quotes if present
615
+ trimmed = trimmed.replace(/^["']|["']$/g, "").trim();
616
+ // Support tapo:// custom scheme from Camera.UI / homebridge-tapo-camera
617
+ // e.g. tapo://user:password@192.168.1.100 -> rtsp://user:password@192.168.1.100:554/stream1
618
+ if (trimmed.startsWith("tapo://")) {
619
+ const afterProto = trimmed.substring(7);
620
+ if (!afterProto.includes(":554") && !afterProto.includes("/")) {
621
+ return `rtsp://${afterProto}:554/stream1`;
622
+ }
623
+ else if (!afterProto.includes("/")) {
624
+ return `rtsp://${afterProto}/stream1`;
625
+ }
626
+ return `rtsp://${afterProto}`;
627
+ }
628
+ if (trimmed.startsWith("rtsp://") ||
629
+ trimmed.startsWith("rtsps://") ||
630
+ trimmed.startsWith("http://") ||
631
+ trimmed.startsWith("https://")) {
632
+ // Strip URL fragments (#gop=1, #timeout=..., etc.) — RTSP does NOT support URL fragments
633
+ // and FFmpeg will fail with "Invalid data found when processing input" if they are present.
634
+ const hashIndex = trimmed.indexOf("#");
635
+ if (hashIndex !== -1) {
636
+ trimmed = trimmed.substring(0, hashIndex);
637
+ }
638
+ return trimmed || undefined;
639
+ }
640
+ return undefined;
641
+ }
642
+ /**
643
+ * Fetches recent notifications / OpenCV detections from Camera.UI API.
644
+ */
645
+ async getRecentNotifications() {
646
+ const candidates = this.getCandidateUrls();
647
+ if (candidates.length === 0)
648
+ return [];
649
+ const endpoints = [
650
+ "/api/notifications/history?page=1&pageSize=20",
651
+ "/api/notifications/history",
652
+ "/notifications/history?page=1&pageSize=20",
653
+ "/notifications/history",
654
+ "/api/notifications?page=1&pageSize=20",
655
+ "/api/notifications",
656
+ "/notifications?page=1&pageSize=20",
657
+ "/notifications",
658
+ ];
659
+ for (const baseUrl of candidates) {
660
+ for (const ep of endpoints) {
661
+ try {
662
+ let res = await fetch(`${baseUrl}${ep}`, {
663
+ headers: this.getHeaders(),
664
+ signal: AbortSignal.timeout(2500),
665
+ });
666
+ // If unauthorized, attempt re-login once with baseUrl
667
+ if (res.status === 401 && this.config.username && this.config.password) {
668
+ const loginRes = await this.login(baseUrl);
669
+ if (loginRes.ok) {
670
+ res = await fetch(`${baseUrl}${ep}`, {
671
+ headers: this.getHeaders(),
672
+ signal: AbortSignal.timeout(2500),
673
+ });
674
+ }
675
+ }
676
+ if (res.ok) {
677
+ const json = await res.json();
678
+ if (Array.isArray(json))
679
+ return json;
680
+ if (Array.isArray(json?.history))
681
+ return json.history;
682
+ if (Array.isArray(json?.result))
683
+ return json.result;
684
+ if (Array.isArray(json?.notifications))
685
+ return json.notifications;
686
+ if (Array.isArray(json?.data))
687
+ return json.data;
688
+ if (Array.isArray(json?.items))
689
+ return json.items;
690
+ }
691
+ }
692
+ catch { }
693
+ }
694
+ }
695
+ return [];
696
+ }
697
+ }
698
+ //# sourceMappingURL=cameraui-client.js.map