[PROD-3727] [FIX] made response topic expression required

This commit is contained in:
mpetrov 2024-06-12 10:52:59 +03:00
parent c9565719b7
commit 98f873f0b6
2 changed files with 32 additions and 4 deletions

View File

@ -35,7 +35,7 @@
{{ 'gateway.rpc.withResponse' | translate }} {{ 'gateway.rpc.withResponse' | translate }}
</mat-slide-toggle> </mat-slide-toggle>
<mat-form-field *ngIf="isMQTTWithResponse.value"> <mat-form-field *ngIf="isMQTTWithResponse.value">
<mat-label>{{ 'gateway.rpc.responseTopicExpression' | translate }}</mat-label> <mat-label>{{ 'gateway.rpc.responseTopicExpression' | translate }}*</mat-label>
<input matInput formControlName="responseTopicExpression" <input matInput formControlName="responseTopicExpression"
placeholder="sensor/${deviceName}/response/${methodName}/${requestId}"/> placeholder="sensor/${deviceName}/response/${methodName}/${requestId}"/>
</mat-form-field> </mat-form-field>

View File

@ -14,7 +14,7 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, forwardRef, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { import {
ControlValueAccessor, ControlValueAccessor,
FormArray, FormArray,
@ -22,6 +22,8 @@ import {
FormControl, FormControl,
FormGroup, FormGroup,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormControl,
ValidatorFn,
Validators Validators
} from '@angular/forms'; } from '@angular/forms';
import { import {
@ -53,6 +55,8 @@ import {
} from '@shared/components/dialog/json-object-edit-dialog.component'; } from '@shared/components/dialog/json-object-edit-dialog.component';
import { jsonRequired } from '@shared/components/json-object-edit.component'; import { jsonRequired } from '@shared/components/json-object-edit.component';
import { deepClone } from '@core/utils'; import { deepClone } from '@core/utils';
import { takeUntil, tap } from "rxjs/operators";
import { Subject } from "rxjs";
@Component({ @Component({
selector: 'tb-gateway-service-rpc-connector', selector: 'tb-gateway-service-rpc-connector',
@ -66,7 +70,7 @@ import { deepClone } from '@core/utils';
} }
] ]
}) })
export class GatewayServiceRPCConnectorComponent implements OnInit, ControlValueAccessor { export class GatewayServiceRPCConnectorComponent implements OnInit, OnDestroy, ControlValueAccessor {
@Input() @Input()
connectorType: ConnectorType; connectorType: ConnectorType;
@ -105,6 +109,7 @@ export class GatewayServiceRPCConnectorComponent implements OnInit, ControlValue
private propagateChange = (v: any) => { private propagateChange = (v: any) => {
} }
private destroy$ = new Subject<void>();
constructor(private fb: FormBuilder, constructor(private fb: FormBuilder,
private dialog: MatDialog,) { private dialog: MatDialog,) {
@ -138,6 +143,17 @@ export class GatewayServiceRPCConnectorComponent implements OnInit, ControlValue
} }
}); });
this.isMQTTWithResponse = this.fb.control(false); this.isMQTTWithResponse = this.fb.control(false);
this.isMQTTWithResponse.valueChanges.pipe(
tap(() => {
this.commandForm.get('responseTopicExpression').updateValueAndValidity();
}),
takeUntil(this.destroy$),
).subscribe();
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
} }
connectorParamsFormGroupByType(type: ConnectorType): FormGroup { connectorParamsFormGroupByType(type: ConnectorType): FormGroup {
@ -148,7 +164,7 @@ export class GatewayServiceRPCConnectorComponent implements OnInit, ControlValue
formGroup = this.fb.group({ formGroup = this.fb.group({
methodFilter: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], methodFilter: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]],
requestTopicExpression: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], requestTopicExpression: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]],
responseTopicExpression: [null, [Validators.pattern(noLeadTrailSpacesRegex)]], responseTopicExpression: [null, [this.requiredOnWithResponse(), Validators.pattern(noLeadTrailSpacesRegex)]],
responseTimeout: [null, [Validators.min(10), Validators.pattern(this.numbersOnlyPattern)]], responseTimeout: [null, [Validators.min(10), Validators.pattern(this.numbersOnlyPattern)]],
valueExpression: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], valueExpression: [null, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]],
}) })
@ -424,4 +440,16 @@ export class GatewayServiceRPCConnectorComponent implements OnInit, ControlValue
this.commandForm.patchValue(value, {onlySelf: false}); this.commandForm.patchValue(value, {onlySelf: false});
} }
} }
private requiredOnWithResponse(): ValidatorFn {
return (control: UntypedFormControl) => {
const withResponse: boolean = this.isMQTTWithResponse?.value;
if (withResponse && !control.value) {
return { 'required': true };
}
return null;
};
}
} }