eufy-security-client 3.6.0 → 3.7.1

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 (74) hide show
  1. package/.prettierignore/342/200/216 +8 -0
  2. package/.prettierrc +11 -0
  3. package/README.md +18 -0
  4. package/a.ts +61 -0
  5. package/build/error.js.map +1 -1
  6. package/build/eufysecurity.d.ts +1 -0
  7. package/build/eufysecurity.js +721 -224
  8. package/build/eufysecurity.js.map +1 -1
  9. package/build/http/api.d.ts +29 -0
  10. package/build/http/api.js +991 -701
  11. package/build/http/api.js.map +1 -1
  12. package/build/http/cache.js.map +1 -1
  13. package/build/http/const.d.ts +6 -1
  14. package/build/http/const.js +2044 -7536
  15. package/build/http/const.js.map +1 -1
  16. package/build/http/device.d.ts +4 -0
  17. package/build/http/device.js +1325 -440
  18. package/build/http/device.js.map +1 -1
  19. package/build/http/error.js.map +1 -1
  20. package/build/http/index.js.map +1 -1
  21. package/build/http/interfaces.d.ts +22 -15
  22. package/build/http/models.d.ts +2 -1
  23. package/build/http/parameter.js +74 -63
  24. package/build/http/parameter.js.map +1 -1
  25. package/build/http/station.d.ts +20 -2
  26. package/build/http/station.js +8639 -3566
  27. package/build/http/station.js.map +1 -1
  28. package/build/http/types.d.ts +12 -0
  29. package/build/http/types.js +297 -155
  30. package/build/http/types.js.map +1 -1
  31. package/build/http/utils.d.ts +16 -6
  32. package/build/http/utils.js +335 -208
  33. package/build/http/utils.js.map +1 -1
  34. package/build/index.js.map +1 -1
  35. package/build/interfaces.d.ts +4 -3
  36. package/build/logging.js +8 -13
  37. package/build/logging.js.map +1 -1
  38. package/build/mqtt/interface.d.ts +2 -2
  39. package/build/mqtt/service.js +12 -3
  40. package/build/mqtt/service.js.map +1 -1
  41. package/build/p2p/ble.js +7 -6
  42. package/build/p2p/ble.js.map +1 -1
  43. package/build/p2p/error.js.map +1 -1
  44. package/build/p2p/interfaces.d.ts +41 -6
  45. package/build/p2p/session.js +1484 -383
  46. package/build/p2p/session.js.map +1 -1
  47. package/build/p2p/talkback.js.map +1 -1
  48. package/build/p2p/types.js +36 -36
  49. package/build/p2p/types.js.map +1 -1
  50. package/build/p2p/utils.d.ts +10 -0
  51. package/build/p2p/utils.js +183 -90
  52. package/build/p2p/utils.js.map +1 -1
  53. package/build/push/client.js +15 -4
  54. package/build/push/client.js.map +1 -1
  55. package/build/push/error.js.map +1 -1
  56. package/build/push/interfaces.d.ts +8 -8
  57. package/build/push/models.js.map +1 -1
  58. package/build/push/parser.js +6 -2
  59. package/build/push/parser.js.map +1 -1
  60. package/build/push/service.js +214 -85
  61. package/build/push/service.js.map +1 -1
  62. package/build/push/types.js.map +1 -1
  63. package/build/push/utils.js.map +1 -1
  64. package/build/utils.js +7 -15
  65. package/build/utils.js.map +1 -1
  66. package/coverage/clover.xml +11133 -13648
  67. package/coverage/coverage-final.json +20 -30
  68. package/coverage/lcov-report/error.ts.html +3 -3
  69. package/coverage/lcov-report/index.html +50 -65
  70. package/coverage/lcov-report/logging.ts.html +598 -0
  71. package/coverage/lcov.info +21072 -25751
  72. package/dont-care.js +101 -0
  73. package/package.json +9 -5
  74. package/build/package.json +0 -81
@@ -5,7 +5,7 @@ const dgram_1 = require("dgram");
5
5
  const tiny_typed_emitter_1 = require("tiny-typed-emitter");
6
6
  const stream_1 = require("stream");
7
7
  const sweet_collections_1 = require("sweet-collections");
8
- const { parse } = require('date-and-time');
8
+ const { parse } = require("date-and-time");
9
9
  const utils_1 = require("./utils");
10
10
  const types_1 = require("./types");
11
11
  const types_2 = require("../http/types");
@@ -45,12 +45,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
45
45
  MAX_SEQUENCE_NUMBER = 65535;
46
46
  LOOP_RUNAWAY_LIMIT = 1000;
47
47
  /*
48
- * SEQUENCE_PROCESSING_BOUNDARY is used to determine if an incoming sequence number
49
- * that is lower than the expected one was already processed.
50
- * If it is within the boundary, it is determined as 'already processed',
51
- * If it is even lower, it is assumed that the sequence count has reached
52
- * MAX_SEQUENCE_NUMBER and restarted at 0.
53
- * */
48
+ * SEQUENCE_PROCESSING_BOUNDARY is used to determine if an incoming sequence number
49
+ * that is lower than the expected one was already processed.
50
+ * If it is within the boundary, it is determined as 'already processed',
51
+ * If it is even lower, it is assumed that the sequence count has reached
52
+ * MAX_SEQUENCE_NUMBER and restarted at 0.
53
+ * */
54
54
  SEQUENCE_PROCESSING_BOUNDARY = 20000; // worth of approx. 90 seconds of continous streaming
55
55
  socket;
56
56
  binded = false;
@@ -115,7 +115,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
115
115
  this.listeningPort = listeningPort;
116
116
  this.enableEmbeddedPKCS1Support = enableEmbeddedPKCS1Support;
117
117
  this.cloudAddresses = (0, utils_1.decodeP2PCloudIPs)(rawStation.app_conn);
118
- logging_1.rootP2PLogger.debug("Loaded P2P cloud ip addresses", { stationSN: rawStation.station_sn, ipAddress: ipAddress, cloudAddresses: this.cloudAddresses });
118
+ logging_1.rootP2PLogger.debug("Loaded P2P cloud ip addresses", {
119
+ stationSN: rawStation.station_sn,
120
+ ipAddress: ipAddress,
121
+ cloudAddresses: this.cloudAddresses,
122
+ });
119
123
  this.updateRawStation(rawStation);
120
124
  this.socket = (0, dgram_1.createSocket)("udp4");
121
125
  this.socket.on("message", (msg, rinfo) => this.handleMsg(msg, rinfo));
@@ -136,13 +140,15 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
136
140
  return true;
137
141
  }
138
142
  _wasSequenceNumberAlreadyProcessed(expectedSequence, receivedSequence) {
139
- if ((expectedSequence - this.SEQUENCE_PROCESSING_BOUNDARY) > 0) { // complete boundary without squence number reset
143
+ if (expectedSequence - this.SEQUENCE_PROCESSING_BOUNDARY > 0) {
144
+ // complete boundary without squence number reset
140
145
  return this._isBetween(receivedSequence, expectedSequence - this.SEQUENCE_PROCESSING_BOUNDARY, expectedSequence);
141
146
  }
142
- else { // there was a sequence number reset recently
147
+ else {
148
+ // there was a sequence number reset recently
143
149
  const isInRangeAfterReset = this._isBetween(receivedSequence, 0, expectedSequence);
144
150
  const isInRangeBeforeReset = this._isBetween(receivedSequence, this.MAX_SEQUENCE_NUMBER + (expectedSequence - this.SEQUENCE_PROCESSING_BOUNDARY), this.MAX_SEQUENCE_NUMBER);
145
- return (isInRangeBeforeReset || isInRangeAfterReset);
151
+ return isInRangeBeforeReset || isInRangeAfterReset;
146
152
  }
147
153
  }
148
154
  _initialize() {
@@ -186,10 +192,10 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
186
192
  bytesToRead: 0,
187
193
  channel: 0,
188
194
  signCode: 0,
189
- type: 0
195
+ type: 0,
190
196
  },
191
197
  bytesRead: 0,
192
- messages: {}
198
+ messages: {},
193
199
  };
194
200
  }
195
201
  initializeMessageState(datatype, rsaKey = null) {
@@ -210,14 +216,14 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
210
216
  videoFPS: 15,
211
217
  videoHeight: 1080,
212
218
  videoWidth: 1920,
213
- audioCodec: types_1.AudioCodec.NONE
219
+ audioCodec: types_1.AudioCodec.NONE,
214
220
  },
215
221
  rtspStream: {},
216
222
  rtspStreaming: {},
217
223
  receivedFirstIFrame: false,
218
224
  preFrameVideoData: Buffer.from([]),
219
225
  p2pTalkback: false,
220
- p2pTalkbackChannel: -1
226
+ p2pTalkbackChannel: -1,
221
227
  };
222
228
  }
223
229
  _clearTimeout(timeout) {
@@ -278,7 +284,13 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
278
284
  async sendMessage(errorSubject, address, msgID, payload) {
279
285
  await (0, utils_1.sendMessage)(this.socket, address, msgID, payload).catch((err) => {
280
286
  const error = (0, error_1.ensureError)(err);
281
- logging_1.rootP2PLogger.error(`${errorSubject} - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, address: address, msgID: msgID.toString("hex"), payload: payload?.toString("hex") });
287
+ logging_1.rootP2PLogger.error(`${errorSubject} - Error`, {
288
+ error: (0, utils_3.getError)(error),
289
+ stationSN: this.rawStation.station_sn,
290
+ address: address,
291
+ msgID: msgID.toString("hex"),
292
+ payload: payload?.toString("hex"),
293
+ });
282
294
  });
283
295
  }
284
296
  _disconnected() {
@@ -302,7 +314,8 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
302
314
  for (const channel in this.currentMessageState[types_1.P2PDataType.DATA].rtspStreaming) {
303
315
  this.endRTSPStream(Number.parseInt(channel));
304
316
  }
305
- this.sendQueue = this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING && queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING);
317
+ this.sendQueue = this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING &&
318
+ queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING);
306
319
  if (this.connected) {
307
320
  this.emit("close");
308
321
  }
@@ -312,7 +325,8 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
312
325
  this._initialize();
313
326
  }
314
327
  closeEnergySavingDevice() {
315
- if (this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING && queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length === 0 &&
328
+ if (this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING &&
329
+ queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length === 0 &&
316
330
  this.energySavingDevice &&
317
331
  !this.isCurrentlyStreaming() &&
318
332
  Array.from(this.messageStates.values()).filter((msgState) => msgState.acknowledged === false).length === 0) {
@@ -329,8 +343,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
329
343
  }
330
344
  }
331
345
  async renewDSKKey() {
332
- if (this.dskKey === "" || (this.dskExpiration && (new Date()).getTime() >= this.dskExpiration.getTime())) {
333
- logging_1.rootP2PLogger.debug(`DSK keys not present or expired, get/renew it`, { stationSN: this.rawStation.station_sn, dskKey: this.dskKey, dskExpiration: this.dskExpiration });
346
+ if (this.dskKey === "" || (this.dskExpiration && new Date().getTime() >= this.dskExpiration.getTime())) {
347
+ logging_1.rootP2PLogger.debug(`DSK keys not present or expired, get/renew it`, {
348
+ stationSN: this.rawStation.station_sn,
349
+ dskKey: this.dskKey,
350
+ dskExpiration: this.dskExpiration,
351
+ });
334
352
  await this.getDSKKeys();
335
353
  }
336
354
  }
@@ -394,7 +412,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
394
412
  }, this.MAX_CONNECTION_TIMEOUT);
395
413
  }
396
414
  _connect(address, p2p_did) {
397
- logging_1.rootP2PLogger.debug(`Connecting to host ${address.host} on port ${address.port} (CHECK_CAM)`, { stationSN: this.rawStation.station_sn, address: address, p2pDid: p2p_did });
415
+ logging_1.rootP2PLogger.debug(`Connecting to host ${address.host} on port ${address.port} (CHECK_CAM)`, {
416
+ stationSN: this.rawStation.station_sn,
417
+ address: address,
418
+ p2pDid: p2p_did,
419
+ });
398
420
  this.sendCamCheck(address, p2p_did);
399
421
  for (let i = address.port - 3; i < address.port; i++)
400
422
  this.sendCamCheck({ host: address.host, port: i }, p2p_did);
@@ -444,7 +466,13 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
444
466
  }
445
467
  catch (err) {
446
468
  const error = (0, error_1.ensureError)(err);
447
- logging_1.rootP2PLogger.error(`connect - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, host: host, currentRecBufferSize: this.socket.getRecvBufferSize(), recBufferRequestedSize: this.UDP_RECVBUFFERSIZE_BYTES });
469
+ logging_1.rootP2PLogger.error(`connect - Error`, {
470
+ error: (0, utils_3.getError)(error),
471
+ stationSN: this.rawStation.station_sn,
472
+ host: host,
473
+ currentRecBufferSize: this.socket.getRecvBufferSize(),
474
+ recBufferRequestedSize: this.UDP_RECVBUFFERSIZE_BYTES,
475
+ });
448
476
  }
449
477
  this.lookup(host);
450
478
  });
@@ -462,8 +490,10 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
462
490
  await this.sendMessage(`Send cam check (2)`, address, types_1.RequestMessageType.CHECK_CAM2, payload);
463
491
  }
464
492
  async sendPing(address) {
465
- if ((this.lastPong && ((new Date().getTime() - this.lastPong) / this.getHeartbeatInterval() >= this.MAX_RETRIES)) ||
466
- (this.connectTime && !this.lastPong && ((new Date().getTime() - this.connectTime) / this.getHeartbeatInterval() >= this.MAX_RETRIES))) {
493
+ if ((this.lastPong && (new Date().getTime() - this.lastPong) / this.getHeartbeatInterval() >= this.MAX_RETRIES) ||
494
+ (this.connectTime &&
495
+ !this.lastPong &&
496
+ (new Date().getTime() - this.connectTime) / this.getHeartbeatInterval() >= this.MAX_RETRIES)) {
467
497
  if (!this.energySavingDevice)
468
498
  logging_1.rootP2PLogger.warn(`Heartbeat check failed for station ${this.rawStation.station_sn}. Connection seems lost. Try to reconnect...`);
469
499
  this._disconnected();
@@ -502,7 +532,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
502
532
  }
503
533
  catch (err) {
504
534
  const error = (0, error_1.ensureError)(err);
505
- logging_1.rootP2PLogger.error(`sendCommandWithStringPayload CMD_SET_PAYLOAD - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, p2pcommand: p2pcommand, customData: customData });
535
+ logging_1.rootP2PLogger.error(`sendCommandWithStringPayload CMD_SET_PAYLOAD - Error`, {
536
+ error: (0, utils_3.getError)(error),
537
+ stationSN: this.rawStation.station_sn,
538
+ p2pcommand: p2pcommand,
539
+ customData: customData,
540
+ });
506
541
  }
507
542
  }
508
543
  else if (p2pcommand.commandType == types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD) {
@@ -512,7 +547,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
512
547
  }
513
548
  catch (err) {
514
549
  const error = (0, error_1.ensureError)(err);
515
- logging_1.rootP2PLogger.error(`sendCommandWithStringPayload CMD_DOORBELL_SET_PAYLOAD - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, p2pcommand: p2pcommand, customData: customData });
550
+ logging_1.rootP2PLogger.error(`sendCommandWithStringPayload CMD_DOORBELL_SET_PAYLOAD - Error`, {
551
+ error: (0, utils_3.getError)(error),
552
+ stationSN: this.rawStation.station_sn,
553
+ p2pcommand: p2pcommand,
554
+ customData: customData,
555
+ });
516
556
  }
517
557
  }
518
558
  this.sendCommand(p2pcommand, types_1.InternalP2PCommandType.WithStringPayload, nested_commandType, customData, nested_commandType2);
@@ -529,19 +569,19 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
529
569
  sendCommandPing(channel = this.channel) {
530
570
  this.sendCommand({
531
571
  commandType: types_1.CommandType.CMD_PING,
532
- channel: channel
572
+ channel: channel,
533
573
  }, types_1.InternalP2PCommandType.WithoutData);
534
574
  }
535
575
  sendCommandDevicePing(channel = this.channel) {
536
576
  this.sendCommand({
537
577
  commandType: types_1.CommandType.CMD_GET_DEVICE_PING,
538
- channel: channel
578
+ channel: channel,
539
579
  }, types_1.InternalP2PCommandType.WithoutData);
540
580
  }
541
581
  sendCommandWithoutData(commandType, channel = this.channel) {
542
582
  this.sendCommand({
543
583
  commandType: commandType,
544
- channel: channel
584
+ channel: channel,
545
585
  }, types_1.InternalP2PCommandType.WithoutData);
546
586
  }
547
587
  sendQueuedMessage() {
@@ -551,8 +591,10 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
551
591
  while ((queuedMessage = this.sendQueue.shift()) !== undefined) {
552
592
  let exists = false;
553
593
  let waitingAcknowledge = false;
554
- this.messageStates.forEach(stateMessage => {
555
- if (stateMessage.commandType === queuedMessage.p2pCommand.commandType && stateMessage.nestedCommandType === queuedMessage.nestedCommandType && !stateMessage.acknowledged) {
594
+ this.messageStates.forEach((stateMessage) => {
595
+ if (stateMessage.commandType === queuedMessage.p2pCommand.commandType &&
596
+ stateMessage.nestedCommandType === queuedMessage.nestedCommandType &&
597
+ !stateMessage.acknowledged) {
556
598
  exists = true;
557
599
  }
558
600
  if (!stateMessage.acknowledged || stateMessage.commandType === types_1.CommandType.CMD_GATEWAYINFO) {
@@ -569,8 +611,14 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
569
611
  }
570
612
  }
571
613
  }
572
- else if (!this.connected && this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING && queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length > 0) {
573
- logging_1.rootP2PLogger.debug(`Initiate station p2p connection to send queued data`, { stationSN: this.rawStation.station_sn, queuedDataCount: this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING && queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length });
614
+ else if (!this.connected &&
615
+ this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING &&
616
+ queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length > 0) {
617
+ logging_1.rootP2PLogger.debug(`Initiate station p2p connection to send queued data`, {
618
+ stationSN: this.rawStation.station_sn,
619
+ queuedDataCount: this.sendQueue.filter((queue) => queue.p2pCommand.commandType !== types_1.CommandType.CMD_PING &&
620
+ queue.p2pCommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING).length,
621
+ });
574
622
  this.connect();
575
623
  }
576
624
  }
@@ -581,9 +629,9 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
581
629
  p2pCommand: p2pcommand,
582
630
  nestedCommandType: nestedCommandType,
583
631
  nestedCommandType2: nested_commandType2,
584
- timestamp: +new Date,
632
+ timestamp: +new Date(),
585
633
  customData: customData,
586
- p2pCommandType: p2pcommandType
634
+ p2pCommandType: p2pcommandType,
587
635
  };
588
636
  this.sendQueue.push(message);
589
637
  if (p2pcommand.commandType !== types_1.CommandType.CMD_PING && p2pcommand.commandType !== types_1.CommandType.CMD_GET_DEVICE_PING)
@@ -598,7 +646,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
598
646
  if (this.connectAddress) {
599
647
  (0, utils_1.sendMessage)(this.socket, this.connectAddress, types_1.RequestMessageType.DATA, messageState.data).catch((err) => {
600
648
  const error = (0, error_1.ensureError)(err);
601
- logging_1.rootP2PLogger.error(`resendNotAcknowledgedCommand - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, sequence: sequence });
649
+ logging_1.rootP2PLogger.error(`resendNotAcknowledgedCommand - Error`, {
650
+ error: (0, utils_3.getError)(error),
651
+ stationSN: this.rawStation.station_sn,
652
+ sequence: sequence,
653
+ });
602
654
  });
603
655
  this.resendNotAcknowledgedCommand(sequence);
604
656
  }
@@ -607,7 +659,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
607
659
  }
608
660
  async _sendCommand(message) {
609
661
  if ((0, utils_1.isP2PQueueMessage)(message)) {
610
- const ageing = +new Date - message.timestamp;
662
+ const ageing = +new Date() - message.timestamp;
611
663
  if (ageing <= this.MAX_COMMAND_QUEUE_TIMEOUT) {
612
664
  const commandHeader = (0, utils_1.buildCommandHeader)(this.seqNumber, message.p2pCommand.commandType);
613
665
  let payload;
@@ -620,7 +672,8 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
620
672
  payload = (0, utils_1.buildIntStringCommandPayload)(this.encryption, this.p2pKey, this.rawStation.station_sn, this.rawStation.p2p_did, message.p2pCommand.commandType, message.p2pCommand.value, message.p2pCommand.valueSub === undefined ? 0 : message.p2pCommand.valueSub, message.p2pCommand.strValue === undefined ? "" : message.p2pCommand.strValue, message.p2pCommand.strValueSub === undefined ? "" : message.p2pCommand.strValueSub, channel);
621
673
  //TODO: Check if this "if" can be moved elsewhere
622
674
  if (message.p2pCommand.commandType === types_1.CommandType.CMD_NAS_TEST) {
623
- this.currentMessageState[types_1.P2PDataType.DATA].rtspStream[channel] = message.p2pCommand.value === 1 ? true : false;
675
+ this.currentMessageState[types_1.P2PDataType.DATA].rtspStream[channel] =
676
+ message.p2pCommand.value === 1 ? true : false;
624
677
  }
625
678
  break;
626
679
  case types_1.InternalP2PCommandType.WithString:
@@ -643,21 +696,28 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
643
696
  data: data,
644
697
  retries: 0,
645
698
  acknowledged: false,
646
- customData: message.customData
699
+ customData: message.customData,
647
700
  };
648
701
  message = messageState;
649
702
  this.seqNumber = this._incrementSequence(this.seqNumber);
650
703
  }
651
- else if (message.p2pCommand.commandType === types_1.CommandType.CMD_PING || message.p2pCommand.commandType === types_1.CommandType.CMD_GET_DEVICE_PING) {
704
+ else if (message.p2pCommand.commandType === types_1.CommandType.CMD_PING ||
705
+ message.p2pCommand.commandType === types_1.CommandType.CMD_GET_DEVICE_PING) {
652
706
  return;
653
707
  }
654
708
  else {
655
- logging_1.rootP2PLogger.warn(`Command aged out from send queue for station ${this.rawStation.station_sn}`, { commandType: message.p2pCommand.commandType, nestedCommandType: message.nestedCommandType, channel: message.p2pCommand.channel, ageing: ageing, maxAgeing: this.MAX_COMMAND_QUEUE_TIMEOUT });
709
+ logging_1.rootP2PLogger.warn(`Command aged out from send queue for station ${this.rawStation.station_sn}`, {
710
+ commandType: message.p2pCommand.commandType,
711
+ nestedCommandType: message.nestedCommandType,
712
+ channel: message.p2pCommand.channel,
713
+ ageing: ageing,
714
+ maxAgeing: this.MAX_COMMAND_QUEUE_TIMEOUT,
715
+ });
656
716
  this.emit("command", {
657
717
  command_type: message.nestedCommandType !== undefined ? message.nestedCommandType : message.p2pCommand.commandType,
658
718
  channel: message.p2pCommand.channel,
659
719
  return_code: types_1.ErrorCode.ERROR_CONNECT_TIMEOUT,
660
- customData: message.customData
720
+ customData: message.customData,
661
721
  });
662
722
  return;
663
723
  }
@@ -674,23 +734,34 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
674
734
  message.retries++;
675
735
  }
676
736
  else {
677
- logging_1.rootP2PLogger.error(`Max p2p command send retries reached.`, { stationSN: this.rawStation.station_sn, sequence: message.sequence, commandType: message.commandType, channel: message.channel, retries: message.retries, returnCode: message.returnCode });
737
+ logging_1.rootP2PLogger.error(`Max p2p command send retries reached.`, {
738
+ stationSN: this.rawStation.station_sn,
739
+ sequence: message.sequence,
740
+ commandType: message.commandType,
741
+ channel: message.channel,
742
+ retries: message.retries,
743
+ returnCode: message.returnCode,
744
+ });
678
745
  this.emit("command", {
679
746
  command_type: message.nestedCommandType !== undefined ? message.nestedCommandType : message.commandType,
680
747
  channel: message.channel,
681
748
  return_code: types_1.ErrorCode.ERROR_COMMAND_TIMEOUT,
682
- customData: message.customData
749
+ customData: message.customData,
683
750
  });
684
751
  if (message.commandType === types_1.CommandType.CMD_START_REALTIME_MEDIA ||
685
- (message.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA && message.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
752
+ (message.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA &&
753
+ message.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
686
754
  message.commandType === types_1.CommandType.CMD_RECORD_VIEW ||
687
- (message.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM && message.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
688
- if (this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreaming && message.channel === this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreamChannel) {
755
+ (message.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM &&
756
+ message.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
757
+ if (this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreaming &&
758
+ message.channel === this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreamChannel) {
689
759
  this.endStream(types_1.P2PDataType.VIDEO);
690
760
  }
691
761
  }
692
762
  else if (message.commandType === types_1.CommandType.CMD_DOWNLOAD_VIDEO) {
693
- if (this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming && message.channel === this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel) {
763
+ if (this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming &&
764
+ message.channel === this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel) {
694
765
  this.endStream(types_1.P2PDataType.BINARY);
695
766
  }
696
767
  }
@@ -712,32 +783,52 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
712
783
  }, this.RESEND_NOT_ACKNOWLEDGED_COMMAND);
713
784
  if (messageState.commandType !== types_1.CommandType.CMD_PING) {
714
785
  this.p2pSeqMapping.set(this.p2pDataSeqNumber, message.sequence);
715
- logging_1.rootP2PLogger.trace(`Added sequence number mapping`, { stationSN: this.rawStation.station_sn, commandType: message.commandType, seqNumber: message.sequence, p2pDataSeqNumber: this.p2pDataSeqNumber, p2pSeqMappingCount: this.p2pSeqMapping.size });
786
+ logging_1.rootP2PLogger.trace(`Added sequence number mapping`, {
787
+ stationSN: this.rawStation.station_sn,
788
+ commandType: message.commandType,
789
+ seqNumber: message.sequence,
790
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
791
+ p2pSeqMappingCount: this.p2pSeqMapping.size,
792
+ });
716
793
  this.p2pDataSeqNumber = this._incrementSequence(this.p2pDataSeqNumber);
717
794
  }
718
- logging_1.rootP2PLogger.debug("Sending p2p command...", { station: this.rawStation.station_sn, sequence: messageState.sequence, commandType: messageState.commandType, channel: messageState.channel, retries: messageState.retries, messageStatesSize: this.messageStates.size });
795
+ logging_1.rootP2PLogger.debug("Sending p2p command...", {
796
+ station: this.rawStation.station_sn,
797
+ sequence: messageState.sequence,
798
+ commandType: messageState.commandType,
799
+ channel: messageState.channel,
800
+ retries: messageState.retries,
801
+ messageStatesSize: this.messageStates.size,
802
+ });
719
803
  await this.sendMessage(`Send p2p command`, this.connectAddress, types_1.RequestMessageType.DATA, messageState.data);
720
804
  if (messageState.retries === 0) {
721
805
  if (messageState.commandType === types_1.CommandType.CMD_START_REALTIME_MEDIA ||
722
- (messageState.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA && messageState.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
806
+ (messageState.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA &&
807
+ messageState.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
723
808
  messageState.commandType === types_1.CommandType.CMD_RECORD_VIEW ||
724
- (messageState.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM && messageState.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
725
- if (this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreaming && messageState.channel !== this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreamChannel) {
809
+ (messageState.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM &&
810
+ messageState.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
811
+ if (this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreaming &&
812
+ messageState.channel !== this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreamChannel) {
726
813
  this.endStream(types_1.P2PDataType.VIDEO);
727
814
  }
728
815
  this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreaming = true;
729
816
  this.currentMessageState[types_1.P2PDataType.VIDEO].p2pStreamChannel = messageState.channel;
730
817
  this.waitForStreamData(types_1.P2PDataType.VIDEO);
731
818
  }
732
- else if (messageState.commandType === types_1.CommandType.CMD_DOWNLOAD_VIDEO) {
733
- if (this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming && messageState.channel !== this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel) {
819
+ else if (messageState.commandType === types_1.CommandType.CMD_DOWNLOAD_VIDEO ||
820
+ (messageState.nestedCommandType === types_1.CommandType.CMD_DOWNLOAD_VIDEO &&
821
+ messageState.commandType === types_1.CommandType.CMD_SET_PAYLOAD)) {
822
+ if (this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming &&
823
+ messageState.channel !== this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel) {
734
824
  this.endStream(types_1.P2PDataType.BINARY);
735
825
  }
736
826
  this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming = true;
737
827
  this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel = message.channel;
738
828
  this.waitForStreamData(types_1.P2PDataType.BINARY);
739
829
  }
740
- else if (messageState.commandType === types_1.CommandType.CMD_STOP_REALTIME_MEDIA) { //TODO: CommandType.CMD_RECORD_PLAY_CTRL only if stop
830
+ else if (messageState.commandType === types_1.CommandType.CMD_STOP_REALTIME_MEDIA) {
831
+ //TODO: CommandType.CMD_RECORD_PLAY_CTRL only if stop
741
832
  this.endStream(types_1.P2PDataType.VIDEO);
742
833
  }
743
834
  else if (messageState.commandType === types_1.CommandType.CMD_DOWNLOAD_CANCEL) {
@@ -759,14 +850,35 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
759
850
  if (!this.connected) {
760
851
  this._clearLookupTimeout();
761
852
  this._clearLocalLookupRetryTimeout();
762
- const p2pDid = `${msg.subarray(4, 12).toString("utf8").replace(/[\0]+$/g, "")}-${msg.subarray(12, 16).readUInt32BE().toString().padStart(6, "0")}-${msg.subarray(16, 24).toString("utf8").replace(/[\0]+$/g, "")}`;
763
- logging_1.rootP2PLogger.trace(`Received message - LOCAL_LOOKUP_RESP - Got response`, { stationSN: this.rawStation.station_sn, ip: rinfo.address, port: rinfo.port, p2pDid: p2pDid });
853
+ const p2pDid = `${msg
854
+ .subarray(4, 12)
855
+ .toString("utf8")
856
+ .replace(/[\0]+$/g, "")}-${msg.subarray(12, 16).readUInt32BE().toString().padStart(6, "0")}-${msg
857
+ .subarray(16, 24)
858
+ .toString("utf8")
859
+ .replace(/[\0]+$/g, "")}`;
860
+ logging_1.rootP2PLogger.trace(`Received message - LOCAL_LOOKUP_RESP - Got response`, {
861
+ stationSN: this.rawStation.station_sn,
862
+ ip: rinfo.address,
863
+ port: rinfo.port,
864
+ p2pDid: p2pDid,
865
+ });
764
866
  if (p2pDid === this.rawStation.p2p_did) {
765
- logging_1.rootP2PLogger.debug(`Received message - LOCAL_LOOKUP_RESP - Wanted device was found, connect to it`, { stationSN: this.rawStation.station_sn, ip: rinfo.address, port: rinfo.port, p2pDid: p2pDid });
867
+ logging_1.rootP2PLogger.debug(`Received message - LOCAL_LOOKUP_RESP - Wanted device was found, connect to it`, {
868
+ stationSN: this.rawStation.station_sn,
869
+ ip: rinfo.address,
870
+ port: rinfo.port,
871
+ p2pDid: p2pDid,
872
+ });
766
873
  this._connect({ host: rinfo.address, port: rinfo.port }, p2pDid);
767
874
  }
768
875
  else {
769
- logging_1.rootP2PLogger.debug(`Received message - LOCAL_LOOKUP_RESP - Unwanted device was found, don't connect to it`, { stationSN: this.rawStation.station_sn, ip: rinfo.address, port: rinfo.port, p2pDid: p2pDid });
876
+ logging_1.rootP2PLogger.debug(`Received message - LOCAL_LOOKUP_RESP - Unwanted device was found, don't connect to it`, {
877
+ stationSN: this.rawStation.station_sn,
878
+ ip: rinfo.address,
879
+ port: rinfo.port,
880
+ p2pDid: p2pDid,
881
+ });
770
882
  }
771
883
  }
772
884
  }
@@ -774,9 +886,19 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
774
886
  if (!this.connected) {
775
887
  const port = msg.subarray(6, 8).readUInt16LE();
776
888
  const ip = `${msg[11]}.${msg[10]}.${msg[9]}.${msg[8]}`;
777
- logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR - Got response`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { ip: ip, port: port } });
889
+ logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR - Got response`, {
890
+ stationSN: this.rawStation.station_sn,
891
+ remoteAddress: rinfo.address,
892
+ remotePort: rinfo.port,
893
+ response: { ip: ip, port: port },
894
+ });
778
895
  if (ip === "0.0.0.0") {
779
- logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR - Got invalid ip address 0.0.0.0, ignoring response...`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { ip: ip, port: port } });
896
+ logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR - Got invalid ip address 0.0.0.0, ignoring response...`, {
897
+ stationSN: this.rawStation.station_sn,
898
+ remoteAddress: rinfo.address,
899
+ remotePort: rinfo.port,
900
+ response: { ip: ip, port: port },
901
+ });
780
902
  return;
781
903
  }
782
904
  if ((0, utils_1.isPrivateIp)(ip))
@@ -785,14 +907,22 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
785
907
  if ((0, utils_1.isPrivateIp)(ip)) {
786
908
  this._clearLookupTimeout();
787
909
  this._clearLookupRetryTimeout();
788
- logging_1.rootP2PLogger.debug(`Trying to connect in ONLY_LOCAL mode...`, { stationSN: this.rawStation.station_sn, ip: ip, port: port });
910
+ logging_1.rootP2PLogger.debug(`Trying to connect in ONLY_LOCAL mode...`, {
911
+ stationSN: this.rawStation.station_sn,
912
+ ip: ip,
913
+ port: port,
914
+ });
789
915
  this._connect({ host: ip, port: port }, this.rawStation.p2p_did);
790
916
  }
791
917
  }
792
918
  else if (this.connectionType === types_1.P2PConnectionType.QUICKEST) {
793
919
  this._clearLookupTimeout();
794
920
  this._clearLookupRetryTimeout();
795
- logging_1.rootP2PLogger.debug(`Trying to connect in QUICKEST mode...`, { stationSN: this.rawStation.station_sn, ip: ip, port: port });
921
+ logging_1.rootP2PLogger.debug(`Trying to connect in QUICKEST mode...`, {
922
+ stationSN: this.rawStation.station_sn,
923
+ ip: ip,
924
+ port: port,
925
+ });
796
926
  this._connect({ host: ip, port: port }, this.rawStation.p2p_did);
797
927
  }
798
928
  }
@@ -835,15 +965,21 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
835
965
  const tmpSendQueue = [...this.sendQueue];
836
966
  this.sendQueue = [];
837
967
  this.sendCommandPing(http_1.Station.CHANNEL);
838
- tmpSendQueue.forEach(element => {
968
+ tmpSendQueue.forEach((element) => {
839
969
  this.sendQueue.push(element);
840
970
  });
841
971
  }
842
- else if (device_1.Device.isLockWifiR10(this.rawStation.device_type) || device_1.Device.isLockWifiR20(this.rawStation.device_type)) {
972
+ else if (device_1.Device.isLockWifiR10(this.rawStation.device_type) ||
973
+ device_1.Device.isLockWifiR20(this.rawStation.device_type)) {
843
974
  const tmpSendQueue = [...this.sendQueue];
844
975
  this.sendQueue = [];
845
976
  const payload = (0, utils_1.buildVoidCommandPayload)(http_1.Station.CHANNEL);
846
- const data = Buffer.concat([(0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_GATEWAYINFO), payload.subarray(0, payload.length - 2), (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_PING).subarray(2), payload]);
977
+ const data = Buffer.concat([
978
+ (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_GATEWAYINFO),
979
+ payload.subarray(0, payload.length - 2),
980
+ (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_PING).subarray(2),
981
+ payload,
982
+ ]);
847
983
  const message = {
848
984
  sequence: this.seqNumber,
849
985
  commandType: types_1.CommandType.CMD_PING,
@@ -858,15 +994,24 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
858
994
  }, this.RESEND_NOT_ACKNOWLEDGED_COMMAND);
859
995
  this.seqNumber = this._incrementSequence(this.seqNumber);
860
996
  this.sendMessage(`Send lock wifi gateway v12 command to station`, this.connectAddress, types_1.RequestMessageType.DATA, data);
861
- tmpSendQueue.forEach(element => {
997
+ tmpSendQueue.forEach((element) => {
862
998
  this.sendQueue.push(element);
863
999
  });
864
1000
  }
865
- else if (this.rawStation.devices !== undefined && this.rawStation.devices !== null && this.rawStation.devices.length !== undefined && this.rawStation.devices.length > 0 && device_1.Device.isLockWifiVideo(this.rawStation.devices[0]?.device_type)) {
1001
+ else if (this.rawStation.devices !== undefined &&
1002
+ this.rawStation.devices !== null &&
1003
+ this.rawStation.devices.length !== undefined &&
1004
+ this.rawStation.devices.length > 0 &&
1005
+ device_1.Device.isLockWifiVideo(this.rawStation.devices[0]?.device_type)) {
866
1006
  const tmpSendQueue = [...this.sendQueue];
867
1007
  this.sendQueue = [];
868
1008
  const payload = (0, utils_1.buildVoidCommandPayload)(http_1.Station.CHANNEL);
869
- const data = Buffer.concat([(0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_GATEWAYINFO), payload.subarray(0, payload.length - 2), (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_PING).subarray(2), payload]);
1009
+ const data = Buffer.concat([
1010
+ (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_GATEWAYINFO),
1011
+ payload.subarray(0, payload.length - 2),
1012
+ (0, utils_1.buildCommandHeader)(0, types_1.CommandType.CMD_PING).subarray(2),
1013
+ payload,
1014
+ ]);
870
1015
  const message = {
871
1016
  sequence: this.seqNumber,
872
1017
  commandType: types_1.CommandType.CMD_PING,
@@ -881,7 +1026,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
881
1026
  }, this.RESEND_NOT_ACKNOWLEDGED_COMMAND);
882
1027
  this.seqNumber = this._incrementSequence(this.seqNumber);
883
1028
  this.sendMessage(`Send lock wifi gateway command to station`, this.connectAddress, types_1.RequestMessageType.DATA, data);
884
- tmpSendQueue.forEach(element => {
1029
+ tmpSendQueue.forEach((element) => {
885
1030
  this.sendQueue.push(element);
886
1031
  });
887
1032
  }
@@ -889,7 +1034,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
889
1034
  const tmpSendQueue = [...this.sendQueue];
890
1035
  this.sendQueue = [];
891
1036
  this.sendCommandWithoutData(types_1.CommandType.CMD_GATEWAYINFO, http_1.Station.CHANNEL);
892
- tmpSendQueue.forEach(element => {
1037
+ tmpSendQueue.forEach((element) => {
893
1038
  this.sendQueue.push(element);
894
1039
  });
895
1040
  }
@@ -916,7 +1061,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
916
1061
  }
917
1062
  else if ((0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.END)) {
918
1063
  // Connection is closed by device
919
- logging_1.rootP2PLogger.debug(`Received message - END`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port });
1064
+ logging_1.rootP2PLogger.debug(`Received message - END`, {
1065
+ stationSN: this.rawStation.station_sn,
1066
+ remoteAddress: rinfo.address,
1067
+ remotePort: rinfo.port,
1068
+ });
920
1069
  if (this.energySavingDevice && this.connected) {
921
1070
  this.closeEnergySavingDevice();
922
1071
  }
@@ -935,13 +1084,20 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
935
1084
  const seqBuffer = msg.subarray(idx, idx + 2);
936
1085
  const ackedSeqNo = seqBuffer.readUIntBE(0, seqBuffer.length);
937
1086
  // -> Message with seqNo was received at the station
938
- logging_1.rootP2PLogger.trace(`Received message - ACK ${types_1.P2PDataType[dataType]} - sequence ${ackedSeqNo}`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, ackedSeqNo: ackedSeqNo, dataType: types_1.P2PDataType[dataType] });
1087
+ logging_1.rootP2PLogger.trace(`Received message - ACK ${types_1.P2PDataType[dataType]} - sequence ${ackedSeqNo}`, {
1088
+ stationSN: this.rawStation.station_sn,
1089
+ remoteAddress: rinfo.address,
1090
+ remotePort: rinfo.port,
1091
+ ackedSeqNo: ackedSeqNo,
1092
+ dataType: types_1.P2PDataType[dataType],
1093
+ });
939
1094
  if (dataType === types_1.P2PDataType.DATA) {
940
1095
  const msg_state = this.messageStates.get(ackedSeqNo);
941
1096
  if (msg_state && !msg_state.acknowledged) {
942
1097
  this._clearTimeout(msg_state.retryTimeout);
943
1098
  this._clearTimeout(msg_state.timeout);
944
- if (msg_state.commandType === types_1.CommandType.CMD_PING || msg_state.commandType === types_1.CommandType.CMD_GET_DEVICE_PING) {
1099
+ if (msg_state.commandType === types_1.CommandType.CMD_PING ||
1100
+ msg_state.commandType === types_1.CommandType.CMD_GET_DEVICE_PING) {
945
1101
  this.messageStates.delete(ackedSeqNo);
946
1102
  this.sendQueuedMessage();
947
1103
  }
@@ -950,12 +1106,24 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
950
1106
  msg_state.timeout = setTimeout(() => {
951
1107
  //TODO: Retry command in these case?
952
1108
  if (msg_state.commandType !== types_1.CommandType.CMD_GATEWAYINFO) {
953
- logging_1.rootP2PLogger.warn(`Result data for command not received`, { stationSN: this.rawStation.station_sn, message: { sequence: msg_state.sequence, commandType: msg_state.commandType, nestedCommandType: msg_state.nestedCommandType, channel: msg_state.channel, acknowledged: msg_state.acknowledged, retries: msg_state.retries, returnCode: msg_state.returnCode, data: msg_state.data } });
1109
+ logging_1.rootP2PLogger.warn(`Result data for command not received`, {
1110
+ stationSN: this.rawStation.station_sn,
1111
+ message: {
1112
+ sequence: msg_state.sequence,
1113
+ commandType: msg_state.commandType,
1114
+ nestedCommandType: msg_state.nestedCommandType,
1115
+ channel: msg_state.channel,
1116
+ acknowledged: msg_state.acknowledged,
1117
+ retries: msg_state.retries,
1118
+ returnCode: msg_state.returnCode,
1119
+ data: msg_state.data,
1120
+ },
1121
+ });
954
1122
  this.emit("command", {
955
1123
  command_type: msg_state.nestedCommandType !== undefined ? msg_state.nestedCommandType : msg_state.commandType,
956
1124
  channel: msg_state.channel,
957
1125
  return_code: types_1.ErrorCode.ERROR_COMMAND_TIMEOUT,
958
- customData: msg_state.customData
1126
+ customData: msg_state.customData,
959
1127
  });
960
1128
  }
961
1129
  else {
@@ -965,11 +1133,25 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
965
1133
  }
966
1134
  });
967
1135
  this.p2pDataSeqNumber--;
968
- logging_1.rootP2PLogger.debug(`Result data for command CMD_GATEWAYINFO not received`, { stationSN: this.rawStation.station_sn, message: { sequence: msg_state.sequence, commandType: msg_state.commandType, nestedCommandType: msg_state.nestedCommandType, channel: msg_state.channel, acknowledged: msg_state.acknowledged, retries: msg_state.retries, returnCode: msg_state.returnCode, data: msg_state.data } });
1136
+ logging_1.rootP2PLogger.debug(`Result data for command CMD_GATEWAYINFO not received`, {
1137
+ stationSN: this.rawStation.station_sn,
1138
+ message: {
1139
+ sequence: msg_state.sequence,
1140
+ commandType: msg_state.commandType,
1141
+ nestedCommandType: msg_state.nestedCommandType,
1142
+ channel: msg_state.channel,
1143
+ acknowledged: msg_state.acknowledged,
1144
+ retries: msg_state.retries,
1145
+ returnCode: msg_state.returnCode,
1146
+ data: msg_state.data,
1147
+ },
1148
+ });
969
1149
  }
970
1150
  this.messageStates.delete(ackedSeqNo);
971
1151
  this.sendQueuedMessage();
972
- }, msg_state.commandType !== types_1.CommandType.CMD_GATEWAYINFO ? this.MAX_COMMAND_RESULT_WAIT : this.MAX_GATEWAY_COMMAND_RESULT_WAIT);
1152
+ }, msg_state.commandType !== types_1.CommandType.CMD_GATEWAYINFO
1153
+ ? this.MAX_COMMAND_RESULT_WAIT
1154
+ : this.MAX_GATEWAY_COMMAND_RESULT_WAIT);
973
1155
  this.messageStates.set(ackedSeqNo, msg_state);
974
1156
  this.sendQueuedMessage();
975
1157
  }
@@ -993,10 +1175,16 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
993
1175
  bytesToRead: msg.subarray(2, 4).readUInt16BE(),
994
1176
  type: dataType,
995
1177
  seqNo: seqNo,
996
- data: msg.subarray(8)
1178
+ data: msg.subarray(8),
997
1179
  };
998
1180
  this.sendAck({ host: rinfo.address, port: rinfo.port }, dataTypeBuffer, seqNo);
999
- logging_1.rootP2PLogger.debug(`Received message - DATA ${types_1.P2PDataType[message.type]} - Processing sequence ${message.seqNo}...`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: message.seqNo });
1181
+ logging_1.rootP2PLogger.debug(`Received message - DATA ${types_1.P2PDataType[message.type]} - Processing sequence ${message.seqNo}...`, {
1182
+ stationSN: this.rawStation.station_sn,
1183
+ remoteAddress: rinfo.address,
1184
+ remotePort: rinfo.port,
1185
+ dataType: types_1.P2PDataType[message.type],
1186
+ seqNo: message.seqNo,
1187
+ });
1000
1188
  if (message.seqNo === this.expectedSeqNo[dataType]) {
1001
1189
  // expected seq packet arrived
1002
1190
  const timeout = this.currentMessageState[dataType].waitForSeqNoTimeout;
@@ -1006,10 +1194,26 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1006
1194
  }
1007
1195
  this.expectedSeqNo[dataType] = this._incrementSequence(this.expectedSeqNo[dataType]);
1008
1196
  this.parseDataMessage(message);
1009
- logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received expected sequence`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: message.seqNo, expectedSeqNo: this.expectedSeqNo[dataType], queuedDataSize: this.currentMessageState[dataType].queuedData.size });
1197
+ logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received expected sequence`, {
1198
+ stationSN: this.rawStation.station_sn,
1199
+ remoteAddress: rinfo.address,
1200
+ remotePort: rinfo.port,
1201
+ dataType: types_1.P2PDataType[message.type],
1202
+ seqNo: message.seqNo,
1203
+ expectedSeqNo: this.expectedSeqNo[dataType],
1204
+ queuedDataSize: this.currentMessageState[dataType].queuedData.size,
1205
+ });
1010
1206
  let queuedMessage = this.currentMessageState[dataType].queuedData.get(this.expectedSeqNo[dataType]);
1011
1207
  while (queuedMessage) {
1012
- logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[queuedMessage.type]} - Work off queued data`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: queuedMessage.seqNo, expectedSeqNo: this.expectedSeqNo[dataType], queuedDataSize: this.currentMessageState[dataType].queuedData.size });
1208
+ logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[queuedMessage.type]} - Work off queued data`, {
1209
+ stationSN: this.rawStation.station_sn,
1210
+ remoteAddress: rinfo.address,
1211
+ remotePort: rinfo.port,
1212
+ dataType: types_1.P2PDataType[message.type],
1213
+ seqNo: queuedMessage.seqNo,
1214
+ expectedSeqNo: this.expectedSeqNo[dataType],
1215
+ queuedDataSize: this.currentMessageState[dataType].queuedData.size,
1216
+ });
1013
1217
  this.expectedSeqNo[dataType] = this._incrementSequence(this.expectedSeqNo[dataType]);
1014
1218
  this.parseDataMessage(queuedMessage);
1015
1219
  this.currentMessageState[dataType].queuedData.delete(queuedMessage.seqNo);
@@ -1020,7 +1224,15 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1020
1224
  // We have already seen this message, skip!
1021
1225
  // This can happen because the device is sending the message till it gets a ACK
1022
1226
  // which can take some time.
1023
- logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received already processed sequence`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: message.seqNo, expectedSeqNo: this.expectedSeqNo[dataType], queuedDataSize: this.currentMessageState[dataType].queuedData.size });
1227
+ logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received already processed sequence`, {
1228
+ stationSN: this.rawStation.station_sn,
1229
+ remoteAddress: rinfo.address,
1230
+ remotePort: rinfo.port,
1231
+ dataType: types_1.P2PDataType[message.type],
1232
+ seqNo: message.seqNo,
1233
+ expectedSeqNo: this.expectedSeqNo[dataType],
1234
+ queuedDataSize: this.currentMessageState[dataType].queuedData.size,
1235
+ });
1024
1236
  return;
1025
1237
  }
1026
1238
  else {
@@ -1031,10 +1243,26 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1031
1243
  }, this.MAX_EXPECTED_SEQNO_WAIT);
1032
1244
  if (!this.currentMessageState[dataType].queuedData.get(message.seqNo)) {
1033
1245
  this.currentMessageState[dataType].queuedData.set(message.seqNo, message);
1034
- logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received not expected sequence, added to the queue for future processing`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: message.seqNo, expectedSeqNo: this.expectedSeqNo[dataType], queuedDataSize: this.currentMessageState[dataType].queuedData.size });
1246
+ logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received not expected sequence, added to the queue for future processing`, {
1247
+ stationSN: this.rawStation.station_sn,
1248
+ remoteAddress: rinfo.address,
1249
+ remotePort: rinfo.port,
1250
+ dataType: types_1.P2PDataType[message.type],
1251
+ seqNo: message.seqNo,
1252
+ expectedSeqNo: this.expectedSeqNo[dataType],
1253
+ queuedDataSize: this.currentMessageState[dataType].queuedData.size,
1254
+ });
1035
1255
  }
1036
1256
  else {
1037
- logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received not expected sequence, discarded since already present in queue for future processing`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, dataType: types_1.P2PDataType[message.type], seqNo: message.seqNo, expectedSeqNo: this.expectedSeqNo[dataType], queuedDataSize: this.currentMessageState[dataType].queuedData.size });
1257
+ logging_1.rootP2PLogger.trace(`Received message - DATA ${types_1.P2PDataType[message.type]} - Received not expected sequence, discarded since already present in queue for future processing`, {
1258
+ stationSN: this.rawStation.station_sn,
1259
+ remoteAddress: rinfo.address,
1260
+ remotePort: rinfo.port,
1261
+ dataType: types_1.P2PDataType[message.type],
1262
+ seqNo: message.seqNo,
1263
+ expectedSeqNo: this.expectedSeqNo[dataType],
1264
+ queuedDataSize: this.currentMessageState[dataType].queuedData.size,
1265
+ });
1038
1266
  }
1039
1267
  }
1040
1268
  }
@@ -1047,8 +1275,18 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1047
1275
  this._clearLookupTimeout();
1048
1276
  this._clearLookupRetryTimeout();
1049
1277
  this._clearLookup2RetryTimeout();
1050
- logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR2 - Got response`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { ip: ip, port: port, data: data.toString("hex") } });
1051
- logging_1.rootP2PLogger.debug(`Connecting to host ${ip} on port ${port} (CHECK_CAM2)...`, { stationSN: this.rawStation.station_sn, ip: ip, port: port, data: data.toString("hex") });
1278
+ logging_1.rootP2PLogger.trace(`Received message - LOOKUP_ADDR2 - Got response`, {
1279
+ stationSN: this.rawStation.station_sn,
1280
+ remoteAddress: rinfo.address,
1281
+ remotePort: rinfo.port,
1282
+ response: { ip: ip, port: port, data: data.toString("hex") },
1283
+ });
1284
+ logging_1.rootP2PLogger.debug(`Connecting to host ${ip} on port ${port} (CHECK_CAM2)...`, {
1285
+ stationSN: this.rawStation.station_sn,
1286
+ ip: ip,
1287
+ port: port,
1288
+ data: data.toString("hex"),
1289
+ });
1052
1290
  for (let i = 0; i < 4; i++)
1053
1291
  this.sendCamCheck2({ host: ip, port: port }, data);
1054
1292
  this._startConnectTimeout();
@@ -1060,7 +1298,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1060
1298
  }
1061
1299
  else if ((0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.TURN_SERVER_OK)) {
1062
1300
  if (!this.connected && !this.p2pTurnConfirmed) {
1063
- logging_1.rootP2PLogger.trace(`Received message - TURN_SERVER_OK - Got response`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { message: msg.toString("hex"), length: msg.length } });
1301
+ logging_1.rootP2PLogger.trace(`Received message - TURN_SERVER_OK - Got response`, {
1302
+ stationSN: this.rawStation.station_sn,
1303
+ remoteAddress: rinfo.address,
1304
+ remotePort: rinfo.port,
1305
+ response: { message: msg.toString("hex"), length: msg.length },
1306
+ });
1064
1307
  this.sendMessage(`Send TURN_CLIENT_OK`, { host: rinfo.address, port: rinfo.port }, types_1.RequestMessageType.TURN_CLIENT_OK);
1065
1308
  this.p2pTurnConfirmed = true;
1066
1309
  }
@@ -1070,29 +1313,59 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1070
1313
  const ip = `${msg[7]}.${msg[6]}.${msg[5]}.${msg[4]}`;
1071
1314
  const port = msg.subarray(8, 10).readUInt16BE();
1072
1315
  const binaryIP = msg.subarray(4, 8);
1073
- logging_1.rootP2PLogger.debug(`Connecting to host ${ip} on port ${port} (CHECK_CAM2)...`, { stationSN: this.rawStation.station_sn, ip: ip, port: port, binaryIP: binaryIP.toString("hex") });
1316
+ logging_1.rootP2PLogger.debug(`Connecting to host ${ip} on port ${port} (CHECK_CAM2)...`, {
1317
+ stationSN: this.rawStation.station_sn,
1318
+ ip: ip,
1319
+ port: port,
1320
+ binaryIP: binaryIP.toString("hex"),
1321
+ });
1074
1322
  for (let i = 0; i < 4; i++)
1075
1323
  this.sendCamCheck2({ host: rinfo.address, port: port }, binaryIP);
1076
- logging_1.rootP2PLogger.trace(`Received message - TURN_SERVER_TOKEN - Got response`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { port: port, binaryIP: binaryIP.toString("hex") } });
1324
+ logging_1.rootP2PLogger.trace(`Received message - TURN_SERVER_TOKEN - Got response`, {
1325
+ stationSN: this.rawStation.station_sn,
1326
+ remoteAddress: rinfo.address,
1327
+ remotePort: rinfo.port,
1328
+ response: { port: port, binaryIP: binaryIP.toString("hex") },
1329
+ });
1077
1330
  this.cloudLookupWithTurnServer({ host: rinfo.address, port: port }, binaryIP);
1078
1331
  }
1079
1332
  }
1080
- else if ((0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.TURN_SERVER_LOOKUP_OK) || (0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.UNKNOWN_83) || (0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.TURN_SERVER_LIST)) {
1333
+ else if ((0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.TURN_SERVER_LOOKUP_OK) ||
1334
+ (0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.UNKNOWN_83) ||
1335
+ (0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.TURN_SERVER_LIST)) {
1081
1336
  // Do nothing / ignore
1082
1337
  }
1083
1338
  else if ((0, utils_1.hasHeader)(msg, types_1.ResponseMessageType.LOOKUP_RESP)) {
1084
1339
  if (!this.connected) {
1085
1340
  const responseCode = msg.subarray(4, 6).readUInt16LE();
1086
- logging_1.rootP2PLogger.trace(`Received message - LOOKUP_RESP - Got response`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { responseCode: responseCode } });
1341
+ logging_1.rootP2PLogger.trace(`Received message - LOOKUP_RESP - Got response`, {
1342
+ stationSN: this.rawStation.station_sn,
1343
+ remoteAddress: rinfo.address,
1344
+ remotePort: rinfo.port,
1345
+ response: { responseCode: responseCode },
1346
+ });
1087
1347
  }
1088
1348
  }
1089
1349
  else {
1090
- logging_1.rootP2PLogger.debug(`Received unknown message`, { stationSN: this.rawStation.station_sn, remoteAddress: rinfo.address, remotePort: rinfo.port, response: { message: msg.toString("hex"), length: msg.length } });
1350
+ logging_1.rootP2PLogger.debug(`Received unknown message`, {
1351
+ stationSN: this.rawStation.station_sn,
1352
+ remoteAddress: rinfo.address,
1353
+ remotePort: rinfo.port,
1354
+ response: { message: msg.toString("hex"), length: msg.length },
1355
+ });
1091
1356
  }
1092
1357
  }
1093
1358
  parseDataMessage(message) {
1094
- if ((message.type === types_1.P2PDataType.BINARY || message.type === types_1.P2PDataType.VIDEO) && !this.currentMessageState[message.type].p2pStreaming) {
1095
- logging_1.rootP2PLogger.trace(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Stream not started ignore this data`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, header: this.currentMessageBuilder[message.type].header, bytesRead: this.currentMessageBuilder[message.type].bytesRead, bytesToRead: message.bytesToRead, messageSize: message.data.length });
1359
+ if ((message.type === types_1.P2PDataType.BINARY || message.type === types_1.P2PDataType.VIDEO) &&
1360
+ !this.currentMessageState[message.type].p2pStreaming) {
1361
+ logging_1.rootP2PLogger.trace(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Stream not started ignore this data`, {
1362
+ stationSN: this.rawStation.station_sn,
1363
+ seqNo: message.seqNo,
1364
+ header: this.currentMessageBuilder[message.type].header,
1365
+ bytesRead: this.currentMessageBuilder[message.type].bytesRead,
1366
+ bytesToRead: message.bytesToRead,
1367
+ messageSize: message.data.length,
1368
+ });
1096
1369
  }
1097
1370
  else {
1098
1371
  if (this.currentMessageState[message.type].leftoverData.length > 0) {
@@ -1110,7 +1383,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1110
1383
  bytesToRead: 0,
1111
1384
  channel: 0,
1112
1385
  signCode: 0,
1113
- type: 0
1386
+ type: 0,
1114
1387
  };
1115
1388
  header.commandId = data.subarray(4, 6).readUIntLE(0, 2);
1116
1389
  header.bytesToRead = data.subarray(6, 10).readUIntLE(0, 4);
@@ -1142,12 +1415,23 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1142
1415
  }
1143
1416
  else {
1144
1417
  // finish message and print
1145
- if (this.currentMessageBuilder[message.type].header.bytesToRead - this.currentMessageBuilder[message.type].bytesRead === 0 && data.length > this.P2P_DATA_HEADER_BYTES) {
1146
- logging_1.rootP2PLogger.debug(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Discarding unexpected data (infinite loop prevention)`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, dataSize: data.length, data: data.toString("hex") });
1418
+ if (this.currentMessageBuilder[message.type].header.bytesToRead -
1419
+ this.currentMessageBuilder[message.type].bytesRead ===
1420
+ 0 &&
1421
+ data.length > this.P2P_DATA_HEADER_BYTES) {
1422
+ logging_1.rootP2PLogger.debug(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Discarding unexpected data (infinite loop prevention)`, {
1423
+ stationSN: this.rawStation.station_sn,
1424
+ seqNo: message.seqNo,
1425
+ dataSize: data.length,
1426
+ data: data.toString("hex"),
1427
+ });
1147
1428
  data = Buffer.from([]);
1148
1429
  }
1149
- else if (this.currentMessageBuilder[message.type].header.bytesToRead - this.currentMessageBuilder[message.type].bytesRead <= data.length) {
1150
- const payload = data.subarray(0, this.currentMessageBuilder[message.type].header.bytesToRead - this.currentMessageBuilder[message.type].bytesRead);
1430
+ else if (this.currentMessageBuilder[message.type].header.bytesToRead -
1431
+ this.currentMessageBuilder[message.type].bytesRead <=
1432
+ data.length) {
1433
+ const payload = data.subarray(0, this.currentMessageBuilder[message.type].header.bytesToRead -
1434
+ this.currentMessageBuilder[message.type].bytesRead);
1151
1435
  this.currentMessageBuilder[message.type].messages[message.seqNo] = payload;
1152
1436
  this.currentMessageBuilder[message.type].bytesRead += payload.byteLength;
1153
1437
  data = data.subarray(payload.byteLength);
@@ -1167,26 +1451,54 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1167
1451
  data = Buffer.from([]);
1168
1452
  }
1169
1453
  }
1170
- logging_1.rootP2PLogger.trace(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Received data`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, header: this.currentMessageBuilder[message.type].header, bytesRead: this.currentMessageBuilder[message.type].bytesRead, bytesToRead: this.currentMessageBuilder[message.type].header.bytesToRead, firstPartMessage: firstPartMessage, messageSize: message.data.length, runaway_limit: runaway_limit });
1171
- if (this.currentMessageBuilder[message.type].bytesRead === this.currentMessageBuilder[message.type].header.bytesToRead) {
1454
+ logging_1.rootP2PLogger.trace(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Received data`, {
1455
+ stationSN: this.rawStation.station_sn,
1456
+ seqNo: message.seqNo,
1457
+ header: this.currentMessageBuilder[message.type].header,
1458
+ bytesRead: this.currentMessageBuilder[message.type].bytesRead,
1459
+ bytesToRead: this.currentMessageBuilder[message.type].header.bytesToRead,
1460
+ firstPartMessage: firstPartMessage,
1461
+ messageSize: message.data.length,
1462
+ runaway_limit: runaway_limit,
1463
+ });
1464
+ if (this.currentMessageBuilder[message.type].bytesRead ===
1465
+ this.currentMessageBuilder[message.type].header.bytesToRead) {
1172
1466
  const completeMessage = (0, utils_1.sortP2PMessageParts)(this.currentMessageBuilder[message.type].messages);
1173
1467
  const data_message = {
1174
1468
  ...this.currentMessageBuilder[message.type].header,
1175
- seqNo: (message.seqNo + this.offsetDataSeqNumber),
1469
+ seqNo: message.seqNo + this.offsetDataSeqNumber,
1176
1470
  dataType: message.type,
1177
- data: completeMessage
1471
+ data: completeMessage,
1178
1472
  };
1179
1473
  this.handleData(data_message);
1180
1474
  this.initializeMessageBuilder(message.type);
1181
1475
  if (data.length > 0 && message.type === types_1.P2PDataType.DATA) {
1182
- logging_1.rootP2PLogger.debug(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Parsed data`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, data_message: data_message, datalen: data.length, data: data.toString("hex"), offsetDataSeqNumber: this.offsetDataSeqNumber, seqNumber: this.seqNumber, p2pDataSeqNumber: this.p2pDataSeqNumber });
1476
+ logging_1.rootP2PLogger.debug(`Parsing message - DATA ${types_1.P2PDataType[message.type]} - Parsed data`, {
1477
+ stationSN: this.rawStation.station_sn,
1478
+ seqNo: message.seqNo,
1479
+ data_message: data_message,
1480
+ datalen: data.length,
1481
+ data: data.toString("hex"),
1482
+ offsetDataSeqNumber: this.offsetDataSeqNumber,
1483
+ seqNumber: this.seqNumber,
1484
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
1485
+ });
1183
1486
  this.offsetDataSeqNumber++;
1184
1487
  }
1185
1488
  }
1186
1489
  runaway_limit++;
1187
- } while ((data.length > 0) && (runaway_limit < this.LOOP_RUNAWAY_LIMIT));
1490
+ } while (data.length > 0 && runaway_limit < this.LOOP_RUNAWAY_LIMIT);
1188
1491
  if (runaway_limit >= this.LOOP_RUNAWAY_LIMIT) {
1189
- logging_1.rootP2PLogger.warn(`Infinite loop detected (limit >= ${this.LOOP_RUNAWAY_LIMIT}) during parsing of p2p message`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, dataType: types_1.P2PDataType[message.type], header: this.currentMessageBuilder[message.type].header, bytesRead: this.currentMessageBuilder[message.type].bytesRead, bytesToRead: this.currentMessageBuilder[message.type].header.bytesToRead, message: message.data.toString("hex"), messageSize: message.data.length });
1492
+ logging_1.rootP2PLogger.warn(`Infinite loop detected (limit >= ${this.LOOP_RUNAWAY_LIMIT}) during parsing of p2p message`, {
1493
+ stationSN: this.rawStation.station_sn,
1494
+ seqNo: message.seqNo,
1495
+ dataType: types_1.P2PDataType[message.type],
1496
+ header: this.currentMessageBuilder[message.type].header,
1497
+ bytesRead: this.currentMessageBuilder[message.type].bytesRead,
1498
+ bytesToRead: this.currentMessageBuilder[message.type].header.bytesToRead,
1499
+ message: message.data.toString("hex"),
1500
+ messageSize: message.data.length,
1501
+ });
1190
1502
  this.initializeMessageBuilder(message.type);
1191
1503
  }
1192
1504
  }
@@ -1202,33 +1514,91 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1202
1514
  let return_code = 0;
1203
1515
  let resultData;
1204
1516
  if (message.bytesToRead > 0) {
1205
- if (message.signCode > 0) {
1206
- try {
1207
- message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1517
+ if (message.signCode > 0 && message.data.length > 0) {
1518
+ if (message.data.length % 16 === 0) {
1519
+ try {
1520
+ message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1521
+ }
1522
+ catch (err) {
1523
+ const error = (0, error_1.ensureError)(err);
1524
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
1525
+ error: (0, utils_3.getError)(error),
1526
+ stationSN: this.rawStation.station_sn,
1527
+ message: {
1528
+ seqNo: message.seqNo,
1529
+ channel: message.channel,
1530
+ commandType: types_1.CommandType[message.commandId],
1531
+ signCode: message.signCode,
1532
+ type: message.type,
1533
+ dataType: types_1.P2PDataType[message.dataType],
1534
+ data: message.data.toString("hex"),
1535
+ },
1536
+ });
1537
+ }
1208
1538
  }
1209
- catch (err) {
1210
- const error = (0, error_1.ensureError)(err);
1211
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
1539
+ else {
1540
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Skipping decryption, data not block-aligned`, {
1541
+ stationSN: this.rawStation.station_sn,
1542
+ seqNo: message.seqNo,
1543
+ commandType: types_1.CommandType[message.commandId],
1544
+ signCode: message.signCode,
1545
+ dataLength: message.data.length,
1546
+ mod16: message.data.length % 16,
1547
+ });
1212
1548
  }
1213
1549
  }
1214
1550
  return_code = message.data.subarray(0, 4).readUInt32LE() | 0;
1215
1551
  resultData = message.data.subarray(4);
1216
1552
  }
1217
1553
  const error_codeStr = types_1.ErrorCode[return_code];
1218
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received data`, { stationSN: this.rawStation.station_sn, seqNo: message.seqNo, commandIdName: commandStr, commandId: message.commandId, resultCodeName: error_codeStr, resultCode: return_code, resultData: resultData?.toString("hex"), data: message.data.toString("hex"), seqNumber: this.seqNumber, p2pDataSeqNumber: this.p2pDataSeqNumber, offsetDataSeqNumber: this.offsetDataSeqNumber });
1554
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received data`, {
1555
+ stationSN: this.rawStation.station_sn,
1556
+ seqNo: message.seqNo,
1557
+ commandIdName: commandStr,
1558
+ commandId: message.commandId,
1559
+ resultCodeName: error_codeStr,
1560
+ resultCode: return_code,
1561
+ resultData: resultData?.toString("hex"),
1562
+ data: message.data.toString("hex"),
1563
+ seqNumber: this.seqNumber,
1564
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
1565
+ offsetDataSeqNumber: this.offsetDataSeqNumber,
1566
+ });
1219
1567
  let msg_state = this.messageStates.get(message.seqNo);
1220
1568
  const goodSeqNumber = this.p2pSeqMapping.get(message.seqNo);
1221
1569
  if (goodSeqNumber) {
1222
1570
  this.p2pSeqMapping.delete(message.seqNo);
1223
1571
  msg_state = this.messageStates.get(goodSeqNumber);
1224
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data received - Detecting correct sequence number`, { stationSN: this.rawStation.station_sn, commandIdName: commandStr, commandId: message.commandId, seqNumber: message.seqNo, newSeqNumber: goodSeqNumber, p2pSeqMappingCount: this.p2pSeqMapping.size });
1572
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data received - Detecting correct sequence number`, {
1573
+ stationSN: this.rawStation.station_sn,
1574
+ commandIdName: commandStr,
1575
+ commandId: message.commandId,
1576
+ seqNumber: message.seqNo,
1577
+ newSeqNumber: goodSeqNumber,
1578
+ p2pSeqMappingCount: this.p2pSeqMapping.size,
1579
+ });
1225
1580
  message.seqNo = goodSeqNumber;
1226
1581
  }
1227
1582
  if (msg_state) {
1228
1583
  if (msg_state.commandType === message.commandId) {
1229
1584
  this._clearTimeout(msg_state.timeout);
1230
1585
  const command_type = msg_state.nestedCommandType !== undefined ? msg_state.nestedCommandType : msg_state.commandType;
1231
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data for command received`, { stationSN: this.rawStation.station_sn, message: { sequence: msg_state.sequence, commandType: msg_state.commandType, nestedCommandType: msg_state.nestedCommandType, channel: msg_state.channel, acknowledged: msg_state.acknowledged, retries: msg_state.retries, returnCode: return_code, data: msg_state.data, customData: msg_state.customData }, resultCodeName: error_codeStr, resultCode: return_code });
1586
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data for command received`, {
1587
+ stationSN: this.rawStation.station_sn,
1588
+ message: {
1589
+ sequence: msg_state.sequence,
1590
+ commandType: msg_state.commandType,
1591
+ nestedCommandType: msg_state.nestedCommandType,
1592
+ channel: msg_state.channel,
1593
+ acknowledged: msg_state.acknowledged,
1594
+ retries: msg_state.retries,
1595
+ returnCode: return_code,
1596
+ data: msg_state.data,
1597
+ customData: msg_state.customData,
1598
+ },
1599
+ resultCodeName: error_codeStr,
1600
+ resultCode: return_code,
1601
+ });
1232
1602
  if (return_code === types_1.ErrorCode.ERROR_FAILED_TO_REQUEST) {
1233
1603
  msg_state.returnCode = return_code;
1234
1604
  this._sendCommand(msg_state);
@@ -1239,11 +1609,13 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1239
1609
  command_type: command_type,
1240
1610
  channel: msg_state.channel,
1241
1611
  return_code: return_code,
1242
- customData: msg_state.customData
1612
+ customData: msg_state.customData,
1243
1613
  });
1244
1614
  }
1245
1615
  this.messageStates.delete(message.seqNo);
1246
- if (command_type === types_1.CommandType.CMD_SMARTSAFE_SETTINGS || command_type === types_1.CommandType.CMD_SET_PAYLOAD_LOCKV12 || command_type === types_1.CommandType.CMD_TRANSFER_PAYLOAD) {
1616
+ if (command_type === types_1.CommandType.CMD_SMARTSAFE_SETTINGS ||
1617
+ command_type === types_1.CommandType.CMD_SET_PAYLOAD_LOCKV12 ||
1618
+ command_type === types_1.CommandType.CMD_TRANSFER_PAYLOAD) {
1247
1619
  if (msg_state.customData) {
1248
1620
  for (const index of Object.keys(this.customDataStaging)) {
1249
1621
  const id = Number.parseInt(index);
@@ -1251,20 +1623,39 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1251
1623
  delete this.customDataStaging[id];
1252
1624
  }
1253
1625
  }
1254
- this.customDataStaging[msg_state.nestedCommandType2 ? msg_state.nestedCommandType2 : msg_state.nestedCommandType ? msg_state.nestedCommandType : msg_state.commandType] = {
1626
+ this.customDataStaging[msg_state.nestedCommandType2
1627
+ ? msg_state.nestedCommandType2
1628
+ : msg_state.nestedCommandType
1629
+ ? msg_state.nestedCommandType
1630
+ : msg_state.commandType] = {
1255
1631
  channel: msg_state.channel,
1256
1632
  customData: msg_state.customData,
1257
1633
  timestamp: new Date().getTime(),
1258
1634
  };
1259
1635
  }
1260
1636
  this.secondaryCommandTimeout = setTimeout(() => {
1261
- logging_1.rootP2PLogger.warn(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data for secondary command not received`, { stationSN: this.rawStation.station_sn, message: { sequence: msg_state.sequence, commandType: msg_state.commandType, nestedCommandType: msg_state.nestedCommandType, channel: msg_state.channel, acknowledged: msg_state.acknowledged, retries: msg_state.retries, returnCode: msg_state.returnCode, data: msg_state.data, customData: msg_state.customData } });
1637
+ logging_1.rootP2PLogger.warn(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Result data for secondary command not received`, {
1638
+ stationSN: this.rawStation.station_sn,
1639
+ message: {
1640
+ sequence: msg_state.sequence,
1641
+ commandType: msg_state.commandType,
1642
+ nestedCommandType: msg_state.nestedCommandType,
1643
+ channel: msg_state.channel,
1644
+ acknowledged: msg_state.acknowledged,
1645
+ retries: msg_state.retries,
1646
+ returnCode: msg_state.returnCode,
1647
+ data: msg_state.data,
1648
+ customData: msg_state.customData,
1649
+ },
1650
+ });
1262
1651
  this.secondaryCommandTimeout = undefined;
1263
1652
  this.emit("secondary command", {
1264
- command_type: msg_state.nestedCommandType !== undefined ? msg_state.nestedCommandType : msg_state.commandType,
1653
+ command_type: msg_state.nestedCommandType !== undefined
1654
+ ? msg_state.nestedCommandType
1655
+ : msg_state.commandType,
1265
1656
  channel: msg_state.channel,
1266
1657
  return_code: types_1.ErrorCode.ERROR_COMMAND_TIMEOUT,
1267
- customData: msg_state.customData
1658
+ customData: msg_state.customData,
1268
1659
  });
1269
1660
  this._clearESDDisconnectTimeout();
1270
1661
  this.sendQueuedMessage();
@@ -1275,29 +1666,41 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1275
1666
  this.sendQueuedMessage();
1276
1667
  }
1277
1668
  if (msg_state.commandType === types_1.CommandType.CMD_START_REALTIME_MEDIA ||
1278
- (msg_state.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA && msg_state.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
1669
+ (msg_state.nestedCommandType === types_1.CommandType.CMD_START_REALTIME_MEDIA &&
1670
+ msg_state.commandType === types_1.CommandType.CMD_SET_PAYLOAD) ||
1279
1671
  msg_state.commandType === types_1.CommandType.CMD_RECORD_VIEW ||
1280
- (msg_state.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM && msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
1672
+ (msg_state.nestedCommandType === http_1.ParamType.COMMAND_START_LIVESTREAM &&
1673
+ msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD)) {
1281
1674
  this.waitForStreamData(types_1.P2PDataType.VIDEO, true);
1282
1675
  }
1283
1676
  else if (msg_state.commandType === types_1.CommandType.CMD_DOWNLOAD_VIDEO) {
1284
1677
  this.waitForStreamData(types_1.P2PDataType.BINARY, true);
1285
1678
  }
1286
- else if (msg_state.commandType === types_1.CommandType.CMD_START_TALKBACK || (msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD && msg_state.nestedCommandType === types_1.IndoorSoloSmartdropCommandType.CMD_START_SPEAK)) {
1679
+ else if (msg_state.commandType === types_1.CommandType.CMD_START_TALKBACK ||
1680
+ (msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD &&
1681
+ msg_state.nestedCommandType === types_1.IndoorSoloSmartdropCommandType.CMD_START_SPEAK)) {
1287
1682
  if (return_code === types_1.ErrorCode.ERROR_PPCS_SUCCESSFUL) {
1288
1683
  this.startTalkback(msg_state.channel);
1289
1684
  }
1290
1685
  else if (return_code === types_1.ErrorCode.ERROR_NOT_FIND_DEV) {
1291
- this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Someone is responding now.", { context: { station: this.rawStation.station_sn, channel: msg_state.channel } }));
1686
+ this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Someone is responding now.", {
1687
+ context: { station: this.rawStation.station_sn, channel: msg_state.channel },
1688
+ }));
1292
1689
  }
1293
1690
  else if (return_code === types_1.ErrorCode.ERROR_DEV_BUSY) {
1294
- this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Wait a second, device is busy.", { context: { station: this.rawStation.station_sn, channel: msg_state.channel } }));
1691
+ this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Wait a second, device is busy.", {
1692
+ context: { station: this.rawStation.station_sn, channel: msg_state.channel },
1693
+ }));
1295
1694
  }
1296
1695
  else {
1297
- this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Connect failed please try again later.", { context: { station: this.rawStation.station_sn, channel: msg_state.channel } }));
1696
+ this.emit("talkback error", msg_state.channel, new error_1.TalkbackError("Connect failed please try again later.", {
1697
+ context: { station: this.rawStation.station_sn, channel: msg_state.channel },
1698
+ }));
1298
1699
  }
1299
1700
  }
1300
- else if (msg_state.commandType === types_1.CommandType.CMD_STOP_TALKBACK || (msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD && msg_state.nestedCommandType === types_1.IndoorSoloSmartdropCommandType.CMD_END_SPEAK)) {
1701
+ else if (msg_state.commandType === types_1.CommandType.CMD_STOP_TALKBACK ||
1702
+ (msg_state.commandType === types_1.CommandType.CMD_DOORBELL_SET_PAYLOAD &&
1703
+ msg_state.nestedCommandType === types_1.IndoorSoloSmartdropCommandType.CMD_END_SPEAK)) {
1301
1704
  this.stopTalkback(msg_state.channel);
1302
1705
  }
1303
1706
  else if (msg_state.commandType === types_1.CommandType.CMD_SDINFO_EX && resultData && resultData.length >= 8) {
@@ -1311,31 +1714,90 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1311
1714
  }
1312
1715
  else {
1313
1716
  this.messageStates.delete(message.seqNo);
1314
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Expected different command type for received sequencenumber!`, { stationSN: this.rawStation.station_sn, msg_sequence: msg_state.sequence, msg_channel: msg_state.channel, msg_commandType: msg_state.commandType, seqNumber: this.seqNumber, p2pDataSeqNumber: this.p2pDataSeqNumber, offsetDataSeqNumber: this.offsetDataSeqNumber, message: { seqNo: message.seqNo, commandType: types_1.CommandType[message.commandId], channel: message.channel, signCode: message.signCode, data: message.data.toString("hex") } });
1717
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Expected different command type for received sequencenumber!`, {
1718
+ stationSN: this.rawStation.station_sn,
1719
+ msg_sequence: msg_state.sequence,
1720
+ msg_channel: msg_state.channel,
1721
+ msg_commandType: msg_state.commandType,
1722
+ seqNumber: this.seqNumber,
1723
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
1724
+ offsetDataSeqNumber: this.offsetDataSeqNumber,
1725
+ message: {
1726
+ seqNo: message.seqNo,
1727
+ commandType: types_1.CommandType[message.commandId],
1728
+ channel: message.channel,
1729
+ signCode: message.signCode,
1730
+ data: message.data.toString("hex"),
1731
+ },
1732
+ });
1315
1733
  logging_1.rootP2PLogger.warn(`P2P protocol instability detected for station ${this.rawStation.station_sn}. Please reinitialise the connection to solve the problem!`);
1316
1734
  }
1317
1735
  }
1318
- else if (message.commandId !== types_1.CommandType.CMD_PING && message.commandId !== types_1.CommandType.CMD_GET_DEVICE_PING && message.commandId !== types_1.CommandType.CMD_GATEWAYINFO) {
1319
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received unexpected data!`, { stationSN: this.rawStation.station_sn, seqNumber: this.seqNumber, p2pDataSeqNumber: this.p2pDataSeqNumber, offsetDataSeqNumber: this.offsetDataSeqNumber, message: { seqNo: message.seqNo, commandType: types_1.CommandType[message.commandId], channel: message.channel, signCode: message.signCode, data: message.data.toString("hex") } });
1736
+ else if (message.commandId !== types_1.CommandType.CMD_PING &&
1737
+ message.commandId !== types_1.CommandType.CMD_GET_DEVICE_PING &&
1738
+ message.commandId !== types_1.CommandType.CMD_GATEWAYINFO) {
1739
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received unexpected data!`, {
1740
+ stationSN: this.rawStation.station_sn,
1741
+ seqNumber: this.seqNumber,
1742
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
1743
+ offsetDataSeqNumber: this.offsetDataSeqNumber,
1744
+ message: {
1745
+ seqNo: message.seqNo,
1746
+ commandType: types_1.CommandType[message.commandId],
1747
+ channel: message.channel,
1748
+ signCode: message.signCode,
1749
+ data: message.data.toString("hex"),
1750
+ },
1751
+ });
1320
1752
  }
1321
1753
  }
1322
1754
  else {
1323
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Unsupported response`, { stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, commandType: message.commandId, channel: message.channel, signCode: message.signCode, data: message.data.toString("hex") } });
1755
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Unsupported response`, {
1756
+ stationSN: this.rawStation.station_sn,
1757
+ message: {
1758
+ seqNo: message.seqNo,
1759
+ commandType: message.commandId,
1760
+ channel: message.channel,
1761
+ signCode: message.signCode,
1762
+ data: message.data.toString("hex"),
1763
+ },
1764
+ });
1324
1765
  }
1325
1766
  }
1326
1767
  else if (message.dataType === types_1.P2PDataType.VIDEO || message.dataType === types_1.P2PDataType.BINARY) {
1327
1768
  this.handleDataBinaryAndVideo(message);
1328
1769
  }
1329
1770
  else {
1330
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented data type`, { stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, commandType: message.commandId, channel: message.channel, signCode: message.signCode, data: message.data.toString("hex") } });
1771
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented data type`, {
1772
+ stationSN: this.rawStation.station_sn,
1773
+ message: {
1774
+ seqNo: message.seqNo,
1775
+ commandType: message.commandId,
1776
+ channel: message.channel,
1777
+ signCode: message.signCode,
1778
+ data: message.data.toString("hex"),
1779
+ },
1780
+ });
1331
1781
  }
1332
1782
  }
1333
1783
  isIFrame(data, isKeyFrame) {
1334
- if (this.rawStation.station_sn.startsWith("T8410") || this.rawStation.station_sn.startsWith("T8400") || this.rawStation.station_sn.startsWith("T8401") || this.rawStation.station_sn.startsWith("T8411") ||
1335
- this.rawStation.station_sn.startsWith("T8202") || this.rawStation.station_sn.startsWith("T8422") || this.rawStation.station_sn.startsWith("T8424") || this.rawStation.station_sn.startsWith("T8423") ||
1336
- this.rawStation.station_sn.startsWith("T8130") || this.rawStation.station_sn.startsWith("T8131") || this.rawStation.station_sn.startsWith("T8420") || this.rawStation.station_sn.startsWith("T8440") ||
1337
- this.rawStation.station_sn.startsWith("T8171") || this.rawStation.station_sn.startsWith("T8426") ||
1338
- this.rawStation.station_sn.startsWith("T8441") || this.rawStation.station_sn.startsWith("T8442") || (0, utils_1.checkT8420)(this.rawStation.station_sn)) {
1784
+ if (this.rawStation.station_sn.startsWith("T8410") ||
1785
+ this.rawStation.station_sn.startsWith("T8400") ||
1786
+ this.rawStation.station_sn.startsWith("T8401") ||
1787
+ this.rawStation.station_sn.startsWith("T8411") ||
1788
+ this.rawStation.station_sn.startsWith("T8202") ||
1789
+ this.rawStation.station_sn.startsWith("T8422") ||
1790
+ this.rawStation.station_sn.startsWith("T8424") ||
1791
+ this.rawStation.station_sn.startsWith("T8423") ||
1792
+ this.rawStation.station_sn.startsWith("T8130") ||
1793
+ this.rawStation.station_sn.startsWith("T8131") ||
1794
+ this.rawStation.station_sn.startsWith("T8420") ||
1795
+ this.rawStation.station_sn.startsWith("T8440") ||
1796
+ this.rawStation.station_sn.startsWith("T8171") ||
1797
+ this.rawStation.station_sn.startsWith("T8426") ||
1798
+ this.rawStation.station_sn.startsWith("T8441") ||
1799
+ this.rawStation.station_sn.startsWith("T8442") ||
1800
+ (0, utils_1.checkT8420)(this.rawStation.station_sn)) {
1339
1801
  //TODO: Need to add battery doorbells as seen in source => T8210,T8220,T8221,T8222
1340
1802
  return isKeyFrame;
1341
1803
  }
@@ -1368,7 +1830,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1368
1830
  videoHeight: 1080,
1369
1831
  videoTimestamp: 0,
1370
1832
  videoDataLength: 0,
1371
- aesKey: ""
1833
+ aesKey: "",
1372
1834
  };
1373
1835
  const data_length = message.data.readUInt32LE();
1374
1836
  const isKeyFrame = message.data.subarray(4, 5).readUInt8() === 1 ? true : false;
@@ -1386,13 +1848,22 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1386
1848
  if (rsaKey) {
1387
1849
  try {
1388
1850
  videoMetaData.aesKey = rsaKey.decrypt(key).toString("hex");
1389
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypted AES key`, { stationSN: this.rawStation.station_sn, key: videoMetaData.aesKey });
1851
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypted AES key`, {
1852
+ stationSN: this.rawStation.station_sn,
1853
+ key: videoMetaData.aesKey,
1854
+ });
1390
1855
  }
1391
1856
  catch (err) {
1392
1857
  const error = (0, error_1.ensureError)(err);
1393
- logging_1.rootP2PLogger.warn(`Error: AES key could not be decrypted! The entire stream is discarded.`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, key: key.toString("hex") });
1858
+ logging_1.rootP2PLogger.warn(`Error: AES key could not be decrypted! The entire stream is discarded.`, {
1859
+ error: (0, utils_3.getError)(error),
1860
+ stationSN: this.rawStation.station_sn,
1861
+ key: key.toString("hex"),
1862
+ });
1394
1863
  this.currentMessageState[message.dataType].invalidStream = true;
1395
- this.emit("livestream error", message.channel, new error_1.LivestreamError("Station AES key could not be decrypted! The entire stream is discarded.", { context: { station: this.rawStation.station_sn } }));
1864
+ this.emit("livestream error", message.channel, new error_1.LivestreamError("Station AES key could not be decrypted! The entire stream is discarded.", {
1865
+ context: { station: this.rawStation.station_sn },
1866
+ }));
1396
1867
  return;
1397
1868
  }
1398
1869
  }
@@ -1413,36 +1884,93 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1413
1884
  else {
1414
1885
  video_data = message.data.subarray(payloadStart, payloadStart + videoMetaData.videoDataLength);
1415
1886
  }
1416
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME`, { stationSN: this.rawStation.station_sn, dataSize: message.data.length, metadata: videoMetaData, videoDataSize: video_data.length });
1887
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME`, {
1888
+ stationSN: this.rawStation.station_sn,
1889
+ dataSize: message.data.length,
1890
+ metadata: videoMetaData,
1891
+ videoDataSize: video_data.length,
1892
+ });
1417
1893
  this.currentMessageState[message.dataType].p2pStreamMetadata.videoFPS = videoMetaData.videoFPS;
1418
1894
  this.currentMessageState[message.dataType].p2pStreamMetadata.videoHeight = videoMetaData.videoHeight;
1419
1895
  this.currentMessageState[message.dataType].p2pStreamMetadata.videoWidth = videoMetaData.videoWidth;
1420
1896
  if (!this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived) {
1421
- if (this.rawStation.station_sn.startsWith("T8410") || this.rawStation.station_sn.startsWith("T8400") || this.rawStation.station_sn.startsWith("T8401") || this.rawStation.station_sn.startsWith("T8411") ||
1422
- this.rawStation.station_sn.startsWith("T8202") || this.rawStation.station_sn.startsWith("T8422") || this.rawStation.station_sn.startsWith("T8424") || this.rawStation.station_sn.startsWith("T8423") ||
1423
- this.rawStation.station_sn.startsWith("T8130") || this.rawStation.station_sn.startsWith("T8131") || this.rawStation.station_sn.startsWith("T8420") || this.rawStation.station_sn.startsWith("T8440") ||
1424
- this.rawStation.station_sn.startsWith("T8171") || this.rawStation.station_sn.startsWith("T8426") ||
1425
- this.rawStation.station_sn.startsWith("T8441") || this.rawStation.station_sn.startsWith("T8442") || (0, utils_1.checkT8420)(this.rawStation.station_sn)) {
1426
- this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec = videoMetaData.streamType === 1 ? types_1.VideoCodec.H264 : videoMetaData.streamType === 2 ? types_1.VideoCodec.H265 : (0, utils_1.getVideoCodec)(video_data);
1427
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Video codec information received from packet`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, metadata: videoMetaData });
1897
+ if (this.rawStation.station_sn.startsWith("T8410") ||
1898
+ this.rawStation.station_sn.startsWith("T8400") ||
1899
+ this.rawStation.station_sn.startsWith("T8401") ||
1900
+ this.rawStation.station_sn.startsWith("T8411") ||
1901
+ this.rawStation.station_sn.startsWith("T8202") ||
1902
+ this.rawStation.station_sn.startsWith("T8422") ||
1903
+ this.rawStation.station_sn.startsWith("T8424") ||
1904
+ this.rawStation.station_sn.startsWith("T8423") ||
1905
+ this.rawStation.station_sn.startsWith("T8130") ||
1906
+ this.rawStation.station_sn.startsWith("T8131") ||
1907
+ this.rawStation.station_sn.startsWith("T8420") ||
1908
+ this.rawStation.station_sn.startsWith("T8440") ||
1909
+ this.rawStation.station_sn.startsWith("T8171") ||
1910
+ this.rawStation.station_sn.startsWith("T8426") ||
1911
+ this.rawStation.station_sn.startsWith("T8441") ||
1912
+ this.rawStation.station_sn.startsWith("T8442") ||
1913
+ (0, utils_1.checkT8420)(this.rawStation.station_sn)) {
1914
+ this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec =
1915
+ videoMetaData.streamType === 1
1916
+ ? types_1.VideoCodec.H264
1917
+ : videoMetaData.streamType === 2
1918
+ ? types_1.VideoCodec.H265
1919
+ : (0, utils_1.getVideoCodec)(video_data);
1920
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Video codec information received from packet`, {
1921
+ stationSN: this.rawStation.station_sn,
1922
+ commandIdName: types_1.CommandType[message.commandId],
1923
+ commandId: message.commandId,
1924
+ channel: message.channel,
1925
+ metadata: videoMetaData,
1926
+ });
1428
1927
  }
1429
1928
  else if (this.isIFrame(video_data, isKeyFrame)) {
1430
1929
  this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec = (0, utils_1.getVideoCodec)(video_data);
1431
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Video codec extracted from video data`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, metadata: videoMetaData });
1930
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Video codec extracted from video data`, {
1931
+ stationSN: this.rawStation.station_sn,
1932
+ commandIdName: types_1.CommandType[message.commandId],
1933
+ commandId: message.commandId,
1934
+ channel: message.channel,
1935
+ metadata: videoMetaData,
1936
+ });
1432
1937
  }
1433
1938
  else {
1434
1939
  this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec = (0, utils_1.getVideoCodec)(video_data);
1435
1940
  if (this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec === types_1.VideoCodec.UNKNOWN) {
1436
- this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec = videoMetaData.streamType === 1 ? types_1.VideoCodec.H264 : videoMetaData.streamType === 2 ? types_1.VideoCodec.H265 : types_1.VideoCodec.UNKNOWN;
1941
+ this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec =
1942
+ videoMetaData.streamType === 1
1943
+ ? types_1.VideoCodec.H264
1944
+ : videoMetaData.streamType === 2
1945
+ ? types_1.VideoCodec.H265
1946
+ : types_1.VideoCodec.UNKNOWN;
1437
1947
  if (this.currentMessageState[message.dataType].p2pStreamMetadata.videoCodec === types_1.VideoCodec.UNKNOWN) {
1438
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Unknown video codec`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, metadata: videoMetaData });
1948
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Unknown video codec`, {
1949
+ stationSN: this.rawStation.station_sn,
1950
+ commandIdName: types_1.CommandType[message.commandId],
1951
+ commandId: message.commandId,
1952
+ channel: message.channel,
1953
+ metadata: videoMetaData,
1954
+ });
1439
1955
  }
1440
1956
  else {
1441
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Fallback, using video codec information received from packet`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, metadata: videoMetaData });
1957
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Fallback, using video codec information received from packet`, {
1958
+ stationSN: this.rawStation.station_sn,
1959
+ commandIdName: types_1.CommandType[message.commandId],
1960
+ commandId: message.commandId,
1961
+ channel: message.channel,
1962
+ metadata: videoMetaData,
1963
+ });
1442
1964
  }
1443
1965
  }
1444
1966
  else {
1445
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Fallback, video codec extracted from video data`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, metadata: videoMetaData });
1967
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME - Fallback, video codec extracted from video data`, {
1968
+ stationSN: this.rawStation.station_sn,
1969
+ commandIdName: types_1.CommandType[message.commandId],
1970
+ commandId: message.commandId,
1971
+ channel: message.channel,
1972
+ metadata: videoMetaData,
1973
+ });
1446
1974
  }
1447
1975
  }
1448
1976
  this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived = true;
@@ -1451,20 +1979,27 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1451
1979
  this.currentMessageState[message.dataType].waitForAudioData = undefined;
1452
1980
  this.currentMessageState[message.dataType].p2pStreamMetadata.audioCodec = types_1.AudioCodec.NONE;
1453
1981
  this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived = true;
1454
- if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived && this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived && this.currentMessageState[message.dataType].p2pStreamNotStarted) {
1982
+ if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived &&
1983
+ this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived &&
1984
+ this.currentMessageState[message.dataType].p2pStreamNotStarted) {
1455
1985
  this.emitStreamStartEvent(message.dataType);
1456
1986
  }
1457
1987
  }, this.AUDIO_CODEC_ANALYZE_TIMEOUT);
1458
1988
  }
1459
1989
  }
1460
1990
  if (this.currentMessageState[message.dataType].p2pStreamNotStarted) {
1461
- if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived && this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived) {
1991
+ if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived &&
1992
+ this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived) {
1462
1993
  this.emitStreamStartEvent(message.dataType);
1463
1994
  }
1464
1995
  }
1465
1996
  if (message.dataType === types_1.P2PDataType.VIDEO) {
1466
1997
  if ((0, utils_1.findStartCode)(video_data)) {
1467
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME: startcode found`, { stationSN: this.rawStation.station_sn, isKeyFrame: isKeyFrame, preFrameVideoDataLength: this.currentMessageState[message.dataType].preFrameVideoData.length });
1998
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME: startcode found`, {
1999
+ stationSN: this.rawStation.station_sn,
2000
+ isKeyFrame: isKeyFrame,
2001
+ preFrameVideoDataLength: this.currentMessageState[message.dataType].preFrameVideoData.length,
2002
+ });
1468
2003
  if (!this.currentMessageState[message.dataType].receivedFirstIFrame)
1469
2004
  this.currentMessageState[message.dataType].receivedFirstIFrame = this.isIFrame(video_data, isKeyFrame);
1470
2005
  if (this.currentMessageState[message.dataType].receivedFirstIFrame) {
@@ -1480,9 +2015,16 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1480
2015
  }
1481
2016
  }
1482
2017
  else {
1483
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME: No startcode found`, { stationSN: this.rawStation.station_sn, isKeyFrame: isKeyFrame, preFrameVideoDataLength: this.currentMessageState[message.dataType].preFrameVideoData.length });
2018
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_VIDEO_FRAME: No startcode found`, {
2019
+ stationSN: this.rawStation.station_sn,
2020
+ isKeyFrame: isKeyFrame,
2021
+ preFrameVideoDataLength: this.currentMessageState[message.dataType].preFrameVideoData.length,
2022
+ });
1484
2023
  if (this.currentMessageState[message.dataType].preFrameVideoData.length > 0) {
1485
- this.currentMessageState[message.dataType].preFrameVideoData = Buffer.concat([this.currentMessageState[message.dataType].preFrameVideoData, video_data]);
2024
+ this.currentMessageState[message.dataType].preFrameVideoData = Buffer.concat([
2025
+ this.currentMessageState[message.dataType].preFrameVideoData,
2026
+ video_data,
2027
+ ]);
1486
2028
  }
1487
2029
  }
1488
2030
  }
@@ -1496,35 +2038,60 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1496
2038
  audioType: types_1.AudioCodec.NONE,
1497
2039
  audioSeqNo: 0,
1498
2040
  audioTimestamp: 0,
1499
- audioDataLength: 0
2041
+ audioDataLength: 0,
1500
2042
  };
1501
2043
  audioMetaData.audioDataLength = message.data.subarray(0, 4).readUInt32LE();
1502
2044
  audioMetaData.audioType = message.data.subarray(5, 6).readUInt8();
1503
2045
  audioMetaData.audioSeqNo = message.data.subarray(6, 8).readUInt16LE();
1504
2046
  audioMetaData.audioTimestamp = message.data.subarray(8, 14).readUIntLE(0, 6);
1505
2047
  const audio_data = Buffer.from(message.data.subarray(16));
1506
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_AUDIO_FRAME`, { stationSN: this.rawStation.station_sn, dataSize: message.data.length, metadata: audioMetaData, audioDataSize: audio_data.length });
2048
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_AUDIO_FRAME`, {
2049
+ stationSN: this.rawStation.station_sn,
2050
+ dataSize: message.data.length,
2051
+ metadata: audioMetaData,
2052
+ audioDataSize: audio_data.length,
2053
+ });
1507
2054
  if (this.currentMessageState[message.dataType].waitForAudioData !== undefined) {
1508
2055
  clearTimeout(this.currentMessageState[message.dataType].waitForAudioData);
1509
2056
  }
1510
2057
  if (!this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived) {
1511
2058
  this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived = true;
1512
- this.currentMessageState[message.dataType].p2pStreamMetadata.audioCodec = audioMetaData.audioType === 0 ? types_1.AudioCodec.AAC : audioMetaData.audioType === 1 ? types_1.AudioCodec.AAC_LC : audioMetaData.audioType === 7 ? types_1.AudioCodec.AAC_ELD : types_1.AudioCodec.UNKNOWN;
2059
+ this.currentMessageState[message.dataType].p2pStreamMetadata.audioCodec =
2060
+ audioMetaData.audioType === 0
2061
+ ? types_1.AudioCodec.AAC
2062
+ : audioMetaData.audioType === 1
2063
+ ? types_1.AudioCodec.AAC_LC
2064
+ : audioMetaData.audioType === 7
2065
+ ? types_1.AudioCodec.AAC_ELD
2066
+ : types_1.AudioCodec.UNKNOWN;
1513
2067
  }
1514
2068
  if (this.currentMessageState[message.dataType].p2pStreamNotStarted) {
1515
- if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived && this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived) {
2069
+ if (this.currentMessageState[message.dataType].p2pStreamFirstAudioDataReceived &&
2070
+ this.currentMessageState[message.dataType].p2pStreamFirstVideoDataReceived) {
1516
2071
  this.emitStreamStartEvent(message.dataType);
1517
2072
  }
1518
2073
  }
1519
2074
  this.currentMessageState[message.dataType].audioStream?.push(audio_data);
1520
2075
  break;
1521
2076
  default:
1522
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented message`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, data: message.data.toString("hex") });
2077
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented message`, {
2078
+ stationSN: this.rawStation.station_sn,
2079
+ commandIdName: types_1.CommandType[message.commandId],
2080
+ commandId: message.commandId,
2081
+ channel: message.channel,
2082
+ data: message.data.toString("hex"),
2083
+ });
1523
2084
  break;
1524
2085
  }
1525
2086
  }
1526
2087
  else {
1527
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Invalid stream data, dropping complete stream`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, data: message.data.toString("hex") });
2088
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Invalid stream data, dropping complete stream`, {
2089
+ stationSN: this.rawStation.station_sn,
2090
+ commandIdName: types_1.CommandType[message.commandId],
2091
+ commandId: message.commandId,
2092
+ channel: message.channel,
2093
+ data: message.data.toString("hex"),
2094
+ });
1528
2095
  }
1529
2096
  }
1530
2097
  handleDataControl(message) {
@@ -1537,125 +2104,277 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1537
2104
  }
1538
2105
  catch (err) {
1539
2106
  const error = (0, error_1.ensureError)(err);
1540
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2107
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
2108
+ error: (0, utils_3.getError)(error),
2109
+ stationSN: this.rawStation.station_sn,
2110
+ message: {
2111
+ seqNo: message.seqNo,
2112
+ channel: message.channel,
2113
+ commandType: types_1.CommandType[message.commandId],
2114
+ signCode: message.signCode,
2115
+ type: message.type,
2116
+ dataType: types_1.P2PDataType[message.dataType],
2117
+ data: message.data.toString("hex"),
2118
+ },
2119
+ });
1541
2120
  }
1542
2121
  }
1543
- logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received data`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, data: message.data.toString("hex"), seqNumber: this.seqNumber, p2pDataSeqNumber: this.p2pDataSeqNumber, offsetDataSeqNumber: this.offsetDataSeqNumber });
2122
+ logging_1.rootP2PLogger.trace(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Received data`, {
2123
+ stationSN: this.rawStation.station_sn,
2124
+ commandIdName: types_1.CommandType[message.commandId],
2125
+ commandId: message.commandId,
2126
+ data: message.data.toString("hex"),
2127
+ seqNumber: this.seqNumber,
2128
+ p2pDataSeqNumber: this.p2pDataSeqNumber,
2129
+ offsetDataSeqNumber: this.offsetDataSeqNumber,
2130
+ });
1544
2131
  switch (message.commandId) {
1545
2132
  case types_1.CommandType.CMD_GET_ALARM_MODE:
1546
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Alarm mode changed to: ${types_2.AlarmMode[data.readUIntBE(0, 1)]}`, { stationSN: this.rawStation.station_sn, });
2133
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Alarm mode changed to: ${types_2.AlarmMode[data.readUIntBE(0, 1)]}`, { stationSN: this.rawStation.station_sn });
1547
2134
  this.emit("alarm mode", data.readUIntBE(0, 1));
1548
2135
  break;
1549
2136
  case types_1.CommandType.CMD_CAMERA_INFO:
1550
2137
  try {
1551
2138
  const cameraData = (0, utils_1.getNullTerminatedString)(data, "utf8");
1552
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Camera info`, { stationSN: this.rawStation.station_sn, cameraInfo: cameraData });
2139
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Camera info`, {
2140
+ stationSN: this.rawStation.station_sn,
2141
+ cameraInfo: cameraData,
2142
+ });
1553
2143
  this.emit("camera info", (0, utils_3.parseJSON)(cameraData, logging_1.rootP2PLogger));
1554
2144
  }
1555
2145
  catch (err) {
1556
2146
  const error = (0, error_1.ensureError)(err);
1557
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Camera info - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2147
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Camera info - Error`, {
2148
+ error: (0, utils_3.getError)(error),
2149
+ stationSN: this.rawStation.station_sn,
2150
+ message: {
2151
+ seqNo: message.seqNo,
2152
+ channel: message.channel,
2153
+ commandType: types_1.CommandType[message.commandId],
2154
+ signCode: message.signCode,
2155
+ type: message.type,
2156
+ dataType: types_1.P2PDataType[message.dataType],
2157
+ data: message.data.toString("hex"),
2158
+ },
2159
+ });
1558
2160
  }
1559
2161
  break;
1560
2162
  case types_1.CommandType.CMD_CONVERT_MP4_OK:
1561
2163
  const totalBytes = data.subarray(1).readUInt32LE();
1562
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_CONVERT_MP4_OK`, { stationSN: this.rawStation.station_sn, channel: message.channel, totalBytes: totalBytes });
2164
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_CONVERT_MP4_OK`, {
2165
+ stationSN: this.rawStation.station_sn,
2166
+ channel: message.channel,
2167
+ totalBytes: totalBytes,
2168
+ });
1563
2169
  this.downloadTotalBytes = totalBytes;
1564
2170
  this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreaming = true;
1565
- this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel = message.channel;
2171
+ // HB3 sends channel 255 (broadcast) which is not a valid device channel
2172
+ if (message.channel !== 255) {
2173
+ this.currentMessageState[types_1.P2PDataType.BINARY].p2pStreamChannel = message.channel;
2174
+ }
1566
2175
  break;
1567
2176
  case types_1.CommandType.CMD_WIFI_CONFIG:
1568
2177
  const rssi = data.readInt32LE();
1569
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_WIFI_CONFIG`, { stationSN: this.rawStation.station_sn, channel: message.channel, rssi: rssi });
2178
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_WIFI_CONFIG`, {
2179
+ stationSN: this.rawStation.station_sn,
2180
+ channel: message.channel,
2181
+ rssi: rssi,
2182
+ });
1570
2183
  this.emit("wifi rssi", message.channel, rssi);
1571
2184
  break;
1572
2185
  case types_1.CommandType.CMD_DOWNLOAD_FINISH:
1573
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOWNLOAD_FINISH`, { stationSN: this.rawStation.station_sn, channel: message.channel });
2186
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOWNLOAD_FINISH`, {
2187
+ stationSN: this.rawStation.station_sn,
2188
+ channel: message.channel,
2189
+ });
1574
2190
  this.endStream(types_1.P2PDataType.BINARY);
1575
2191
  break;
1576
2192
  case types_1.CommandType.CMD_DOORBELL_NOTIFY_PAYLOAD:
1577
2193
  try {
1578
2194
  const str = (0, utils_1.getNullTerminatedString)(data, "utf8");
1579
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORBELL_NOTIFY_PAYLOAD`, { stationSN: this.rawStation.station_sn, payload: str });
2195
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORBELL_NOTIFY_PAYLOAD`, {
2196
+ stationSN: this.rawStation.station_sn,
2197
+ payload: str,
2198
+ });
1580
2199
  //TODO: Finish implementation, emit an event...
1581
2200
  //VDBStreamInfo (1005) and VoltageEvent (1015)
1582
2201
  //this.emit("", parseJSON(str, this.log) as xy);
1583
2202
  }
1584
2203
  catch (err) {
1585
2204
  const error = (0, error_1.ensureError)(err);
1586
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORBELL_NOTIFY_PAYLOAD - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2205
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORBELL_NOTIFY_PAYLOAD - Error`, {
2206
+ error: (0, utils_3.getError)(error),
2207
+ stationSN: this.rawStation.station_sn,
2208
+ message: {
2209
+ seqNo: message.seqNo,
2210
+ channel: message.channel,
2211
+ commandType: types_1.CommandType[message.commandId],
2212
+ signCode: message.signCode,
2213
+ type: message.type,
2214
+ dataType: types_1.P2PDataType[message.dataType],
2215
+ data: message.data.toString("hex"),
2216
+ },
2217
+ });
1587
2218
  }
1588
2219
  break;
1589
2220
  case types_1.CommandType.CMD_NAS_SWITCH:
1590
2221
  try {
1591
2222
  const str = (0, utils_1.getNullTerminatedString)(data, "utf8");
1592
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NAS_SWITCH`, { stationSN: this.rawStation.station_sn, payload: str });
2223
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NAS_SWITCH`, {
2224
+ stationSN: this.rawStation.station_sn,
2225
+ payload: str,
2226
+ });
1593
2227
  this.emit("rtsp url", message.channel, str);
1594
2228
  }
1595
2229
  catch (err) {
1596
2230
  const error = (0, error_1.ensureError)(err);
1597
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NAS_SWITCH - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2231
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NAS_SWITCH - Error`, {
2232
+ error: (0, utils_3.getError)(error),
2233
+ stationSN: this.rawStation.station_sn,
2234
+ message: {
2235
+ seqNo: message.seqNo,
2236
+ channel: message.channel,
2237
+ commandType: types_1.CommandType[message.commandId],
2238
+ signCode: message.signCode,
2239
+ type: message.type,
2240
+ dataType: types_1.P2PDataType[message.dataType],
2241
+ data: message.data.toString("hex"),
2242
+ },
2243
+ });
1598
2244
  }
1599
2245
  break;
1600
2246
  case types_1.CommandType.SUB1G_REP_UNPLUG_POWER_LINE:
1601
2247
  try {
1602
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_UNPLUG_POWER_LINE`, { stationSN: this.rawStation.station_sn, payload: data.toString() });
2248
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_UNPLUG_POWER_LINE`, {
2249
+ stationSN: this.rawStation.station_sn,
2250
+ payload: data.toString(),
2251
+ });
1603
2252
  const chargeType = data.subarray(0, 4).readUInt32LE();
1604
2253
  const batteryLevel = data.subarray(4, 8).readUInt32LE();
1605
2254
  this.emit("charging state", message.channel, chargeType, batteryLevel);
1606
2255
  }
1607
2256
  catch (err) {
1608
2257
  const error = (0, error_1.ensureError)(err);
1609
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_UNPLUG_POWER_LINE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2258
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_UNPLUG_POWER_LINE - Error`, {
2259
+ error: (0, utils_3.getError)(error),
2260
+ stationSN: this.rawStation.station_sn,
2261
+ message: {
2262
+ seqNo: message.seqNo,
2263
+ channel: message.channel,
2264
+ commandType: types_1.CommandType[message.commandId],
2265
+ signCode: message.signCode,
2266
+ type: message.type,
2267
+ dataType: types_1.P2PDataType[message.dataType],
2268
+ data: message.data.toString("hex"),
2269
+ },
2270
+ });
1610
2271
  }
1611
2272
  break;
1612
2273
  case types_1.CommandType.SUB1G_REP_RUNTIME_STATE:
1613
2274
  try {
1614
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_RUNTIME_STATE`, { stationSN: this.rawStation.station_sn, payload: data.toString() });
2275
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_RUNTIME_STATE`, {
2276
+ stationSN: this.rawStation.station_sn,
2277
+ payload: data.toString(),
2278
+ });
1615
2279
  const batteryLevel = data.subarray(0, 4).readUInt32LE();
1616
2280
  const temperature = data.subarray(4, 8).readUInt32LE();
1617
2281
  this.emit("runtime state", message.channel, batteryLevel, temperature);
1618
2282
  }
1619
2283
  catch (err) {
1620
2284
  const error = (0, error_1.ensureError)(err);
1621
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_RUNTIME_STATE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2285
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - SUB1G_REP_RUNTIME_STATE - Error`, {
2286
+ error: (0, utils_3.getError)(error),
2287
+ stationSN: this.rawStation.station_sn,
2288
+ message: {
2289
+ seqNo: message.seqNo,
2290
+ channel: message.channel,
2291
+ commandType: types_1.CommandType[message.commandId],
2292
+ signCode: message.signCode,
2293
+ type: message.type,
2294
+ dataType: types_1.P2PDataType[message.dataType],
2295
+ data: message.data.toString("hex"),
2296
+ },
2297
+ });
1622
2298
  }
1623
2299
  break;
1624
2300
  case types_1.CommandType.CMD_SET_FLOODLIGHT_MANUAL_SWITCH:
1625
2301
  try {
1626
2302
  const enabled = data.readUIntBE(0, 1) === 1 ? true : false;
1627
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_FLOODLIGHT_MANUAL_SWITCH`, { stationSN: this.rawStation.station_sn, enabled: enabled, payload: data.toString() });
2303
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_FLOODLIGHT_MANUAL_SWITCH`, {
2304
+ stationSN: this.rawStation.station_sn,
2305
+ enabled: enabled,
2306
+ payload: data.toString(),
2307
+ });
1628
2308
  this.emit("floodlight manual switch", message.channel, enabled);
1629
2309
  }
1630
2310
  catch (err) {
1631
2311
  const error = (0, error_1.ensureError)(err);
1632
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_FLOODLIGHT_MANUAL_SWITCH - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2312
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_FLOODLIGHT_MANUAL_SWITCH - Error`, {
2313
+ error: (0, utils_3.getError)(error),
2314
+ stationSN: this.rawStation.station_sn,
2315
+ message: {
2316
+ seqNo: message.seqNo,
2317
+ channel: message.channel,
2318
+ commandType: types_1.CommandType[message.commandId],
2319
+ signCode: message.signCode,
2320
+ type: message.type,
2321
+ dataType: types_1.P2PDataType[message.dataType],
2322
+ data: message.data.toString("hex"),
2323
+ },
2324
+ });
1633
2325
  }
1634
2326
  break;
1635
2327
  case types_1.CommandType.CMD_GET_DEVICE_PING:
1636
2328
  try {
1637
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DEVICE_PING`, { stationSN: this.rawStation.station_sn, payload: data.toString() });
2329
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DEVICE_PING`, {
2330
+ stationSN: this.rawStation.station_sn,
2331
+ payload: data.toString(),
2332
+ });
1638
2333
  this.sendCommandDevicePing(message.channel);
1639
2334
  }
1640
2335
  catch (err) {
1641
2336
  const error = (0, error_1.ensureError)(err);
1642
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DEVICE_PING - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2337
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DEVICE_PING - Error`, {
2338
+ error: (0, utils_3.getError)(error),
2339
+ stationSN: this.rawStation.station_sn,
2340
+ message: {
2341
+ seqNo: message.seqNo,
2342
+ channel: message.channel,
2343
+ commandType: types_1.CommandType[message.commandId],
2344
+ signCode: message.signCode,
2345
+ type: message.type,
2346
+ dataType: types_1.P2PDataType[message.dataType],
2347
+ data: message.data.toString("hex"),
2348
+ },
2349
+ });
1643
2350
  }
1644
2351
  break;
1645
2352
  case types_1.CommandType.CMD_NOTIFY_PAYLOAD:
1646
2353
  try {
1647
- logging_1.rootP2PLogger.debug(`Station Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD`, { stationSN: this.rawStation.station_sn, payload: data.toString() });
2354
+ logging_1.rootP2PLogger.debug(`Station Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD`, {
2355
+ stationSN: this.rawStation.station_sn,
2356
+ payload: data.toString(),
2357
+ });
1648
2358
  const json = (0, utils_3.parseJSON)((0, utils_1.getNullTerminatedString)(data, "utf8"), logging_1.rootP2PLogger);
1649
2359
  if (json !== undefined) {
1650
2360
  if (device_1.Device.isLockWifi(this.rawStation.device_type, this.rawStation.station_sn)) {
1651
2361
  //TODO: Implement notification payload or T8520
1652
- if (json.cmd === types_1.CommandType.P2P_ADD_PW || json.cmd === types_1.CommandType.P2P_QUERY_PW || json.cmd === types_1.CommandType.P2P_GET_LOCK_PARAM || json.cmd === types_1.CommandType.P2P_GET_USER_AND_PW_ID) {
2362
+ if (json.cmd === types_1.CommandType.P2P_ADD_PW ||
2363
+ json.cmd === types_1.CommandType.P2P_QUERY_PW ||
2364
+ json.cmd === types_1.CommandType.P2P_GET_LOCK_PARAM ||
2365
+ json.cmd === types_1.CommandType.P2P_GET_USER_AND_PW_ID) {
1653
2366
  // encrypted data
1654
2367
  //TODO: Handle decryption of encrypted Data (AES) - For decryption use the cached aeskey used for sending the command!
1655
2368
  const aesKey = this.getLockAESKey(json.cmd);
1656
2369
  if (aesKey !== undefined) {
1657
2370
  const decryptedPayload = (0, utils_1.decryptPayloadData)(Buffer.from(json.payload, "base64"), Buffer.from(aesKey, "hex"), Buffer.from((0, utils_1.getLockVectorBytes)(this.rawStation.station_sn), "hex")).toString();
1658
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock - Received`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, decryptedPayload: decryptedPayload, aesKey: aesKey });
2371
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock - Received`, {
2372
+ stationSN: this.rawStation.station_sn,
2373
+ commandIdName: types_1.CommandType[json.cmd],
2374
+ commandId: json.cmd,
2375
+ decryptedPayload: decryptedPayload,
2376
+ aesKey: aesKey,
2377
+ });
1659
2378
  switch (json.cmd) {
1660
2379
  case types_1.CommandType.P2P_ADD_PW:
1661
2380
  // decryptedPayload: {"code":0,"passwordId":"002C"}
@@ -1670,7 +2389,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1670
2389
  this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTLOCK_QUERY_STATUS, payload.slState);
1671
2390
  }
1672
2391
  else {
1673
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Not implemented`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, message: data.toString() });
2392
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Not implemented`, {
2393
+ stationSN: this.rawStation.station_sn,
2394
+ commandIdName: types_1.CommandType[json.cmd],
2395
+ commandId: json.cmd,
2396
+ message: data.toString(),
2397
+ });
1674
2398
  }
1675
2399
  }
1676
2400
  else if (json.cmd === types_1.CommandType.P2P_QUERY_STATUS_IN_LOCK) {
@@ -1681,7 +2405,8 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1681
2405
  this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTLOCK_NIGHT_VISION_SIDE, payload.slOpenDirection);
1682
2406
  }
1683
2407
  else if (json.cmd === types_1.CommandType.CMD_DOORLOCK_P2P_SEQ) {
1684
- if (device_1.Device.isLockWifi(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn) || device_1.Device.isLockWifiNoFinger(this.rawStation.devices[0]?.device_type)) {
2408
+ if (device_1.Device.isLockWifi(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn) ||
2409
+ device_1.Device.isLockWifiNoFinger(this.rawStation.devices[0]?.device_type)) {
1685
2410
  const payload = json.payload;
1686
2411
  if (payload.seq_num !== undefined) {
1687
2412
  this.lockSeqNumber = payload.seq_num;
@@ -1694,18 +2419,34 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1694
2419
  else {
1695
2420
  const payload = json.payload;
1696
2421
  if (payload.seq_num !== undefined) {
1697
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Lock sequence number`, { stationSN: this.rawStation.station_sn, oldSequenceNumber: this.lockSeqNumber, newSequenceNumber: this.lockSeqNumber + 1, payload: payload });
2422
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Lock sequence number`, {
2423
+ stationSN: this.rawStation.station_sn,
2424
+ oldSequenceNumber: this.lockSeqNumber,
2425
+ newSequenceNumber: this.lockSeqNumber + 1,
2426
+ payload: payload,
2427
+ });
1698
2428
  this.lockSeqNumber = payload.seq_num + 1;
1699
2429
  }
1700
2430
  if (payload.lock_cmd > 0) {
1701
- if (device_1.Device.isLockWifiR10(this.rawStation.devices[0]?.device_type) || device_1.Device.isLockWifiR20(this.rawStation.devices[0]?.device_type)) {
2431
+ if (device_1.Device.isLockWifiR10(this.rawStation.devices[0]?.device_type) ||
2432
+ device_1.Device.isLockWifiR20(this.rawStation.devices[0]?.device_type)) {
1702
2433
  this.emit("sequence error", message.channel, types_1.ESLCommand[types_1.ESLBleCommand[payload.lock_cmd]], payload.seq_num, payload.dev_sn);
1703
2434
  }
1704
- else if (device_1.Device.isLockWifiT8506(this.rawStation.devices[0]?.device_type) || device_1.Device.isLockWifiT8502(this.rawStation.devices[0]?.device_type) || device_1.Device.isLockWifiT8510P(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn) || device_1.Device.isLockWifiT8520P(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn)) {
1705
- this.emit("sequence error", message.channel, types_1.SmartLockCommand[payload.bus_type == types_1.SmartLockFunctionType.TYPE_2 ? types_1.SmartLockBleCommandFunctionType2[payload.lock_cmd] : types_1.SmartLockBleCommandFunctionType1[payload.lock_cmd]], payload.seq_num, payload.dev_sn);
2435
+ else if (device_1.Device.isLockWifiT8506(this.rawStation.devices[0]?.device_type) ||
2436
+ device_1.Device.isLockWifiT8502(this.rawStation.devices[0]?.device_type) ||
2437
+ device_1.Device.isLockWifiT8510P(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn) ||
2438
+ device_1.Device.isLockWifiT8520P(this.rawStation.devices[0]?.device_type, this.rawStation.devices[0]?.device_sn)) {
2439
+ this.emit("sequence error", message.channel, types_1.SmartLockCommand[payload.bus_type == types_1.SmartLockFunctionType.TYPE_2
2440
+ ? types_1.SmartLockBleCommandFunctionType2[payload.lock_cmd]
2441
+ : types_1.SmartLockBleCommandFunctionType1[payload.lock_cmd]], payload.seq_num, payload.dev_sn);
1706
2442
  }
1707
2443
  else {
1708
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Lock sequence number - Unknwon device`, { stationSN: this.rawStation.station_sn, oldSequenceNumber: this.lockSeqNumber, newSequenceNumber: this.lockSeqNumber + 1, payload: payload });
2444
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Lock sequence number - Unknwon device`, {
2445
+ stationSN: this.rawStation.station_sn,
2446
+ oldSequenceNumber: this.lockSeqNumber,
2447
+ newSequenceNumber: this.lockSeqNumber + 1,
2448
+ payload: payload,
2449
+ });
1709
2450
  }
1710
2451
  }
1711
2452
  }
@@ -1717,7 +2458,14 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1717
2458
  const decoded = (0, utils_1.decodeBase64)((0, utils_1.decodeLockPayload)(Buffer.from(payload.lock_payload)));
1718
2459
  const key = (0, utils_1.generateBasicLockAESKey)(this.deviceSNs[message.channel].adminUserId, this.rawStation.station_sn);
1719
2460
  const iv = (0, utils_1.getLockVectorBytes)(this.rawStation.station_sn);
1720
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORLOCK_DATA_PASS_THROUGH`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, key: key, iv: iv, decoded: decoded.toString("hex") });
2461
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DOORLOCK_DATA_PASS_THROUGH`, {
2462
+ stationSN: this.rawStation.station_sn,
2463
+ commandIdName: types_1.CommandType[json.cmd],
2464
+ commandId: json.cmd,
2465
+ key: key,
2466
+ iv: iv,
2467
+ decoded: decoded.toString("hex"),
2468
+ });
1721
2469
  payload.lock_payload = (0, utils_1.decryptLockAESData)(key, iv, decoded).toString("hex");
1722
2470
  switch (payload.lock_cmd) {
1723
2471
  case types_1.ESLBleCommand.NOTIFY:
@@ -1747,19 +2495,28 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1747
2495
  const returnCode = data.readInt8(0);
1748
2496
  const commandType = Number.parseInt(types_1.ESLCommand[types_1.ESLBleCommand[fac.getCommandCode()]]);
1749
2497
  const customData = {
1750
- ...this.customDataStaging[commandType]
2498
+ ...this.customDataStaging[commandType],
1751
2499
  };
1752
2500
  if (this.customDataStaging[commandType]) {
1753
2501
  const result = {
1754
2502
  channel: customData.channel,
1755
2503
  command_type: commandType,
1756
2504
  return_code: returnCode,
1757
- customData: customData.customData
2505
+ customData: customData.customData,
1758
2506
  };
1759
2507
  this.emit("secondary command", result);
1760
2508
  delete this.customDataStaging[commandType];
1761
2509
  }
1762
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 return code: ${returnCode}`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, decoded: data.toString("hex"), bleCommandCode: types_1.ESLBleCommand[fac.getCommandCode()], returnCode: returnCode, channel: customData?.channel, customData: customData?.customData });
2510
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 return code: ${returnCode}`, {
2511
+ stationSN: this.rawStation.station_sn,
2512
+ commandIdName: types_1.CommandType[json.cmd],
2513
+ commandId: json.cmd,
2514
+ decoded: data.toString("hex"),
2515
+ bleCommandCode: types_1.ESLBleCommand[fac.getCommandCode()],
2516
+ returnCode: returnCode,
2517
+ channel: customData?.channel,
2518
+ customData: customData?.customData,
2519
+ });
1763
2520
  const parsePayload = new utils_2.ParsePayload(data.subarray(1));
1764
2521
  switch (fac.getCommandCode()) {
1765
2522
  case types_1.ESLBleCommand.QUERY_STATUS_IN_LOCK:
@@ -1783,18 +2540,33 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1783
2540
  //this.emit("parameter", message.channel, CommandType.CMD_SMARTLOCK_WIFI_STATUS, parsePayload.readInt8(BleParameterIndex.THIRTEEN).toString());
1784
2541
  break;
1785
2542
  case types_1.ESLBleCommand.ADD_PW:
1786
- if (customData && customData.customData && customData.customData.command && customData.customData.command.name === http_1.CommandName.DeviceAddUser) {
2543
+ if (customData &&
2544
+ customData.customData &&
2545
+ customData.customData.command &&
2546
+ customData.customData.command.name === http_1.CommandName.DeviceAddUser) {
1787
2547
  this.api.updateUserPassword(customData.customData.command.value?.deviceSN, customData.customData.command.value?.shortUserId, parsePayload.readStringHex(ble_1.BleParameterIndex.ONE), customData.customData.command.value?.schedule);
1788
2548
  }
1789
2549
  break;
1790
2550
  default:
1791
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, { stationSN: this.rawStation.station_sn, fac: fac.toString(), returnCode: returnCode, channel: customData?.channel, customData: customData?.customData });
2551
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, {
2552
+ stationSN: this.rawStation.station_sn,
2553
+ fac: fac.toString(),
2554
+ returnCode: returnCode,
2555
+ channel: customData?.channel,
2556
+ customData: customData?.customData,
2557
+ });
1792
2558
  break;
1793
2559
  }
1794
2560
  }
1795
2561
  catch (err) {
1796
2562
  const error = (0, error_1.ensureError)(err);
1797
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 unable to decrypt (maybe event from other lock connection)`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, payload: payload });
2563
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 unable to decrypt (maybe event from other lock connection)`, {
2564
+ error: (0, utils_3.getError)(error),
2565
+ stationSN: this.rawStation.station_sn,
2566
+ commandIdName: types_1.CommandType[json.cmd],
2567
+ commandId: json.cmd,
2568
+ payload: payload,
2569
+ });
1798
2570
  }
1799
2571
  this._clearSecondaryCommandTimeout();
1800
2572
  this.sendQueuedMessage();
@@ -1804,7 +2576,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1804
2576
  }
1805
2577
  }
1806
2578
  else {
1807
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 - Unexpected response`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, message: data.toString() });
2579
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Lock V12 - Unexpected response`, {
2580
+ stationSN: this.rawStation.station_sn,
2581
+ commandIdName: types_1.CommandType[json.cmd],
2582
+ commandId: json.cmd,
2583
+ message: data.toString(),
2584
+ });
1808
2585
  }
1809
2586
  }
1810
2587
  else if (json.cmd === types_1.CommandType.CMD_TRANSFER_PAYLOAD) {
@@ -1825,22 +2602,32 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1825
2602
  }
1826
2603
  if (data.length > 0 && fac.getDataType() !== undefined) {
1827
2604
  const returnCode = data.readInt8(0);
1828
- const functionTypeCommand = fac.getDataType() === types_1.SmartLockFunctionType.TYPE_2 ? types_1.SmartLockBleCommandFunctionType2 : types_1.SmartLockBleCommandFunctionType1;
2605
+ const functionTypeCommand = fac.getDataType() === types_1.SmartLockFunctionType.TYPE_2
2606
+ ? types_1.SmartLockBleCommandFunctionType2
2607
+ : types_1.SmartLockBleCommandFunctionType1;
1829
2608
  const commandType = Number.parseInt(types_1.SmartLockCommand[functionTypeCommand[fac.getCommandCode()]]);
1830
2609
  const customData = {
1831
- ...this.customDataStaging[commandType]
2610
+ ...this.customDataStaging[commandType],
1832
2611
  };
1833
2612
  if (this.customDataStaging[commandType]) {
1834
2613
  const result = {
1835
2614
  channel: customData.channel,
1836
2615
  command_type: commandType,
1837
2616
  return_code: returnCode,
1838
- customData: customData.customData
2617
+ customData: customData.customData,
1839
2618
  };
1840
2619
  this.emit("secondary command", result);
1841
2620
  delete this.customDataStaging[commandType];
1842
2621
  }
1843
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock return code: ${returnCode}`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, decoded: data.toString("hex"), bleCommandCode: functionTypeCommand[fac.getCommandCode()], returnCode: returnCode, channel: message.channel });
2622
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock return code: ${returnCode}`, {
2623
+ stationSN: this.rawStation.station_sn,
2624
+ commandIdName: types_1.CommandType[json.cmd],
2625
+ commandId: json.cmd,
2626
+ decoded: data.toString("hex"),
2627
+ bleCommandCode: functionTypeCommand[fac.getCommandCode()],
2628
+ returnCode: returnCode,
2629
+ channel: message.channel,
2630
+ });
1844
2631
  const parsePayload = new utils_2.ParsePayload(data.subarray(1));
1845
2632
  if (fac.getDataType() === types_1.SmartLockFunctionType.TYPE_2) {
1846
2633
  switch (fac.getCommandCode()) {
@@ -1865,17 +2652,32 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1865
2652
  //this.emit("parameter", message.channel, CommandType.CMD_SMARTLOCK_LOG, parsePayload.readInt8(BleParameterIndex.THIRTEEN).toString());
1866
2653
  break;
1867
2654
  case types_1.SmartLockBleCommandFunctionType2.ADD_PW:
1868
- if (customData && customData.customData && customData.customData.command && customData.customData.command.name === http_1.CommandName.DeviceAddUser) {
2655
+ if (customData &&
2656
+ customData.customData &&
2657
+ customData.customData.command &&
2658
+ customData.customData.command.name === http_1.CommandName.DeviceAddUser) {
1869
2659
  this.api.updateUserPassword(customData.customData.command.value?.deviceSN, customData.customData.command.value?.shortUserId, parsePayload.readStringHex(ble_1.BleParameterIndex.ONE), customData.customData.command.value?.schedule);
1870
2660
  }
1871
2661
  break;
1872
2662
  default:
1873
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, { stationSN: this.rawStation.station_sn, fac: fac.toString(), returnCode: returnCode, channel: customData?.channel, customData: customData?.customData });
2663
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, {
2664
+ stationSN: this.rawStation.station_sn,
2665
+ fac: fac.toString(),
2666
+ returnCode: returnCode,
2667
+ channel: customData?.channel,
2668
+ customData: customData?.customData,
2669
+ });
1874
2670
  break;
1875
2671
  }
1876
2672
  }
1877
2673
  else {
1878
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, { stationSN: this.rawStation.station_sn, fac: fac.toString(), returnCode: returnCode, channel: customData?.channel, customData: customData?.customData });
2674
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Not implemented`, {
2675
+ stationSN: this.rawStation.station_sn,
2676
+ fac: fac.toString(),
2677
+ returnCode: returnCode,
2678
+ channel: customData?.channel,
2679
+ customData: customData?.customData,
2680
+ });
1879
2681
  }
1880
2682
  }
1881
2683
  else {
@@ -1892,94 +2694,121 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1892
2694
  }
1893
2695
  catch (err) {
1894
2696
  const error = (0, error_1.ensureError)(err);
1895
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, payload: payload });
2697
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock Error`, {
2698
+ error: (0, utils_3.getError)(error),
2699
+ stationSN: this.rawStation.station_sn,
2700
+ commandIdName: types_1.CommandType[json.cmd],
2701
+ commandId: json.cmd,
2702
+ payload: payload,
2703
+ });
1896
2704
  }
1897
2705
  this._clearSecondaryCommandTimeout();
1898
2706
  this.sendQueuedMessage();
1899
2707
  }
1900
2708
  else {
1901
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Unexpected response`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, message: data.toString() });
2709
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Smart Lock - Unexpected response`, {
2710
+ stationSN: this.rawStation.station_sn,
2711
+ commandIdName: types_1.CommandType[json.cmd],
2712
+ commandId: json.cmd,
2713
+ message: data.toString(),
2714
+ });
1902
2715
  }
1903
2716
  }
1904
2717
  else if (device_1.Device.isSmartSafe(this.rawStation.device_type)) {
1905
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd });
2718
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe`, {
2719
+ stationSN: this.rawStation.station_sn,
2720
+ commandIdName: types_1.CommandType[json.cmd],
2721
+ commandId: json.cmd,
2722
+ });
1906
2723
  switch (json.cmd) {
1907
- case types_1.CommandType.CMD_SMARTSAFE_SETTINGS:
1908
- {
1909
- const payload = json.payload;
1910
- try {
1911
- const data = (0, utils_1.decodeSmartSafeData)(this.rawStation.station_sn, Buffer.from(payload.data, "hex"));
1912
- const returnCode = data.data.readInt8(0);
1913
- const customData = {
1914
- ...this.customDataStaging[payload.prj_id]
2724
+ case types_1.CommandType.CMD_SMARTSAFE_SETTINGS: {
2725
+ const payload = json.payload;
2726
+ try {
2727
+ const data = (0, utils_1.decodeSmartSafeData)(this.rawStation.station_sn, Buffer.from(payload.data, "hex"));
2728
+ const returnCode = data.data.readInt8(0);
2729
+ const customData = {
2730
+ ...this.customDataStaging[payload.prj_id],
2731
+ };
2732
+ if (this.customDataStaging[payload.prj_id]) {
2733
+ const result = {
2734
+ channel: customData.channel,
2735
+ command_type: payload.prj_id,
2736
+ return_code: returnCode,
2737
+ customData: customData.customData,
1915
2738
  };
1916
- if (this.customDataStaging[payload.prj_id]) {
1917
- const result = {
1918
- channel: customData.channel,
1919
- command_type: payload.prj_id,
1920
- return_code: returnCode,
1921
- customData: customData.customData
1922
- };
1923
- this.emit("secondary command", result);
1924
- delete this.customDataStaging[payload.prj_id];
1925
- }
1926
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe return code: ${data.data.readInt8(0)}`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, decoded: data, commandCode: types_1.SmartSafeCommandCode[data.commandCode], returnCode: returnCode, channel: customData?.channel, customData: customData?.customData });
1927
- }
1928
- catch (err) {
1929
- const error = (0, error_1.ensureError)(err);
1930
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, payload: payload });
2739
+ this.emit("secondary command", result);
2740
+ delete this.customDataStaging[payload.prj_id];
1931
2741
  }
1932
- this._clearSecondaryCommandTimeout();
1933
- this.sendQueuedMessage();
1934
- break;
2742
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe return code: ${data.data.readInt8(0)}`, {
2743
+ stationSN: this.rawStation.station_sn,
2744
+ commandIdName: types_1.CommandType[json.cmd],
2745
+ commandId: json.cmd,
2746
+ decoded: data,
2747
+ commandCode: types_1.SmartSafeCommandCode[data.commandCode],
2748
+ returnCode: returnCode,
2749
+ channel: customData?.channel,
2750
+ customData: customData?.customData,
2751
+ });
1935
2752
  }
1936
- case types_1.CommandType.CMD_SMARTSAFE_STATUS_UPDATE:
1937
- {
1938
- const payload = json.payload;
1939
- switch (payload.event_type) {
1940
- case types_3.SmartSafeEvent.LOCK_STATUS:
1941
- {
1942
- const eventValues = payload.event_value;
1943
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Status update - LOCK_STATUS`, { stationSN: this.rawStation.station_sn, eventValues: eventValues });
1944
- /*
1945
- type values:
1946
- 1: Unlocked by PIN
1947
- 2: Unlocked by User
1948
- 3: Unlocked by key
1949
- 4: Unlocked by App
1950
- 5: Unlocked by Dual Unlock
1951
- */
1952
- if (eventValues.action === 0) {
1953
- this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTSAFE_LOCK_STATUS, "0");
1954
- }
1955
- else if (eventValues.action === 1) {
1956
- this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTSAFE_LOCK_STATUS, "1");
1957
- }
1958
- else if (eventValues.action === 2) {
1959
- this.emit("jammed", message.channel);
1960
- }
1961
- else if (eventValues.action === 3) {
1962
- this.emit("low battery", message.channel);
1963
- }
1964
- break;
1965
- }
1966
- case types_3.SmartSafeEvent.SHAKE_ALARM:
1967
- this.emit("shake alarm", message.channel, payload.event_value);
1968
- break;
1969
- case types_3.SmartSafeEvent.ALARM_911:
1970
- this.emit("911 alarm", message.channel, payload.event_value);
1971
- break;
1972
- //case SmartSafeEvent.BATTERY_STATUS:
1973
- // break;
1974
- case types_3.SmartSafeEvent.INPUT_ERR_MAX:
1975
- this.emit("wrong try-protect alarm", message.channel);
1976
- break;
1977
- default:
1978
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Status update - Not implemented`, { stationSN: this.rawStation.station_sn, message: data.toString() });
1979
- break;
2753
+ catch (err) {
2754
+ const error = (0, error_1.ensureError)(err);
2755
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Error`, {
2756
+ error: (0, utils_3.getError)(error),
2757
+ stationSN: this.rawStation.station_sn,
2758
+ commandIdName: types_1.CommandType[json.cmd],
2759
+ commandId: json.cmd,
2760
+ payload: payload,
2761
+ });
2762
+ }
2763
+ this._clearSecondaryCommandTimeout();
2764
+ this.sendQueuedMessage();
2765
+ break;
2766
+ }
2767
+ case types_1.CommandType.CMD_SMARTSAFE_STATUS_UPDATE: {
2768
+ const payload = json.payload;
2769
+ switch (payload.event_type) {
2770
+ case types_3.SmartSafeEvent.LOCK_STATUS: {
2771
+ const eventValues = payload.event_value;
2772
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Status update - LOCK_STATUS`, { stationSN: this.rawStation.station_sn, eventValues: eventValues });
2773
+ /*
2774
+ type values:
2775
+ 1: Unlocked by PIN
2776
+ 2: Unlocked by User
2777
+ 3: Unlocked by key
2778
+ 4: Unlocked by App
2779
+ 5: Unlocked by Dual Unlock
2780
+ */
2781
+ if (eventValues.action === 0) {
2782
+ this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTSAFE_LOCK_STATUS, "0");
2783
+ }
2784
+ else if (eventValues.action === 1) {
2785
+ this.emit("parameter", message.channel, types_1.CommandType.CMD_SMARTSAFE_LOCK_STATUS, "1");
2786
+ }
2787
+ else if (eventValues.action === 2) {
2788
+ this.emit("jammed", message.channel);
2789
+ }
2790
+ else if (eventValues.action === 3) {
2791
+ this.emit("low battery", message.channel);
2792
+ }
2793
+ break;
1980
2794
  }
1981
- break;
2795
+ case types_3.SmartSafeEvent.SHAKE_ALARM:
2796
+ this.emit("shake alarm", message.channel, payload.event_value);
2797
+ break;
2798
+ case types_3.SmartSafeEvent.ALARM_911:
2799
+ this.emit("911 alarm", message.channel, payload.event_value);
2800
+ break;
2801
+ //case SmartSafeEvent.BATTERY_STATUS:
2802
+ // break;
2803
+ case types_3.SmartSafeEvent.INPUT_ERR_MAX:
2804
+ this.emit("wrong try-protect alarm", message.channel);
2805
+ break;
2806
+ default:
2807
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe Status update - Not implemented`, { stationSN: this.rawStation.station_sn, message: data.toString() });
2808
+ break;
1982
2809
  }
2810
+ break;
2811
+ }
1983
2812
  default:
1984
2813
  logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD SmartSafe - Not implemented`, { stationSN: this.rawStation.station_sn, message: data.toString() });
1985
2814
  break;
@@ -1996,7 +2825,12 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1996
2825
  else if (json.cmd === types_1.CommandType.CMD_CAMERA_GARAGE_DOOR_STATUS) {
1997
2826
  // {"cmd":7500,"payload":{"type":24,"notify_tag":"","door_id":2}}
1998
2827
  const payload = json.payload;
1999
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD GarageDoor Status update`, { stationSN: this.rawStation.station_sn, doorId: payload?.door_id, status: payload?.type, notify_tag: payload?.notify_tag });
2828
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD GarageDoor Status update`, {
2829
+ stationSN: this.rawStation.station_sn,
2830
+ doorId: payload?.door_id,
2831
+ status: payload?.type,
2832
+ notify_tag: payload?.notify_tag,
2833
+ });
2000
2834
  if (payload) {
2001
2835
  this.emit("garage door status", message.channel, payload.door_id, payload.type);
2002
2836
  }
@@ -2009,18 +2843,38 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2009
2843
  }
2010
2844
  }
2011
2845
  else {
2012
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Not implemented`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[json.cmd], commandId: json.cmd, message: data.toString() });
2846
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD - Not implemented`, {
2847
+ stationSN: this.rawStation.station_sn,
2848
+ commandIdName: types_1.CommandType[json.cmd],
2849
+ commandId: json.cmd,
2850
+ message: data.toString(),
2851
+ });
2013
2852
  }
2014
2853
  }
2015
2854
  }
2016
2855
  catch (err) {
2017
2856
  const error = (0, error_1.ensureError)(err);
2018
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString() } });
2857
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_NOTIFY_PAYLOAD Error`, {
2858
+ error: (0, utils_3.getError)(error),
2859
+ stationSN: this.rawStation.station_sn,
2860
+ message: {
2861
+ seqNo: message.seqNo,
2862
+ channel: message.channel,
2863
+ commandType: types_1.CommandType[message.commandId],
2864
+ signCode: message.signCode,
2865
+ type: message.type,
2866
+ dataType: types_1.P2PDataType[message.dataType],
2867
+ data: message.data.toString(),
2868
+ },
2869
+ });
2019
2870
  }
2020
2871
  break;
2021
2872
  case types_1.CommandType.CMD_GET_DELAY_ALARM:
2022
2873
  try {
2023
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DELAY_ALARM :`, { stationSN: this.rawStation.station_sn, payload: data.toString("hex") });
2874
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DELAY_ALARM :`, {
2875
+ stationSN: this.rawStation.station_sn,
2876
+ payload: data.toString("hex"),
2877
+ });
2024
2878
  //When the alarm is armed, CMD_GET_DELAY_ALARM is called with event data 0, so ignore it
2025
2879
  const alarmEventNumber = data.subarray(0, 4).readUInt32LE();
2026
2880
  const alarmDelay = data.subarray(4, 8).readUInt32LE();
@@ -2033,29 +2887,71 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2033
2887
  }
2034
2888
  catch (err) {
2035
2889
  const error = (0, error_1.ensureError)(err);
2036
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DELAY_ALARM - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2890
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_DELAY_ALARM - Error`, {
2891
+ error: (0, utils_3.getError)(error),
2892
+ stationSN: this.rawStation.station_sn,
2893
+ message: {
2894
+ seqNo: message.seqNo,
2895
+ channel: message.channel,
2896
+ commandType: types_1.CommandType[message.commandId],
2897
+ signCode: message.signCode,
2898
+ type: message.type,
2899
+ dataType: types_1.P2PDataType[message.dataType],
2900
+ data: message.data.toString("hex"),
2901
+ },
2902
+ });
2037
2903
  }
2038
2904
  break;
2039
2905
  case types_1.CommandType.CMD_SET_TONE_FILE:
2040
2906
  try {
2041
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_TONE_FILE :`, { stationSN: this.rawStation.station_sn, payload: data.toString("hex") });
2907
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_TONE_FILE :`, {
2908
+ stationSN: this.rawStation.station_sn,
2909
+ payload: data.toString("hex"),
2910
+ });
2042
2911
  const alarmEventNumber = data.subarray(0, 4).readUInt32LE();
2043
2912
  this.emit("alarm event", alarmEventNumber);
2044
2913
  }
2045
2914
  catch (err) {
2046
2915
  const error = (0, error_1.ensureError)(err);
2047
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_TONE_FILE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2916
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_TONE_FILE - Error`, {
2917
+ error: (0, utils_3.getError)(error),
2918
+ stationSN: this.rawStation.station_sn,
2919
+ message: {
2920
+ seqNo: message.seqNo,
2921
+ channel: message.channel,
2922
+ commandType: types_1.CommandType[message.commandId],
2923
+ signCode: message.signCode,
2924
+ type: message.type,
2925
+ dataType: types_1.P2PDataType[message.dataType],
2926
+ data: message.data.toString("hex"),
2927
+ },
2928
+ });
2048
2929
  }
2049
2930
  break;
2050
2931
  case types_1.CommandType.CMD_SET_SNOOZE_MODE:
2051
2932
  // Received for station managed devices when snooze time ends
2052
2933
  try {
2053
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_SNOOZE_MODE`, { stationSN: this.rawStation.station_sn, payload: Buffer.from(data.toString(), "base64").toString() });
2934
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_SNOOZE_MODE`, {
2935
+ stationSN: this.rawStation.station_sn,
2936
+ payload: Buffer.from(data.toString(), "base64").toString(),
2937
+ });
2054
2938
  this.emit("parameter", message.channel, types_1.CommandType.CMD_SET_SNOOZE_MODE, data.toString());
2055
2939
  }
2056
2940
  catch (err) {
2057
2941
  const error = (0, error_1.ensureError)(err);
2058
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_SNOOZE_MODE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2942
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_SET_SNOOZE_MODE - Error`, {
2943
+ error: (0, utils_3.getError)(error),
2944
+ stationSN: this.rawStation.station_sn,
2945
+ message: {
2946
+ seqNo: message.seqNo,
2947
+ channel: message.channel,
2948
+ commandType: types_1.CommandType[message.commandId],
2949
+ signCode: message.signCode,
2950
+ type: message.type,
2951
+ dataType: types_1.P2PDataType[message.dataType],
2952
+ data: message.data.toString("hex"),
2953
+ },
2954
+ });
2059
2955
  }
2060
2956
  break;
2061
2957
  case types_1.CommandType.CMD_PING:
@@ -2065,57 +2961,89 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2065
2961
  // Received data for preview image download
2066
2962
  try {
2067
2963
  const str = (0, utils_1.getNullTerminatedString)(data, "utf8");
2068
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE_IMAGE`, { stationSN: this.rawStation.station_sn, message: str });
2964
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE_IMAGE`, {
2965
+ stationSN: this.rawStation.station_sn,
2966
+ message: str,
2967
+ });
2069
2968
  const image = (0, utils_3.parseJSON)(str, logging_1.rootP2PLogger);
2070
2969
  this.emit("image download", image.file, (0, utils_2.decodeImage)(this.rawStation.p2p_did, Buffer.from(image.content, "base64")));
2071
2970
  }
2072
2971
  catch (err) {
2073
2972
  const error = (0, error_1.ensureError)(err);
2074
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE_IMAGE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString() } });
2973
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE_IMAGE - Error`, {
2974
+ error: (0, utils_3.getError)(error),
2975
+ stationSN: this.rawStation.station_sn,
2976
+ message: {
2977
+ seqNo: message.seqNo,
2978
+ channel: message.channel,
2979
+ commandType: types_1.CommandType[message.commandId],
2980
+ signCode: message.signCode,
2981
+ type: message.type,
2982
+ dataType: types_1.P2PDataType[message.dataType],
2983
+ data: message.data.toString(),
2984
+ },
2985
+ });
2075
2986
  }
2076
2987
  break;
2077
2988
  case types_1.CommandType.CMD_GET_TFCARD_STATUS:
2078
2989
  try {
2079
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_TFCARD_STATUS :`, { stationSN: this.rawStation.station_sn, payload: data.toString("hex") });
2990
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_TFCARD_STATUS :`, {
2991
+ stationSN: this.rawStation.station_sn,
2992
+ payload: data.toString("hex"),
2993
+ });
2080
2994
  const tfCardStatus = data.subarray(0, 4).readUInt32LE();
2081
2995
  this.emit("tfcard status", message.channel, tfCardStatus);
2082
2996
  }
2083
2997
  catch (err) {
2084
2998
  const error = (0, error_1.ensureError)(err);
2085
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_TFCARD_STATUS - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
2999
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GET_TFCARD_STATUS - Error`, {
3000
+ error: (0, utils_3.getError)(error),
3001
+ stationSN: this.rawStation.station_sn,
3002
+ message: {
3003
+ seqNo: message.seqNo,
3004
+ channel: message.channel,
3005
+ commandType: types_1.CommandType[message.commandId],
3006
+ signCode: message.signCode,
3007
+ type: message.type,
3008
+ dataType: types_1.P2PDataType[message.dataType],
3009
+ data: message.data.toString("hex"),
3010
+ },
3011
+ });
2086
3012
  }
2087
3013
  break;
2088
3014
  case types_1.CommandType.CMD_DATABASE:
2089
3015
  try {
2090
3016
  const str = (0, utils_1.getNullTerminatedString)(data, "utf8");
2091
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE :`, { stationSN: this.rawStation.station_sn, payload: str });
3017
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE :`, {
3018
+ stationSN: this.rawStation.station_sn,
3019
+ payload: str,
3020
+ });
2092
3021
  const databaseResponse = (0, utils_3.parseJSON)(str, logging_1.rootP2PLogger);
2093
3022
  switch (databaseResponse.cmd) {
2094
- case types_1.CommandType.CMD_DATABASE_QUERY_LATEST_INFO:
2095
- {
2096
- let data = [];
2097
- if (databaseResponse.data !== undefined && databaseResponse.data !== "[]")
2098
- data = databaseResponse.data;
2099
- const result = [];
2100
- for (const record of data) {
2101
- if (record.payload.crop_hb3_path !== "") {
2102
- result.push({
2103
- device_sn: record.device_sn,
2104
- event_count: record.payload.event_count,
2105
- crop_local_path: record.payload.crop_hb3_path
2106
- });
2107
- }
2108
- else {
2109
- result.push({
2110
- device_sn: record.device_sn,
2111
- event_count: record.payload.event_count,
2112
- crop_cloud_path: record.payload.crop_cloud_path
2113
- });
2114
- }
3023
+ case types_1.CommandType.CMD_DATABASE_QUERY_LATEST_INFO: {
3024
+ let data = [];
3025
+ if (databaseResponse.data !== undefined && databaseResponse.data !== "[]")
3026
+ data = databaseResponse.data;
3027
+ const result = [];
3028
+ for (const record of data) {
3029
+ if (record.payload.crop_hb3_path !== "") {
3030
+ result.push({
3031
+ device_sn: record.device_sn,
3032
+ event_count: record.payload.event_count,
3033
+ crop_local_path: record.payload.crop_hb3_path,
3034
+ });
3035
+ }
3036
+ else {
3037
+ result.push({
3038
+ device_sn: record.device_sn,
3039
+ event_count: record.payload.event_count,
3040
+ crop_cloud_path: record.payload.crop_cloud_path,
3041
+ });
2115
3042
  }
2116
- this.emit("database query latest", databaseResponse.mIntRet, result);
2117
- break;
2118
3043
  }
3044
+ this.emit("database query latest", databaseResponse.mIntRet, result);
3045
+ break;
3046
+ }
2119
3047
  case types_1.CommandType.CMD_DATABASE_COUNT_BY_DATE: {
2120
3048
  let data = [];
2121
3049
  if (databaseResponse.data !== undefined && databaseResponse.data !== "[]")
@@ -2124,7 +3052,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2124
3052
  for (const record of data) {
2125
3053
  result.push({
2126
3054
  day: parse(record.days, "YYYYMMDD"),
2127
- count: record.count
3055
+ count: record.count,
2128
3056
  });
2129
3057
  }
2130
3058
  this.emit("database count by date", databaseResponse.mIntRet, result);
@@ -2189,7 +3117,8 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2189
3117
  person_id: tableRecord.person_id,
2190
3118
  crop_path: tableRecord.crop_path,
2191
3119
  event_time: parse(tableRecord.event_time, "YYYY-MM-DD HH:mm:ss"),
2192
- person_recog_flag: tableRecord.person_recog_flag,
3120
+ person_recog_flag: tableRecord
3121
+ .person_recog_flag,
2193
3122
  crop_pic_quality: tableRecord.crop_pic_quality,
2194
3123
  pic_marking_flag: tableRecord.pic_marking_flag,
2195
3124
  group_id: tableRecord.group_id,
@@ -2211,6 +3140,33 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2211
3140
  this.emit("database query local", databaseResponse.mIntRet, Array.from(result.values()));
2212
3141
  break;
2213
3142
  }
3143
+ case types_1.CommandType.CMD_DATABASE_QUERY_BY_DATE: {
3144
+ let data = [];
3145
+ if (databaseResponse.data !== undefined && databaseResponse.data !== "[]")
3146
+ data = databaseResponse.data;
3147
+ const result = [];
3148
+ for (const record of data) {
3149
+ result.push({
3150
+ device_sn: record.device_sn,
3151
+ device_type: record.device_type,
3152
+ start_time: parse(record.start_time, "YYYY-MM-DD HH:mm:ss"),
3153
+ end_time: parse(record.end_time, "YYYY-MM-DD HH:mm:ss"),
3154
+ storage_path: record.storage_path,
3155
+ thumb_path: record.thumb_path,
3156
+ cipher_id: record.cipher_id,
3157
+ folder_size: record.folder_size,
3158
+ frame_num: record.frame_num,
3159
+ trigger_type: record.trigger_type,
3160
+ video_type: record.video_type,
3161
+ record_id: record.record_id,
3162
+ station_sn: record.station_sn,
3163
+ storage_type: record.storage_type,
3164
+ storage_cloud: record.storage_cloud,
3165
+ });
3166
+ }
3167
+ this.emit("database query by date", databaseResponse.mIntRet, result);
3168
+ break;
3169
+ }
2214
3170
  case types_1.CommandType.CMD_DATABASE_DELETE: {
2215
3171
  const data = databaseResponse.data;
2216
3172
  let failed_delete = [];
@@ -2220,27 +3176,98 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2220
3176
  break;
2221
3177
  }
2222
3178
  default:
2223
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented - CMD_DATABASE message`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, databaseResponse: databaseResponse });
3179
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented - CMD_DATABASE message`, {
3180
+ stationSN: this.rawStation.station_sn,
3181
+ commandIdName: types_1.CommandType[message.commandId],
3182
+ commandId: message.commandId,
3183
+ channel: message.channel,
3184
+ databaseResponse: databaseResponse,
3185
+ });
2224
3186
  break;
2225
3187
  }
2226
3188
  }
2227
3189
  catch (err) {
2228
3190
  const error = (0, error_1.ensureError)(err);
2229
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString() } });
3191
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_DATABASE - Error`, {
3192
+ error: (0, utils_3.getError)(error),
3193
+ stationSN: this.rawStation.station_sn,
3194
+ message: {
3195
+ seqNo: message.seqNo,
3196
+ channel: message.channel,
3197
+ commandType: types_1.CommandType[message.commandId],
3198
+ signCode: message.signCode,
3199
+ type: message.type,
3200
+ dataType: types_1.P2PDataType[message.dataType],
3201
+ data: message.data.toString(),
3202
+ },
3203
+ });
2230
3204
  }
2231
3205
  break;
2232
3206
  case types_1.CommandType.CMD_GATEWAYINFO:
2233
3207
  const cipherID = data.subarray(0, 2).readUInt16LE();
2234
3208
  //const unknownNumber = data.subarray(2, 2).readUInt16LE();
2235
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - cipherID`, { stationSN: this.rawStation.station_sn, channel: message.channel, data: data.toString("hex"), cipherID: cipherID });
2236
- const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(data.subarray(4));
2237
- this.api.getCipher(/*this.rawStation.station_sn, */ cipherID, this.rawStation.member.admin_user_id).then((cipher) => {
2238
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - get cipher with cipherID`, { stationSN: this.rawStation.station_sn, channel: message.channel, data: data.toString("hex"), cipherID: cipherID, cipher: JSON.stringify(cipher) });
3209
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - cipherID`, {
3210
+ stationSN: this.rawStation.station_sn,
3211
+ channel: message.channel,
3212
+ data: data.toString("hex"),
3213
+ cipherID: cipherID,
3214
+ });
3215
+ // Keep full raw buffer for ECDH — readNullTerminatedBuffer truncates binary ECIES envelopes at 0x00 bytes
3216
+ const rawEncryptedKey = data.subarray(4);
3217
+ const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(rawEncryptedKey);
3218
+ this.api
3219
+ .getCipher(/*this.rawStation.station_sn, */ cipherID, this.rawStation.member.admin_user_id)
3220
+ .then((cipher) => {
3221
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - get cipher with cipherID`, {
3222
+ stationSN: this.rawStation.station_sn,
3223
+ channel: message.channel,
3224
+ data: data.toString("hex"),
3225
+ cipherID: cipherID,
3226
+ cipher: JSON.stringify(cipher),
3227
+ });
2239
3228
  if (cipher !== undefined) {
2240
- this.encryption = types_1.EncryptionType.LEVEL_2;
2241
- const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
2242
- this.p2pKey = rsa.decrypt(encryptedKey);
2243
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - set encryption level 2`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3229
+ // Try RSA first
3230
+ try {
3231
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3232
+ const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
3233
+ this.p2pKey = rsa.decrypt(encryptedKey);
3234
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA success - set encryption level 2`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3235
+ }
3236
+ catch (rsaErr) {
3237
+ const rsaError = (0, error_1.ensureError)(rsaErr);
3238
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA decrypt failed, trying ECDH`, {
3239
+ error: (0, utils_3.getError)(rsaError),
3240
+ stationSN: this.rawStation.station_sn,
3241
+ hasEccKey: !!cipher.ecc_private_key,
3242
+ });
3243
+ // Try ECDH if ecc_private_key is available
3244
+ if (cipher.ecc_private_key) {
3245
+ try {
3246
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3247
+ this.p2pKey = (0, utils_1.decryptP2PKeyECDH)(rawEncryptedKey, cipher.ecc_private_key);
3248
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH success - set encryption level 2`, {
3249
+ stationSN: this.rawStation.station_sn,
3250
+ key: this.p2pKey.toString("hex"),
3251
+ keyLength: this.p2pKey.length,
3252
+ });
3253
+ }
3254
+ catch (ecdhErr) {
3255
+ const ecdhError = (0, error_1.ensureError)(ecdhErr);
3256
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH also failed, falling back to Level 1`, {
3257
+ error: (0, utils_3.getError)(ecdhError),
3258
+ stationSN: this.rawStation.station_sn,
3259
+ });
3260
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3261
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3262
+ }
3263
+ }
3264
+ else {
3265
+ // No ECC key available, fall back to Level 1
3266
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3267
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3268
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - No ECC key, set encryption level 1`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3269
+ }
3270
+ }
2244
3271
  }
2245
3272
  else {
2246
3273
  this.encryption = types_1.EncryptionType.LEVEL_1;
@@ -2256,11 +3283,25 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2256
3283
  this.p2pDataSeqNumber--;
2257
3284
  this.messageStates.delete(message.seqNo);
2258
3285
  this.sendQueuedMessage();
2259
- }).catch((err) => {
3286
+ })
3287
+ .catch((err) => {
2260
3288
  const error = (0, error_1.ensureError)(err);
2261
3289
  this.encryption = types_1.EncryptionType.LEVEL_1;
2262
3290
  this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
2263
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - set encryption level 1 (fallback)`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") }, key: this.p2pKey.toString("hex") });
3291
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - set encryption level 1 (fallback)`, {
3292
+ error: (0, utils_3.getError)(error),
3293
+ stationSN: this.rawStation.station_sn,
3294
+ message: {
3295
+ seqNo: message.seqNo,
3296
+ channel: message.channel,
3297
+ commandType: types_1.CommandType[message.commandId],
3298
+ signCode: message.signCode,
3299
+ type: message.type,
3300
+ dataType: types_1.P2PDataType[message.dataType],
3301
+ data: message.data.toString("hex"),
3302
+ },
3303
+ key: this.p2pKey.toString("hex"),
3304
+ });
2264
3305
  this._clearTimeout(this.messageStates.get(message.seqNo)?.timeout);
2265
3306
  this.p2pSeqMapping.forEach((value, key, map) => {
2266
3307
  if (value === message.seqNo) {
@@ -2273,13 +3314,31 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2273
3314
  });
2274
3315
  break;
2275
3316
  default:
2276
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented`, { stationSN: this.rawStation.station_sn, commandIdName: types_1.CommandType[message.commandId], commandId: message.commandId, channel: message.channel, data: data.toString("hex") });
3317
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Not implemented`, {
3318
+ stationSN: this.rawStation.station_sn,
3319
+ commandIdName: types_1.CommandType[message.commandId],
3320
+ commandId: message.commandId,
3321
+ channel: message.channel,
3322
+ data: data.toString("hex"),
3323
+ });
2277
3324
  break;
2278
3325
  }
2279
3326
  }
2280
3327
  catch (err) {
2281
3328
  const error = (0, error_1.ensureError)(err);
2282
- logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn, message: { seqNo: message.seqNo, channel: message.channel, commandType: types_1.CommandType[message.commandId], signCode: message.signCode, type: message.type, dataType: types_1.P2PDataType[message.dataType], data: message.data.toString("hex") } });
3329
+ logging_1.rootP2PLogger.error(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Error`, {
3330
+ error: (0, utils_3.getError)(error),
3331
+ stationSN: this.rawStation.station_sn,
3332
+ message: {
3333
+ seqNo: message.seqNo,
3334
+ channel: message.channel,
3335
+ commandType: types_1.CommandType[message.commandId],
3336
+ signCode: message.signCode,
3337
+ type: message.type,
3338
+ dataType: types_1.P2PDataType[message.dataType],
3339
+ data: message.data.toString("hex"),
3340
+ },
3341
+ });
2283
3342
  }
2284
3343
  }
2285
3344
  async sendAck(address, dataType, seqNo) {
@@ -2351,7 +3410,9 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2351
3410
  }, this.getHeartbeatInterval());
2352
3411
  }
2353
3412
  else {
2354
- logging_1.rootP2PLogger.debug(`Heartbeat not activated because no connection is present!`, { stationSN: this.rawStation.station_sn });
3413
+ logging_1.rootP2PLogger.debug(`Heartbeat not activated because no connection is present!`, {
3414
+ stationSN: this.rawStation.station_sn,
3415
+ });
2355
3416
  }
2356
3417
  }
2357
3418
  scheduleP2PKeepalive() {
@@ -2363,7 +3424,9 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2363
3424
  this.closeEnergySavingDevice();
2364
3425
  }
2365
3426
  else {
2366
- logging_1.rootP2PLogger.debug(`P2P keepalive not activated because no connection is present`, { stationSN: this.rawStation.station_sn });
3427
+ logging_1.rootP2PLogger.debug(`P2P keepalive not activated because no connection is present`, {
3428
+ stationSN: this.rawStation.station_sn,
3429
+ });
2367
3430
  }
2368
3431
  }
2369
3432
  getDownloadRSAPrivateKey() {
@@ -2383,27 +3446,29 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2383
3446
  this.currentMessageState[datatype].audioStream?.destroy();
2384
3447
  this.currentMessageState[datatype].videoStream = null;
2385
3448
  this.currentMessageState[datatype].audioStream = null;
2386
- this.currentMessageState[datatype].videoStream = new stream_1.Readable({ autoDestroy: true,
3449
+ this.currentMessageState[datatype].videoStream = new stream_1.Readable({
3450
+ autoDestroy: true,
2387
3451
  read() { } /*,
2388
-
2389
- destroy(this, error, _callback) {
2390
- if (error) {
2391
- this.emit("error", error);
2392
- }
2393
- this.emit("end");
2394
- this.emit("close");
2395
- }*/
3452
+
3453
+ destroy(this, error, _callback) {
3454
+ if (error) {
3455
+ this.emit("error", error);
3456
+ }
3457
+ this.emit("end");
3458
+ this.emit("close");
3459
+ }*/,
2396
3460
  });
2397
- this.currentMessageState[datatype].audioStream = new stream_1.Readable({ autoDestroy: true,
3461
+ this.currentMessageState[datatype].audioStream = new stream_1.Readable({
3462
+ autoDestroy: true,
2398
3463
  read() { } /*,
2399
-
2400
- destroy(this, error, _callback) {
2401
- if (error) {
2402
- this.emit("error", error);
2403
- }
2404
- this.emit("end");
2405
- this.emit("close");
2406
- }*/
3464
+
3465
+ destroy(this, error, _callback) {
3466
+ if (error) {
3467
+ this.emit("error", error);
3468
+ }
3469
+ this.emit("end");
3470
+ this.emit("close");
3471
+ }*/,
2407
3472
  });
2408
3473
  this.currentMessageState[datatype].p2pStreaming = false;
2409
3474
  if (this.currentMessageState[datatype].waitForSeqNoTimeout !== undefined) {
@@ -2423,11 +3488,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2423
3488
  this.sendCommandWithInt({
2424
3489
  commandType: types_1.CommandType.CMD_STOP_REALTIME_MEDIA,
2425
3490
  value: this.currentMessageState[datatype].p2pStreamChannel,
2426
- channel: this.currentMessageState[datatype].p2pStreamChannel
3491
+ channel: this.currentMessageState[datatype].p2pStreamChannel,
2427
3492
  }, {
2428
3493
  command: {
2429
- name: http_1.CommandName.DeviceStopLivestream
2430
- }
3494
+ name: http_1.CommandName.DeviceStopLivestream,
3495
+ },
2431
3496
  });
2432
3497
  break;
2433
3498
  case types_1.P2PDataType.BINARY:
@@ -2435,11 +3500,11 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2435
3500
  commandType: types_1.CommandType.CMD_DOWNLOAD_CANCEL,
2436
3501
  value: this.currentMessageState[datatype].p2pStreamChannel,
2437
3502
  strValueSub: this.rawStation.member.admin_user_id,
2438
- channel: this.currentMessageState[datatype].p2pStreamChannel
3503
+ channel: this.currentMessageState[datatype].p2pStreamChannel,
2439
3504
  }, {
2440
3505
  command: {
2441
- name: http_1.CommandName.DeviceCancelDownload
2442
- }
3506
+ name: http_1.CommandName.DeviceCancelDownload,
3507
+ },
2443
3508
  });
2444
3509
  break;
2445
3510
  }
@@ -2502,7 +3567,9 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2502
3567
  return false;
2503
3568
  }
2504
3569
  isRTSPLiveStreaming(channel) {
2505
- return this.currentMessageState[types_1.P2PDataType.DATA].rtspStreaming[channel] ? this.currentMessageState[types_1.P2PDataType.DATA].rtspStreaming[channel] : false;
3570
+ return this.currentMessageState[types_1.P2PDataType.DATA].rtspStreaming[channel]
3571
+ ? this.currentMessageState[types_1.P2PDataType.DATA].rtspStreaming[channel]
3572
+ : false;
2506
3573
  }
2507
3574
  isDownloading(channel) {
2508
3575
  return this.isStreaming(channel, types_1.P2PDataType.BINARY);
@@ -2542,38 +3609,53 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2542
3609
  const data = {
2543
3610
  invalid_dsks: {},
2544
3611
  station_sns: [this.rawStation.station_sn],
2545
- transaction: `${new Date().getTime()}`
3612
+ transaction: `${new Date().getTime()}`,
2546
3613
  };
2547
3614
  data.invalid_dsks[this.rawStation.station_sn] = "";
2548
3615
  const response = await this.api.request({
2549
3616
  method: "post",
2550
3617
  endpoint: "v1/app/equipment/get_dsk_keys",
2551
- data: data
3618
+ data: data,
2552
3619
  });
2553
3620
  logging_1.rootP2PLogger.debug(`Get DSK keys - Response:`, { stationSN: this.rawStation.station_sn, data: response.data });
2554
3621
  if (response.status == 200) {
2555
3622
  const result = response.data;
2556
3623
  if (result.code == 0) {
2557
3624
  const dataresult = result.data;
2558
- dataresult.dsk_keys.forEach(key => {
3625
+ dataresult.dsk_keys.forEach((key) => {
2559
3626
  if (key.station_sn == this.rawStation.station_sn) {
2560
3627
  this.dskKey = key.dsk_key;
2561
3628
  this.dskExpiration = new Date(key.expiration * 1000);
2562
- logging_1.rootP2PLogger.debug(`Get DSK keys - received key and expiration`, { stationSN: this.rawStation.station_sn, dskKey: this.dskKey, dskExpiration: this.dskExpiration });
3629
+ logging_1.rootP2PLogger.debug(`Get DSK keys - received key and expiration`, {
3630
+ stationSN: this.rawStation.station_sn,
3631
+ dskKey: this.dskKey,
3632
+ dskExpiration: this.dskExpiration,
3633
+ });
2563
3634
  }
2564
3635
  });
2565
3636
  }
2566
3637
  else {
2567
- logging_1.rootP2PLogger.error(`Get DSK keys - Response code not ok`, { stationSN: this.rawStation.station_sn, code: result.code, msg: result.msg });
3638
+ logging_1.rootP2PLogger.error(`Get DSK keys - Response code not ok`, {
3639
+ stationSN: this.rawStation.station_sn,
3640
+ code: result.code,
3641
+ msg: result.msg,
3642
+ });
2568
3643
  }
2569
3644
  }
2570
3645
  else {
2571
- logging_1.rootP2PLogger.error(`Get DSK keys - Status return code not 200`, { stationSN: this.rawStation.station_sn, status: response.status, statusText: response.statusText });
3646
+ logging_1.rootP2PLogger.error(`Get DSK keys - Status return code not 200`, {
3647
+ stationSN: this.rawStation.station_sn,
3648
+ status: response.status,
3649
+ statusText: response.statusText,
3650
+ });
2572
3651
  }
2573
3652
  }
2574
3653
  catch (err) {
2575
3654
  const error = (0, error_1.ensureError)(err);
2576
- logging_1.rootP2PLogger.error(`Get DSK keys - Generic Error`, { error: (0, utils_3.getError)(error), stationSN: this.rawStation.station_sn });
3655
+ logging_1.rootP2PLogger.error(`Get DSK keys - Generic Error`, {
3656
+ error: (0, utils_3.getError)(error),
3657
+ stationSN: this.rawStation.station_sn,
3658
+ });
2577
3659
  }
2578
3660
  }
2579
3661
  }
@@ -2595,15 +3677,21 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2595
3677
  for (const device of this.rawStation.devices) {
2596
3678
  this.deviceSNs[device.device_channel] = {
2597
3679
  sn: device.device_sn,
2598
- adminUserId: this.rawStation.member.admin_user_id
3680
+ adminUserId: this.rawStation.member.admin_user_id,
2599
3681
  };
2600
3682
  }
2601
3683
  }
2602
3684
  initializeTalkbackStream(channel = 0) {
2603
3685
  this.talkbackStream = new talkback_1.TalkbackStream();
2604
- this.talkbackStream.on("data", (audioData) => { this.sendTalkbackAudioFrame(audioData, channel); });
2605
- this.talkbackStream.on("error", (error) => { this.onTalkbackStreamError(error); });
2606
- this.talkbackStream.on("close", () => { this.onTalkbackStreamClose(); });
3686
+ this.talkbackStream.on("data", (audioData) => {
3687
+ this.sendTalkbackAudioFrame(audioData, channel);
3688
+ });
3689
+ this.talkbackStream.on("error", (error) => {
3690
+ this.onTalkbackStreamError(error);
3691
+ });
3692
+ this.talkbackStream.on("close", () => {
3693
+ this.onTalkbackStreamClose();
3694
+ });
2607
3695
  }
2608
3696
  sendTalkbackAudioFrame(audioData, channel) {
2609
3697
  const messageHeader = (0, utils_1.buildCommandHeader)(this.videoSeqNumber, types_1.CommandType.CMD_AUDIO_FRAME, types_1.P2PDataTypeHeader.VIDEO);
@@ -2613,7 +3701,7 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2613
3701
  sequence: this.videoSeqNumber,
2614
3702
  channel: channel,
2615
3703
  data: messageData,
2616
- retries: 0
3704
+ retries: 0,
2617
3705
  };
2618
3706
  this.videoSeqNumber = this._incrementSequence(this.videoSeqNumber);
2619
3707
  this._sendVideoData(message);
@@ -2630,9 +3718,16 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2630
3718
  message.retries++;
2631
3719
  }
2632
3720
  else {
2633
- logging_1.rootP2PLogger.error(`Sending video data - Max send video data retries ${this.messageVideoStates.get(message.sequence)?.retries} reached. Discard data.`, { stationSN: this.rawStation.station_sn, sequence: message.sequence, channel: message.channel, retries: message.retries });
3721
+ logging_1.rootP2PLogger.error(`Sending video data - Max send video data retries ${this.messageVideoStates.get(message.sequence)?.retries} reached. Discard data.`, {
3722
+ stationSN: this.rawStation.station_sn,
3723
+ sequence: message.sequence,
3724
+ channel: message.channel,
3725
+ retries: message.retries,
3726
+ });
2634
3727
  this.messageVideoStates.delete(message.sequence);
2635
- this.emit("talkback error", message.channel, new error_1.TalkbackError("Max send video data retries reached. Discard data packet.", { context: { station: this.rawStation.station_sn, channel: message.channel, retries: message.retries } }));
3728
+ this.emit("talkback error", message.channel, new error_1.TalkbackError("Max send video data retries reached. Discard data packet.", {
3729
+ context: { station: this.rawStation.station_sn, channel: message.channel, retries: message.retries },
3730
+ }));
2636
3731
  return;
2637
3732
  }
2638
3733
  message = message;
@@ -2640,7 +3735,13 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
2640
3735
  this._sendVideoData(message);
2641
3736
  }, this.MAX_AKNOWLEDGE_TIMEOUT);
2642
3737
  this.messageVideoStates.set(message.sequence, message);
2643
- logging_1.rootP2PLogger.trace("Sending p2p video data...", { station: this.rawStation.station_sn, sequence: message.sequence, channel: message.channel, retries: message.retries, messageVideoStatesSize: this.messageVideoStates.size });
3738
+ logging_1.rootP2PLogger.trace("Sending p2p video data...", {
3739
+ station: this.rawStation.station_sn,
3740
+ sequence: message.sequence,
3741
+ channel: message.channel,
3742
+ retries: message.retries,
3743
+ messageVideoStatesSize: this.messageVideoStates.size,
3744
+ });
2644
3745
  await this.sendMessage(`Send video data`, this.connectAddress, types_1.RequestMessageType.DATA, message.data);
2645
3746
  }
2646
3747
  isTalkbackOngoing(channel) {