Merge Request #14

Merged
Created by Eric Fernandez

Master(efernandez)

Assignee: Eric Fernandez
Milestone: None

Merged by Eric Fernandez

Commits (1)
1 participants
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 8
9 const client = new WebSocketClient(config.urlHO); 9 const client = new WebSocketClient(config.urlHO);
10 10
11 client.on('open', function open() { 11 client.on('open', function open() {
12 console.log('conection to socket ho is open'); 12 console.log('conection to socket ho is open');
13 13
14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
15 15
16 client.on('message', function incoming(message) { 16 client.on('message', function incoming(message) {
17 17
18 message = JSON.parse(message.toString('utf8')); 18 message = JSON.parse(message.toString('utf8'));
19 19
20 switch (message.action) { 20 switch (message.action) {
21 case 'getEntity': 21 case 'getEntity':
22 22
23 data.getEntity(message).then(function(message) { 23 data.getEntity(message).then(function(message) {
24 24
25 client.send(JSON.stringify(message)); 25 client.send(JSON.stringify(message));
26 }); 26 });
27 break; 27 break;
28 28
29 case 'comprobante': 29 case 'comprobante':
30 console.log('recibiendo comprobante') 30 console.log('recibiendo comprobante')
31 data.guardarComprobante(message.req).then(() => { 31 data.guardarComprobante(message.req).then(() => {
32 console.log('se guardó'); 32 console.log('se guardó');
33 client.send(JSON.stringify('ok')); 33 message.ok = true;
34 client.send(JSON.stringify(message));
34 }).catch((ee) => { 35 }).catch((ee) => {
35 console.log('no se guardó' + ee); 36 console.log('no se guardó' + ee);
36 client.send(JSON.stringify('bad')); 37 message.ok = false;
38 client.send(JSON.stringify(message));
37 }); 39 });
38 40
39 break; 41 break;
40 42
41 default: 43 default:
42 break; 44 break;
43 } 45 }
44 }); 46 });
45 }); 47 });
46 48
47 client.on('error', function(e) { 49 client.on('error', function(e) {
48 console.log(e); 50 console.log(e);
49 }) 51 })
50 52
51 console.log('Sevice connected to wsServer: ' + config.urlHO); 53 console.log('Sevice connected to wsServer: ' + config.urlHO);
52 54