Skip to content

Commit

Permalink
child bridge button depends on existance
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 committed Nov 9, 2023
1 parent c101948 commit 2c552aa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ <h3 class="text-center primary-text" [translate]="'platform.version.title_servic
</p>
<div class="text-center">
<button type="button" class="btn btn-primary"
*ngIf="!['homebridge', 'homebridge-config-ui-x'].includes(pluginName)"
(click)="onRestartChildBridgeClick(item._bridge?.username)"
*ngIf="!['homebridge', 'homebridge-config-ui-x'].includes(pluginName) && hasChildBridges"
(click)="restartChildBridges()"
[translate]="'plugins.manage.child_bridge_button_restart_now'">Restart Child Bridges</button>
<button type="button" class="btn btn-primary" (click)="onRestartHomebridgeClick()"
[translate]="'plugins.manage.button_restart_now'">Restart Homebridge</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import { NotificationService } from '@/app/core/notification.service';
templateUrl: './manage-plugins-modal.component.html',
styleUrls: ['./manage-plugins-modal.component.scss'],
})

export class ManagePluginsModalComponent implements OnInit, OnDestroy {
@Input() pluginName;
@Input() pluginName: string;
@Input() targetVersion = 'latest';
@Input() action;
@Input() action: string;

private io = this.$ws.connectToNamespace('plugins');

Expand All @@ -33,7 +34,8 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
public justUpdatedPlugin = false;
public updateToBeta = false;
public changeLog: string;
public release;
public release: any;
public hasChildBridges = false;

private toastSuccess: string;
public presentTenseVerb: string;
Expand All @@ -45,7 +47,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
public activeModal: NgbActiveModal,
public $toastr: ToastrService,
private $translate: TranslateService,
private $settings: SettingsService,
public $settings: SettingsService,
private $api: ApiService,
private $ws: WsService,
private $notification: NotificationService,
Expand All @@ -59,13 +61,13 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
this.term.open(this.termTarget);
this.fitAddon.fit();

this.io.socket.on('stdout', (data) => {
this.io.socket.on('stdout', (data: string | Uint8Array) => {
this.term.write(data);
});

this.toastSuccess = this.$translate.instant('toast.title_success');

this.onlineUpdateOk = !(['homebridge', 'homebridge-config-ui-x'].includes(this.pluginName) && this.$settings.env.platform === 'win32');
this.hasChildBridges = this.getChildBridgeMetadata().length > 0;

switch (this.action) {
case 'Install':
Expand Down Expand Up @@ -112,7 +114,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
termCols: this.term.cols,
termRows: this.term.rows,
}).subscribe(
(data) => {
() => {
this.$router.navigate(['/plugins'], {
queryParams: { installed: this.pluginName },
});
Expand All @@ -133,7 +135,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
termCols: this.term.cols,
termRows: this.term.rows,
}).subscribe(
(data) => {
() => {
this.activeModal.close();
this.$router.navigate(['/plugins']);
this.$toastr.success(`${this.pastTenseVerb} ${this.pluginName}`, this.toastSuccess);
Expand Down Expand Up @@ -164,7 +166,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
termCols: this.term.cols,
termRows: this.term.rows,
}).subscribe(
(data) => {
() => {
this.justUpdatedPlugin = true;
this.$router.navigate(['/plugins']);
this.$toastr.success(`${this.pastTenseVerb} ${this.pluginName}`, this.toastSuccess);
Expand All @@ -184,7 +186,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
termCols: this.term.cols,
termRows: this.term.rows,
}).subscribe(
(data) => {
() => {
this.$router.navigate(['/restart']);
this.activeModal.close();
this.$toastr.success(this.pastTenseVerb, this.toastSuccess);
Expand All @@ -206,7 +208,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
this.activeModal.close();
}
},
(err) => {
() => {
this.activeModal.close();
},
);
Expand All @@ -218,7 +220,7 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
this.showReleaseNotes = true;
this.release = data;
},
(err) => {
() => {
if (this.onlineUpdateOk) {
this.update();
}
Expand All @@ -231,42 +233,35 @@ export class ManagePluginsModalComponent implements OnInit, OnDestroy {
this.activeModal.close();
}


public getChildBridgeMetadata(): any[] {
const childBridges: any[] = [];
this.$api.get('/plugins').subscribe((data: any[]) => {
data.forEach((plugin) => {
if (plugin._bridge) {
childBridges.push({
pluginName: plugin.name,
username: plugin._bridge.username,
restartInProgress: false,
});
}
});
const io = this.$ws.connectToNamespace('child-bridges');
const childBridges = [];
io.request('get-homebridge-child-bridge-status').subscribe((data) => {
childBridges.push(...data);
});
return childBridges;
io.end();

return childBridges.filter((x) => x.pluginName === this.pluginName);
}

public async onRestartChildBridgeClick() {
const childBridges = this.getChildBridgeMetadata();
const bridge = childBridges.find((x) => x.pluginName === this.pluginName);
bridge.restartInProgress = true;
try {
await this.io.request('restart-child-bridge', bridge.username).toPromise();
} catch (err) {
this.$toastr.error(
'Failed to restart bridge: ' + err.error?.message,
this.$translate.instant('toast.title_error'),
);
bridge.restartInProgress = false;
} finally {
this.activeModal.close();
}
public async restartChildBridges() {
// const childBridges = this.getChildBridgeMetadata();
// const bridge = childBridges.find((x) => x.pluginName === this.pluginName);
// bridge.restartInProgress = true;
// try {
// await this.io.request('restart-child-bridge', bridge.username).toPromise();
// } catch (err) {
// this.$toastr.error(
// 'Failed to restart bridge: ' + err.error?.message,
// this.$translate.instant('toast.title_error'),
// );
// bridge.restartInProgress = false;
// } finally {
// this.activeModal.close();
// }
}

ngOnDestroy() {
this.io.end();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ <h5 class="m-0">{{ block.name || pluginAlias }}</h5>
</div>
<div class="modal-footer justify-content-between">
<div style="min-width: 25%;" class="text-left">
<button type="button" class="btn btn-elegant mr-auto" data-dismiss="modal" (click)="addBlock()"
*ngIf="canConfigure">
<i class="fa fa-fw fa-plus"></i> Add {{ pluginType }}
</button>
<button type="button" class="btn btn-elegant" data-dismiss="modal" (click)="activeModal.close()"
[translate]="'form.button_close'">Close</button>
</div>

<div style="min-width: 25%;" class="d-none d-lg-inline text-center">
Expand All @@ -79,8 +77,11 @@ <h5 class="m-0">{{ block.name || pluginAlias }}</h5>
</div>

<div style="min-width: 25%;" class="text-right">
<button type="button" class="btn btn-elegant" data-dismiss="modal" (click)="activeModal.close()"
[translate]="'form.button_close'">Close</button>
<button type="button" class="btn btn-elegant mr-auto" data-dismiss="modal" (click)="addBlock()"
*ngIf="canConfigure">
Add {{ pluginType }}
</button>

<button type="button" class="btn btn-primary" data-dismiss="modal" (click)="save()" [disabled]="saveInProgress"
*ngIf="canConfigure">
{{ 'form.button_save' | translate }} <i *ngIf="saveInProgress" class="fas fa-fw fa-spinner fa-pulse"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { NotificationService } from '@/app/core/notification.service';
styleUrls: ['./manual-plugin-config-modal.component.scss'],
})
export class ManualPluginConfigModalComponent implements OnInit {
@Input() plugin;
@Input() plugin: any;

public pluginAlias: string;
public pluginType: 'platform' | 'accessory';
Expand All @@ -30,7 +30,7 @@ export class ManualPluginConfigModalComponent implements OnInit {
public currentBlockIndex: number | null = null;
public saveInProgress = false;

public monacoEditor;
public monacoEditor: any;
public editorOptions = {
language: 'json',
theme: this.$settings.theme.startsWith('dark-mode') ? 'vs-dark' : 'vs-light',
Expand Down Expand Up @@ -62,7 +62,7 @@ export class ManualPluginConfigModalComponent implements OnInit {
return this.pluginType === 'accessory' ? 'accessories' : 'platforms';
}

async onEditorInit(editor) {
async onEditorInit(editor: any) {
this.monacoEditor = editor;
window['editor'] = editor;
await this.monacoEditor.getModel().setValue(this.currentBlock);
Expand All @@ -80,7 +80,7 @@ export class ManualPluginConfigModalComponent implements OnInit {
this.loading = false;
}
},
(err) => {
() => {
this.loading = false;
},
);
Expand All @@ -103,9 +103,11 @@ export class ManualPluginConfigModalComponent implements OnInit {
);
}

// configBlocks(): Array<any> {
// return this.pluginConfig;
// }
blockChanged() {
for (const block of this.pluginConfig) {
block.name = block.config.name || block.name;
}
}

addBlock() {
if (!this.saveCurrentBlock()) {
Expand Down Expand Up @@ -204,33 +206,30 @@ export class ManualPluginConfigModalComponent implements OnInit {
this.show = '';
}

save() {
async save() {
this.saveInProgress = true;
if (!this.saveCurrentBlock()) {
this.saveInProgress = false;
return;
}

return this.$api.post(`/config-editor/plugin/${encodeURIComponent(this.plugin.name)}`, this.pluginConfig)
.toPromise()
.then(data => {
this.$toastr.success(
this.translate.instant('plugins.settings.toast_restart_required'),
this.translate.instant('plugins.settings.toast_plugin_config_saved'),
);

this.$notification.configUpdated.next(undefined);
this.activeModal.close();
})
.catch(err => {
this.$toastr.error(this.translate.instant('config.toast_failed_to_save_config'), this.translate.instant('toast.title_error'));
this.saveInProgress = false;
});
try {
await this.$api.post(`/config-editor/plugin/${encodeURIComponent(this.plugin.name)}`, this.pluginConfig)
.toPromise();
this.$toastr.success(
this.translate.instant('plugins.settings.toast_restart_required'),
this.translate.instant('plugins.settings.toast_plugin_config_saved'));

this.$notification.configUpdated.next(undefined);
this.activeModal.close();
} catch {
this.$toastr.error(this.translate.instant('config.toast_failed_to_save_config'), this.translate.instant('toast.title_error'));
this.saveInProgress = false;
}
}

openFullConfigEditor() {
this.$router.navigate(['/config']);
this.activeModal.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { TranslateService } from '@ngx-translate/core';
import { ToastrService } from 'ngx-toastr';

import { WsService } from '@/app/core/ws.service';
import { ManagePluginsService } from '@/app/core/manage-plugins/manage-plugins.service';

@Component({
selector: 'app-child-bridge-widget',
templateUrl: './child-bridge-widget.component.html',
styleUrls: ['./child-bridge-widget.component.scss'],
})
export class ChildBridgeWidgetComponent implements OnInit, OnDestroy {
@Input() widget;
@Input() widget: any;

private io = this.$ws.connectToNamespace('child-bridges');

Expand All @@ -21,7 +20,6 @@ export class ChildBridgeWidgetComponent implements OnInit, OnDestroy {
private $toastr: ToastrService,
private $translate: TranslateService,
private $ws: WsService,
public $plugin: ManagePluginsService,
) { }

ngOnInit(): void {
Expand All @@ -30,7 +28,7 @@ export class ChildBridgeWidgetComponent implements OnInit, OnDestroy {
this.io.socket.emit('monitor-child-bridge-status');
});

this.io.socket.on('child-bridge-status-update', (data) => {
this.io.socket.on('child-bridge-status-update', (data: any) => {
const existingBridge = this.childBridges.find(x => x.username === data.username);
if (existingBridge) {
Object.assign(existingBridge, data);
Expand All @@ -46,7 +44,7 @@ export class ChildBridgeWidgetComponent implements OnInit, OnDestroy {
});
}

async restartChildBridge(bridge) {
async restartChildBridge(bridge: any) {
bridge.restartInProgress = true;
try {
await this.io.request('restart-child-bridge', bridge.username).toPromise();
Expand Down

0 comments on commit 2c552aa

Please sign in to comment.