mqtt-devices-parser 1.0.6 → 1.0.8
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 +17 -7
- package/config/index.js +2 -1
- package/models/devices.models.js +4 -0
- package/package.json +1 -1
- package/src/db/device.js +22 -0
- package/src/db/firmware.js +12 -28
- package/src/device/device.js +49 -2
package/Changelog.md
CHANGED
|
@@ -1,20 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.8
|
|
4
|
+
- removes port from link to update fw devices
|
|
5
|
+
|
|
6
|
+
## 1.0.7
|
|
7
|
+
- config/index: changes local IP
|
|
8
|
+
- src/db/firmware: changes query to get latest fw release
|
|
9
|
+
- src/device/device: adds semver. Checks fw version on "uptime" topic sent. Updates fota_tries field on db
|
|
10
|
+
|
|
11
|
+
## 1.0.6
|
|
2
12
|
- device: updates project if mismatch
|
|
3
13
|
|
|
4
|
-
|
|
14
|
+
## 1.0.5
|
|
5
15
|
- db: adds mqtt user passed on mqtt struct and grants admin privileges
|
|
6
16
|
|
|
7
|
-
|
|
17
|
+
## 1.0.4
|
|
8
18
|
- mqtt: adds retain to project state
|
|
9
19
|
|
|
10
|
-
|
|
20
|
+
## 1.0.3
|
|
11
21
|
- fixes config arg
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
## 1.0.2
|
|
14
24
|
- passes config struct por methods
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
## 1.0.1
|
|
17
27
|
- adds deploy method
|
|
18
28
|
|
|
19
|
-
|
|
29
|
+
## 1.0.0
|
|
20
30
|
- 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 || '
|
|
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',
|
package/models/devices.models.js
CHANGED
package/package.json
CHANGED
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
|
|
package/src/db/firmware.js
CHANGED
|
@@ -10,27 +10,19 @@ var self = module.exports = {
|
|
|
10
10
|
let query = "";
|
|
11
11
|
let table = [];
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
package/src/device/device.js
CHANGED
|
@@ -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
|
|
|
@@ -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)
|
|
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.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
|
|