@formant/data-sdk 1.46.0 → 1.48.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.
@@ -31642,6 +31642,12 @@ class BasicUniverseDataConnector {
31642
31642
  setTime(t) {
31643
31643
  t !== "live" && (this.time = t), this.timeChangeListeners.forEach((n) => n(t));
31644
31644
  }
31645
+ getTime() {
31646
+ return this.time;
31647
+ }
31648
+ getTimeMs() {
31649
+ return this.time === "live" ? (/* @__PURE__ */ new Date()).getTime() : this.time.getTime();
31650
+ }
31645
31651
  getAvailablePCDWorker() {
31646
31652
  for (let t = 0; t < PCD_WORKER_POOL_SIZE; t++)
31647
31653
  if (!this.pcdWorkerPoolOccupancy[t])
@@ -31895,35 +31901,35 @@ class LiveUniverseData extends BasicUniverseDataConnector {
31895
31901
  }
31896
31902
  throw new Error("Telemetry bitset not implemented");
31897
31903
  }
31898
- subscribeToOdometry(t, n, r) {
31904
+ subscribeToOdometry(t, n, r, o) {
31899
31905
  if (n.sourceType === "realtime") {
31900
- const o = (s, g) => {
31901
- if (g.payload.odometry) {
31902
- const l = g.payload.odometry;
31906
+ const s = (g, l) => {
31907
+ if (l.payload.odometry) {
31908
+ const u = l.payload.odometry;
31903
31909
  r({
31904
- worldToLocal: l.worldToLocal,
31910
+ worldToLocal: u.worldToLocal,
31905
31911
  pose: {
31906
31912
  translation: {
31907
- x: l.pose.translation.x,
31908
- y: l.pose.translation.y,
31909
- z: l.pose.translation.z
31913
+ x: u.pose.translation.x,
31914
+ y: u.pose.translation.y,
31915
+ z: u.pose.translation.z
31910
31916
  },
31911
31917
  rotation: {
31912
- x: l.pose.rotation.x,
31913
- y: l.pose.rotation.y,
31914
- z: l.pose.rotation.z,
31915
- w: l.pose.rotation.w
31918
+ x: u.pose.rotation.x,
31919
+ y: u.pose.rotation.y,
31920
+ z: u.pose.rotation.z,
31921
+ w: u.pose.rotation.w
31916
31922
  }
31917
31923
  },
31918
31924
  covariance: []
31919
31925
  });
31920
31926
  }
31921
31927
  };
31922
- return this.subscribeToRealtimeMessages(t, n.rosTopicName, o), () => {
31928
+ return this.subscribeToRealtimeMessages(t, n.rosTopicName, s), () => {
31923
31929
  this.unsubscribeToRealtimeMessages(
31924
31930
  t,
31925
31931
  n.rosTopicName,
31926
- o
31932
+ s
31927
31933
  );
31928
31934
  };
31929
31935
  }
@@ -31931,35 +31937,35 @@ class LiveUniverseData extends BasicUniverseDataConnector {
31931
31937
  return this.addRemovableTelemetrySubscription(
31932
31938
  t,
31933
31939
  n,
31934
- async (o) => {
31935
- let s, g;
31936
- for (let l = 0; l < o.length; l += 1) {
31937
- const u = o[l];
31938
- if (u.deviceId === t && u.name === n.streamName && u.type === "localization") {
31939
- const [c, d] = u.points[u.points.length - 1];
31940
- s = d;
31940
+ async (s) => {
31941
+ let g, l;
31942
+ for (let u = 0; u < s.length; u += 1) {
31943
+ const c = s[u];
31944
+ if (c.deviceId === t && c.name === n.streamName && c.type === "localization") {
31945
+ const [d, B] = c.points[c.points.length - 1];
31946
+ g = B;
31941
31947
  }
31942
31948
  }
31943
- return s && s.odometry && (g = {
31944
- worldToLocal: s.odometry.worldToLocal,
31949
+ return g && g.odometry && (l = {
31950
+ worldToLocal: g.odometry.worldToLocal,
31945
31951
  pose: {
31946
31952
  translation: {
31947
- x: s.odometry.pose.translation.x,
31948
- y: s.odometry.pose.translation.y,
31949
- z: s.odometry.pose.translation.z
31953
+ x: g.odometry.pose.translation.x,
31954
+ y: g.odometry.pose.translation.y,
31955
+ z: g.odometry.pose.translation.z
31950
31956
  },
31951
31957
  rotation: {
31952
- x: s.odometry.pose.rotation.x,
31953
- y: s.odometry.pose.rotation.y,
31954
- z: s.odometry.pose.rotation.z,
31955
- w: s.odometry.pose.rotation.w
31958
+ x: g.odometry.pose.rotation.x,
31959
+ y: g.odometry.pose.rotation.y,
31960
+ z: g.odometry.pose.rotation.z,
31961
+ w: g.odometry.pose.rotation.w
31956
31962
  }
31957
31963
  },
31958
31964
  covariance: []
31959
31965
  }), {
31960
31966
  deviceId: t,
31961
31967
  sourceId: n.id,
31962
- data: g
31968
+ data: l
31963
31969
  };
31964
31970
  },
31965
31971
  r
@@ -32387,14 +32393,17 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32387
32393
  o(NoData);
32388
32394
  return;
32389
32395
  }
32390
- const u = l[l.length - 1][1];
32391
- let c = u;
32392
- u.url && (s.postMessage({ url: u.url }), s.onmessage = (d) => {
32393
- c = d.data.response, c.path && o({
32394
- worldToLocal: c.path.worldToLocal,
32395
- poses: c.path.poses
32396
- });
32397
- });
32396
+ const u = this.getNearestPoint(l)[1];
32397
+ if (u.url) {
32398
+ const c = await fetch(u.url).then(
32399
+ (d) => d.json()
32400
+ );
32401
+ c.path && o(c.path);
32402
+ return;
32403
+ } else if (u.path) {
32404
+ o(u.path);
32405
+ return;
32406
+ }
32398
32407
  }
32399
32408
  );
32400
32409
  return () => {
@@ -32413,43 +32422,50 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32413
32422
  }
32414
32423
  addFinder(n, r, o, s, g) {
32415
32424
  const l = (u) => {
32416
- let c;
32417
- g ? c = addYears(u, -1) : c = addSeconds(u, -15);
32418
- let d;
32419
- g ? d = u : d = addSeconds(u, 5);
32420
- let B = this.queryStore.moduleQuery(
32421
- {
32422
- deviceIds: [r]
32423
- },
32425
+ const c = g ? addYears(u, -1) : addSeconds(u, -60), d = g ? addMilliseconds(u, 1) : addSeconds(u, 5), B = this.queryStore.moduleQuery(
32426
+ { deviceIds: [r] },
32424
32427
  o,
32425
32428
  s,
32426
32429
  c,
32427
32430
  d,
32428
32431
  g
32429
32432
  );
32430
- if (B === void 0)
32433
+ if (B === void 0) {
32431
32434
  n(void 0);
32432
- else if (B === "too much data")
32435
+ return;
32436
+ }
32437
+ if (B === "too much data") {
32433
32438
  n("too much data");
32434
- else if (B.length > 0) {
32435
- const f = B[0].points;
32436
- if (f.length > 0) {
32437
- const p = f[f.length - 1];
32438
- if (g) {
32439
- let U = p[0], y = p[1];
32440
- f.forEach((w) => {
32441
- const D = w[0], m = w[1];
32442
- Math.abs(D - u.getTime()) < Math.abs(U - u.getTime()) && (U = D, y = m);
32443
- }), n([[U, y]]);
32444
- } else
32445
- n(f);
32446
- } else
32447
- n(void 0);
32448
- } else
32439
+ return;
32440
+ }
32441
+ if (B.length === 0) {
32442
+ n(void 0);
32443
+ return;
32444
+ }
32445
+ const R = B.reduce((f, p) => f.concat(p.points), []);
32446
+ if (!R || R.length === 0) {
32449
32447
  n(void 0);
32448
+ return;
32449
+ }
32450
+ if (g) {
32451
+ const p = R[R.length - 1][0], U = R.filter(
32452
+ (y) => y[0] > addSeconds(p, -15).getTime()
32453
+ );
32454
+ n(U);
32455
+ return;
32456
+ }
32457
+ n(R);
32450
32458
  };
32451
32459
  return this.timeFinders.push(l), l;
32452
32460
  }
32461
+ getNearestPoint(n, r = this.time) {
32462
+ const o = r === "live" ? addMilliseconds(/* @__PURE__ */ new Date(), 1) : this.time;
32463
+ let s = n[0][0], g = n[0][1];
32464
+ return n.forEach((l) => {
32465
+ const u = l[0], c = l[1];
32466
+ Math.abs(u - o.getTime()) < Math.abs(s - o.getTime()) && (s = u, g = c);
32467
+ }), [s, g];
32468
+ }
32453
32469
  removeFinder(n) {
32454
32470
  this.timeFinders = this.timeFinders.filter((r) => r !== n);
32455
32471
  }
@@ -32488,7 +32504,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32488
32504
  o(NoData);
32489
32505
  return;
32490
32506
  }
32491
- const d = c[c.length - 1][1];
32507
+ const d = this.getNearestPoint(c)[1];
32492
32508
  if (typeof d == "string")
32493
32509
  o(JSON.parse(d));
32494
32510
  else {
@@ -32510,7 +32526,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32510
32526
  o(NoData);
32511
32527
  return;
32512
32528
  }
32513
- let d = c[c.length - 1][1];
32529
+ let d = this.getNearestPoint(c)[1];
32514
32530
  d.url && (g.postMessage({ url: d.url }), g.onmessage = (B) => {
32515
32531
  if (d = B.data.response, d.pointClouds) {
32516
32532
  const { url: R, worldToLocal: f } = d.pointClouds[0];
@@ -32527,46 +32543,69 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32527
32543
  this.releasePCDWorker(s), this.releaseDataFetchWorker(g), l(), u();
32528
32544
  };
32529
32545
  }
32530
- subscribeToOdometry(n, r, o) {
32546
+ subscribeToOdometry(n, r, o, s = 0) {
32531
32547
  if (r.sourceType !== "telemetry")
32532
32548
  throw new Error("Telemetry sources only supported");
32533
- const s = this.getAvailableDataFetchWorker();
32534
- if (!s)
32549
+ const g = this.getAvailableDataFetchWorker();
32550
+ if (!g)
32535
32551
  throw new Error("No available data fetch worker");
32536
- const g = this.subscribeTelemetry(
32552
+ const l = this.subscribeTelemetry(
32537
32553
  n,
32538
32554
  r,
32539
32555
  "localization",
32540
- async (l) => {
32541
- if (l === "too much data" || l === void 0) {
32556
+ async (u) => {
32557
+ if (u === "too much data" || u === void 0) {
32542
32558
  o(NoData);
32543
32559
  return;
32544
32560
  }
32545
- const u = l[l.length - 1][1];
32546
- let c = u;
32547
- u.url && (s.postMessage({ url: u.url }), s.onmessage = (d) => {
32548
- c = d.data.response, c.odometry && o({
32549
- worldToLocal: c.odometry.worldToLocal,
32550
- pose: {
32551
- translation: {
32552
- x: c.odometry.pose.translation.x,
32553
- y: c.odometry.pose.translation.y,
32554
- z: c.odometry.pose.translation.z
32555
- },
32556
- rotation: {
32557
- x: c.odometry.pose.rotation.x,
32558
- y: c.odometry.pose.rotation.y,
32559
- z: c.odometry.pose.rotation.z,
32560
- w: c.odometry.pose.rotation.w
32561
+ const c = this.getNearestPoint(
32562
+ u
32563
+ );
32564
+ let d;
32565
+ if (c[1].url)
32566
+ try {
32567
+ d = (await (await fetch(c[1].url)).json()).odometry;
32568
+ } catch (B) {
32569
+ throw console.error("Failed to fetch odometry data:", B), B;
32570
+ }
32571
+ else
32572
+ d = c[1].odometry;
32573
+ if (s) {
32574
+ const R = u.filter(
32575
+ (f) => f[0] <= c[0] && f[0] >= c[0] - s * 1e3
32576
+ ).map(async (f) => {
32577
+ var p, U;
32578
+ if (f[1].url)
32579
+ try {
32580
+ const w = await (await fetch(f[1].url)).json();
32581
+ return [f[0], (p = w.odometry) == null ? void 0 : p.pose];
32582
+ } catch (y) {
32583
+ throw console.error("Failed to fetch trail odometry data:", y), y;
32561
32584
  }
32562
- },
32563
- covariance: []
32585
+ return [f[0], (U = f[1].odometry) == null ? void 0 : U.pose];
32564
32586
  });
32587
+ try {
32588
+ const f = await Promise.all(R);
32589
+ o({
32590
+ worldToLocal: d.worldToLocal,
32591
+ pose: d.pose,
32592
+ trail: f,
32593
+ covariance: []
32594
+ });
32595
+ return;
32596
+ } catch (f) {
32597
+ throw console.error("Failed to process trail data:", f), f;
32598
+ }
32599
+ }
32600
+ o({
32601
+ worldToLocal: d.worldToLocal,
32602
+ pose: d.pose,
32603
+ covariance: []
32565
32604
  });
32566
32605
  }
32567
32606
  );
32568
32607
  return () => {
32569
- this.releaseDataFetchWorker(s), g();
32608
+ this.releaseDataFetchWorker(g), l();
32570
32609
  };
32571
32610
  }
32572
32611
  subscribeToPose(n, r, o) {
@@ -32586,7 +32625,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32586
32625
  o(NoData);
32587
32626
  return;
32588
32627
  }
32589
- let u = l[l.length - 1][1];
32628
+ let u = this.getNearestPoint(l)[1];
32590
32629
  u.startsWith("http") ? (s.postMessage({ url: u }), s.onmessage = (c) => {
32591
32630
  u = JSON.stringify(c.data.response), o(JSON.parse(u));
32592
32631
  }) : o(JSON.parse(u));
@@ -32617,7 +32656,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32617
32656
  o(NoData);
32618
32657
  return;
32619
32658
  }
32620
- const c = u[u.length - 1][1];
32659
+ const c = this.getNearestPoint(u)[1];
32621
32660
  if (c.url) {
32622
32661
  if (s[c.url]) {
32623
32662
  o(s[c.url]);
@@ -32654,7 +32693,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32654
32693
  o(NoData);
32655
32694
  return;
32656
32695
  }
32657
- const g = s[s.length - 1][1], { url: l } = g, u = this.videoCache.get(l, async () => new Promise((c) => {
32696
+ const g = this.getNearestPoint(s)[1], { url: l } = g, u = this.videoCache.get(l, async () => new Promise((c) => {
32658
32697
  const d = document.createElement("video");
32659
32698
  d.src = l, d.onload = () => {
32660
32699
  };
@@ -32679,7 +32718,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32679
32718
  o(NoData);
32680
32719
  return;
32681
32720
  }
32682
- o(s[s.length - 1][1]);
32721
+ o(this.getNearestPoint(s)[1]);
32683
32722
  }
32684
32723
  );
32685
32724
  }
@@ -32699,7 +32738,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32699
32738
  o(NoData);
32700
32739
  return;
32701
32740
  }
32702
- const u = l[l.length - 1][1];
32741
+ const u = this.getNearestPoint(l)[1];
32703
32742
  o(u);
32704
32743
  }
32705
32744
  );
@@ -32715,7 +32754,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32715
32754
  o(NoData);
32716
32755
  return;
32717
32756
  }
32718
- let g = s[s.length - 1][1];
32757
+ let g = this.getNearestPoint(s)[1];
32719
32758
  g.startsWith("http") && (g = await (await fetch(g)).text()), o(JSON.parse(g));
32720
32759
  });
32721
32760
  }
@@ -32727,7 +32766,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32727
32766
  o(NoData);
32728
32767
  return;
32729
32768
  }
32730
- o(s[s.length - 1][1]);
32769
+ o(this.getNearestPoint(s)[1]);
32731
32770
  });
32732
32771
  }
32733
32772
  subscribeToNumeric(n, r, o) {
@@ -32765,7 +32804,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32765
32804
  o(NoData);
32766
32805
  return;
32767
32806
  }
32768
- const g = s[s.length - 1][1].url, l = new Image();
32807
+ const g = this.getNearestPoint(s)[1].url, l = new Image();
32769
32808
  l.src = g, l.onload = () => {
32770
32809
  const u = document.createElement("canvas");
32771
32810
  u.width = l.width, u.height = l.height;