added proper status message

This commit is contained in:
Sören Oesterwind 2022-11-13 19:12:33 +01:00
parent 811d96f5fa
commit de22ec5c3a

View File

@ -1,9 +1,11 @@
const { app, BrowserWindow, ipcMain, Tray, Menu, nativeImage, MenuItem, dialog } = require('electron') const { app, BrowserWindow, ipcMain, Tray, Menu, MenuItem, dialog } = require('electron')
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const _ = require("underscore") const _ = require("underscore")
const open = require('open'); const open = require('open');
const childProcess = require('child_process'); const childProcess = require('child_process');
const http = require('http');
const packageJson = JSON.parse(fs.readFileSync("package.json")) const packageJson = JSON.parse(fs.readFileSync("package.json"))
@ -20,6 +22,8 @@ const tempJsonText = JSON.parse(fs.readFileSync("config.json", "utf8"));
configObject = _.extend(configObject, tempJsonText); configObject = _.extend(configObject, tempJsonText);
fs.writeFileSync("config.json", JSON.stringify(configObject)); fs.writeFileSync("config.json", JSON.stringify(configObject));
let serverStatus = "Starting"
const processArgs = process.argv; const processArgs = process.argv;
// Check if --headless is passed as an argument // Check if --headless is passed as an argument
if (processArgs.includes("--headless")) { if (processArgs.includes("--headless")) {
@ -31,11 +35,29 @@ if (processArgs.includes("--headless")) {
startServer() startServer()
createTray() createTray()
createWindow() createWindow()
setInterval(() => {
http.get('http://127.0.0.1:' + configObject.port + "/api/v1/system", (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let parsed = JSON.parse(data)
serverStatus = "Running"
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}, 1000);
}) })
ipcMain.on('info', function () { ipcMain.on('info', function () {
if (win) { if (win) {
win.webContents.send('info', { "appStatus": "Testing", "appURL": "http://127.0.0.1:" + configObject.port, "appName": "openCountdown " + packageJson.version, "startMinimised": configObject.startMinimised, "port": configObject.port }) win.webContents.send('info', { "appStatus": serverStatus, "appURL": "http://127.0.0.1:" + configObject.port, "appName": "openCountdown " + packageJson.version, "startMinimised": configObject.startMinimised, "port": configObject.port })
} }
}) })
@ -62,7 +84,8 @@ if (processArgs.includes("--headless")) {
fs.writeFileSync("config.json", JSON.stringify(configObject)); fs.writeFileSync("config.json", JSON.stringify(configObject));
dialog.showMessageBoxSync({ dialog.showMessageBoxSync({
message: "Port changed. Restart openCountdown to apply change.", message: "Port changed. Restart openCountdown to apply change.",
buttons: ["OK"]}) buttons: ["OK"]
})
}) })
} }
@ -131,21 +154,21 @@ function createTray() {
}); });
} }
function showScreen(){ function showScreen() {
win.show() win.show()
} }
function launchUI(){ function launchUI() {
open("http://127.0.0.1:" + configObject.port) open("http://127.0.0.1:" + configObject.port)
} }
function trayQuit(){ function trayQuit() {
let options = { let options = {
buttons: ["Yes","No"], buttons: ["Yes", "No"],
message: "Do you really want to quit openCountdown?" message: "Do you really want to quit openCountdown?"
} }
let response = dialog.showMessageBoxSync(options) let response = dialog.showMessageBoxSync(options)
if(response == 0){ if (response == 0) {
srvProc.kill("SIGINT") srvProc.kill("SIGINT")
setTimeout(app.quit, 1000) setTimeout(app.quit, 1000)
} }
@ -180,11 +203,11 @@ function runScript(scriptPath, callback, valueCb) {
srvProc = null; srvProc = null;
function setServer(process){ function setServer(process) {
srvProc = process srvProc = process
} }
function startServer(){ function startServer() {
runScript(path.join(__dirname, 'index.js'), function (err) { runScript(path.join(__dirname, 'index.js'), function (err) {
if (err) throw err; if (err) throw err;
}, setServer) }, setServer)