Merge Request #4
← To merge requests
efernandez/websocketbo:
master
→
master
Commits (2)
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 (message) { | 6 | getEntity: function (message) { |
| 7 | 7 | ||
| 8 | return new Promise(function(resolve, reject) { | 8 | return new Promise(function(resolve, reject) { |
| 9 | 9 | ||
| 10 | var promise; | 10 | var promise; |
| 11 | |||
| 12 | if (message.queryString) { | ||
| 13 | |||
| 14 | promise = knex.schema.raw(message.queryString); | ||
| 15 | } else { | ||
| 16 | |||
| 17 | promise = knex(message.tableName).where(message.where).select('*'); | ||
| 18 | } | ||
| 19 | |||
| 20 | promise.then(function (data) { | ||
| 11 | 21 | ||
| 12 | if (message.queryString) { | 22 | message.data = data[0]; |
| 13 | 23 | resolve(message); | |
| 14 | promise = knex.schema.raw(message.queryString); | 24 | }); |
| 15 | } else { | 25 | }); |
| 16 | 26 | } | |
| 17 | promise = knex(message.tableName).where(message.where).select('*'); | 27 | } |
| 18 | } | 28 | } |
| 19 | 29 |
index.js
| 1 | const express = require('express'); | ||
| 2 | const app = express(); | ||
| 3 | |||
| 4 | const WebSocketClient = require('ws'); | 1 | const WebSocketClient = require('ws'); |
| 5 | const config = require('./config/config.json'); | 2 | const config = require('./config/config.json'); |
| 6 | const data = require('./data/index')(config.bo); | 3 | const data = require('./data/index')(config.bo); |
| 7 | 4 | ||
| 8 | app.listen(config.port); | ||
| 9 | app.use(express.json({limit: '50mb'})); | ||
| 10 | |||
| 11 | const client = new WebSocketClient(config.urlHO); | 5 | const client = new WebSocketClient(config.urlHO); |
| 12 | 6 | ||
| 13 | client.on('open', function open() { | 7 | client.on('open', function open() { |
| 14 | console.log('conection to socket ho is open'); | 8 | console.log('conection to socket ho is open'); |
| 15 | 9 | ||
| 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); | 10 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); |
| 17 | 11 | ||
| 18 | client.on('message', function incoming(message) { | 12 | client.on('message', function incoming(message) { |
| 19 | 13 | ||
| 20 | message = JSON.parse(message.toString('utf8')); | 14 | message = JSON.parse(message.toString('utf8')); |
| 21 | 15 | ||
| 22 | switch (message.action) { | 16 | switch (message.action) { |
| 23 | case 'getEntity': | 17 | case 'getEntity': |
| 24 | 18 | ||
| 25 | data.getEntity(message).then(function(message) { | 19 | data.getEntity(message).then(function(message) { |
| 26 | 20 | ||
| 27 | client.send(JSON.stringify(message)); | 21 | client.send(JSON.stringify(message)); |
| 28 | }); | 22 | }); |
| 29 | break; | 23 | break; |
| 30 | default: | 24 | default: |
| 31 | break; | 25 | break; |
| 32 | } | 26 | } |
| 33 | }); | 27 | }); |
| 34 | }); | 28 | }); |
| 35 | 29 | ||
| 36 | console.log('Sevice open port ' + config.port); | 30 | console.log('Sevice connected to wsServer: ' + config.urlHO); |
| 37 | 31 |