Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
345 lines (286 loc) · 9.44 KB
/
Copy pathserver.js
File metadata and controls
345 lines (286 loc) · 9.44 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
const fs = require("fs");
const { vJoy, vJoyDevice } = require("vjoy");
const express = require("express");
const groupRender = require("./lib/groupRender.js");
const configManager = require("./lib/configManager.js");
const nunjucks = require("nunjucks");
const app = express();
const path = require("path");
const { networkInterfaces } = require("os");
const { setTimeout } = require("timers");
const {
color,
log,
red,
green,
cyan,
cyanBright,
} = require("console-log-colors");
require("log-timestamp")(function () {
return "<" + new Date().toISOString() + "> %s";
});
// -----------------------------------------------------------------
// read the global config
let globalConfig = JSON.parse(
fs.readFileSync(path.join(__dirname, "/global-config.json"))
);
// -----------------------------------------------------------------
// read the last loaded config
let userConfig = {};
userConfig = configManager.readLastConfig(globalConfig.lastConfig.filePath);
let configs = configManager.readConfigs(path.join(__dirname, "/configs"));
// -----------------------------------------------------------------
// find the current local IP
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === "IPv4" && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
const inferEthernetInterface = () => {
const keys = Object.keys(results);
for (const key of keys) {
if (key.startsWith("Ethernet")) {
return results[key];
}
}
return keys.length > 0 ? results[keys[0]] : null;
};
let serverAdress = (results["Ethernet"] ||
inferEthernetInterface() || ["localhost"])[0];
// -----------------------------------------------------------------
// create a joystick
let deviceId = userConfig.vJoy.deviceId;
// -----------------------------------------------------------------
// check if there is a device ID
if (process.argv.length > 2) {
deviceId = Number(process.argv[2]);
}
// -----------------------------------------------------------------
// check if vjoy controllers are enabled
if (!vJoy.isEnabled()) {
console.log("vJoy is not enabled.");
process.exit();
}
// -----------------------------------------------------------------
// create a device
let device = vJoyDevice.create(deviceId);
// -----------------------------------------------------------------
// check if it was created
if (device === null) {
console.log(
`Could not initialize the device. Status: ${vJoyDevice.status(
deviceId
)}`
);
process.exit();
}
// -----------------------------------------------------------------
// resetting the buttons on start-up
device.resetButtons();
// -----------------------------------------------------------------
// setup the express server
app.listen(process.env.port || globalConfig.server.port);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "nunjucks");
app.use(express.static(path.join(__dirname, "public")));
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
nunjucks.configure("views", {
autoescape: false,
express: app,
});
let renderedGroups = groupRender(
userConfig,
userConfig.general.defaultButtonsEnabled
);
let sliders = userConfig.customInputs.filter((item) => item.type === "slider");
// -----------------------------------------------------------------
// Init Sliders
setTimeout(() => {
sliders.forEach((element) => {
try {
device.axes[`${element.id}`].set(element.range.default);
} catch (error) {
console.log("error setting default range");
}
});
}, 1500);
// -----------------------------------------------------------------
// requests
app.get("/", function (req, res) {
res.render("client.html", {
renderedGroups: renderedGroups,
configStorage: JSON.stringify(userConfig),
configs: JSON.stringify(configs),
});
});
app.get("/config", function (req, res) {
res.render("config.html");
});
app.post("/button", function (req, res) {
console.log(req.body);
let button = {
id: req.body.id,
type: req.body.type,
};
device.buttons[button.id].set(true); // press the first button
setTimeout(() => {
device.buttons[button.id].set(false);
}, 50);
res.send(JSON.stringify({ status: 200, id: button.id })); // try res.json() if getList() returns an object or array
});
app.post("/pseudotoggle", function (req, res) {
console.log(req.body);
let button = {
id: req.body.id,
type: req.body.type,
};
device.buttons[button.id].set(true); // press the first button
setTimeout(() => {
device.buttons[button.id].set(false);
}, 50);
res.send(JSON.stringify({ status: 200, id: button.id })); // try res.json() if getList() returns an object or array
});
app.post("/toggle", function (req, res) {
console.log(req.body);
let button = {
id: req.body.id,
state: req.body.state,
type: req.body.type,
};
device.buttons[button.id].set(button.state);
res.send(
JSON.stringify({ status: 200, id: button.id, state: button.state })
); // try res.json() if getList() returns an object or array
});
app.post("/repeat", function (req, res) {
console.log(req.body);
let button = {
id: req.body.id,
interval: req.body.interval,
type: req.body.type,
};
device.buttons[button.id].set(true); // press the first button
setTimeout(() => {
device.buttons[button.id].set(false);
}, 300);
res.send(
JSON.stringify({
status: 200,
id: button.id,
interval: button.interval,
})
); // try res.json() if getList() returns an object or array
});
app.post("/slider", function (req, res) {
console.log(req.body);
let slider = {
id: req.body.id,
type: req.body.type,
value: parseInt(req.body.value),
};
device.axes[`${slider.id}`].set(slider.value);
res.send(
JSON.stringify({ status: 200, id: slider.id, value: slider.value })
); // try res.json() if getList() returns an object or array
});
app.post("/hold", function (req, res) {
console.log(req.body);
let button = {
id: req.body.id,
type: req.body.type,
};
device.buttons[button.id].set(true); // press the first button
setTimeout(() => {
device.buttons[button.id].set(false);
}, 300);
res.send(JSON.stringify({ status: 200, id: button.id })); // try res.json() if getList() returns an object or array
});
app.post("/refresh", function (req, res) {
console.log(
"Site has been refreshed/started up, changing buttons states to their defaults"
);
configs = configManager.readConfigs(path.join(__dirname, "/configs"));
device.resetButtons();
setTimeout(() => {
sliders.forEach((element) => {
try {
console.log(
`Setting ${element.id} to defined default value ${element.range.default}`
);
device.axes[`${element.id}`].set(element.range.default);
} catch (error) {
console.log("error setting default range");
}
});
}, 1500);
});
// -----------------------------------------------------------------
// config management
app.post("/changeConfig", function (req, res) {
console.log(`Changing config to "${req.body.name}".`);
// unbinding the current device
device.free();
// setting the new userconfig
userConfig = configManager.changeConfig(req.body.file);
// setting the last loaded config
globalConfig.lastConfig.filePath = req.body.file;
globalConfig.lastConfig.name = req.body.name;
configManager.writeLastConfig(
path.join(__dirname, "/global-config.json"),
globalConfig
);
// rerender the groups
renderedGroups = groupRender(
userConfig,
userConfig.general.defaultButtonsEnabled
);
sliders = userConfig.customInputs.filter((item) => item.type === "slider");
// create a device
device = vJoyDevice.create(parseInt(req.body.vJoy.deviceId));
// check if it was created
if (device === null) {
console.log(
`Could not initialize the device. Status: ${vJoyDevice.status(
parseInt(req.body.vJoy.deviceId)
)}`
);
process.exit();
}
// sending response to the client
res.send(JSON.stringify({ status: 200, op: "ok" }));
});
// -----------------------------------------------------------------
// server management
app.post("/stopServer", function () {
console.log("Stopping Server.");
process.exit();
});
app.post("/restartServer", function () {
console.log("Restarting Server.");
device.resetButtons();
listenToGameLog("/Games/StarCitizen/LIVE/Game.log");
});
// -----------------------------------------------------------------
// running announcement
console.log(
`Server running with the config "${green(
userConfig.name
)}" on vJoy device id ${green(
userConfig.vJoy.deviceId
)} at: ${green.underline(
"http://" + serverAdress + ":" + globalConfig.server.port + "/"
)}`
);