mqtt-devices-parser 1.0.5 → 1.0.7

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,17 +1,27 @@
1
- # 1.0.5
1
+ # Changelog
2
+
3
+ ## 1.0.7
4
+ - config/index: changes local IP
5
+ - src/db/firmware: changes query to get latest fw release
6
+ - src/device/device: adds semver. Checks fw version on "uptime" topic sent. Updates fota_tries field on db
7
+
8
+ ## 1.0.6
9
+ - device: updates project if mismatch
10
+
11
+ ## 1.0.5
2
12
  - db: adds mqtt user passed on mqtt struct and grants admin privileges
3
13
 
4
- # 1.0.4
14
+ ## 1.0.4
5
15
  - mqtt: adds retain to project state
6
16
 
7
- # 1.0.3
17
+ ## 1.0.3
8
18
  - fixes config arg
9
19
 
10
- # 1.0.2
20
+ ## 1.0.2
11
21
  - passes config struct por methods
12
22
 
13
- # 1.0.1
23
+ ## 1.0.1
14
24
  - adds deploy method
15
25
 
16
- # 1.0.0
26
+ ## 1.0.0
17
27
  - Module to parse mqtt messages from devices
package/config/index.js CHANGED
@@ -3,7 +3,8 @@ module.exports = {
3
3
  dev : process.env.dev || "true",
4
4
  web:{
5
5
  protocol : process.env.HTTP_PROTOCOL || "http://",
6
- domain: process.env.DOMAIN || '192.168.1.1',
6
+ domain: process.env.DOMAIN || '10.168.1.162',
7
+ port: process.env.WEB_PORT || 8002,
7
8
  },
8
9
  mqtt: {
9
10
  protocol:process.env.MQTT_PROTOCOL || 'MQTT',
@@ -31,6 +31,10 @@ module.exports = (sequelize,DataTypes)=>{
31
31
  key: 'id'
32
32
  }
33
33
  },
34
+ tech: {
35
+ type: DataTypes.STRING,
36
+ allowNull: true
37
+ },
34
38
  },
35
39
  {
36
40
  tableName: 'devices',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mqtt-devices-parser",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/db/device.js CHANGED
@@ -65,6 +65,28 @@ var self = module.exports = {
65
65
  });
66
66
  },
67
67
 
68
+ updateTech : async(uid,tech)=>{
69
+ return new Promise((resolve,reject) => {
70
+
71
+ let obj = {
72
+ tech : tech,
73
+ updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss')
74
+ };
75
+
76
+ let filter = {
77
+ uid : uid,
78
+ };
79
+
80
+ $.db.update("devices",obj,filter)
81
+ .then (rows => {
82
+ return resolve(rows);
83
+ })
84
+ .catch(error => {
85
+ return reject(error);
86
+ });
87
+ });
88
+ },
89
+
68
90
  updateProject : async(uid,project_id)=>{
69
91
  return new Promise((resolve,reject) => {
70
92
 
@@ -10,27 +10,19 @@ var self = module.exports = {
10
10
  let query = "";
11
11
  let table = [];
12
12
 
13
- if(release == "stable"){
14
- query = `SELECT fw_version,filename,token FROM firmwares where model_id = ? and fw_release = ? ORDER BY CAST(SUBSTRING_INDEX(fw_version, '.', 1) AS UNSIGNED) DESC,
15
- CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(fw_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
16
- CAST(SUBSTRING_INDEX(fw_version, '.', -1) AS UNSIGNED) DESC
17
- LIMIT 1`;
18
- table = [modelId,release];
19
- }else{
20
- query = `SELECT fw_version,filename,token FROM firmwares where model_id = ? ORDER BY CAST(SUBSTRING_INDEX(fw_version, '.', 1) AS UNSIGNED) DESC,
21
- CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(fw_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
22
- CAST(SUBSTRING_INDEX(fw_version, '.', -1) AS UNSIGNED) DESC
23
- LIMIT 1`;
24
- table = [modelId];
25
- }
13
+ query = `SELECT fw_version,filename,token FROM firmwares where model_id = ? and fw_release = ? ORDER BY CAST(SUBSTRING_INDEX(fw_version, '.', 1) AS UNSIGNED) DESC,
14
+ CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(fw_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
15
+ CAST(SUBSTRING_INDEX(fw_version, '.', -1) AS UNSIGNED) DESC
16
+ LIMIT 1`;
17
+ table = [modelId,release];
26
18
 
27
19
  query = mysql.format(query,table);
28
20
 
29
21
  $.db.queryRow(query)
30
22
  .then( rows => {
31
- if(rows.length > 0)
23
+ if(rows.length > 0){
32
24
  return resolve(rows[0]);
33
- else
25
+ }else
34
26
  return resolve(null);
35
27
  })
36
28
  .catch( err => {
@@ -47,19 +39,11 @@ var self = module.exports = {
47
39
  let query = "";
48
40
  let table = [];
49
41
 
50
- if(release == "stable"){
51
- query = `SELECT app_version,filename,token FROM firmwares where model_id = ? and fw_release = ? ORDER BY CAST(SUBSTRING_INDEX(app_version, '.', 1) AS UNSIGNED) DESC,
52
- CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(app_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
53
- CAST(SUBSTRING_INDEX(app_version, '.', -1) AS UNSIGNED) DESC
54
- LIMIT 1`;
55
- table = [modelId,release];
56
- }else{
57
- query = `SELECT app_version,filename,token FROM firmwares where model_id = ? ORDER BY CAST(SUBSTRING_INDEX(app_version, '.', 1) AS UNSIGNED) DESC,
58
- CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(app_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
59
- CAST(SUBSTRING_INDEX(app_version, '.', -1) AS UNSIGNED) DESC
60
- LIMIT 1`;
61
- table = [modelId];
62
- }
42
+ query = `SELECT app_version,filename,token FROM firmwares where model_id = ? and fw_release = ? ORDER BY CAST(SUBSTRING_INDEX(app_version, '.', 1) AS UNSIGNED) DESC,
43
+ CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(app_version, '.', 2), '.', -1) AS UNSIGNED) DESC,
44
+ CAST(SUBSTRING_INDEX(app_version, '.', -1) AS UNSIGNED) DESC
45
+ LIMIT 1`;
46
+ table = [modelId,release];
63
47
 
64
48
  query = mysql.format(query,table);
65
49
 
@@ -1,6 +1,7 @@
1
1
 
2
2
  var project = [];
3
3
  const logs_table = "sensor_logs";
4
+ const semver = require('semver');
4
5
 
5
6
  var self = module.exports = {
6
7
 
@@ -62,11 +63,11 @@ var self = module.exports = {
62
63
  .catch( (err) => {});
63
64
  }
64
65
 
65
- if(device?.project_id == null){
66
- let res = await $.db_project.getByName(project_name);
67
- let project_id = res?.id;
68
- if(project_id != null) $.db_device.updateProject(uid,project_id);
69
- }
66
+ // update project id if needed
67
+ let res = await $.db_project.getByName(project_name);
68
+ let project_id = res?.id;
69
+ if(project_id != null && project_id != device?.project_id)
70
+ $.db_device.updateProject(uid,project_id);
70
71
 
71
72
  if(topic.endsWith("status")){
72
73
  if(payload == "online" || payload == "offline")
@@ -76,13 +77,59 @@ var self = module.exports = {
76
77
  let res = await $.db_model.getByName(payload);
77
78
  let model_id = res?.id;
78
79
 
79
- if(model_id != null) $.db_device.updateModel(uid,model_id);
80
+ if(model_id != null){
81
+ let res = await $.db_device.updateModel(uid,model_id);
82
+ }
83
+ }else if(topic.endsWith("tech")){
84
+ await $.db_device.updateTech(uid,payload);
85
+ }else if(topic.endsWith("fw_version")){
86
+ let fw_version = await $.db.getFieldFromDeviceId(project_name,device.id,"fw_version");
87
+ if(fw_version == payload)
88
+ await $.db_data.update(project_name,device.id,"fota_tries",1); // ! 0 is not working..
89
+ }else if(topic.endsWith("app_version")){
90
+ let app_version = await $.db.getFieldFromDeviceId(project_name,device.id,"app_version");
91
+ if(app_version != payload)
92
+ await $.db_data.update(project_name,device.id,"fota_tries",1); // ! 0 is not working..
93
+ }else if(topic.endsWith("uptime")){ // !! maybe this topic should be change for something ending in fota
94
+ // check fw version
95
+ // get current fw and app version
96
+ let release = await $.db.getFieldFromDeviceId(project_name,device.id,"fw_release");
97
+ if(release == null || release == "critical"){
98
+ // don't update
99
+ return;
100
+ }
101
+ let fota_tries = await $.db.getFieldFromDeviceId(project_name,device.id,"fota_tries");
102
+ if(fota_tries > 3)
103
+ return;
104
+
105
+ let fw_version = await $.db.getFieldFromDeviceId(project_name,device.id,"fw_version");
106
+ let app_version = await $.db.getFieldFromDeviceId(project_name,device.id,"app_version");
107
+ let new_firmware = await $.db_firmware.getLatestFWVersion(device.model_id,release);
108
+ let new_app = await $.db_firmware.getLatestAppVersion(device.model_id,release);
109
+ if(new_firmware == null || new_app == null)
110
+ return;
111
+
112
+ let mqtt_prefix = `${project_name}/${uid}`;
113
+ let link = `${$.config.web.protocol}${$.config.web.domain}:${$.config.web.port}${$.config.web.fw_path}${new_firmware.filename}/download?token=${new_firmware.token}`;
114
+
115
+ if(new_firmware != null && semver.lt(fw_version, new_firmware.fw_version)) {
116
+ console.log(`updating firmware of ${uid} to ${new_firmware.fw_version}`);
117
+ client.publish(mqtt_prefix+"/fw/fota/update/set",`{"url":"${link}"}`,{qos:2,retain:false});
118
+ await $.db_data.update(project_name,device.id,"fota_tries",++fota_tries);
119
+ }else{
120
+ if(new_app != null && semver.lt(app_version, new_app.app_version)) {
121
+ console.log(`updating firmware of ${uid} to ${new_app.app_version}`);
122
+ client.publish(mqtt_prefix+"/fw/fota/update/set",`{"url":"${link}"}`,{qos:2,retain:false});
123
+ await $.db_data.update(project_name,device.id,"fota_tries",++fota_tries);
124
+ }
125
+ }
126
+
80
127
  }
81
128
 
82
129
  if(device.project_id != null && device.model_id != null){
83
130
  let res = await $.db_sensor.getByRef(topic)
84
131
  if(res != null){
85
- $.db_sensor.insert(logs_table,device.id,res.id,payload);
132
+ await $.db_sensor.insert(logs_table,device.id,res.id,payload);
86
133
  }
87
134
  }
88
135