-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathifc.js
232 lines (183 loc) · 6.32 KB
/
ifc.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var dgram = require('dgram');
var net = require('net');
var IFC = {
host: null,
port: null,
enableLog: false,
name: "IF Connect",
isConnected: false,
foreFlight: {
socket: false,
broadcastPort: 49002,
dataModels: {
// GPS
"XGPSInfinite Flight": {
"name": "GPS",
"fields": ["lat", "lng", "alt", "hdg", "gs"]
},
// Attitude
"XATTInfinite Flight": {
"name": "attitude",
"fields": ["hdg", "pitch", "roll"]
},
// Traffic
"XTRAFFICInfinite Flight": {
"name": "traffic",
"fields": ["icao", "lat", "lng", "alt", "vs", "gnd", "hdg", "spd", "callsign"]
}
}
},
infiniteFlight: {
broadcastPort: 15000,
serverPort: 0,
discoverSocket: false,
clientSocket: false
},
initSocketOnHostDiscovered: true,
closeUDPSocketOnHostDiscovered: true,
log: function(msg) { if (IFC.enableLog) console.log(IFC.name, msg); },
beforeInitSocket: function() { IFC.log("Connecting..."); },
onHostUndefined: function() { IFC.log("Host Undefined"); },
onHostSearchStarted: function() { IFC.log("Searching for host"); },
onSocketConnected: function() { IFC.log("Connected"); },
onSocketConnectionError: function() { IFC.log("Connection error"); },
onHostDiscovered: function(host, port, callback) { IFC.log("Host Discovered"); },
onDataReceived: function(data) {},
onHostSearchFailed: function() {},
// SHORTCUTS FUNCTIONS //
init: function(successCallback, errorCallback) {
if (successCallback) IFC.onSocketConnected = successCallback;
if (errorCallback) IFC.onSocketConnectionError = errorCallback;
IFC.searchHost(successCallback, errorCallback);
},
initForeFlight: function(onForeFlightDataReceived) {
IFC.initForeFlightReceiver(onForeFlightDataReceived);
},
// FORE FLIGHT //
onForeFlightDataReceived: function(data) { IFC.log(data); },
initForeFlightReceiver: function(onForeFlightDataReceived) {
if (onForeFlightDataReceived) IFC.onForeFlightDataReceived = onForeFlightDataReceived;
if (IFC.foreFlight.socket) IFC.foreFlight.socket.close(function() {
IFC.foreFlight.socket = false;
});
IFC.foreFlight.socket = dgram.createSocket('udp4');
IFC.foreFlight.socket.on('message', function (msg, info){
msg = IFC._ab2str(msg);
var data = {};
var dataParts = msg.split(",");
var dataType = dataParts.shift();
var dataModel = IFC.foreFlight.dataModels[dataType];
if (!dataModel) return IFC.log("No format found for ", dataType);
var name = dataModel.name;
var fields = dataModel.fields;
var log = [name];
data._name = name;
for (var i = 0; i < fields.length; i++) {
log.push(fields[i] + ' : ' + dataParts[i]);
data[fields[i]] = dataParts[i];
IFC.onForeFlightDataReceived(data);
}
//IFC.log(log.join(' '));
});
IFC.foreFlight.socket.on('listening', function() {
var address = IFC.foreFlight.socket.address();
IFC.log("listening on :", address.address, ":" , address.port);
});
IFC.foreFlight.socket.bind(IFC.foreFlight.broadcastPort);
},
searchHost: function(successCallback, errorCallback) {
if (IFC.infiniteFlight.discoverSocket) return;
IFC.infiniteFlight.discoverSocket = dgram.createSocket('udp4');
IFC.infiniteFlight.discoverSocket.on('message', function (info){
IFC.log("Discover socket : data received");
var dataStr = info;
IFC.log(dataStr);
var data = {};
try {
data = JSON.parse(IFC._ab2str(dataStr));
IFC.log()
IFC.log(data);
} catch(e) {
IFC.log("Discover socket : parsing error");
}
if (data.Address && data.Port) {
IFC.log("Host Discovered");
IFC.isConnected = true;
IFC.infiniteFlight.serverAddress = data.Address;
IFC.infiniteFlight.serverPort = data.Port;
IFC.initIFClient(data.Address, data.Port);
IFC.infiniteFlight.discoverSocket.close(function() {
IFC.infiniteFlight.discoverSocket = false;
});
} else {
IFC.onDataReceived(data);
}
});
IFC.infiniteFlight.discoverSocket.on('listening', function(){
var address = IFC.infiniteFlight.discoverSocket.address();
IFC.log("IF discoverer listening on :" + address.address + ":" + address.port);
});
IFC.infiniteFlight.discoverSocket.bind(IFC.infiniteFlight.broadcastPort);
},
initIFClient: function(host, port) {
IFC.log('initializing socket');
if (IFC.infiniteFlight.clientSocket) IFC.infiniteFlight.clientSocket.close();
IFC.beforeInitSocket();
if (!host) { return IFC.onHostUndefined(); }
IFC.infiniteFlight.clientSocket = new net.Socket();
IFC.infiniteFlight.clientSocket.connect(port, host, function() {
IFC.log('Connected to IF server');
IFC.onSocketConnected();
});
IFC.infiniteFlight.clientSocket.on('data', function(data) {
IFC.log('Received: ' + data);
try {
IFC.onDataReceived(JSON.parse(IFC._ab2str(data)));
} catch(e) {
IFC.log(e);
}
});
IFC.infiniteFlight.clientSocket.on('close', function() {
IFC.infiniteFlight.clientSocket = false;
});
},
cmd: function(cmd) {
IFC.sendCommand({
"Command": "Commands." + cmd,
"Parameters": []
})
},
getAirplaneState: function(onDataReceived) {
if (onDataReceived) IFC.onDataReceived = onDataReceived;
IFC.sendCommand({ "Command": "Airplane.GetState", "Parameters": []});
},
sendCommand: function(cmd) {
try {
var jsonStr = JSON.stringify(cmd);
var data = new Uint8Array(jsonStr.length + 4);
data[0] = jsonStr.length;
for (var i = 0; i < jsonStr.length; i++) {
data[i+4] = jsonStr.charCodeAt(i);
}
var buffer = new Buffer(data);
IFC.infiniteFlight.clientSocket.write(buffer);
} catch(e) {
IFC.log(e);
}
},
// Converters from https://developer.chrome.com/trunk/apps/app_hardware.html
// String to ArrayBuffer
_str2ab: function(str) {
var buf=new ArrayBuffer(str.length);
var bufView=new Uint8Array(buf);
for (var i=0; i<str.length; i++) {
bufView[i]=str.charCodeAt(i);
}
return buf;
},
// ArrayBuffer to String
_ab2str: function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
},
};
module.exports = IFC;