iobroker.agent-dvr 0.4.3 → 0.5.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/build/main.js CHANGED
@@ -295,6 +295,7 @@ class AgentDvr extends utils.Adapter {
295
295
  ensuredFolders = /* @__PURE__ */ new Set();
296
296
  registry = /* @__PURE__ */ new Map();
297
297
  ptzActive = /* @__PURE__ */ new Map();
298
+ ptzPresetNames = /* @__PURE__ */ new Map();
298
299
  widgetSig = {};
299
300
  profileSig = "";
300
301
  lastCamNames = [];
@@ -562,13 +563,16 @@ class AgentDvr extends utils.Adapter {
562
563
  }
563
564
  }
564
565
  }
565
- async writeLeaf(id, val) {
566
+ async writeLeaf(id, val, unit) {
566
567
  const t = typeof val;
567
568
  let common;
568
569
  if (t === "boolean") {
569
570
  common = { name: id.split(".").pop() || id, type: "boolean", role: "indicator", read: true, write: false };
570
571
  } else if (t === "number") {
571
572
  common = { name: id.split(".").pop() || id, type: "number", role: "value", read: true, write: false };
573
+ if (unit) {
574
+ common.unit = unit;
575
+ }
572
576
  } else {
573
577
  const role = typeof val === "string" && /^https?:\/\//i.test(val) ? "text.url" : "text";
574
578
  common = { name: id.split(".").pop() || id, type: "string", role, read: true, write: false };
@@ -810,6 +814,7 @@ class AgentDvr extends utils.Adapter {
810
814
  await this.ensureButton(cid, `PTZ ${p.id}`, { kind: "ptz", oid: d.oid, dir: p.dir });
811
815
  }
812
816
  }
817
+ await this.buildPtzPresets(d, fid);
813
818
  }
814
819
  if (d.ot === 2) {
815
820
  const sfx = { kind: "levelCmd", path: "/command/setSensitivity", oid: d.oid, ot: d.ot };
@@ -876,6 +881,35 @@ class AgentDvr extends utils.Adapter {
876
881
  }
877
882
  await this.updateCameraEvents(d, fid);
878
883
  }
884
+ // ---- PTZ presets ----
885
+ async buildPtzPresets(d, fid) {
886
+ var _a;
887
+ const selectorId = `${fid}.control.ptz.preset`;
888
+ if (!this.ptzPresetNames.has(d.oid)) {
889
+ const res = await this.apiGet(`/command/ptzpresets?oid=${d.oid}&ot=2`);
890
+ const json = asJson(res.data);
891
+ const raw = json && Array.isArray(json.presets) ? json.presets : [];
892
+ const names2 = raw.map((p) => {
893
+ var _a2, _b;
894
+ return toStr((_b = (_a2 = p.name) != null ? _a2 : p.token) != null ? _b : "");
895
+ }).filter(Boolean);
896
+ this.ptzPresetNames.set(d.oid, names2);
897
+ }
898
+ const names = (_a = this.ptzPresetNames.get(d.oid)) != null ? _a : [];
899
+ if (!names.length) {
900
+ return;
901
+ }
902
+ const states = {};
903
+ names.forEach((n, i) => {
904
+ states[i] = n;
905
+ });
906
+ await this.ensureSelector(
907
+ selectorId,
908
+ "Preset",
909
+ { kind: "ptzPreset", oid: d.oid, ot: d.ot, presetNames: names },
910
+ states
911
+ );
912
+ }
879
913
  // ---- event formatting ----
880
914
  fmtEvent(ev, oid) {
881
915
  var _a, _b, _c;
@@ -887,13 +921,14 @@ class AgentDvr extends utils.Adapter {
887
921
  const dur = e.d != null ? String(e.d) : "";
888
922
  const tag = (_b = e.tg) != null ? _b : "";
889
923
  const ms = (parseFloat(String((_c = e.c) != null ? _c : 0)) - 621355968e9) / 1e4;
924
+ const ts = Number.isFinite(ms) ? Math.round(ms) : 0;
890
925
  const dt = new Date(ms);
891
926
  const two = (n) => String(n).padStart(2, "0");
892
927
  const date = `${two(dt.getDate())}.${two(dt.getMonth() + 1)}.`;
893
928
  const time = `${two(dt.getHours())}:${two(dt.getMinutes())}`;
894
929
  const thumb = `${this.baseUrl}/fileThumb.jpg?oid=${oid}&fn=${encodeURIComponent(jpg)}`;
895
930
  const video = `${this.baseUrl}/streamFile.cgi?oid=${oid}&ot=2&fn=${encodeURIComponent(fn)}`;
896
- return { fn, sizeMB, dur, tag, date, time, thumb, video };
931
+ return { fn, sizeMB, dur, tag, date, time, thumb, video, ts };
897
932
  }
898
933
  // ---- gallery HTML builders ----
899
934
  effectiveMinCol() {
@@ -1031,7 +1066,7 @@ class AgentDvr extends utils.Adapter {
1031
1066
  }
1032
1067
  // ---- event data points ----
1033
1068
  async writeEventDps(d, fid, events) {
1034
- var _a, _b, _c, _d;
1069
+ var _a, _b, _c, _d, _e;
1035
1070
  const oid = d.oid;
1036
1071
  await this.ensureFolder(`${fid}.events`, "Events", "channel");
1037
1072
  await this.ensureFolder(`${fid}.events.last`, "Last event", "channel");
@@ -1064,43 +1099,24 @@ class AgentDvr extends utils.Adapter {
1064
1099
  }
1065
1100
  await this.setStateAsync(jsonId, {
1066
1101
  val: JSON.stringify(
1067
- events.slice(0, Math.max(1, this.config.widgetAnzahl || 50)).map((ev) => {
1102
+ events.slice(0, Math.max(1, (_b = this.config.dashMaxRec) != null ? _b : 200)).map((ev) => {
1068
1103
  const p = this.fmtEvent(ev, oid);
1069
1104
  return {
1105
+ fn: p.fn,
1070
1106
  date: p.date,
1071
1107
  time: p.time,
1072
1108
  dur: p.dur,
1073
1109
  size: p.sizeMB,
1074
1110
  tag: p.tag,
1075
1111
  thumb: p.thumb,
1076
- video: p.video
1112
+ video: p.video,
1113
+ ts: p.ts
1077
1114
  };
1078
1115
  })
1079
1116
  ),
1080
1117
  ack: true
1081
1118
  });
1082
1119
  await this.ensureFlag(`${fid}.events.new`, "New event");
1083
- if (this.config.enablePush) {
1084
- const pid = `${fid}.events.pushTrigger`;
1085
- if (!this.ensuredFolders.has(pid)) {
1086
- await this.ensurePath(pid);
1087
- await this.setObjectNotExistsAsync(pid, {
1088
- type: "state",
1089
- common: {
1090
- name: "Push trigger (AgentDVR action)",
1091
- type: "string",
1092
- role: "text",
1093
- read: true,
1094
- write: true,
1095
- def: ""
1096
- },
1097
- native: {}
1098
- });
1099
- await this.setStateAsync(pid, { val: "", ack: true });
1100
- this.ensuredFolders.add(pid);
1101
- }
1102
- this.registry.set(pid, { kind: "push", oid, fid });
1103
- }
1104
1120
  const ignore = (this.config.eventTagsIgnore || "detected").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
1105
1121
  const tokensOf = (s) => s.toLowerCase().split(/[,\s]+/).filter(Boolean);
1106
1122
  for (const kw of (this.config.eventTags || "").split(",").map((s) => s.trim()).filter(Boolean)) {
@@ -1109,7 +1125,7 @@ class AgentDvr extends utils.Adapter {
1109
1125
  if (this.config.eventTagsDynamic) {
1110
1126
  const seen = /* @__PURE__ */ new Set();
1111
1127
  for (const ev of events) {
1112
- tokensOf((_b = ev.tg) != null ? _b : "").forEach((t) => seen.add(t));
1128
+ tokensOf((_c = ev.tg) != null ? _c : "").forEach((t) => seen.add(t));
1113
1129
  }
1114
1130
  for (const t of seen) {
1115
1131
  if (ignore.includes(t)) {
@@ -1125,13 +1141,13 @@ class AgentDvr extends utils.Adapter {
1125
1141
  const i = events.findIndex((e) => e.fn === prev);
1126
1142
  newCount = i === -1 ? events.length : i;
1127
1143
  }
1128
- this.lastEventFn[oid] = (_c = events[0].fn) != null ? _c : "";
1144
+ this.lastEventFn[oid] = (_d = events[0].fn) != null ? _d : "";
1129
1145
  }
1130
1146
  if (newCount > 0) {
1131
1147
  await this.pulse(`${fid}.events.new`);
1132
1148
  const tags = /* @__PURE__ */ new Set();
1133
1149
  for (let i = 0; i < newCount && i < events.length; i++) {
1134
- tokensOf((_d = events[i].tg) != null ? _d : "").forEach((t) => tags.add(t));
1150
+ tokensOf((_e = events[i].tg) != null ? _e : "").forEach((t) => tags.add(t));
1135
1151
  }
1136
1152
  for (const t of tags) {
1137
1153
  if (ignore.includes(t)) {
@@ -1198,22 +1214,6 @@ class AgentDvr extends utils.Adapter {
1198
1214
  this.eventBusy[d.oid] = false;
1199
1215
  }
1200
1216
  }
1201
- pushRefresh(oid) {
1202
- const d = this.devById.get(oid);
1203
- if (!d) {
1204
- this.log.warn(`Push: camera ${oid} unknown (available after first poll)`);
1205
- return;
1206
- }
1207
- const fid = this.deviceFolder(d);
1208
- for (const ms of [0, 1500, 3500, 6e3]) {
1209
- this.setTimeout(
1210
- () => this.updateCameraEvents(d, fid).catch(
1211
- (e) => this.log.warn(`Push refresh error: ${e.message}`)
1212
- ),
1213
- ms
1214
- );
1215
- }
1216
- }
1217
1217
  // ---- main poll ----
1218
1218
  async poll() {
1219
1219
  var _a, _b;
@@ -1251,10 +1251,94 @@ class AgentDvr extends utils.Adapter {
1251
1251
  await this.pruneStaleDevices(activeFolders);
1252
1252
  const stats = asJson((await this.apiGet("/command/getSystemStats")).data);
1253
1253
  if (stats) {
1254
- await this.flattenWrite(stats, "system.stats", 0);
1254
+ const {
1255
+ disks: _disks,
1256
+ cpup,
1257
+ ramp,
1258
+ ramu,
1259
+ cpu_used,
1260
+ ram_free,
1261
+ disk_free: _disk_free,
1262
+ diskp,
1263
+ ...statsRest
1264
+ } = stats;
1265
+ if (cpup !== void 0) {
1266
+ await this.writeLeaf("system.hardware.cpu.percent", cpup, "%");
1267
+ }
1268
+ await this.delObjectAsync("system.hardware.cpu.used").catch(() => {
1269
+ });
1270
+ if (ramp !== void 0) {
1271
+ await this.writeLeaf("system.hardware.ram.percent", ramp, "%");
1272
+ }
1273
+ if (ramu !== void 0) {
1274
+ await this.writeLeaf("system.hardware.ram.used", toStr(ramu));
1275
+ }
1276
+ if (ram_free !== void 0) {
1277
+ await this.writeLeaf("system.hardware.ram.free", toStr(ram_free));
1278
+ }
1279
+ if (Object.keys(statsRest).length) {
1280
+ await this.flattenWrite(
1281
+ { ...statsRest, ...diskp !== void 0 && { disk_percent: diskp } },
1282
+ "system.stats",
1283
+ 0
1284
+ );
1285
+ }
1286
+ for (const old of [
1287
+ "cpup",
1288
+ "ramp",
1289
+ "ramu",
1290
+ "diskp",
1291
+ "cpu_used",
1292
+ "ram_free",
1293
+ "disk_free",
1294
+ "cpu_percent",
1295
+ "ram_percent",
1296
+ "ram_used",
1297
+ "disk_percent"
1298
+ ]) {
1299
+ await this.delObjectAsync(`system.stats.${old}`).catch(() => {
1300
+ });
1301
+ }
1302
+ for (const old of ["system.cpu", "system.ram", "system.drives", "system.disk_free_gb"]) {
1303
+ await this.delObjectAsync(old, { recursive: true }).catch(() => {
1304
+ });
1305
+ }
1255
1306
  const gb = parseSizeGb(stats.disk_free);
1256
1307
  if (gb !== null) {
1257
- await this.writeLeaf("system.disk_free_gb", gb);
1308
+ await this.writeLeaf("system.hardware.disk_free_gb", gb);
1309
+ }
1310
+ if (Array.isArray(stats.disks)) {
1311
+ const activeKeys = /* @__PURE__ */ new Set();
1312
+ for (const drv of stats.disks) {
1313
+ if (!drv || typeof drv !== "object") {
1314
+ continue;
1315
+ }
1316
+ const key = sanitize(toStr(drv.n).replace(/^\//, "") || "root") || "root";
1317
+ activeKeys.add(key);
1318
+ await this.writeLeaf(
1319
+ `system.hardware.drives.${key}.name`,
1320
+ toStr(drv.n) === "/" ? "/ Root" : toStr(drv.n)
1321
+ );
1322
+ await this.writeLeaf(`system.hardware.drives.${key}.free`, toStr(drv.d));
1323
+ await this.writeLeaf(`system.hardware.drives.${key}.used`, toStr(drv.u));
1324
+ await this.writeLeaf(
1325
+ `system.hardware.drives.${key}.percent`,
1326
+ typeof drv.p === "number" ? drv.p : 0,
1327
+ "%"
1328
+ );
1329
+ const fgb = parseSizeGb(drv.d);
1330
+ if (fgb !== null) {
1331
+ await this.writeLeaf(`system.hardware.drives.${key}.free_gb`, fgb);
1332
+ }
1333
+ }
1334
+ const allObjs = await this.getAdapterObjectsAsync();
1335
+ for (const fullId of Object.keys(allObjs)) {
1336
+ const rel = fullId.slice(this.namespace.length + 1);
1337
+ const m = rel.match(/^system\.hardware\.drives\.([^.]+)$/);
1338
+ if (m && !activeKeys.has(m[1])) {
1339
+ await this.delObjectAsync(rel, { recursive: true });
1340
+ }
1341
+ }
1258
1342
  }
1259
1343
  }
1260
1344
  const status = asJson((await this.apiGet("/command/getStatus")).data);
@@ -1369,6 +1453,7 @@ class AgentDvr extends utils.Adapter {
1369
1453
  }
1370
1454
  }
1371
1455
  async runCommand(relId, entry, val) {
1456
+ var _a, _b;
1372
1457
  if (entry.kind === "setProfile") {
1373
1458
  const ind = typeof val === "number" ? val : parseInt(String(val != null ? val : ""), 10);
1374
1459
  if (!isNaN(ind)) {
@@ -1405,10 +1490,20 @@ class AgentDvr extends utils.Adapter {
1405
1490
  await this.setStateAsync(relId, { val: false, ack: true });
1406
1491
  return;
1407
1492
  }
1408
- if (entry.kind === "push") {
1409
- this.log.info(`Push trigger cam ${entry.oid}`);
1410
- await this.setStateAsync(relId, { val: "", ack: true });
1411
- this.pushRefresh(entry.oid);
1493
+ if (entry.kind === "ptzPreset") {
1494
+ const idx = typeof val === "number" ? val : parseInt(String(val != null ? val : ""), 10);
1495
+ const name = isNaN(idx) ? "" : (_b = ((_a = entry.presetNames) != null ? _a : [])[idx]) != null ? _b : "";
1496
+ if (!name) {
1497
+ return;
1498
+ }
1499
+ const url2 = `/command/ptzpreset?oid=${encodeURIComponent(String(entry.oid))}&ot=2&preset=${encodeURIComponent(name)}`;
1500
+ const cmdRes2 = await this.apiGet(url2);
1501
+ if (cmdRes2.ok) {
1502
+ this.log.debug(`PTZ preset \u2192 ${name}`);
1503
+ await this.setStateAsync(relId, { val: idx, ack: true });
1504
+ } else {
1505
+ this.log.warn(`PTZ preset failed (${url2}): ${cmdRes2.error}`);
1506
+ }
1412
1507
  return;
1413
1508
  }
1414
1509
  if (entry.kind === "ptzHold") {