Skip to content

Commit

Permalink
Enable standby mode (#213)
Browse files Browse the repository at this point in the history
* Move services and CodeFactor improvements

* Design for standby screen

* Standby working for now

* Add PSUControl plugin

* Enable callback to electron
  • Loading branch information
UnchartedBull committed Oct 15, 2019
1 parent 1ffd04e commit 725115f
Show file tree
Hide file tree
Showing 20 changed files with 312 additions and 52 deletions.
3 changes: 2 additions & 1 deletion icons.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
"Heat" by Adrien Coquet from the Noun Project
"sleeping" by AomAm from the Noun Project
16 changes: 15 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -63,7 +64,8 @@ export class MyHammerConfig extends HammerGestureConfig {
FilamentComponent,
FilesComponent,
SettingsComponent,
URLSafePipe
URLSafePipe,
StandbyComponent
],
imports: [
BrowserModule,
Expand Down
7 changes: 6 additions & 1 deletion src/app/app.routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: StandbyComponent
},
{
path: 'main-screen-no-touch',
Expand Down Expand Up @@ -44,6 +45,10 @@ const routes: Routes = [
{
path: 'settings',
component: SettingsComponent
},
{
path: 'standby',
component: StandbyComponent
}
];

Expand Down
28 changes: 15 additions & 13 deletions src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\''
];
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

}
Expand Down
17 changes: 12 additions & 5 deletions src/app/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -163,6 +167,7 @@ interface OctoDash {
touchscreen: boolean;
temperatureSensor: TemperatureSensor | null;
customActions: CustomAction[];
turnScreenOffSleep: boolean;
}

interface CustomAction {
Expand All @@ -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 {
Expand Down Expand Up @@ -282,7 +284,8 @@ const schema = {
required: [
'touchscreen',
'temperatureSensor',
'customActions'
'customActions',
'turnScreenOffSleep'
],
properties: {
touchscreen: {
Expand Down Expand Up @@ -344,7 +347,11 @@ const schema = {
}
}
}
}
},
turnScreenOffSleep: {
$id: '#/properties/octodash/properties/turnScreenOffSleep',
type: 'boolean'
},
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/config/no-config/no-config.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export class NoConfigComponent implements OnInit {
command: '[!KILL]',
color: '#e84118'
}
]
],
turnScreenOffSleep: false
}
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/app/control/control.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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();
}

Expand All @@ -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]', ''));
Expand Down
2 changes: 1 addition & 1 deletion src/app/notification/notification.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
(click)="hideNotification()">
<span class="notification__heading"> {{ notification.heading }} </span>
<span class="notification__text"> {{ notification.text }} </span>
<span class="notification__close">Tap this card to close it ...</span>
<span class="notification__close">tap this card to close it ...</span>
</div>
27 changes: 21 additions & 6 deletions src/app/notification/notification.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -18,20 +20,33 @@ 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;
}
});
} else {
this.notification = message;
this.show = true;
}
}

ngOnDestroy() {
public ngOnDestroy() {
this.subscriptions.unsubscribe();
}

Expand Down
5 changes: 3 additions & 2 deletions src/app/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,4 +33,5 @@ export interface Message {
heading: string;
text: string;
type: string;
printerStatusError?: boolean;
}
21 changes: 21 additions & 0 deletions src/app/octoprint-api/connectionAPI.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
baudrates: Array<number>;
printerProfiles: Array<object>;
portPreference: string;
baudratePreference: string;
printerProfilePreference: string;
autoconnect: boolean;
}
8 changes: 4 additions & 4 deletions src/app/plugin-service/layer-progress.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { NotificationService } from '../notification/notification.service';
})
export class LayerProgressService {

httpRequest: Subscription;
httpGETRequest: Subscription;
observable: Observable<DisplayLayerProgressAPI>;

constructor(private configService: ConfigService, private notificationService: NotificationService, private http: HttpClient) {
this.observable = new Observable((observer: Observer<any>) => {
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({
Expand Down
Loading

0 comments on commit 725115f

Please sign in to comment.