bin.shen
2016-12-03 c87f4c4d262200cdc2b1d25faefd403af5d887ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * Created by bin.shen on 5/30/16.
 */
 
var net = require('net');
var moment = require('moment');
var mongoClient = require('mongodb').MongoClient;
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, 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) {
            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, 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(db, socket, value);
 
        doWork(db, socket, data.slice(length));
    }
}
 
mongoClient.connect(config.URL, function(err, db) {
    if (err) return;
    console.log('Connecting to Mongo DB at ' + config.URL);
 
    net.createServer().on('connection', function(socket){
        console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
 
        socket.on('data', function(data) {
            if(data == null) {
                console.log("Error - invalid data - null or undefined");
                return;
            }
 
            var value = data.toString('hex').toLowerCase();
            if(value.length < 12) {
                console.log("Error - invalid data - less than 12 character long");
                return;
            }
 
            //console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value);
 
            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);
        });
 
        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);
});