2024-11-06 19:53:06 +02:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Component,
|
|
|
|
|
Input,
|
|
|
|
|
Renderer2,
|
|
|
|
|
ViewContainerRef,
|
|
|
|
|
DestroyRef,
|
|
|
|
|
ChangeDetectionStrategy,
|
2024-11-29 13:05:33 +02:00
|
|
|
forwardRef
|
2024-11-06 19:53:06 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
|
|
import { SharedModule } from '@shared/shared.module';
|
2024-11-13 14:50:23 +02:00
|
|
|
import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe';
|
2024-11-06 19:53:06 +02:00
|
|
|
import { TbPopoverService } from '@shared/components/popover.service';
|
|
|
|
|
import { MatButton } from '@angular/material/button';
|
2024-11-29 13:05:33 +02:00
|
|
|
import { DebugSettingsPanelComponent } from './debug-settings-panel.component';
|
2024-11-06 19:53:06 +02:00
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
2024-11-13 15:09:29 +02:00
|
|
|
import { shareReplay, timer } from 'rxjs';
|
2024-11-12 17:13:58 +02:00
|
|
|
import { SECOND } from '@shared/models/time/time.models';
|
2024-11-29 13:05:33 +02:00
|
|
|
import { DebugSettings } from '@shared/models/entity.models';
|
2024-11-13 14:50:23 +02:00
|
|
|
import { map } from 'rxjs/operators';
|
2024-11-13 15:09:29 +02:00
|
|
|
import { getCurrentAuthState } from '@core/auth/auth.selectors';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
2024-11-29 13:05:33 +02:00
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
|
|
|
|
FormBuilder,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
UntypedFormGroup,
|
|
|
|
|
} from '@angular/forms';
|
2024-11-06 19:53:06 +02:00
|
|
|
|
|
|
|
|
@Component({
|
2024-11-29 13:05:33 +02:00
|
|
|
selector: 'tb-debug-settings-button',
|
|
|
|
|
templateUrl: './debug-settings-button.component.html',
|
2024-11-06 19:53:06 +02:00
|
|
|
standalone: true,
|
|
|
|
|
imports: [
|
|
|
|
|
CommonModule,
|
|
|
|
|
SharedModule,
|
2024-11-13 14:50:23 +02:00
|
|
|
DurationLeftPipe,
|
2024-11-06 19:53:06 +02:00
|
|
|
],
|
2024-11-29 13:05:33 +02:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => DebugSettingsButtonComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-11-06 19:53:06 +02:00
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
|
})
|
2024-11-29 13:05:33 +02:00
|
|
|
export class DebugSettingsButtonComponent implements ControlValueAccessor {
|
2024-11-06 19:53:06 +02:00
|
|
|
|
2024-11-13 15:09:29 +02:00
|
|
|
@Input() debugLimitsConfiguration: string;
|
2024-11-06 19:53:06 +02:00
|
|
|
|
2024-11-29 13:05:33 +02:00
|
|
|
debugSettingsFormGroup: UntypedFormGroup;
|
|
|
|
|
disabled = false;
|
|
|
|
|
isDebugAllActive$ = timer(0, SECOND).pipe(map(() => this.allEnabledUntil > new Date().getTime() || this.allEnabled), shareReplay(1));
|
2024-11-13 15:09:29 +02:00
|
|
|
|
|
|
|
|
readonly maxDebugModeDurationMinutes = getCurrentAuthState(this.store).maxDebugModeDurationMinutes;
|
2024-11-13 14:50:23 +02:00
|
|
|
|
2024-11-29 13:05:33 +02:00
|
|
|
private onChange: (settings: DebugSettings) => void;
|
|
|
|
|
|
2024-11-13 14:50:23 +02:00
|
|
|
constructor(private popoverService: TbPopoverService,
|
2024-11-06 19:53:06 +02:00
|
|
|
private renderer: Renderer2,
|
2024-11-13 15:09:29 +02:00
|
|
|
private store: Store<AppState>,
|
2024-11-06 19:53:06 +02:00
|
|
|
private viewContainerRef: ViewContainerRef,
|
|
|
|
|
private destroyRef: DestroyRef,
|
2024-11-29 13:05:33 +02:00
|
|
|
private fb: FormBuilder,
|
|
|
|
|
) {
|
|
|
|
|
this.debugSettingsFormGroup = this.fb.group({
|
|
|
|
|
failuresEnabled: [false],
|
|
|
|
|
allEnabled: [false],
|
|
|
|
|
allEnabledUntil: []
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.debugSettingsFormGroup.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(value => {
|
|
|
|
|
this.onChange(value);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get failuresEnabled(): boolean {
|
|
|
|
|
return this.debugSettingsFormGroup.get('failuresEnabled').value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get allEnabled(): boolean {
|
|
|
|
|
return this.debugSettingsFormGroup.get('allEnabled').value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get allEnabledUntil(): number {
|
|
|
|
|
return this.debugSettingsFormGroup.get('allEnabledUntil').value;
|
|
|
|
|
}
|
2024-11-06 19:53:06 +02:00
|
|
|
|
|
|
|
|
openDebugStrategyPanel($event: Event, matButton: MatButton): void {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
const trigger = matButton._elementRef.nativeElement;
|
2024-11-29 13:05:33 +02:00
|
|
|
const debugSettings = this.debugSettingsFormGroup.value;
|
|
|
|
|
|
2024-11-06 19:53:06 +02:00
|
|
|
if (this.popoverService.hasPopover(trigger)) {
|
|
|
|
|
this.popoverService.hidePopover(trigger);
|
|
|
|
|
} else {
|
|
|
|
|
const debugStrategyPopover = this.popoverService.displayPopover(trigger, this.renderer,
|
2024-11-29 13:05:33 +02:00
|
|
|
this.viewContainerRef, DebugSettingsPanelComponent, 'bottom', true, null,
|
2024-11-12 17:13:58 +02:00
|
|
|
{
|
2024-11-29 13:05:33 +02:00
|
|
|
...debugSettings,
|
2024-11-13 15:09:29 +02:00
|
|
|
maxDebugModeDurationMinutes: this.maxDebugModeDurationMinutes,
|
|
|
|
|
debugLimitsConfiguration: this.debugLimitsConfiguration
|
2024-11-12 17:13:58 +02:00
|
|
|
},
|
2024-11-06 19:53:06 +02:00
|
|
|
{},
|
|
|
|
|
{}, {}, true);
|
|
|
|
|
debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover;
|
2024-11-29 13:05:33 +02:00
|
|
|
debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((settings: DebugSettings) => {
|
|
|
|
|
this.debugSettingsFormGroup.patchValue(settings);
|
2024-11-06 19:53:06 +02:00
|
|
|
debugStrategyPopover.hide();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-29 13:05:33 +02:00
|
|
|
|
|
|
|
|
registerOnChange(fn: (settings: DebugSettings) => void): void {
|
|
|
|
|
this.onChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(_: () => void): void {}
|
|
|
|
|
|
|
|
|
|
writeValue(settings: DebugSettings): void {
|
|
|
|
|
this.debugSettingsFormGroup.patchValue(settings, {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
this.debugSettingsFormGroup[isDisabled ? 'disable' : 'enable']({emitEvent: false});
|
|
|
|
|
}
|
2024-11-06 19:53:06 +02:00
|
|
|
}
|