pxt-common-packages 9.3.6 → 9.3.10

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.
@@ -17,6 +17,7 @@ namespace azureiot {
17
17
  let _messageBusId: number;
18
18
  let _receiveHandler: (msg: Json, sysProps: SMap<string>) => void;
19
19
  let _methodHandlers: SMap<(msg: Json) => Json>;
20
+ let twinRespHandlers: SMap<(status: number, body: any) => void>
20
21
 
21
22
  function log(msg: string) {
22
23
  console.add(logPriority, "azureiot: " + msg);
@@ -156,7 +157,6 @@ namespace azureiot {
156
157
  }
157
158
  catch {
158
159
  // just ignore errors disconnecting
159
- _mqttClient = undefined
160
160
  }
161
161
  }
162
162
  }
@@ -167,6 +167,7 @@ namespace azureiot {
167
167
  export function connect() {
168
168
  const c = mqttClient();
169
169
  if (!c.connected) {
170
+ c.connect() // start connect if not started yet
170
171
  // busy wait for connection
171
172
  const start = control.millis()
172
173
  const timeout = 30000
@@ -249,11 +250,17 @@ namespace azureiot {
249
250
  topic += encodeQuery(sysProps);
250
251
  if (len == null)
251
252
  len = msg.length
253
+ if (len > msg.length) {
254
+ log(`len too long: ${len}/${msg.length}`)
255
+ len = msg.length
256
+ }
252
257
  // qos, retained are not supported
253
- c.startPublish(topic, len * 2)
254
- const chunk = 128
255
- for (let ptr = 0; ptr < len; ptr += chunk)
256
- c.continuePublish(Buffer.fromUTF8(msg.slice(ptr, Math.min(chunk, len - ptr)).toHex()))
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
+ }
257
264
  }
258
265
 
259
266
  /**
@@ -313,16 +320,16 @@ namespace azureiot {
313
320
  c.publish('$iothub/methods/res/' + status + "/?$rid=" + props["$rid"], JSON.stringify(resp))
314
321
  }
315
322
 
316
- let twinRespHandlers: SMap<(status: number, body: any) => void>
317
-
318
323
  // $iothub/twin/res/{status}/?$rid={request id}
319
324
  function twinResponse(msg: mqtt.IMessage) {
320
325
  const args = parseTopicArgs(msg.topic)
321
326
  const h = twinRespHandlers[args["$rid"]]
322
327
  const status = parseInt(msg.topic.slice(17))
323
328
  // log(`twin resp: ${status} ${msg.content.toHex()} ${msg.content.toString()}`)
324
- if (h)
329
+ if (h) {
330
+ delete twinRespHandlers[args["$rid"]]
325
331
  h(status, JSON.parse(msg.content.toString() || "{}"))
332
+ }
326
333
  }
327
334
 
328
335
  export class ValueAwaiter {