@transitive-sdk/utils-web 0.14.14 → 0.15.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.
Files changed (2) hide show
  1. package/dist/utils-web.js +37 -11
  2. package/package.json +1 -1
package/dist/utils-web.js CHANGED
@@ -406,6 +406,8 @@ var require_common = __commonJS({
406
406
  callback && callback(count);
407
407
  }, delay);
408
408
  };
409
+ var storageRequestToSelector = (topic) => pathToTopic2(topicToPath2(topic).map((value) => value == "$store" ? "+" : value).map((value) => value == "$storeTail" ? "#" : value));
410
+ var selectorToStorageRequest = (topic) => pathToTopic2(topicToPath2(topic).map((value) => value[0] == "+" ? "$store" : value).map((value) => value[0] == "#" ? "$storeTail" : value));
409
411
  var getRandomId = (bytes = 6) => {
410
412
  const buffer = new Uint8Array(bytes);
411
413
  crypto.getRandomValues(buffer);
@@ -494,7 +496,9 @@ var require_common = __commonJS({
494
496
  formatDuration,
495
497
  tryJSONParse,
496
498
  decodeJWT: decodeJWT3,
497
- visitAncestor
499
+ visitAncestor,
500
+ storageRequestToSelector,
501
+ selectorToStorageRequest
498
502
  };
499
503
  }
500
504
  });
@@ -713,7 +717,8 @@ var require_MqttSync = __commonJS({
713
717
  versionCompare,
714
718
  encodeTopicElement,
715
719
  visitAncestor,
716
- getRandomId
720
+ getRandomId,
721
+ selectorToStorageRequest
717
722
  } = require_common();
718
723
  var { DataCache } = require_DataCache();
719
724
  var log2 = getLogger2("MqttSync");
@@ -771,7 +776,8 @@ var require_MqttSync = __commonJS({
771
776
  migrate,
772
777
  onReady,
773
778
  sliceTopic,
774
- onHeartbeatGranted
779
+ onHeartbeatGranted,
780
+ inclMeta
775
781
  }) {
776
782
  this.mqtt = mqttClient;
777
783
  this.sliceTopic = sliceTopic;
@@ -796,6 +802,9 @@ var require_MqttSync = __commonJS({
796
802
  path = path.slice(sliceTopic);
797
803
  topic = pathToTopic2(path);
798
804
  }
805
+ if (!inclMeta && path.some((field) => field[0] == "$")) {
806
+ return;
807
+ }
799
808
  if (this.rpcHandlers[topic]) {
800
809
  const json = mqttParsePayload(payload);
801
810
  this.handleRPCRequest(topic, json);
@@ -976,15 +985,15 @@ var require_MqttSync = __commonJS({
976
985
  callback && callback(count);
977
986
  });
978
987
  }
979
- /** register a callback for the next heartbeat from the broker */
988
+ /** Register a callback for the next heartbeat from the broker */
980
989
  waitForHeartbeatOnce(callback) {
981
990
  setTimeout(() => this.heartbeatWaitersOnce.push(callback), 1);
982
991
  }
983
- /** check whether we are subscribed to the given topic */
992
+ /* check whether we are subscribed to the given topic */
984
993
  isSubscribed(topic) {
985
994
  return Object.keys(this.subscribedPaths).some((subscribedTopic) => topicMatch(subscribedTopic, topic));
986
995
  }
987
- /** Check whether we are publishing the given topic in a non-atomic way.
996
+ /* Check whether we are publishing the given topic in a non-atomic way.
988
997
  This is used to determine whether to store the published value or not. */
989
998
  isPublished(topic) {
990
999
  return Object.keys(this.publishedPaths).some(
@@ -1018,7 +1027,7 @@ var require_MqttSync = __commonJS({
1018
1027
  delete this.subscribedPaths[topic];
1019
1028
  }
1020
1029
  }
1021
- /** Publish retained to MQTT, store as published, and return a promise */
1030
+ /* Publish retained to MQTT, store as published, and return a promise */
1022
1031
  _actuallyPublish(topic, value) {
1023
1032
  if (!this.mqtt.connected) {
1024
1033
  log2.warn("not connected, not publishing", topic);
@@ -1033,7 +1042,7 @@ var require_MqttSync = __commonJS({
1033
1042
  );
1034
1043
  return true;
1035
1044
  }
1036
- /** Send all items in the queue in sequence, if any and if not already
1045
+ /* Send all items in the queue in sequence, if any and if not already
1037
1046
  running. */
1038
1047
  // async _processQueue() {
1039
1048
  // if (this._processing) return; // already running (and probably waiting)
@@ -1082,7 +1091,7 @@ var require_MqttSync = __commonJS({
1082
1091
  addToQueue(topic, value) {
1083
1092
  this.publishQueue.set(topic, value);
1084
1093
  }
1085
- /** Add to publication queue */
1094
+ /* Add to publication queue */
1086
1095
  _enqueue(topic, value) {
1087
1096
  log2.debug("enqueuing", topic);
1088
1097
  this.addToQueue(topic, value);
@@ -1234,13 +1243,22 @@ var require_MqttSync = __commonJS({
1234
1243
  }
1235
1244
  /** Make an RPC request. Example:
1236
1245
  * ```js
1237
- * mqttSync.call('/mySquare', 11, result => {
1246
+ * mqttSync.call('/orgId/deviceId/@capScope/capName/capVersion/mySquare', 11, result => {
1238
1247
  * log.debug(`Called /mySquare with arg 11 and got ${result}`);
1239
1248
  * });
1240
1249
  * ```
1250
+ * This would call the RPC 'mySquare' registered by version `capVersion` of the
1251
+ * capability `@capScope/capName` running on device `deviceId` by user `orgId`.
1252
+ * That RPC would have been registered on the device using
1253
+ * `mqttSync.register('/mySquare', ...)` as shown above (because namespaces
1254
+ * on the device are auto-extended to the org, device and capability).
1255
+ *
1256
+ * RPCs are typically registered on the robot and called from the web or cloud
1257
+ * but the inverse is also possible, with the same namespace caveat.
1258
+ *
1241
1259
  * Alternative you can omit the callback and use async/await:
1242
1260
  * ```js
1243
- * const result = await mqttSync.call('/mySquare', 11);
1261
+ * const result = await mqttSync.call('${prefix}/mySquare', 11);
1244
1262
  * log.debug(`Called /mySquare with arg 11 and got ${result}`);
1245
1263
  * ```
1246
1264
  * See the note about namespaces in `register`.
@@ -1273,6 +1291,14 @@ var require_MqttSync = __commonJS({
1273
1291
  });
1274
1292
  }
1275
1293
  }
1294
+ /** Request the history of the described topics (selector with wildcards) to
1295
+ * be stored in ClickHouse for the `ttl` number of days (if the mqtt2clickhouse
1296
+ * service is running -- as it usually is inside the
1297
+ * transitiverobotics/clickhouse docker image). */
1298
+ requestHistoryStorage(topic, ttl = 1) {
1299
+ const storageRequest = selectorToStorageRequest(topic);
1300
+ this.mqtt.publish(storageRequest, String(ttl), { retain: true });
1301
+ }
1276
1302
  };
1277
1303
  module2.exports = MqttSync3;
1278
1304
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/utils-web",
3
- "version": "0.14.14",
3
+ "version": "0.15.0",
4
4
  "description": "Web utils for the Transitive framework",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "repository": {