Commit 096a49ea4b794545bb3f028e122baaaaca5784a7
1 parent
b98e2730ff
Exists in
master
avances
Showing
2 changed files
with
10 additions
and
5 deletions
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: '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 | message.data = data[0]; | ||
28 | 26 | ||
29 | client.send(JSON.stringify(message)); | 27 | client.send(JSON.stringify(message)); |
30 | }); | 28 | }); |
31 | break; | 29 | break; |
32 | default: | 30 | default: |
33 | break; | 31 | break; |
34 | } | 32 | } |
35 | }); | 33 | }); |
36 | }); | 34 | }); |
37 | 35 | ||
38 | console.log('Sevice open port ' + config.port); | 36 | console.log('Sevice open port ' + config.port); |
39 | 37 |