From a775c658d4b0577b473daefaf2a8d145b812ba8c Mon Sep 17 00:00:00 2001
From: 沈斌 <23420800@qq.com>
Date: Tue, 31 Jan 2017 15:51:50 +0800
Subject: [PATCH] 解决forEach循环中无法取得全局变量值的问题

---
 method.js |  150 ++++++++++++++++++++++++++++----------------------
 1 files changed, 84 insertions(+), 66 deletions(-)

diff --git a/method.js b/method.js
index 6590e33..88d31a1 100644
--- a/method.js
+++ b/method.js
@@ -131,6 +131,10 @@
     mac = mac.toLowerCase();
     this.updateDeviceLastUpdated(db, mac, function(data) {});
 
+    //������������������������������������������������������
+    //var ver = this.toDec(fields[18]) * 256 + this.toDec(fields[19]); //���������
+
+
     var x1  = this.toDec(fields[20]) * 256 + this.toDec(fields[21]); //PM2.5������
     var x2  = this.toDec(fields[22]) * 256 + this.toDec(fields[23]); //PM10
     var x3  = this.toDec(fields[24]) * 256 + this.toDec(fields[25]); //0.1���0.3um���
@@ -167,15 +171,20 @@
     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 = config.ALARM_DEFAULTS;
+        _config.mac = mac;
+        global.configs[mac] = _config;
+    }
+
     var current = moment();
 
-    queue.pushToScreen({
+    var sensor_data = {
         mac: mac,
-        location: {
-            lat: 31.430616,
-            lng: 120.988327
-        },
+        location: _config.location,
         data: {
+            x0: ferval,
             x1: x1,  //PM2.5 - (������:ppm)
             x2: x3,  //PM2.5 - 0.1���0.3um���
             x3: x9,  //������
@@ -183,34 +192,10 @@
             x5: x10, //������
             x6: x14  //������������
         },
+        options: _config.options,
         time: current.valueOf()
-    });
-
-    if(x1 >= 500) {
-        queue.pushToAlarm({
-            mac: mac,
-            address: "���������������������������������",
-            location: {
-                lat:31.430616,
-                lng:120.988327
-            },
-            data:{
-                x1: x1,  //PM2.5
-                x2: x3,  //PM2.5
-                x3: x9,  //������
-                x4: x11, //������
-                x5: x10, //������
-                x6: x14  //������������
-            },
-            level: 1,
-            notice: {
-                tel: "15950198162",
-                email: "it01@moral.org.cn",
-                open_id: "o-RTuwvMHWotyirPHLmdSB_dKoQU"
-            },
-            time: moment.valueOf()
-        });
-    }
+    };
+    queue.pushToMQ('ex_data_screen', sensor_data);
 
     var rank = 0;
     if(s > 0) {
@@ -230,6 +215,7 @@
         }, function(err, result) { });
     }
 
+    var _this = this;
     db.collection("data").insertOne({
         mac: mac,
         x1: x1,
@@ -259,10 +245,40 @@
         mcu: mcu,
         day: current.format('YYYYMMDD'),
         created: current.valueOf()
-    }, function(err, result) {
+    }, function(err, doc) {
         if (err) return;
-        callback(result, rank);
+
+        var level1 = _this.getLevel(x1,  _config['options'][1]);
+        var level2 = _this.getLevel(x9,  _config['options'][2]);
+        var level3 = _this.getLevel(x11, _config['options'][3]);
+        var level4 = _this.getLevel(x10, _config['options'][4]);
+        var level5 = _this.getLevel(x14, _config['options'][5]);
+        var level = Math.max(level1, level2, level3, level4, level5);
+        if(level > 0) {
+            sensor_data._id = doc.insertedId;
+            sensor_data.level = level;
+            sensor_data.notice = _config.notice;
+            sensor_data.address = _config.address;
+            sensor_data.data.levels = [level1, level2, level3, level4, level5];
+            queue.pushToMQ('ex_data_alarm', sensor_data);
+        }
+
+        callback(doc, rank);
     });
+};
+
+module.exports.getLevel = function(val, option) {
+    var level = 0;
+    if(option == null || option.length < 3) return level;
+    if(val > option[0]) {
+        var level = 1;
+        if(val > option[2]) {
+            level = 3;
+        } else if(val > option[1]) {
+            level = 2;
+        }
+    }
+    return level;
 };
 
 module.exports.updateDeviceSleep = function(db, data, callback) {
@@ -335,37 +351,39 @@
     }
 };
 
-/////////////////////////////////////////////////
-module.exports.insertDocument2 = function(db, data, rank, callback) {
-    var fields = data.match(/.{2}/g);
-    var mac = fields[6] + fields[7] + fields[8] + fields[9] + fields[10] + fields[11]; //Mac
-
-    var x01 = this.toDec(fields[20]) * 256 + this.toDec(fields[21]); //PM2.5������
-    var x02 = this.toDec(fields[24]) * 256 + this.toDec(fields[25]); //PM2.5������
-    var x09 = this.toDec(fields[32]) * 256 + this.toDec(fields[33]); //������
-    var x10 = this.toDec(fields[34]) * 256 + this.toDec(fields[35]); //������
-    var x11 = this.toDec(fields[36]) * 256 + this.toDec(fields[37]); //������
-    var x14 = this.toDec(fields[42]) * 256 + this.toDec(fields[43]); //������������
-
-    var fei = this.toDec(fields[58]);
-    var ferval = this.toDec(fields[59]);
-    var aqi = this.toDec(fields[60]) * 256 + this.toDec(fields[61]);
-    var ddv = this.toDec(fields[62]) * 256 + this.toDec(fields[63]);
-    var mcu = this.toDec(fields[64]) + this.toDec(fields[65]) / 100;
-
-    db.collection("devices").find({ mac: mac }).limit(1).next(function(err, doc){
-        if (err) return;
-
-        var app = 0;
-        var app_status = doc.app_status;
-        var app_last_updated = doc.app_last_updated;
-        if(app_status == 1 && app_last_updated != null && Date.now() - app_last_updated <= 30000) {
-            app = 1;
-        }
-        db.collection(config.COLLECTION).insertOne({ mac: mac.toLowerCase(), data: data + ' - PM2.5:' + x01 + ', ������:' + x09 + ', ������:' + x10 + ', ������:' + x11 + ', ������:' + x02 + ', FEI:' +  ferval + ', ������:' + fei + ', ������:' + x14 + ', AQI:' + aqi + ', ������������:' + ddv + ', MCU������:' + mcu + ', APP:' + app, date: Date.now() }, function(err, doc) {
-            if (err) return;
-            callback(doc);
+module.exports.initConfigs = function(db) {
+    var _address = config.ALARM_DEFAULTS.address;
+    var _location = config.ALARM_DEFAULTS.location;
+    var _options = config.ALARM_DEFAULTS.options;
+    db.collection("devices").find().toArray(function(err, docs) {
+        docs.forEach(function(doc) {
+            var mac = doc.mac;
+            var address = doc.address || _address;
+            var location = doc.location || _location;
+            var options = doc.options || _options;
+            var notice = doc.notice;
+            var config = {
+                mac: mac,
+                address: address,
+                location: location,
+                notice: notice,
+                options: options
+            };
+            if(notice == null || notice == {}) {
+                var userID = doc.userID;
+                db.collection("users").find({"_id": userID}).limit(1).next(function(err, user){
+                    if(user) {
+                        config["notice"] = {
+                            tel: user.username,
+                            email: user.email,
+                            open_id: user.open_id
+                        };
+                        global.configs[mac] = config;
+                    }
+                });
+            } else {
+                global.configs[mac] = config;
+            }
         });
     });
-};
-
+};
\ No newline at end of file

--
Gitblit v1.8.0