forked from EvenAR/node-simconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
134 lines (111 loc) · 4.83 KB
/
example.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
const simConnect = require('../../');
connectToSim();
// Open connection
function connectToSim() {
console.log("Trying to connect...")
var success = simConnect.open("MyAppName", function(name, version) {
console.log("\n-----------------------------------------------\nConnected to: " + name + "\nSimConnect version: " + version + "\n-----------------------------------------------");
doStuffWithSimconnect();
}, () => {
console.log("Quit.... :(");
connectToSim();
}, (exception) => {
console.log("SimConnect exception: " + exception.name + " (" + exception.dwException + ", " + exception.dwSendID + ", " + exception.dwIndex + ", " + exception.cbData + ")");
}, (error) => {
// Happens for example when connection with SimConnect is lost unexpectedly. For crash details: ntstatus.h
console.log("SimConnect error: " + error);
// The connection must be re-opened
connectToSim();
});
if(!success) {
setTimeout(() => {
connectToSim();
}, 5000);
}
}
function doStuffWithSimconnect() {
var vs = 0;
var gnd = 1;
// Pause the sim
simConnect.transmitClientEvent("PAUSE_ON");
// Move aircraft
simConnect.setAircraftInitialPosition({
"latitude": 60.187982,
"longitude": 11.075415,
"altitude": 0,
"pitch": 0,
"bank": 0,
"heading": 15,
"onGround": 1,
"airspeed": 0
});
// Set throttle lever positions
simConnect.setDataOnSimObject("GENERAL ENG THROTTLE LEVER POSITION:1", "Percent", 0);
simConnect.setDataOnSimObject("GENERAL ENG THROTTLE LEVER POSITION:2", "Percent", 0);
// Get the .air file name of the loaded aircraft. Then get the aircraft title.
simConnect.requestSystemState("AircraftLoaded", function(obj) {
simConnect.requestDataOnSimObject([["NUMBER OF ENGINES", "Number"], ["TITLE", null, simConnect.datatype.STRINGV]], function(data) {
console.log("Aircraft Name: " + data["TITLE"]);
console.log("Aircraft Path: " + obj.string);
console.log("Aircraft number of engines: " + data["NUMBER OF ENGINES"]);
}, simConnect.objectId.USER, simConnect.period.ONCE);
});
// Subscribe to paused/unpaused event
simConnect.subscribeToSystemEvent("Pause", (paused) => {
if(paused)
console.log("Sim paused");
else
console.log("Sim un-paused");
});
// Subscribe to aircraft change
simConnect.subscribeToSystemEvent("AircraftLoaded", (filePath) => {
console.log("New aircraft loaded: " + filePath);
});
// Request pushback state and get updates whenever it changes
simConnect.requestDataOnSimObject([
["LIGHT CABIN","Bool"],
["PUSHBACK STATE","Enum"]
], function(data) {
console.log(data);
}, simConnect.objectId.USER, simConnect.period.SIM_FRAME, simConnect.dataRequestFlag.CHANGED);
// Create data defintion that can be re-used for multiple requests
var navInfoDefId = simConnect.createDataDefinition([
["NAV DME:1", "Nautical miles"],
["NAV GLIDE SLOPE ERROR:1", "Degrees"],
["NAV RADIAL ERROR:1", "Degrees"],
]);
setInterval(() => {
simConnect.requestDataOnSimObjectType(navInfoDefId, function(data) {
console.log(data)
}, 0, simConnect.simobjectType.USER)
},10000)
// Get positions of all aircraft within a 10 km radius
simConnect.requestDataOnSimObjectType([
["ATC MODEL",null,simConnect.datatype.STRINGV],
["Plane Latitude", "degrees"],
["Plane Longitude", "degrees"]
], function(data) {
console.log(data);
}, 10000, simConnect.simobjectType.AIRCRAFT);
// Get updates when the combustion (running or not) state of each engine changes
simConnect.requestDataOnSimObject([
["ENG COMBUSTION:1","bool"],
["ENG COMBUSTION:2","bool"],
["ENG COMBUSTION:3","bool"],
["ENG COMBUSTION:4","bool"]
], function(data) {
console.log("Engine state: " + data["ENG COMBUSTION:1"] + "," + data["ENG COMBUSTION:2"] + "," + data["ENG COMBUSTION:3"] + "," + data["ENG COMBUSTION:4"]);
}, simConnect.objectId.USER, simConnect.period.SIM_FRAME, simConnect.dataRequestFlag.CHANGED);
// Check if sim is on ground. When aircraft hits the ground, print the vertical speed.
simConnect.requestDataOnSimObject([
["SIM ON GROUND","Bool"],
["VERTICAL SPEED","feet per minute"]
], function(data) {
let onGndNow = data["SIM ON GROUND"] == 1;
if(!onGndNow)
vs = data["VERTICAL SPEED"];
else if(gnd != onGndNow)
console.log("Landed with " + vs + " feet/min");
gnd = onGndNow;
}, simConnect.objectId.USER, simConnect.period.SIM_FRAME, simConnect.dataRequestFlag.CHANGED);
}