Commit 8e683d5318f9f4deb2117443e441b59a6ffb452c
Exists in
master
Merge branch 'master' into 'master'
Master See merge request !18
Showing
3 changed files
Show diff stats
config/config.ejemplo.json
1 | { | 1 | { |
2 | "urlHO": "ws://localhost:5050", | ||
3 | "port": 6060 | ||
4 | } | ||
2 | "urlHO": "ws://10.231.45.234:5555", | ||
3 | "bo": { | ||
4 | "client": "mssql", | ||
5 | "connection": { | ||
6 | "host": "10.231.45.211", | ||
7 | "user": "sa", | ||
8 | "password": "xxzza", | ||
9 | "database": "UNITYOIL_28082017" | ||
10 | }, | ||
11 | "debug": false | ||
12 | }, | ||
13 | "gln": "7790968003283", |
data/index.js
1 | module.exports = function(config) { | 1 | module.exports = function(config) { |
2 | 2 | ||
3 | return { | 3 | return { |
4 | getEntity: function (message) { | 4 | getEntity: function (message) { |
5 | 5 | ||
6 | return new Promise(function(resolve, reject) { | 6 | return new Promise(function(resolve, reject) { |
7 | 7 | ||
8 | var promise; | 8 | var promise; |
9 | 9 | ||
10 | if (message.queryString) { | 10 | if (message.queryString) { |
11 | 11 | ||
12 | promise = knex.schema.raw(message.queryString); | 12 | promise = knex.schema.raw(message.queryString); |
13 | } else { | 13 | } else { |
14 | 14 | ||
15 | promise = knex(message.tableName).where(message.where).select('*'); | 15 | promise = knex(message.tableName).where(message.where).select('*'); |
16 | } | 16 | } |
17 | 17 | ||
18 | promise.then(function (data) { | 18 | promise.then(function (data) { |
19 | 19 | ||
20 | message.data = data[0]; | 20 | message.data = data[0]; |
21 | resolve(message); | 21 | resolve(message); |
22 | }); | 22 | }); |
23 | }); | 23 | }); |
24 | }, | 24 | }, |
25 | 25 | ||
26 | guardarComprobante: function (body) { | 26 | guardarComprobante: function (body) { |
27 | 27 | ||
28 | console.log('recibo datos guardarComprobante en estación'); | 28 | console.log('recibo datos guardarComprobante en estación'); |
29 | console.log(body); | 29 | console.log(body); |
30 | 30 | ||
31 | return new Promise((resolve, reject) => { | 31 | return new Promise((resolve, reject) => { |
32 | 32 | ||
33 | delete body.cabecera.idTransaccion; | 33 | delete body.cabecera.idTransaccion; |
34 | delete body.cabecera.GLN; | 34 | delete body.cabecera.GLN; |
35 | delete body.cabecera.C_HD2; | 35 | delete body.cabecera.C_HD2; |
36 | delete body.cabecera.E_HD2; | 36 | delete body.cabecera.E_HD2; |
37 | 37 | ||
38 | //smalldatetime | 38 | //smalldatetime |
39 | body.cabecera.FEC = new Date(body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' '); | 39 | body.cabecera.FEC = new Date(body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' '); |
40 | body.cabecera.FECCAI = new Date(body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' '); | 40 | body.cabecera.FECCAI = new Date(body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' '); |
41 | body.cabecera.FECVEN = new Date(body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' '); | 41 | body.cabecera.FECVEN = new Date(body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' '); |
42 | //datetime | 42 | //datetime |
43 | body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD'); | 43 | body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD'); |
44 | body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD'); | 44 | body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD'); |
45 | 45 | ||
46 | knex('PMAEFACT').insert(body.cabecera).then(() => { | 46 | knex('PMAEFACT').insert(body.cabecera).then(() => { |
47 | 47 | ||
48 | var promesas = [ | 48 | var promesas = [ |
49 | knex('PCOBYPAG').insert({ | 49 | knex('PCOBYPAG').insert({ |
50 | COD: body.cabecera.COD, | 50 | COD: body.cabecera.COD, |
51 | FEP: body.cabecera.FEC, | 51 | FEP: body.cabecera.FEC, |
52 | TIP: body.cabecera.TIP, | 52 | TIP: body.cabecera.TIP, |
53 | TCO: body.cabecera.TCO, | 53 | TCO: body.cabecera.TCO, |
54 | SUC: body.cabecera.SUC, | 54 | SUC: body.cabecera.SUC, |
55 | NCO: body.cabecera.NCO, | 55 | NCO: body.cabecera.NCO, |
56 | IPA: body.cabecera.TOT, | 56 | IPA: body.cabecera.TOT, |
57 | SAL: 0, | 57 | SAL: 0, |
58 | TCA: 1, | 58 | TCA: 1, |
59 | ZONA: body.cabecera.ZON, | 59 | ZONA: body.cabecera.ZON, |
60 | FPA: body.cabecera.FPA, | 60 | FPA: body.cabecera.FPA, |
61 | REC: 0, | 61 | REC: 0, |
62 | FER: '19000101 00:00', | 62 | FER: '19000101 00:00', |
63 | PRO: '', | 63 | PRO: '', |
64 | FEV: body.cabecera.FEV, | 64 | FEV: body.cabecera.FEV, |
65 | ANU: '', | 65 | ANU: '', |
66 | PLA: 0, | 66 | PLA: 0, |
67 | LUG: 0, | 67 | LUG: 0, |
68 | RES: 0, | 68 | RES: 0, |
69 | CCU: 0, | 69 | CCU: 0, |
70 | UCU: 0, | 70 | UCU: 0, |
71 | HOS: '', | 71 | HOS: '', |
72 | E_HD: '', | 72 | E_HD: '', |
73 | C_HD: '' | 73 | C_HD: '' |
74 | }), | 74 | }), |
75 | ]; | 75 | ]; |
76 | 76 | ||
77 | body.cuerpo.forEach(cuerpo => { | 77 | body.cuerpo.forEach(cuerpo => { |
78 | 78 | ||
79 | if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) { | 79 | if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) { |
80 | 80 | ||
81 | promesas.push(knex('AMOVSTOC').insert({ | 81 | promesas.push(knex('AMOVSTOC').insert({ |
82 | SEC: cuerpo.COD, | 82 | SEC: cuerpo.COD, |
83 | ART: cuerpo.ART, | 83 | ART: cuerpo.ART, |
84 | FEC: body.cabecera.FEC, | 84 | FEC: body.cabecera.FEC, |
85 | CYV: 'C', | 85 | CYV: 'C', |
86 | TIP: body.cabecera.TIP, | 86 | TIP: body.cabecera.TIP, |
87 | TCO: body.cabecera.TCO, | 87 | TCO: body.cabecera.TCO, |
88 | PVE: body.cabecera.SUC, | 88 | PVE: body.cabecera.SUC, |
89 | NCO: body.cabecera.NCO, | 89 | NCO: body.cabecera.NCO, |
90 | ORD: cuerpo.ORD, | 90 | ORD: cuerpo.ORD, |
91 | CAN: cuerpo.cantidadRecibida, | 91 | CAN: cuerpo.cantidadRecibida, |
92 | PUN: cuerpo.PUN, | 92 | PUN: cuerpo.PUN, |
93 | COD: body.cabecera.COD, | 93 | COD: body.cabecera.COD, |
94 | DTO: cuerpo.DTO, | 94 | DTO: cuerpo.DTO, |
95 | IMI: cuerpo.IMI, | 95 | IMI: cuerpo.IMI, |
96 | PLA: 0, | 96 | PLA: 0, |
97 | LUG: 0, | 97 | LUG: 0, |
98 | ANU: '', | 98 | ANU: '', |
99 | TIM: 'Co', | 99 | TIM: 'Co', |
100 | OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario | 100 | OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario |
101 | IMI2: cuerpo.IMI2, | 101 | IMI2: cuerpo.IMI2, |
102 | E_HD: '', | 102 | E_HD: '', |
103 | C_HD: '', | 103 | C_HD: '', |
104 | JUS: 'PLANEX', | 104 | JUS: 'PLANEX', |
105 | NLC: 0, | 105 | NLC: 0, |
106 | IMI3: cuerpo.IMI3, | 106 | IMI3: cuerpo.IMI3, |
107 | JJN: '', | 107 | JJN: '', |
108 | JDJ: '', | 108 | JDJ: '', |
109 | ID_MOTIVO_NANB: 0 | 109 | ID_MOTIVO_NANB: 0 |
110 | })); | 110 | })); |
111 | }; | 111 | }; |
112 | 112 | ||
113 | cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false; | 113 | cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false; |
114 | 114 | ||
115 | delete cuerpo.GLN; | 115 | delete cuerpo.GLN; |
116 | delete cuerpo.C_HD2; | 116 | delete cuerpo.C_HD2; |
117 | delete cuerpo.E_HD2; | 117 | delete cuerpo.E_HD2; |
118 | delete cuerpo.codigoBarras; | 118 | delete cuerpo.codigoBarras; |
119 | delete cuerpo.estado; | 119 | delete cuerpo.estado; |
120 | delete cuerpo.recibido; | 120 | delete cuerpo.recibido; |
121 | delete cuerpo.input; | 121 | delete cuerpo.input; |
122 | delete cuerpo.tempRecibido; | 122 | delete cuerpo.tempRecibido; |
123 | 123 | ||
124 | cuerpo.LI0 = cuerpo.LI0.slice(0, 29); | 124 | cuerpo.LI0 = cuerpo.LI0.slice(0, 29); |
125 | cuerpo.TIO = cuerpo.TIO.slice(0, 29); | 125 | cuerpo.TIO = cuerpo.TIO.slice(0, 29); |
126 | 126 | ||
127 | promesas.push(knex('PMOVFACT').insert(cuerpo)); | 127 | promesas.push(knex('PMOVFACT').insert(cuerpo)); |
128 | 128 | ||
129 | }); | 129 | }); |
130 | 130 | ||
131 | Promise.all(promesas) | 131 | Promise.all(promesas) |
132 | .then(function() { | 132 | .then(function() { |
133 | 133 | ||
134 | console.log('comprobantes guardados con éxito'); | 134 | console.log('comprobantes guardados con éxito'); |
135 | resolve(); | 135 | resolve(); |
136 | 136 | ||
137 | }) | 137 | }) |
138 | .catch(function(e) { | 138 | .catch(function(e) { |
139 | console.log(e); | 139 | console.log(e); |
140 | reject('Ocurrió un error'); | 140 | reject('Ocurrió un error'); |
141 | }); | 141 | }); |
142 | 142 | ||
143 | }) | 143 | }) |
144 | .catch(reject); | 144 | .catch(() => { |
145 | reject(510); | ||
146 | }); | ||
145 | 147 | ||
146 | }); | 148 | }); |
147 | } | 149 | } |
148 | } | 150 | } |
149 | } | 151 | } |
150 | 152 |
index.js
1 | const WebSocketClient = require('ws'); | 1 | const WebSocketClient = require('ws'); |
2 | const config = require('./config/config.json'); | 2 | const config = require('./config/config.json'); |
3 | const data = require('./data/index')(config.bo); | 3 | const data = require('./data/index')(config.bo); |
4 | module.exports = | 4 | module.exports = |
5 | knex = require('knex')(config.bo), | 5 | knex = require('knex')(config.bo), |
6 | moment = require('moment'); | 6 | moment = require('moment'); |
7 | 7 | ||
8 | const client = new WebSocketClient(config.urlHO); | 8 | const client = new WebSocketClient(config.urlHO); |
9 | var pingTimeout; | 9 | var pingTimeout; |
10 | var tiempoVivo = new Date(); | 10 | var tiempoVivo = new Date(); |
11 | 11 | ||
12 | client.on('open', function open() { | 12 | client.on('open', function open() { |
13 | console.log('conection to socket ho is open'); | 13 | console.log('conection to socket ho is open'); |
14 | tiempoVivo = new Date(); | 14 | tiempoVivo = new Date(); |
15 | 15 | ||
16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); | 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); |
17 | 17 | ||
18 | client.on('message', function incoming(message) { | 18 | client.on('message', function incoming(message) { |
19 | 19 | ||
20 | message = JSON.parse(message.toString('utf8')); | 20 | message = JSON.parse(message.toString('utf8')); |
21 | 21 | ||
22 | switch (message.action) { | 22 | switch (message.action) { |
23 | case 'getEntity': | 23 | case 'getEntity': |
24 | 24 | ||
25 | data.getEntity(message).then(function(message) { | 25 | data.getEntity(message).then(function(message) { |
26 | 26 | ||
27 | client.send(JSON.stringify(message)); | 27 | client.send(JSON.stringify(message)); |
28 | }); | 28 | }); |
29 | break; | 29 | break; |
30 | 30 | ||
31 | case 'comprobante': | 31 | case 'comprobante': |
32 | console.log('recibiendo comprobante') | 32 | console.log('recibiendo comprobante') |
33 | data.guardarComprobante(message.req).then(() => { | 33 | data.guardarComprobante(message.req).then(() => { |
34 | console.log('se guardó'); | 34 | console.log('se guardó'); |
35 | message.ok = 1; | 35 | message.ok = 1; |
36 | client.send(JSON.stringify(message)); | 36 | client.send(JSON.stringify(message)); |
37 | }).catch((ee) => { | 37 | }).catch((ee) => { |
38 | console.log('no se guardó' + ee); | 38 | console.log('no se guardó' + ee); |
39 | message.code = ee; | ||
39 | message.ok = 0; | 40 | message.ok = 0; |
40 | client.send(JSON.stringify(message)); | 41 | client.send(JSON.stringify(message)); |
41 | }); | 42 | }); |
42 | 43 | ||
43 | break; | 44 | break; |
44 | 45 | ||
45 | default: | 46 | default: |
46 | break; | 47 | break; |
47 | } | 48 | } |
48 | }); | 49 | }); |
49 | 50 | ||
50 | heartbeat(); | 51 | heartbeat(); |
51 | }); | 52 | }); |
52 | 53 | ||
53 | client.on('error', function(e) { | 54 | client.on('error', function(e) { |
54 | console.log(e); | 55 | console.log(e); |
55 | }); | 56 | }); |
56 | 57 | ||
57 | client.on('ping', heartbeat); | 58 | client.on('ping', heartbeat); |
58 | 59 | ||
59 | client.on('close', function clear() { | 60 | client.on('close', function clear() { |
60 | clearTimeout(pingTimeout); | 61 | clearTimeout(pingTimeout); |
61 | }); | 62 | }); |
62 | 63 | ||
63 | function heartbeat() { | 64 | function heartbeat() { |
64 | console.log('recibiendo ping'); | 65 | console.log('recibiendo ping'); |
65 | console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`); | 66 | console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`); |
66 | 67 | ||
67 | clearTimeout(pingTimeout); | 68 | clearTimeout(pingTimeout); |
68 | 69 | ||
69 | // Use `WebSocket#terminate()`, which immediately destroys the connection, | 70 | // Use `WebSocket#terminate()`, which immediately destroys the connection, |
70 | // instead of `WebSocket#close()`, which waits for the close timer. | 71 | // instead of `WebSocket#close()`, which waits for the close timer. |
71 | // Delay should be equal to the interval at which your server | 72 | // Delay should be equal to the interval at which your server |
72 | // sends out pings plus a conservative assumption of the latency. | 73 | // sends out pings plus a conservative assumption of the latency. |
73 | pingTimeout = setTimeout(() => { | 74 | pingTimeout = setTimeout(() => { |
74 | client.terminate(); | 75 | client.terminate(); |
75 | process.exit(1); | 76 | process.exit(1); |
76 | }, 10000 + 1000); | 77 | }, 10000 + 1000); |
77 | } | 78 | } |
78 | console.log('Sevice connected to wsServer: ' + config.urlHO); | 79 | console.log('Sevice connected to wsServer: ' + config.urlHO); |
79 | 80 |