From 636afcb4d4e130d27af78e117e1c3cd33425ab7a Mon Sep 17 00:00:00 2001
From: bin.shen <bluelazysb@hotmail.com>
Date: Thu, 01 Dec 2016 19:55:51 +0800
Subject: [PATCH] updates
---
test.js | 9 +
server.js | 196 ++++++++++++++++++++++++++++++-------------------
2 files changed, 126 insertions(+), 79 deletions(-)
diff --git a/server.js b/server.js
index 5e97df3..1a5d74d 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(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(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);
+
+ doWork(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(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);
});
diff --git a/test.js b/test.js
index b851f88..0d6cfe7 100644
--- a/test.js
+++ b/test.js
@@ -236,6 +236,9 @@
//console.log(Math.floor(Math.random() * 3 + 1))
-var moment = require('moment');
-var current = moment().format('YYYY-MM-DD HH:mm:ss');
-console.log(current);
\ No newline at end of file
+// var moment = require('moment');
+// var current = moment().format('YYYY-MM-DD HH:mm:ss');
+// console.log(current);
+
+var data = "5a0000010007accf23d44de6a77ace5b";
+console.log(data.length);
\ No newline at end of file
--
Gitblit v1.8.0