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

Properly handle Connection Errors #569

Merged
merged 3 commits into from
Apr 29, 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
13 changes: 11 additions & 2 deletions src/app/plugin-service/enclosure.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { NotificationService } from '../notification/notification.service';
export class EnclosureService {
private httpGETRequest: Subscription;
private observable: Observable<TemperatureReading>;
private initialRequest = true;

public constructor(
private http: HttpClient,
private configService: ConfigService,
private notificationService: NotificationService,
) {
this.observable = new Observable((observer: Observer<TemperatureReading>): void => {
timer(2500, 30000).subscribe((): void => {
timer(2500, 15000).subscribe((): void => {
if (this.httpGETRequest) {
this.httpGETRequest.unsubscribe();
}
Expand All @@ -33,14 +34,22 @@ export class EnclosureService {
)
.subscribe(
(data: EnclosurePluginAPI): void => {
this.initialRequest = false;
observer.next(({
temperature: data.temp_sensor_temp,
humidity: data.temp_sensor_humidity,
unit: data.use_fahrenheit ? '°F' : '°C',
} as unknown) as TemperatureReading);
},
(error: HttpErrorResponse): void => {
this.notificationService.setError("Can't retrieve enclosure temperature!", error.message);
if (this.initialRequest && error.status === 500) {
this.initialRequest = false;
} else {
this.notificationService.setError(
"Can't retrieve enclosure temperature!",
error.message,
);
}
},
);
});
Expand Down
23 changes: 18 additions & 5 deletions src/app/standby/standby.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class StandbyComponent implements OnInit {
.post(this.configService.getURL('connection'), connectPayload, this.configService.getHTTPHeaders())
.subscribe(
(): void => {
setTimeout(this.checkConnection.bind(this), 3000);
setTimeout(this.checkConnection.bind(this), 5000);
},
(): void => {
this.setConnectionError();
Expand All @@ -60,16 +60,29 @@ export class StandbyComponent implements OnInit {
this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders()).subscribe(
(data: OctoprintConnectionAPI): void => {
if (data.current.state === 'Closed') {
if (this.connectionRetries === 0) {
if (this.connectionRetries <= 0) {
this.connectionRetries = 3;
this.setConnectionError();
} else {
this.connectionRetries--;
setTimeout(this.connectToPrinter.bind(this), 500);
}
} else if (data.current.state === 'Error') {
this.connectionRetries = 3;
this.setConnectionError();
} else if (data.current.state.includes('Error')) {
if (this.connectionRetries <= 1) {
this.connectionRetries = 3;
this.setConnectionError();
} else {
this.connectionRetries--;
setTimeout(this.connectToPrinter.bind(this), 500);
}
} else if (data.current.state === 'Connecting') {
if (this.connectionRetries < 0) {
this.connectionRetries = 3;
this.setConnectionError();
} else {
this.connectionRetries--;
setTimeout(this.checkConnection.bind(this), 5000);
}
} else {
this.disableStandby();
}
Expand Down