You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.7 KiB
58 lines
1.7 KiB
//Se declara express
|
|
const express = require("express");
|
|
const res = require("express/lib/response");
|
|
|
|
//Se inicializa express
|
|
var app = express();
|
|
|
|
//Conexión MariaDB
|
|
const pool = require('../../db')
|
|
|
|
//Agrega el mensaje a la BD
|
|
async function addDevice(device) {
|
|
try{
|
|
//Se utiliza solo pool.query porque se necesita solo realizar una consulta y cerrar la conexión a la BD
|
|
//pool.getConnection() deja abierta la conexión y luego de unos segundos crashea por no utilizarla
|
|
//const conn = await pool.getConnection();
|
|
|
|
|
|
//Se listan todos los firmwares de ese producto
|
|
const listFirmwares = await pool.query(`SELECT firmware_id FROM main_database.firmwares WHERE product_id = ${device.product_id} ORDER BY date DESC`);
|
|
|
|
//Se filtra el último firmware subido a ese dispositivo
|
|
const lastFirmware = listFirmwares[0].firmware_id;
|
|
const rows = await pool.query(`INSERT INTO main_database.devices (chip_id, product_id, firmware_id_act, firmware_id_update) VALUES ('${device.chip_id}', ${device.product_id}, ${lastFirmware}, ${lastFirmware})`);
|
|
console.log(lastFirmware)
|
|
return rows;
|
|
}catch(err){
|
|
if(err.code == 'ER_DUP_ENTRY'){
|
|
return err.code
|
|
}else{
|
|
return 'nohayfirmware';
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function getDevices(filterDevices) {
|
|
console.log(filterDevices);
|
|
var myQuery = 'SELECT * FROM main_database.devices';
|
|
if(filterDevices != null){
|
|
myQuery += ` WHERE product_id = ${filterDevices} ORDER BY device_id DESC`;
|
|
}else{
|
|
myQuery += ' ORDER BY device_id DESC';
|
|
}
|
|
return new Promise(async (resolve, reject) => {
|
|
//const conn = await pool.getConnection();
|
|
const rows = await pool.query(myQuery);
|
|
resolve(rows);
|
|
});
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
add: addDevice,
|
|
list: getDevices,
|
|
|
|
};
|
|
|