@transitive-sdk/utils-web 0.15.0 → 0.16.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/dist/utils-web.js +36 -25
- package/package.json +1 -1
package/dist/utils-web.js
CHANGED
|
@@ -805,9 +805,10 @@ var require_MqttSync = __commonJS({
|
|
|
805
805
|
if (!inclMeta && path.some((field) => field[0] == "$")) {
|
|
806
806
|
return;
|
|
807
807
|
}
|
|
808
|
-
|
|
808
|
+
const rpcHandler = this.getRPCHandler(topic);
|
|
809
|
+
if (rpcHandler) {
|
|
809
810
|
const json = mqttParsePayload(payload);
|
|
810
|
-
this.handleRPCRequest(
|
|
811
|
+
this.handleRPCRequest(path, rpcHandler, json);
|
|
811
812
|
} else if (this.rpcCallbacks[topic]) {
|
|
812
813
|
const json = mqttParsePayload(payload);
|
|
813
814
|
this.handleRPCResponse(topic, json);
|
|
@@ -1183,12 +1184,17 @@ var require_MqttSync = __commonJS({
|
|
|
1183
1184
|
/* --------------------------------------------------------------------------
|
|
1184
1185
|
* Remote Procedure Calls (RPC)
|
|
1185
1186
|
*/
|
|
1187
|
+
/** Given a (ground) topic find the matching RPC handler, if any. This is
|
|
1188
|
+
* needed because RPC topics can include wildcards. */
|
|
1189
|
+
getRPCHandler(topic) {
|
|
1190
|
+
return _3.find(this.rpcHandlers, (_handler, topicSelector) => topicMatch(topicSelector, topic));
|
|
1191
|
+
}
|
|
1186
1192
|
/* Handle RPC requests */
|
|
1187
|
-
async handleRPCRequest(
|
|
1188
|
-
log2.debug("handling RPC request for",
|
|
1189
|
-
const
|
|
1190
|
-
const result = handler(json.args);
|
|
1191
|
-
const responseTopic = `${
|
|
1193
|
+
async handleRPCRequest(path, handler, json) {
|
|
1194
|
+
log2.debug("handling RPC request for", path, json);
|
|
1195
|
+
const commandTopic = pathToTopic2(path.slice(0, -1));
|
|
1196
|
+
const result = handler(json.args, commandTopic);
|
|
1197
|
+
const responseTopic = `${commandTopic}/response/${json.id}`;
|
|
1192
1198
|
if (result instanceof Promise) {
|
|
1193
1199
|
result.then((resultValue) => this.mqtt.publish(
|
|
1194
1200
|
responseTopic,
|
|
@@ -1211,24 +1217,29 @@ var require_MqttSync = __commonJS({
|
|
|
1211
1217
|
this.mqtt.unsubscribe(topic);
|
|
1212
1218
|
}
|
|
1213
1219
|
/** Register an RPC request handler. Example:
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1220
|
+
* ```js
|
|
1221
|
+
* mqttSync.register('/mySquare', (arg, commandTopic) => {
|
|
1222
|
+
* log.debug('we got request on topic', commandTopic);
|
|
1223
|
+
* log.debug('running /mySquare with args', arg);
|
|
1224
|
+
* return arg * arg;
|
|
1225
|
+
* });
|
|
1226
|
+
* ```
|
|
1227
|
+
* Note that the command topic needs to be in the capabilities namespace like
|
|
1228
|
+
* any other topic. In robot capabilities, as usual, these can start in `/`
|
|
1229
|
+
* because the local mqtt bridge operated by the robot agent will place all
|
|
1230
|
+
* topics in their respective namespace. In the cloud and on the web you will
|
|
1231
|
+
* need to use the respective namespace, i.e.,
|
|
1232
|
+
* `/orgId/deviceId/@scope/capName/capVersion/`.
|
|
1233
|
+
*
|
|
1234
|
+
* You can use wildcards in the registered topic. The handler will receive the
|
|
1235
|
+
* actual, ground topic the request was made on as the second argument. This
|
|
1236
|
+
* allows you to make the RPCs behavior depend on the topic.
|
|
1237
|
+
*
|
|
1238
|
+
* #### Async/Await
|
|
1239
|
+
* Yes, you can make the handler `async` and use `await` inside of it. This
|
|
1240
|
+
* will be handled correctly, i.e., MqttSync will await the result of the
|
|
1241
|
+
* handler before responding to the RPC request client.
|
|
1242
|
+
*/
|
|
1232
1243
|
register(command, handler) {
|
|
1233
1244
|
log2.debug("registering RPC handler for", command);
|
|
1234
1245
|
const requestTopic = `${command}/request`;
|