iobroker.agent-dvr 0.8.1 → 0.9.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/README.de.md +113 -0
- package/README.md +113 -9
- package/admin/assets/{index-CUI5sWkp.js → index-Dy3EUVMO.js} +2 -2
- package/admin/i18n/de.json +25 -1
- package/admin/i18n/en.json +25 -1
- package/admin/i18n/es.json +25 -1
- package/admin/i18n/fr.json +25 -1
- package/admin/i18n/it.json +25 -1
- package/admin/i18n/nl.json +25 -1
- package/admin/i18n/pl.json +25 -1
- package/admin/i18n/pt.json +25 -1
- package/admin/i18n/ru.json +25 -1
- package/admin/i18n/uk.json +25 -1
- package/admin/i18n/zh-cn.json +25 -1
- package/admin/index_m.html +1 -1
- package/build/lib/recording.js +447 -0
- package/build/lib/recording.js.map +7 -0
- package/build/main.js +621 -9
- package/build/main.js.map +3 -3
- package/io-package.json +34 -14
- package/lib/web.js +36 -0
- package/package.json +3 -3
package/build/main.js
CHANGED
|
@@ -24,8 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
var utils = __toESM(require("@iobroker/adapter-core"));
|
|
25
25
|
var http = __toESM(require("node:http"));
|
|
26
26
|
var https = __toESM(require("node:https"));
|
|
27
|
+
var fs = __toESM(require("node:fs"));
|
|
27
28
|
var path = __toESM(require("node:path"));
|
|
28
29
|
var import_widget_i18n = require("./lib/widget-i18n");
|
|
30
|
+
var import_recording = require("./lib/recording");
|
|
29
31
|
const CAM_COMMANDS = [
|
|
30
32
|
{ id: "record", path: "/command/record", params: ["oid", "ot"], name: "Start recording" },
|
|
31
33
|
{ id: "recordStop", path: "/command/recordStop", params: ["oid", "ot"], name: "Stop recording" },
|
|
@@ -338,6 +340,18 @@ class AgentDvr extends utils.Adapter {
|
|
|
338
340
|
camAspect = {};
|
|
339
341
|
devById = /* @__PURE__ */ new Map();
|
|
340
342
|
eventBusy = {};
|
|
343
|
+
// recordings in the file storage: detected ffmpeg, one save at a time per camera (+ the newest waiting one)
|
|
344
|
+
ffmpeg = void 0;
|
|
345
|
+
ffmpegDetecting = void 0;
|
|
346
|
+
recBusy = {};
|
|
347
|
+
recPending = {};
|
|
348
|
+
audioDropLogged = false;
|
|
349
|
+
noFfmpegLogged = false;
|
|
350
|
+
noLocalPathLogged = false;
|
|
351
|
+
// initial fill of the recording slots runs one camera after the other, after the start cleanup
|
|
352
|
+
recGate = () => {
|
|
353
|
+
};
|
|
354
|
+
recChain = new Promise((resolve) => this.recGate = resolve);
|
|
341
355
|
constructor(options = {}) {
|
|
342
356
|
super({ ...options, name: "agent-dvr" });
|
|
343
357
|
this.on("ready", this.onReady.bind(this));
|
|
@@ -369,11 +383,25 @@ class AgentDvr extends utils.Adapter {
|
|
|
369
383
|
this.authHeader = `Basic ${Buffer.from(`${this.config.user}:${this.config.pass}`).toString("base64")}`;
|
|
370
384
|
}
|
|
371
385
|
await this.buildSystem();
|
|
386
|
+
await this.setObjectNotExistsAsync("info.ffmpeg", {
|
|
387
|
+
type: "state",
|
|
388
|
+
common: {
|
|
389
|
+
name: "ffmpeg (version and path)",
|
|
390
|
+
type: "string",
|
|
391
|
+
role: "text",
|
|
392
|
+
read: true,
|
|
393
|
+
write: false,
|
|
394
|
+
def: ""
|
|
395
|
+
},
|
|
396
|
+
native: {}
|
|
397
|
+
});
|
|
398
|
+
void this.runFfmpegDetection();
|
|
372
399
|
try {
|
|
373
400
|
await this.poll();
|
|
374
401
|
} catch (e) {
|
|
375
402
|
this.log.warn(`First poll error: ${e.message}`);
|
|
376
403
|
}
|
|
404
|
+
void this.syncRecordingStorage().catch((e) => this.log.warn(`Recording cleanup: ${e.message}`)).finally(() => this.recGate());
|
|
377
405
|
this.schedulePoll(pollSeconds * 1e3);
|
|
378
406
|
this.subscribeStates("*");
|
|
379
407
|
this.subscribeForeignObjects("system.adapter.web.*");
|
|
@@ -411,6 +439,24 @@ class AgentDvr extends utils.Adapter {
|
|
|
411
439
|
void this.fetchAvailableSources().then((result) => {
|
|
412
440
|
this.sendTo(obj.from, obj.command, result, obj.callback);
|
|
413
441
|
});
|
|
442
|
+
} else if (obj.command === "detectFfmpeg") {
|
|
443
|
+
const p = typeof obj.message === "object" && obj.message !== null && typeof obj.message.path === "string" ? obj.message.path : void 0;
|
|
444
|
+
void this.runFfmpegDetection(p).then((info) => {
|
|
445
|
+
if (obj.callback) {
|
|
446
|
+
this.sendTo(obj.from, obj.command, info, obj.callback);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
} else if (obj.command === "sendRecording" || obj.command === "getRecording") {
|
|
450
|
+
const msg = typeof obj.message === "object" && obj.message !== null ? obj.message : {};
|
|
451
|
+
const job = obj.command === "sendRecording" ? this.sendRecording(msg) : this.getRecording(msg);
|
|
452
|
+
void job.catch((e) => ({ ok: false, error: e.message })).then((result) => {
|
|
453
|
+
if (!result.ok) {
|
|
454
|
+
this.log.warn(`${obj.command}: ${result.error}`);
|
|
455
|
+
}
|
|
456
|
+
if (obj.callback) {
|
|
457
|
+
this.sendTo(obj.from, obj.command, (0, import_recording.safeMessage)(result), obj.callback);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
414
460
|
}
|
|
415
461
|
}
|
|
416
462
|
async fetchAvailableSources() {
|
|
@@ -934,14 +980,7 @@ class AgentDvr extends utils.Adapter {
|
|
|
934
980
|
return;
|
|
935
981
|
}
|
|
936
982
|
const ch = `${fid}.alertPhotos`;
|
|
937
|
-
|
|
938
|
-
await this.setForeignObjectNotExistsAsync(this.namespace, {
|
|
939
|
-
type: "meta",
|
|
940
|
-
common: { name: "Alert photos", type: "meta.user" },
|
|
941
|
-
native: {}
|
|
942
|
-
});
|
|
943
|
-
this.ensuredFolders.add(this.namespace);
|
|
944
|
-
}
|
|
983
|
+
await this.ensureFileMeta();
|
|
945
984
|
await this.ensureAlertPhotoObjects(ch);
|
|
946
985
|
for (let i = 2; i >= 1; i--) {
|
|
947
986
|
try {
|
|
@@ -995,6 +1034,7 @@ class AgentDvr extends utils.Adapter {
|
|
|
995
1034
|
});
|
|
996
1035
|
}
|
|
997
1036
|
}
|
|
1037
|
+
await this.refreshRecordingUrls();
|
|
998
1038
|
}
|
|
999
1039
|
// http(s) URL of a stored alert photo via the web extension, for apps that only accept web links
|
|
1000
1040
|
async alertPhotoUrl(fid, n) {
|
|
@@ -1053,6 +1093,563 @@ class AgentDvr extends utils.Adapter {
|
|
|
1053
1093
|
alertPhotoPath(fid, n) {
|
|
1054
1094
|
return path.join(utils.getAbsoluteDefaultDataDir(), "files", this.namespace, fid, `grab${n}.jpg`);
|
|
1055
1095
|
}
|
|
1096
|
+
// ---- recordings in the file storage + sendRecording / getRecording ----
|
|
1097
|
+
// Detects ffmpeg (configured path → PATH → common locations) and writes info.ffmpeg.
|
|
1098
|
+
// Concurrent calls share one run.
|
|
1099
|
+
runFfmpegDetection(configPath) {
|
|
1100
|
+
var _a;
|
|
1101
|
+
if (this.ffmpegDetecting && configPath === void 0) {
|
|
1102
|
+
return this.ffmpegDetecting;
|
|
1103
|
+
}
|
|
1104
|
+
const cfg = (_a = configPath != null ? configPath : this.config.ffmpegPath) != null ? _a : "";
|
|
1105
|
+
const run = (0, import_recording.detectFfmpeg)((0, import_recording.ffmpegCandidates)(cfg, process.platform)).catch(() => ({ found: false, version: "", path: "" })).then(async (found) => {
|
|
1106
|
+
const info = found.found ? { ...found, path: (0, import_recording.resolveOnPath)(found.path, process.platform) } : found;
|
|
1107
|
+
this.ffmpeg = info;
|
|
1108
|
+
this.noFfmpegLogged = false;
|
|
1109
|
+
if (info.found) {
|
|
1110
|
+
this.log.info(`ffmpeg ${info.version} found (${info.path})`);
|
|
1111
|
+
} else {
|
|
1112
|
+
this.log.debug("ffmpeg not found");
|
|
1113
|
+
}
|
|
1114
|
+
await this.setStateAsync("info.ffmpeg", { val: (0, import_recording.ffmpegStatusText)(info), ack: true }).catch(() => {
|
|
1115
|
+
});
|
|
1116
|
+
return info;
|
|
1117
|
+
}).finally(() => {
|
|
1118
|
+
if (this.ffmpegDetecting === run) {
|
|
1119
|
+
this.ffmpegDetecting = void 0;
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1122
|
+
this.ffmpegDetecting = run;
|
|
1123
|
+
return run;
|
|
1124
|
+
}
|
|
1125
|
+
recordingCamEnabled(fid) {
|
|
1126
|
+
if (!this.config.enableRecordingFiles) {
|
|
1127
|
+
return false;
|
|
1128
|
+
}
|
|
1129
|
+
const sel = Array.isArray(this.config.recordingFileCams) ? this.config.recordingFileCams : [];
|
|
1130
|
+
return sel.length === 0 || sel.includes(fid);
|
|
1131
|
+
}
|
|
1132
|
+
recordingTimeoutMs() {
|
|
1133
|
+
return (0, import_recording.clampNumber)(this.config.recordingTimeoutSec, 120, 10, 900) * 1e3;
|
|
1134
|
+
}
|
|
1135
|
+
// One save per camera at a time; a recording arriving meanwhile waits (only the newest one).
|
|
1136
|
+
queueRecordingSave(d, fid, ev) {
|
|
1137
|
+
if (this.recBusy[fid]) {
|
|
1138
|
+
this.recPending[fid] = { d, ev };
|
|
1139
|
+
return Promise.resolve();
|
|
1140
|
+
}
|
|
1141
|
+
this.recBusy[fid] = true;
|
|
1142
|
+
return (async () => {
|
|
1143
|
+
let job = { d, ev };
|
|
1144
|
+
while (job && !this._pollStopped) {
|
|
1145
|
+
try {
|
|
1146
|
+
await this.saveRecording(job.d, fid, job.ev);
|
|
1147
|
+
} catch (e) {
|
|
1148
|
+
this.log.warn(`Recording file ${fid}: ${e.message}`);
|
|
1149
|
+
}
|
|
1150
|
+
job = this.recPending[fid];
|
|
1151
|
+
delete this.recPending[fid];
|
|
1152
|
+
}
|
|
1153
|
+
this.recBusy[fid] = false;
|
|
1154
|
+
})();
|
|
1155
|
+
}
|
|
1156
|
+
// Downloads a recording from AgentDVR into memory (never to disk), with size limit and timeout.
|
|
1157
|
+
fetchRecording(oid, fn, maxBytes) {
|
|
1158
|
+
return (0, import_recording.downloadToBuffer)(
|
|
1159
|
+
{
|
|
1160
|
+
hostname: this.config.serverIp,
|
|
1161
|
+
port: this.config.port || 8090,
|
|
1162
|
+
path: `/streamFile.cgi?oid=${encodeURIComponent(String(oid))}&ot=2&fn=${encodeURIComponent(fn)}`,
|
|
1163
|
+
headers: this.authHeader ? { Authorization: this.authHeader } : void 0
|
|
1164
|
+
},
|
|
1165
|
+
maxBytes,
|
|
1166
|
+
this.recordingTimeoutMs()
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
// Remuxes to MP4 if wanted and possible; otherwise (or on failure) the data is returned unchanged.
|
|
1170
|
+
async toWantedFormat(data, srcExt, wantMp4, maxBytes) {
|
|
1171
|
+
var _a;
|
|
1172
|
+
if (!wantMp4 || srcExt === "mp4") {
|
|
1173
|
+
return { data, ext: srcExt };
|
|
1174
|
+
}
|
|
1175
|
+
const ff = (_a = this.ffmpeg) != null ? _a : await this.runFfmpegDetection();
|
|
1176
|
+
if (!ff.found) {
|
|
1177
|
+
if (!this.noFfmpegLogged) {
|
|
1178
|
+
this.noFfmpegLogged = true;
|
|
1179
|
+
this.log.info("ffmpeg not found \u2014 recordings are provided as MKV (install ffmpeg for MP4)");
|
|
1180
|
+
}
|
|
1181
|
+
return { data, ext: srcExt };
|
|
1182
|
+
}
|
|
1183
|
+
try {
|
|
1184
|
+
const mp4 = await (0, import_recording.remuxToMp4)(ff.path, data, this.recordingTimeoutMs(), maxBytes, (reason) => {
|
|
1185
|
+
if (!this.audioDropLogged) {
|
|
1186
|
+
this.audioDropLogged = true;
|
|
1187
|
+
this.log.info(
|
|
1188
|
+
`Audio track does not fit into MP4, recordings are remuxed without audio (${reason})`
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
return { data: mp4, ext: "mp4" };
|
|
1193
|
+
} catch (e) {
|
|
1194
|
+
this.log.warn(`MP4 remux failed, keeping ${srcExt.toUpperCase()}: ${e.message}`);
|
|
1195
|
+
return { data, ext: srcExt };
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
async ensureFileMeta() {
|
|
1199
|
+
if (this.ensuredFolders.has(this.namespace)) {
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
await this.setForeignObjectNotExistsAsync(this.namespace, {
|
|
1203
|
+
type: "meta",
|
|
1204
|
+
common: { name: "Alert photos", type: "meta.user" },
|
|
1205
|
+
native: {}
|
|
1206
|
+
});
|
|
1207
|
+
this.ensuredFolders.add(this.namespace);
|
|
1208
|
+
}
|
|
1209
|
+
async ensureRecordingObjects(ch, count) {
|
|
1210
|
+
const key = `${ch}#${count}`;
|
|
1211
|
+
if (this.ensuredFolders.has(key)) {
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
await this.ensureFolder(ch, "Recordings", "channel");
|
|
1215
|
+
const defs = [
|
|
1216
|
+
{ id: "file", name: "file path", type: "string", role: "text" },
|
|
1217
|
+
{ id: "uri", name: "iobfile URI", type: "string", role: "text" },
|
|
1218
|
+
{ id: "url", name: "http URL", type: "string", role: "text.url" },
|
|
1219
|
+
{ id: "name", name: "AgentDVR file name", type: "string", role: "text" },
|
|
1220
|
+
{ id: "ts", name: "time", type: "number", role: "value.time" }
|
|
1221
|
+
];
|
|
1222
|
+
for (let i = 1; i <= count; i++) {
|
|
1223
|
+
for (const def of defs) {
|
|
1224
|
+
await this.setObjectNotExistsAsync(`${ch}.${def.id}${i}`, {
|
|
1225
|
+
type: "state",
|
|
1226
|
+
common: {
|
|
1227
|
+
name: `Recording ${i} (${def.name})`,
|
|
1228
|
+
type: def.type,
|
|
1229
|
+
role: def.role,
|
|
1230
|
+
read: true,
|
|
1231
|
+
write: false
|
|
1232
|
+
},
|
|
1233
|
+
native: {}
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
this.ensuredFolders.add(key);
|
|
1238
|
+
}
|
|
1239
|
+
// Extension of the file stored in a slot, or '' if the slot is empty.
|
|
1240
|
+
async slotExt(fid, slot) {
|
|
1241
|
+
for (const ext of ["mp4", "mkv"]) {
|
|
1242
|
+
if (await this.fileExistsAsync(this.namespace, (0, import_recording.recordingStoragePath)(fid, slot, ext)).catch(() => false)) {
|
|
1243
|
+
return ext;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return "";
|
|
1247
|
+
}
|
|
1248
|
+
async deleteSlotFiles(fid, slot) {
|
|
1249
|
+
for (const ext of ["mp4", "mkv"]) {
|
|
1250
|
+
const p = (0, import_recording.recordingStoragePath)(fid, slot, ext);
|
|
1251
|
+
if (await this.fileExistsAsync(this.namespace, p).catch(() => false)) {
|
|
1252
|
+
await this.delFileAsync(this.namespace, p).catch(() => {
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
// Local path only if the file storage is on this host's disk (file/jsonl objects DB), else ''.
|
|
1258
|
+
recordingLocalPath(fid, slot, ext) {
|
|
1259
|
+
const abs = (0, import_recording.recordingAbsPath)(utils.getAbsoluteDefaultDataDir(), this.namespace, fid, slot, ext);
|
|
1260
|
+
if (fs.existsSync(abs)) {
|
|
1261
|
+
return abs;
|
|
1262
|
+
}
|
|
1263
|
+
if (!this.noLocalPathLogged) {
|
|
1264
|
+
this.noLocalPathLogged = true;
|
|
1265
|
+
this.log.info(
|
|
1266
|
+
"Recording files are not on the local disk (non-file objects DB) \u2014 recordings.file* stay empty, use uri*"
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
return "";
|
|
1270
|
+
}
|
|
1271
|
+
async writeSlotStates(fid, slot, ext, name, ts) {
|
|
1272
|
+
const ch = `${fid}.recordings`;
|
|
1273
|
+
await this.setStateAsync(`${ch}.file${slot}`, { val: this.recordingLocalPath(fid, slot, ext), ack: true });
|
|
1274
|
+
await this.setStateAsync(`${ch}.uri${slot}`, { val: (0, import_recording.recordingUri)(this.namespace, fid, slot, ext), ack: true });
|
|
1275
|
+
await this.setStateAsync(`${ch}.url${slot}`, { val: await this.recordingWebUrl(fid, slot, ext), ack: true });
|
|
1276
|
+
await this.setStateAsync(`${ch}.name${slot}`, { val: name, ack: true });
|
|
1277
|
+
await this.setStateAsync(`${ch}.ts${slot}`, { val: ts != null ? ts : null, ack: true });
|
|
1278
|
+
}
|
|
1279
|
+
async recordingWebUrl(fid, slot, ext) {
|
|
1280
|
+
if (this.webBaseUrl === void 0) {
|
|
1281
|
+
this.webBaseUrl = await this.findWebBaseUrl();
|
|
1282
|
+
}
|
|
1283
|
+
return (0, import_recording.recordingUrl)(this.webBaseUrl, this.namespace, fid, slot, ext);
|
|
1284
|
+
}
|
|
1285
|
+
// Keeps the last N recordings per camera in the file storage (<namespace>/<fid>/rec1..N.mp4|mkv,
|
|
1286
|
+
// rec1 = newest), like the alert photos. ts1 is written last: the file is complete by then.
|
|
1287
|
+
async saveRecording(d, fid, ev) {
|
|
1288
|
+
var _a, _b, _c, _d, _e;
|
|
1289
|
+
const fn = (_a = ev.fn) != null ? _a : "";
|
|
1290
|
+
if (!fn) {
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
const maxBytes = (0, import_recording.maxRecordingBytes)(this.config.recordingMaxSizeMb);
|
|
1294
|
+
const listed = Number((_b = ev.sb) != null ? _b : 0);
|
|
1295
|
+
if ((0, import_recording.exceedsLimit)(listed, maxBytes)) {
|
|
1296
|
+
this.log.info(`Recording ${fn} (${(0, import_recording.fmtMb)(listed)}) not stored: larger than ${(0, import_recording.fmtMb)(maxBytes)}`);
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
const dl = await this.fetchRecording(d.oid, fn, maxBytes);
|
|
1300
|
+
if (!dl.ok || !dl.data) {
|
|
1301
|
+
if (dl.tooLarge) {
|
|
1302
|
+
this.log.info(`Recording ${fn} not stored: ${dl.error}`);
|
|
1303
|
+
} else {
|
|
1304
|
+
this.log.warn(`Recording ${fn} not downloaded: ${(_c = dl.error) != null ? _c : "empty response"}`);
|
|
1305
|
+
}
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
const out = await this.toWantedFormat(dl.data, (0, import_recording.extOf)(fn), this.config.recordingMp4 !== false, maxBytes);
|
|
1309
|
+
if ((0, import_recording.exceedsLimit)(out.data.length, maxBytes)) {
|
|
1310
|
+
this.log.info(`Recording ${fn} (${(0, import_recording.fmtMb)(out.data.length)}) not stored: larger than ${(0, import_recording.fmtMb)(maxBytes)}`);
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
const count = (0, import_recording.recordingSlots)(this.config.recordingFileCount);
|
|
1314
|
+
const ch = `${fid}.recordings`;
|
|
1315
|
+
await this.ensureFileMeta();
|
|
1316
|
+
await this.ensureRecordingObjects(ch, count);
|
|
1317
|
+
for (let i = count - 1; i >= 1; i--) {
|
|
1318
|
+
const ext = await this.slotExt(fid, i);
|
|
1319
|
+
if (!ext) {
|
|
1320
|
+
continue;
|
|
1321
|
+
}
|
|
1322
|
+
await this.deleteSlotFiles(fid, i + 1);
|
|
1323
|
+
const from = (0, import_recording.recordingStoragePath)(fid, i, ext);
|
|
1324
|
+
const to = (0, import_recording.recordingStoragePath)(fid, i + 1, ext);
|
|
1325
|
+
try {
|
|
1326
|
+
await this.renameAsync(this.namespace, from, to);
|
|
1327
|
+
} catch {
|
|
1328
|
+
const prev = await this.readFileAsync(this.namespace, from);
|
|
1329
|
+
await this.writeFileAsync(this.namespace, to, prev.file);
|
|
1330
|
+
await this.delFileAsync(this.namespace, from).catch(() => {
|
|
1331
|
+
});
|
|
1332
|
+
}
|
|
1333
|
+
const name = await this.getStateAsync(`${ch}.name${i}`);
|
|
1334
|
+
const ts = await this.getStateAsync(`${ch}.ts${i}`);
|
|
1335
|
+
await this.writeSlotStates(fid, i + 1, ext, String((_d = name == null ? void 0 : name.val) != null ? _d : ""), ts == null ? void 0 : ts.val);
|
|
1336
|
+
}
|
|
1337
|
+
await this.deleteSlotFiles(fid, 1);
|
|
1338
|
+
await this.writeFileAsync(this.namespace, (0, import_recording.recordingStoragePath)(fid, 1, out.ext), out.data);
|
|
1339
|
+
const tsMs = (parseFloat(String((_e = ev.c) != null ? _e : 0)) - 621355968e9) / 1e4;
|
|
1340
|
+
await this.writeSlotStates(
|
|
1341
|
+
fid,
|
|
1342
|
+
1,
|
|
1343
|
+
out.ext,
|
|
1344
|
+
fn,
|
|
1345
|
+
Number.isFinite(tsMs) && tsMs > 0 ? Math.round(tsMs) : Date.now()
|
|
1346
|
+
);
|
|
1347
|
+
await this.pruneRecordingSlots(fid, count);
|
|
1348
|
+
this.log.debug(
|
|
1349
|
+
`Recording ${fn} stored as ${(0, import_recording.recordingStoragePath)(fid, 1, out.ext)} (${(0, import_recording.fmtMb)(out.data.length)})`
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1352
|
+
// Removes files and states of the slots above the configured number.
|
|
1353
|
+
async pruneRecordingSlots(fid, count) {
|
|
1354
|
+
const ch = `${fid}.recordings`;
|
|
1355
|
+
for (let i = count + 1; i <= 3; i++) {
|
|
1356
|
+
await this.deleteSlotFiles(fid, i);
|
|
1357
|
+
for (const id of ["file", "uri", "url", "name", "ts"]) {
|
|
1358
|
+
if (await this.getObjectAsync(`${ch}.${id}${i}`)) {
|
|
1359
|
+
await this.delObjectAsync(`${ch}.${id}${i}`).catch(() => {
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
for (let c = count + 1; c <= 3; c++) {
|
|
1365
|
+
this.ensuredFolders.delete(`${ch}#${c}`);
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
// Start: cameras that are switched off or deselected lose their recordings (states and files),
|
|
1369
|
+
// the others lose the slots above the configured number.
|
|
1370
|
+
async syncRecordingStorage() {
|
|
1371
|
+
const count = (0, import_recording.recordingSlots)(this.config.recordingFileCount);
|
|
1372
|
+
for (const dev of this.devById.values()) {
|
|
1373
|
+
if (dev.ot !== 2) {
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
const fid = this.deviceFolder(dev);
|
|
1377
|
+
if (this.recordingCamEnabled(fid)) {
|
|
1378
|
+
if (await this.getObjectAsync(`${fid}.recordings`)) {
|
|
1379
|
+
await this.pruneRecordingSlots(fid, count);
|
|
1380
|
+
}
|
|
1381
|
+
} else {
|
|
1382
|
+
await this.cleanupCameraRecordings(fid);
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
// Removes <cam>.recordings (recursive) and the files rec1..3.* and send.* of a camera.
|
|
1387
|
+
async cleanupCameraRecordings(fid) {
|
|
1388
|
+
let removed = 0;
|
|
1389
|
+
for (const name of (0, import_recording.cameraRecordingFiles)()) {
|
|
1390
|
+
const p = `${fid}/${name}`;
|
|
1391
|
+
if (await this.fileExistsAsync(this.namespace, p).catch(() => false)) {
|
|
1392
|
+
await this.delFileAsync(this.namespace, p).catch(() => {
|
|
1393
|
+
});
|
|
1394
|
+
removed++;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const ch = `${fid}.recordings`;
|
|
1398
|
+
if (await this.getObjectAsync(ch)) {
|
|
1399
|
+
await this.delObjectAsync(ch, { recursive: true });
|
|
1400
|
+
removed++;
|
|
1401
|
+
}
|
|
1402
|
+
this.forgetEnsured(ch);
|
|
1403
|
+
for (let c = 1; c <= 3; c++) {
|
|
1404
|
+
this.ensuredFolders.delete(`${ch}#${c}`);
|
|
1405
|
+
}
|
|
1406
|
+
if (removed) {
|
|
1407
|
+
this.log.info(`Recordings of ${fid} removed (option off or camera not selected)`);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
// Removes a folder in the file storage recursively (recordings, send file, alert photos of a camera).
|
|
1411
|
+
async deleteStorageFolder(dir) {
|
|
1412
|
+
const entries = await this.readDirAsync(this.namespace, dir).catch(() => []);
|
|
1413
|
+
for (const e of entries) {
|
|
1414
|
+
const p = `${dir}/${e.file}`;
|
|
1415
|
+
if (e.isDir) {
|
|
1416
|
+
await this.deleteStorageFolder(p);
|
|
1417
|
+
} else {
|
|
1418
|
+
await this.delFileAsync(this.namespace, p).catch(() => {
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
await this.delFileAsync(this.namespace, dir).catch(() => {
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
// Stores the current last recording if slot 1 is empty or holds an older one. Runs in a chain,
|
|
1426
|
+
// one camera after the other, and only after the start cleanup.
|
|
1427
|
+
queueInitialFill(d, fid, ev) {
|
|
1428
|
+
this.recChain = this.recChain.then(async () => {
|
|
1429
|
+
if (this._pollStopped || !this.recordingCamEnabled(fid) || this.recBusy[fid] || !ev.fn) {
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
try {
|
|
1433
|
+
const name1 = await this.getStateAsync(`${fid}.recordings.name1`).catch(() => null);
|
|
1434
|
+
const slot1 = await this.slotExt(fid, 1);
|
|
1435
|
+
if (!(0, import_recording.needsInitialFill)(!!slot1, name1 == null ? void 0 : name1.val, ev.fn)) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
this.log.debug(`Initial fill ${fid}: ${ev.fn}`);
|
|
1439
|
+
await this.queueRecordingSave(d, fid, ev);
|
|
1440
|
+
} catch (e) {
|
|
1441
|
+
this.log.warn(`Initial recording ${fid}: ${e.message}`);
|
|
1442
|
+
}
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
// Rewrites url1..N after a change of the web instance.
|
|
1446
|
+
async refreshRecordingUrls() {
|
|
1447
|
+
for (const dev of this.devById.values()) {
|
|
1448
|
+
const fid = this.deviceFolder(dev);
|
|
1449
|
+
if (!await this.getObjectAsync(`${fid}.recordings`)) {
|
|
1450
|
+
continue;
|
|
1451
|
+
}
|
|
1452
|
+
for (let n = 1; n <= 3; n++) {
|
|
1453
|
+
const uri = await this.getStateAsync(`${fid}.recordings.uri${n}`).catch(() => null);
|
|
1454
|
+
if (!(uri == null ? void 0 : uri.val)) {
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
await this.setStateAsync(`${fid}.recordings.url${n}`, {
|
|
1458
|
+
val: await this.recordingWebUrl(fid, n, (0, import_recording.extOf)(String(uri.val))),
|
|
1459
|
+
ack: true
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
// Makes a recording available in the adapter's file storage for sendRecording/getRecording and
|
|
1465
|
+
// returns where it is. Never returns the data itself: video must not travel over the message bus
|
|
1466
|
+
// (a 46 MB Buffer in a sendTo answer blocked the whole states DB).
|
|
1467
|
+
// 'last' (newest in AgentDVR), slot 1..3 or an AgentDVR file name. A recording that is already in
|
|
1468
|
+
// a stored slot is used directly; otherwise it is downloaded into memory, remuxed if wanted and
|
|
1469
|
+
// written to the fixed per-camera file <cam>/send.mp4|mkv (overwritten each time).
|
|
1470
|
+
async provideRecording(msg, limitBytes, limitName) {
|
|
1471
|
+
var _a, _b, _c, _d;
|
|
1472
|
+
const cams = [...this.devById.values()].filter((d) => d.ot === 2).map((d) => ({ oid: d.oid, name: d.name, fid: this.deviceFolder(d) }));
|
|
1473
|
+
const cam = (0, import_recording.findCamera)(msg.camera, cams);
|
|
1474
|
+
if (!cam) {
|
|
1475
|
+
return { ok: false, error: `Unknown camera "${JSON.stringify((_a = msg.camera) != null ? _a : "")}"` };
|
|
1476
|
+
}
|
|
1477
|
+
const wantMp4 = typeof msg.mp4 === "boolean" ? msg.mp4 : this.config.recordingMp4 !== false;
|
|
1478
|
+
const rawRec = msg.recording;
|
|
1479
|
+
const rec = typeof rawRec === "number" ? String(rawRec) : typeof rawRec === "string" && rawRec ? rawRec : "last";
|
|
1480
|
+
let slot = 0;
|
|
1481
|
+
let fn = "";
|
|
1482
|
+
let listedSize = 0;
|
|
1483
|
+
if (/^\d+$/.test(rec)) {
|
|
1484
|
+
slot = Number(rec);
|
|
1485
|
+
if (slot < 1 || slot > 3) {
|
|
1486
|
+
return { ok: false, error: `Invalid recording slot ${rec} (1-3)` };
|
|
1487
|
+
}
|
|
1488
|
+
} else {
|
|
1489
|
+
if (rec !== "last" && !(0, import_recording.isValidRecordingName)(rec)) {
|
|
1490
|
+
return { ok: false, error: `Invalid recording "${rec}" (use 'last', 1-3 or an AgentDVR file name)` };
|
|
1491
|
+
}
|
|
1492
|
+
const evRes = await this.apiGet(`/q/getEvents?oid=${encodeURIComponent(String(cam.oid))}&ot=2`);
|
|
1493
|
+
const evJson = asJson(evRes.data);
|
|
1494
|
+
const events = Array.isArray(evJson == null ? void 0 : evJson.events) ? evJson.events : Array.isArray(evJson) ? evJson : [];
|
|
1495
|
+
const ev = rec === "last" ? events[0] : events.find((e) => e.fn === rec);
|
|
1496
|
+
fn = rec === "last" ? (_b = ev == null ? void 0 : ev.fn) != null ? _b : "" : rec;
|
|
1497
|
+
if (!fn) {
|
|
1498
|
+
return { ok: false, error: `No recording found for ${cam.fid}${evRes.ok ? "" : ` (${evRes.error})`}` };
|
|
1499
|
+
}
|
|
1500
|
+
listedSize = Number((_c = ev == null ? void 0 : ev.sb) != null ? _c : 0);
|
|
1501
|
+
for (let i = 1; i <= 3; i++) {
|
|
1502
|
+
const name = await this.getStateAsync(`${cam.fid}.recordings.name${i}`).catch(() => null);
|
|
1503
|
+
if ((name == null ? void 0 : name.val) === fn) {
|
|
1504
|
+
slot = i;
|
|
1505
|
+
break;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
if (slot) {
|
|
1510
|
+
const ext = await this.slotExt(cam.fid, slot);
|
|
1511
|
+
if (ext) {
|
|
1512
|
+
const storagePath2 = (0, import_recording.recordingStoragePath)(cam.fid, slot, ext);
|
|
1513
|
+
const size = await this.storedFileSize(storagePath2);
|
|
1514
|
+
const name = await this.getStateAsync(`${cam.fid}.recordings.name${slot}`).catch(() => null);
|
|
1515
|
+
const base = String((name == null ? void 0 : name.val) || `${cam.fid}_rec${slot}`).replace(/\.[^.]+$/, "");
|
|
1516
|
+
if ((0, import_recording.exceedsLimit)(size, limitBytes)) {
|
|
1517
|
+
return { ok: false, size, format: ext, error: `Recording is ${(0, import_recording.fmtMb)(size)} \u2014 ${limitName}` };
|
|
1518
|
+
}
|
|
1519
|
+
return this.describeStored(storagePath2, `${base}.${ext}`, ext, size);
|
|
1520
|
+
}
|
|
1521
|
+
if (!fn) {
|
|
1522
|
+
return { ok: false, error: `No stored recording ${slot} for ${cam.fid}` };
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
if ((0, import_recording.exceedsLimit)(listedSize, limitBytes)) {
|
|
1526
|
+
return { ok: false, size: listedSize, error: `Recording ${fn} is ${(0, import_recording.fmtMb)(listedSize)} \u2014 ${limitName}` };
|
|
1527
|
+
}
|
|
1528
|
+
const dl = await this.fetchRecording(cam.oid, fn, limitBytes);
|
|
1529
|
+
if (!dl.ok || !dl.data) {
|
|
1530
|
+
return {
|
|
1531
|
+
ok: false,
|
|
1532
|
+
size: dl.size,
|
|
1533
|
+
error: dl.tooLarge ? `Recording ${fn}: ${dl.error} \u2014 ${limitName}` : `Recording ${fn} not downloaded: ${(_d = dl.error) != null ? _d : "empty response"}`
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1536
|
+
const out = await this.toWantedFormat(dl.data, (0, import_recording.extOf)(fn), wantMp4, limitBytes);
|
|
1537
|
+
if ((0, import_recording.exceedsLimit)(out.data.length, limitBytes)) {
|
|
1538
|
+
return {
|
|
1539
|
+
ok: false,
|
|
1540
|
+
size: out.data.length,
|
|
1541
|
+
format: out.ext,
|
|
1542
|
+
error: `Recording is ${(0, import_recording.fmtMb)(out.data.length)} \u2014 ${limitName}`
|
|
1543
|
+
};
|
|
1544
|
+
}
|
|
1545
|
+
await this.ensureFileMeta();
|
|
1546
|
+
for (const ext of ["mp4", "mkv"]) {
|
|
1547
|
+
const p = (0, import_recording.sendFileStoragePath)(cam.fid, ext);
|
|
1548
|
+
if (ext !== out.ext && await this.fileExistsAsync(this.namespace, p).catch(() => false)) {
|
|
1549
|
+
await this.delFileAsync(this.namespace, p).catch(() => {
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
const storagePath = (0, import_recording.sendFileStoragePath)(cam.fid, out.ext);
|
|
1554
|
+
await this.writeFileAsync(this.namespace, storagePath, out.data);
|
|
1555
|
+
return this.describeStored(storagePath, `${fn.replace(/\.[^.]+$/, "")}.${out.ext}`, out.ext, out.data.length);
|
|
1556
|
+
}
|
|
1557
|
+
async storedFileSize(storagePath) {
|
|
1558
|
+
const local = this.localFilePath(storagePath);
|
|
1559
|
+
if (local) {
|
|
1560
|
+
try {
|
|
1561
|
+
return fs.statSync(local).size;
|
|
1562
|
+
} catch {
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
const f = await this.readFileAsync(this.namespace, storagePath).catch(() => null);
|
|
1566
|
+
const file = f == null ? void 0 : f.file;
|
|
1567
|
+
return file ? Buffer.isBuffer(file) ? file.length : Buffer.byteLength(file) : 0;
|
|
1568
|
+
}
|
|
1569
|
+
// Absolute path if the file storage is on this host's disk, else ''.
|
|
1570
|
+
localFilePath(storagePath) {
|
|
1571
|
+
const abs = path.join(utils.getAbsoluteDefaultDataDir(), "files", this.namespace, ...storagePath.split("/"));
|
|
1572
|
+
return fs.existsSync(abs) ? abs : "";
|
|
1573
|
+
}
|
|
1574
|
+
async describeStored(storagePath, fileName, format, size) {
|
|
1575
|
+
if (this.webBaseUrl === void 0) {
|
|
1576
|
+
this.webBaseUrl = await this.findWebBaseUrl();
|
|
1577
|
+
}
|
|
1578
|
+
return {
|
|
1579
|
+
ok: true,
|
|
1580
|
+
fileName,
|
|
1581
|
+
format,
|
|
1582
|
+
mime: (0, import_recording.mimeOf)(format),
|
|
1583
|
+
size,
|
|
1584
|
+
uri: `iobfile://${this.namespace}/${storagePath}`,
|
|
1585
|
+
url: this.webBaseUrl ? `${this.webBaseUrl}/${this.namespace}/recordings/${storagePath}` : "",
|
|
1586
|
+
file: this.localFilePath(storagePath)
|
|
1587
|
+
};
|
|
1588
|
+
}
|
|
1589
|
+
async getRecording(msg) {
|
|
1590
|
+
return this.provideRecording(
|
|
1591
|
+
msg,
|
|
1592
|
+
(0, import_recording.maxRecordingBytes)(this.config.recordingMaxSizeMb),
|
|
1593
|
+
"larger than the configured maximum size"
|
|
1594
|
+
);
|
|
1595
|
+
}
|
|
1596
|
+
async sendRecording(msg) {
|
|
1597
|
+
var _a, _b, _c;
|
|
1598
|
+
const target = typeof msg.target === "string" && msg.target ? msg.target : "telegram.0";
|
|
1599
|
+
if (!/^[a-z0-9_-]+\.\d+$/i.test(target)) {
|
|
1600
|
+
return { ok: false, error: `Invalid target "${target}" (expected an instance like telegram.0)` };
|
|
1601
|
+
}
|
|
1602
|
+
const telegram = target.startsWith("telegram.");
|
|
1603
|
+
const maxBytes = (0, import_recording.maxRecordingBytes)(this.config.recordingMaxSizeMb);
|
|
1604
|
+
const tgLimit = telegram && import_recording.TELEGRAM_MAX_BYTES <= maxBytes;
|
|
1605
|
+
const r = await this.provideRecording(
|
|
1606
|
+
msg,
|
|
1607
|
+
tgLimit ? import_recording.TELEGRAM_MAX_BYTES : maxBytes,
|
|
1608
|
+
tgLimit ? "Telegram bots can only send files up to 50 MB" : "larger than the configured maximum size"
|
|
1609
|
+
);
|
|
1610
|
+
if (!r.ok || !r.uri) {
|
|
1611
|
+
return r;
|
|
1612
|
+
}
|
|
1613
|
+
if (telegram && ((_a = r.size) != null ? _a : 0) > import_recording.TELEGRAM_MAX_BYTES) {
|
|
1614
|
+
return {
|
|
1615
|
+
...r,
|
|
1616
|
+
ok: false,
|
|
1617
|
+
error: `Recording is ${(0, import_recording.fmtMb)((_b = r.size) != null ? _b : 0)} \u2014 Telegram bots can only send files up to 50 MB`
|
|
1618
|
+
};
|
|
1619
|
+
}
|
|
1620
|
+
const payload = {
|
|
1621
|
+
text: telegram ? r.uri : r.file || r.uri,
|
|
1622
|
+
type: "video",
|
|
1623
|
+
fileName: r.fileName
|
|
1624
|
+
};
|
|
1625
|
+
for (const k of ["caption", "user", "chatId"]) {
|
|
1626
|
+
const v = msg[k];
|
|
1627
|
+
if (typeof v === "string" || typeof v === "number") {
|
|
1628
|
+
if (v !== "") {
|
|
1629
|
+
payload[k] = v;
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
(0, import_recording.assertNoBinary)(payload);
|
|
1634
|
+
const result = await new Promise((resolve) => {
|
|
1635
|
+
const timer = this.setTimeout(() => resolve({ timeout: true }), 3e5);
|
|
1636
|
+
this.sendTo(target, "send", payload, (res2) => {
|
|
1637
|
+
if (timer !== void 0) {
|
|
1638
|
+
this.clearTimeout(timer);
|
|
1639
|
+
}
|
|
1640
|
+
resolve({ res: res2 });
|
|
1641
|
+
});
|
|
1642
|
+
});
|
|
1643
|
+
if (result.timeout) {
|
|
1644
|
+
return { ...r, ok: false, error: `No answer from ${target} within 300 s` };
|
|
1645
|
+
}
|
|
1646
|
+
this.log.debug(`Recording ${r.fileName} (${(0, import_recording.fmtMb)((_c = r.size) != null ? _c : 0)}) sent to ${target}`);
|
|
1647
|
+
const res = result.res;
|
|
1648
|
+
return {
|
|
1649
|
+
...r,
|
|
1650
|
+
result: typeof res === "number" || typeof res === "string" || typeof res === "boolean" ? res : void 0
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1056
1653
|
// ---- device folder name ----
|
|
1057
1654
|
deviceFolder(d) {
|
|
1058
1655
|
return `${d.ot === 1 ? "mic" : "cam"}_${sanitize(d.oid)}_${sanitize(d.name)}`;
|
|
@@ -1284,6 +1881,13 @@ class AgentDvr extends utils.Adapter {
|
|
|
1284
1881
|
this.log.info(`[agent-dvr] removing stale device: ${rel}`);
|
|
1285
1882
|
await this.delObjectAsync(rel, { recursive: true });
|
|
1286
1883
|
this.forgetEnsured(rel);
|
|
1884
|
+
for (let c = 1; c <= 3; c++) {
|
|
1885
|
+
this.ensuredFolders.delete(`${rel}.recordings#${c}`);
|
|
1886
|
+
}
|
|
1887
|
+
delete this.recPending[rel];
|
|
1888
|
+
await this.deleteStorageFolder(rel).catch(
|
|
1889
|
+
(e) => this.log.debug(`File storage cleanup ${rel}: ${e.message}`)
|
|
1890
|
+
);
|
|
1287
1891
|
for (const [oid, d] of this.devById) {
|
|
1288
1892
|
if (this.deviceFolder(d) === rel) {
|
|
1289
1893
|
this.devById.delete(oid);
|
|
@@ -1598,6 +2202,9 @@ class AgentDvr extends utils.Adapter {
|
|
|
1598
2202
|
}
|
|
1599
2203
|
let newCount = 0;
|
|
1600
2204
|
const firstRun = this.lastEventFn[oid] === void 0;
|
|
2205
|
+
if (firstRun && events.length && this.recordingCamEnabled(fid)) {
|
|
2206
|
+
this.queueInitialFill(d, fid, events[0]);
|
|
2207
|
+
}
|
|
1601
2208
|
if (events.length) {
|
|
1602
2209
|
const prev = this.lastEventFn[oid];
|
|
1603
2210
|
if (prev !== void 0) {
|
|
@@ -1630,6 +2237,9 @@ class AgentDvr extends utils.Adapter {
|
|
|
1630
2237
|
this.eventTagsSeen[oid] = nextKnown;
|
|
1631
2238
|
if (newCount > 0) {
|
|
1632
2239
|
await this.pulse(`${fid}.events.new`);
|
|
2240
|
+
if (this.recordingCamEnabled(fid)) {
|
|
2241
|
+
void this.queueRecordingSave(d, fid, events[0]);
|
|
2242
|
+
}
|
|
1633
2243
|
}
|
|
1634
2244
|
if (newCount > 0 || lateTags > 0) {
|
|
1635
2245
|
for (const t of tags) {
|
|
@@ -1747,7 +2357,9 @@ class AgentDvr extends utils.Adapter {
|
|
|
1747
2357
|
for (const d of devices) {
|
|
1748
2358
|
await this.buildDevice(d);
|
|
1749
2359
|
}
|
|
1750
|
-
|
|
2360
|
+
if (devices.length) {
|
|
2361
|
+
await this.pruneStaleDevices(activeFolders);
|
|
2362
|
+
}
|
|
1751
2363
|
const stats = asJson((await this.apiGet("/command/getSystemStats")).data);
|
|
1752
2364
|
if (stats) {
|
|
1753
2365
|
const {
|