Implemented shutdown through UI and implemented router check
This commit is contained in:
43
src/Main.ts
43
src/Main.ts
@@ -10,7 +10,10 @@ import { UnityRunner } from './Unity/UnityRunner';
|
||||
import { UnityWebSocket } from './Unity/UnityWebSocket';
|
||||
import { TwilioHandler } from './Twilio';
|
||||
import { delay } from './Utils';
|
||||
import * as ping from 'ping';
|
||||
import { shutdown } from './Shutdown';
|
||||
|
||||
const PREFIX = '[Main]';
|
||||
export class Main {
|
||||
dataPath = join(homedir(), 'MorphixProductions', 'NTSHControl');
|
||||
|
||||
@@ -26,6 +29,9 @@ export class Main {
|
||||
|
||||
async start() {
|
||||
await this.ConfigurationManager.load();
|
||||
|
||||
await this.waitForRouter();
|
||||
|
||||
await this.WebServer.listen();
|
||||
await this.Twilio.load();
|
||||
|
||||
@@ -73,4 +79,41 @@ export class Main {
|
||||
|
||||
console.log('Restart complete.');
|
||||
}
|
||||
|
||||
async shutdown() {
|
||||
console.log('Stopping UnityRunner...');
|
||||
await this.UnityRunner.stop();
|
||||
|
||||
const doShutdown = process.argv.includes('--no-shutdown')
|
||||
? false
|
||||
: true;
|
||||
if (doShutdown) {
|
||||
console.log('Shutting down system...');
|
||||
shutdown();
|
||||
} else {
|
||||
console.log('Shutdown skipped due to --no-shutdown flag.');
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
waitForRouter() {
|
||||
if (this.Config.router?.waitForStartup !== true) return;
|
||||
return new Promise<void>((resolve) => {
|
||||
const check = () => {
|
||||
console.log(PREFIX, 'Waiting for router...');
|
||||
ping.sys.probe(this.Config.router.ip, async ({ alive }) => {
|
||||
if (alive) {
|
||||
console.log(PREFIX, 'Router is online');
|
||||
await delay(3000);
|
||||
return resolve();
|
||||
}
|
||||
|
||||
await delay(1000);
|
||||
check();
|
||||
});
|
||||
};
|
||||
check();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user