Commit 66da13c695e89915577b877f57237f7a1f000b49
1 parent
e21c618a70
Exists in
master
console log on query
Showing
1 changed file
with
2 additions
and
0 deletions
Show diff stats
index.js
1 | //express | 1 | //express |
2 | const express = require('express'); | 2 | const express = require('express'); |
3 | const app = express(); | 3 | const app = express(); |
4 | const config = require('./config/config.json'); | 4 | const config = require('./config/config.json'); |
5 | const socket = require('./socket/socket.js'); | 5 | const socket = require('./socket/socket.js'); |
6 | 6 | ||
7 | const port = config.servidorveeder.port; | 7 | const port = config.servidorveeder.port; |
8 | const host = config.servidorveeder.direccion; | 8 | const host = config.servidorveeder.direccion; |
9 | var fecha; | 9 | var fecha; |
10 | 10 | ||
11 | app.listen(config.servidornode.port); | 11 | app.listen(config.servidornode.port); |
12 | 12 | ||
13 | app.get('/tank-info/:tank*?', function (req, res) { | 13 | app.get('/tank-info/:tank*?', function (req, res) { |
14 | 14 | ||
15 | console.log('Se realiza consulta de tankes, ' + req.ip); | ||
16 | |||
15 | socket.initConection(port, host); | 17 | socket.initConection(port, host); |
16 | 18 | ||
17 | var numberTank = req.params.tank ? req.params.tank : '00'; | 19 | var numberTank = req.params.tank ? req.params.tank : '00'; |
18 | 20 | ||
19 | socket.getTank('i201' + numberTank).then(function (response) { | 21 | socket.getTank('i201' + numberTank).then(function (response) { |
20 | 22 | ||
21 | //obtengo una cadena de caracteres, los primeros 17 datos son de informacion de tanque | 23 | //obtengo una cadena de caracteres, los primeros 17 datos son de informacion de tanque |
22 | // e información de la fecha | 24 | // e información de la fecha |
23 | fecha = response.slice(7, 17); | 25 | fecha = response.slice(7, 17); |
24 | 26 | ||
25 | var dataTanks = getJson(response.slice(17, response.length)); | 27 | var dataTanks = getJson(response.slice(17, response.length)); |
26 | 28 | ||
27 | res.status(200).send(dataTanks); | 29 | res.status(200).send(dataTanks); |
28 | 30 | ||
29 | socket.end(); | 31 | socket.end(); |
30 | }); | 32 | }); |
31 | }); | 33 | }); |
32 | 34 | ||
33 | function getJson(tanks) { | 35 | function getJson(tanks) { |
34 | 36 | ||
35 | var json = { | 37 | var json = { |
36 | tanques: [] | 38 | tanques: [] |
37 | }; | 39 | }; |
38 | var arraysTanks = []; | 40 | var arraysTanks = []; |
39 | 41 | ||
40 | while (tanks.length > 10) { | 42 | while (tanks.length > 10) { |
41 | 43 | ||
42 | var stringTank = tanks.slice(0, 9); | 44 | var stringTank = tanks.slice(0, 9); |
43 | var cantOfProperties = stringTank.length + (parseInt(tanks.slice(7, 9)) * 8); | 45 | var cantOfProperties = stringTank.length + (parseInt(tanks.slice(7, 9)) * 8); |
44 | stringTank += tanks.slice(9, cantOfProperties) | 46 | stringTank += tanks.slice(9, cantOfProperties) |
45 | 47 | ||
46 | arraysTanks.push(stringTank); | 48 | arraysTanks.push(stringTank); |
47 | 49 | ||
48 | tanks = tanks.slice(stringTank.length, tanks.length); | 50 | tanks = tanks.slice(stringTank.length, tanks.length); |
49 | } | 51 | } |
50 | 52 | ||
51 | arraysTanks.forEach(function (tank) { | 53 | arraysTanks.forEach(function (tank) { |
52 | var objTank = { | 54 | var objTank = { |
53 | tank: tank.slice(0, 2), | 55 | tank: tank.slice(0, 2), |
54 | fecha: fecha, | 56 | fecha: fecha, |
55 | product: tank.slice(2, 3), | 57 | product: tank.slice(2, 3), |
56 | deliveryProgress: tank.slice(3, 4), | 58 | deliveryProgress: tank.slice(3, 4), |
57 | testProgress: tank.slice(4, 5), | 59 | testProgress: tank.slice(4, 5), |
58 | alarm: tank.slice(5, 6), | 60 | alarm: tank.slice(5, 6), |
59 | volume: getValue(tank.slice(9, 17)), | 61 | volume: getValue(tank.slice(9, 17)), |
60 | tCVolume: getValue(tank.slice(17, 25)), | 62 | tCVolume: getValue(tank.slice(17, 25)), |
61 | ullage: getValue(tank.slice(25, 33)), | 63 | ullage: getValue(tank.slice(25, 33)), |
62 | altura: getValue(tank.slice(33, 41)), | 64 | altura: getValue(tank.slice(33, 41)), |
63 | agua: getValue(tank.slice(41, 49)), | 65 | agua: getValue(tank.slice(41, 49)), |
64 | temperatura: getValue(tank.slice(49, 57)), | 66 | temperatura: getValue(tank.slice(49, 57)), |
65 | alturaAgua: getValue(tank.slice(57, 65)), | 67 | alturaAgua: getValue(tank.slice(57, 65)), |
66 | } | 68 | } |
67 | 69 | ||
68 | json.tanques.push(objTank); | 70 | json.tanques.push(objTank); |
69 | }); | 71 | }); |
70 | 72 | ||
71 | json.cantidadTanques = json.tanques.length; | 73 | json.cantidadTanques = json.tanques.length; |
72 | 74 | ||
73 | return json; | 75 | return json; |
74 | } | 76 | } |
75 | 77 | ||
76 | function getValue(property) { | 78 | function getValue(property) { |
77 | 79 | ||
78 | var binary = parseInt(property, 16).toString(2); | 80 | var binary = parseInt(property, 16).toString(2); |
79 | 81 | ||
80 | if (binary.length < 32) { | 82 | if (binary.length < 32) { |
81 | 83 | ||
82 | var difence = 32 - binary.length; | 84 | var difence = 32 - binary.length; |
83 | 85 | ||
84 | var ceros = ''; | 86 | var ceros = ''; |
85 | 87 | ||
86 | for (let i = 0; i < difence; i++) { | 88 | for (let i = 0; i < difence; i++) { |
87 | ceros += '0'; | 89 | ceros += '0'; |
88 | } | 90 | } |
89 | 91 | ||
90 | binary = ceros + binary; | 92 | binary = ceros + binary; |
91 | } | 93 | } |
92 | 94 | ||
93 | var e = parseInt(binary.slice(1, 9), 2).toString(10); | 95 | var e = parseInt(binary.slice(1, 9), 2).toString(10); |
94 | 96 | ||
95 | var m = parseInt(binary.slice(9), 2).toString(10); | 97 | var m = parseInt(binary.slice(9), 2).toString(10); |
96 | 98 | ||
97 | var exponent = 2 ** (e - 127); | 99 | var exponent = 2 ** (e - 127); |
98 | 100 | ||
99 | 101 | ||
100 | if(parseInt(binary.slice(0, 1))) exponent = exponent * -1; | 102 | if(parseInt(binary.slice(0, 1))) exponent = exponent * -1; |
101 | 103 | ||
102 | var mantissa = 1.0 + (m / 8388608); | 104 | var mantissa = 1.0 + (m / 8388608); |
103 | 105 | ||
104 | return parseFloat(exponent * mantissa).toFixed(2); | 106 | return parseFloat(exponent * mantissa).toFixed(2); |
105 | } | 107 | } |
106 | 108 |