@camstack/addon-post-analysis 0.1.19 → 0.2.0
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/dist/dist-4mTLJ7BJ.mjs +20750 -0
- package/dist/dist-CS2K80so.js +20933 -0
- package/dist/embedding-encoder/index.js +977 -902
- package/dist/embedding-encoder/index.mjs +967 -860
- package/dist/enrichment-engine/index.js +834 -833
- package/dist/enrichment-engine/index.mjs +828 -832
- package/dist/pipeline-analytics/_stub.js +1680 -1397
- package/dist/pipeline-analytics/_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-DOSUJ-U0.mjs +156 -0
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-DJvmVCso.mjs +26 -0
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_tanstack_mf_1_react_mf_2_query__loadShare__.js-B3Wx5J80.mjs +26 -0
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare__react__loadShare__.js-C0AuF9av.mjs +26 -0
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_1_jsx_mf_2_runtime__loadShare__.js-Bm-iyjmq.mjs +26 -0
- package/dist/pipeline-analytics/dist-CYZr2fwk.mjs +2726 -0
- package/dist/pipeline-analytics/hostInit-BazRS2O7.mjs +129 -0
- package/dist/pipeline-analytics/index.js +7133 -2558
- package/dist/pipeline-analytics/index.mjs +7124 -2557
- package/dist/pipeline-analytics/remoteEntry.js +134 -2973
- package/dist/pipeline-analytics/remoteEntry.ssr.js +33 -0
- package/dist/pipeline-analytics/virtualExposes-BgYzpJZG.mjs +27 -0
- package/dist/pipeline-analytics/virtual_mf-exposes-ssr___mfe_internal__addon_pipeline_analytics_widgets__remoteEntry_js-D7qgWCKX.mjs +10 -0
- package/dist/resolve-frame-5lMxmeI1.js +57 -0
- package/dist/resolve-frame-CT1T1tWy.mjs +44 -0
- package/package.json +26 -32
- package/dist/embedding-encoder/index.js.map +0 -1
- package/dist/embedding-encoder/index.mjs.map +0 -1
- package/dist/enrichment-engine/index.js.map +0 -1
- package/dist/enrichment-engine/index.mjs.map +0 -1
- package/dist/ffmpeg-config-DRONlBsj.mjs +0 -56
- package/dist/ffmpeg-config-DRONlBsj.mjs.map +0 -1
- package/dist/ffmpeg-config-uANz3sV5.js +0 -73
- package/dist/ffmpeg-config-uANz3sV5.js.map +0 -1
- package/dist/index-BFbwYH1P.js +0 -14343
- package/dist/index-BFbwYH1P.js.map +0 -1
- package/dist/index-BrTlzsrE.mjs +0 -14344
- package/dist/index-BrTlzsrE.mjs.map +0 -1
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/AudioHistoryChart.d.ts +0 -4
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/AudioMetricsPanel.d.ts +0 -10
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/DetectionHistoryChart.d.ts +0 -4
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/LiveStatsTab.d.ts +0 -5
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/MotionHistoryChart.d.ts +0 -4
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/OccupancyHistoryChart.d.ts +0 -4
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/OccupancyPanel.d.ts +0 -10
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/chart-utils.d.ts +0 -97
- package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/index.d.ts +0 -29
- package/dist/pipeline-analytics/@mf-types/widgets.d.ts +0 -2
- package/dist/pipeline-analytics/@mf-types.d.ts +0 -3
- package/dist/pipeline-analytics/@mf-types.zip +0 -0
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_sdk__loadShare__.mjs-h5aXOPSA.mjs +0 -12
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.mjs-NjF4kxzW.mjs +0 -19
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.mjs-7HAAnpQu.mjs +0 -18
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_tanstack_mf_1_react_mf_2_query__loadShare__.mjs-DoWbefqS.mjs +0 -104
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_trpc_mf_1_client__loadShare__.mjs-52bfkwC8.mjs +0 -85
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_trpc_mf_1_react_mf_2_query__loadShare__.mjs-CVrnrGED.mjs +0 -62
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react__loadShare__.mjs-BZTB2scQ.mjs +0 -88
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react__loadShare__.mjs_commonjs-proxy-CJO5YKGV.mjs +0 -29
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_1_jsx_mf_2_runtime__loadShare__.mjs-BsyrX6NO.mjs +0 -36
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom__loadShare__.mjs-Dp8hqYOB.mjs +0 -45
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom__loadShare__.mjs_commonjs-proxy-B0h0AGOH.mjs +0 -6
- package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom_mf_1_client__loadShare__.mjs-BZjEt71l.mjs +0 -34
- package/dist/pipeline-analytics/_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-kZBmgzMg.mjs +0 -156
- package/dist/pipeline-analytics/client-BlxIUpgf.mjs +0 -9836
- package/dist/pipeline-analytics/getErrorShape-BPSzUA7W-TlK8ipWe.mjs +0 -211
- package/dist/pipeline-analytics/hostInit-qBB1Thhi.mjs +0 -168
- package/dist/pipeline-analytics/index-BoL0rgZt.mjs +0 -435
- package/dist/pipeline-analytics/index-CR1aiZDH.mjs +0 -185
- package/dist/pipeline-analytics/index-CWkKuNLr.mjs +0 -232
- package/dist/pipeline-analytics/index-DlhiA9R0.mjs +0 -2603
- package/dist/pipeline-analytics/index-DtdgkNgf.mjs +0 -725
- package/dist/pipeline-analytics/index-Dw6Q30NI.mjs +0 -1655
- package/dist/pipeline-analytics/index-Dy2V7VOm.mjs +0 -14379
- package/dist/pipeline-analytics/index-i47purqY.mjs +0 -37880
- package/dist/pipeline-analytics/index-xncRG7-x.mjs +0 -2713
- package/dist/pipeline-analytics/index.js.map +0 -1
- package/dist/pipeline-analytics/index.mjs.map +0 -1
- package/dist/pipeline-analytics/jsx-runtime-Dlbl3gpr.mjs +0 -55
- package/dist/pipeline-analytics/schemas-ClCuS4qa.mjs +0 -3594
- package/dist/pipeline-analytics/virtualExposes-8FzWTdq3.mjs +0 -42
- package/dist/playlist-generator-EhPaB7Hn.js +0 -48
- package/dist/playlist-generator-EhPaB7Hn.js.map +0 -1
- package/dist/playlist-generator-VTkgn53O.mjs +0 -48
- package/dist/playlist-generator-VTkgn53O.mjs.map +0 -1
- package/dist/recording/index.js +0 -257
- package/dist/recording/index.js.map +0 -1
- package/dist/recording/index.mjs +0 -235
- package/dist/recording/index.mjs.map +0 -1
- package/dist/recording-coordinator-BoGr5moz.js +0 -1052
- package/dist/recording-coordinator-BoGr5moz.js.map +0 -1
- package/dist/recording-coordinator-CsYH9LqF.mjs +0 -1012
- package/dist/recording-coordinator-CsYH9LqF.mjs.map +0 -1
- package/dist/recording-db-gOgaoQh0.js +0 -348
- package/dist/recording-db-gOgaoQh0.js.map +0 -1
- package/dist/recording-db-lIkSMTLq.mjs +0 -348
- package/dist/recording-db-lIkSMTLq.mjs.map +0 -1
- package/dist/recording-service-facade-B9lG6OFn.mjs +0 -123
- package/dist/recording-service-facade-B9lG6OFn.mjs.map +0 -1
- package/dist/recording-service-facade-Do1PKlAL.js +0 -123
- package/dist/recording-service-facade-Do1PKlAL.js.map +0 -1
- package/dist/storage-estimator-CRpoQc9j.js +0 -72
- package/dist/storage-estimator-CRpoQc9j.js.map +0 -1
- package/dist/storage-estimator-DzD8gWJH.mjs +0 -72
- package/dist/storage-estimator-DzD8gWJH.mjs.map +0 -1
|
@@ -1,841 +1,837 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as number, E as tuple, S as boolean, T as string, b as _enum, c as asJsonObject, d as createEvent, i as EventCategory, n as BaseAddon, w as object, x as array } from "../dist-4mTLJ7BJ.mjs";
|
|
2
|
+
import { n as extractCrop, t as resolveFrame } from "../resolve-frame-CT1T1tWy.mjs";
|
|
2
3
|
import { FrameRingReaderCache } from "@camstack/shm-ring";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
4
|
+
//#region src/enrichment-engine/types.ts
|
|
5
|
+
var DEFAULT_ENRICHMENT_CONFIG = {
|
|
6
|
+
enabled: false,
|
|
7
|
+
embedding: {
|
|
8
|
+
enabled: false,
|
|
9
|
+
modelId: "clip-vit-b32",
|
|
10
|
+
agentId: "local",
|
|
11
|
+
runtime: "node",
|
|
12
|
+
backend: "cpu",
|
|
13
|
+
classes: [],
|
|
14
|
+
minConfidence: .5,
|
|
15
|
+
maxPerSecPerCamera: 1,
|
|
16
|
+
cropStrategy: "first",
|
|
17
|
+
retentionDays: 30
|
|
18
|
+
},
|
|
19
|
+
sceneMonitor: {
|
|
20
|
+
enabled: false,
|
|
21
|
+
modelId: "clip-vit-b32",
|
|
22
|
+
pollIntervalSec: 10,
|
|
23
|
+
hysteresisCount: 3
|
|
24
|
+
},
|
|
25
|
+
activitySummary: {
|
|
26
|
+
enabled: false,
|
|
27
|
+
intervalSec: 60,
|
|
28
|
+
activityThresholds: {
|
|
29
|
+
low: 2,
|
|
30
|
+
medium: 10,
|
|
31
|
+
high: 30
|
|
32
|
+
}
|
|
33
|
+
}
|
|
33
34
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
tags: { deviceId: Number(pending.deviceId) },
|
|
252
|
-
meta: { class: pending.class, trackId: pending.trackId, inferenceMs: Number(inferenceMs.toFixed(1)) }
|
|
253
|
-
});
|
|
254
|
-
} catch (err) {
|
|
255
|
-
this.logger.warn("Failed to embed track", {
|
|
256
|
-
tags: { deviceId: Number(pending.deviceId) },
|
|
257
|
-
meta: { trackId: pending.trackId, error: String(err) }
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
const TrackPositionSchema = object({
|
|
263
|
-
x: number(),
|
|
264
|
-
y: number(),
|
|
265
|
-
timestamp: number(),
|
|
266
|
-
bbox: tuple([number(), number(), number(), number()])
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/enrichment-engine/workers/embedding-dispatcher.ts
|
|
37
|
+
var EmbeddingDispatcher = class {
|
|
38
|
+
config;
|
|
39
|
+
encoders;
|
|
40
|
+
eventBus;
|
|
41
|
+
logger;
|
|
42
|
+
ownNodeId;
|
|
43
|
+
readers;
|
|
44
|
+
getRemoteFrame;
|
|
45
|
+
lastEmbedTime = /* @__PURE__ */ new Map();
|
|
46
|
+
pendingCrops = /* @__PURE__ */ new Map();
|
|
47
|
+
flushTimer = null;
|
|
48
|
+
unsubscribe = null;
|
|
49
|
+
_processedCount = 0;
|
|
50
|
+
_totalInferenceMs = 0;
|
|
51
|
+
_encoderIndex = 0;
|
|
52
|
+
constructor(deps) {
|
|
53
|
+
this.config = deps.config;
|
|
54
|
+
this.encoders = deps.encoders;
|
|
55
|
+
this.eventBus = deps.eventBus;
|
|
56
|
+
this.logger = deps.logger;
|
|
57
|
+
this.ownNodeId = deps.ownNodeId;
|
|
58
|
+
this.readers = deps.readers;
|
|
59
|
+
this.getRemoteFrame = deps.getRemoteFrame;
|
|
60
|
+
}
|
|
61
|
+
async start() {
|
|
62
|
+
if (!this.config.enabled) {
|
|
63
|
+
this.logger.info("EmbeddingDispatcher disabled");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this.unsubscribe = this.eventBus.subscribe({ category: EventCategory.DetectionResult }, (event) => {
|
|
67
|
+
this.handleDetectionResult(event);
|
|
68
|
+
});
|
|
69
|
+
if (this.config.cropStrategy === "best-confidence") this.flushTimer = setInterval(() => {
|
|
70
|
+
this.flushPending();
|
|
71
|
+
}, 1e3);
|
|
72
|
+
this.logger.info("EmbeddingDispatcher started", { meta: {
|
|
73
|
+
strategy: this.config.cropStrategy,
|
|
74
|
+
maxPerSec: this.config.maxPerSecPerCamera
|
|
75
|
+
} });
|
|
76
|
+
}
|
|
77
|
+
async stop() {
|
|
78
|
+
this.unsubscribe?.();
|
|
79
|
+
this.unsubscribe = null;
|
|
80
|
+
if (this.flushTimer) {
|
|
81
|
+
clearInterval(this.flushTimer);
|
|
82
|
+
this.flushTimer = null;
|
|
83
|
+
}
|
|
84
|
+
await this.flushPending();
|
|
85
|
+
}
|
|
86
|
+
get processedCount() {
|
|
87
|
+
return this._processedCount;
|
|
88
|
+
}
|
|
89
|
+
get avgInferenceMs() {
|
|
90
|
+
return this._processedCount > 0 ? this._totalInferenceMs / this._processedCount : 0;
|
|
91
|
+
}
|
|
92
|
+
get queueDepth() {
|
|
93
|
+
return this.pendingCrops.size;
|
|
94
|
+
}
|
|
95
|
+
async handleDetectionResult(event) {
|
|
96
|
+
const data = event.data;
|
|
97
|
+
const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
|
|
98
|
+
if (!deviceId) return;
|
|
99
|
+
const detections = data.analysisResults ?? [];
|
|
100
|
+
const handle = data.frameHandle;
|
|
101
|
+
if (!handle) {
|
|
102
|
+
this.logger.debug("skip: no frameHandle on DetectionResult", { meta: { deviceId } });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
let decoded;
|
|
106
|
+
try {
|
|
107
|
+
decoded = await resolveFrame(handle, {
|
|
108
|
+
ownNodeId: this.ownNodeId,
|
|
109
|
+
readers: this.readers,
|
|
110
|
+
getRemoteFrame: this.getRemoteFrame
|
|
111
|
+
});
|
|
112
|
+
} catch (err) {
|
|
113
|
+
this.logger.debug("skip: resolveFrame threw", { meta: {
|
|
114
|
+
deviceId,
|
|
115
|
+
shmId: handle.shmId,
|
|
116
|
+
error: String(err)
|
|
117
|
+
} });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!decoded) {
|
|
121
|
+
this.logger.debug("skip: frame recycled before resolve", { meta: {
|
|
122
|
+
deviceId,
|
|
123
|
+
shmId: handle.shmId
|
|
124
|
+
} });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (decoded.format !== "rgb") {
|
|
128
|
+
this.logger.debug("skip: resolved frame is not RGB", { meta: {
|
|
129
|
+
deviceId,
|
|
130
|
+
format: decoded.format
|
|
131
|
+
} });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const frameData = Buffer.isBuffer(decoded.data) ? decoded.data : Buffer.from(decoded.data);
|
|
135
|
+
const frameWidth = decoded.width;
|
|
136
|
+
const frameHeight = decoded.height;
|
|
137
|
+
for (const det of detections) {
|
|
138
|
+
const detection = det.detection;
|
|
139
|
+
if (!detection) continue;
|
|
140
|
+
if (this.config.classes.length > 0 && !this.config.classes.includes(detection.class)) continue;
|
|
141
|
+
if (detection.score < this.config.minConfidence) continue;
|
|
142
|
+
const now = Date.now();
|
|
143
|
+
const minInterval = 1e3 / this.config.maxPerSecPerCamera;
|
|
144
|
+
if (now - (this.lastEmbedTime.get(deviceId) ?? 0) < minInterval) continue;
|
|
145
|
+
const trackId = detection.trackId ?? `${deviceId}-${now}`;
|
|
146
|
+
const pending = {
|
|
147
|
+
trackId,
|
|
148
|
+
deviceId,
|
|
149
|
+
class: detection.class,
|
|
150
|
+
confidence: detection.score,
|
|
151
|
+
frameData,
|
|
152
|
+
frameWidth,
|
|
153
|
+
frameHeight,
|
|
154
|
+
bbox: detection.bbox,
|
|
155
|
+
receivedAt: now
|
|
156
|
+
};
|
|
157
|
+
switch (this.config.cropStrategy) {
|
|
158
|
+
case "first":
|
|
159
|
+
if (!this.pendingCrops.has(trackId)) {
|
|
160
|
+
this.pendingCrops.set(trackId, pending);
|
|
161
|
+
this.processOne(pending);
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
case "best-confidence": {
|
|
165
|
+
const existing = this.pendingCrops.get(trackId);
|
|
166
|
+
if (!existing || pending.confidence > existing.confidence) this.pendingCrops.set(trackId, pending);
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case "track-end":
|
|
170
|
+
if (det.objectState?.state === "leaving") {
|
|
171
|
+
this.pendingCrops.set(trackId, pending);
|
|
172
|
+
this.processOne(pending);
|
|
173
|
+
} else {
|
|
174
|
+
const existing = this.pendingCrops.get(trackId);
|
|
175
|
+
if (!existing || pending.confidence > existing.confidence) this.pendingCrops.set(trackId, pending);
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async flushPending() {
|
|
182
|
+
const now = Date.now();
|
|
183
|
+
const toFlush = [];
|
|
184
|
+
for (const [trackId, pending] of this.pendingCrops) if (now - pending.receivedAt > 3e3) {
|
|
185
|
+
toFlush.push(pending);
|
|
186
|
+
this.pendingCrops.delete(trackId);
|
|
187
|
+
}
|
|
188
|
+
await Promise.all(toFlush.map((p) => this.processOne(p)));
|
|
189
|
+
}
|
|
190
|
+
async processOne(pending) {
|
|
191
|
+
if (this.encoders.length === 0) return;
|
|
192
|
+
try {
|
|
193
|
+
const { crop, width, height } = await extractCrop(pending.frameData, pending.frameWidth, pending.frameHeight, pending.bbox);
|
|
194
|
+
const encoder = this.encoders[this._encoderIndex % this.encoders.length];
|
|
195
|
+
this._encoderIndex++;
|
|
196
|
+
const { embedding: _embedding, inferenceMs } = await encoder.encode(crop, width, height);
|
|
197
|
+
const info = encoder.getInfo();
|
|
198
|
+
this._processedCount++;
|
|
199
|
+
this._totalInferenceMs += inferenceMs;
|
|
200
|
+
this.lastEmbedTime.set(pending.deviceId, Date.now());
|
|
201
|
+
this.pendingCrops.delete(pending.trackId);
|
|
202
|
+
const embeddingId = `${pending.deviceId}/${pending.trackId}/${Date.now()}`;
|
|
203
|
+
const payload = {
|
|
204
|
+
deviceId: Number(pending.deviceId),
|
|
205
|
+
trackId: pending.trackId,
|
|
206
|
+
class: pending.class,
|
|
207
|
+
embeddingId,
|
|
208
|
+
modelId: info.modelId,
|
|
209
|
+
embeddingDim: info.embeddingDim,
|
|
210
|
+
inferenceMs,
|
|
211
|
+
timestamp: Date.now()
|
|
212
|
+
};
|
|
213
|
+
this.eventBus.emit(createEvent(EventCategory.EnrichmentEmbeddingStored, {
|
|
214
|
+
type: "addon",
|
|
215
|
+
id: "enrichment-engine"
|
|
216
|
+
}, payload));
|
|
217
|
+
this.logger.debug("Embedded track", {
|
|
218
|
+
tags: { deviceId: Number(pending.deviceId) },
|
|
219
|
+
meta: {
|
|
220
|
+
class: pending.class,
|
|
221
|
+
trackId: pending.trackId,
|
|
222
|
+
inferenceMs: Number(inferenceMs.toFixed(1))
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
} catch (err) {
|
|
226
|
+
this.logger.warn("Failed to embed track", {
|
|
227
|
+
tags: { deviceId: Number(pending.deviceId) },
|
|
228
|
+
meta: {
|
|
229
|
+
trackId: pending.trackId,
|
|
230
|
+
error: String(err)
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/_analytics-schemas/persistence-records.ts
|
|
238
|
+
/**
|
|
239
|
+
* Zod schemas for analytics-suite persisted record types.
|
|
240
|
+
* Used by persistence services to parse settings-store query results.
|
|
241
|
+
*/
|
|
242
|
+
var TrackPositionSchema = object({
|
|
243
|
+
x: number(),
|
|
244
|
+
y: number(),
|
|
245
|
+
timestamp: number(),
|
|
246
|
+
bbox: tuple([
|
|
247
|
+
number(),
|
|
248
|
+
number(),
|
|
249
|
+
number(),
|
|
250
|
+
number()
|
|
251
|
+
])
|
|
267
252
|
});
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
253
|
+
var TrackSnapshotSchema = object({
|
|
254
|
+
timestamp: number(),
|
|
255
|
+
position: TrackPositionSchema,
|
|
256
|
+
thumbnailPath: string()
|
|
272
257
|
});
|
|
273
258
|
object({
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
});
|
|
286
|
-
const SceneMonitorConfigSchema = object({
|
|
287
|
-
monitors: array(object({
|
|
288
|
-
id: string(),
|
|
289
|
-
label: string(),
|
|
290
|
-
roi: object({ x: number(), y: number(), w: number(), h: number() }),
|
|
291
|
-
enabled: boolean(),
|
|
292
|
-
states: array(object({
|
|
293
|
-
id: string(),
|
|
294
|
-
label: string(),
|
|
295
|
-
prompt: string().optional(),
|
|
296
|
-
textPrompts: array(string()).optional(),
|
|
297
|
-
referenceEmbeddings: array(array(number())).optional(),
|
|
298
|
-
threshold: number().optional(),
|
|
299
|
-
notify: boolean().optional(),
|
|
300
|
-
severity: _enum(["info", "warning", "alert"]).optional()
|
|
301
|
-
}))
|
|
302
|
-
}))
|
|
259
|
+
trackId: string(),
|
|
260
|
+
deviceId: string(),
|
|
261
|
+
className: string(),
|
|
262
|
+
label: string().optional(),
|
|
263
|
+
firstSeen: number(),
|
|
264
|
+
lastSeen: number(),
|
|
265
|
+
positions: array(TrackPositionSchema),
|
|
266
|
+
snapshots: array(TrackSnapshotSchema),
|
|
267
|
+
totalDistance: number(),
|
|
268
|
+
zonesVisited: array(string()),
|
|
269
|
+
active: boolean()
|
|
303
270
|
});
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
271
|
+
var SceneMonitorConfigSchema = object({ monitors: array(object({
|
|
272
|
+
id: string(),
|
|
273
|
+
label: string(),
|
|
274
|
+
roi: object({
|
|
275
|
+
x: number(),
|
|
276
|
+
y: number(),
|
|
277
|
+
w: number(),
|
|
278
|
+
h: number()
|
|
279
|
+
}),
|
|
280
|
+
enabled: boolean(),
|
|
281
|
+
states: array(object({
|
|
282
|
+
id: string(),
|
|
283
|
+
label: string(),
|
|
284
|
+
prompt: string().optional(),
|
|
285
|
+
textPrompts: array(string()).optional(),
|
|
286
|
+
referenceEmbeddings: array(array(number())).optional(),
|
|
287
|
+
threshold: number().optional(),
|
|
288
|
+
notify: boolean().optional(),
|
|
289
|
+
severity: _enum([
|
|
290
|
+
"info",
|
|
291
|
+
"warning",
|
|
292
|
+
"alert"
|
|
293
|
+
]).optional()
|
|
294
|
+
}))
|
|
295
|
+
})) });
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/enrichment-engine/services/text-embedding-cache.ts
|
|
298
|
+
/**
|
|
299
|
+
* In-memory cache for text prompt embeddings.
|
|
300
|
+
*
|
|
301
|
+
* Keys follow the convention `${monitorId}:${text}` so that all entries
|
|
302
|
+
* belonging to a specific scene monitor can be invalidated in one call.
|
|
303
|
+
*/
|
|
304
|
+
var TextEmbeddingCache = class {
|
|
305
|
+
cache = /* @__PURE__ */ new Map();
|
|
306
|
+
get(key) {
|
|
307
|
+
return this.cache.get(key);
|
|
308
|
+
}
|
|
309
|
+
set(key, embedding) {
|
|
310
|
+
this.cache.set(key, embedding);
|
|
311
|
+
}
|
|
312
|
+
has(key) {
|
|
313
|
+
return this.cache.has(key);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Deletes all entries whose key starts with `${monitorId}:`.
|
|
317
|
+
* Used when a scene monitor's text prompts are updated.
|
|
318
|
+
*/
|
|
319
|
+
invalidateMonitor(monitorId) {
|
|
320
|
+
const prefix = `${monitorId}:`;
|
|
321
|
+
for (const key of this.cache.keys()) if (key.startsWith(prefix)) this.cache.delete(key);
|
|
322
|
+
}
|
|
323
|
+
clear() {
|
|
324
|
+
this.cache.clear();
|
|
325
|
+
}
|
|
326
|
+
get size() {
|
|
327
|
+
return this.cache.size;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region src/enrichment-engine/workers/scene-state-worker.ts
|
|
332
|
+
var SceneStateWorker = class {
|
|
333
|
+
config;
|
|
334
|
+
encoders;
|
|
335
|
+
eventBus;
|
|
336
|
+
store;
|
|
337
|
+
streamBrokerRegistry;
|
|
338
|
+
logger;
|
|
339
|
+
textCache = new TextEmbeddingCache();
|
|
340
|
+
monitorStates = /* @__PURE__ */ new Map();
|
|
341
|
+
pollTimer = null;
|
|
342
|
+
_activeCount = 0;
|
|
343
|
+
constructor(deps) {
|
|
344
|
+
this.config = deps.config;
|
|
345
|
+
this.encoders = deps.encoders;
|
|
346
|
+
this.eventBus = deps.eventBus;
|
|
347
|
+
this.store = deps.store;
|
|
348
|
+
this.streamBrokerRegistry = deps.streamBrokerRegistry;
|
|
349
|
+
this.logger = deps.logger;
|
|
350
|
+
}
|
|
351
|
+
async start() {
|
|
352
|
+
if (!this.config.enabled) {
|
|
353
|
+
this.logger.info("SceneStateWorker disabled");
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
this.pollTimer = setInterval(() => {
|
|
357
|
+
this.pollAll();
|
|
358
|
+
}, this.config.pollIntervalSec * 1e3);
|
|
359
|
+
this.logger.info("SceneStateWorker started", { meta: {
|
|
360
|
+
pollIntervalSec: this.config.pollIntervalSec,
|
|
361
|
+
hysteresis: this.config.hysteresisCount
|
|
362
|
+
} });
|
|
363
|
+
}
|
|
364
|
+
async stop() {
|
|
365
|
+
if (this.pollTimer) {
|
|
366
|
+
clearInterval(this.pollTimer);
|
|
367
|
+
this.pollTimer = null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
get activeCount() {
|
|
371
|
+
return this._activeCount;
|
|
372
|
+
}
|
|
373
|
+
async pollAll() {
|
|
374
|
+
const allConfigs = (await this.store.query.query({
|
|
375
|
+
collection: "device-settings",
|
|
376
|
+
filter: { where: { id: "enrichment:scene-monitor:%" } }
|
|
377
|
+
})).map((r) => ({
|
|
378
|
+
id: r.id,
|
|
379
|
+
data: SceneMonitorConfigSchema.parse(r.data)
|
|
380
|
+
}));
|
|
381
|
+
const deviceMonitors = /* @__PURE__ */ new Map();
|
|
382
|
+
for (const record of allConfigs) {
|
|
383
|
+
const deviceId = record.id.replace("enrichment:scene-monitor:", "");
|
|
384
|
+
const active = record.data.monitors.filter((m) => m.enabled);
|
|
385
|
+
if (active.length > 0) deviceMonitors.set(deviceId, active);
|
|
386
|
+
}
|
|
387
|
+
this._activeCount = [...deviceMonitors.values()].reduce((sum, ms) => sum + ms.length, 0);
|
|
388
|
+
await Promise.all([...deviceMonitors.entries()].map(([deviceId, monitors]) => this.pollCamera(deviceId, monitors)));
|
|
389
|
+
}
|
|
390
|
+
async pollCamera(deviceId, monitors) {
|
|
391
|
+
if (this.encoders.length === 0) return;
|
|
392
|
+
try {
|
|
393
|
+
const snapshot = await this.streamBrokerRegistry.getSnapshot(deviceId);
|
|
394
|
+
if (!snapshot) return;
|
|
395
|
+
const encoder = this.encoders[0];
|
|
396
|
+
for (const monitor of monitors) try {
|
|
397
|
+
await this.processMonitor(deviceId, monitor, snapshot, encoder);
|
|
398
|
+
} catch (err) {
|
|
399
|
+
this.logger.warn("SceneState error", {
|
|
400
|
+
tags: { deviceId: Number(deviceId) },
|
|
401
|
+
meta: {
|
|
402
|
+
monitorLabel: monitor.label,
|
|
403
|
+
error: String(err)
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
} catch (err) {
|
|
408
|
+
this.logger.warn("Failed to capture snapshot", {
|
|
409
|
+
tags: { deviceId: Number(deviceId) },
|
|
410
|
+
meta: { error: String(err) }
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
async processMonitor(deviceId, monitor, snapshot, encoder) {
|
|
415
|
+
const bbox = {
|
|
416
|
+
x: monitor.roi.x * snapshot.width,
|
|
417
|
+
y: monitor.roi.y * snapshot.height,
|
|
418
|
+
w: monitor.roi.w * snapshot.width,
|
|
419
|
+
h: monitor.roi.h * snapshot.height
|
|
420
|
+
};
|
|
421
|
+
const { crop, width, height } = await extractCrop(snapshot.data, snapshot.width, snapshot.height, bbox);
|
|
422
|
+
const { embedding: imageEmb } = await encoder.encode(crop, width, height);
|
|
423
|
+
let bestState = null;
|
|
424
|
+
let bestScore = -1;
|
|
425
|
+
for (const state of monitor.states) {
|
|
426
|
+
let score = 0;
|
|
427
|
+
if (state.referenceEmbeddings && state.referenceEmbeddings.length > 0) for (const refEmb of state.referenceEmbeddings) {
|
|
428
|
+
const sim = cosineSimilarity(imageEmb, new Float32Array(refEmb));
|
|
429
|
+
score = Math.max(score, sim);
|
|
430
|
+
}
|
|
431
|
+
else if (state.textPrompts && state.textPrompts.length > 0) for (const prompt of state.textPrompts) {
|
|
432
|
+
const cacheKey = `${monitor.id}:${state.id}:${prompt}`;
|
|
433
|
+
let textEmb = this.textCache.get(cacheKey);
|
|
434
|
+
if (!textEmb) {
|
|
435
|
+
textEmb = (await encoder.encodeText(prompt)).embedding;
|
|
436
|
+
this.textCache.set(cacheKey, textEmb);
|
|
437
|
+
}
|
|
438
|
+
const sim = cosineSimilarity(imageEmb, textEmb);
|
|
439
|
+
score = Math.max(score, sim);
|
|
440
|
+
}
|
|
441
|
+
if (score > bestScore) {
|
|
442
|
+
bestScore = score;
|
|
443
|
+
bestState = state.label;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (!bestState) return;
|
|
447
|
+
const key = `${deviceId}:${monitor.id}`;
|
|
448
|
+
let ms = this.monitorStates.get(key);
|
|
449
|
+
if (!ms) {
|
|
450
|
+
ms = {
|
|
451
|
+
currentState: null,
|
|
452
|
+
pendingState: null,
|
|
453
|
+
pendingCount: 0,
|
|
454
|
+
pendingConfidence: 0
|
|
455
|
+
};
|
|
456
|
+
this.monitorStates.set(key, ms);
|
|
457
|
+
}
|
|
458
|
+
if (bestState === ms.pendingState) {
|
|
459
|
+
ms.pendingCount++;
|
|
460
|
+
ms.pendingConfidence = bestScore;
|
|
461
|
+
} else {
|
|
462
|
+
ms.pendingState = bestState;
|
|
463
|
+
ms.pendingCount = 1;
|
|
464
|
+
ms.pendingConfidence = bestScore;
|
|
465
|
+
}
|
|
466
|
+
if (ms.pendingCount < this.config.hysteresisCount) return;
|
|
467
|
+
if (bestState === ms.currentState) return;
|
|
468
|
+
const previousState = ms.currentState ?? "unknown";
|
|
469
|
+
ms.currentState = bestState;
|
|
470
|
+
ms.pendingState = null;
|
|
471
|
+
ms.pendingCount = 0;
|
|
472
|
+
const data = {
|
|
473
|
+
deviceId: Number(deviceId),
|
|
474
|
+
monitorId: monitor.id,
|
|
475
|
+
monitorLabel: monitor.label,
|
|
476
|
+
previousState,
|
|
477
|
+
currentState: bestState,
|
|
478
|
+
confidence: bestScore,
|
|
479
|
+
timestamp: Date.now()
|
|
480
|
+
};
|
|
481
|
+
this.eventBus.emit({
|
|
482
|
+
id: `scene-state-${deviceId}-${monitor.id}-${Date.now()}`,
|
|
483
|
+
category: EventCategory.EnrichmentSceneStateChanged,
|
|
484
|
+
source: {
|
|
485
|
+
type: "device",
|
|
486
|
+
id: deviceId
|
|
487
|
+
},
|
|
488
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
489
|
+
data
|
|
490
|
+
});
|
|
491
|
+
this.logger.info("Scene state changed", {
|
|
492
|
+
tags: { deviceId: Number(deviceId) },
|
|
493
|
+
meta: {
|
|
494
|
+
monitorLabel: monitor.label,
|
|
495
|
+
previousState,
|
|
496
|
+
currentState: bestState,
|
|
497
|
+
confidence: Number(bestScore.toFixed(2))
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
};
|
|
494
502
|
function cosineSimilarity(a, b) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
503
|
+
let dot = 0;
|
|
504
|
+
let normA = 0;
|
|
505
|
+
let normB = 0;
|
|
506
|
+
for (let i = 0; i < a.length; i++) {
|
|
507
|
+
dot += a[i] * b[i];
|
|
508
|
+
normA += a[i] * a[i];
|
|
509
|
+
normB += b[i] * b[i];
|
|
510
|
+
}
|
|
511
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
504
512
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
const stored = asJsonObject(await this.ctx.api?.settingsStore.get.query({ collection: "addon-settings", key: "enrichment:global" }));
|
|
828
|
-
if (stored) {
|
|
829
|
-
const merged = { ...DEFAULT_ENRICHMENT_CONFIG, ...stored };
|
|
830
|
-
return merged;
|
|
831
|
-
}
|
|
832
|
-
} catch {
|
|
833
|
-
}
|
|
834
|
-
return DEFAULT_ENRICHMENT_CONFIG;
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
export {
|
|
838
|
-
EnrichmentEngineAddon,
|
|
839
|
-
EnrichmentEngineAddon as default
|
|
513
|
+
//#endregion
|
|
514
|
+
//#region src/enrichment-engine/workers/activity-summary.ts
|
|
515
|
+
var ActivitySummaryWorker = class {
|
|
516
|
+
config;
|
|
517
|
+
eventBus;
|
|
518
|
+
store;
|
|
519
|
+
logger;
|
|
520
|
+
buffers = /* @__PURE__ */ new Map();
|
|
521
|
+
summaryTimer = null;
|
|
522
|
+
unsubDetection = null;
|
|
523
|
+
unsubSceneState = null;
|
|
524
|
+
_lastSummary = null;
|
|
525
|
+
constructor(deps) {
|
|
526
|
+
this.config = deps.config;
|
|
527
|
+
this.eventBus = deps.eventBus;
|
|
528
|
+
this.store = deps.store;
|
|
529
|
+
this.logger = deps.logger;
|
|
530
|
+
}
|
|
531
|
+
async start() {
|
|
532
|
+
if (!this.config.enabled) {
|
|
533
|
+
this.logger.info("ActivitySummaryWorker disabled");
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
this.unsubDetection = this.eventBus.subscribe({ category: EventCategory.DetectionResult }, (event) => {
|
|
537
|
+
this.handleDetection(event);
|
|
538
|
+
});
|
|
539
|
+
this.unsubSceneState = this.eventBus.subscribe({ category: EventCategory.EnrichmentSceneStateChanged }, (event) => {
|
|
540
|
+
this.handleSceneStateChange(event);
|
|
541
|
+
});
|
|
542
|
+
this.summaryTimer = setInterval(() => {
|
|
543
|
+
this.emitSummaries();
|
|
544
|
+
}, this.config.intervalSec * 1e3);
|
|
545
|
+
this.logger.info("ActivitySummaryWorker started", { meta: { intervalSec: this.config.intervalSec } });
|
|
546
|
+
}
|
|
547
|
+
async stop() {
|
|
548
|
+
this.unsubDetection?.();
|
|
549
|
+
this.unsubSceneState?.();
|
|
550
|
+
if (this.summaryTimer) {
|
|
551
|
+
clearInterval(this.summaryTimer);
|
|
552
|
+
this.summaryTimer = null;
|
|
553
|
+
}
|
|
554
|
+
await this.emitSummaries();
|
|
555
|
+
}
|
|
556
|
+
get lastSummary() {
|
|
557
|
+
return this._lastSummary;
|
|
558
|
+
}
|
|
559
|
+
getBuffer(deviceId) {
|
|
560
|
+
let buf = this.buffers.get(deviceId);
|
|
561
|
+
if (!buf) {
|
|
562
|
+
buf = {
|
|
563
|
+
tracks: /* @__PURE__ */ new Map(),
|
|
564
|
+
zoneEvents: [],
|
|
565
|
+
stateChanges: []
|
|
566
|
+
};
|
|
567
|
+
this.buffers.set(deviceId, buf);
|
|
568
|
+
}
|
|
569
|
+
return buf;
|
|
570
|
+
}
|
|
571
|
+
handleDetection(event) {
|
|
572
|
+
const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
|
|
573
|
+
if (!deviceId) return;
|
|
574
|
+
const results = event.data.analysisResults ?? [];
|
|
575
|
+
const buf = this.getBuffer(deviceId);
|
|
576
|
+
const now = Date.now();
|
|
577
|
+
for (const det of results) {
|
|
578
|
+
const detection = det.detection;
|
|
579
|
+
if (!detection) continue;
|
|
580
|
+
const trackId = detection.trackId ?? `unknown-${now}`;
|
|
581
|
+
const existing = buf.tracks.get(trackId);
|
|
582
|
+
if (existing) existing.lastSeen = now;
|
|
583
|
+
else buf.tracks.set(trackId, {
|
|
584
|
+
class: detection.class,
|
|
585
|
+
deviceId,
|
|
586
|
+
firstSeen: now,
|
|
587
|
+
lastSeen: now,
|
|
588
|
+
zones: /* @__PURE__ */ new Set()
|
|
589
|
+
});
|
|
590
|
+
const zoneEvents = det.zoneEvents ?? [];
|
|
591
|
+
for (const ze of zoneEvents) {
|
|
592
|
+
const track = buf.tracks.get(trackId);
|
|
593
|
+
if (track) track.zones.add(ze.zoneId);
|
|
594
|
+
if (ze.type === "zone-enter" || ze.type === "zone-exit") buf.zoneEvents.push({
|
|
595
|
+
type: ze.type === "zone-enter" ? "enter" : "exit",
|
|
596
|
+
zoneId: ze.zoneId,
|
|
597
|
+
trackId,
|
|
598
|
+
timestamp: ze.timestamp ?? now
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
handleSceneStateChange(event) {
|
|
604
|
+
const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
|
|
605
|
+
if (!deviceId) return;
|
|
606
|
+
const data = event.data;
|
|
607
|
+
this.getBuffer(deviceId).stateChanges.push({
|
|
608
|
+
monitorId: data.monitorId,
|
|
609
|
+
from: data.previousState,
|
|
610
|
+
to: data.currentState,
|
|
611
|
+
timestamp: data.timestamp
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
async emitSummaries() {
|
|
615
|
+
const now = Date.now();
|
|
616
|
+
for (const [deviceId, buf] of this.buffers) {
|
|
617
|
+
if (buf.tracks.size === 0 && buf.zoneEvents.length === 0 && buf.stateChanges.length === 0) continue;
|
|
618
|
+
const objectCounts = {};
|
|
619
|
+
for (const track of buf.tracks.values()) objectCounts[track.class] = (objectCounts[track.class] ?? 0) + 1;
|
|
620
|
+
const zoneMap = /* @__PURE__ */ new Map();
|
|
621
|
+
for (const ze of buf.zoneEvents) {
|
|
622
|
+
let z = zoneMap.get(ze.zoneId);
|
|
623
|
+
if (!z) {
|
|
624
|
+
z = {
|
|
625
|
+
entries: 0,
|
|
626
|
+
exits: 0,
|
|
627
|
+
dwellTimes: []
|
|
628
|
+
};
|
|
629
|
+
zoneMap.set(ze.zoneId, z);
|
|
630
|
+
}
|
|
631
|
+
if (ze.type === "enter") z.entries++;
|
|
632
|
+
else z.exits++;
|
|
633
|
+
}
|
|
634
|
+
const zoneActivity = [...zoneMap.entries()].map(([zoneId, z]) => ({
|
|
635
|
+
zoneId,
|
|
636
|
+
entries: z.entries,
|
|
637
|
+
exits: z.exits,
|
|
638
|
+
avgDwellMs: z.dwellTimes.length > 0 ? z.dwellTimes.reduce((a, b) => a + b, 0) / z.dwellTimes.length : 0
|
|
639
|
+
}));
|
|
640
|
+
const eventsPerMin = (buf.tracks.size + buf.zoneEvents.length) / (this.config.intervalSec / 60);
|
|
641
|
+
const activityLevel = eventsPerMin >= this.config.activityThresholds.high ? "high" : eventsPerMin >= this.config.activityThresholds.medium ? "medium" : eventsPerMin >= this.config.activityThresholds.low ? "low" : "none";
|
|
642
|
+
const summary = {
|
|
643
|
+
deviceId: Number(deviceId),
|
|
644
|
+
periodStart: now - this.config.intervalSec * 1e3,
|
|
645
|
+
periodEnd: now,
|
|
646
|
+
objectCounts,
|
|
647
|
+
zoneActivity,
|
|
648
|
+
stateChanges: [...buf.stateChanges],
|
|
649
|
+
activityLevel
|
|
650
|
+
};
|
|
651
|
+
this._lastSummary = summary;
|
|
652
|
+
this.eventBus.emit(createEvent(EventCategory.EnrichmentActivitySummary, {
|
|
653
|
+
type: "device",
|
|
654
|
+
id: deviceId
|
|
655
|
+
}, summary));
|
|
656
|
+
try {
|
|
657
|
+
await this.store.insert.mutate({
|
|
658
|
+
collection: "addon-settings",
|
|
659
|
+
record: {
|
|
660
|
+
id: `${deviceId}:${now}`,
|
|
661
|
+
data: { ...summary }
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
} catch {}
|
|
665
|
+
buf.tracks.clear();
|
|
666
|
+
buf.zoneEvents.length = 0;
|
|
667
|
+
buf.stateChanges.length = 0;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
//#endregion
|
|
672
|
+
//#region src/enrichment-engine/index.ts
|
|
673
|
+
/**
|
|
674
|
+
* Extended context shape injected at runtime by the server's capability wiring.
|
|
675
|
+
* Not part of the base AddonContext interface because capabilities are resolved
|
|
676
|
+
* after addon initialization.
|
|
677
|
+
*/
|
|
678
|
+
var EnrichmentEngineAddon = class extends BaseAddon {
|
|
679
|
+
embeddingDispatcher = null;
|
|
680
|
+
sceneStateWorker = null;
|
|
681
|
+
activitySummary = null;
|
|
682
|
+
/**
|
|
683
|
+
* Shared shm-ring reader cache for downstream frame access. Owned by the
|
|
684
|
+
* engine so the cached segments stay open across worker restarts and close
|
|
685
|
+
* exactly once on shutdown.
|
|
686
|
+
*/
|
|
687
|
+
readers = null;
|
|
688
|
+
currentFlags = {
|
|
689
|
+
embeddingEnabled: true,
|
|
690
|
+
sceneMonitorEnabled: true,
|
|
691
|
+
activitySummaryEnabled: true
|
|
692
|
+
};
|
|
693
|
+
constructor() {
|
|
694
|
+
super({});
|
|
695
|
+
}
|
|
696
|
+
async onInitialize() {
|
|
697
|
+
const config = await this.loadConfig();
|
|
698
|
+
const encoderCollection = this.capabilities?.getCollection?.("embedding-encoder") ?? [];
|
|
699
|
+
const streamBrokerRaw = this.capabilities?.get?.("stream-broker");
|
|
700
|
+
const rawNodeId = this.ctx.kernel.localNodeId ?? this.ctx.id;
|
|
701
|
+
const ownNodeId = rawNodeId.includes("/") ? rawNodeId.split("/")[0] : rawNodeId;
|
|
702
|
+
this.readers = new FrameRingReaderCache(this.ctx.logger.child("shm-readers"));
|
|
703
|
+
const getRemoteFrame = async (handle) => {
|
|
704
|
+
const remote = await this.ctx.api.decoder.getFrame.query({
|
|
705
|
+
handle,
|
|
706
|
+
nodeId: handle.nodeId
|
|
707
|
+
});
|
|
708
|
+
if (!remote) return null;
|
|
709
|
+
return {
|
|
710
|
+
data: Buffer.from(remote.data),
|
|
711
|
+
width: remote.width,
|
|
712
|
+
height: remote.height,
|
|
713
|
+
format: remote.format,
|
|
714
|
+
timestamp: remote.timestamp
|
|
715
|
+
};
|
|
716
|
+
};
|
|
717
|
+
this.embeddingDispatcher = new EmbeddingDispatcher({
|
|
718
|
+
config: config.embedding,
|
|
719
|
+
encoders: encoderCollection,
|
|
720
|
+
eventBus: this.ctx.eventBus,
|
|
721
|
+
logger: this.ctx.logger.child("EmbeddingDispatcher"),
|
|
722
|
+
ownNodeId,
|
|
723
|
+
readers: this.readers,
|
|
724
|
+
getRemoteFrame
|
|
725
|
+
});
|
|
726
|
+
this.sceneStateWorker = new SceneStateWorker({
|
|
727
|
+
config: config.sceneMonitor,
|
|
728
|
+
encoders: encoderCollection,
|
|
729
|
+
eventBus: this.ctx.eventBus,
|
|
730
|
+
store: this.ctx.api.settingsStore,
|
|
731
|
+
streamBrokerRegistry: streamBrokerRaw ?? { getSnapshot: async () => null },
|
|
732
|
+
logger: this.ctx.logger.child("SceneStateWorker")
|
|
733
|
+
});
|
|
734
|
+
this.activitySummary = new ActivitySummaryWorker({
|
|
735
|
+
config: config.activitySummary,
|
|
736
|
+
eventBus: this.ctx.eventBus,
|
|
737
|
+
store: this.ctx.api.settingsStore,
|
|
738
|
+
logger: this.ctx.logger.child("ActivitySummary")
|
|
739
|
+
});
|
|
740
|
+
this.currentFlags = {
|
|
741
|
+
embeddingEnabled: config.embedding.enabled,
|
|
742
|
+
sceneMonitorEnabled: config.sceneMonitor.enabled,
|
|
743
|
+
activitySummaryEnabled: config.activitySummary.enabled
|
|
744
|
+
};
|
|
745
|
+
await this.embeddingDispatcher.start();
|
|
746
|
+
await this.sceneStateWorker.start();
|
|
747
|
+
await this.activitySummary.start();
|
|
748
|
+
this.ctx.logger.info("Enrichment engine initialized with 3 workers");
|
|
749
|
+
}
|
|
750
|
+
async onShutdown() {
|
|
751
|
+
await this.embeddingDispatcher?.stop();
|
|
752
|
+
await this.sceneStateWorker?.stop();
|
|
753
|
+
await this.activitySummary?.stop();
|
|
754
|
+
this.embeddingDispatcher = null;
|
|
755
|
+
this.sceneStateWorker = null;
|
|
756
|
+
this.activitySummary = null;
|
|
757
|
+
this.readers?.close();
|
|
758
|
+
this.readers = null;
|
|
759
|
+
}
|
|
760
|
+
globalSettingsSchema() {
|
|
761
|
+
return this.schema({ sections: [{
|
|
762
|
+
id: "enrichment-engine-settings",
|
|
763
|
+
title: "Enrichment Engine",
|
|
764
|
+
columns: 2,
|
|
765
|
+
fields: [
|
|
766
|
+
{
|
|
767
|
+
type: "boolean",
|
|
768
|
+
key: "embeddingEnabled",
|
|
769
|
+
label: "Embedding Enabled",
|
|
770
|
+
description: "Compute face/object embeddings from detection crops.",
|
|
771
|
+
default: true
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
type: "boolean",
|
|
775
|
+
key: "sceneMonitorEnabled",
|
|
776
|
+
label: "Scene Monitor Enabled",
|
|
777
|
+
description: "Run periodic scene state capture for change detection.",
|
|
778
|
+
default: true
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
type: "boolean",
|
|
782
|
+
key: "activitySummaryEnabled",
|
|
783
|
+
label: "Activity Summary Enabled",
|
|
784
|
+
description: "Aggregate hourly activity summaries per camera.",
|
|
785
|
+
default: true
|
|
786
|
+
}
|
|
787
|
+
]
|
|
788
|
+
}] });
|
|
789
|
+
}
|
|
790
|
+
async updateGlobalSettings(patch) {
|
|
791
|
+
await this.ctx?.settings?.writeAddonStore(patch);
|
|
792
|
+
const prev = this.currentFlags;
|
|
793
|
+
const next = {
|
|
794
|
+
embeddingEnabled: typeof patch["embeddingEnabled"] === "boolean" ? patch["embeddingEnabled"] : prev.embeddingEnabled,
|
|
795
|
+
sceneMonitorEnabled: typeof patch["sceneMonitorEnabled"] === "boolean" ? patch["sceneMonitorEnabled"] : prev.sceneMonitorEnabled,
|
|
796
|
+
activitySummaryEnabled: typeof patch["activitySummaryEnabled"] === "boolean" ? patch["activitySummaryEnabled"] : prev.activitySummaryEnabled
|
|
797
|
+
};
|
|
798
|
+
this.currentFlags = next;
|
|
799
|
+
if (prev.embeddingEnabled && !next.embeddingEnabled) {
|
|
800
|
+
this.ctx?.logger.info("Stopping embedding dispatcher (disabled via config)");
|
|
801
|
+
await this.embeddingDispatcher?.stop();
|
|
802
|
+
} else if (!prev.embeddingEnabled && next.embeddingEnabled) {
|
|
803
|
+
this.ctx?.logger.info("Starting embedding dispatcher (enabled via config)");
|
|
804
|
+
await this.embeddingDispatcher?.start();
|
|
805
|
+
}
|
|
806
|
+
if (prev.sceneMonitorEnabled && !next.sceneMonitorEnabled) {
|
|
807
|
+
this.ctx?.logger.info("Stopping scene state worker (disabled via config)");
|
|
808
|
+
await this.sceneStateWorker?.stop();
|
|
809
|
+
} else if (!prev.sceneMonitorEnabled && next.sceneMonitorEnabled) {
|
|
810
|
+
this.ctx?.logger.info("Starting scene state worker (enabled via config)");
|
|
811
|
+
await this.sceneStateWorker?.start();
|
|
812
|
+
}
|
|
813
|
+
if (prev.activitySummaryEnabled && !next.activitySummaryEnabled) {
|
|
814
|
+
this.ctx?.logger.info("Stopping activity summary worker (disabled via config)");
|
|
815
|
+
await this.activitySummary?.stop();
|
|
816
|
+
} else if (!prev.activitySummaryEnabled && next.activitySummaryEnabled) {
|
|
817
|
+
this.ctx?.logger.info("Starting activity summary worker (enabled via config)");
|
|
818
|
+
await this.activitySummary?.start();
|
|
819
|
+
}
|
|
820
|
+
this.ctx?.logger.info("Enrichment engine flags updated", { meta: { flags: this.currentFlags } });
|
|
821
|
+
}
|
|
822
|
+
async loadConfig() {
|
|
823
|
+
try {
|
|
824
|
+
const stored = asJsonObject(await this.ctx.api?.settingsStore.get.query({
|
|
825
|
+
collection: "addon-settings",
|
|
826
|
+
key: "enrichment:global"
|
|
827
|
+
}));
|
|
828
|
+
if (stored) return {
|
|
829
|
+
...DEFAULT_ENRICHMENT_CONFIG,
|
|
830
|
+
...stored
|
|
831
|
+
};
|
|
832
|
+
} catch {}
|
|
833
|
+
return DEFAULT_ENRICHMENT_CONFIG;
|
|
834
|
+
}
|
|
840
835
|
};
|
|
841
|
-
//#
|
|
836
|
+
//#endregion
|
|
837
|
+
export { EnrichmentEngineAddon, EnrichmentEngineAddon as default };
|