refactoring

This commit is contained in:
mpetrov 2024-07-10 12:27:40 +03:00
parent 1953ccb70f
commit c65b5724f5
3 changed files with 34 additions and 32 deletions

View File

@ -33,7 +33,6 @@ import { debounceTime, distinctUntilChanged, map, take, takeUntil } from 'rxjs/o
import { import {
ControlValueAccessor, ControlValueAccessor,
FormBuilder, FormBuilder,
FormGroup,
NG_VALIDATORS, NG_VALIDATORS,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormArray, UntypedFormArray,
@ -192,7 +191,7 @@ export class MappingTableComponent implements ControlValueAccessor, Validator, A
$event.stopPropagation(); $event.stopPropagation();
} }
const value = isDefinedAndNotNull(index) ? this.mappingFormGroup.at(index).value : {}; const value = isDefinedAndNotNull(index) ? this.mappingFormGroup.at(index).value : {};
this.dialog.open<MappingDialogComponent, MappingInfo, MappingValue>(MappingDialogComponent, { this.dialog.open<MappingDialogComponent, MappingInfo, ConnectorMapping>(MappingDialogComponent, {
disableClose: true, disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'], panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: { data: {
@ -207,26 +206,13 @@ export class MappingTableComponent implements ControlValueAccessor, Validator, A
if (isDefinedAndNotNull(index)) { if (isDefinedAndNotNull(index)) {
this.mappingFormGroup.at(index).patchValue(res); this.mappingFormGroup.at(index).patchValue(res);
} else { } else {
this.mappingFormGroup.push(this.getMappedDialogDataFormGroup(res)); this.pushDataAsFormArrays([res]);
} }
this.mappingFormGroup.markAsDirty(); this.mappingFormGroup.markAsDirty();
} }
}); });
} }
private getMappedDialogDataFormGroup(mappingValue: MappingValue): FormGroup {
Object.keys(mappingValue).forEach(key => {
if (Array.isArray(mappingValue[key])) {
mappingValue = {
...mappingValue,
[key]: this.fb.control(mappingValue[key]),
};
}
});
return this.fb.group(mappingValue);
}
private updateTableData(value: ConnectorMapping[], textSearch?: string): void { private updateTableData(value: ConnectorMapping[], textSearch?: string): void {
let tableValue = value.map(mappingValue => this.getMappingValue(mappingValue)); let tableValue = value.map(mappingValue => this.getMappingValue(mappingValue));
if (textSearch) { if (textSearch) {

View File

@ -24,9 +24,13 @@ import { Router } from '@angular/router';
import { import {
Attribute, Attribute,
AttributesUpdate, AttributesUpdate,
ConnectorMapping,
ConnectorMappingFormValue,
ConverterMappingFormValue,
ConvertorType, ConvertorType,
ConvertorTypeTranslationsMap, ConvertorTypeTranslationsMap,
DataConversionTranslationsMap, DataConversionTranslationsMap,
DeviceConnectorMapping,
DeviceInfoType, DeviceInfoType,
MappingHintTranslationsMap, MappingHintTranslationsMap,
MappingInfo, MappingInfo,
@ -37,11 +41,11 @@ import {
MappingKeysType, MappingKeysType,
MappingType, MappingType,
MappingTypeTranslationsMap, MappingTypeTranslationsMap,
MappingValue,
noLeadTrailSpacesRegex, noLeadTrailSpacesRegex,
OPCUaSourceTypes, OPCUaSourceTypes,
QualityTypes, QualityTypes,
QualityTypeTranslationsMap, QualityTypeTranslationsMap,
RequestMappingFormValue,
RequestType, RequestType,
RequestTypesTranslationsMap, RequestTypesTranslationsMap,
RpcMethod, RpcMethod,
@ -62,7 +66,7 @@ import { MappingDataKeysPanelComponent } from '@home/components/widget/lib/gatew
templateUrl: './mapping-dialog.component.html', templateUrl: './mapping-dialog.component.html',
styleUrls: ['./mapping-dialog.component.scss'] styleUrls: ['./mapping-dialog.component.scss']
}) })
export class MappingDialogComponent extends DialogComponent<MappingDialogComponent, MappingValue> implements OnDestroy { export class MappingDialogComponent extends DialogComponent<MappingDialogComponent, ConnectorMapping> implements OnDestroy {
mappingForm: UntypedFormGroup; mappingForm: UntypedFormGroup;
@ -104,7 +108,7 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
constructor(protected store: Store<AppState>, constructor(protected store: Store<AppState>,
protected router: Router, protected router: Router,
@Inject(MAT_DIALOG_DATA) public data: MappingInfo, @Inject(MAT_DIALOG_DATA) public data: MappingInfo,
public dialogRef: MatDialogRef<MappingDialogComponent, MappingValue>, public dialogRef: MatDialogRef<MappingDialogComponent, ConnectorMapping>,
private fb: FormBuilder, private fb: FormBuilder,
private popoverService: TbPopoverService, private popoverService: TbPopoverService,
private renderer: Renderer2, private renderer: Renderer2,
@ -247,7 +251,7 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
} }
} }
private prepareMappingData(): { [key: string]: unknown } { private prepareMappingData(): ConnectorMapping {
const formValue = this.mappingForm.value; const formValue = this.mappingForm.value;
switch (this.data.mappingType) { switch (this.data.mappingType) {
case MappingType.DATA: case MappingType.DATA:
@ -270,7 +274,7 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
} }
} }
private prepareFormValueData(): { [key: string]: unknown } { private getFormValueData(): ConnectorMappingFormValue {
if (this.data.value && Object.keys(this.data.value).length) { if (this.data.value && Object.keys(this.data.value).length) {
switch (this.data.mappingType) { switch (this.data.mappingType) {
case MappingType.DATA: case MappingType.DATA:
@ -282,16 +286,16 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
type: converter.type, type: converter.type,
[converter.type]: {...converter} [converter.type]: {...converter}
} }
}; } as ConverterMappingFormValue;
case MappingType.REQUESTS: case MappingType.REQUESTS:
return { return {
requestType: this.data.value.requestType, requestType: this.data.value.requestType,
requestValue: { requestValue: {
[this.data.value.requestType]: this.data.value.requestValue [this.data.value.requestType]: this.data.value.requestValue
} }
}; } as RequestMappingFormValue;
default: default:
return this.data.value; return this.data.value as DeviceConnectorMapping;
} }
} }
} }
@ -317,7 +321,7 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
extensionConfig: [{}, []] extensionConfig: [{}, []]
}), }),
})); }));
this.mappingForm.patchValue(this.prepareFormValueData()); this.mappingForm.patchValue(this.getFormValueData());
this.mappingForm.get('converter.type').valueChanges.pipe( this.mappingForm.get('converter.type').valueChanges.pipe(
startWith(this.mappingForm.get('converter.type').value), startWith(this.mappingForm.get('converter.type').value),
takeUntil(this.destroy$) takeUntil(this.destroy$)
@ -397,7 +401,7 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
requestValueGroup.get('responseTimeout').enable({emitEvent: false}); requestValueGroup.get('responseTimeout').enable({emitEvent: false});
} }
}); });
this.mappingForm.patchValue(this.prepareFormValueData()); this.mappingForm.patchValue(this.getFormValueData());
} }
private createOPCUAMappingForm(): void { private createOPCUAMappingForm(): void {
@ -410,6 +414,6 @@ export class MappingDialogComponent extends DialogComponent<MappingDialogCompone
rpc_methods: [[], []], rpc_methods: [[], []],
attributes_updates: [[], []] attributes_updates: [[], []]
}); });
this.mappingForm.patchValue(this.prepareFormValueData()); this.mappingForm.patchValue(this.getFormValueData());
} }
} }

View File

@ -174,6 +174,8 @@ export interface ConnectorSecurity {
export type ConnectorMapping = DeviceConnectorMapping | RequestMappingData | ConverterConnectorMapping; export type ConnectorMapping = DeviceConnectorMapping | RequestMappingData | ConverterConnectorMapping;
export type ConnectorMappingFormValue = DeviceConnectorMapping | RequestMappingFormValue | ConverterMappingFormValue;
export interface ConnectorBaseConfig { export interface ConnectorBaseConfig {
mapping?: DeviceConnectorMapping[]; mapping?: DeviceConnectorMapping[];
dataMapping?: ConverterConnectorMapping[]; dataMapping?: ConverterConnectorMapping[];
@ -223,7 +225,7 @@ export interface AttributesUpdate {
value: string; value: string;
} }
interface Converter { export interface Converter {
type: ConvertorType; type: ConvertorType;
deviceNameJsonExpression: string; deviceNameJsonExpression: string;
deviceTypeJsonExpression: string; deviceTypeJsonExpression: string;
@ -239,14 +241,20 @@ export interface ConverterConnectorMapping {
converter: Converter; converter: Converter;
} }
export type ConverterMappingFormValue = Omit<ConverterConnectorMapping, 'converter'> & {
converter: {
type: ConvertorType;
} & Record<ConvertorType, Converter>;
};
export interface DeviceConnectorMapping { export interface DeviceConnectorMapping {
deviceNodePattern: string; deviceNodePattern: string;
deviceNodeSource: string; deviceNodeSource: string;
deviceInfo: DeviceInfo; deviceInfo: DeviceInfo;
attributes: Attribute[]; attributes?: Attribute[];
timeseries: Timeseries[]; timeseries?: Timeseries[];
rpc_methods: RpcMethod[]; rpc_methods?: RpcMethod[];
attributes_updates: AttributesUpdate[]; attributes_updates?: AttributesUpdate[];
} }
export enum ConnectorType { export enum ConnectorType {
@ -597,6 +605,10 @@ export interface RequestMappingData {
requestValue: RequestDataItem; requestValue: RequestDataItem;
} }
export type RequestMappingFormValue = Omit<RequestMappingData, 'requestValue'> & {
requestValue: Record<RequestType, RequestDataItem>;
};
export interface RequestDataItem { export interface RequestDataItem {
type: string; type: string;
details: string; details: string;