Commit 0a6bf0aecbb2410d3f1e3e90a7e1a0b46f842683

Authored by Marcelo Puebla
1 parent c9b8362ad7
Exists in master

cambios jose

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