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
/**
 * Created by bin.shen on 6/16/16.
 */
 
var net = require('net');
var moment = require('moment');
var mongoClient = require('mongodb').MongoClient;
var config = require('./config2');
var method = require('./method');
 
mongoClient.connect(config.URL, function(err, db) {
    if (err) return;
    console.log('Connecting to Mongo DB at ' + config.URL);
 
    net.createServer(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);
 
            /*
            //3.传感器数据上传
            if(value.startsWith('5a0034010003')) {
                method.insertDocument2(db, value, function(data) {});
                return;
            }
 
            //2.云地址写入WIFI模块
            if(value.startsWith('5a0033010002')) {
                method.insertDocument(db, value, function(data) {});
                socket.write(new Buffer(config.OUTPUT_2));
                return;
            }
 
            //6.配网数据
            if(value.startsWith('5a0033010006')) {
                socket.write(new Buffer(config.OUTPUT_6));
                return;
            }
 
            //12.联云通知
            if(value.startsWith('5a000a01000c')) {
                socket.write(new Buffer(config.OUTPUT_6));
                return;
            }
            */
        });
        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);
});