[4143] refactoring
This commit is contained in:
parent
10d91187ae
commit
75528e98bf
@ -106,6 +106,7 @@ export class MqttRpcParametersComponent implements ControlValueAccessor, Validat
|
||||
|
||||
writeValue(value: RPCTemplateConfigMQTT): void {
|
||||
this.rpcParametersFormGroup.patchValue(value, {emitEvent: false});
|
||||
this.toggleResponseFields(value.withResponse);
|
||||
}
|
||||
|
||||
private observeValueChanges(): void {
|
||||
@ -119,18 +120,20 @@ export class MqttRpcParametersComponent implements ControlValueAccessor, Validat
|
||||
|
||||
private observeWithResponse(): void {
|
||||
this.rpcParametersFormGroup.get('withResponse').valueChanges.pipe(
|
||||
tap((isActive: boolean) => {
|
||||
const responseTopicControl = this.rpcParametersFormGroup.get('responseTopicExpression');
|
||||
const responseTimeoutControl = this.rpcParametersFormGroup.get('responseTimeout');
|
||||
if (isActive) {
|
||||
responseTopicControl.enable();
|
||||
responseTimeoutControl.enable();
|
||||
} else {
|
||||
responseTopicControl.disable();
|
||||
responseTimeoutControl.disable();
|
||||
}
|
||||
}),
|
||||
tap((isActive: boolean) => this.toggleResponseFields(isActive)),
|
||||
takeUntil(this.destroy$),
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
private toggleResponseFields(enabled: boolean): void {
|
||||
const responseTopicControl = this.rpcParametersFormGroup.get('responseTopicExpression');
|
||||
const responseTimeoutControl = this.rpcParametersFormGroup.get('responseTimeout');
|
||||
if (enabled) {
|
||||
responseTopicControl.enable();
|
||||
responseTimeoutControl.enable();
|
||||
} else {
|
||||
responseTopicControl.disable();
|
||||
responseTimeoutControl.disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@
|
||||
</mat-icon>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<button mat-icon-button (click)="removeOCPUAArguments(i)"
|
||||
<button mat-icon-button (click)="removeArgument(i)"
|
||||
class="tb-box-button"
|
||||
matTooltip="{{ 'gateway.rpc.remove' | translate }}"
|
||||
matTooltipPosition="above">
|
||||
@ -82,7 +82,7 @@
|
||||
</div>
|
||||
<button mat-raised-button
|
||||
fxFlexAlign="start"
|
||||
(click)="addOCPUAArguments()">
|
||||
(click)="addArgument()">
|
||||
{{ 'gateway.rpc.add-argument' | translate }}
|
||||
</button>
|
||||
</fieldset>
|
||||
|
||||
@ -44,7 +44,7 @@ import {
|
||||
OPCTypeValue,
|
||||
RPCTemplateConfigOPC
|
||||
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
||||
import { isEqual } from '@core/utils';
|
||||
import { isDefinedAndNotNull, isEqual } from '@core/utils';
|
||||
|
||||
@Component({
|
||||
selector: 'tb-opc-rpc-parameters',
|
||||
@ -77,8 +77,8 @@ export class OpcRpcParametersComponent implements ControlValueAccessor, Validato
|
||||
readonly MappingValueType = MappingValueType;
|
||||
readonly valueTypes = mappingValueTypesMap;
|
||||
|
||||
private onChange: (value: RPCTemplateConfigOPC) => void;
|
||||
private onTouched: () => void;
|
||||
private onChange: (value: RPCTemplateConfigOPC) => void = (_) => {} ;
|
||||
private onTouched: () => void = () => {};
|
||||
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
@ -111,11 +111,11 @@ export class OpcRpcParametersComponent implements ControlValueAccessor, Validato
|
||||
}
|
||||
|
||||
writeValue(params: RPCTemplateConfigOPC): void {
|
||||
this.clearFromArrayByName('arguments');
|
||||
this.clearArguments();
|
||||
params.arguments?.map(({type, value}) => ({type, [type]: value }))
|
||||
.forEach(argument => this.addOCPUAArguments(argument as OPCTypeValue));
|
||||
.forEach(argument => this.addArgument(argument as OPCTypeValue));
|
||||
this.cdr.markForCheck();
|
||||
this.rpcParametersFormGroup.get('method').patchValue(params.method, {emitEvent: false});
|
||||
this.rpcParametersFormGroup.get('method').patchValue(params.method);
|
||||
}
|
||||
|
||||
private observeValueChanges(): void {
|
||||
@ -128,27 +128,30 @@ export class OpcRpcParametersComponent implements ControlValueAccessor, Validato
|
||||
});
|
||||
}
|
||||
|
||||
removeOCPUAArguments(index: number): void {
|
||||
removeArgument(index: number): void {
|
||||
(this.rpcParametersFormGroup.get('arguments') as FormArray).removeAt(index);
|
||||
}
|
||||
|
||||
addOCPUAArguments(value: OPCTypeValue = {} as OPCTypeValue): void {
|
||||
addArgument(value: OPCTypeValue = {} as OPCTypeValue): void {
|
||||
const fromGroup = this.fb.group({
|
||||
type: [value.type ?? MappingValueType.STRING],
|
||||
string: [
|
||||
value.string ?? { value: '', disabled: !(isEqual(value, {}) || value.string)},
|
||||
[Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]
|
||||
],
|
||||
integer: [{value: value.integer ?? 0, disabled: !value.integer}, [Validators.required, Validators.pattern(integerRegex)]],
|
||||
double: [{value: value.double ?? 0, disabled: !value.double}, [Validators.required]],
|
||||
boolean: [{value: value.boolean ?? false, disabled: !value.boolean}, [Validators.required]],
|
||||
integer: [
|
||||
{value: value.integer ?? 0, disabled: !isDefinedAndNotNull(value.integer)},
|
||||
[Validators.required, Validators.pattern(integerRegex)]
|
||||
],
|
||||
double: [{value: value.double ?? 0, disabled: !isDefinedAndNotNull(value.double)}, [Validators.required]],
|
||||
boolean: [{value: value.boolean ?? false, disabled: !isDefinedAndNotNull(value.boolean)}, [Validators.required]],
|
||||
});
|
||||
this.observeTypeChange(fromGroup);
|
||||
(this.rpcParametersFormGroup.get('arguments') as FormArray).push(fromGroup, {emitEvent: false});
|
||||
}
|
||||
|
||||
clearFromArrayByName(name: string): void {
|
||||
const formArray = this.rpcParametersFormGroup.get(name) as FormArray;
|
||||
clearArguments(): void {
|
||||
const formArray = this.rpcParametersFormGroup.get('arguments') as FormArray;
|
||||
while (formArray.length !== 0) {
|
||||
formArray.removeAt(0);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user