UNPKG

mgmt-iot-web

Version:

web platform to configure and interact with iot devices using mqtt

267 lines (220 loc) 6.6 kB
var path = require('path'); const fs = require('fs') var mysql = require('mysql2'); var db = require('../controllers/db'); var CryptoJS = require("crypto-js"); const moment = require('moment'); module.exports = { getFirmwareToken : (fw_token,filename,cb)=>{ var query = `select * from ?? where token = ? and originalname = ?`; var table = ["firmwares",fw_token,filename]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }) }, list : (cb)=>{ var query = `select * from ??`; var table = ["firmwares"]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }) }, listWithClientPermission : (clientId, cb)=>{ var query = `select f.* from firmwares as f inner join modelPermissions as mp on mp.client_id = ? and mp.model_id = f.model_id`; var table = [clientId]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }) }, listByModel : (modelId,cb)=>{ var query = `select * from ?? where model_id = ?`; var table = ["firmwares",modelId]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }) }, listByModelWithClientPermission : (clientId, modelId,cb)=>{ var query = ` SELECT f.* FROM firmwares AS f INNER JOIN modelPermissions AS mp ON mp.model_id = f.model_id WHERE mp.client_id = ? AND f.model_id = ?`; var table = [clientId,modelId]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }) }, add : async (filename,originalname,version,app_version,modelId,cb)=>{ let query = "select * from ?? where ?? = ? and ?? = ? and ?? = ?"; let table = ["firmwares","version",version,"app_version",app_version,"model_id",modelId]; query = mysql.format(query,table); db.queryRow(query) .then(rows => { if(rows.length > 0){ return cb("this version already exists for this model, try increase it",null) }else{ var token = ""; var SHA256 = require("crypto-js/sha256"); let message = originalname+"\º~"+version+app_version; let key = String(Date.now()/3621) var token = CryptoJS.HmacSHA256(message, key).toString(); let obj = { filename : filename, originalname : originalname, version : version, app_version : app_version, model_id : modelId, token : token, createdAt : moment().utc().format('YYYY-MM-DD HH:mm:ss'), updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss') } db.insert("firmwares",obj) .then( rows => { return cb(null,rows); }) .catch( error => { return cb(error, null); }) } }) .catch(error => { return cb(error,null); }) }, delete : (id,cb)=>{ var query = `select * from ?? where id = ?`; var table = ["firmwares",id]; query = mysql.format(query,table); var filename = ""; db.queryRow(query) .then(rows => { if(rows.length == 0) return cb(null,rows); else{ filename = rows[0].filename; let filter = { id : id } db.delete("firmwares",filter) .then (rows => { let filePath = ""; if( process.env?.NODE_ENV?.toLowerCase().includes("docker") ){ filePath = "/mgmt-iot/devices/firmwares"; }else{ file_path = path.join(__dirname, "../public/firmwares/"+filename); } fs.unlinkSync(file_path) return cb(null,rows); }) .catch(error => { return cb(error,null); }); } }) .catch(error => { return cb(error,null); }) }, /* update : (firmwareId,cb)=>{ let obj = { updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss') }; let filter = { id : firmwareId }; db.update("firmwares",obj,filter) .then (rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }); }, */ updateRelease : (firmwareId,release,cb)=>{ let obj = { build_release : release, updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss') }; let filter = { id : firmwareId }; db.update("firmwares",obj,filter) .then (rows => { return cb(null,rows); }) .catch(error => { return cb(error,null); }); }, getLatestVersion : async (modelId,release)=>{ return new Promise((resolve,reject) => { let query = ""; let table = []; query = `SELECT version,filename,token,id FROM firmwares where model_id = ? and build_release = ? ORDER BY CAST(SUBSTRING_INDEX(version, '.', 1) AS UNSIGNED) DESC, CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(version, '.', 2), '.', -1) AS UNSIGNED) DESC, CAST(SUBSTRING_INDEX(version, '.', -1) AS UNSIGNED) DESC LIMIT 1`; table = [modelId,release]; query = mysql.format(query,table); db.queryRow(query) .then( rows => { if(rows.length > 0){ return resolve(rows[0]); }else return resolve(null); }) .catch( err => { console.log(err); return resolve(null); }); }); }, getLatestAppVersion : async (modelId,release)=>{ return new Promise((resolve,reject) => { let query = ""; let table = []; query = `SELECT app_version,filename,token,id FROM firmwares where model_id = ? and build_release = ? ORDER BY CAST(SUBSTRING_INDEX(app_version, '.', 1) AS UNSIGNED) DESC, CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(app_version, '.', 2), '.', -1) AS UNSIGNED) DESC, CAST(SUBSTRING_INDEX(app_version, '.', -1) AS UNSIGNED) DESC LIMIT 1`; table = [modelId,release]; query = mysql.format(query,table); db.queryRow(query) .then( rows => { if(rows.length > 0) return resolve(rows[0]); else return resolve(null); }) .catch( err => { console.log(err); return resolve(null); }); }); }, };