mqtt-devices-parser 1.0.0

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.
@@ -0,0 +1,77 @@
1
+ const moment = require('moment');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ var app = [];
6
+
7
+ MACRO_UID_PREFIX = "uid:"
8
+
9
+ module.exports = {
10
+
11
+ sync_db : async()=>{
12
+
13
+ return new Promise( async (resolve,reject) => {
14
+
15
+ console.log("Sync dbs for rtls-linux module");
16
+ await $.models.init();
17
+ await $.models.connect();
18
+ await $.models.load(__dirname+"/models");
19
+
20
+ return resolve();
21
+ });
22
+ },
23
+
24
+ init : async ()=>{
25
+
26
+ console.log("Project rtls-linux started");
27
+ await $.models.load(__dirname+"/models");
28
+
29
+ // load apps
30
+ if(fs.existsSync(__dirname+"/apps")){
31
+ fs.readdirSync(__dirname+"/apps")
32
+ .filter((file) => {
33
+ // Exclude any extension file
34
+ return (file.indexOf('.') == -1);
35
+ })
36
+ .forEach((name) => {
37
+ app[name] = {
38
+ module : null
39
+ };
40
+
41
+ app[name].module = require(__dirname + '/apps/'+name+'/'+name+".js")
42
+ app[name].module.init();
43
+ });
44
+ }
45
+ },
46
+
47
+ parseMessage : async (client, project, device, topic, payload, retain, cb)=>{
48
+
49
+ // The next two lines updates dB with received data
50
+ $.db_data.update(project,device.id,topic,payload);
51
+ $.db_data.addLog("logs_"+project,device.id,topic,payload);
52
+
53
+ let index = topic.indexOf("/")
54
+ if(index == -1)
55
+ return cb();
56
+
57
+ if(topic.substring(0,3) == "app") {
58
+ topic = topic.substring(4);
59
+ index = topic.indexOf("/")
60
+ if(index == -1)
61
+ return cb();
62
+
63
+ let app_name = topic.substring(0,index);
64
+ topic = topic.substring(index+1);
65
+ if(app[app_name]){ // call app
66
+ let res = await $.db_model.getByName(app_name);
67
+ let model_id = res?.id;
68
+ if(model_id != null && device.model_id != model_id) $.db_device.updateModel(device.uid,model_id);
69
+ app[app_name].module.parseMessage(client,app_name,device,topic,payload,retain,()=>{})
70
+ }
71
+
72
+ }
73
+
74
+ }
75
+
76
+
77
+ }
@@ -0,0 +1,17 @@
1
+
2
+ module.exports = (sequelize,DataTypes)=>{
3
+ return sequelize.define("logs_myapp", {
4
+ device_id: {
5
+ type: DataTypes.INTEGER,
6
+ references: {
7
+ model: 'devices',
8
+ key: 'id'
9
+ }
10
+ }
11
+ },
12
+ {
13
+ tableName: 'logs_myapp',
14
+ freezeTableName: true
15
+ })
16
+ }
17
+
@@ -0,0 +1,18 @@
1
+
2
+ module.exports = (sequelize,DataTypes)=>{
3
+ return sequelize.define("myapp", {
4
+ device_id: {
5
+ type: DataTypes.INTEGER,
6
+ unique: true,
7
+ references: {
8
+ model: 'devices',
9
+ key: 'id'
10
+ }
11
+ },
12
+ },
13
+ {
14
+ tableName: 'myapp',
15
+ freezeTableName: true
16
+ })
17
+ }
18
+
@@ -0,0 +1,90 @@
1
+ const fs = require('fs');
2
+
3
+ $ = {}
4
+ var path = "/../.."
5
+ $.config = require(__dirname+ path+'/config');
6
+ $.models = require(__dirname+ path+'/models/models.js');
7
+ $.device = require(__dirname+ path+'/src/device/device.js');
8
+ $.db = require(__dirname+ path+'/src/db/db');
9
+ $.db_data = require(__dirname+ path+'/src/db/data');
10
+ $.db_project = require(__dirname+ path+'/src/db/project');
11
+ $.db_device = require(__dirname+ path+'/src/db/device');
12
+ $.db_model = require(__dirname+ path+'/src/db/model');
13
+ $.db_firmware = require(__dirname+ path+'/src/db/firmware');
14
+
15
+ var projectsPath = __dirname+'/../projects';
16
+ var projects = [];
17
+
18
+ let topics_rtls_linux = [
19
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/status",
20
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/version",
21
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/ssid",
22
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/rssi",
23
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/channel",
24
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/uptime",
25
+ "rtls-linux/uid:ea188c9c-d695-4644-b360-b59acc785076/ip",
26
+ ]
27
+ let payloads_rtls_linux = [
28
+ "online",
29
+ "1.0.0",
30
+ "fake_ssid",
31
+ "-79",
32
+ "4",
33
+ "122",
34
+ "0.0.0.0",
35
+ ]
36
+
37
+ let topics_freeRTOS2 = [
38
+ "freeRTOS2/uid:ac67b2403178/status",
39
+ "freeRTOS2/uid:ac67b2403178/model",
40
+ "freeRTOS2/uid:ac67b2403178/app/HH/temperature"
41
+ ]
42
+ let payloads_freeRTOS2 = [
43
+ "offline",
44
+ "HH_GW_WIFI",
45
+ "30"
46
+ ];
47
+
48
+ let topics = topics_freeRTOS2;
49
+ let payloads = payloads_freeRTOS2
50
+
51
+ let client = {
52
+ publish : (packet,cb)=>{
53
+ return cb();
54
+ }
55
+ };
56
+
57
+ async function setup(){
58
+ await $.models.init();
59
+ await $.models.connect();
60
+ }
61
+
62
+ async function test1(){
63
+
64
+ console.log(__dirname)
65
+ fs.readdirSync(projectsPath)
66
+ .filter((file) => {
67
+ return (file.indexOf('.') == -1);
68
+ })
69
+ .forEach((project) => {
70
+ projects.push(project);
71
+ });
72
+
73
+ await $.device.init(projects);
74
+
75
+ topics.forEach((topic,counter)=>{
76
+
77
+ console.log("\ntest:",counter);
78
+
79
+ let payload = payloads[counter];
80
+ console.log("topic:",topic);
81
+ console.log("payload:",payload);
82
+ if(topic.includes("uid:")){
83
+ $.device.parseMessage(client,topic,payload,false);
84
+ }
85
+ })
86
+ }
87
+
88
+ setup();
89
+ test1();
90
+