-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
95 lines (85 loc) · 2.39 KB
/
index.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
var net = require('net');
var Packet = require('./packet');
var APRS = function() {
this.state = 'disconnected';
this.username = null;
this.passcode = null;
this.serverName = null;
this.serverSoftwareName = null;
this.serverSoftwareVersion = null;
this.socket = null;
};
APRS.prototype.connect = function(user, pass) {
this.username = user;
this.passcode = pass || null;
var _this = this;
var socket = net.connect({
host: 'noam.aprs2.net',
port: 14580
});
socket.on('connect', function() {
_this.socket = this;
_this.changeState('connected');
});
socket.on('end', function() {
_this.socket = null;
_this.serverSoftwareName = null;
_this.serverSoftwareVersion = null;
_this.changeState('disconnected');
});
socket.on('data', _this.parse.bind(_this));
};
APRS.prototype.disconnect = function() {
this.socket.end();
};
APRS.prototype.changeState = function(state) {
console.log('State changed:', state);
this.state = state;
};
APRS.prototype.parse = function(data) {
data = data.toString().slice(0, -2);
if(data.charAt(0) === '#') {
var tokens = data.split(' ');
if(tokens[1] === 'logresp') {
this.serverName = tokens[5];
this.changeState(tokens[3]);
} else if(tokens.length === 3) {
this.serverSoftwareName = tokens[1];
this.serverSoftwareVersion = tokens[2];
this.login(this.username, this.passcode);
}
} else {
var packet = new Packet(data);
console.log('From:', packet.sourceAddress);
console.log('Dest:', packet.destinationAddress);
console.log('Data:', packet.payload);
if(packet.time) {
console.log('Time:', packet.time);
} else {
console.log('Time: NONE');
}
if(packet.latitude) {
console.log(' Pos:', packet.latitude, packet.longitude);
} else {
console.log(' Pos: NONE');
}
}
};
APRS.prototype.filter = function(filter) {
var filterstring;
if(Array.isArray(filter)) {
filterstring = filter.join(' ');
} else {
filterstring = filter;
}
var packet = '# filter ' + filterstring;
this.socket.write(packet + '\r\n');
};
APRS.prototype.login = function(user, pass, name, version) {
var pass = pass || '-1';
var name = name || 'node-aprs-is';
var version = version || '0.0.1';
var packet = 'user ' + user + ' pass ' + pass + ' vers ' + name + ' ' + version;
this.socket.write(packet + '\r\n');
};
module.exports = APRS;