mqtt-devices-parser 1.0.29 → 1.0.30

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,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.30
4
+ fix: tests, recover "settings" parsing, deal with new call addMqttMsgLog
5
+ doc: update Release
6
+ fix: handle ER_DUP_ENTRY race condition in fota.update() (#14)
7
+ perf(src/device/device): clean and parse mqtt payload
8
+
3
9
  ## 1.0.29
4
10
  fix(models/sensors): allow null model_id
5
11
  fix: synching mqtt topics
@@ -11,7 +17,7 @@
11
17
  doc(device): recover readme add test.md file
12
18
  fix(models/variants): remove unique true from column name
13
19
  ci: fix vulnerabilities
14
-
20
+
15
21
  ## 1.0.27
16
22
  feat(db): adds variants table
17
23
 
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.30",
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
@@ -420,6 +419,7 @@ async function parseMqttMessage(client, project_name, device, topic, payload, re
420
419
 
421
420
  try{
422
421
  payload = JSON.parse(payload);
422
+ payload = cleanAndParse(payload);
423
423
  }catch(error){}
424
424
 
425
425
  let word = $.parser.getFirstWord(topic);
@@ -515,7 +515,6 @@ async function parseMqttMessage(client, project_name, device, topic, payload, re
515
515
  }
516
516
  break;
517
517
  case "settings": // deprecated
518
- break;
519
518
  if(topic.endsWith("/set")){
520
519
  updateLocalSettings(device,topic,payload);
521
520
  }else{
@@ -726,4 +725,18 @@ function handleFotaError (deviceId, error){
726
725
  $.db_fota.updateLog(deviceId,object);
727
726
  }
728
727
 
729
-
728
+ function cleanAndParse(payload) {
729
+ if (typeof payload === 'string') {
730
+ let cleaned = payload.trim();
731
+ if (cleaned.startsWith('"') && cleaned.endsWith('"')) {
732
+ cleaned = cleaned.slice(1, -1);
733
+ }
734
+ cleaned = cleaned.replace(/\\"/g, '"');
735
+ try {
736
+ return JSON.parse(cleaned);
737
+ } catch (e) {
738
+ return payload; // fallback: return original if still invalid
739
+ }
740
+ }
741
+ return payload;
742
+ }
@@ -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(),