diff --git a/src/app/config/config.service.ts b/src/app/config/config.service.ts index 146e2aaf3..fc0d37a82 100644 --- a/src/app/config/config.service.ts +++ b/src/app/config/config.service.ts @@ -154,6 +154,11 @@ export class ConfigService { public getAutomaticScreenSleep(): boolean { return this.config.octodash.turnScreenOffSleep; } + + public isPSUControlEnabled(): boolean { + // TODO: implement in next config change + return false; + } } export interface Config { diff --git a/src/app/notification/notification.component.ts b/src/app/notification/notification.component.ts index 39e95ecac..53eda1f12 100644 --- a/src/app/notification/notification.component.ts +++ b/src/app/notification/notification.component.ts @@ -1,8 +1,6 @@ import { Component, OnDestroy } from '@angular/core'; import { Subscription } from 'rxjs'; -import { NotificationService, Message } from './notification.service'; -import { PrinterService } from '../printer.service'; -import { Router } from '@angular/router'; +import { NotificationService, Notification } from './notification.service'; @Component({ selector: 'app-notification', @@ -13,41 +11,27 @@ export class NotificationComponent implements OnDestroy { private subscriptions: Subscription = new Subscription(); - public notification: Message = { + public notification: Notification = { heading: '', text: '', type: '' }; public show = false; - public constructor(private notificationService: NotificationService, private printerService: PrinterService, private router: Router) { - this.subscriptions.add(this.notificationService.getObservable().subscribe((message: Message) => this.setMessage(message))); + public constructor(private notificationService: NotificationService) { + this.subscriptions.add(this.notificationService.getObservable().subscribe((message: Notification) => this.setNotification(message))); } public hideNotification() { this.show = false; } - public setMessage(message: Message) { - if (message.printerStatusError) { - this.printerService.isPrinterOffline().then((printerOffline) => { - if (printerOffline) { - this.hideNotification(); - console.clear(); - this.router.navigate(['/standby']); - } else { - this.notification = message; - this.show = true; - } - }); - } else { - this.notification = message; - this.show = true; - } + public setNotification(notification: Notification) { + this.notification = notification; + this.show = true; } public ngOnDestroy() { this.subscriptions.unsubscribe(); } - } diff --git a/src/app/notification/notification.service.ts b/src/app/notification/notification.service.ts index a5608c691..78b121d77 100644 --- a/src/app/notification/notification.service.ts +++ b/src/app/notification/notification.service.ts @@ -7,17 +7,30 @@ import { shareReplay } from 'rxjs/operators'; }) export class NotificationService { - private observable: Observable; - private observer: Observer; + private observable: Observable; + private observer: Observer; + private hideNotifications = false; constructor() { - this.observable = new Observable((observer: Observer) => { + this.observable = new Observable((observer: Observer) => { this.observer = observer; }).pipe(shareReplay(1)); } - setError(heading: string, text: string, printerStatusError = false) { - this.observer.next({ heading, text, type: 'error', printerStatusError }); + enableNotifications() { + console.clear(); + this.hideNotifications = false; + } + + disableNotifications() { + console.clear(); + this.hideNotifications = true; + } + + setError(heading: string, text: string) { + if (!this.hideNotifications) { + this.observer.next({ heading, text, type: 'error' }); + } } setUpdate(heading: string, text: string) { @@ -29,9 +42,8 @@ export class NotificationService { } } -export interface Message { +export interface Notification { heading: string; text: string; type: string; - printerStatusError?: boolean; } diff --git a/src/app/printer.service.ts b/src/app/printer.service.ts index cabffa0a9..e330a252d 100644 --- a/src/app/printer.service.ts +++ b/src/app/printer.service.ts @@ -6,6 +6,7 @@ import { shareReplay } from 'rxjs/operators'; import { OctoprintPrinterStatusAPI } from './octoprint-api/printerStatusAPI'; import { NotificationService } from './notification/notification.service'; import { OctoprintConnectionAPI } from './octoprint-api/connectionAPI'; +import { Router } from '@angular/router'; @Injectable({ providedIn: 'root' @@ -16,7 +17,11 @@ export class PrinterService { httpPOSTRequest: Subscription; observable: Observable; - constructor(private http: HttpClient, private configService: ConfigService, private notificationService: NotificationService) { + constructor( + private http: HttpClient, + private configService: ConfigService, + private notificationService: NotificationService, + private router: Router) { this.observable = new Observable((observer: Observer) => { timer(500, this.configService.getAPIInterval()).subscribe(_ => { if (this.httpGETRequest) { @@ -38,7 +43,14 @@ export class PrinterService { observer.next(printerStatus); }, (error: HttpErrorResponse) => { if (error.status === 409) { - this.notificationService.setError('Can\'t retrieve printer status!', error.message, true); + this.isPrinterOffline().then((printerOffline) => { + if (printerOffline) { + this.router.navigate(['/standby']); + this.notificationService.disableNotifications(); + } else { + this.notificationService.setError('Can\'t retrieve printer status!', error.message); + } + }); } else { const printerStatus: PrinterStatusAPI = { status: `error (${error.status})`, diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts index 19d24dedc..4c73fc0d2 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -1,9 +1,11 @@ import { Component, OnInit } from '@angular/core'; import { ConfigService } from '../config/config.service'; -import { Subscription } from 'rxjs'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Router } from '@angular/router'; import { AppService } from '../app.service'; +import { NotificationService } from '../notification/notification.service'; +import { OctoprintConnectionAPI } from '../octoprint-api/connectionAPI'; +import { PsuControlService } from '../plugin-service/psu-control.service'; @Component({ selector: 'app-standby', @@ -14,9 +16,14 @@ export class StandbyComponent implements OnInit { connecting = false; error = ''; - httpPOSTRequest: Subscription; - constructor(private configService: ConfigService, private http: HttpClient, private router: Router, private service: AppService) { } + constructor( + private configService: ConfigService, + private http: HttpClient, + private router: Router, + private service: AppService, + private notificationService: NotificationService, + private psuControlService: PsuControlService) { } ngOnInit() { if (this.configService.getAutomaticScreenSleep()) { @@ -26,30 +33,46 @@ export class StandbyComponent implements OnInit { reconnect() { this.connecting = true; - if (this.httpPOSTRequest) { - this.httpPOSTRequest.unsubscribe(); + if (this.configService.isPSUControlEnabled()) { + this.psuControlService.changePSUState(true); } + this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders()) + .subscribe( + (data: OctoprintConnectionAPI) => { + if (data.current.state === 'Closed') { + this.http.post(this.configService.getURL('connection'), connectPayload, this.configService.getHTTPHeaders()) + .subscribe( + () => { + this.disableStandby(); + }, + () => { + this.connecting = false; + this.error = + 'OctoPrint can\'t connect to your printer. Please make sure that the connection works, then come back and try again.'; + }); + } else { + this.disableStandby(); + } + }, + (error: HttpErrorResponse) => { + this.connecting = false; + this.error = 'There is something really wrong, OctoDash can\'t get a response from OctoPrint. Please check your setup!'; + }); const connectPayload: ConnectCommand = { command: 'connect', save: false }; - this.httpPOSTRequest = this.http.post(this.configService.getURL('connection'), connectPayload, this.configService.getHTTPHeaders()) - .subscribe( - () => { - setTimeout(() => { - this.connecting = false; - if (this.configService.getAutomaticScreenSleep()) { - this.service.turnDisplayOn(); - } - this.router.navigate(['/main-screen']); - }, 4000); - }, - () => { - this.connecting = false; - this.error = - 'OctoPrint can\'t connect to your printer. Please make sure that the connection works, then come back and try again.'; - } - ); + } + + disableStandby() { + setTimeout(() => { + this.connecting = false; + if (this.configService.getAutomaticScreenSleep()) { + this.service.turnDisplayOn(); + } + this.notificationService.enableNotifications(); + this.router.navigate(['/main-screen']); + }, 2000); } }