-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (85 loc) · 2.7 KB
/
app.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
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
var client = dgram.createSocket("udp4");
var serverIp = process.env["serverIp"] || "localhost";
//var outputWs = require("./server");
var sendRREFMessage = function (dataref) {
// Sim/cockpit/radios/nav1 freq hz
// sim/cockpit/radios/adf1_dme_dist_m
var outgoing = new Buffer(new Array(413));
outgoing.write("RREF\0");
outgoing.writeInt32LE(1, 5);
outgoing.writeInt32LE(97, 9);
outgoing.write(dataref + "\0", 13);
console.log("Sending " + outgoing.length + " bytes");
console.log(outgoing);
client.send(outgoing, 0, outgoing.length, 49000, serverIp, function(err) {
if (err) {
console.error("UDP client error sending to " + serverIp, err);
}
});
};
var sendRPOSMessage = function() {
var outgoing = new Buffer(new Array(9));
outgoing.write("RPOS\0");
outgoing.writeInt32LE(100, 5);
console.log("Sending " + outgoing.length + " bytes");
console.log(outgoing);
client.send(outgoing, 0, outgoing.length, 49000, serverIp, function(err) {
if (err) {
console.error("UDP client error sending to " + serverIp, err);
}
});
};
function sendMessage(message) {
// TODO
}
var dataRef;
var sendDatarefValue = function(dataref, value) {
// sim/cockpit/radios/nav1_freq_hz
// sim/cockpit/radios/adf1_dme_dist_m
var outgoing = new Buffer(new Array(509));
outgoing.write("DREF\0");
outgoing.writeFloatLE(value, 5);
outgoing.write(dataref + "\0", 9);
console.log("Sending " + outgoing.length + " bytes");
console.log(outgoing);
client.send(outgoing, 0, outgoing.length, 49000, serverIp, function(err) {
if (err) {
console.error("UDP client error sending to " + serverIp, err);
}
});
}
server.on("error", function (err) {
console.log("server error:\n" + err.stack);
server.close();
});
client.on("message", function (msg, rinfo) {
console.log("Receiving", msg);
});
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("line", function(cmd) {
var entered = cmd.trim();
if (isNaN(parseFloat(entered))) {
dataRef = entered;
sendRREFMessage(entered);
} else {
sendDatarefValue(dataRef, parseFloat(entered));
}
//sendRPOSMessage();
rl.prompt();
}).on('close', function() {
console.log('Have a great day!');
process.exit(0);
});
rl.setPrompt("Enter dataref > ");
server.on("listening", function () {
var address = server.address();
console.log("Server listening to " + address.address + ":" + address.port);
});
server.bind(12345);
rl.prompt();