@formant/data-sdk 0.0.76 → 0.0.77

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.
@@ -19217,6 +19217,227 @@ class Device {
19217
19217
  return interventionResponse;
19218
19218
  }
19219
19219
  }
19220
+ class PeerDevice {
19221
+ constructor(peer_url) {
19222
+ __publicField(this, "rtcClient");
19223
+ __publicField(this, "remoteDevicePeerId");
19224
+ __publicField(this, "realtimeListeners", []);
19225
+ __publicField(this, "id");
19226
+ __publicField(this, "handleMessage", (peerId, message) => {
19227
+ this.realtimeListeners.forEach((_) => _(peerId, message));
19228
+ });
19229
+ this.peer_url = peer_url;
19230
+ }
19231
+ async getLatestTelemetry() {
19232
+ const data = await fetch(`${this.peer_url}/telemetry`);
19233
+ const telemetry = await data.json();
19234
+ const entries = Object.entries(telemetry);
19235
+ return entries.map(([stream, latestValue]) => {
19236
+ const v = {
19237
+ deviceId: this.id,
19238
+ streamName: stream,
19239
+ streamType: "json",
19240
+ currentValue: latestValue,
19241
+ currentValueTime: latestValue.timestamp,
19242
+ tags: {}
19243
+ };
19244
+ return v;
19245
+ });
19246
+ }
19247
+ async getDeviceId() {
19248
+ let result = await fetch(`${this.peer_url}/configuration`);
19249
+ const cfg = await result.json();
19250
+ return cfg.agent_config.id;
19251
+ }
19252
+ async getConfiguration() {
19253
+ let result = await fetch(`${this.peer_url}/configuration`);
19254
+ const cfg = await result.json();
19255
+ return cfg.agent_config.document;
19256
+ }
19257
+ getRealtimeStatus() {
19258
+ if (this.rtcClient && this.remoteDevicePeerId) {
19259
+ return this.rtcClient.getConnectionStatus(this.remoteDevicePeerId);
19260
+ } else {
19261
+ throw new Error(`Realtime connection hasn't been started`);
19262
+ }
19263
+ }
19264
+ getRealtimePing() {
19265
+ if (this.rtcClient && this.remoteDevicePeerId) {
19266
+ return this.rtcClient.getPing(this.remoteDevicePeerId);
19267
+ } else {
19268
+ throw new Error(`Realtime connection hasn't been started`);
19269
+ }
19270
+ }
19271
+ async startRealtimeConnection(sessionType) {
19272
+ if (!this.rtcClient) {
19273
+ const rtcClient = new dist.exports.RtcClient({
19274
+ signalingClient: new dist.exports.SignalingPromiseClient(FORMANT_API_URL, null, null),
19275
+ getToken: async () => {
19276
+ return defined(Authentication.token, "Realtime when user isn't authorized");
19277
+ },
19278
+ receive: this.handleMessage
19279
+ });
19280
+ while (!rtcClient.isReady()) {
19281
+ await delay(100);
19282
+ }
19283
+ const peers = await rtcClient.getPeers();
19284
+ const devicePeer = peers.find((_) => _.deviceId === this.id);
19285
+ if (!devicePeer) {
19286
+ throw new Error("Cannot find peer, is the robot offline?");
19287
+ }
19288
+ this.remoteDevicePeerId = devicePeer.id;
19289
+ await rtcClient.connect(this.remoteDevicePeerId, void 0, sessionType);
19290
+ while (rtcClient.getConnectionStatus(this.remoteDevicePeerId) !== "connected") {
19291
+ await delay(100);
19292
+ }
19293
+ this.rtcClient = rtcClient;
19294
+ } else {
19295
+ throw new Error(`Already created realtime connection to device ${this.id}`);
19296
+ }
19297
+ }
19298
+ addRealtimeListener(listener) {
19299
+ this.realtimeListeners.push(listener);
19300
+ }
19301
+ removeRealtimeListener(listener) {
19302
+ const i = this.realtimeListeners.indexOf(listener);
19303
+ if (i === -1) {
19304
+ throw new Error("Could not find realtime listener to remove");
19305
+ }
19306
+ this.realtimeListeners.splice(i, 1);
19307
+ }
19308
+ async getRealtimeVideoStreams() {
19309
+ var _a, _b, _c;
19310
+ const document2 = await this.getConfiguration();
19311
+ const streams = [];
19312
+ for (const _ of (_a = document2.teleop.hardwareStreams) != null ? _a : []) {
19313
+ if (_.rtcStreamType === "h264-video-frame") {
19314
+ streams.push({
19315
+ name: _.name
19316
+ });
19317
+ }
19318
+ }
19319
+ for (const _ of (_b = document2.teleop.rosStreams) != null ? _b : []) {
19320
+ if (_.topicType == "formant/H264VideoFrame") {
19321
+ streams.push({
19322
+ name: _.topicName
19323
+ });
19324
+ }
19325
+ }
19326
+ for (const _ of (_c = document2.teleop.customStreams) != null ? _c : []) {
19327
+ if (_.rtcStreamType === "h264-video-frame") {
19328
+ streams.push({
19329
+ name: _.name
19330
+ });
19331
+ }
19332
+ }
19333
+ return streams;
19334
+ }
19335
+ async getRealtimeManipulators() {
19336
+ var _a;
19337
+ const document2 = await this.getConfiguration();
19338
+ const manipulators = [];
19339
+ for (const _ of (_a = document2.teleop.rosStreams) != null ? _a : []) {
19340
+ if (_.topicType == "sensor_msgs/JointState") {
19341
+ manipulators.push(new Manipulator(this, {
19342
+ currentJointStateStream: { name: _.topicName },
19343
+ plannedJointStateStream: _.plannedTopic ? { name: _.plannedTopic } : void 0,
19344
+ planValidStream: _.planValidTopic ? { name: _.planValidTopic } : void 0,
19345
+ endEffectorStream: _.endEffectorTopic ? { name: _.endEffectorTopic } : void 0,
19346
+ endEffectorLinkName: _.endEffectorLinkName,
19347
+ baseReferenceFrame: _.baseReferenceFrame,
19348
+ localFrame: _.localFrame
19349
+ }));
19350
+ }
19351
+ }
19352
+ return manipulators;
19353
+ }
19354
+ async startListeningToRealtimeVideo(stream) {
19355
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19356
+ const devicePeer = await this.getRemotePeer();
19357
+ client.controlRemoteStream(defined(devicePeer).id, {
19358
+ streamName: stream.name,
19359
+ enable: true,
19360
+ pipeline: "rtc"
19361
+ });
19362
+ }
19363
+ async stopListeningToRealtimeVideo(stream) {
19364
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19365
+ const devicePeer = await this.getRemotePeer();
19366
+ client.controlRemoteStream(defined(devicePeer).id, {
19367
+ streamName: stream.name,
19368
+ enable: false,
19369
+ pipeline: "rtc"
19370
+ });
19371
+ }
19372
+ async startListeningToRealtimeDataStream(stream) {
19373
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19374
+ const devicePeer = await this.getRemotePeer();
19375
+ client.controlRemoteStream(defined(devicePeer).id, {
19376
+ streamName: stream.name,
19377
+ enable: true,
19378
+ pipeline: "rtc"
19379
+ });
19380
+ }
19381
+ async stopListeningToRealtimeDataStream(stream) {
19382
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19383
+ const devicePeer = await this.getRemotePeer();
19384
+ client.controlRemoteStream(defined(devicePeer).id, {
19385
+ streamName: stream.name,
19386
+ enable: false,
19387
+ pipeline: "rtc"
19388
+ });
19389
+ }
19390
+ async enableRealtimeTelemetryPriorityIngestion(streamName) {
19391
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19392
+ const devicePeer = await this.getRemotePeer();
19393
+ client.controlRemoteStream(defined(devicePeer).id, {
19394
+ streamName,
19395
+ enablePriorityUpload: true,
19396
+ pipeline: "telemetry"
19397
+ });
19398
+ }
19399
+ async disableRealtimeTelemetryPriorityIngestion(streamName) {
19400
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19401
+ const devicePeer = await this.getRemotePeer();
19402
+ client.controlRemoteStream(defined(devicePeer).id, {
19403
+ streamName,
19404
+ enablePriorityUpload: false,
19405
+ pipeline: "telemetry"
19406
+ });
19407
+ }
19408
+ async getRemotePeer() {
19409
+ const peers = await defined(this.rtcClient, "Realtime connection has not been started").getPeers();
19410
+ const devicePeer = peers.find((_) => _.deviceId === this.id);
19411
+ return defined(devicePeer, "Could not find remote peer for device " + this.id);
19412
+ }
19413
+ async stopRealtimeConnection() {
19414
+ if (this.rtcClient) {
19415
+ await this.rtcClient.disconnect(this.id);
19416
+ } else {
19417
+ throw new Error(`Realtime connection hasn't been started for ${this.id}`);
19418
+ }
19419
+ }
19420
+ async createCustomDataChannel(channelName, rtcConfig) {
19421
+ const client = defined(this.rtcClient, "Realtime connection has not been started");
19422
+ const devicePeer = await this.getRemotePeer();
19423
+ const p = await new Promise((resolve) => {
19424
+ client.createCustomDataChannel(defined(devicePeer).id, channelName, __spreadValues({
19425
+ ordered: true
19426
+ }, rtcConfig), false, (_peerId, channel) => {
19427
+ const dataChannel = new DataChannel(channel);
19428
+ resolve(dataChannel);
19429
+ });
19430
+ });
19431
+ await p.waitTilReady();
19432
+ return p;
19433
+ }
19434
+ createCustomRequestDataChannel(channelName, timeout = 3e3) {
19435
+ return new TextRequestDataChannel(this, channelName, timeout);
19436
+ }
19437
+ createCustomBinaryRequestDataChannel(channelName, timeout = 3e3) {
19438
+ return new BinaryRequestDataChannel(this, channelName, timeout);
19439
+ }
19440
+ }
19220
19441
  const _Fleet = class {
19221
19442
  static async setDefaultDevice(deviceId) {
19222
19443
  _Fleet.defaultDeviceId = deviceId;
@@ -19242,6 +19463,11 @@ const _Fleet = class {
19242
19463
  _Fleet.knownContext.push(new WeakRef(context));
19243
19464
  return context;
19244
19465
  }
19466
+ static async getPeerDevice(url) {
19467
+ const peer = new PeerDevice(url);
19468
+ peer.id = await peer.getDeviceId();
19469
+ return peer;
19470
+ }
19245
19471
  static async getDevice(deviceId) {
19246
19472
  if (!Authentication.token) {
19247
19473
  throw new Error("Not authenticated");
@@ -19537,4 +19763,4 @@ const moduleName = urlParams.get("module");
19537
19763
  if (moduleName) {
19538
19764
  Authentication.listenForRefresh();
19539
19765
  }
19540
- export { App, Authentication, BinaryRequestDataChannel, CaptureStream, DataChannel, Device, Fleet, KeyValue, Manipulator, SessionType, TextRequestDataChannel, accessLevels, administrator, aggregateLevels, annotationTypes, eventTypes, healthStatuses, interventionTypes, operator, severities, videoMimeTypes, viewer };
19766
+ export { App, Authentication, BinaryRequestDataChannel, CaptureStream, DataChannel, Device, Fleet, KeyValue, Manipulator, PeerDevice, SessionType, TextRequestDataChannel, accessLevels, administrator, aggregateLevels, annotationTypes, eventTypes, healthStatuses, interventionTypes, operator, severities, videoMimeTypes, viewer };