Commit c9b8362ad7b6d3d491892f350b09e5912fd7cbb1

Authored by Nicolás Guarnieri
1 parent f72e635e44
Exists in master

pruebas con exito para telemetria

Showing 3 changed files with 120 additions and 0 deletions   Show diff stats
File was created 1
2 node_modules/
3
4 package-lock\.json
5
File was created 1 // Include Nodejs' net module.
2 const Net = require('net');
3 const converter = require('hex2dec');
4 // The port number and hostname of the server.
5 const port = 10001;
6 const host = '190.15.193.76';
7 var respuesta = '';
8
9 // Create a new TCP client.
10 const client = new Net.Socket();
11 // Send a connection request to the server.
12 client.connect({ port: port, host: host }, function() {
13 // If there is no error, the server has accepted the request and created a new
14 // socket dedicated to us.
15 console.log('TCP connection established with the server.');
16 var code = String.fromCharCode(01);
17 // The client can now send data to the server by writing to its socket.
18 console.info(client.write(code + 'i201TT'));
19 });
20
21 // The client can also receive data from the server by reading from its socket.
22 client.on('data', function(chunk) {
23 var data = chunk.toString('utf8');
24 if (respuesta) {
25 respuesta += data;
26 } else {
27 var respuesta = data;
28 }
29
30 if (data[data.length - 1].charCodeAt(0) == 3 && respuesta.slice(1,5) == 'i201') {
31 var indice = 0;
32 var fecha = new Date();
33 var respuesta = respuesta.slice(17, respuesta.length);
34 respuesta = respuesta.split('&&')[0];
35 console.info(procesar(respuesta));
36 }
37
38 // Request an end to the connection after the data has been received.
39 client.end();
40 });
41
42 client.on('end', function() {
43 console.log('Requested an end to the TCP connection');
44 });
45
46 function procesar(respuesta) {
47 var tanque = {
48 tanque: respuesta.slice(0,2),
49 producto: respuesta.slice(2,3),
50 estado: respuesta.slice(3,7),
51 campos: []
52 }
53
54 var campos = parseInt(respuesta.slice(7,9));
55
56 var inicio = 9;
57 var fin = 17;
58
59 for (var i = 0; i < campos; i++) {
60 tanque.campos.push(procesarCampo(respuesta.slice(inicio,fin)));
61 inicio += 8;
62 fin += 8;
63 }
64
65 if ((fin - 8) < respuesta.length) {
66 var respuestas = procesar(respuesta.slice((fin - 8), respuesta.length));
67 } else {
68 var respuestas = [];
69 }
70
71 respuestas.unshift(tanque);
72 return respuestas;
73 }
74
75 function procesarCampo(campo) {
76 var proceso = '';
77
78 if (parseInt(campo) == 0) {
79 return 0;
80 }
81
82 for (var i = 0; i < campo.length; i++) {
83 proceso += hexToBinary(campo[i]);
84 }
85
86 var signo = proceso[0] == 0 ? 1 : -1;
87 var exponente = parseInt(proceso.slice(1, 9), 2);
88 var mantiza = parseInt(proceso.slice(9, 32), 2);
89
90 return signo * Math.pow(2, exponente - 127) * (1 + (mantiza / 8388608));
91 }
92
93 function hexToBinary(digito) {
94 var binario = parseInt(digito, 16).toString(2);
95
96 while (binario.length < 4) {
97 binario = '0' + binario;
98 }
99
100 return binario;
101 }
File was created 1 {
2 "name": "tcp-prueba",
3 "version": "1.0.0",
4 "description": "",
5 "main": "index.js",
6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1"
8 },
9 "author": "",
10 "license": "ISC",
11 "dependencies": {
12 "hex2dec": "^1.1.2",
13 "net": "^1.0.2"
14 }
15 }
16