Skip to content

Commit

Permalink
Automatic upgrade and disable sync on wifi implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Oct 14, 2024
1 parent 10f771f commit c95017f
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 96 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Maybe you only want to execute the application in the browser with hot reload? J
| `npm run ng:serve` | Execute the app in the web browser (DEV mode) |
| `npm run web:build` | Build the app that can be used directly in the web browser. Your built files are in the /dist folder. |
| `npm run electron:local` | Builds your application and start electron locally |
| `npm run electron:local:dev` | Builds your application and start electron locally (DEV MODE) |
| `npm run electron:build` | Builds your application and creates an app consumable based on your operating system |

**Your application is optimised. Only /dist folder and NodeJS dependencies are included in the final bundle.**
Expand Down
99 changes: 92 additions & 7 deletions app/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app, BrowserWindow, ipcMain, screen, dialog, Tray, Menu, MenuItemConstructorOptions } from 'electron';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { ChildProcessWithoutNullStreams, exec, ExecException, spawn } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import * as https from 'https';
Expand All @@ -21,7 +21,13 @@ const args = process.argv.slice(1),
function createWindow(): BrowserWindow {

const size = screen.getPrimaryDisplay().workAreaSize;
const wdwIcon = path.join(__dirname, 'assets/icons/monero-symbol-on-white-480.png');
let dirname = __dirname;

if (dirname.endsWith('/app')) {
dirname = dirname.replace('/app', '/src')
}

const wdwIcon = path.join(dirname, 'assets/icons/monero-symbol-on-white-480.png');

const trayMenuTemplate: MenuItemConstructorOptions[] = [
{
Expand Down Expand Up @@ -128,14 +134,19 @@ function createWindow(): BrowserWindow {
return win;
}

function isWifiConnected() {
function isWifiConnectedOld() {
console.log("isWifiConnected()");
const networkInterfaces = os.networkInterfaces();

console.log(`isWifiConnected(): network interfaces ${networkInterfaces}`);
console.log(networkInterfaces);

for (const interfaceName in networkInterfaces) {
const networkInterface = networkInterfaces[interfaceName];

if (networkInterface) {
for (const network of networkInterface) {
console.log(network);
if (network.family === 'IPv4' && !network.internal && network.mac !== '00:00:00:00:00:00') {
if (interfaceName.toLowerCase().includes('wifi') || interfaceName.toLowerCase().includes('wlan')) {
return true;
Expand All @@ -147,6 +158,62 @@ function isWifiConnected() {
return false;
}

function isConnectedToWiFi(): Promise<boolean> {
return new Promise((resolve, reject) => {
const platform = os.platform(); // Use os to get the platform

let command = '';
if (platform === 'win32') {
// Windows: Use 'netsh' command to check the Wi-Fi status
command = 'netsh wlan show interfaces';
} else if (platform === 'darwin') {
// macOS: Use 'airport' command to check the Wi-Fi status
command = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep 'state: running'";
} else if (platform === 'linux') {
// Linux: Use 'nmcli' to check for Wi-Fi connectivity
command = 'nmcli dev status';
} else {
resolve(false); // Unsupported platform
}

// Execute the platform-specific command
if (command) {
exec(command, (error: ExecException | null, stdout: string, stderr: string) => {
if (error) {
console.error(error);
reject(stderr);
resolve(false); // In case of error, assume not connected to Wi-Fi
} else {
// Check if the output indicates a connected status
if (stdout) {
const components: string[] = stdout.split("\n");
console.log(components);

components.forEach((component: string) => {
if (component.includes('wifi') && !component.includes('--')) {
resolve(true);
}
});

resolve(false);
} else {
resolve(false);
}
}
});
}
});
}

function isWifiConnected() {
isConnectedToWiFi().then((connected: boolean) => {
win?.webContents.send('is-wifi-connected-result', connected);
}).catch((error: any) => {
console.error(error);
win?.webContents.send('is-wifi-connected-result', false);
});
}

function getMonerodVersion(monerodFilePath: string): void {
const monerodProcess = spawn(monerodFilePath, [ '--version' ]);

Expand All @@ -159,6 +226,8 @@ function getMonerodVersion(monerodFilePath: string): void {
})
}

let moneroFirstStdout: boolean = true;

function startMoneroDaemon(commandOptions: string[]): ChildProcessWithoutNullStreams {
const monerodPath = commandOptions.shift();

Expand All @@ -168,26 +237,41 @@ function startMoneroDaemon(commandOptions: string[]): ChildProcessWithoutNullStr
}

console.log("Starting monerod daemon with options: " + commandOptions.join(" "));

moneroFirstStdout = true;

// Avvia il processo usando spawn
const monerodProcess = spawn(monerodPath, commandOptions);

// Gestisci l'output di stdout in streaming
monerodProcess.stdout.on('data', (data) => {
//console.log(`monerod stdout: ${data}`);
const pattern = '**********************************************************************';

if (moneroFirstStdout && data.includes(pattern)) {
win?.webContents.send('monerod-started', true);
moneroFirstStdout = false;
}

win?.webContents.send('monero-stdout', `${data}`);
// Puoi anche inviare i log all'interfaccia utente tramite IPC
});

// Gestisci gli errori in stderr
monerodProcess.stderr.on('data', (data) => {
//console.error(`monerod stderr: ${data}`);
console.error(`monerod error: ${data}`);

if (moneroFirstStdout) {
win?.webContents.send('monerod-started', false);
moneroFirstStdout = false;
}

win?.webContents.send('monero-stderr', `${data}`);
});

// Gestisci la chiusura del processo
monerodProcess.on('close', (code) => {
console.log(`monerod chiuso con codice: ${code}`);
monerodProcess.on('close', (code: number) => {
console.log(`monerod exited with code: ${code}`);
win?.webContents.send('monero-stdout', `monerod exited with code: ${code}`);
win?.webContents.send('monero-close', code);
});
Expand Down Expand Up @@ -421,7 +505,8 @@ try {
});

ipcMain.handle('is-wifi-connected', async (event) => {
win?.webContents.send('is-wifi-connected-result', isWifiConnected());
isWifiConnected();
//win?.webContents.send('is-wifi-connected-result', isWifiConnected());
});

ipcMain.handle('get-os-type', (event) => {
Expand Down
3 changes: 3 additions & 0 deletions app/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
startMonerod: (args) => {
ipcRenderer.invoke('start-monerod', args);
},
onMonerodStarted: (callback) => {
ipcRenderer.on('monerod-started', callback);
},
onMoneroStdout: (callback) => {
ipcRenderer.on('monero-stdout', callback);
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"electron:serve-tsc": "tsc -p tsconfig.serve.json",
"electron:serve": "wait-on tcp:4200 && npm run electron:serve-tsc && electron . --serve",
"electron:local": "npm run build:prod && electron .",
"electron:local:dev": "npm run build:dev && electron .",
"electron:build": "npm run build:prod && electron-builder build --publish=never",
"test": "ng test --watch=false",
"test:watch": "ng test",
Expand Down
51 changes: 43 additions & 8 deletions src/app/core/services/daemon/daemon-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class DaemonDataService {
private _txPoolBacklog: TxBacklogEntry[] = [];
private _gettingTxPoolBackLog: boolean = false;

public readonly syncStart: EventEmitter<void> = new EventEmitter<void>();
public readonly syncStart: EventEmitter<{ first: boolean }> = new EventEmitter<{ first: boolean }>();
public readonly syncEnd: EventEmitter<void> = new EventEmitter<void>();
public readonly syncError: EventEmitter<Error> = new EventEmitter<Error>();

Expand Down Expand Up @@ -91,7 +91,7 @@ export class DaemonDataService {
}

public get running(): boolean {
return this._daemonRunning;
return this._daemonRunning
}

public get starting(): boolean {
Expand Down Expand Up @@ -239,11 +239,17 @@ export class DaemonDataService {
throw new Error("Loop already started");
}
this._firstRefresh = true;
this.refreshInterval = setInterval(() => {
this.refresh().then().catch((error: any) => {
console.error(error);
});
},this.refreshTimeoutMs);

this.refresh().then(() => {
this.refreshInterval = setInterval(() => {
this.refresh().then().catch((error: any) => {
console.error(error);
});
},this.refreshTimeoutMs);
}).catch((error: any) => {
console.error(error);
this._refreshing = false;
});
}

private stopLoop(): void {
Expand Down Expand Up @@ -320,7 +326,31 @@ export class DaemonDataService {
}

this._refreshing = true;
this.syncStart.emit();

const settings = await this.daemonService.getSettings();

const updateInfo = await this.daemonService.checkUpdate()

if (updateInfo.update) {
await this.daemonService.upgrade();
return;
}

const syncAlreadyDisabled = this.daemonService.settings.noSync;
const wifiConnected = await this.daemonService.isWifiConnected();

if (!settings.noSync && !syncAlreadyDisabled && !settings.syncOnWifi && wifiConnected) {
console.log("Disabling sync ...");

await this.daemonService.disableSync();
}
else if (!settings.noSync && syncAlreadyDisabled && !settings.syncOnWifi && !wifiConnected) {
console.log("Enabling sync ...");

await this.daemonService.enableSync();
}

this.syncStart.emit({ first: this._firstRefresh });

try {
const firstRefresh = this._firstRefresh;
Expand All @@ -337,6 +367,11 @@ export class DaemonDataService {
this._daemonInfo = await this.daemonService.getInfo();
this._gettingDaemonInfo = false;

if (this.daemonService.settings.upgradeAutomatically && this._daemonInfo.updateAvailable) {
await this.daemonService.upgrade();
return;
}

this._gettingSyncInfo = true;
this.syncInfoRefreshStart.emit();
this._syncInfo = await this.daemonService.syncInfo();
Expand Down
Loading

0 comments on commit c95017f

Please sign in to comment.