From 06a6dc6aba7a5ebb55ef50473b7fa165594eb004 Mon Sep 17 00:00:00 2001
From: 沈斌 <23420800@qq.com>
Date: Tue, 31 Jan 2017 15:45:28 +0800
Subject: [PATCH] 修复json config中notice问题
---
server.js | 213 +++++++++++++++++++++++++++++++++--------------------
1 files changed, 133 insertions(+), 80 deletions(-)
diff --git a/server.js b/server.js
index 97f5967..511a5ad 100644
--- a/server.js
+++ b/server.js
@@ -7,12 +7,126 @@
var mongoClient = require('mongodb').MongoClient;
var config = require('./config');
var method = require('./method');
+var queue = require('./queue');
+
+var map = {
+ "5a0000010001": 32, //���������������
+ "5a0000010002": 102, //���������������WIFI������
+ "5a0000010003": 162, //���������������������
+ "5a0000010004": 32, //������������
+ "5a0000010006": 122, //������������
+ "5a0000010007": 32, //������������������������������������������
+ "5a000001000c": 20 //������������
+};
+
+global.configs = {};
+
+function handleData(db, socket, value) {
+
+ //1.���������������
+ if(value.startsWith('5a0000010001')) {
+ var output = [ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x01, 0xA1, 0x1A, 0xC7, 0x6B ];
+ method.getAppStatus(db, value, function(doc) {
+ 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) {
+ output[7] = 0x1B;
+ }
+ socket.write(new Buffer(output));
+ });
+ return;
+ }
+
+ //3.���������������������
+ 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) {
+ var fields = method.padLeft(rank.toString(16), 8).match(/.{2}/g);
+ output[6] = method.toDec(fields[0]);
+ output[7] = method.toDec(fields[1]);
+ output[8] = method.toDec(fields[2]);
+ output[9] = method.toDec(fields[3]);
+ socket.write(new Buffer(output));
+ });
+ return;
+ }
+
+ //7.������������������������������������������
+ if(value.startsWith('5a0000010007')) {
+ method.updateDeviceSleep(db, value, function(data) {});
+ return;
+ }
+
+ //4.������������
+ if(value.startsWith('5a0000010004')) {
+ method.updateDeviceWakeup(db, value, function(data) {});
+ var current_time = moment();
+ var output = [
+ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x04, //������(0-5)
+ parseInt(current_time.format('s')), //���
+ parseInt(current_time.format('m')), //���
+ parseInt(current_time.format('H')), //������
+ parseInt(current_time.format('D')), //���
+ parseInt(current_time.format('M')), //���
+ parseInt(current_time.format('E')), //������
+ parseInt(current_time.format('YY')), //���
+ 0x36, //���������
+ 0x6B //������
+ ];
+ socket.write(new Buffer(output));
+ return;
+ }
+
+ //2.���������������WIFI������
+ if(value.startsWith('5a0000010002')) {
+ method.insertDocument(db, value, function(data) {});
+ socket.write(new Buffer(config.OUTPUT_2));
+ return;
+ }
+
+ //6.������������
+ if(value.startsWith('5a0000010006')) {
+ method.registerDevice(db, value, function(data) {});
+ socket.write(new Buffer(config.OUTPUT_6));
+ return;
+ }
+
+ //12.������������
+ if(value.startsWith('5a000001000c')) {
+ socket.write(new Buffer(config.OUTPUT_6));
+ return;
+ }
+}
+
+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);
+
+ queue.publishMessage(value);
+
+ handleData(db, socket, value);
+
+ doWork(db, socket, data.slice(length));
+ }
+}
mongoClient.connect(config.URL, function(err, db) {
- if (err) return;
+ if (err) {
+ console.log(err.message);
+ return;
+ }
console.log('Connecting to Mongo DB at ' + config.URL);
- net.createServer(function(socket) {
+ //���������������������������������
+ method.initConfigs(db);
+
+ //���������������������������������������
+ queue.listenToMQ("ex_data_config");
+
+ net.createServer().on('connection', function(socket){
console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
socket.on('data', function(data) {
@@ -27,87 +141,26 @@
return;
}
- console.log("=> " + value);
-
- //1.���������������
- if(value.startsWith('5a0000010001')) {
- var output = [ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x01, 0xA1, 0x1A, 0xC7, 0x6B ];
- method.getAppStatus(db, value, function(doc) {
- 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) {
- output[7] = 0x1B;
- }
- socket.write(new Buffer(output));
- });
- return;
- }
-
- //3.���������������������
- 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]);
- output[8] = method.toDec(fields[2]);
- output[9] = method.toDec(fields[3]);
- socket.write(new Buffer(output));
- });
- return;
- }
-
- //7.������������������������������������������
- if(value.startsWith('5a0000010007')) {
- method.updateDeviceSleep(db, value, function(data) {});
- return;
- }
-
- //4.������������
- if(value.startsWith('5a0000010004')) {
- method.updateDeviceWakeup(db, value, function(data) {});
- var current_time = moment();
- var output = [
- 0x6A, 0x00, 0x00, 0x01, 0x00, 0x04, //������(0-5)
- parseInt(current_time.format('s')), //���
- parseInt(current_time.format('m')), //���
- parseInt(current_time.format('H')), //������
- parseInt(current_time.format('D')), //���
- parseInt(current_time.format('M')), //���
- parseInt(current_time.format('E')), //������
- parseInt(current_time.format('YY')), //���
- 0x36, //���������
- 0x6B //������
- ];
- socket.write(new Buffer(output));
- return;
- }
-
- //2.���������������WIFI������
- if(value.startsWith('5a0000010002')) {
- method.insertDocument(db, value, function(data) {});
- socket.write(new Buffer(config.OUTPUT_2));
- return;
- }
-
- //6.������������
- if(value.startsWith('5a0000010006')) {
- method.registerDevice(db, value, function(data) {});
- socket.write(new Buffer(config.OUTPUT_6));
- return;
- }
-
- //12.������������
- if(value.startsWith('5a000001000c')) {
- socket.write(new Buffer(config.OUTPUT_6));
- return;
- }
+ doWork(db, socket, value);
});
+
+ socket.on('end', function(){
+
+ });
+
+ socket.on('error', function(error) {
+ console.log(error);
+ socket.end();
+ });
+
+ socket.on('timeout',function(){
+ socket.end();
+ });
+
socket.on('close', function(data) {
- console.log('Closed socket: ' + socket.remoteAddress + ' ' + socket.remotePort);
+ 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