mqtt-devices-parser 1.0.13 → 1.0.15

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,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.15
4
+ src/device/device: fixes fota for sniffer model
5
+ changes DB tables:
6
+ - adds level to modelPermissions
7
+ - adds project_id to model
8
+ - adds projectPermissions table
9
+ - deletes file modelPermissions.js
10
+
11
+ ## 1.0.14
12
+ Fixes check FOTA mechanism and response
13
+
3
14
  ## 1.0.13
4
15
  adds 2 new tables to db:
5
16
  Adds 5 new columns to devices
package/Release.md CHANGED
@@ -3,9 +3,13 @@
3
3
  ## Before release
4
4
 
5
5
  increases version on package.json
6
+ run npm install
6
7
  edit changelog
7
8
  commit and push to repo
8
9
 
9
10
  ## Releasing
10
11
 
12
+ launch release on git:
13
+ >> gh release create ${tag}
14
+
11
15
  npm publish
@@ -4,6 +4,9 @@ module.exports = (sequelize,DataTypes)=>{
4
4
  type: DataTypes.INTEGER,
5
5
  unique: true,
6
6
  },
7
+ model_id: {
8
+ type: DataTypes.INTEGER,
9
+ },
7
10
  target_version: {
8
11
  type: DataTypes.STRING,
9
12
  },
@@ -14,6 +14,9 @@ module.exports = (sequelize,DataTypes)=>{
14
14
  model: 'models',
15
15
  key: 'id'
16
16
  }
17
+ },
18
+ level: {
19
+ type: DataTypes.INTEGER,
17
20
  }
18
21
  },
19
22
  {
@@ -16,6 +16,10 @@ module.exports = (sequelize,DataTypes)=>{
16
16
  type: DataTypes.STRING,
17
17
  allowNull: true
18
18
  },
19
+ project_id: {
20
+ type: DataTypes.INTEGER,
21
+ allowNull: false
22
+ },
19
23
  fw_enabled: {
20
24
  type: DataTypes.BOOLEAN,
21
25
  defaultValue: false
@@ -1,6 +1,6 @@
1
1
 
2
2
  module.exports = (sequelize,DataTypes)=>{
3
- return sequelize.define("modelPermissions", {
3
+ return sequelize.define("projectPermissions", {
4
4
  client_id: {
5
5
  type: DataTypes.INTEGER,
6
6
  references: {
@@ -8,16 +8,19 @@ module.exports = (sequelize,DataTypes)=>{
8
8
  key: 'id'
9
9
  }
10
10
  },
11
- model_id: {
11
+ project_id: {
12
12
  type: DataTypes.INTEGER,
13
13
  references: {
14
- model: 'models',
14
+ model: 'projects',
15
15
  key: 'id'
16
16
  }
17
+ },
18
+ level: {
19
+ type: DataTypes.INTEGER,
17
20
  }
18
21
  },
19
22
  {
20
- tableName: 'modelPermissions',
23
+ tableName: 'projectPermissions',
21
24
  freezeTableName: true
22
25
  })
23
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mqtt-devices-parser",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/db/fota.js CHANGED
@@ -7,7 +7,7 @@ var self = module.exports = {
7
7
 
8
8
  return new Promise((resolve,reject) => {
9
9
 
10
- let query = `SELECT COUNT(*) FROM ?? where
10
+ let query = `SELECT * FROM ?? where
11
11
  device_id = ? and
12
12
  target_version = ? and
13
13
  target_app_version = ? and
@@ -91,13 +91,13 @@ var self = module.exports = {
91
91
  obj['createdAt'] = moment().utc().format('YYYY-MM-DD HH:mm:ss');
92
92
  obj['device_id'] = deviceId;
93
93
 
94
- $.db.insert("fota",obj)
95
- .then (rows => {
96
- return resolve(rows[0]);
97
- })
98
- .catch(error => {
99
- return reject(error);
100
- });
94
+ $.db.insert("fota",obj)
95
+ .then (rows => {
96
+ return resolve(rows[0]);
97
+ })
98
+ .catch(error => {
99
+ return reject(error);
100
+ });
101
101
 
102
102
  }else{
103
103
 
@@ -106,18 +106,18 @@ var self = module.exports = {
106
106
  };
107
107
 
108
108
  $.db.update("fota",obj,filter)
109
- .then (rows => {
110
- return resolve(rows[0]);
111
- })
112
- .catch(error => {
113
- return reject(error);
114
- });
109
+ .then (rows => {
110
+ return resolve(rows[0]);
111
+ })
112
+ .catch(error => {
113
+ return reject(error);
114
+ });
115
115
  }
116
116
 
117
117
  });
118
118
  },
119
119
 
120
- getDeviceLastLog : async(deviceId)=>{
120
+ getFotaLastLog : async(deviceId)=>{
121
121
 
122
122
  return new Promise((resolve,reject) => {
123
123
 
@@ -127,7 +127,7 @@ var self = module.exports = {
127
127
  LIMIT 1`;
128
128
 
129
129
  let table = [
130
- "fota",
130
+ "logs_fota",
131
131
  deviceId,
132
132
  ];
133
133
  query = mysql.format(query,table);
@@ -195,7 +195,7 @@ var self = module.exports = {
195
195
 
196
196
  return new Promise(async (resolve,reject) => {
197
197
 
198
- const log = await getDeviceLastLog(deviceId);
198
+ const log = await self.getFotaLastLog(deviceId);
199
199
 
200
200
  if(log?.createdAt != log?.updatedAt)
201
201
 
@@ -314,12 +314,15 @@ var self = module.exports = {
314
314
 
315
315
  for (const model of models) {
316
316
 
317
+ console.log("model:",model?.name);
317
318
  if(!model?.id)
318
319
  continue;
319
320
 
320
321
  try{
321
322
  const latestVersion = await $.db_firmware.getLatestVersion(model.id,release);
322
323
  const latestAppVersion = await $.db_firmware.getLatestAppVersion(model.id,release);
324
+ console.log("latestVersion:",latestVersion?.version);
325
+ console.log("latestAppVersion:",latestAppVersion?.app_version);
323
326
 
324
327
  const devices = await $.db_device.listByModel(model.id);
325
328
 
@@ -356,12 +359,21 @@ var self = module.exports = {
356
359
  };
357
360
  }
358
361
  if(obj != null){
359
- try{
360
- let res = await $.db_fota.getEntry(device.id,obj);
361
- if(res == null)
362
- $.db_fota.update(device.id,obj);
363
- }catch(error){
364
- console.error(error)
362
+ try {
363
+
364
+ let res = await $.db_fota.getEntry(device.id, obj);
365
+
366
+ if (res == null) {
367
+ try {
368
+ console.log("add fota entry for device:", device.uid);
369
+ await $.db_fota.update(device.id, obj);
370
+ } catch (err) {
371
+ console.error("Error updating FOTA entry:", err);
372
+ }
373
+ }
374
+
375
+ } catch (error) {
376
+ console.error("Error getting or processing FOTA entry:", error);
365
377
  }
366
378
  }
367
379
  }
@@ -389,19 +401,22 @@ var self = module.exports = {
389
401
  const model_name = model?.name;
390
402
  let topic = "";
391
403
  if(model_name == "sniffer"){
392
- resolve();
393
- // !! This cannot be handled on this way
394
404
 
395
405
  // get sniffer info associated to device
396
- const sniffer = await $.db_data.getAssociatedToDevice("sniffer",device.id);
397
- if(sniffer == null) return;
406
+ const associatedDevice = await $.db_device.getAssociatedDevice(device.id);
407
+ if(associatedDevice == null) return;
408
+
398
409
  // get device_uid from devices table
399
- const gw = await $.db_device.getById(sniffer?.id)
410
+ const gw = await $.db_device.getById(associatedDevice)
400
411
  if(gw == null) return;
401
- let mqtt_prefix = `${project_name}/${gw.uid}`;
402
- if (semver.gt(sniffer.version, "2.0.1"))
412
+
413
+ const gwProject = await $.db_project.getById(gw?.project_id);
414
+ const gwProjectName = project?.name;
415
+
416
+ let mqtt_prefix = `${gwProjectName}/${gw?.uid}`;
417
+ if (semver.gt(gw.app_version, "1.0.5"))
403
418
  topic = mqtt_prefix+`/app/sniffer/${sniffer.uid}/fota/update/set`;
404
- else
419
+ else // deprecate it as soon as all gws are updated
405
420
  topic = mqtt_prefix+`/app/sniffer/fota/update/set`;
406
421
  }
407
422
  else{
@@ -409,7 +424,7 @@ var self = module.exports = {
409
424
  topic = mqtt_prefix+"/fw/fota/update/set";
410
425
  }
411
426
  let link = `${$.config.web.protocol}${$.config.web.domain}${$.config.web.fw_path}${firmware?.filename}/download?token=${firmware?.token}`;
412
- console.log(`Requesting firmware update of ${device.uid} to ${firmware?.filename}`);
427
+ console.log(`Requesting firmware update for ${device.uid} to ${firmware?.filename}`);
413
428
  $.mqtt_client.publish(topic,`{"url":"${link}"}`,{qos:1,retain:false});
414
429
 
415
430
  let obj = {
@@ -448,14 +463,14 @@ var self = module.exports = {
448
463
  object = {
449
464
  success : 1,
450
465
  }
451
- await $.db_fota.updateLog(deviceId,obj);
466
+ await $.db_fota.updateLog(deviceId,object);
452
467
  },
453
468
 
454
469
  handleFotaError : async (deviceId, error) => {
455
470
  let object = {
456
471
  error : error,
457
472
  }
458
- await $.db_fota.updateLog(deviceId,obj);
473
+ await $.db_fota.updateLog(deviceId,object);
459
474
  }
460
475
  }
461
476