pxt-common-packages 9.3.5 → 9.3.9

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.
@@ -5,5 +5,7 @@
5
5
  "azureiot.onEvent": "Registers code when the MQTT client gets connected or disconnected",
6
6
  "azureiot.onEvent|param|event": "@param handler ",
7
7
  "azureiot.onMessageReceived": "Registers code to run when a message is received",
8
+ "azureiot.publishMessageBuffer": "Send a message via mqtt",
9
+ "azureiot.publishMessageHex": "Send a message via mqtt",
8
10
  "azureiot.publishMessageJSON": "Send a message via mqtt"
9
11
  }
@@ -154,7 +154,8 @@ namespace azureiot {
154
154
  try {
155
155
  c.disconnect()
156
156
  }
157
- finally {
157
+ catch {
158
+ // just ignore errors disconnecting
158
159
  _mqttClient = undefined
159
160
  }
160
161
  }
@@ -166,6 +167,7 @@ namespace azureiot {
166
167
  export function connect() {
167
168
  const c = mqttClient();
168
169
  if (!c.connected) {
170
+ c.connect() // start connect if not started yet
169
171
  // busy wait for connection
170
172
  const start = control.millis()
171
173
  const timeout = 30000
@@ -216,8 +218,49 @@ namespace azureiot {
216
218
  let topic = `devices/${c.opt.clientId}/messages/events/`;
217
219
  if (sysProps)
218
220
  topic += encodeQuery(sysProps);
221
+ const m = JSON.stringify(msg)
222
+ msg = null
219
223
  // qos, retained are not supported
220
- c.publish(topic, JSON.stringify(msg));
224
+ c.publish(topic, m);
225
+ }
226
+
227
+ /**
228
+ * Send a message via mqtt
229
+ * @param msg
230
+ */
231
+ //%
232
+ export function publishMessageBuffer(msg: Buffer, sysProps?: SMap<string>) {
233
+ const c = mqttClient();
234
+ let topic = `devices/${c.opt.clientId}/messages/events/`;
235
+ if (sysProps)
236
+ topic += encodeQuery(sysProps);
237
+ // qos, retained are not supported
238
+ c.publish(topic, msg);
239
+ }
240
+
241
+ /**
242
+ * Send a message via mqtt
243
+ * @param msg
244
+ */
245
+ //%
246
+ export function publishMessageHex(msg: Buffer, len?: number, sysProps?: SMap<string>) {
247
+ const c = mqttClient();
248
+ let topic = `devices/${c.opt.clientId}/messages/events/`;
249
+ if (sysProps)
250
+ topic += encodeQuery(sysProps);
251
+ if (len == null)
252
+ len = msg.length
253
+ if (len > msg.length) {
254
+ log(`len too long: ${len}/${msg.length}`)
255
+ len = msg.length
256
+ }
257
+ // qos, retained are not supported
258
+ if (c.startPublish(topic, len * 2)) {
259
+ const chunk = 128
260
+ for (let ptr = 0; ptr < len; ptr += chunk)
261
+ c.continuePublish(Buffer.fromUTF8(msg.slice(ptr, Math.min(chunk, len - ptr)).toHex()))
262
+ c.finishPublish()
263
+ }
221
264
  }
222
265
 
223
266
  /**