mqtt-devices-parser 1.0.15 → 1.0.17
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 +22 -0
- package/config/index.js +4 -0
- package/models/clients.models.js +4 -0
- package/models/devices.models.js +6 -3
- package/models/logs_sensor.models.js +6 -8
- package/models/sensors.models.js +6 -7
- package/package.json +1 -1
- package/src/db/db.js +61 -0
- package/src/db/device.js +21 -0
- package/src/db/model.js +21 -0
- package/src/db/project.js +3 -1
- package/src/db/sensor.js +3 -1
- package/src/device/device.js +41 -15
package/Changelog.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.0.17
|
|
4
|
+
Fix and improve Sensors, checks uid length
|
|
5
|
+
config/index: When defining a project from here uidPrefix and uidLength must be defined
|
|
6
|
+
models/logs_sensor.models: removes client_id association
|
|
7
|
+
models/sensors.models: removes unique and references properties from model_id, adds device_id
|
|
8
|
+
src/db/db: adds method updateOrInsert
|
|
9
|
+
src/db/device: adds method getSensorByRef
|
|
10
|
+
src/db/model: adds method getSensorByRef
|
|
11
|
+
src/db/project: adds uidPrefix and uidLength properties
|
|
12
|
+
src/db/sensor: insert: adds fields error and obj
|
|
13
|
+
src/device/device:
|
|
14
|
+
- fixes logs_table
|
|
15
|
+
- adds projectOptions
|
|
16
|
+
- checks if device.uidLength <= project.uidLength
|
|
17
|
+
- looks for sensor ref by device_id first and then by model_id if not found. Insert object, value and error fields
|
|
18
|
+
- removes some fota logs
|
|
19
|
+
|
|
20
|
+
## 1.0.16
|
|
21
|
+
changes db tables:
|
|
22
|
+
- models/devices.models: removes endpoint, adds protocol and psk.
|
|
23
|
+
- adds mqtt_password
|
|
24
|
+
|
|
3
25
|
## 1.0.15
|
|
4
26
|
src/device/device: fixes fota for sniffer model
|
|
5
27
|
changes DB tables:
|
package/config/index.js
CHANGED
package/models/clients.models.js
CHANGED
package/models/devices.models.js
CHANGED
|
@@ -59,11 +59,14 @@ module.exports = (sequelize,DataTypes)=>{
|
|
|
59
59
|
type: DataTypes.INTEGER,
|
|
60
60
|
allowNull: true
|
|
61
61
|
},
|
|
62
|
-
|
|
63
|
-
type: DataTypes.
|
|
62
|
+
protocol: { // protocol communication to be used
|
|
63
|
+
type: DataTypes.STRING,
|
|
64
|
+
allowNull: false
|
|
65
|
+
},
|
|
66
|
+
psk: { // pre shared key
|
|
67
|
+
type: DataTypes.STRING,
|
|
64
68
|
allowNull: true
|
|
65
69
|
},
|
|
66
|
-
|
|
67
70
|
},
|
|
68
71
|
{
|
|
69
72
|
tableName: 'devices',
|
|
@@ -22,16 +22,14 @@ module.exports = (sequelize,DataTypes)=>{
|
|
|
22
22
|
type: DataTypes.STRING,
|
|
23
23
|
allowNull: true,
|
|
24
24
|
},
|
|
25
|
-
|
|
26
|
-
type: DataTypes.
|
|
27
|
-
|
|
25
|
+
error: {
|
|
26
|
+
type: DataTypes.STRING,
|
|
27
|
+
allowNull: true,
|
|
28
|
+
},
|
|
29
|
+
obj: {
|
|
30
|
+
type: DataTypes.STRING,
|
|
28
31
|
allowNull: true,
|
|
29
|
-
references: {
|
|
30
|
-
model: 'clients',
|
|
31
|
-
key: 'id'
|
|
32
|
-
}
|
|
33
32
|
},
|
|
34
|
-
|
|
35
33
|
},
|
|
36
34
|
{
|
|
37
35
|
tableName: 'logs_sensor',
|
package/models/sensors.models.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
|
|
2
2
|
module.exports = (sequelize,DataTypes)=>{
|
|
3
3
|
return sequelize.define("sensors", {
|
|
4
|
-
model_id: {
|
|
4
|
+
model_id: { // reference a model
|
|
5
5
|
type: DataTypes.INTEGER,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
6
|
+
allowNull: true,
|
|
7
|
+
},
|
|
8
|
+
device_id: { // reference a device id
|
|
9
|
+
type: DataTypes.INTEGER,
|
|
10
|
+
allowNull: true,
|
|
12
11
|
},
|
|
13
12
|
active: {
|
|
14
13
|
type: DataTypes.BOOLEAN,
|
package/package.json
CHANGED
package/src/db/db.js
CHANGED
|
@@ -144,6 +144,67 @@ var self = module.exports = {
|
|
|
144
144
|
});
|
|
145
145
|
},
|
|
146
146
|
|
|
147
|
+
updateOrInsert: async (table, data, filter) => {
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
self.getConnection((err, conn) => {
|
|
150
|
+
if (err) return reject(err);
|
|
151
|
+
|
|
152
|
+
let updateQuery = "";
|
|
153
|
+
let updateValues = [];
|
|
154
|
+
|
|
155
|
+
if (typeof data === "object") {
|
|
156
|
+
const keys = Object.keys(data);
|
|
157
|
+
updateValues = Object.values(data);
|
|
158
|
+
updateQuery = `UPDATE ?? SET ${keys.map(key => `${key} = ?`).join(', ')}`;
|
|
159
|
+
} else {
|
|
160
|
+
self.close_db_connection(conn);
|
|
161
|
+
return reject("data passed is not an object");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (typeof filter === "object") {
|
|
165
|
+
const filterKeys = Object.keys(filter);
|
|
166
|
+
const filterValues = Object.values(filter);
|
|
167
|
+
updateQuery += ` WHERE ${filterKeys.map(key => `${key} = ?`).join(' AND ')}`;
|
|
168
|
+
updateValues.push(...filterValues);
|
|
169
|
+
} else {
|
|
170
|
+
self.close_db_connection(conn);
|
|
171
|
+
return reject("filter passed is not an object");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
updateValues.unshift(table);
|
|
175
|
+
updateQuery = mysql.format(updateQuery, updateValues);
|
|
176
|
+
|
|
177
|
+
conn.query(updateQuery, function (err, rows) {
|
|
178
|
+
if (err) {
|
|
179
|
+
self.close_db_connection(conn);
|
|
180
|
+
return reject(err);
|
|
181
|
+
}
|
|
182
|
+
// If affectedRows = 0, do an insert
|
|
183
|
+
if (rows.affectedRows === 0) {
|
|
184
|
+
// Combine data and filter for insert
|
|
185
|
+
const insertObj = { ...filter, ...data }; // filter first, data overrides on conflict
|
|
186
|
+
const insertKeys = Object.keys(insertObj);
|
|
187
|
+
const insertValues = Object.values(insertObj);
|
|
188
|
+
|
|
189
|
+
const insertQuery = mysql.format(
|
|
190
|
+
`INSERT INTO ?? (${insertKeys.map(() => '??').join(', ')}) VALUES (${insertKeys.map(() => '?').join(', ')})`,
|
|
191
|
+
[table, ...insertKeys, ...insertValues]
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
conn.query(insertQuery, function (insertErr, insertRows) {
|
|
195
|
+
self.close_db_connection(conn);
|
|
196
|
+
if (insertErr) return reject(insertErr);
|
|
197
|
+
else return resolve(insertRows);
|
|
198
|
+
});
|
|
199
|
+
} else {
|
|
200
|
+
self.close_db_connection(conn);
|
|
201
|
+
return resolve(rows);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
|
|
147
208
|
// update column with json data
|
|
148
209
|
updateJSON : async (table, column, data, filter) => {
|
|
149
210
|
|
package/src/db/device.js
CHANGED
|
@@ -387,6 +387,27 @@ var self = module.exports = {
|
|
|
387
387
|
});
|
|
388
388
|
|
|
389
389
|
});
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
getSensorByRef : async(deviceId, ref)=>{
|
|
393
|
+
return new Promise((resolve,reject) => {
|
|
394
|
+
|
|
395
|
+
let query = "SELECT * FROM ?? where device_id = ? and ref = ?";
|
|
396
|
+
let args = ["sensors",deviceId,ref];
|
|
397
|
+
query = mysql.format(query,args);
|
|
398
|
+
|
|
399
|
+
$.db.queryRow(query)
|
|
400
|
+
.then( rows => {
|
|
401
|
+
if(rows.length > 0)
|
|
402
|
+
return resolve(rows[0]);
|
|
403
|
+
else
|
|
404
|
+
return resolve(null);
|
|
405
|
+
})
|
|
406
|
+
.catch( err => {
|
|
407
|
+
console.log(err);
|
|
408
|
+
return resolve(null);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
390
411
|
}
|
|
391
412
|
|
|
392
413
|
}
|
package/src/db/model.js
CHANGED
|
@@ -112,4 +112,25 @@ var self = module.exports = {
|
|
|
112
112
|
});
|
|
113
113
|
},
|
|
114
114
|
|
|
115
|
+
getSensorByRef : async(modelId, ref)=>{
|
|
116
|
+
return new Promise((resolve,reject) => {
|
|
117
|
+
|
|
118
|
+
let query = "SELECT * FROM ?? where model_id = ? and ref = ?";
|
|
119
|
+
let args = ["sensors",modelId,ref];
|
|
120
|
+
query = mysql.format(query,args);
|
|
121
|
+
|
|
122
|
+
$.db.queryRow(query)
|
|
123
|
+
.then( rows => {
|
|
124
|
+
if(rows.length > 0)
|
|
125
|
+
return resolve(rows[0]);
|
|
126
|
+
else
|
|
127
|
+
return resolve(null);
|
|
128
|
+
})
|
|
129
|
+
.catch( err => {
|
|
130
|
+
console.log(err);
|
|
131
|
+
return resolve(null);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
115
136
|
}
|
package/src/db/project.js
CHANGED
|
@@ -64,13 +64,15 @@ var self = module.exports = {
|
|
|
64
64
|
});
|
|
65
65
|
},
|
|
66
66
|
|
|
67
|
-
insert : async(project, project_table, logs_table)=>{
|
|
67
|
+
insert : async(project, project_table, logs_table, projectOptions)=>{
|
|
68
68
|
|
|
69
69
|
return new Promise((resolve,reject) => {
|
|
70
70
|
let obj = {
|
|
71
71
|
name : project,
|
|
72
72
|
project_table : project_table,
|
|
73
73
|
logs_table : logs_table,
|
|
74
|
+
uidPrefix : projectOptions.uidPrefix,
|
|
75
|
+
uidLength : projectOptions.uidLength,
|
|
74
76
|
createdAt : moment().utc().format('YYYY-MM-DD HH:mm:ss'),
|
|
75
77
|
updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss')
|
|
76
78
|
}
|
package/src/db/sensor.js
CHANGED
|
@@ -95,7 +95,9 @@ var self = module.exports = {
|
|
|
95
95
|
let obj = {
|
|
96
96
|
device_id : deviceId,
|
|
97
97
|
sensor_id : sensorId,
|
|
98
|
-
value : payload,
|
|
98
|
+
value : payload?.value,
|
|
99
|
+
error : payload?.error,
|
|
100
|
+
obj : payload?.object,
|
|
99
101
|
createdAt : moment().utc().format('YYYY-MM-DD HH:mm:ss'),
|
|
100
102
|
updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss')
|
|
101
103
|
}
|
package/src/device/device.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
const moment = require('moment');
|
|
2
2
|
|
|
3
3
|
var _project = [];
|
|
4
|
-
const logs_table = "
|
|
4
|
+
const logs_table = "logs_sensor";
|
|
5
5
|
const semver = require('semver');
|
|
6
6
|
|
|
7
7
|
var self = module.exports = {
|
|
8
8
|
|
|
9
9
|
init : async (config,projects)=>{
|
|
10
|
-
|
|
11
10
|
return new Promise(async (resolve,reject) => {
|
|
12
11
|
$.db.connect(config,()=>{
|
|
13
12
|
console.log("MYSQL is connected");
|
|
@@ -19,10 +18,12 @@ var self = module.exports = {
|
|
|
19
18
|
};
|
|
20
19
|
_project[name].module = require(`${BASE_DIR}/projects/${name}/${name}.js`)
|
|
21
20
|
_project[name].module?.init();
|
|
21
|
+
let projectOptions = config[name];
|
|
22
22
|
let row = await $.db_project.getByName(name);
|
|
23
|
+
|
|
23
24
|
if(row == null)
|
|
24
|
-
$.db_project.insert(name,name,"logs_"+name);
|
|
25
|
-
|
|
25
|
+
$.db_project.insert(name,name,"logs_"+name,projectOptions);
|
|
26
|
+
|
|
26
27
|
if(counter == projects.length-1)
|
|
27
28
|
return resolve();
|
|
28
29
|
})
|
|
@@ -60,7 +61,7 @@ var self = module.exports = {
|
|
|
60
61
|
topic = topic.substring(index+1);
|
|
61
62
|
|
|
62
63
|
// check if topic corresponds to a device
|
|
63
|
-
if(uid.startsWith(project.uidPrefix) && uid.length
|
|
64
|
+
if(uid.startsWith(project.uidPrefix) && uid.length <= project?.uidLength){
|
|
64
65
|
|
|
65
66
|
let device = await $.db_device.get(uid);
|
|
66
67
|
|
|
@@ -159,15 +160,40 @@ var self = module.exports = {
|
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
162
|
}else if(topic.startsWith("sensor/")){
|
|
162
|
-
updateSensor(device,topic,
|
|
163
|
+
//updateSensor(device,topic,payload)
|
|
163
164
|
index = topic.indexOf("/");
|
|
164
|
-
if(index
|
|
165
|
-
|
|
165
|
+
if(index != -1){
|
|
166
|
+
const ref = topic.substring(index+1);
|
|
167
|
+
let res = await $.db_device.getSensorByRef(device.id,ref)
|
|
168
|
+
if(res == null)
|
|
169
|
+
res = await $.db_model.getSensorByRef(device.model_id,ref)
|
|
170
|
+
if(res != null){
|
|
171
|
+
object = null;
|
|
172
|
+
value = null;
|
|
173
|
+
error = null;
|
|
174
|
+
try{
|
|
175
|
+
object = JSON.parse(payload);
|
|
176
|
+
}catch(err){
|
|
177
|
+
console.log(err);
|
|
178
|
+
}
|
|
179
|
+
if (typeof object === 'object' && object !== null) {
|
|
180
|
+
value = object?.value;
|
|
181
|
+
error = object?.error;
|
|
182
|
+
}else{
|
|
183
|
+
value = payload;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if(value || error)
|
|
187
|
+
object = null;
|
|
166
188
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
189
|
+
const data = {
|
|
190
|
+
object,
|
|
191
|
+
value,
|
|
192
|
+
error
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
await $.db_sensor.insert(logs_table,device.id,res.id,data);
|
|
196
|
+
}
|
|
171
197
|
}
|
|
172
198
|
}
|
|
173
199
|
|
|
@@ -314,15 +340,15 @@ var self = module.exports = {
|
|
|
314
340
|
|
|
315
341
|
for (const model of models) {
|
|
316
342
|
|
|
317
|
-
console.log("model:",model?.name);
|
|
343
|
+
//console.log("model:",model?.name);
|
|
318
344
|
if(!model?.id)
|
|
319
345
|
continue;
|
|
320
346
|
|
|
321
347
|
try{
|
|
322
348
|
const latestVersion = await $.db_firmware.getLatestVersion(model.id,release);
|
|
323
349
|
const latestAppVersion = await $.db_firmware.getLatestAppVersion(model.id,release);
|
|
324
|
-
console.log("latestVersion:",latestVersion?.version);
|
|
325
|
-
console.log("latestAppVersion:",latestAppVersion?.app_version);
|
|
350
|
+
//console.log("latestVersion:",latestVersion?.version);
|
|
351
|
+
//console.log("latestAppVersion:",latestAppVersion?.app_version);
|
|
326
352
|
|
|
327
353
|
const devices = await $.db_device.listByModel(model.id);
|
|
328
354
|
|