From a2e16fe45084564d961e5debd229494e64cdcaa3 Mon Sep 17 00:00:00 2001 From: bin.shen <bluelazysb@hotmail.com> Date: Thu, 01 Dec 2016 20:02:59 +0800 Subject: [PATCH] updates --- server.js | 196 ++++++++++++++++++++++++++++++------------------- 1 files changed, 120 insertions(+), 76 deletions(-) diff --git a/server.js b/server.js index 5e97df3..7288fe8 100644 --- a/server.js +++ b/server.js @@ -8,11 +8,110 @@ var config = require('./config'); var method = require('./method'); +var map = { + "5a0000010001": 32, + "5a0000010002": 102, + "5a0000010003": 162, + "5a0000010004": 32, + "5a0000010006": 122, + "5a0000010007": 32, + "5a000001000c": 20 +}; + +function handleData(db, 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; + } +} + +function doWork(db, 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(db, value); + + doWork(db, data.slice(length)); + } +} + mongoClient.connect(config.URL, function(err, db) { if (err) return; console.log('Connecting to Mongo DB at ' + config.URL); - net.createServer(function(socket) { + net.createServer().on('connection', function(socket){ console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort); socket.on('data', function(data) { @@ -27,83 +126,28 @@ return; } - console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value); + //console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + 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, value); }); + + socket.on('end', function(){ + console.log(count) + }); + + 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); + }); + socket.on('close', function(data) { console.log('Closed socket: ' + socket.remoteAddress + ' ' + socket.remotePort); }); -- Gitblit v1.8.0