From c9b8362ad7b6d3d491892f350b09e5912fd7cbb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s?= Date: Thu, 28 Feb 2019 17:27:28 -0300 Subject: [PATCH] pruebas con exito para telemetria --- .gitignore | 4 +++ index.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 +++++++++ 3 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27fa5eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +node_modules/ + +package-lock\.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..7a9f140 --- /dev/null +++ b/index.js @@ -0,0 +1,101 @@ +// 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 = '190.15.193.76'; +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 + 'i201TT')); +}); + +// The client can also receive data from the server by reading from its socket. +client.on('data', function(chunk) { + var data = chunk.toString('utf8'); + if (respuesta) { + respuesta += data; + } else { + var respuesta = data; + } + + if (data[data.length - 1].charCodeAt(0) == 3 && respuesta.slice(1,5) == 'i201') { + 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(); +}); + +client.on('end', function() { + console.log('Requested an end to the TCP connection'); +}); + +function procesar(respuesta) { + var tanque = { + tanque: respuesta.slice(0,2), + producto: respuesta.slice(2,3), + estado: respuesta.slice(3,7), + campos: [] + } + + var campos = parseInt(respuesta.slice(7,9)); + + var inicio = 9; + var fin = 17; + + 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 = []; + } + + respuestas.unshift(tanque); + return respuestas; +} + +function procesarCampo(campo) { + var proceso = ''; + + if (parseInt(campo) == 0) { + return 0; + } + + for (var i = 0; i < campo.length; i++) { + proceso += hexToBinary(campo[i]); + } + + var signo = proceso[0] == 0 ? 1 : -1; + var exponente = parseInt(proceso.slice(1, 9), 2); + var mantiza = parseInt(proceso.slice(9, 32), 2); + + return signo * Math.pow(2, exponente - 127) * (1 + (mantiza / 8388608)); +} + +function hexToBinary(digito) { + var binario = parseInt(digito, 16).toString(2); + + while (binario.length < 4) { + binario = '0' + binario; + } + + return binario; +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..66d9302 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "tcp-prueba", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "hex2dec": "^1.1.2", + "net": "^1.0.2" + } +} -- 1.9.1