@formant/data-sdk 1.47.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,44 +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
- n(f);
32440
- else {
32441
- let U = p[0], y = p[1];
32442
- f.forEach((w) => {
32443
- const D = w[0], m = w[1];
32444
- Math.abs(D - u.getTime()) < Math.abs(U - u.getTime()) && (U = D, y = m);
32445
- }), n([[U, y]]);
32446
- }
32447
- } else
32448
- n(void 0);
32449
- } 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) {
32450
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);
32451
32458
  };
32452
32459
  return this.timeFinders.push(l), l;
32453
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
+ }
32454
32469
  removeFinder(n) {
32455
32470
  this.timeFinders = this.timeFinders.filter((r) => r !== n);
32456
32471
  }
@@ -32489,7 +32504,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32489
32504
  o(NoData);
32490
32505
  return;
32491
32506
  }
32492
- const d = c[c.length - 1][1];
32507
+ const d = this.getNearestPoint(c)[1];
32493
32508
  if (typeof d == "string")
32494
32509
  o(JSON.parse(d));
32495
32510
  else {
@@ -32511,7 +32526,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32511
32526
  o(NoData);
32512
32527
  return;
32513
32528
  }
32514
- let d = c[c.length - 1][1];
32529
+ let d = this.getNearestPoint(c)[1];
32515
32530
  d.url && (g.postMessage({ url: d.url }), g.onmessage = (B) => {
32516
32531
  if (d = B.data.response, d.pointClouds) {
32517
32532
  const { url: R, worldToLocal: f } = d.pointClouds[0];
@@ -32528,46 +32543,69 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32528
32543
  this.releasePCDWorker(s), this.releaseDataFetchWorker(g), l(), u();
32529
32544
  };
32530
32545
  }
32531
- subscribeToOdometry(n, r, o) {
32546
+ subscribeToOdometry(n, r, o, s = 0) {
32532
32547
  if (r.sourceType !== "telemetry")
32533
32548
  throw new Error("Telemetry sources only supported");
32534
- const s = this.getAvailableDataFetchWorker();
32535
- if (!s)
32549
+ const g = this.getAvailableDataFetchWorker();
32550
+ if (!g)
32536
32551
  throw new Error("No available data fetch worker");
32537
- const g = this.subscribeTelemetry(
32552
+ const l = this.subscribeTelemetry(
32538
32553
  n,
32539
32554
  r,
32540
32555
  "localization",
32541
- async (l) => {
32542
- if (l === "too much data" || l === void 0) {
32556
+ async (u) => {
32557
+ if (u === "too much data" || u === void 0) {
32543
32558
  o(NoData);
32544
32559
  return;
32545
32560
  }
32546
- const u = l[l.length - 1][1];
32547
- let c = u;
32548
- u.url && (s.postMessage({ url: u.url }), s.onmessage = (d) => {
32549
- c = d.data.response, c.odometry && o({
32550
- worldToLocal: c.odometry.worldToLocal,
32551
- pose: {
32552
- translation: {
32553
- x: c.odometry.pose.translation.x,
32554
- y: c.odometry.pose.translation.y,
32555
- z: c.odometry.pose.translation.z
32556
- },
32557
- rotation: {
32558
- x: c.odometry.pose.rotation.x,
32559
- y: c.odometry.pose.rotation.y,
32560
- z: c.odometry.pose.rotation.z,
32561
- 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;
32562
32584
  }
32563
- },
32564
- covariance: []
32585
+ return [f[0], (U = f[1].odometry) == null ? void 0 : U.pose];
32565
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: []
32566
32604
  });
32567
32605
  }
32568
32606
  );
32569
32607
  return () => {
32570
- this.releaseDataFetchWorker(s), g();
32608
+ this.releaseDataFetchWorker(g), l();
32571
32609
  };
32572
32610
  }
32573
32611
  subscribeToPose(n, r, o) {
@@ -32587,7 +32625,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32587
32625
  o(NoData);
32588
32626
  return;
32589
32627
  }
32590
- let u = l[l.length - 1][1];
32628
+ let u = this.getNearestPoint(l)[1];
32591
32629
  u.startsWith("http") ? (s.postMessage({ url: u }), s.onmessage = (c) => {
32592
32630
  u = JSON.stringify(c.data.response), o(JSON.parse(u));
32593
32631
  }) : o(JSON.parse(u));
@@ -32618,7 +32656,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32618
32656
  o(NoData);
32619
32657
  return;
32620
32658
  }
32621
- const c = u[u.length - 1][1];
32659
+ const c = this.getNearestPoint(u)[1];
32622
32660
  if (c.url) {
32623
32661
  if (s[c.url]) {
32624
32662
  o(s[c.url]);
@@ -32655,7 +32693,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32655
32693
  o(NoData);
32656
32694
  return;
32657
32695
  }
32658
- 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) => {
32659
32697
  const d = document.createElement("video");
32660
32698
  d.src = l, d.onload = () => {
32661
32699
  };
@@ -32680,7 +32718,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32680
32718
  o(NoData);
32681
32719
  return;
32682
32720
  }
32683
- o(s[s.length - 1][1]);
32721
+ o(this.getNearestPoint(s)[1]);
32684
32722
  }
32685
32723
  );
32686
32724
  }
@@ -32700,7 +32738,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32700
32738
  o(NoData);
32701
32739
  return;
32702
32740
  }
32703
- const u = l[l.length - 1][1];
32741
+ const u = this.getNearestPoint(l)[1];
32704
32742
  o(u);
32705
32743
  }
32706
32744
  );
@@ -32716,7 +32754,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32716
32754
  o(NoData);
32717
32755
  return;
32718
32756
  }
32719
- let g = s[s.length - 1][1];
32757
+ let g = this.getNearestPoint(s)[1];
32720
32758
  g.startsWith("http") && (g = await (await fetch(g)).text()), o(JSON.parse(g));
32721
32759
  });
32722
32760
  }
@@ -32728,7 +32766,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32728
32766
  o(NoData);
32729
32767
  return;
32730
32768
  }
32731
- o(s[s.length - 1][1]);
32769
+ o(this.getNearestPoint(s)[1]);
32732
32770
  });
32733
32771
  }
32734
32772
  subscribeToNumeric(n, r, o) {
@@ -32766,7 +32804,7 @@ class TelemetryUniverseData extends BasicUniverseDataConnector {
32766
32804
  o(NoData);
32767
32805
  return;
32768
32806
  }
32769
- const g = s[s.length - 1][1].url, l = new Image();
32807
+ const g = this.getNearestPoint(s)[1].url, l = new Image();
32770
32808
  l.src = g, l.onload = () => {
32771
32809
  const u = document.createElement("canvas");
32772
32810
  u.width = l.width, u.height = l.height;