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