From d934ec7fc90a0db619a3f82c7db83ab442b445ca Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Wed, 22 Jan 2020 17:03:38 +0100 Subject: [PATCH 1/4] Silence notifications until connected --- .../notification/notification.component.ts | 29 +++++-------------- src/app/notification/notification.service.ts | 26 ++++++++++++----- src/app/printer.service.ts | 16 ++++++++-- src/app/standby/standby.component.ts | 13 +++++++-- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/app/notification/notification.component.ts b/src/app/notification/notification.component.ts index 39e95ecac..90497e91d 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,37 +11,24 @@ 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() { 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..8153eefef 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -1,9 +1,10 @@ import { Component, OnInit } from '@angular/core'; import { ConfigService } from '../config/config.service'; import { Subscription } from 'rxjs'; -import { HttpClient, HttpErrorResponse } from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; import { AppService } from '../app.service'; +import { NotificationService } from '../notification/notification.service'; @Component({ selector: 'app-standby', @@ -16,7 +17,12 @@ export class StandbyComponent implements OnInit { 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) { } ngOnInit() { if (this.configService.getAutomaticScreenSleep()) { @@ -41,8 +47,9 @@ export class StandbyComponent implements OnInit { if (this.configService.getAutomaticScreenSleep()) { this.service.turnDisplayOn(); } + this.notificationService.enableNotifications(); this.router.navigate(['/main-screen']); - }, 4000); + }, 2000); }, () => { this.connecting = false; From ef82aa91cd9ccfee05ab75ca470a8c84d33df974 Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Wed, 22 Jan 2020 17:14:39 +0100 Subject: [PATCH 2/4] check connection before connecting --- .../notification/notification.component.ts | 1 - src/app/standby/standby.component.ts | 60 +++++++++++-------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/app/notification/notification.component.ts b/src/app/notification/notification.component.ts index 90497e91d..53eda1f12 100644 --- a/src/app/notification/notification.component.ts +++ b/src/app/notification/notification.component.ts @@ -34,5 +34,4 @@ export class NotificationComponent implements OnDestroy { public ngOnDestroy() { this.subscriptions.unsubscribe(); } - } diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts index 8153eefef..adf905331 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -1,10 +1,11 @@ import { Component, OnInit } from '@angular/core'; import { ConfigService } from '../config/config.service'; import { Subscription } from 'rxjs'; -import { HttpClient } from '@angular/common/http'; +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'; @Component({ selector: 'app-standby', @@ -15,7 +16,6 @@ export class StandbyComponent implements OnInit { connecting = false; error = ''; - httpPOSTRequest: Subscription; constructor( private configService: ConfigService, @@ -32,31 +32,43 @@ export class StandbyComponent implements OnInit { reconnect() { this.connecting = true; - if (this.httpPOSTRequest) { - this.httpPOSTRequest.unsubscribe(); - } + 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.notificationService.enableNotifications(); - this.router.navigate(['/main-screen']); - }, 2000); - }, - () => { - 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); } } @@ -67,4 +79,4 @@ interface ConnectCommand { printerProfile?: string; save?: boolean; autoconnect?: boolean; -} +} \ No newline at end of file From f2402f57853c8d13847039651f2a68dfa87226a6 Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Wed, 22 Jan 2020 17:15:20 +0100 Subject: [PATCH 3/4] Fix styling issue --- src/app/standby/standby.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts index adf905331..d874956af 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -79,4 +79,4 @@ interface ConnectCommand { printerProfile?: string; save?: boolean; autoconnect?: boolean; -} \ No newline at end of file +} From 129909840b810ff3443b1f2f882188566b3d280d Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Wed, 22 Jan 2020 17:21:15 +0100 Subject: [PATCH 4/4] Add PSU Control on reconnect --- src/app/config/config.service.ts | 5 +++++ src/app/standby/standby.component.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) 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/standby/standby.component.ts b/src/app/standby/standby.component.ts index d874956af..4c73fc0d2 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -1,11 +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', @@ -22,7 +22,8 @@ export class StandbyComponent implements OnInit { private http: HttpClient, private router: Router, private service: AppService, - private notificationService: NotificationService) { } + private notificationService: NotificationService, + private psuControlService: PsuControlService) { } ngOnInit() { if (this.configService.getAutomaticScreenSleep()) { @@ -32,6 +33,9 @@ export class StandbyComponent implements OnInit { reconnect() { this.connecting = true; + if (this.configService.isPSUControlEnabled()) { + this.psuControlService.changePSUState(true); + } this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders()) .subscribe( (data: OctoprintConnectionAPI) => {