diff --git a/index.js b/index.js index 30a792c..e37ccba 100644 --- a/index.js +++ b/index.js @@ -1,102 +1,105 @@ -// Include Nodejs' net module. -const Net = require('net'); -const converter = require('hex2dec'); -// The port number and hostname of the server. -const port = 10001; -const host = 'fipdebo.ddns.net'; -var respuesta = ''; - -// Create a new TCP client. -const client = new Net.Socket(); -// Send a connection request to the server. -client.connect({ port: port, host: host }, function() { - // If there is no error, the server has accepted the request and created a new - // socket dedicated to us. - console.log('TCP connection established with the server.'); - var code = String.fromCharCode(01); - // The client can now send data to the server by writing to its socket. - console.info(client.write(code + 'i20100')); -}); +//express +const express = require('express'); +const app = express(); +const config = require('./config/config.json'); +const socket = require('./socket/socket.js'); -// The client can also receive data from the server by reading from its socket. -client.on('data', function(chunk) { - var data = chunk.toString('utf8'); - console.log('response', data); - if (respuesta) { - respuesta += data; - } else { - var respuesta = data; - } - - if (data[data.length - 1].charCodeAt(0) == 3) { - var indice = 0; - var fecha = new Date(); - var respuesta = respuesta.slice(17, respuesta.length); - respuesta = respuesta.split('&&')[0]; - console.info(procesar(respuesta)); - } - - // Request an end to the connection after the data has been received. - client.end(); -}); +const port = config.puerto; +const host = config.direccion; +var fecha; + +app.listen(4500) + +app.get('/tank-info/:tank*?', function (req, res) { + + socket.initConection(port, host); + + var numberTank = req.params.tank ? req.params.tank : '00'; -client.on('end', function() { - console.log('Requested an end to the TCP connection'); + socket.getTank('i201' + numberTank).then(function (response) { + + //obtengo una cadena de caracteres, los primeros 17 datos son de informacion de tanque + // e información de la fecha + fecha = response.slice(7, 16); + + var dataTanks = getJson(response.slice(17, response.length)); + + res.status(200).send(dataTanks); + + socket.end(); + }); }); -function procesar(respuesta) { - var tanque = { - tanque: respuesta.slice(0,2), - producto: respuesta.slice(2,3), - estado: respuesta.slice(3,7), - campos: [] - } +function getJson(tanks) { - var campos = parseInt(respuesta.slice(7,9)); + var json = { + tanques: [] + }; + var arraysTanks = []; - var inicio = 9; - var fin = 17; + while (tanks.length > 10) { - for (var i = 0; i < campos; i++) { - tanque.campos.push(procesarCampo(respuesta.slice(inicio,fin))); - inicio += 8; - fin += 8; - } - - if ((fin - 8) < respuesta.length) { - var respuestas = procesar(respuesta.slice((fin - 8), respuesta.length)); - } else { - var respuestas = []; - } + var stringTank = tanks.slice(0, 9); + var cantOfProperties = stringTank.length + (parseInt(tanks.slice(7, 9)) * 8); + stringTank += tanks.slice(9, cantOfProperties) - respuestas.unshift(tanque); - return respuestas; -} + arraysTanks.push(stringTank); -function procesarCampo(campo) { - var proceso = ''; - - if (parseInt(campo) == 0) { - return 0; + tanks = tanks.slice(stringTank.length, tanks.length); } - for (var i = 0; i < campo.length; i++) { - proceso += hexToBinary(campo[i]); - } + arraysTanks.forEach(function (tank) { + var objTank = { + tank: tank.slice(0, 2), + fecha: fecha, + product: tank.slice(2, 3), + deliveryProgress: tank.slice(3, 4), + testProgress: tank.slice(4, 5), + alarm: tank.slice(5, 6), + volume: getValue(tank.slice(9, 17)), + tCVolume: getValue(tank.slice(17, 25)), + ullage: getValue(tank.slice(25, 33)), + altura: getValue(tank.slice(33, 41)), + agua: getValue(tank.slice(41, 49)), + temperatura: getValue(tank.slice(49, 57)), + alturaAgua: getValue(tank.slice(57, 65)), + } + + json.tanques.push(objTank); + }); - var signo = proceso[0] == 0 ? 1 : -1; - var exponente = parseInt(proceso.slice(1, 9), 2); - var mantiza = parseInt(proceso.slice(9, 32), 2); + json.cantidadTanques = json.tanques.length; - return signo * Math.pow(2, exponente - 127) * (1 + (mantiza / 8388608)); + return json; } -function hexToBinary(digito) { - var binario = parseInt(digito, 16).toString(2); - - while (binario.length < 4) { - binario = '0' + binario; +function getValue(property) { + + var binary = parseInt(property, 16).toString(2); + + if (binary.length < 32) { + + var difence = 32 - binary.length; + + var ceros = ''; + + for (let i = 0; i < difence; i++) { + ceros += '0'; + } + + binary = ceros + binary; } - - return binario; -} \ No newline at end of file + + var e = parseInt(binary.slice(1, 9), 2).toString(10); + + var m = parseInt(binary.slice(9), 2).toString(10); + + var exponent = 2 ** (e - 127); + + + if(parseInt(binary.slice(0, 1))) exponent = exponent * -1; + + var mantissa = 1.0 + (m / 8388608); + + return parseFloat(exponent * mantissa).toFixed(2); +} diff --git a/package.json b/package.json index 66d9302..4f8d599 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "", "license": "ISC", "dependencies": { + "express": "^4.16.4", "hex2dec": "^1.1.2", "net": "^1.0.2" }