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

actually check if the printer is connected after connecting #414

Merged
merged 3 commits into from
Feb 11, 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
4 changes: 2 additions & 2 deletions src/app/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export class NotificationService {
}

enableNotifications() {
console.clear();
// console.clear();
this.hideNotifications = false;
}

disableNotifications() {
console.clear();
// console.clear();
this.hideNotifications = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/printer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class PrinterService {
this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders())
.subscribe(
(data: OctoprintConnectionAPI) => {
resolve(data.current.state === 'Closed');
resolve(data.current.state === 'Closed' || data.current.state.includes('Error:'));
},
(error: HttpErrorResponse) => {
this.notificationService.setError('Can\'t retrieve connection state!', error.message);
Expand Down
58 changes: 38 additions & 20 deletions src/app/standby/standby.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { PsuControlService } from '../plugin-service/psu-control.service';
})
export class StandbyComponent implements OnInit {

connecting = false;
error = '';
public connecting = false;
public error = '';
private connectionRetries = 3;

constructor(
private configService: ConfigService,
Expand All @@ -31,25 +32,35 @@ export class StandbyComponent implements OnInit {
}
}

reconnect() {
public reconnect() {
this.connecting = true;
if (this.configService.turnOnPSUWhenExitingSleep()) {
this.psuControlService.changePSUState(true);
setTimeout(this.checkConnection.bind(this), 5000);
} else {
this.checkConnection();
}
}

private connectToPrinter() {
this.http.post(this.configService.getURL('connection'), connectPayload, this.configService.getHTTPHeaders())
.subscribe(
() => { setTimeout(this.checkConnection.bind(this), 3000); },
() => { this.setConnectionError(); });
}

private checkConnection() {
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.';
});
if (data.current.state !== 'Operational') {
if (this.connectionRetries === 0) {
this.connectionRetries = 3;
this.setConnectionError();
} else {
this.connectionRetries--;
setTimeout(this.connectToPrinter.bind(this), 500);
}
} else {
this.disableStandby();
}
Expand All @@ -58,24 +69,31 @@ export class StandbyComponent implements OnInit {
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
};
}

disableStandby() {
private setConnectionError() {
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.';
}

private disableStandby() {
setTimeout(() => {
this.connecting = false;
if (this.configService.getAutomaticScreenSleep()) {
this.service.turnDisplayOn();
}
this.notificationService.enableNotifications();
this.router.navigate(['/main-screen']);
}, 2000);
}, 1000);
}
}

const connectPayload: ConnectCommand = {
command: 'connect',
save: false
};

interface ConnectCommand {
command: string;
port?: string;
Expand Down