mqtt-devices-parser 1.0.8 → 1.0.9
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 +4 -0
- package/index.js +5 -0
- package/package.json +1 -1
- package/src/db/data.js +50 -0
- package/src/db/db.js +53 -0
- package/src/device/device.js +23 -0
package/Changelog.md
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
package/src/db/data.js
CHANGED
|
@@ -191,6 +191,56 @@ var self = module.exports = {
|
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
return resolve();
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
deleteLogs : async (table, deviceId, topic, payload)=>{
|
|
199
|
+
|
|
200
|
+
return new Promise((resolve,reject) => {
|
|
201
|
+
|
|
202
|
+
let db_columns = $.models.get(table);
|
|
203
|
+
if(db_columns == null)
|
|
204
|
+
return resolve();
|
|
205
|
+
|
|
206
|
+
$.db.delete(table,{id:0})
|
|
207
|
+
let index = topic.indexOf("/");
|
|
208
|
+
|
|
209
|
+
if(index > -1){ // check if topic has subtopics
|
|
210
|
+
let key = topic.substring(0,index);
|
|
211
|
+
topic = topic.substring(index+1);
|
|
212
|
+
|
|
213
|
+
if(db_columns.hasOwnProperty(key)){ // check if db has a column for this key
|
|
214
|
+
// create JSON struct with the remaining topic
|
|
215
|
+
// key is the column
|
|
216
|
+
|
|
217
|
+
// build object with the remaining topic and respective data
|
|
218
|
+
if(typeof data === "object")
|
|
219
|
+
obj[key] = JSON.stringify(parser.pathIntoObject(topic,JSON.stringify(data)))
|
|
220
|
+
else
|
|
221
|
+
obj[key] = JSON.stringify(parser.pathIntoObject(topic,data));
|
|
222
|
+
|
|
223
|
+
obj['device_id'] = deviceId;
|
|
224
|
+
obj['createdAt'] = moment().utc().format('YYYY-MM-DD HH:mm:ss');
|
|
225
|
+
$.db.insert(table,obj)
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
}else{
|
|
229
|
+
|
|
230
|
+
let key = topic;
|
|
231
|
+
if(db_columns.hasOwnProperty(key)){ // check if db has a column for this key
|
|
232
|
+
// build object with respective data
|
|
233
|
+
if(typeof data === "object")
|
|
234
|
+
obj[key] = JSON.stringify(data);
|
|
235
|
+
else
|
|
236
|
+
obj[key] = data;
|
|
237
|
+
|
|
238
|
+
obj['device_id'] = deviceId;
|
|
239
|
+
obj['createdAt'] = moment().utc().format('YYYY-MM-DD HH:mm:ss');
|
|
240
|
+
$.db.insert(table,obj);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
194
244
|
return resolve();
|
|
195
245
|
});
|
|
196
246
|
}
|
package/src/db/db.js
CHANGED
|
@@ -264,6 +264,59 @@ var self = module.exports = {
|
|
|
264
264
|
});
|
|
265
265
|
},
|
|
266
266
|
|
|
267
|
+
getTables : async()=>{
|
|
268
|
+
|
|
269
|
+
return new Promise((resolve,reject) => {
|
|
270
|
+
|
|
271
|
+
self.getConnection((err,conn)=>{
|
|
272
|
+
if(err) return reject(err);
|
|
273
|
+
|
|
274
|
+
let query = "SHOW TABLES;"
|
|
275
|
+
|
|
276
|
+
conn.query(query,function(err,rows){
|
|
277
|
+
self.close_db_connection(conn);
|
|
278
|
+
if(err) return reject(err)
|
|
279
|
+
else return resolve(rows);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
deleteOldEntries : async(table,filter)=>{
|
|
286
|
+
|
|
287
|
+
return new Promise((resolve,reject) => {
|
|
288
|
+
|
|
289
|
+
self.getConnection((err,conn)=>{
|
|
290
|
+
if(err) return reject(err);
|
|
291
|
+
|
|
292
|
+
let query = "";
|
|
293
|
+
if(typeof filter === "object"){
|
|
294
|
+
let values = [];
|
|
295
|
+
query = `DELETE FROM ?? WHERE `;
|
|
296
|
+
values.push(table);
|
|
297
|
+
for (let key in filter){
|
|
298
|
+
if(key == "createdAt"){
|
|
299
|
+
query += "createdAt < ?";
|
|
300
|
+
}else if(key = "id")
|
|
301
|
+
query += "id < ?"
|
|
302
|
+
else
|
|
303
|
+
return reject("key not supported");
|
|
304
|
+
values.push(filter[key]);
|
|
305
|
+
}
|
|
306
|
+
query = mysql.format(query,values);
|
|
307
|
+
}else{
|
|
308
|
+
return reject("filter passed is not an object");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
conn.query(query,function(err,rows){
|
|
312
|
+
self.close_db_connection(conn);
|
|
313
|
+
if(err) return reject(err)
|
|
314
|
+
else return resolve(rows);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
},
|
|
319
|
+
|
|
267
320
|
getFieldFromDeviceId : async (table,deviceId,field)=>{
|
|
268
321
|
|
|
269
322
|
return new Promise((resolve,reject) => {
|
package/src/device/device.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const moment = require('moment');
|
|
1
2
|
|
|
2
3
|
var project = [];
|
|
3
4
|
const logs_table = "sensor_logs";
|
|
@@ -31,6 +32,25 @@ var self = module.exports = {
|
|
|
31
32
|
|
|
32
33
|
},
|
|
33
34
|
|
|
35
|
+
deleteLogs : async()=>{
|
|
36
|
+
|
|
37
|
+
let tables = await $.db.getTables();
|
|
38
|
+
// Define one week in milliseconds
|
|
39
|
+
// Loop through each table
|
|
40
|
+
for (const tableObj of tables) {
|
|
41
|
+
const tableName = tableObj['Tables_in_mqtt-aedes']
|
|
42
|
+
const oneWeekAgo = moment().utc().subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss');
|
|
43
|
+
if(tableName.startsWith("logs_")){
|
|
44
|
+
const uptimeSeconds = Math.floor(process.uptime());
|
|
45
|
+
console.log(`deleting logs of table ${tableName} older than ${oneWeekAgo}"`);
|
|
46
|
+
await $.db.deleteOldEntries(tableName, { createdAt: oneWeekAgo });
|
|
47
|
+
time = Math.floor(process.uptime()) - uptimeSeconds;
|
|
48
|
+
console.log(`Logs of table ${tableName} deleted in ${time}s`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
},
|
|
53
|
+
|
|
34
54
|
parseMessage : async (client,topic,payload,retain)=>{
|
|
35
55
|
|
|
36
56
|
let topic_bck = topic;
|
|
@@ -141,3 +161,6 @@ var self = module.exports = {
|
|
|
141
161
|
},
|
|
142
162
|
|
|
143
163
|
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|