mqtt-devices-parser 1.0.11 → 1.0.13
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 +10 -0
- package/index.js +24 -3
- package/models/devices.models.js +20 -3
- package/models/firmwares.models.js +2 -10
- package/models/fota.models.js +32 -0
- package/models/logs_actions.models.js +17 -0
- package/models/logs_device.models.js +62 -0
- package/models/logs_fota.models.js +41 -0
- package/models/{sensor_logs.models.js → logs_sensor.models.js} +2 -2
- package/models/modelPermissions.models.js +23 -0
- package/models/models.js +238 -211
- package/models/projects.models.js +8 -0
- package/package.json +1 -1
- package/src/db/data.js +91 -87
- package/src/db/db.js +24 -0
- package/src/db/device.js +288 -7
- package/src/db/firmware.js +30 -5
- package/src/db/fota.js +219 -0
- package/src/device/Readme.md +39 -4
- package/src/device/device.js +374 -152
package/models/models.js
CHANGED
|
@@ -1,223 +1,250 @@
|
|
|
1
1
|
const fs = require('fs').promises;
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const {Sequelize, DataTypes} = require("sequelize");
|
|
4
|
-
var SqlString = require('sequelize/lib/sql-string')
|
|
3
|
+
const { Sequelize, DataTypes } = require("sequelize");
|
|
4
|
+
var SqlString = require('sequelize/lib/sql-string');
|
|
5
5
|
var mysql = require('mysql2');
|
|
6
6
|
var sequelize;
|
|
7
7
|
|
|
8
|
-
//var modelsPath = './models/';
|
|
8
|
+
// var modelsPath = './models/';
|
|
9
9
|
|
|
10
10
|
var self = module.exports = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
port: config.mysqldb.port,
|
|
21
|
-
dialect: 'mysql',
|
|
22
|
-
});
|
|
23
|
-
return resolve(sequelize);
|
|
24
|
-
})
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
connect : async ()=>{
|
|
28
|
-
|
|
29
|
-
return new Promise((resolve,reject) => {
|
|
30
|
-
sequelize.authenticate().then(() => {
|
|
31
|
-
console.log('Connection has been established successfully.');
|
|
32
|
-
return resolve();
|
|
33
|
-
}).catch((error) => {
|
|
34
|
-
console.error('Unable to connect to the database: ', error);
|
|
35
|
-
return reject();
|
|
36
|
-
});
|
|
11
|
+
init: async (config) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
sequelize = new Sequelize({
|
|
14
|
+
database: config.mysqldb.name,
|
|
15
|
+
username: config.mysqldb.user,
|
|
16
|
+
password: config.mysqldb.pwd,
|
|
17
|
+
host: config.mysqldb.host,
|
|
18
|
+
port: config.mysqldb.port,
|
|
19
|
+
dialect: 'mysql',
|
|
37
20
|
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
return resolve(sequelize);
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
connect: async () => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
sequelize
|
|
28
|
+
.authenticate()
|
|
29
|
+
.then(() => {
|
|
30
|
+
console.log('Connection has been established successfully.');
|
|
31
|
+
return resolve();
|
|
32
|
+
})
|
|
33
|
+
.catch((error) => {
|
|
34
|
+
console.error('Unable to connect to the database: ', error);
|
|
35
|
+
return reject();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
load: async (modelsPath) => {
|
|
41
|
+
return new Promise(async (resolve, reject) => {
|
|
42
|
+
var files = await fs.readdir(modelsPath);
|
|
43
|
+
files = files.filter((file) => {
|
|
44
|
+
// Exclude models.js and non-js files
|
|
45
|
+
return (
|
|
46
|
+
file.indexOf('.') !== 0 &&
|
|
47
|
+
file !== 'models.js' &&
|
|
48
|
+
file.slice(-9) === 'models.js'
|
|
49
|
+
);
|
|
55
50
|
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
sequelize.sync({alter:true}).then( async () => {
|
|
63
|
-
console.log('All tables were synced!');
|
|
64
|
-
return resolve();
|
|
65
|
-
}).catch((error) => {
|
|
66
|
-
console.error('Unable to create table : ', error);
|
|
67
|
-
return reject();
|
|
68
|
-
});
|
|
51
|
+
files.forEach((file, counter) => {
|
|
52
|
+
const model = require(path.join(modelsPath, file));
|
|
53
|
+
model(sequelize, DataTypes);
|
|
54
|
+
if (counter === files.length - 1) {
|
|
55
|
+
return resolve();
|
|
56
|
+
}
|
|
69
57
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
.then(res=>{
|
|
106
|
-
indexes = res;
|
|
107
|
-
|
|
108
|
-
// Filter out primary key index and unique indexes if you don't want to drop them
|
|
109
|
-
const filteredIndexes = indexes[0].filter(index => index.Key_name !== 'PRIMARY' && !index.Non_unique);
|
|
110
|
-
|
|
111
|
-
if(!filteredIndexes?.length)
|
|
112
|
-
return resolve();
|
|
113
|
-
|
|
114
|
-
// Drop each index
|
|
115
|
-
filteredIndexes.forEach( async(index,counter)=>{
|
|
116
|
-
let query = `DROP INDEX ?? ON ??`
|
|
117
|
-
query = mysql.format(query,[index.Key_name,tableName]);
|
|
118
|
-
sequelize.query(query)
|
|
119
|
-
.then(res=>{
|
|
120
|
-
console.log(`Dropped index ${index.Key_name}`);
|
|
121
|
-
if(counter == filteredIndexes.length-1)
|
|
122
|
-
return resolve();
|
|
123
|
-
})
|
|
124
|
-
.catch(error=>{
|
|
125
|
-
if(counter == filteredIndexes.length-1)
|
|
126
|
-
return resolve();
|
|
127
|
-
})
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
.catch(error=>{
|
|
131
|
-
return resolve();
|
|
132
|
-
})
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
|
-
|
|
136
|
-
get: (modelName)=>{
|
|
137
|
-
|
|
138
|
-
if(modelName != ""){
|
|
139
|
-
if(sequelize.models[modelName]){
|
|
140
|
-
const model = sequelize.models[modelName];
|
|
141
|
-
const keys = Object.keys(model.rawAttributes);
|
|
142
|
-
let obj = {}
|
|
143
|
-
keys.map((key)=>{
|
|
144
|
-
obj[key] = null;
|
|
145
|
-
})
|
|
146
|
-
return obj;
|
|
147
|
-
}else return;
|
|
148
|
-
}else{
|
|
149
|
-
return models = Object.keys(sequelize.models).map(modelName => {
|
|
150
|
-
const model = sequelize.models[modelName];
|
|
151
|
-
const keys = Object.keys(model.rawAttributes);
|
|
152
|
-
return {
|
|
153
|
-
modelName,
|
|
154
|
-
keys
|
|
155
|
-
};
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
getUser : async (type)=>{
|
|
161
|
-
|
|
162
|
-
return new Promise( async (resolve,reject) => {
|
|
163
|
-
const user = await sequelize.models['users'].findOne({
|
|
164
|
-
where: {
|
|
165
|
-
type: type
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
return resolve(user);
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
sync: async () => {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
// sequelize.sync({alter:true,force:true}).then(() => {
|
|
64
|
+
sequelize
|
|
65
|
+
.sync({ alter: true })
|
|
66
|
+
.then(async () => {
|
|
67
|
+
console.log('All tables were synced!');
|
|
68
|
+
return resolve();
|
|
69
|
+
})
|
|
70
|
+
.catch((error) => {
|
|
71
|
+
console.error('Unable to create table : ', error);
|
|
72
|
+
return reject();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
dropTableIndexes: async () => {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
let tables = Object.keys(sequelize.models);
|
|
80
|
+
|
|
81
|
+
console.log(tables);
|
|
82
|
+
if (!tables?.length) return resolve();
|
|
83
|
+
|
|
84
|
+
tables.forEach(async (tableName, counter) => {
|
|
85
|
+
self
|
|
86
|
+
.dropAllIndexes(tableName)
|
|
87
|
+
.then((res) => {
|
|
88
|
+
if (counter === tables.length - 1) return resolve();
|
|
89
|
+
})
|
|
90
|
+
.catch((error) => {
|
|
91
|
+
if (counter === tables.length - 1) return resolve();
|
|
92
|
+
});
|
|
169
93
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
dropAllIndexes: async (tableName) => {
|
|
98
|
+
return new Promise(async (resolve, reject) => {
|
|
99
|
+
// Fetch all indexes from the table
|
|
100
|
+
let query = `SHOW INDEXES FROM ??`;
|
|
101
|
+
query = mysql.format(query, [tableName]);
|
|
102
|
+
|
|
103
|
+
var indexes;
|
|
104
|
+
sequelize
|
|
105
|
+
.query(query)
|
|
106
|
+
.then((res) => {
|
|
107
|
+
indexes = res;
|
|
108
|
+
|
|
109
|
+
// Filter out primary key index and unique indexes if you don't want to drop them
|
|
110
|
+
const filteredIndexes = indexes[0].filter(
|
|
111
|
+
(index) => index.Key_name !== 'PRIMARY' && !index.Non_unique
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
if (!filteredIndexes?.length) return resolve();
|
|
115
|
+
|
|
116
|
+
// Drop each index
|
|
117
|
+
filteredIndexes.forEach(async (index, counter) => {
|
|
118
|
+
let query = `DROP INDEX ?? ON ??`;
|
|
119
|
+
query = mysql.format(query, [index.Key_name, tableName]);
|
|
120
|
+
sequelize
|
|
121
|
+
.query(query)
|
|
122
|
+
.then((res) => {
|
|
123
|
+
console.log(`Dropped index ${index.Key_name}`);
|
|
124
|
+
if (counter === filteredIndexes.length - 1) return resolve();
|
|
125
|
+
})
|
|
126
|
+
.catch((error) => {
|
|
127
|
+
if (counter === filteredIndexes.length - 1)
|
|
128
|
+
return resolve();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
})
|
|
132
|
+
.catch(() => {
|
|
133
|
+
return resolve();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
get: (modelName) => {
|
|
139
|
+
if (modelName != "") {
|
|
140
|
+
if (sequelize.models[modelName]) {
|
|
141
|
+
const model = sequelize.models[modelName];
|
|
142
|
+
const keys = Object.keys(model.rawAttributes);
|
|
143
|
+
let obj = {};
|
|
144
|
+
keys.map((key) => {
|
|
145
|
+
obj[key] = null;
|
|
146
|
+
});
|
|
147
|
+
return obj;
|
|
148
|
+
} else return;
|
|
149
|
+
} else {
|
|
150
|
+
return (models = Object.keys(sequelize.models).map((modelName) => {
|
|
151
|
+
const model = sequelize.models[modelName];
|
|
152
|
+
const keys = Object.keys(model.rawAttributes);
|
|
153
|
+
return {
|
|
154
|
+
modelName,
|
|
155
|
+
keys,
|
|
156
|
+
};
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
getUser: async (type) => {
|
|
162
|
+
return new Promise(async (resolve, reject) => {
|
|
163
|
+
const user = await sequelize.models['users'].findOne({
|
|
164
|
+
where: {
|
|
165
|
+
type: type,
|
|
166
|
+
},
|
|
220
167
|
});
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
168
|
+
return resolve(user);
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
insertUser: async (type, password, level) => {
|
|
173
|
+
return new Promise(async (resolve, reject) => {
|
|
174
|
+
// Insert a record if it doesn't exist
|
|
175
|
+
const res = await sequelize.models['users'].findOrCreate({
|
|
176
|
+
where: { type: type },
|
|
177
|
+
defaults: {
|
|
178
|
+
password: password,
|
|
179
|
+
level: level,
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
.then(([user, created]) => {
|
|
183
|
+
if (created) {
|
|
184
|
+
console.log('New user created:', type);
|
|
185
|
+
} else {
|
|
186
|
+
console.log('User already exists:', type);
|
|
187
|
+
}
|
|
188
|
+
return resolve(user);
|
|
189
|
+
})
|
|
190
|
+
.catch((error) => {
|
|
191
|
+
console.error('Error:', error);
|
|
192
|
+
return reject(error);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
insertClient: async (nick, token, user_id) => {
|
|
198
|
+
return new Promise(async (resolve, reject) => {
|
|
199
|
+
// Insert a record if it doesn't exist
|
|
200
|
+
const res = await sequelize.models['clients'].findOrCreate({
|
|
201
|
+
where: { nick: nick },
|
|
202
|
+
defaults: {
|
|
203
|
+
user_id: user_id,
|
|
204
|
+
token: token,
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
.then(([client, created]) => {
|
|
208
|
+
if (created) {
|
|
209
|
+
console.log('New client created:', nick);
|
|
210
|
+
} else {
|
|
211
|
+
console.log('Client already exists:', nick);
|
|
212
|
+
}
|
|
213
|
+
return resolve(client);
|
|
214
|
+
})
|
|
215
|
+
.catch((error) => {
|
|
216
|
+
console.error('Error:', error);
|
|
217
|
+
return reject(error);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
insertProject: async(project) => {
|
|
223
|
+
return new Promise(async (resolve, reject) => {
|
|
224
|
+
// Insert a record if it doesn't exist
|
|
225
|
+
const res = await sequelize.models['projects'].findOrCreate({
|
|
226
|
+
where : { name: project?.name },
|
|
227
|
+
defaults : {
|
|
228
|
+
description : "firmware running on esp32",
|
|
229
|
+
project_table : project?.name,
|
|
230
|
+
logs_table : "logs_"+project?.name,
|
|
231
|
+
uidPrefix : project?.uidPrefix,
|
|
232
|
+
uidLength : project?.uidLength,
|
|
233
|
+
},
|
|
234
|
+
})
|
|
235
|
+
.then(([client, created]) => {
|
|
236
|
+
if (created) {
|
|
237
|
+
console.log('New project created:', project?.name);
|
|
238
|
+
} else {
|
|
239
|
+
console.log('Project already exists:', project?.name);
|
|
240
|
+
}
|
|
241
|
+
return resolve(client);
|
|
242
|
+
})
|
|
243
|
+
.catch((error) => {
|
|
244
|
+
console.error('Error:', error);
|
|
245
|
+
return reject(error);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
};
|
|
@@ -16,6 +16,14 @@ module.exports = (sequelize,DataTypes)=>{
|
|
|
16
16
|
type: DataTypes.STRING,
|
|
17
17
|
allowNull: true
|
|
18
18
|
},
|
|
19
|
+
uidPrefix : {
|
|
20
|
+
type: DataTypes.STRING,
|
|
21
|
+
allowNull: false
|
|
22
|
+
},
|
|
23
|
+
uidLength : {
|
|
24
|
+
type: DataTypes.INTEGER,
|
|
25
|
+
allowNull: false
|
|
26
|
+
}
|
|
19
27
|
},
|
|
20
28
|
{
|
|
21
29
|
tableName: 'projects',
|