From ee567fd428466945ec8bb290308f3e26287dda63 Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Fri, 23 Dec 2016 22:36:48 +0800
Subject: [PATCH] 代码优化,重构消息推送函数
---
queue.js | 20 +++-----------------
method.js | 16 ++++++++++++----
2 files changed, 15 insertions(+), 21 deletions(-)
diff --git a/method.js b/method.js
index 6590e33..f888085 100644
--- a/method.js
+++ b/method.js
@@ -169,13 +169,14 @@
var current = moment();
- queue.pushToScreen({
+ queue.pushToMQ('ex_data_screen', {
mac: mac,
location: {
lat: 31.430616,
lng: 120.988327
},
data: {
+ x0: ferval,
x1: x1, //PM2.5 - (������:ppm)
x2: x3, //PM2.5 - 0.1���0.3um���
x3: x9, //������
@@ -186,8 +187,14 @@
time: current.valueOf()
});
- if(x1 >= 500) {
- queue.pushToAlarm({
+ if(x1 > 250) {
+ var level = 1;
+ if(x1 > 500) {
+ level = 2;
+ } else if(x1 > 750) {
+ level = 3;
+ }
+ queue.pushToMQ('ex_data_alarm', {
mac: mac,
address: "���������������������������������",
location: {
@@ -195,6 +202,7 @@
lng:120.988327
},
data:{
+ x0: ferval,
x1: x1, //PM2.5
x2: x3, //PM2.5
x3: x9, //������
@@ -202,7 +210,7 @@
x5: x10, //������
x6: x14 //������������
},
- level: 1,
+ level: level,
notice: {
tel: "15950198162",
email: "it01@moral.org.cn",
diff --git a/queue.js b/queue.js
index b136fbb..c1cb158 100644
--- a/queue.js
+++ b/queue.js
@@ -22,25 +22,11 @@
});
};
-module.exports.pushToScreen = function(data) {
+module.exports.pushToMQ = function(ex, data) {
amqp.connect(uri, function(err, conn) {
conn.createChannel(function(err, ch) {
- var _ex = 'ex_data_screen';
- ch.assertExchange(_ex, 'fanout', { durable: false });
- ch.publish(_ex, '', new Buffer(JSON.stringify(data)));
- });
- setTimeout(function() {
- conn.close();
- }, 500);
- });
-};
-
-module.exports.pushToAlarm = function(data) {
- amqp.connect(uri, function(err, conn) {
- conn.createChannel(function(err, ch) {
- var _ex = 'ex_data_alarm';
- ch.assertExchange(_ex, 'fanout', { durable: false });
- ch.publish(_ex, '', new Buffer(JSON.stringify(data)));
+ ch.assertExchange(ex, 'fanout', { durable: false });
+ ch.publish(ex, '', new Buffer(JSON.stringify(data)));
});
setTimeout(function() {
conn.close();
--
Gitblit v1.8.0