-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.js
105 lines (98 loc) · 2.63 KB
/
client.js
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
var net = require('net');
var decoder = require('./decode.js');
var http = require("http");
var fs = require("fs");
var login = "user bg5zzz pass 24229 "
var postData = '';
var pullOptions = {
host: 'rotate.aprs2.net',
port: 14580,
}
var client = net.connect(pullOptions,function(){
console.log('Client connected.');
client.write(login);
client.write('# filter t/po\r\n');
}).on('error',function(error){
console.log('Error: '+error.message);
})
client.on('data',function(data){
var i=0;
var rec = data.toString();
var j=0;
var buff = '';
var myDate = new Date();
for (i = 0; i < rec.length; ++i) {
buff += rec[i];
if ((rec[i] == '\n') && (i > 0) && (rec[i - 1] == '\r')) {
filter(buff, myDate);
buff = '';
}
}
})
function file_write(content, file, myDate) {
fs.open(file,'a',function open(err,fd){
if(err){throw err;}
var writeBuffer=new Buffer('['+myDate.toUTCString()+']'+content);
var bufferPosition=0;
var bufferLength=writeBuffer.length;
var fileposition=null;
fs.write(fd,
writeBuffer,
bufferPosition,
bufferLength,
fileposition,
function wrote(err,written){
if(err) {throw err;}
console.log('wrote'+written+'byte');
fs.closeSync(fd);
});
});
}
function filter(d_msg, myDate){
file_write(d_msg, './all_data.log', myDate);
var i=0;
while(d_msg[i]!=':' && i<d_msg.length)
++i;
if((d_msg[i+1]=='`' || d_msg[i+1]=="'") && (d_msg.search(">") >= 0) /*&& (send == 1)*/) {
file_write(d_msg, './filter_data.log', myDate);
console.log(d_msg);
var haha = decoder.decode(d_msg, myDate);
postData = JSON.stringify(haha);
if (postData != undefined) {
var postOptions = {
hostname : 'localhost',
port : 2333,
path : '/moving_object',
method : 'POST',
headers : {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
}
var req = http.request(postOptions, function(res) {
console.log("yes, iykon is handsome.")
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(postData);
req.end();
}
else {
file_write(d_msg, './delete_data.log', myDate);
}
}
else{
file_write(d_msg, './delete_data.log', myDate);
}
}
client.on('end',function(){
console.log('Client unconnected.');
client = net.connect(pullOptions,function(){
console.log('Client connected.');
client.write(login);
client.write('# filter t/po\r\n');
}).on('error',function(error){
console.log('Error: '+error.message);
})
})