Skip to content

Commit

Permalink
Remove migratePSUControlOption function & clean code
Browse files Browse the repository at this point in the history
Co-authored-by: Timon G. <[email protected]>
  • Loading branch information
xunleii and UnchartedBull committed Dec 22, 2020
1 parent 6b1b290 commit 2fde959
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 23 deletions.
18 changes: 1 addition & 17 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export class AppComponent implements OnInit {
if (this.configService && this.configService.isInitialized()) {
if (this.configService.isLoaded()) {
if (this.configService.isValid()) {
// TODO: this call is only here to allow migration path on `turnOnPrinterWhenExitingSleep`
// option release.
this.migratePSUControlOption();

this.waitForOctoprint();
} else {
this.checkInvalidConfig();
Expand All @@ -60,7 +56,7 @@ export class AppComponent implements OnInit {
const errors = this.configService.getErrors();

if (this.service.hasUpdateError(errors)) {
if (this.service.autoFixErrors(errors)) {
if (this.service.fixUpdateErrors(errors)) {
this.initialize();
} else {
this.configService.setUpdate();
Expand Down Expand Up @@ -130,16 +126,4 @@ export class AppComponent implements OnInit {
// })
// .finally(() => clearTimeout(showPrinterConnectedTimeout));
}

// TODO: this method is only here to allow migration path on `turnOnPrinterWhenExitingSleep`
// option release, which can break current PSUControl behaviour.
// It will be removed when all people have used a release with this fix.
private migratePSUControlOption() {
const config = this.configService.getCurrentConfig();
if (config.plugins.psuControl.turnOnPSUWhenExitingSleep) {
config.octodash.turnOnPrinterWhenExitingSleep = true;
config.plugins.psuControl.turnOnPSUWhenExitingSleep = false;
this.configService.saveConfig(config);
}
}
}
10 changes: 6 additions & 4 deletions src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ export class AppService {
(config.plugins.tpLinkSmartPlug = { enabled: true, smartPlugIP: '127.0.0.1' }),
".octodash should have required property 'previewProgressCircle'": config =>
(config.octodash.previewProgressCircle = false),
".octodash should have required property 'turnOnPrinterWhenExitingSleep'": config =>
(config.octodash.turnOnPrinterWhenExitingSleep = false),
".octodash should have required property 'turnOnPrinterWhenExitingSleep'": config => {
config.octodash.turnOnPrinterWhenExitingSleep = config.plugins.psuControl.turnOnPSUWhenExitingSleep ?? false;
delete config.plugins.psuControl.turnOnPSUWhenExitingSleep
}
};
}

// If the errors can be automatically fixed return true here
public autoFixErrors(errors: string[]): boolean {
// If all errors can be automatically fixed return true here
public fixUpdateErrors(errors: string[]): boolean {
const config = this.configService.getCurrentConfig();

let fullyAutofixed = true;
Expand Down
2 changes: 1 addition & 1 deletion src/app/config/config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface EnclosurePlugin extends Plugin {
interface PSUControlPlugin extends Plugin {
// TODO: this option still exists to allow migration path... need to be removed
// when the new `turnOnPSUWhenExitingSleep` will be released
turnOnPSUWhenExitingSleep: boolean;
turnOnPSUWhenExitingSleep?: boolean;
}

interface TPLinkSmartPlugPlugin extends Plugin {
Expand Down
3 changes: 2 additions & 1 deletion src/app/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class ConfigService {

public getErrors(): string[] {
const errors = [];
this.validator.errors.forEach((error): void => {
this.validator.errors?.forEach((error): void => {
if (error.keyword === 'type') {
errors.push(`${error.dataPath} ${error.message}`);
} else {
Expand All @@ -97,6 +97,7 @@ export class ConfigService {
const configStored = this.store.get('config');
if (this.validateGiven(configStored)) {
this.config = config;
this.valid = true;
this.generateHttpHeaders();
return null;
} else {
Expand Down

0 comments on commit 2fde959

Please sign in to comment.