| | |
| | | /** |
| | | * Created by bin.shen on 03/12/2016. |
| | | */ |
| | | |
| | | var amqp = require('amqplib/callback_api'); |
| | | var moment = require('moment'); |
| | | |
| | | var uri = 'amqp://guest:guest@121.40.92.176'; |
| | | var ex = 'ex_data_dev1'; |
| | | |
| | | module.exports.publishMessage = function(data) { |
| | | this.pushToMQ('ex_data_dev1', { |
| | | data: data, |
| | | time: moment().format('YYYY-MM-DD HH:mm:ss') |
| | | }) |
| | | }; |
| | | |
| | | module.exports.pushToMQ = function(ex, data) { |
| | | amqp.connect(uri, function(err, conn) { |
| | | conn.createChannel(function(err, ch) { |
| | | ch.assertExchange(ex, 'fanout', { durable: false }); |
| | | var message = '{"data": "' + data + '", "time":"' + moment().format('YYYY-MM-DD HH:mm:ss') + '"}'; |
| | | ch.publish(ex, '', new Buffer(message)); |
| | | ch.publish(ex, '', new Buffer(JSON.stringify(data))); |
| | | }); |
| | | setTimeout(function() { |
| | | conn.close(); |
| | | //process.exit(0) |
| | | }, 500); |
| | | }); |
| | | }; |
| | | |
| | | module.exports.pushToScreen = function(data) { |
| | | module.exports.listenToMQ = function(ex) { |
| | | amqp.connect(uri, function(err, conn) { |
| | | conn.createChannel(function(err, ch) { |
| | | ch.assertExchange('ex_data_screen', 'fanout', { durable: false }); |
| | | var message = { |
| | | mac: data.mac, |
| | | address: "江苏省昆山市摩瑞尔电器", |
| | | location: { |
| | | lat:31.430616, |
| | | lng:120.988327 |
| | | }, |
| | | data: { |
| | | x1: data.x1, |
| | | x2: data.x2, |
| | | x3: data.x3, |
| | | x4: data.x4, |
| | | x5: data.x5, |
| | | x6: data.x6 |
| | | } |
| | | }; |
| | | ch.publish('ex_data_screen', '', new Buffer(JSON.stringify(message))); |
| | | ch.assertExchange(ex, 'fanout', { durable: false }); |
| | | ch.assertQueue('', {exclusive: true}, function(err, q) { |
| | | ch.bindQueue(q.queue, ex, ''); |
| | | ch.consume(q.queue, function(msg) { |
| | | var message = JSON.parse(msg.content); |
| | | global.configs[message.mac] = message; |
| | | }, { noAck: true }); |
| | | }); |
| | | }); |
| | | setTimeout(function() { |
| | | conn.close(); |
| | | }, 500); |
| | | }); |
| | | }; |