Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/standby improvements #378

Merged
merged 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 7 additions & 23 deletions src/app/notification/notification.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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();
}

}
26 changes: 19 additions & 7 deletions src/app/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@ import { shareReplay } from 'rxjs/operators';
})
export class NotificationService {

private observable: Observable<Message>;
private observer: Observer<Message>;
private observable: Observable<Notification>;
private observer: Observer<Notification>;
private hideNotifications = false;

constructor() {
this.observable = new Observable((observer: Observer<Message>) => {
this.observable = new Observable((observer: Observer<Notification>) => {
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) {
Expand All @@ -29,9 +42,8 @@ export class NotificationService {
}
}

export interface Message {
export interface Notification {
heading: string;
text: string;
type: string;
printerStatusError?: boolean;
}
16 changes: 14 additions & 2 deletions src/app/printer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -16,7 +17,11 @@ export class PrinterService {
httpPOSTRequest: Subscription;
observable: Observable<PrinterStatusAPI>;

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<PrinterStatusAPI>) => {
timer(500, this.configService.getAPIInterval()).subscribe(_ => {
if (this.httpGETRequest) {
Expand All @@ -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})`,
Expand Down
67 changes: 45 additions & 22 deletions src/app/standby/standby.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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()) {
Expand All @@ -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);
}
}

Expand Down