Sync period implementation

This commit is contained in:
everoddandeven 2024-10-16 23:24:01 +02:00
parent 245cc59394
commit f237d43557
5 changed files with 81 additions and 2 deletions

View file

@ -201,7 +201,6 @@ function isConnectedToWiFi(): Promise<boolean> {
// 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('--')) {

View file

@ -250,6 +250,8 @@ export class DaemonDataService {
throw new Error("Loop already started");
}
this._firstRefresh = true;
this.syncDisabledByPeriodPolicy = false;
this.syncDisabledByWifiPolicy = false;
this.refresh().then(() => {
this.refreshInterval = setInterval(() => {
@ -327,6 +329,40 @@ export class DaemonDataService {
}
public syncDisabledByWifiPolicy: boolean = false;
public syncDisabledByPeriodPolicy: boolean = false;
private isInTimeRange(fromHours: string, toHours: string): boolean {
const now = new Date();
// Estraiamo l'ora e i minuti dalla stringa in formato hh:mm
const [fromHour, fromMinute] = fromHours.split(":").map(Number);
const [toHour, toMinute] = toHours.split(":").map(Number);
// Otteniamo l'ora corrente in ore e minuti
const currentHour = now.getHours();
const currentMinute = now.getMinutes();
// Creiamo oggetti Date per le ore 'from', 'to', e l'ora attuale
const currentTime = new Date();
currentTime.setHours(currentHour, currentMinute, 0, 0);
const fromTime = new Date();
fromTime.setHours(fromHour, fromMinute, 0, 0);
const toTime = new Date();
toTime.setHours(toHour, toMinute, 0, 0);
// Gestione del caso in cui la fascia oraria attraversi la mezzanotte
if (fromTime > toTime) {
// Se l'ora attuale è dopo 'fromTime' o prima di 'toTime'
return currentTime >= fromTime || currentTime <= toTime;
} else {
// Caso normale: la fascia oraria è nello stesso giorno
return currentTime >= fromTime && currentTime <= toTime;
}
}
private async refresh(): Promise<void> {
if (this.refreshing || this.tooEarlyForRefresh) {
return;
@ -351,6 +387,7 @@ export class DaemonDataService {
if (wifiConnected) {
console.log("Disabling sync ...");
await this.daemonService.disableSync();
this.syncDisabledByWifiPolicy = true;
}
}
else if (!settings.noSync && syncAlreadyDisabled && !settings.syncOnWifi) {
@ -360,8 +397,23 @@ export class DaemonDataService {
console.log("Enabling sync ...");
await this.daemonService.enableSync();
this.syncDisabledByWifiPolicy = false;
}
else {
this.syncDisabledByWifiPolicy = true;
}
}
else {
this.syncDisabledByWifiPolicy = false;
}
if (!syncAlreadyDisabled && !this.syncDisabledByPeriodPolicy && settings.syncPeriodEnabled && !this.isInTimeRange(settings.syncPeriodFrom, settings.syncPeriodTo)) {
await this.daemonService.disableSync();
this.syncDisabledByPeriodPolicy = true;
}
else if (syncAlreadyDisabled && this.syncDisabledByPeriodPolicy && settings.syncPeriodEnabled && this.isInTimeRange(settings.syncPeriodFrom, settings.syncPeriodTo)) {
await this.daemonService.enableSync();
this.syncDisabledByPeriodPolicy = false;
}
this.syncStart.emit({ first: this._firstRefresh });

View file

@ -12,6 +12,19 @@
</div>
<app-daemon-not-running></app-daemon-not-running>
<div *ngIf="daemonRunning && syncDisabledByWifiPolicy" class="alert alert-warning d-flex align-items-center justify-content-center text-center" role="alert">
<h4><i class="bi bi-exclamation-triangle m-2"></i></h4>&nbsp;&nbsp;
<div>
Sync on wifi is disabled
</div>
</div>
<div *ngIf="daemonRunning && syncDisabledByPeriodPolicy" class="alert alert-warning d-flex align-items-center justify-content-center text-center" role="alert">
<h4><i class="bi bi-exclamation-triangle m-2"></i></h4>&nbsp;&nbsp;
<div>
Sync disabled from {{ syncDisabledTo }} to {{ syncDisabledFrom }}
</div>
</div>
<div *ngIf="daemonRunning && !stoppingDaemon" class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab" tabindex="0">

View file

@ -28,6 +28,21 @@ export class DetailComponent implements AfterViewInit, OnDestroy {
public readonly navbarLinks: NavbarLink[];
public get syncDisabledByWifiPolicy(): boolean {
return this.daemonData.syncDisabledByWifiPolicy;
}
public get syncDisabledByPeriodPolicy(): boolean {
return this.daemonData.syncDisabledByPeriodPolicy;
}
public get syncDisabledFrom(): string {
return this.daemonService.settings.syncPeriodFrom;
}
public get syncDisabledTo(): string {
return this.daemonService.settings.syncPeriodTo;
}
//#region Sync Info
private get height(): number {

View file

@ -24,7 +24,7 @@
</span>
<ul class="navbar-nav flex-row">
<li *ngIf="!running && !stopping && !starting" class="nav-item text-nowrap">
<li *ngIf="!running && !stopping && !starting && !restarting" class="nav-item text-nowrap">
<button class="btn btn-outline-secondary px-3 text-white" type="button" data-bs-toggle="collapse" aria-expanded="false" aria-label="Start daemon" (click)="startDaemon()">
<i class="bi bi-play-fill"></i>
</button>