mqtt-devices-parser 1.0.29 → 1.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changelog.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.31
4
+ fix: handleFotaSuccess, make it public
5
+
6
+ ## 1.0.30
7
+ fix: tests, recover "settings" parsing, deal with new call addMqttMsgLog
8
+ doc: update Release
9
+ fix: handle ER_DUP_ENTRY race condition in fota.update() (#14)
10
+ perf(src/device/device): clean and parse mqtt payload
11
+
3
12
  ## 1.0.29
4
13
  fix(models/sensors): allow null model_id
5
14
  fix: synching mqtt topics
@@ -11,7 +20,7 @@
11
20
  doc(device): recover readme add test.md file
12
21
  fix(models/variants): remove unique true from column name
13
22
  ci: fix vulnerabilities
14
-
23
+
15
24
  ## 1.0.27
16
25
  feat(db): adds variants table
17
26
 
package/Release.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  ## Before release
4
4
 
5
+ run npm test
5
6
  increases version on package.json
6
- run npm install
7
+ run npm install (check vulnerabilities)
7
8
  edit changelog
8
9
  commit and push to repo
9
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mqtt-devices-parser",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/db/fota.js CHANGED
@@ -96,7 +96,14 @@ var self = module.exports = {
96
96
  return resolve(rows[0]);
97
97
  })
98
98
  .catch(error => {
99
- return reject(error);
99
+ if(error.code === 'ER_DUP_ENTRY'){
100
+ let filter = { device_id : deviceId };
101
+ $.db.update("fota",obj,filter)
102
+ .then(rows => { return resolve(rows[0]); })
103
+ .catch(err => { return reject(err); });
104
+ }else{
105
+ return reject(error);
106
+ }
100
107
  });
101
108
 
102
109
  }else{
@@ -335,7 +335,6 @@ var self = module.exports = {
335
335
 
336
336
  $.db_device.updateLocalTopic(dbTopic.id,payload)
337
337
  .then(()=>{
338
- // set as not synched
339
338
  if(isDeepStrictEqual(payload, dbTopic?.remoteData))
340
339
  $.db_device.setSynchedTopic(dbTopic.id,true);
341
340
  else
@@ -371,6 +370,24 @@ var self = module.exports = {
371
370
  }
372
371
  },
373
372
 
373
+ handleFotaSuccess: async (deviceId)=>{
374
+ let object = {
375
+ "nAttempts" : 0,
376
+ "fUpdate" : 0,
377
+ }
378
+ $.db_fota.update(deviceId,object);
379
+ object = {
380
+ success : 1,
381
+ }
382
+ $.db_fota.updateLog(deviceId,object);
383
+ },
384
+
385
+ handleFotaError: async (deviceId, error)=>{
386
+ let object = {
387
+ error : error,
388
+ }
389
+ $.db_fota.updateLog(deviceId,object);
390
+ }
374
391
  }
375
392
 
376
393
  async function parseLwm2mMessage(client, project_name, device, topic, payload, action){
@@ -420,6 +437,7 @@ async function parseMqttMessage(client, project_name, device, topic, payload, re
420
437
 
421
438
  try{
422
439
  payload = JSON.parse(payload);
440
+ payload = cleanAndParse(payload);
423
441
  }catch(error){}
424
442
 
425
443
  let word = $.parser.getFirstWord(topic);
@@ -480,14 +498,14 @@ async function parseMqttMessage(client, project_name, device, topic, payload, re
480
498
  if (payload != null && payload != device?.version) {
481
499
  $.db_device.addLog(device.id,"version",payload);
482
500
  $.db_device.update(device.id, "version", payload);
483
- handleFotaSuccess(device.id);
501
+ self.handleFotaSuccess(device.id);
484
502
  }
485
503
  break;
486
504
  case "app_version":
487
505
  if (payload != null && payload != device?.app_version) {
488
506
  $.db_device.addLog(device.id,"app_version",payload);
489
507
  $.db_device.update(device.id, "app_version", payload);
490
- handleFotaSuccess(device.id);
508
+ self.handleFotaSuccess(device.id);
491
509
  }
492
510
  break;
493
511
  case "fw":
@@ -515,7 +533,6 @@ async function parseMqttMessage(client, project_name, device, topic, payload, re
515
533
  }
516
534
  break;
517
535
  case "settings": // deprecated
518
- break;
519
536
  if(topic.endsWith("/set")){
520
537
  updateLocalSettings(device,topic,payload);
521
538
  }else{
@@ -707,23 +724,18 @@ async function synchSettings(device,key){
707
724
  */
708
725
  }
709
726
 
710
- function handleFotaSuccess (deviceId){
711
- let object = {
712
- "nAttempts" : 0,
713
- "fUpdate" : 0,
714
- }
715
- $.db_fota.update(deviceId,object);
716
- object = {
717
- success : 1,
718
- }
719
- $.db_fota.updateLog(deviceId,object);
720
- }
721
-
722
- function handleFotaError (deviceId, error){
723
- let object = {
724
- error : error,
727
+ function cleanAndParse(payload) {
728
+ if (typeof payload === 'string') {
729
+ let cleaned = payload.trim();
730
+ if (cleaned.startsWith('"') && cleaned.endsWith('"')) {
731
+ cleaned = cleaned.slice(1, -1);
732
+ }
733
+ cleaned = cleaned.replace(/\\"/g, '"');
734
+ try {
735
+ return JSON.parse(cleaned);
736
+ } catch (e) {
737
+ return payload; // fallback: return original if still invalid
738
+ }
725
739
  }
726
- $.db_fota.updateLog(deviceId,object);
727
- }
728
-
729
-
740
+ return payload;
741
+ }
@@ -29,7 +29,8 @@ global.$ = {
29
29
  updateLocalTopic: jest.fn(),
30
30
  setSynchedTopic: jest.fn(),
31
31
  updateRemoteTopic: jest.fn(),
32
- getAssociatedDevice: jest.fn()
32
+ getAssociatedDevice: jest.fn(),
33
+ addMqttMsgLog: jest.fn()
33
34
  },
34
35
  db_model: {
35
36
  getByName: jest.fn(),