Merge Request #3

Merged
Created by Eric Fernandez

Master(efernandez)

Assignee: Eric Fernandez
Milestone: None

Merged by Eric Fernandez

Commits (2)
1 participants
1 module.exports = function(config) { 1 module.exports = function(config) {
2 2
3 var knex = require('knex')(config); 3 var knex = require('knex')(config);
4 4
5 return { 5 return {
6 getEntity: function (table, where) { 6 getEntity: function (message) {
7 7
8 return knex(table).where(where).select('*'); 8 return new Promise(function(resolve, reject) {
9
10 knex(message.tableName).where(message.where).select('*').then(function (data) {
11
12 message.data = data[0];
13 resolve(message);
14 });
15 });
9 } 16 }
10 } 17 }
11 } 18 }
12 19
1 const express = require('express'); 1 const express = require('express');
2 const app = express(); 2 const app = express();
3 3
4 const WebSocketClient = require('ws'); 4 const WebSocketClient = require('ws');
5 const config = require('./config/config.json'); 5 const config = require('./config/config.json');
6 const data = require('./data/index')(config.bo); 6 const data = require('./data/index')(config.bo);
7 7
8 app.listen(config.port); 8 app.listen(config.port);
9 app.use(express.json({limit: '50mb'})); 9 app.use(express.json({limit: '50mb'}));
10 10
11 const client = new WebSocketClient(config.urlHO); 11 const client = new WebSocketClient(config.urlHO);
12 12
13 client.on('open', function open() { 13 client.on('open', function open() {
14 console.log('conection to socket ho is open'); 14 console.log('conection to socket ho is open');
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.tableName, message.where || {}).then(function(data) { 25 data.getEntity(message).then(function(message) {
26
27 client.send(JSON.stringify(message));
26 28 });
27 message.data = data[0]; 29 break;
28 30 default:
29 client.send(JSON.stringify(message)); 31 break;
30 }); 32 }
31 break; 33 });
32 default: 34 });
33 break; 35
34 } 36 console.log('Sevice open port ' + config.port);
35 }); 37
36 });
37