mqtt-devices-parser 1.0.10 → 1.0.12

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,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.12
4
+ Adds periodically call to check and update devices
5
+
6
+ ## 1.0.11
7
+ index: MQTT
8
+ - makes mqtt client global,
9
+ - adds periodically call for FOTA update
10
+ models/devices.models: adds version fields to devices table
11
+ models/firmwares.models: adds new fields for firmware management
12
+ src/db/device: optimizes calls to manage devices table
13
+ src/device/device: Fota changes, db calls updated to match previous changes
14
+ Adds method to check FOTA updates. Doing 10 at time at each minute for now. Non configurable
15
+
3
16
  ## 1.0.10
4
17
  src/db/data: handles json data
5
18
  - Adds updateJson call
package/index.js CHANGED
@@ -14,6 +14,8 @@ $.db_device = require('./src/db/device');
14
14
  $.db_model = require('./src/db/model');
15
15
  $.db_firmware = require('./src/db/firmware');
16
16
  $.db_sensor = require('./src/db/sensor');
17
+ $.db_fota = require('./src/db/fota');
18
+ $.mqtt_client = null;
17
19
 
18
20
  const packageJson = require(__dirname+'/package.json');
19
21
  const packageVersion = packageJson.version;
@@ -44,7 +46,6 @@ var self = module.exports = {
44
46
  setInterval(async ()=>{
45
47
  await $.device.deleteLogs();
46
48
  },60*60*1000);
47
-
48
49
 
49
50
  return resolve();
50
51
  })
@@ -63,6 +64,7 @@ var self = module.exports = {
63
64
  })
64
65
 
65
66
  let counter_help = 0;
67
+ let project = {}
66
68
  files.forEach( async(name,counter) => {
67
69
  if($.config.projects[name]){
68
70
  project[name] = {};
@@ -82,7 +84,7 @@ var self = module.exports = {
82
84
 
83
85
  },
84
86
 
85
- deploy: async(_config,projectsPath)=>{
87
+ deploy: async(_config,projectsPath, projects)=>{
86
88
 
87
89
  if(_config != null)
88
90
  $.config = _config;
@@ -104,6 +106,15 @@ var self = module.exports = {
104
106
  await $.models.insertUser("client","client_pwd",3);
105
107
  // adds client with credentials admin@admin
106
108
  await $.models.insertClient("admin","admin",user_id); // dashboard login
109
+
110
+ for(name of projects){
111
+ project = $.config[name];
112
+ if (project) {
113
+ // Make a copy if you don't want to modify the original config object
114
+ const projectData = { ...project, name: name };
115
+ await $.models.insertProject(projectData);
116
+ }
117
+ }
107
118
  }
108
119
 
109
120
  // project tables
@@ -117,7 +128,8 @@ var self = module.exports = {
117
128
  function mqtt_connect(){
118
129
 
119
130
  const mqtt_prefix = $.config.mqtt.logs_path+"/"+$.config.mqtt.client;
120
- const client = mqtt.connect({
131
+ let checkFota = null;
132
+ $.mqtt_client = mqtt.connect({
121
133
  host : $.config.mqtt.host,
122
134
  port : $.config.mqtt.port,
123
135
  username : $.config.mqtt.user,
@@ -132,50 +144,68 @@ function mqtt_connect(){
132
144
  }
133
145
  });
134
146
 
135
- client.on("connect", () => {
147
+ $.mqtt_client.on("connect", () => {
136
148
 
137
- client.publish(mqtt_prefix+"/status","online",{qos:2,retain:true});
138
- client.publish(mqtt_prefix+"/version",packageVersion,{qos:2,retain:true});
149
+ $.mqtt_client.publish(mqtt_prefix+"/status","online",{qos:2,retain:true});
150
+ $.mqtt_client.publish(mqtt_prefix+"/version",packageVersion,{qos:2,retain:true});
139
151
 
140
152
  projects.map( (project)=>{
141
- client.subscribe(project+"/#", (err) => {});
153
+ $.mqtt_client.subscribe(project+"/#", (err) => {});
142
154
 
143
155
  for(let project in $.config.projects){
144
156
  if($.config.projects[project])
145
- client.publish(mqtt_prefix+"/"+project,"active",{qos:2,retain:true});
157
+ $.mqtt_client.publish(mqtt_prefix+"/"+project,"active",{qos:2,retain:true});
146
158
  else
147
- client.publish(mqtt_prefix+"/"+project,"deactive",{qos:2,retain:true});
159
+ $.mqtt_client.publish(mqtt_prefix+"/"+project,"deactive",{qos:2,retain:true});
148
160
  };
149
161
  })
150
162
  console.log(`MQTT connected to: ${$.config.mqtt.host}:${$.config.mqtt.port}`);
151
163
  projects.map( project=>{
152
164
  console.log("subscribing project:",project);
153
- client.subscribe(project+"/#")
165
+ $.mqtt_client.subscribe(project+"/#")
154
166
  })
167
+
168
+ // Update 'dev' devices at each 5 min
169
+ checkFota = setInterval(async ()=>{
170
+ await $.device.checkFota("dev");
171
+ await $.device.triggerFota("dev");
172
+ },5*60*1000);
173
+
174
+ // Update 'prod' devices at each hour
175
+ checkFota = setInterval(async ()=>{
176
+ await $.device.checkFota("prod");
177
+ await $.device.triggerFota("prod");
178
+ },60*60*1000);
179
+
180
+ // Devices not set as dev or prod must be triggered manually
181
+
155
182
  });
156
183
 
157
- client.on("message", (topic, payload, packet) => {
184
+ $.mqtt_client.on("message", (topic, payload, packet) => {
158
185
  // payload is Buffer
159
- $.device.parseMessage(client,topic.toString(),payload.toString(),packet.retain);
186
+ $.device.parseMessage($.mqtt_client,topic.toString(),payload.toString(),packet.retain);
160
187
  });
161
188
 
162
- client.on("reconnect",()=>{
189
+ $.mqtt_client.on("reconnect",()=>{
163
190
  console.log("reconnect")
164
191
  });
165
192
 
166
- client.on("close",()=>{
193
+ $.mqtt_client.on("close",()=>{
167
194
  console.log("close")
195
+ checkFota = null;
168
196
  });
169
197
 
170
- client.on("offline",()=>{
198
+ $.mqtt_client.on("offline",()=>{
171
199
  console.log("offline")
200
+ checkFota = null;
172
201
  });
173
202
 
174
- client.on("disconnect",(packet)=>{
203
+ $.mqtt_client.on("disconnect",(packet)=>{
175
204
  console.log(packet)
205
+ checkFota = null;
176
206
  })
177
207
 
178
- client.on("error",(error)=>{
208
+ $.mqtt_client.on("error",(error)=>{
179
209
  console.log(error)
180
210
  })
181
211
  }
@@ -9,10 +9,6 @@ module.exports = (sequelize,DataTypes)=>{
9
9
  type: DataTypes.STRING,
10
10
  allowNull: true
11
11
  },
12
- project: { // deprecated
13
- type: DataTypes.STRING,
14
- allowNull: true
15
- },
16
12
  status: {
17
13
  type: DataTypes.STRING,
18
14
  allowNull: true
@@ -31,6 +27,18 @@ module.exports = (sequelize,DataTypes)=>{
31
27
  key: 'id'
32
28
  }
33
29
  },
30
+ version: {
31
+ type: DataTypes.STRING,
32
+ allowNull: true
33
+ },
34
+ app_version: {
35
+ type: DataTypes.STRING,
36
+ allowNull: true
37
+ },
38
+ accept_release: {
39
+ type: DataTypes.STRING,
40
+ allowNull: true
41
+ },
34
42
  tech: {
35
43
  type: DataTypes.STRING,
36
44
  allowNull: true
@@ -10,7 +10,7 @@ module.exports = (sequelize,DataTypes)=>{
10
10
  allowNull: true,
11
11
  unique: true
12
12
  },
13
- fw_version: {
13
+ version: {
14
14
  type: DataTypes.STRING,
15
15
  allowNull: true
16
16
  },
@@ -18,11 +18,11 @@ module.exports = (sequelize,DataTypes)=>{
18
18
  type: DataTypes.STRING,
19
19
  allowNull: true
20
20
  },
21
- fw_release: {
21
+ build_release: {
22
22
  type: DataTypes.STRING,
23
23
  allowNull: true
24
24
  },
25
- model_id: { // deprecated
25
+ model_id: {
26
26
  type: DataTypes.INTEGER,
27
27
  references: {
28
28
  model: 'models',
@@ -0,0 +1,32 @@
1
+ module.exports = (sequelize,DataTypes)=>{
2
+ return sequelize.define("fota", {
3
+ device_id: {
4
+ type: DataTypes.INTEGER,
5
+ unique: true,
6
+ },
7
+ target_version: {
8
+ type: DataTypes.STRING,
9
+ },
10
+ target_app_version: {
11
+ type: DataTypes.STRING,
12
+ },
13
+ target_release: {
14
+ type: DataTypes.STRING,
15
+ },
16
+ firmware_id: {
17
+ type: DataTypes.INTEGER,
18
+ },
19
+ nAttempts: {
20
+ type: DataTypes.INTEGER,
21
+ default: 0
22
+ },
23
+ fUpdate: {
24
+ type: DataTypes.BOOLEAN,
25
+ default: 0
26
+ },
27
+ },
28
+ {
29
+ tableName: 'fota',
30
+ freezeTableName: true
31
+ })
32
+ }
@@ -0,0 +1,41 @@
1
+ module.exports = (sequelize,DataTypes)=>{
2
+ return sequelize.define("logs_fota", {
3
+ device_id: {
4
+ type: DataTypes.INTEGER,
5
+ },
6
+ model_id: {
7
+ type: DataTypes.STRING,
8
+ },
9
+ local_version: {
10
+ type: DataTypes.STRING,
11
+ },
12
+ local_app_version: {
13
+ type: DataTypes.STRING,
14
+ },
15
+ target_version: {
16
+ type: DataTypes.STRING,
17
+ },
18
+ target_app_version: {
19
+ type: DataTypes.STRING,
20
+ },
21
+ target_file: {
22
+ type: DataTypes.STRING,
23
+ },
24
+ error: {
25
+ type: DataTypes.STRING,
26
+ allowNull: true
27
+ },
28
+ success: {
29
+ type: DataTypes.BOOLEAN,
30
+ default: 0
31
+ },
32
+ nAttempt: {
33
+ type: DataTypes.INTEGER,
34
+ default: 0
35
+ },
36
+ },
37
+ {
38
+ tableName: 'logs_fota',
39
+ freezeTableName: true
40
+ })
41
+ }
@@ -1,5 +1,5 @@
1
1
  module.exports = (sequelize,DataTypes)=>{
2
- return sequelize.define("sensor_logs", {
2
+ return sequelize.define("logs_sensor", {
3
3
  device_id: {
4
4
  type: DataTypes.INTEGER,
5
5
  unique: false,
@@ -34,7 +34,7 @@ module.exports = (sequelize,DataTypes)=>{
34
34
 
35
35
  },
36
36
  {
37
- tableName: 'sensor_logs',
37
+ tableName: 'logs_sensor',
38
38
  freezeTableName: true
39
39
  })
40
40
  }
@@ -0,0 +1,23 @@
1
+
2
+ module.exports = (sequelize,DataTypes)=>{
3
+ return sequelize.define("modelPermissions", {
4
+ client_id: {
5
+ type: DataTypes.INTEGER,
6
+ references: {
7
+ model: 'clients',
8
+ key: 'id'
9
+ }
10
+ },
11
+ model_id: {
12
+ type: DataTypes.INTEGER,
13
+ references: {
14
+ model: 'models',
15
+ key: 'id'
16
+ }
17
+ }
18
+ },
19
+ {
20
+ tableName: 'modelPermissions',
21
+ freezeTableName: true
22
+ })
23
+ }