iobroker.agent-dvr 0.9.0 → 0.9.1

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/build/main.js CHANGED
@@ -425,6 +425,7 @@ class AgentDvr extends utils.Adapter {
425
425
  callback();
426
426
  }
427
427
  onMessage(obj) {
428
+ var _a;
428
429
  if (!obj) {
429
430
  return;
430
431
  }
@@ -435,6 +436,14 @@ class AgentDvr extends utils.Adapter {
435
436
  });
436
437
  } else if (obj.command === "getAgentDvrCameras") {
437
438
  this.sendTo(obj.from, obj.command, { cameras: this.lastCamNames }, obj.callback);
439
+ } else if (obj.command === "getCameras") {
440
+ const opts = this.lastCamNames.map((c) => ({ value: `${this.namespace}.${c.key}`, label: c.name }));
441
+ this.sendTo(obj.from, obj.command, opts, obj.callback);
442
+ } else if (obj.command === "getWidgets") {
443
+ const msg = (_a = obj.message) != null ? _a : {};
444
+ const kind = msg.kind === "live" ? "live" : "recordings";
445
+ const opts = this.config.enableWidget ? this.lastCamNames.map((c) => ({ value: `${this.namespace}.${c.key}.widget_${kind}`, label: c.name })) : [];
446
+ this.sendTo(obj.from, obj.command, opts, obj.callback);
438
447
  } else if (obj.command === "getAvailableSources") {
439
448
  void this.fetchAvailableSources().then((result) => {
440
449
  this.sendTo(obj.from, obj.command, result, obj.callback);
@@ -1153,6 +1162,43 @@ class AgentDvr extends utils.Adapter {
1153
1162
  this.recBusy[fid] = false;
1154
1163
  })();
1155
1164
  }
1165
+ // AgentDVR lists a recording while it is still being written: waits until the camera stops recording
1166
+ // and the listed size stays the same between two checks (at most 10 minutes). false = adapter stops
1167
+ // or the recording disappeared.
1168
+ async waitRecordingFinished(oid, fn) {
1169
+ var _a;
1170
+ const deadline = Date.now() + 10 * 6e4;
1171
+ let prev = -1;
1172
+ let waited = false;
1173
+ while (!this._pollStopped) {
1174
+ const res = await this.apiGet(`/q/getEvents?oid=${encodeURIComponent(String(oid))}&ot=2`);
1175
+ const json = asJson(res.data);
1176
+ const events = Array.isArray(json == null ? void 0 : json.events) ? json.events : Array.isArray(json) ? json : [];
1177
+ const ev = res.ok && json ? events.find((e) => e.fn === fn) : void 0;
1178
+ if (res.ok && json && !ev) {
1179
+ this.log.debug(`Recording ${fn} no longer listed`);
1180
+ return false;
1181
+ }
1182
+ const size = Number((_a = ev == null ? void 0 : ev.sb) != null ? _a : 0);
1183
+ const dev = [...this.devById.values()].find((x) => x.ot === 2 && String(x.oid) === String(oid));
1184
+ const data = dev == null ? void 0 : dev.raw.data;
1185
+ const recording = (data == null ? void 0 : data.recording) === true;
1186
+ if ((0, import_recording.recordingFinished)(prev, size, recording)) {
1187
+ if (waited) {
1188
+ this.log.debug(`Recording ${fn} finished (${(0, import_recording.fmtMb)(size)})`);
1189
+ }
1190
+ return true;
1191
+ }
1192
+ if (Date.now() > deadline) {
1193
+ this.log.info(`Recording ${fn} still growing after 10 minutes, storing the current state`);
1194
+ return true;
1195
+ }
1196
+ prev = size;
1197
+ waited = true;
1198
+ await this.delay(4e3);
1199
+ }
1200
+ return false;
1201
+ }
1156
1202
  // Downloads a recording from AgentDVR into memory (never to disk), with size limit and timeout.
1157
1203
  fetchRecording(oid, fn, maxBytes) {
1158
1204
  return (0, import_recording.downloadToBuffer)(
@@ -1296,6 +1342,9 @@ class AgentDvr extends utils.Adapter {
1296
1342
  this.log.info(`Recording ${fn} (${(0, import_recording.fmtMb)(listed)}) not stored: larger than ${(0, import_recording.fmtMb)(maxBytes)}`);
1297
1343
  return;
1298
1344
  }
1345
+ if (!await this.waitRecordingFinished(d.oid, fn)) {
1346
+ return;
1347
+ }
1299
1348
  const dl = await this.fetchRecording(d.oid, fn, maxBytes);
1300
1349
  if (!dl.ok || !dl.data) {
1301
1350
  if (dl.tooLarge) {
@@ -1525,6 +1574,9 @@ class AgentDvr extends utils.Adapter {
1525
1574
  if ((0, import_recording.exceedsLimit)(listedSize, limitBytes)) {
1526
1575
  return { ok: false, size: listedSize, error: `Recording ${fn} is ${(0, import_recording.fmtMb)(listedSize)} \u2014 ${limitName}` };
1527
1576
  }
1577
+ if (!await this.waitRecordingFinished(cam.oid, fn)) {
1578
+ return { ok: false, error: `Recording ${fn} is no longer available` };
1579
+ }
1528
1580
  const dl = await this.fetchRecording(cam.oid, fn, limitBytes);
1529
1581
  if (!dl.ok || !dl.data) {
1530
1582
  return {
@@ -1552,7 +1604,13 @@ class AgentDvr extends utils.Adapter {
1552
1604
  }
1553
1605
  const storagePath = (0, import_recording.sendFileStoragePath)(cam.fid, out.ext);
1554
1606
  await this.writeFileAsync(this.namespace, storagePath, out.data);
1555
- return this.describeStored(storagePath, `${fn.replace(/\.[^.]+$/, "")}.${out.ext}`, out.ext, out.data.length);
1607
+ return this.describeStored(
1608
+ storagePath,
1609
+ `${fn.replace(/\.[^.]+$/, "")}.${out.ext}`,
1610
+ out.ext,
1611
+ out.data.length,
1612
+ out.data
1613
+ );
1556
1614
  }
1557
1615
  async storedFileSize(storagePath) {
1558
1616
  const local = this.localFilePath(storagePath);
@@ -1571,10 +1629,11 @@ class AgentDvr extends utils.Adapter {
1571
1629
  const abs = path.join(utils.getAbsoluteDefaultDataDir(), "files", this.namespace, ...storagePath.split("/"));
1572
1630
  return fs.existsSync(abs) ? abs : "";
1573
1631
  }
1574
- async describeStored(storagePath, fileName, format, size) {
1632
+ async describeStored(storagePath, fileName, format, size, data) {
1575
1633
  if (this.webBaseUrl === void 0) {
1576
1634
  this.webBaseUrl = await this.findWebBaseUrl();
1577
1635
  }
1636
+ const v = (0, import_recording.videoInfo)(data != null ? data : await this.readStoredFile(storagePath), format);
1578
1637
  return {
1579
1638
  ok: true,
1580
1639
  fileName,
@@ -1583,9 +1642,26 @@ class AgentDvr extends utils.Adapter {
1583
1642
  size,
1584
1643
  uri: `iobfile://${this.namespace}/${storagePath}`,
1585
1644
  url: this.webBaseUrl ? `${this.webBaseUrl}/${this.namespace}/recordings/${storagePath}` : "",
1586
- file: this.localFilePath(storagePath)
1645
+ file: this.localFilePath(storagePath),
1646
+ width: v.width,
1647
+ height: v.height,
1648
+ duration: v.duration
1587
1649
  };
1588
1650
  }
1651
+ // Reads a stored file inside the adapter process (never over the message bus); empty buffer on error.
1652
+ async readStoredFile(storagePath) {
1653
+ const local = this.localFilePath(storagePath);
1654
+ try {
1655
+ if (local) {
1656
+ return await fs.promises.readFile(local);
1657
+ }
1658
+ const f = await this.readFileAsync(this.namespace, storagePath);
1659
+ const file = f == null ? void 0 : f.file;
1660
+ return Buffer.isBuffer(file) ? file : Buffer.from(file != null ? file : "");
1661
+ } catch {
1662
+ return Buffer.alloc(0);
1663
+ }
1664
+ }
1589
1665
  async getRecording(msg) {
1590
1666
  return this.provideRecording(
1591
1667
  msg,
@@ -1622,6 +1698,13 @@ class AgentDvr extends utils.Adapter {
1622
1698
  type: "video",
1623
1699
  fileName: r.fileName
1624
1700
  };
1701
+ if (telegram) {
1702
+ for (const k of ["width", "height", "duration"]) {
1703
+ if (r[k]) {
1704
+ payload[k] = r[k];
1705
+ }
1706
+ }
1707
+ }
1625
1708
  for (const k of ["caption", "user", "chatId"]) {
1626
1709
  const v = msg[k];
1627
1710
  if (typeof v === "string" || typeof v === "number") {