沈斌
2016-12-26 565c4f00094c6894911b47fae031c4777539b616
method.js
@@ -4,6 +4,7 @@
var moment = require('moment');
var config = require('./config');
var queue = require('./queue');
module.exports.toDec = function(hex) {
    if(typeof hex === 'number') {
@@ -166,7 +167,66 @@
    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 = {
            mac: mac,
            address: "江苏省昆山市摩瑞尔电器",
            location: {

                lat:31.430616,
                lng:120.988327

            },
            notice: {
                tel: "15950198162",

                email: "it01@moral.org.cn",

                open_id: "o-RTuwvMHWotyirPHLmdSB_dKoQU"
            }
            configs[mac] = _config;
        }
    }
    var current = moment();
    queue.pushToMQ('ex_data_screen', {
        mac: mac,
        location: _config.location,
        data: {
            x0: ferval,
            x1: x1,  //PM2.5 - (单位:ppm)
            x2: x3,  //PM2.5 - 0.1升0.3um量
            x3: x9,  //甲醛
            x4: x11, //温度
            x5: x10, //湿度
            x6: x14  //光线强度
        },
        time: current.valueOf()
    });
    if(x1 > 250) {
        var level = 1;
        if(x1 > 750) {
            level = 3;
        } else if(x1 > 500) {
            level = 2;
        }
        queue.pushToMQ('ex_data_alarm', {
            mac: mac,
            address: _config.address,
            location: _config.location,
            data:{
                x0: ferval,
                x1: x1,  //PM2.5
                x2: x3,  //PM2.5
                x3: x9,  //甲醛
                x4: x11, //温度
                x5: x10, //湿度
                x6: x14  //光照强度
            },
            level: level,
            notice: _config.notice,
            time: moment.valueOf()
        });
    }
    var rank = 0;
    if(s > 0) {
@@ -291,6 +351,43 @@
    }
};
module.exports.initConfigs = function(db) {
    db.collection("devices").find().toArray(function(err, docs) {
        docs.forEach(function(doc) {
            var mac = doc.mac;
            if(mac != null) {
                var address = doc.address || "";
                var location = doc.location || {};
                var notice = doc.notice;
                if(notice == null || notice == {}) {
                    var userID = doc.userID;
                    db.collection("users").find({"_id": userID}).limit(1).next(function(err, user){
                        if(user) {
                            global.configs[mac] = {
                                mac: mac,
                                address: address,
                                location: location,
                                notice: {
                                    tel: user.username,
                                    email: user.email,
                                    open_id: user.open_id
                                }
                            }
                        }
                    });
                } else {
                    global.configs[mac] = {
                        mac: mac,
                        address: address,
                        location: location,
                        notice: notice
                    }
                }
            }
        });
    });
};
/////////////////////////////////////////////////
module.exports.insertDocument2 = function(db, data, rank, callback) {
    var fields = data.match(/.{2}/g);