From 6fac99b8b5bed8d8bc9ce06aa7c8ecdd6ff1ac3d Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Mon, 14 Oct 2019 15:24:38 +0200 Subject: [PATCH 1/5] Move services and CodeFactor improvements --- main.js | 3 +-- scripts/install-no-autostart.sh | 2 +- scripts/install.sh | 2 +- src/app/bottom-bar/bottom-bar.component.ts | 2 +- src/app/config/no-config/no-config.component.scss | 2 +- src/app/files/files.component.scss | 1 - src/app/layer-progress/layer-progress.component.ts | 2 +- src/app/{ => plugin-service}/enclosure.service.ts | 10 +++++----- .../layer-progress.service.ts | 0 src/app/printer-status/printer-status.component.ts | 2 +- src/assets/reset.css | 1 + src/styles.scss | 10 ---------- 12 files changed, 13 insertions(+), 24 deletions(-) rename src/app/{ => plugin-service}/enclosure.service.ts (84%) rename src/app/{layer-progress => plugin-service}/layer-progress.service.ts (100%) diff --git a/main.js b/main.js index 746e06837..34f0bde46 100644 --- a/main.js +++ b/main.js @@ -13,11 +13,10 @@ const dev = args.some(val => val === '--serve'); const big = args.some(val => val === '--big') let window; -let config; function createWindow() { config = store.get("config"); - store.onDidChange("config", (newValue, _) => { + store.onDidChange("config", (newValue) => { config = newValue }) const { diff --git a/scripts/install-no-autostart.sh b/scripts/install-no-autostart.sh index 1a6e9198b..416116ac9 100755 --- a/scripts/install-no-autostart.sh +++ b/scripts/install-no-autostart.sh @@ -11,7 +11,7 @@ elif [ -d "/home/pi/oprint" ]; then DIRECTORY="/home/pi/oprint" else echo "Neither /home/pi/OctoPrint/venv nor /home/pi/oprint can be found. Please specify OctoPrints full virtualenv path manually (no trailing slash):" - read DIRECTORY + read -r DIRECTORY fi; "$DIRECTORY"/bin/pip install -q "https://github.com/OllisGit/OctoPrint-DisplayLayerProgress/releases/latest/download/master.zip" diff --git a/scripts/install.sh b/scripts/install.sh index 2d74b3152..d746e5691 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -9,7 +9,7 @@ elif [ -d "/home/pi/oprint" ]; then DIRECTORY="/home/pi/oprint" else echo "Neither /home/pi/OctoPrint/venv nor /home/pi/oprint can be found. Please specify OctoPrints full virtualenv path manually (no trailing slash):" - read DIRECTORY + read -r DIRECTORY fi; "$DIRECTORY"/bin/pip install -q "https://github.com/OllisGit/OctoPrint-DisplayLayerProgress/releases/latest/download/master.zip" diff --git a/src/app/bottom-bar/bottom-bar.component.ts b/src/app/bottom-bar/bottom-bar.component.ts index 0fd0e25b6..869f33a95 100644 --- a/src/app/bottom-bar/bottom-bar.component.ts +++ b/src/app/bottom-bar/bottom-bar.component.ts @@ -2,7 +2,7 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; import { ConfigService } from '../config/config.service'; import { PrinterService, PrinterStatusAPI } from '../printer.service'; import { Subscription } from 'rxjs'; -import { EnclosureService } from '../enclosure.service'; +import { EnclosureService } from '../plugin-service/enclosure.service'; @Component({ selector: 'app-bottom-bar', diff --git a/src/app/config/no-config/no-config.component.scss b/src/app/config/no-config/no-config.component.scss index 2abe639eb..6b70bb3dc 100644 --- a/src/app/config/no-config/no-config.component.scss +++ b/src/app/config/no-config/no-config.component.scss @@ -11,7 +11,7 @@ height: 4vh; border-radius: 2vh; background-color: #44bd32; - width: 0vw; + width: 0; transition: width .7s ease-in-out; &-wrapper { diff --git a/src/app/files/files.component.scss b/src/app/files/files.component.scss index 9dc847110..d8351ef59 100755 --- a/src/app/files/files.component.scss +++ b/src/app/files/files.component.scss @@ -95,7 +95,6 @@ white-space: nowrap; text-overflow: ellipsis; display: block; - white-space: nowrap; } &__icon { diff --git a/src/app/layer-progress/layer-progress.component.ts b/src/app/layer-progress/layer-progress.component.ts index 66a7d2c02..0108c4970 100644 --- a/src/app/layer-progress/layer-progress.component.ts +++ b/src/app/layer-progress/layer-progress.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; -import { LayerProgressService, DisplayLayerProgressAPI } from './layer-progress.service'; +import { LayerProgressService, DisplayLayerProgressAPI } from '../plugin-service/layer-progress.service'; import { Subscription } from 'rxjs'; @Component({ diff --git a/src/app/enclosure.service.ts b/src/app/plugin-service/enclosure.service.ts similarity index 84% rename from src/app/enclosure.service.ts rename to src/app/plugin-service/enclosure.service.ts index d875a33a0..fd7ffa728 100644 --- a/src/app/enclosure.service.ts +++ b/src/app/plugin-service/enclosure.service.ts @@ -1,12 +1,12 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; -import { ConfigService } from './config/config.service'; -import { NotificationService } from './notification/notification.service'; +import { ConfigService } from '../config/config.service'; +import { NotificationService } from '../notification/notification.service'; import { Observable, Observer, timer, Subscription } from 'rxjs'; -import { OctoprintSettingsAPI } from './octoprint-api/settingsAPI'; -import { PrinterStatusAPI } from './printer.service'; +import { OctoprintSettingsAPI } from '../octoprint-api/settingsAPI'; +import { PrinterStatusAPI } from '../printer.service'; import { shareReplay } from 'rxjs/operators'; -import { TemperatureReading } from './bottom-bar/bottom-bar.component'; +import { TemperatureReading } from '../bottom-bar/bottom-bar.component'; @Injectable({ providedIn: 'root' diff --git a/src/app/layer-progress/layer-progress.service.ts b/src/app/plugin-service/layer-progress.service.ts similarity index 100% rename from src/app/layer-progress/layer-progress.service.ts rename to src/app/plugin-service/layer-progress.service.ts diff --git a/src/app/printer-status/printer-status.component.ts b/src/app/printer-status/printer-status.component.ts index a4bbc5cbd..c5236f18b 100644 --- a/src/app/printer-status/printer-status.component.ts +++ b/src/app/printer-status/printer-status.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; import { PrinterService, PrinterStatusAPI, PrinterValue } from '../printer.service'; -import { LayerProgressService, DisplayLayerProgressAPI } from '../layer-progress/layer-progress.service'; +import { LayerProgressService, DisplayLayerProgressAPI } from '../plugin-service/layer-progress.service'; import { Subscription } from 'rxjs'; @Component({ diff --git a/src/assets/reset.css b/src/assets/reset.css index dd42700c4..1624a9ffb 100644 --- a/src/assets/reset.css +++ b/src/assets/reset.css @@ -137,6 +137,7 @@ table { input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; + -moz-appearance: none; margin: 0; } diff --git a/src/styles.scss b/src/styles.scss index afbec47ce..c1393dc1a 100755 --- a/src/styles.scss +++ b/src/styles.scss @@ -95,16 +95,6 @@ app-root { background-color: #353b48; padding: 3.5vh 2vw; } -} - -.help { - position: fixed; - left: 0; - top: 0; - right: 0; - bottom: 0; - background-color: rgba(0, 0, 0, .85); - z-index: 1000; &__close { display: block; From f43de835887f312cd99c4d3f696df37e430cc909 Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Mon, 14 Oct 2019 17:58:04 +0200 Subject: [PATCH 2/5] Design for standby screen --- icons.txt | 3 +- src/app/app.module.ts | 4 +- src/app/app.routing.module.ts | 9 +++- .../notification/notification.component.html | 2 +- .../notification/notification.component.ts | 24 +++++++--- src/app/notification/notification.service.ts | 5 +- src/app/octoprint-api/connectionAPI.ts | 21 +++++++++ src/app/printer.service.ts | 47 ++++++++++++++----- src/app/standby/standby.component.html | 8 ++++ src/app/standby/standby.component.scss | 44 +++++++++++++++++ src/app/standby/standby.component.ts | 17 +++++++ src/assets/sleeping.svg | 1 + 12 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 src/app/octoprint-api/connectionAPI.ts create mode 100644 src/app/standby/standby.component.html create mode 100644 src/app/standby/standby.component.scss create mode 100644 src/app/standby/standby.component.ts create mode 100644 src/assets/sleeping.svg diff --git a/icons.txt b/icons.txt index 5372869c4..5e13654d0 100644 --- a/icons.txt +++ b/icons.txt @@ -21,4 +21,5 @@ "print" by Delta from the Noun Project "Delete" by Daily icons from the Noun Project "discard" by Alice Design from the Noun Project -"Heat" by Adrien Coquet from the Noun Project \ No newline at end of file +"Heat" by Adrien Coquet from the Noun Project +"sleeping" by AomAm from the Noun Project \ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 03f254b81..2d2b92db0 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -37,6 +37,7 @@ import { NgxSpinnerModule } from 'ngx-spinner'; import * as Hammer from 'hammerjs'; import { SettingsComponent } from './settings/settings.component'; +import { StandbyComponent } from './standby/standby.component'; export class MyHammerConfig extends HammerGestureConfig { overrides = { @@ -63,7 +64,8 @@ export class MyHammerConfig extends HammerGestureConfig { FilamentComponent, FilesComponent, SettingsComponent, - URLSafePipe + URLSafePipe, + StandbyComponent ], imports: [ BrowserModule, diff --git a/src/app/app.routing.module.ts b/src/app/app.routing.module.ts index e9a4e957a..ede9b9e5f 100644 --- a/src/app/app.routing.module.ts +++ b/src/app/app.routing.module.ts @@ -9,13 +9,14 @@ import { MainScreenNoTouchComponent } from './main-screen/no-touch/main-screen-n import { FilamentComponent } from './filament/filament.component'; import { FilesComponent } from './files/files.component'; import { SettingsComponent } from './settings/settings.component'; +import { StandbyComponent } from './standby/standby.component'; const routes: Routes = [ { path: 'main-screen', - component: MainScreenComponent - // component: FilesComponent + // component: MainScreenComponent + component: StandbyComponent }, { path: 'main-screen-no-touch', @@ -44,6 +45,10 @@ const routes: Routes = [ { path: 'settings', component: SettingsComponent + }, + { + path: 'standby', + component: StandbyComponent } ]; diff --git a/src/app/notification/notification.component.html b/src/app/notification/notification.component.html index 1181738f1..5805f5f4b 100644 --- a/src/app/notification/notification.component.html +++ b/src/app/notification/notification.component.html @@ -2,5 +2,5 @@ (click)="hideNotification()"> {{ notification.heading }} {{ notification.text }} - Tap this card to close it ... + tap this card to close it ... diff --git a/src/app/notification/notification.component.ts b/src/app/notification/notification.component.ts index 77e91fd57..cbde44f73 100644 --- a/src/app/notification/notification.component.ts +++ b/src/app/notification/notification.component.ts @@ -1,6 +1,8 @@ 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'; @Component({ selector: 'app-notification', @@ -18,20 +20,30 @@ export class NotificationComponent implements OnDestroy { }; public show = false; - constructor(private notificationService: NotificationService) { + public constructor(private notificationService: NotificationService, private printerService: PrinterService, private router: Router) { this.subscriptions.add(this.notificationService.getObservable().subscribe((message: Message) => this.setMessage(message))); } - hideNotification() { + public hideNotification() { this.show = false; } - setMessage(message: Message) { - this.notification = message; - this.show = true; + 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; + } } - ngOnDestroy() { + public ngOnDestroy() { this.subscriptions.unsubscribe(); } diff --git a/src/app/notification/notification.service.ts b/src/app/notification/notification.service.ts index 7c47ce3d6..a5608c691 100644 --- a/src/app/notification/notification.service.ts +++ b/src/app/notification/notification.service.ts @@ -16,8 +16,8 @@ export class NotificationService { }).pipe(shareReplay(1)); } - setError(heading: string, text: string) { - this.observer.next({ heading, text, type: 'error' }); + setError(heading: string, text: string, printerStatusError = false) { + this.observer.next({ heading, text, type: 'error', printerStatusError }); } setUpdate(heading: string, text: string) { @@ -33,4 +33,5 @@ export interface Message { heading: string; text: string; type: string; + printerStatusError?: boolean; } diff --git a/src/app/octoprint-api/connectionAPI.ts b/src/app/octoprint-api/connectionAPI.ts new file mode 100644 index 000000000..2379070bc --- /dev/null +++ b/src/app/octoprint-api/connectionAPI.ts @@ -0,0 +1,21 @@ +export interface OctoprintConnectionAPI { + current: OctoprintConnectionCurrentAPI; + options: OctoprintConnectionOptionsAPI; +} + +interface OctoprintConnectionCurrentAPI { + state: string; + port: string; + baudrate: number; + printerProfile: string; +} + +interface OctoprintConnectionOptionsAPI { + ports: Array; + baudrates: Array; + printerProfiles: Array; + portPreference: string; + baudratePreference: string; + printerProfilePreference: string; + autoconnect: boolean; +} diff --git a/src/app/printer.service.ts b/src/app/printer.service.ts index 931367884..44828c199 100644 --- a/src/app/printer.service.ts +++ b/src/app/printer.service.ts @@ -5,6 +5,7 @@ import { Observable, Observer, timer, Subscription } from 'rxjs'; import { shareReplay } from 'rxjs/operators'; import { OctoprintPrinterStatusAPI } from './octoprint-api/printerStatusAPI'; import { NotificationService } from './notification/notification.service'; +import { OctoprintConnectionAPI } from './octoprint-api/connectionAPI'; @Injectable({ providedIn: 'root' @@ -36,19 +37,23 @@ export class PrinterService { }; observer.next(printerStatus); }, (error: HttpErrorResponse) => { - const printerStatus: PrinterStatusAPI = { - status: `error (${error.status})`, - nozzle: { - current: 0, - set: 0 - }, - heatbed: { - current: 0, - set: 0 - } - }; - observer.next(printerStatus); - this.notificationService.setError('Can\'t retrieve printer status!', error.message); + if (error.status === 409) { + this.notificationService.setError(null, null, true); + } else { + const printerStatus: PrinterStatusAPI = { + status: `error (${error.status})`, + nozzle: { + current: 0, + set: 0 + }, + heatbed: { + current: 0, + set: 0 + } + }; + observer.next(printerStatus); + this.notificationService.setError('Can\'t retrieve printer status!', error.message); + } }); }); }).pipe(shareReplay(1)); @@ -88,6 +93,22 @@ export class PrinterService { } ); } + + public isPrinterOffline(): Promise { + return new Promise((resolve) => { + this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders()) + .subscribe( + (data: OctoprintConnectionAPI) => { + // TODO: check for disconnected + resolve(data.current.state !== 'Operational'); + }, + (error: HttpErrorResponse) => { + this.notificationService.setError('Can\'t retrieve connection state!', error.message); + resolve(false); + } + ); + }); + } } export interface PrinterStatusAPI { diff --git a/src/app/standby/standby.component.html b/src/app/standby/standby.component.html new file mode 100644 index 000000000..629b606e5 --- /dev/null +++ b/src/app/standby/standby.component.html @@ -0,0 +1,8 @@ + +Shhh! OctoDash is sleeping. +Press on the screen to wake me up again. + +
+ {{ error }} + tap this card to close it ... +
diff --git a/src/app/standby/standby.component.scss b/src/app/standby/standby.component.scss new file mode 100644 index 000000000..fb57b6223 --- /dev/null +++ b/src/app/standby/standby.component.scss @@ -0,0 +1,44 @@ +.standby { + &__icon { + height: 45vh; + display: block; + margin: 11vh auto; + } + + &__text { + &-big { + display: block; + text-align: center; + margin-bottom: 5vh; + font-size: 4vw; + } + + &-small { + display: block; + text-align: center; + margin-bottom: 5vh; + font-size: 2vw; + } + } + + &__error { + position: fixed; + top: 20vh; + left: 20vw; + right: 20vw; + text-align: center; + background-color: black; + opacity: .92; + border-radius: 2vw; + padding: 5vh 3vw; + font-size: 3vw; + z-index: 100; + + &-close { + font-size: 2vw; + opacity: .8; + display: block; + margin-top: 4vh; + } + } +} diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts new file mode 100644 index 000000000..c12c906b4 --- /dev/null +++ b/src/app/standby/standby.component.ts @@ -0,0 +1,17 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-standby', + templateUrl: './standby.component.html', + styleUrls: ['./standby.component.scss'] +}) +export class StandbyComponent implements OnInit { + + error: string = "OctoPrint can't connect to your printer. Please make sure that the connection works, then come back and try again." + + constructor() { } + + ngOnInit() { + + } +} diff --git a/src/assets/sleeping.svg b/src/assets/sleeping.svg new file mode 100644 index 000000000..100bdaedc --- /dev/null +++ b/src/assets/sleeping.svg @@ -0,0 +1 @@ + \ No newline at end of file From d619c3189124f3e04310455c3eedd925272cf93b Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Tue, 15 Oct 2019 21:00:12 +0200 Subject: [PATCH 3/5] Standby working for now --- src/app/app.routing.module.ts | 4 +- .../notification/notification.component.ts | 3 ++ src/app/printer.service.ts | 5 +-- src/app/standby/standby.component.html | 12 ++++-- src/app/standby/standby.component.ts | 43 ++++++++++++++++++- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/app/app.routing.module.ts b/src/app/app.routing.module.ts index ede9b9e5f..1a66a0491 100644 --- a/src/app/app.routing.module.ts +++ b/src/app/app.routing.module.ts @@ -15,8 +15,8 @@ import { StandbyComponent } from './standby/standby.component'; const routes: Routes = [ { path: 'main-screen', - // component: MainScreenComponent - component: StandbyComponent + component: MainScreenComponent + // component: StandbyComponent }, { path: 'main-screen-no-touch', diff --git a/src/app/notification/notification.component.ts b/src/app/notification/notification.component.ts index cbde44f73..39e95ecac 100644 --- a/src/app/notification/notification.component.ts +++ b/src/app/notification/notification.component.ts @@ -35,6 +35,9 @@ export class NotificationComponent implements OnDestroy { this.hideNotification(); console.clear(); this.router.navigate(['/standby']); + } else { + this.notification = message; + this.show = true; } }); } else { diff --git a/src/app/printer.service.ts b/src/app/printer.service.ts index 44828c199..bca10e975 100644 --- a/src/app/printer.service.ts +++ b/src/app/printer.service.ts @@ -38,7 +38,7 @@ export class PrinterService { observer.next(printerStatus); }, (error: HttpErrorResponse) => { if (error.status === 409) { - this.notificationService.setError(null, null, true); + this.notificationService.setError('Can\'t retrieve printer status!', error.message, true); } else { const printerStatus: PrinterStatusAPI = { status: `error (${error.status})`, @@ -99,8 +99,7 @@ export class PrinterService { this.http.get(this.configService.getURL('connection'), this.configService.getHTTPHeaders()) .subscribe( (data: OctoprintConnectionAPI) => { - // TODO: check for disconnected - resolve(data.current.state !== 'Operational'); + resolve(data.current.state === 'Closed'); }, (error: HttpErrorResponse) => { this.notificationService.setError('Can\'t retrieve connection state!', error.message); diff --git a/src/app/standby/standby.component.html b/src/app/standby/standby.component.html index 629b606e5..e31923a18 100644 --- a/src/app/standby/standby.component.html +++ b/src/app/standby/standby.component.html @@ -1,8 +1,14 @@ - -Shhh! OctoDash is sleeping. -Press on the screen to wake me up again. +
+ + Shhh! OctoDash is sleeping. + Press on the screen to wake me up again. +
{{ error }} tap this card to close it ...
+ +
+ connecting ... +
diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts index c12c906b4..77edd4c2b 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -1,4 +1,8 @@ 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'; @Component({ selector: 'app-standby', @@ -7,11 +11,46 @@ import { Component, OnInit } from '@angular/core'; }) export class StandbyComponent implements OnInit { - error: string = "OctoPrint can't connect to your printer. Please make sure that the connection works, then come back and try again." + connecting = false; + error = ''; + httpPOSTRequest: Subscription; - constructor() { } + constructor(private configService: ConfigService, private http: HttpClient, private router: Router) { } ngOnInit() { + } + reconnect() { + this.connecting = true; + if (this.httpPOSTRequest) { + this.httpPOSTRequest.unsubscribe(); + } + 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; + 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.'; + } + ); } } + +interface ConnectCommand { + command: string; + port?: string; + baudrate?: number; + printerProfile?: string; + save?: boolean; + autoconnect?: boolean; +} From f7fb637678d9715420d82ad97828e95800122ec0 Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Tue, 15 Oct 2019 21:18:50 +0200 Subject: [PATCH 4/5] Add PSUControl plugin --- src/app/control/control.component.ts | 6 ++- .../plugin-service/layer-progress.service.ts | 8 ++-- src/app/plugin-service/psu-control.service.ts | 46 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/app/plugin-service/psu-control.service.ts diff --git a/src/app/control/control.component.ts b/src/app/control/control.component.ts index f9aea9ed5..18c33d92a 100644 --- a/src/app/control/control.component.ts +++ b/src/app/control/control.component.ts @@ -3,6 +3,7 @@ import { PrinterService } from '../printer.service'; import { ConfigService } from '../config/config.service'; import { OctoprintService } from '../octoprint.service'; import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; +import { PsuControlService } from '../plugin-service/psu-control.service'; @Component({ selector: 'app-control', @@ -19,7 +20,7 @@ export class ControlComponent { private printerService: PrinterService, private octoprintService: OctoprintService, private configService: ConfigService, - private domSanitizer: DomSanitizer) { + private psuControlService: PsuControlService) { this.customActions = this.configService.getCustomActions(); } @@ -44,6 +45,9 @@ export class ControlComponent { case '[!REBOOT]': this.rebootPi(); break; case '[!SHUTDOWN]': this.shutdownPi(); break; case '[!KILL]': this.kill(); break; + case '[!POWEROFF]': this.psuControlService.changePSUState(false); break; + case '[!POWERON]': this.psuControlService.changePSUState(true); break; + case '[!POWERTOGGLE]': this.psuControlService.togglePSU(); break; default: { if (command.includes('[!WEB]')) { this.openIFrame(command.replace('[!WEB]', '')); diff --git a/src/app/plugin-service/layer-progress.service.ts b/src/app/plugin-service/layer-progress.service.ts index 4b5a0a052..024c6d1e5 100644 --- a/src/app/plugin-service/layer-progress.service.ts +++ b/src/app/plugin-service/layer-progress.service.ts @@ -11,16 +11,16 @@ import { NotificationService } from '../notification/notification.service'; }) export class LayerProgressService { - httpRequest: Subscription; + httpGETRequest: Subscription; observable: Observable; constructor(private configService: ConfigService, private notificationService: NotificationService, private http: HttpClient) { this.observable = new Observable((observer: Observer) => { timer(1000, this.configService.getAPIInterval()).subscribe(_ => { - if (this.httpRequest) { - this.httpRequest.unsubscribe(); + if (this.httpGETRequest) { + this.httpGETRequest.unsubscribe(); } - this.httpRequest = this.http.get(this.configService.getURL('plugin/DisplayLayerProgress/values').replace('/api', ''), + this.httpGETRequest = this.http.get(this.configService.getURL('plugin/DisplayLayerProgress/values').replace('/api', ''), this.configService.getHTTPHeaders()).subscribe( (data: OctoprintLayerProgressAPI) => { observer.next({ diff --git a/src/app/plugin-service/psu-control.service.ts b/src/app/plugin-service/psu-control.service.ts new file mode 100644 index 000000000..ed5031750 --- /dev/null +++ b/src/app/plugin-service/psu-control.service.ts @@ -0,0 +1,46 @@ +import { Injectable } from '@angular/core'; +import { Subscription } from 'rxjs'; +import { ConfigService } from '../config/config.service'; +import { NotificationService } from '../notification/notification.service'; +import { HttpClient, HttpErrorResponse } from '@angular/common/http'; +import { JobCommand } from '../octoprint-api/jobAPI'; + +@Injectable({ + providedIn: 'root' +}) +export class PsuControlService { + + httpPOSTRequest: Subscription; + + constructor(private configService: ConfigService, private notificationService: NotificationService, private http: HttpClient) { } + + public changePSUState(on: boolean) { + if (this.httpPOSTRequest) { + this.httpPOSTRequest.unsubscribe(); + } + const psuPayload: JobCommand = { + command: on ? 'turnPSUOn' : 'turnPSUOff' + }; + this.httpPOSTRequest = this.http.post(this.configService.getURL('plugin/psucontrol'), psuPayload, this.configService.getHTTPHeaders()) + .subscribe( + () => null, (error: HttpErrorResponse) => { + this.notificationService.setError('Can\'t control PSU!', error.message); + } + ); + } + + public togglePSU() { + if (this.httpPOSTRequest) { + this.httpPOSTRequest.unsubscribe(); + } + const psuPayload: JobCommand = { + command: 'togglePSU' + }; + this.httpPOSTRequest = this.http.post(this.configService.getURL('plugin/psucontrol'), psuPayload, this.configService.getHTTPHeaders()) + .subscribe( + () => null, (error: HttpErrorResponse) => { + this.notificationService.setError('Can\'t control PSU!', error.message); + } + ); + } +} From 87469ac996f936dd705ce15945cac730a3d4dbe2 Mon Sep 17 00:00:00 2001 From: UnchartedBull Date: Tue, 15 Oct 2019 22:08:42 +0200 Subject: [PATCH 5/5] Enable callback to electron --- main.js | 16 ++++++++++- src/app/app.component.ts | 3 +- src/app/app.service.ts | 28 ++++++++++--------- src/app/config/config.service.ts | 17 +++++++---- .../config/no-config/no-config.component.ts | 3 +- src/app/standby/standby.component.ts | 9 +++++- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index 34f0bde46..da7244c60 100644 --- a/main.js +++ b/main.js @@ -6,7 +6,10 @@ const url = require('url') const path = require('path') const Store = require('electron-store'); const store = new Store(); - +const exec = require('child_process').exec; +const { + ipcMain +} = require('electron') const args = process.argv.slice(1); const dev = args.some(val => val === '--serve'); @@ -56,11 +59,22 @@ function createWindow() { } setTimeout(sendVersionInfo, 42 * 1000); + activateSleepListener(); window.on('closed', () => { window = null; }); } +function activateSleepListener() { + ipcMain.on("screenSleep", () => { + exec('xset dpms force standby') + }) + + ipcMain.on("screenWakeup", () => { + exec('xset -dpms') + }) +} + function sendVersionInfo() { window.webContents.send("versionInformation", { version: process.env.npm_package_version diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c8f567216..951586f72 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -26,8 +26,7 @@ export class AppComponent { this.router.navigate(['/main-screen-no-touch']); } } else { - // WARNING: remove later - if (_.isEqual(this.configService.getErrors(), this.service.getUpdateError()) || _.isEqual(this.configService.getErrors(), ['.octodash.temperatureSensor should be object'])) { + if (_.isEqual(this.configService.getErrors(), this.service.getUpdateError())) { if (this.service.autoFixError()) { this.initialize(); } else { diff --git a/src/app/app.service.ts b/src/app/app.service.ts index 61ff87b79..914025af6 100644 --- a/src/app/app.service.ts +++ b/src/app/app.service.ts @@ -27,9 +27,7 @@ export class AppService { } this.updateError = [ - '.octodash.temperatureSensor should have required property \'ambient\'', - '.octodash.temperatureSensor should have required property \'filament1\'', - '.octodash.temperatureSensor should have required property \'filament2\'', + '.octodash should have required property \'turnScreenOffSleep\'' ]; } @@ -46,6 +44,18 @@ export class AppService { setTimeout(this.checkUpdate.bind(this), 21.6 * 1000000); } + public turnDisplayOff(): void { + if (this.ipc) { + this.ipc.send('screenSleep', ''); + } + } + + public turnDisplayOn(): void { + if (this.ipc) { + this.ipc.send('screenWakeup', ''); + } + } + public getUpdateError(): string[] { return this.updateError; } @@ -86,18 +96,10 @@ export class AppService { // If the errors can be automatically fixed return true here public autoFixError(): boolean { const config = this.configService.config; - if (config.octodash.temperatureSensor !== null) { - delete config.octodash.temperatureSensor.gpio; - delete config.octodash.temperatureSensor.type; - } - config.octodash.temperatureSensor = { - ambient: null, - filament1: null, - filament2: null - }; + config.octodash.turnScreenOffSleep = false; this.configService.saveConfig(config); this.configService.updateConfig(); - return false; + return true; } } diff --git a/src/app/config/config.service.ts b/src/app/config/config.service.ts index 06b4415b5..db47b35cd 100644 --- a/src/app/config/config.service.ts +++ b/src/app/config/config.service.ts @@ -150,6 +150,10 @@ export class ConfigService { public getAmbientTemperatureSensorName(): string { return this.config.octodash.temperatureSensor.ambient; } + + public getAutomaticScreenSleep(): boolean { + return this.config.octodash.turnScreenOffSleep; + } } export interface Config { @@ -163,6 +167,7 @@ interface OctoDash { touchscreen: boolean; temperatureSensor: TemperatureSensor | null; customActions: CustomAction[]; + turnScreenOffSleep: boolean; } interface CustomAction { @@ -175,9 +180,6 @@ interface TemperatureSensor { ambient: string | null; filament1: string | null; filament2: string | null; - // deprecate in next version - type?: number; - gpio?: number; } interface Octoprint { @@ -282,7 +284,8 @@ const schema = { required: [ 'touchscreen', 'temperatureSensor', - 'customActions' + 'customActions', + 'turnScreenOffSleep' ], properties: { touchscreen: { @@ -344,7 +347,11 @@ const schema = { } } } - } + }, + turnScreenOffSleep: { + $id: '#/properties/octodash/properties/turnScreenOffSleep', + type: 'boolean' + }, } } } diff --git a/src/app/config/no-config/no-config.component.ts b/src/app/config/no-config/no-config.component.ts index 76988c6df..910b96249 100644 --- a/src/app/config/no-config/no-config.component.ts +++ b/src/app/config/no-config/no-config.component.ts @@ -83,7 +83,8 @@ export class NoConfigComponent implements OnInit { command: '[!KILL]', color: '#e84118' } - ] + ], + turnScreenOffSleep: false } }; } diff --git a/src/app/standby/standby.component.ts b/src/app/standby/standby.component.ts index 77edd4c2b..19d24dedc 100644 --- a/src/app/standby/standby.component.ts +++ b/src/app/standby/standby.component.ts @@ -3,6 +3,7 @@ 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'; @Component({ selector: 'app-standby', @@ -15,9 +16,12 @@ export class StandbyComponent implements OnInit { error = ''; httpPOSTRequest: Subscription; - constructor(private configService: ConfigService, private http: HttpClient, private router: Router) { } + constructor(private configService: ConfigService, private http: HttpClient, private router: Router, private service: AppService) { } ngOnInit() { + if (this.configService.getAutomaticScreenSleep()) { + setTimeout(this.service.turnDisplayOff.bind(this.service), 300000); + } } reconnect() { @@ -34,6 +38,9 @@ export class StandbyComponent implements OnInit { () => { setTimeout(() => { this.connecting = false; + if (this.configService.getAutomaticScreenSleep()) { + this.service.turnDisplayOn(); + } this.router.navigate(['/main-screen']); }, 4000); },