Commit e36a85219133cc0417bc13e11d8aad9382e53816
Exists in
master
Merge branch 'master' into 'master'
Master(efernandez) See merge request !3
Showing
2 changed files
 
Show diff stats
data/index.js
| 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 | 
index.js
| 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: 'gnl'})); | 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 = message.data.toString('utf8'); | 20 | message = JSON.parse(message.toString('utf8')); | 
| 21 | 21 | ||
| 22 | switch (message) { | 22 | switch (message.action) { | 
| 23 | case message.action == 'getEntity': | 23 | case 'getEntity': | 
| 24 | data.getEntity(message.tableName, message.where || {}).then(function(data) { | 24 | |
| 25 | client.send(data); | 25 | data.getEntity(message).then(function(message) { | 
| 26 | |||
| 27 | client.send(JSON.stringify(message)); | ||
| 26 | }); | 28 | }); | 
| 27 | break; | 29 | break; | 
| 28 | default: | 30 | default: | 
| 29 | break; | 31 | break; | 
| 30 | } | 32 | } | 
| 31 | }); | 33 | }); | 
| 32 | }); | 34 | }); | 
| 33 | 35 | ||
| 34 | console.log('Sevice open port ' + config.port); | 36 | console.log('Sevice open port ' + config.port); | 
| 35 | 37 |