Commit 179ad7a7fa9fc0550dce59d3182dd7103f8f44f0
1 parent
4d28a856ac
Exists in
master
primera versión estable
Showing
2 changed files
with
90 additions
and
86 deletions
Show diff stats
index.js
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 = 'fipdebo.ddns.net'; | |
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 + 'i20100')); | |
19 | -}); | |
1 | +//express | |
2 | +const express = require('express'); | |
3 | +const app = express(); | |
4 | +const config = require('./config/config.json'); | |
5 | +const socket = require('./socket/socket.js'); | |
20 | 6 | |
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 | - console.log('response', data); | |
25 | - if (respuesta) { | |
26 | - respuesta += data; | |
27 | - } else { | |
28 | - var respuesta = data; | |
29 | - } | |
30 | - | |
31 | - if (data[data.length - 1].charCodeAt(0) == 3) { | |
32 | - var indice = 0; | |
33 | - var fecha = new Date(); | |
34 | - var respuesta = respuesta.slice(17, respuesta.length); | |
35 | - respuesta = respuesta.split('&&')[0]; | |
36 | - console.info(procesar(respuesta)); | |
37 | - } | |
38 | - | |
39 | - // Request an end to the connection after the data has been received. | |
40 | - client.end(); | |
41 | -}); | |
7 | +const port = config.puerto; | |
8 | +const host = config.direccion; | |
9 | +var fecha; | |
10 | + | |
11 | +app.listen(4500) | |
12 | + | |
13 | +app.get('/tank-info/:tank*?', function (req, res) { | |
14 | + | |
15 | + socket.initConection(port, host); | |
16 | + | |
17 | + var numberTank = req.params.tank ? req.params.tank : '00'; | |
42 | 18 | |
43 | -client.on('end', function() { | |
44 | - console.log('Requested an end to the TCP connection'); | |
19 | + socket.getTank('i201' + numberTank).then(function (response) { | |
20 | + | |
21 | + //obtengo una cadena de caracteres, los primeros 17 datos son de informacion de tanque | |
22 | + // e información de la fecha | |
23 | + fecha = response.slice(7, 16); | |
24 | + | |
25 | + var dataTanks = getJson(response.slice(17, response.length)); | |
26 | + | |
27 | + res.status(200).send(dataTanks); | |
28 | + | |
29 | + socket.end(); | |
30 | + }); | |
45 | 31 | }); |
46 | 32 | |
47 | -function procesar(respuesta) { | |
48 | - var tanque = { | |
49 | - tanque: respuesta.slice(0,2), | |
50 | - producto: respuesta.slice(2,3), | |
51 | - estado: respuesta.slice(3,7), | |
52 | - campos: [] | |
53 | - } | |
33 | +function getJson(tanks) { | |
54 | 34 | |
55 | - var campos = parseInt(respuesta.slice(7,9)); | |
35 | + var json = { | |
36 | + tanques: [] | |
37 | + }; | |
38 | + var arraysTanks = []; | |
56 | 39 | |
57 | - var inicio = 9; | |
58 | - var fin = 17; | |
40 | + while (tanks.length > 10) { | |
59 | 41 | |
60 | - for (var i = 0; i < campos; i++) { | |
61 | - tanque.campos.push(procesarCampo(respuesta.slice(inicio,fin))); | |
62 | - inicio += 8; | |
63 | - fin += 8; | |
64 | - } | |
65 | - | |
66 | - if ((fin - 8) < respuesta.length) { | |
67 | - var respuestas = procesar(respuesta.slice((fin - 8), respuesta.length)); | |
68 | - } else { | |
69 | - var respuestas = []; | |
70 | - } | |
42 | + var stringTank = tanks.slice(0, 9); | |
43 | + var cantOfProperties = stringTank.length + (parseInt(tanks.slice(7, 9)) * 8); | |
44 | + stringTank += tanks.slice(9, cantOfProperties) | |
71 | 45 | |
72 | - respuestas.unshift(tanque); | |
73 | - return respuestas; | |
74 | -} | |
46 | + arraysTanks.push(stringTank); | |
75 | 47 | |
76 | -function procesarCampo(campo) { | |
77 | - var proceso = ''; | |
78 | - | |
79 | - if (parseInt(campo) == 0) { | |
80 | - return 0; | |
48 | + tanks = tanks.slice(stringTank.length, tanks.length); | |
81 | 49 | } |
82 | 50 | |
83 | - for (var i = 0; i < campo.length; i++) { | |
84 | - proceso += hexToBinary(campo[i]); | |
85 | - } | |
51 | + arraysTanks.forEach(function (tank) { | |
52 | + var objTank = { | |
53 | + tank: tank.slice(0, 2), | |
54 | + fecha: fecha, | |
55 | + product: tank.slice(2, 3), | |
56 | + deliveryProgress: tank.slice(3, 4), | |
57 | + testProgress: tank.slice(4, 5), | |
58 | + alarm: tank.slice(5, 6), | |
59 | + volume: getValue(tank.slice(9, 17)), | |
60 | + tCVolume: getValue(tank.slice(17, 25)), | |
61 | + ullage: getValue(tank.slice(25, 33)), | |
62 | + altura: getValue(tank.slice(33, 41)), | |
63 | + agua: getValue(tank.slice(41, 49)), | |
64 | + temperatura: getValue(tank.slice(49, 57)), | |
65 | + alturaAgua: getValue(tank.slice(57, 65)), | |
66 | + } | |
67 | + | |
68 | + json.tanques.push(objTank); | |
69 | + }); | |
86 | 70 | |
87 | - var signo = proceso[0] == 0 ? 1 : -1; | |
88 | - var exponente = parseInt(proceso.slice(1, 9), 2); | |
89 | - var mantiza = parseInt(proceso.slice(9, 32), 2); | |
71 | + json.cantidadTanques = json.tanques.length; | |
90 | 72 | |
91 | - return signo * Math.pow(2, exponente - 127) * (1 + (mantiza / 8388608)); | |
73 | + return json; | |
92 | 74 | } |
93 | 75 | |
94 | -function hexToBinary(digito) { | |
95 | - var binario = parseInt(digito, 16).toString(2); | |
96 | - | |
97 | - while (binario.length < 4) { | |
98 | - binario = '0' + binario; | |
76 | +function getValue(property) { | |
77 | + | |
78 | + var binary = parseInt(property, 16).toString(2); | |
79 | + | |
80 | + if (binary.length < 32) { | |
81 | + | |
82 | + var difence = 32 - binary.length; | |
83 | + | |
84 | + var ceros = ''; | |
85 | + | |
86 | + for (let i = 0; i < difence; i++) { | |
87 | + ceros += '0'; | |
88 | + } | |
89 | + | |
90 | + binary = ceros + binary; | |
99 | 91 | } |
100 | - | |
101 | - return binario; | |
102 | -} | |
103 | 92 | \ No newline at end of file |
93 | + | |
94 | + var e = parseInt(binary.slice(1, 9), 2).toString(10); | |
95 | + | |
96 | + var m = parseInt(binary.slice(9), 2).toString(10); | |
97 | + | |
98 | + var exponent = 2 ** (e - 127); | |
99 | + | |
100 | + | |
101 | + if(parseInt(binary.slice(0, 1))) exponent = exponent * -1; | |
102 | + | |
103 | + var mantissa = 1.0 + (m / 8388608); | |
104 | + | |
105 | + return parseFloat(exponent * mantissa).toFixed(2); | |
106 | +} |