Commit 179ad7a7fa9fc0550dce59d3182dd7103f8f44f0

Authored by Eric
1 parent 4d28a856ac
Exists in master

primera versión estable

Showing 2 changed files with 90 additions and 86 deletions   Show diff stats
1 // Include Nodejs' net module. 1 //express
2 const Net = require('net'); 2 const express = require('express');
3 const converter = require('hex2dec'); 3 const app = express();
4 // The port number and hostname of the server. 4 const config = require('./config/config.json');
5 const port = 10001; 5 const socket = require('./socket/socket.js');
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 });
20 6
21 // The client can also receive data from the server by reading from its socket. 7 const port = config.puerto;
22 client.on('data', function(chunk) { 8 const host = config.direccion;
23 var data = chunk.toString('utf8'); 9 var fecha;
24 console.log('response', data); 10
25 if (respuesta) { 11 app.listen(4500)
26 respuesta += data; 12
27 } else { 13 app.get('/tank-info/:tank*?', function (req, res) {
28 var respuesta = data; 14
29 } 15 socket.initConection(port, host);
30 16
31 if (data[data.length - 1].charCodeAt(0) == 3) { 17 var numberTank = req.params.tank ? req.params.tank : '00';
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 });
42 18
43 client.on('end', function() { 19 socket.getTank('i201' + numberTank).then(function (response) {
44 console.log('Requested an end to the TCP connection'); 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) { 33 function getJson(tanks) {
48 var tanque = {
49 tanque: respuesta.slice(0,2),
50 producto: respuesta.slice(2,3),
51 estado: respuesta.slice(3,7),
52 campos: []
53 }
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; 40 while (tanks.length > 10) {
58 var fin = 17;
59 41
60 for (var i = 0; i < campos; i++) { 42 var stringTank = tanks.slice(0, 9);
61 tanque.campos.push(procesarCampo(respuesta.slice(inicio,fin))); 43 var cantOfProperties = stringTank.length + (parseInt(tanks.slice(7, 9)) * 8);
62 inicio += 8; 44 stringTank += tanks.slice(9, cantOfProperties)
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 }
71 45
72 respuestas.unshift(tanque); 46 arraysTanks.push(stringTank);
73 return respuestas;
74 }
75 47
76 function procesarCampo(campo) { 48 tanks = tanks.slice(stringTank.length, tanks.length);
77 var proceso = '';
78
79 if (parseInt(campo) == 0) {
80 return 0;
81 } 49 }
82 50
83 for (var i = 0; i < campo.length; i++) { 51 arraysTanks.forEach(function (tank) {
84 proceso += hexToBinary(campo[i]); 52 var objTank = {
85 } 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)),
1 { 1 {
2 "name": "tcp-prueba", 2 "name": "tcp-prueba",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 "description": "", 4 "description": "",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 "author": "", 9 "author": "",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 "express": "^4.16.4",
12 "hex2dec": "^1.1.2", 13 "hex2dec": "^1.1.2",
13 "net": "^1.0.2" 14 "net": "^1.0.2"
14 } 15 }
15 } 16 }
16 17