From 8fbd26ea16e81c29926188ec5a8e149c032d102f Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Thu, 03 May 2018 14:23:26 +0800
Subject: [PATCH] 代码优化 - 去掉无效的queue

---
 server.js |   71 +++++++++++++++++++++++------------
 1 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/server.js b/server.js
index 1a5d74d..c1375d6 100644
--- a/server.js
+++ b/server.js
@@ -7,18 +7,22 @@
 var mongoClient = require('mongodb').MongoClient;
 var config = require('./config');
 var method = require('./method');
+//var queue = require('./queue');
 
 var map = {
-    "5a0000010001": 32,
-    "5a0000010002": 102,
-    "5a0000010003": 162,
-    "5a0000010004": 32,
-    "5a0000010006": 122,
-    "5a0000010007": 32,
-    "5a000001000c": 20
+    "5a0000010001": 32,  //���������������
+    "5a0000010002": 102, //���������������WIFI������
+    "5a0000010003": 162, //���������������������
+    "5a0000010004": 32,  //������������
+    "5a0000010006": 122, //������������
+    "5a0000010007": 32,  //������������������������������������������
+    "5a000001000c": 20   //������������
 };
 
-function handleData(value) {
+global.configs = {};
+
+function handleData(db, socket, value) {
+
     //1.���������������
     if(value.startsWith('5a0000010001')) {
         var output = [ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x01, 0xA1, 0x1A, 0xC7, 0x6B ];
@@ -37,7 +41,6 @@
     if(value.startsWith('5a0000010003')) {
         var output = [ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6B ];
         method.insertData(db, value, function(data, rank) {
-            method.insertDocument2(db, value, rank, function(data) {});
             var fields = method.padLeft(rank.toString(16), 8).match(/.{2}/g);
             output[6] = method.toDec(fields[0]);
             output[7] = method.toDec(fields[1]);
@@ -95,21 +98,45 @@
     }
 }
 
-function doWork(data) {
+function doWork(db, socket, data) {
     if(data == "") return;
     var length = map[data.slice(0, 12)];
     if(length > 0) {
         var value = data.slice(0, length);
-        console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value);
-        handleData(value);
+        //console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value);
 
-        doWork(data.slice(length));
+        //queue.publishMessage(value);
+
+        handleData(db, socket, value);
+
+        doWork(db, socket, data.slice(length));
     }
 }
 
-mongoClient.connect(config.URL, function(err, db) {
-    if (err) return;
+mongoClient.connect(config.URL, {
+        server: {
+            poolSize: 10,
+            keepAlive: 120,
+            auto_reconnect: true,
+            reconnectTries: 1000,
+            reconnectInterval: 5000,
+            socketOptions: {
+                keepAlive: 300000,
+                connectTimeoutMS: 30000
+            }
+        }
+    }, function(err, db) {
+    if (err) {
+        console.log(err.message);
+        return;
+    }
     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);
@@ -126,13 +153,11 @@
                 return;
             }
 
-            //console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value);
-
-            doWork(value);
+            doWork(db, socket, value);
         });
 
         socket.on('end', function(){
-            console.log(count)
+
         });
 
         socket.on('error', function(error) {
@@ -147,11 +172,7 @@
         socket.on('close', function(data) {
             console.log('Closed socket: ' + socket.remoteAddress +' '+ socket.remotePort);
         });
-
-        socket.on('close', function(data) {
-            console.log('Closed socket: ' + socket.remoteAddress + ' ' + socket.remotePort);
-        });
     }).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