From 04ce1800ada4f966beba59c389ab12e82061d023 Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Sun, 25 Dec 2016 21:27:27 +0800
Subject: [PATCH] 添加监听代码以便实时修改通讯配置

---
 queue.js  |   17 ++++++++
 method.js |   65 +++++++++++++++++++++++++-------
 server.js |   10 ++++
 3 files changed, 77 insertions(+), 15 deletions(-)

diff --git a/method.js b/method.js
index 0148b5d..7ba59d9 100644
--- a/method.js
+++ b/method.js
@@ -167,14 +167,27 @@
     var ddv = this.toDec(fields[62]) * 256 + this.toDec(fields[63]);
     var mcu = this.toDec(fields[64]) + this.toDec(fields[65]) / 100;
 
+    var _config = global.configs[mac];
+    if(_config == null) {
+        _config = {
+            address: "���������������������������������",
+            location: {���
+                lat:31.430616,
+                lng:120.988327���
+            },
+            notice: {
+                tel: "15950198162",���
+                email: "it01@moral.org.cn",���
+                open_id: "o-RTuwvMHWotyirPHLmdSB_dKoQU"
+            }
+        }
+    }
+
     var current = moment();
 
     queue.pushToMQ('ex_data_screen', {
         mac: mac,
-        location: {
-            lat: 31.430616,
-            lng: 120.988327
-        },
+        location: _config.location,
         data: {
             x0: ferval,
             x1: x1,  //PM2.5 - (������:ppm)
@@ -196,11 +209,8 @@
         }
         queue.pushToMQ('ex_data_alarm', {
             mac: mac,
-            address: "���������������������������������",
-            location: {
-                lat:31.430616,
-                lng:120.988327
-            },
+            address: _config.address,
+            location: _config.location,
             data:{
                 x0: ferval,
                 x1: x1,  //PM2.5
@@ -211,11 +221,7 @@
                 x6: x14  //������������
             },
             level: level,
-            notice: {
-                tel: "15950198162",
-                email: "it01@moral.org.cn",
-                open_id: "o-RTuwvMHWotyirPHLmdSB_dKoQU"
-            },
+            notice: _config.notice,
             time: moment.valueOf()
         });
     }
@@ -343,6 +349,37 @@
     }
 };
 
+module.exports.initConfigs = function(db) {
+    db.collection("devices").find().toArray(function(err, docs) {
+        docs.forEach(function(doc) {
+            var mac = doc.mac;
+            var address = doc.address;
+            var location = doc.location;
+            var notice = doc.notice;
+            if(notice == null) {
+                var userID = doc.userID;
+                db.collection("users").find({"_id": userID}).limit(1).next(function(err, user){
+                    global.configs[mac] = {
+                        address: address,
+                        location: location,
+                        notice: {
+                            tel: user.username,
+                            email: user.email,
+                            open_id: user.open_id
+                        }
+                    }
+                });
+            } else {
+                global.configs[mac] = {
+                    address: address,
+                    location: location,
+                    notice: notice
+                }
+            }
+        });
+    });
+};
+
 /////////////////////////////////////////////////
 module.exports.insertDocument2 = function(db, data, rank, callback) {
     var fields = data.match(/.{2}/g);
diff --git a/queue.js b/queue.js
index 841d19c..7d84d0c 100644
--- a/queue.js
+++ b/queue.js
@@ -24,4 +24,21 @@
             conn.close();
         }, 500);
     });
+};
+
+module.exports.listenToMQ = function(ex) {
+    amqp.connect(uri, function(err, conn) {
+        conn.createChannel(function(err, ch) {
+            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);
+                    var mac = message.mac;
+                    var data = message.data;
+                    global.configs[mac] = data;
+                }, { noAck: true });
+            });
+        });
+    });
 };
\ No newline at end of file
diff --git a/server.js b/server.js
index ae5b574..921b518 100644
--- a/server.js
+++ b/server.js
@@ -19,6 +19,8 @@
     "5a000001000c": 20   //������������
 };
 
+global.configs = {};
+
 function handleData(db, socket, value) {
 
     //1.���������������
@@ -119,6 +121,12 @@
     }
     console.log('Connecting to Mongo DB at ' + config.URL);
 
+    //���������������������������������
+    method.initConfigs(db);
+
+    //���������������������������������������
+    queue.listenToMQ("ex_data_config");
+
     net.createServer().on('connection', function(socket){
         console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
 
@@ -158,4 +166,4 @@
     }).listen(config.PORT, config.HOST);
     
     console.log('TCP Server listening on ' + config.HOST + ':' + config.PORT);
-});
+});
\ No newline at end of file

--
Gitblit v1.8.0