changed approach to abstract class for mapping
This commit is contained in:
parent
d66130fc2c
commit
c4441eba72
@ -0,0 +1,36 @@
|
|||||||
|
import { GatewayConnector } from '@home/components/widget/lib/gateway/gateway-widget.models';
|
||||||
|
|
||||||
|
export abstract class GatewayConnectorVersionProcessor<BasicConfig> {
|
||||||
|
gatewayVersion: number;
|
||||||
|
configVersion: number;
|
||||||
|
|
||||||
|
protected constructor(protected gatewayVersionStr: string, protected connector: GatewayConnector) {
|
||||||
|
this.gatewayVersion = this.parseVersion(this.gatewayVersionStr);
|
||||||
|
this.configVersion = this.parseVersion(connector.configVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
getProcessedByVersion(): BasicConfig {
|
||||||
|
if (this.isVersionUpdateNeeded()) {
|
||||||
|
return this.configVersion > this.gatewayVersion
|
||||||
|
? this.getDowngradedVersion()
|
||||||
|
: this.getUpgradedVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.connector.configurationJson as unknown as BasicConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
private isVersionUpdateNeeded(): boolean {
|
||||||
|
if (!this.gatewayVersion || !this.configVersion) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.configVersion !== this.gatewayVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseVersion(version: string): number {
|
||||||
|
return Number(version?.replace(/\./g, ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract getDowngradedVersion(): BasicConfig;
|
||||||
|
abstract getUpgradedVersion(): BasicConfig;
|
||||||
|
}
|
||||||
@ -0,0 +1,76 @@
|
|||||||
|
import {
|
||||||
|
GatewayConnectorVersionProcessor
|
||||||
|
} from '@home/components/widget/lib/gateway/abstract/gateway-connector-version-processor.abstract';
|
||||||
|
import {
|
||||||
|
GatewayConnector,
|
||||||
|
MQTTBasicConfig, MQTTBasicConfig_v3_5_2,
|
||||||
|
MQTTLegacyBasicConfig,
|
||||||
|
RequestMappingData,
|
||||||
|
RequestType,
|
||||||
|
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
||||||
|
import { isEqual } from '@core/utils';
|
||||||
|
import { MqttVersionMappingUtil } from '@home/components/widget/lib/gateway/utils/mqtt-version-mapping.util';
|
||||||
|
|
||||||
|
export class MqttVersionProcessor extends GatewayConnectorVersionProcessor<MQTTBasicConfig> {
|
||||||
|
private readonly mqttRequestTypeKeys = Object.values(RequestType);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected gatewayVersionStr: string,
|
||||||
|
protected connector: GatewayConnector
|
||||||
|
) {
|
||||||
|
super(gatewayVersionStr, connector);
|
||||||
|
}
|
||||||
|
getUpgradedVersion(): MQTTBasicConfig {
|
||||||
|
const {
|
||||||
|
connectRequests,
|
||||||
|
disconnectRequests,
|
||||||
|
attributeRequests,
|
||||||
|
attributeUpdates,
|
||||||
|
serverSideRpc
|
||||||
|
} = this.connector.configurationJson as MQTTLegacyBasicConfig;
|
||||||
|
let configurationJson = {
|
||||||
|
...this.connector.configurationJson,
|
||||||
|
requestsMapping: MqttVersionMappingUtil.mapRequestsToUpgradedVersion({
|
||||||
|
connectRequests,
|
||||||
|
disconnectRequests,
|
||||||
|
attributeRequests,
|
||||||
|
attributeUpdates,
|
||||||
|
serverSideRpc
|
||||||
|
}),
|
||||||
|
mapping: MqttVersionMappingUtil.mapMappingToUpgradedVersion((this.connector.configurationJson as MQTTLegacyBasicConfig).mapping),
|
||||||
|
};
|
||||||
|
|
||||||
|
this.mqttRequestTypeKeys.forEach((key: RequestType) => {
|
||||||
|
const { [key]: removedKey, ...rest } = configurationJson as MQTTLegacyBasicConfig;
|
||||||
|
configurationJson = { ...rest } as any;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.cleanUpConfigJson(configurationJson as MQTTBasicConfig_v3_5_2);
|
||||||
|
|
||||||
|
return configurationJson as MQTTBasicConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
getDowngradedVersion(): MQTTLegacyBasicConfig {
|
||||||
|
const { requestsMapping, mapping, ...restConfig } = this.connector.configurationJson as MQTTBasicConfig_v3_5_2;
|
||||||
|
|
||||||
|
const updatedRequestsMapping =
|
||||||
|
MqttVersionMappingUtil.mapRequestsToDowngradedVersion(requestsMapping as Record<RequestType, RequestMappingData[]>);
|
||||||
|
const updatedMapping = MqttVersionMappingUtil.mapMappingToDowngradedVersion(mapping);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...restConfig,
|
||||||
|
...updatedRequestsMapping,
|
||||||
|
mapping: updatedMapping,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private cleanUpConfigJson(configurationJson: MQTTBasicConfig_v3_5_2): void {
|
||||||
|
if (isEqual(configurationJson.requestsMapping, {})) {
|
||||||
|
delete configurationJson.requestsMapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEqual(configurationJson.mapping, [])) {
|
||||||
|
delete configurationJson.mapping;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,24 +17,23 @@
|
|||||||
import { ControlValueAccessor, FormBuilder, FormGroup, ValidationErrors, Validator } from '@angular/forms';
|
import { ControlValueAccessor, FormBuilder, FormGroup, ValidationErrors, Validator } from '@angular/forms';
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
import { takeUntil } from 'rxjs/operators';
|
import { takeUntil } from 'rxjs/operators';
|
||||||
import { isObject } from 'lodash';
|
|
||||||
import {
|
import {
|
||||||
MappingType,
|
MappingType,
|
||||||
MQTTBasicConfig,
|
MQTTBasicConfig,
|
||||||
MQTTLegacyBasicConfig,
|
|
||||||
RequestMappingData,
|
RequestMappingData,
|
||||||
RequestMappingValue,
|
RequestMappingValue,
|
||||||
RequestType
|
RequestType
|
||||||
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
||||||
import { Directive, OnDestroy } from '@angular/core';
|
import { Directive, OnDestroy } from '@angular/core';
|
||||||
|
import { isObject } from '@core/utils';
|
||||||
|
|
||||||
@Directive()
|
@Directive()
|
||||||
export abstract class AbstractMqttBasicConfigComponent implements ControlValueAccessor, Validator, OnDestroy {
|
export abstract class AbstractMqttBasicConfigComponent<BasicConfig> implements ControlValueAccessor, Validator, OnDestroy {
|
||||||
|
|
||||||
basicFormGroup: FormGroup;
|
basicFormGroup: FormGroup;
|
||||||
mappingTypes = MappingType;
|
mappingTypes = MappingType;
|
||||||
private destroy$ = new Subject<void>();
|
private destroy$ = new Subject<void>();
|
||||||
private onChange: (value: MQTTBasicConfig | MQTTLegacyBasicConfig) => void;
|
private onChange: (value: BasicConfig) => void;
|
||||||
private onTouched: () => void;
|
private onTouched: () => void;
|
||||||
|
|
||||||
constructor(protected fb: FormBuilder) {
|
constructor(protected fb: FormBuilder) {
|
||||||
@ -58,7 +57,7 @@ export abstract class AbstractMqttBasicConfigComponent implements ControlValueAc
|
|||||||
this.destroy$.complete();
|
this.destroy$.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (value: MQTTBasicConfig | MQTTLegacyBasicConfig) => void): void {
|
registerOnChange(fn: (value: BasicConfig) => void): void {
|
||||||
this.onChange = fn;
|
this.onChange = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,6 +101,6 @@ export abstract class AbstractMqttBasicConfigComponent implements ControlValueAc
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract writeValue(basicConfig: MQTTBasicConfig | MQTTLegacyBasicConfig): void;
|
abstract writeValue(basicConfig: BasicConfig): void;
|
||||||
protected abstract getMappedMQTTConfig(basicConfig: MQTTBasicConfig): MQTTBasicConfig | MQTTLegacyBasicConfig;
|
protected abstract getMappedMQTTConfig(basicConfig: MQTTBasicConfig): BasicConfig;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
import { Component, forwardRef, Input, TemplateRef, ChangeDetectionStrategy } from '@angular/core';
|
import { Component, forwardRef, Input, TemplateRef, ChangeDetectionStrategy } from '@angular/core';
|
||||||
import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
|
import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
|
||||||
import {
|
import {
|
||||||
MQTTBasicConfig,
|
MQTTBasicConfig_v3_5_2,
|
||||||
RequestMappingData,
|
RequestMappingData,
|
||||||
RequestMappingValue,
|
RequestMappingValue,
|
||||||
RequestType
|
RequestType
|
||||||
@ -68,12 +68,12 @@ import {
|
|||||||
MappingTableComponent,
|
MappingTableComponent,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class MqttBasicConfigComponent extends AbstractMqttBasicConfigComponent {
|
export class MqttBasicConfigComponent extends AbstractMqttBasicConfigComponent<MQTTBasicConfig_v3_5_2> {
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
generalTabContent: TemplateRef<any>;
|
generalTabContent: TemplateRef<any>;
|
||||||
|
|
||||||
writeValue(basicConfig: MQTTBasicConfig): void {
|
writeValue(basicConfig: MQTTBasicConfig_v3_5_2): void {
|
||||||
const { broker, mapping = [], requestsMapping } = basicConfig;
|
const { broker, mapping = [], requestsMapping } = basicConfig;
|
||||||
const editedBase = {
|
const editedBase = {
|
||||||
workers: broker && (broker.maxNumberOfWorkers || broker.maxMessageNumberPerWorker) ? {
|
workers: broker && (broker.maxNumberOfWorkers || broker.maxMessageNumberPerWorker) ? {
|
||||||
@ -88,7 +88,7 @@ export class MqttBasicConfigComponent extends AbstractMqttBasicConfigComponent {
|
|||||||
this.basicFormGroup.setValue(editedBase, {emitEvent: false});
|
this.basicFormGroup.setValue(editedBase, {emitEvent: false});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getMappedMQTTConfig(basicConfig: MQTTBasicConfig): MQTTBasicConfig {
|
protected getMappedMQTTConfig(basicConfig: MQTTBasicConfig_v3_5_2): MQTTBasicConfig_v3_5_2 {
|
||||||
let { broker, workers, mapping, requestsMapping } = basicConfig || {};
|
let { broker, workers, mapping, requestsMapping } = basicConfig || {};
|
||||||
|
|
||||||
if (isDefinedAndNotNull(workers.maxNumberOfWorkers) || isDefinedAndNotNull(workers.maxMessageNumberPerWorker)) {
|
if (isDefinedAndNotNull(workers.maxNumberOfWorkers) || isDefinedAndNotNull(workers.maxMessageNumberPerWorker)) {
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
import { Component, forwardRef, Input, TemplateRef, ChangeDetectionStrategy } from '@angular/core';
|
import { Component, forwardRef, Input, TemplateRef, ChangeDetectionStrategy } from '@angular/core';
|
||||||
import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
|
import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
|
||||||
import {
|
import {
|
||||||
MQTTBasicConfig,
|
MQTTBasicConfig_v3_5_2,
|
||||||
MQTTLegacyBasicConfig,
|
MQTTLegacyBasicConfig,
|
||||||
RequestMappingData,
|
RequestMappingData,
|
||||||
RequestMappingValue,
|
RequestMappingValue,
|
||||||
@ -70,7 +70,7 @@ import {
|
|||||||
MappingTableComponent,
|
MappingTableComponent,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigComponent {
|
export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigComponent<MQTTLegacyBasicConfig> {
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
generalTabContent: TemplateRef<any>;
|
generalTabContent: TemplateRef<any>;
|
||||||
@ -85,7 +85,7 @@ export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigCompo
|
|||||||
attributeUpdates = [],
|
attributeUpdates = [],
|
||||||
serverSideRpc = []
|
serverSideRpc = []
|
||||||
} = basicConfig;
|
} = basicConfig;
|
||||||
const updatedRequestMapping = MqttVersionMappingUtil.mapRequestsToNewestVersion({
|
const updatedRequestMapping = MqttVersionMappingUtil.mapRequestsToUpgradedVersion({
|
||||||
connectRequests,
|
connectRequests,
|
||||||
disconnectRequests,
|
disconnectRequests,
|
||||||
attributeRequests,
|
attributeRequests,
|
||||||
@ -97,7 +97,7 @@ export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigCompo
|
|||||||
maxNumberOfWorkers: broker.maxNumberOfWorkers,
|
maxNumberOfWorkers: broker.maxNumberOfWorkers,
|
||||||
maxMessageNumberPerWorker: broker.maxMessageNumberPerWorker,
|
maxMessageNumberPerWorker: broker.maxMessageNumberPerWorker,
|
||||||
} : {},
|
} : {},
|
||||||
mapping: MqttVersionMappingUtil.mapMappingToNewestVersion(mapping) || [],
|
mapping: MqttVersionMappingUtil.mapMappingToUpgradedVersion(mapping) || [],
|
||||||
broker: broker || {},
|
broker: broker || {},
|
||||||
requestsMapping: this.getRequestDataArray(updatedRequestMapping),
|
requestsMapping: this.getRequestDataArray(updatedRequestMapping),
|
||||||
};
|
};
|
||||||
@ -105,7 +105,7 @@ export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigCompo
|
|||||||
this.basicFormGroup.setValue(editedBase, {emitEvent: false});
|
this.basicFormGroup.setValue(editedBase, {emitEvent: false});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getMappedMQTTConfig(basicConfig: MQTTBasicConfig): MQTTLegacyBasicConfig {
|
protected getMappedMQTTConfig(basicConfig: MQTTBasicConfig_v3_5_2): MQTTLegacyBasicConfig {
|
||||||
let { broker, workers, mapping, requestsMapping } = basicConfig || {};
|
let { broker, workers, mapping, requestsMapping } = basicConfig || {};
|
||||||
|
|
||||||
if (isDefinedAndNotNull(workers.maxNumberOfWorkers) || isDefinedAndNotNull(workers.maxMessageNumberPerWorker)) {
|
if (isDefinedAndNotNull(workers.maxNumberOfWorkers) || isDefinedAndNotNull(workers.maxMessageNumberPerWorker)) {
|
||||||
@ -121,8 +121,8 @@ export class MqttLegacyBasicConfigComponent extends AbstractMqttBasicConfigCompo
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
broker,
|
broker,
|
||||||
mapping: MqttVersionMappingUtil.mapMappingToLegacyVersion(mapping),
|
mapping: MqttVersionMappingUtil.mapMappingToDowngradedVersion(mapping),
|
||||||
...(MqttVersionMappingUtil.mapRequestsToLegacyVersion(requestsMapping as Record<RequestType, RequestMappingData[]>))
|
...(MqttVersionMappingUtil.mapRequestsToDowngradedVersion(requestsMapping as Record<RequestType, RequestMappingData[]>))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -97,8 +97,15 @@ export class AddConnectorDialogComponent
|
|||||||
this.submitted = true;
|
this.submitted = true;
|
||||||
const value = this.connectorForm.getRawValue();
|
const value = this.connectorForm.getRawValue();
|
||||||
if (value.useDefaults) {
|
if (value.useDefaults) {
|
||||||
this.getDefaultConfig(value.type).subscribe(({current, legacy, ...defaultConfig}: GatewayVersionedDefaultConfig) => {
|
this.getDefaultConfig(value.type).subscribe((defaultConfig: GatewayVersionedDefaultConfig) => {
|
||||||
value.configurationJson = (this.data.gatewayVersion ? current : legacy) ?? defaultConfig;
|
const gatewayVersion = this.data.gatewayVersion;
|
||||||
|
if (gatewayVersion) {
|
||||||
|
value.configVersion = gatewayVersion;
|
||||||
|
}
|
||||||
|
value.configurationJson = (gatewayVersion
|
||||||
|
? defaultConfig[this.data.gatewayVersion]
|
||||||
|
: defaultConfig.legacy)
|
||||||
|
?? defaultConfig;
|
||||||
if (this.connectorForm.valid) {
|
if (this.connectorForm.valid) {
|
||||||
this.dialogRef.close(value);
|
this.dialogRef.close(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,7 +65,9 @@ import { AddConnectorDialogComponent } from '@home/components/widget/lib/gateway
|
|||||||
import { debounceTime, filter, switchMap, take, takeUntil, tap } from 'rxjs/operators';
|
import { debounceTime, filter, switchMap, take, takeUntil, tap } from 'rxjs/operators';
|
||||||
import { ErrorStateMatcher } from '@angular/material/core';
|
import { ErrorStateMatcher } from '@angular/material/core';
|
||||||
import { PageData } from '@shared/models/page/page-data';
|
import { PageData } from '@shared/models/page/page-data';
|
||||||
import { GatewayConnectorVersionMappingUtil } from './utils/gateway-connector-version-mapping.util';
|
import {
|
||||||
|
GatewayConnectorVersionMappingUtil
|
||||||
|
} from '@home/components/widget/lib/gateway/utils/gateway-connector-version-mapping.util';
|
||||||
|
|
||||||
export class ForceErrorStateMatcher implements ErrorStateMatcher {
|
export class ForceErrorStateMatcher implements ErrorStateMatcher {
|
||||||
isErrorState(control: FormControl | null): boolean {
|
isErrorState(control: FormControl | null): boolean {
|
||||||
@ -247,7 +249,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
|
|||||||
|
|
||||||
value.ts = Date.now();
|
value.ts = Date.now();
|
||||||
|
|
||||||
return GatewayConnectorVersionMappingUtil.getMappedByVersion(value, this.gatewayVersion);
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateData(reload: boolean = false): void {
|
private updateData(reload: boolean = false): void {
|
||||||
@ -741,7 +743,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
|
|||||||
...connector,
|
...connector,
|
||||||
};
|
};
|
||||||
|
|
||||||
connectorState.basicConfig = connectorState.configurationJson;
|
connectorState.basicConfig = GatewayConnectorVersionMappingUtil.getConfig(connectorState, this.gatewayVersion);
|
||||||
this.initialConnector = connectorState;
|
this.initialConnector = connectorState;
|
||||||
this.updateConnector(connectorState);
|
this.updateConnector(connectorState);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -124,21 +124,14 @@ export interface GatewayConnectorBase {
|
|||||||
configVersion?: string;
|
configVersion?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GatewayConnector = CurrentGatewayConnector | LegacyGatewayConnector;
|
export interface GatewayConnector extends GatewayConnectorBase {
|
||||||
|
|
||||||
export interface CurrentGatewayConnector extends GatewayConnectorBase {
|
|
||||||
configurationJson: ConnectorBaseConfig;
|
configurationJson: ConnectorBaseConfig;
|
||||||
basicConfig?: ConnectorBaseConfig;
|
basicConfig?: ConnectorBaseConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LegacyGatewayConnector extends GatewayConnectorBase {
|
|
||||||
configurationJson: ConnectorLegacyBaseConfig;
|
|
||||||
basicConfig?: ConnectorLegacyBaseConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GatewayVersionedDefaultConfig {
|
export interface GatewayVersionedDefaultConfig {
|
||||||
legacy: LegacyGatewayConnector;
|
legacy: GatewayConnector;
|
||||||
current: GatewayConnector;
|
'3.5.1': GatewayConnector;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DataMapping {
|
export interface DataMapping {
|
||||||
@ -201,8 +194,6 @@ export type ConnectorMappingFormValue = DeviceConnectorMapping | RequestMappingF
|
|||||||
|
|
||||||
export type ConnectorBaseConfig = ConnectorBaseInfo | MQTTBasicConfig | OPCBasicConfig | ModbusBasicConfig;
|
export type ConnectorBaseConfig = ConnectorBaseInfo | MQTTBasicConfig | OPCBasicConfig | ModbusBasicConfig;
|
||||||
|
|
||||||
export type ConnectorLegacyBaseConfig = ConnectorBaseInfo | MQTTLegacyBasicConfig;
|
|
||||||
|
|
||||||
export interface ConnectorBaseInfo {
|
export interface ConnectorBaseInfo {
|
||||||
name: string;
|
name: string;
|
||||||
id: string;
|
id: string;
|
||||||
@ -210,7 +201,9 @@ export interface ConnectorBaseInfo {
|
|||||||
logLevel: GatewayLogLevel;
|
logLevel: GatewayLogLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MQTTBasicConfig {
|
export type MQTTBasicConfig = MQTTBasicConfig_v3_5_2 | MQTTLegacyBasicConfig;
|
||||||
|
|
||||||
|
export interface MQTTBasicConfig_v3_5_2 {
|
||||||
mapping: ConverterConnectorMapping[];
|
mapping: ConverterConnectorMapping[];
|
||||||
requestsMapping: Record<RequestType, RequestMappingData[] | RequestMappingValue[]> | RequestMappingData[] | RequestMappingValue[];
|
requestsMapping: Record<RequestType, RequestMappingData[] | RequestMappingValue[]> | RequestMappingData[] | RequestMappingValue[];
|
||||||
broker: BrokerConfig;
|
broker: BrokerConfig;
|
||||||
|
|||||||
@ -15,52 +15,20 @@
|
|||||||
///
|
///
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
ConnectorBaseConfig,
|
||||||
ConnectorType,
|
ConnectorType,
|
||||||
GatewayConnector,
|
GatewayConnector,
|
||||||
LegacyGatewayConnector,
|
|
||||||
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
} from '@home/components/widget/lib/gateway/gateway-widget.models';
|
||||||
import { MqttVersionMappingUtil } from './mqtt-version-mapping.util';
|
import { MqttVersionProcessor } from '@home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract';
|
||||||
|
|
||||||
export class GatewayConnectorVersionMappingUtil {
|
export abstract class GatewayConnectorVersionMappingUtil {
|
||||||
|
|
||||||
static getMappedByVersion(connector: GatewayConnector, gatewayVersion: string): GatewayConnector {
|
static getConfig(connector: GatewayConnector, gatewayVersion: string): ConnectorBaseConfig {
|
||||||
switch(connector.type) {
|
switch(connector.type) {
|
||||||
case ConnectorType.MQTT:
|
case ConnectorType.MQTT:
|
||||||
return this.getMappedMQTTByVersion(connector, gatewayVersion);
|
return new MqttVersionProcessor(gatewayVersion, connector).getProcessedByVersion();
|
||||||
default:
|
default:
|
||||||
return connector;
|
return connector.configurationJson as ConnectorBaseConfig;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getMappedMQTTByVersion(
|
|
||||||
connector: GatewayConnector | LegacyGatewayConnector,
|
|
||||||
gatewayVersion: string
|
|
||||||
): GatewayConnector | LegacyGatewayConnector {
|
|
||||||
if (this.isVersionUpdateNeeded(gatewayVersion, connector.configVersion)) {
|
|
||||||
return this.isGatewayOutdated(gatewayVersion, connector.configVersion)
|
|
||||||
? MqttVersionMappingUtil.getLegacyVersion(connector)
|
|
||||||
: MqttVersionMappingUtil.getNewestVersion(connector);
|
|
||||||
}
|
|
||||||
return connector;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static isGatewayOutdated(gatewayVersion: string, configVersion: string): boolean {
|
|
||||||
if (!gatewayVersion || !configVersion) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.parseVersion(configVersion) > this.parseVersion(gatewayVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static isVersionUpdateNeeded(configVersion: string, gatewayVersion: string): boolean {
|
|
||||||
if (!gatewayVersion || !configVersion) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.parseVersion(configVersion) !== this.parseVersion(gatewayVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static parseVersion(version: string): number {
|
|
||||||
return Number(version?.replace(/\./g, ''));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,21 +14,16 @@
|
|||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
import { deleteNullProperties, isEqual } from '@core/utils';
|
import { deleteNullProperties } from '@core/utils';
|
||||||
import {
|
import {
|
||||||
AttributeRequest,
|
AttributeRequest,
|
||||||
ConnectorDeviceInfo,
|
ConnectorDeviceInfo,
|
||||||
Converter,
|
Converter,
|
||||||
ConverterConnectorMapping,
|
ConverterConnectorMapping,
|
||||||
ConvertorType,
|
ConvertorType,
|
||||||
CurrentGatewayConnector,
|
|
||||||
GatewayConnector,
|
|
||||||
LegacyConverter,
|
LegacyConverter,
|
||||||
LegacyConverterConnectorMapping,
|
LegacyConverterConnectorMapping,
|
||||||
LegacyGatewayConnector,
|
|
||||||
LegacyRequestMappingData,
|
LegacyRequestMappingData,
|
||||||
MQTTBasicConfig,
|
|
||||||
MQTTLegacyBasicConfig,
|
|
||||||
RequestMappingData,
|
RequestMappingData,
|
||||||
RequestType,
|
RequestType,
|
||||||
ServerSideRpc,
|
ServerSideRpc,
|
||||||
@ -44,56 +39,9 @@ export class MqttVersionMappingUtil {
|
|||||||
static readonly mqttRequestMappingNewFields =
|
static readonly mqttRequestMappingNewFields =
|
||||||
['attributeNameExpressionSource', 'responseTopicQoS', 'extensionConfig'];
|
['attributeNameExpressionSource', 'responseTopicQoS', 'extensionConfig'];
|
||||||
|
|
||||||
static getNewestVersion(mqttConnector: GatewayConnector): CurrentGatewayConnector {
|
static mapMappingToUpgradedVersion(
|
||||||
const {
|
mapping: LegacyConverterConnectorMapping[] | ConverterConnectorMapping[]
|
||||||
connectRequests,
|
): ConverterConnectorMapping[] {
|
||||||
disconnectRequests,
|
|
||||||
attributeRequests,
|
|
||||||
attributeUpdates,
|
|
||||||
serverSideRpc
|
|
||||||
} = mqttConnector.configurationJson as MQTTLegacyBasicConfig;
|
|
||||||
let configurationJson = {
|
|
||||||
...mqttConnector.configurationJson,
|
|
||||||
requestsMapping: this.mapRequestsToNewestVersion({
|
|
||||||
connectRequests,
|
|
||||||
disconnectRequests,
|
|
||||||
attributeRequests,
|
|
||||||
attributeUpdates,
|
|
||||||
serverSideRpc
|
|
||||||
}),
|
|
||||||
mapping: this.mapMappingToNewestVersion((mqttConnector.configurationJson as MQTTLegacyBasicConfig).mapping),
|
|
||||||
};
|
|
||||||
|
|
||||||
this.mqttRequestTypeKeys.forEach((key: RequestType) => {
|
|
||||||
const { [key]: removedKey, ...rest } = configurationJson as MQTTLegacyBasicConfig;
|
|
||||||
configurationJson = { ...rest } as any;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.cleanUpConfigJson(configurationJson as MQTTBasicConfig);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...mqttConnector,
|
|
||||||
configurationJson
|
|
||||||
} as CurrentGatewayConnector;
|
|
||||||
}
|
|
||||||
|
|
||||||
static getLegacyVersion(mqttConnector: GatewayConnector): LegacyGatewayConnector {
|
|
||||||
const { requestsMapping, mapping, ...restConfig } = mqttConnector.configurationJson as MQTTBasicConfig;
|
|
||||||
|
|
||||||
const updatedRequestsMapping = this.mapRequestsToLegacyVersion(requestsMapping as Record<RequestType, RequestMappingData[]>);
|
|
||||||
const updatedMapping = this.mapMappingToLegacyVersion(mapping);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...mqttConnector,
|
|
||||||
configurationJson: {
|
|
||||||
...restConfig,
|
|
||||||
...updatedRequestsMapping,
|
|
||||||
mapping: updatedMapping,
|
|
||||||
},
|
|
||||||
} as LegacyGatewayConnector;
|
|
||||||
}
|
|
||||||
|
|
||||||
static mapMappingToNewestVersion(mapping: LegacyConverterConnectorMapping[] | ConverterConnectorMapping[]): ConverterConnectorMapping[] {
|
|
||||||
return mapping?.map(({ converter, topicFilter, subscriptionQos = 1 }) => {
|
return mapping?.map(({ converter, topicFilter, subscriptionQos = 1 }) => {
|
||||||
const deviceInfo = converter.deviceInfo ?? this.extractConverterDeviceInfo(converter);
|
const deviceInfo = converter.deviceInfo ?? this.extractConverterDeviceInfo(converter);
|
||||||
|
|
||||||
@ -109,7 +57,7 @@ export class MqttVersionMappingUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static mapRequestsToNewestVersion(
|
static mapRequestsToUpgradedVersion(
|
||||||
requestMapping: Record<RequestType,
|
requestMapping: Record<RequestType,
|
||||||
RequestMappingData[] | LegacyRequestMappingData[]>
|
RequestMappingData[] | LegacyRequestMappingData[]>
|
||||||
): Record<RequestType, RequestMappingData[]> {
|
): Record<RequestType, RequestMappingData[]> {
|
||||||
@ -119,7 +67,7 @@ export class MqttVersionMappingUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
acc[key] = requestMapping[key].map(value => {
|
acc[key] = requestMapping[key].map(value => {
|
||||||
const newValue = this.mapRequestToNewest(value as LegacyRequestMappingData, key);
|
const newValue = this.mapRequestToUpgradedVersion(value as LegacyRequestMappingData, key);
|
||||||
|
|
||||||
this.cleanUpOldFields(newValue as {});
|
this.cleanUpOldFields(newValue as {});
|
||||||
|
|
||||||
@ -130,7 +78,7 @@ export class MqttVersionMappingUtil {
|
|||||||
}, {}) as Record<RequestType, RequestMappingData[]>;
|
}, {}) as Record<RequestType, RequestMappingData[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
static mapRequestsToLegacyVersion(
|
static mapRequestsToDowngradedVersion(
|
||||||
requestsMapping: Record<RequestType, RequestMappingData[]>
|
requestsMapping: Record<RequestType, RequestMappingData[]>
|
||||||
): Record<RequestType, LegacyRequestMappingData[]> {
|
): Record<RequestType, LegacyRequestMappingData[]> {
|
||||||
return this.mqttRequestTypeKeys.reduce((acc, key) => {
|
return this.mqttRequestTypeKeys.reduce((acc, key) => {
|
||||||
@ -161,11 +109,11 @@ export class MqttVersionMappingUtil {
|
|||||||
}, {}) as Record<RequestType, LegacyRequestMappingData[]>;
|
}, {}) as Record<RequestType, LegacyRequestMappingData[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
static mapMappingToLegacyVersion(
|
static mapMappingToDowngradedVersion(
|
||||||
mapping: ConverterConnectorMapping[]
|
mapping: ConverterConnectorMapping[]
|
||||||
): LegacyConverterConnectorMapping[] {
|
): LegacyConverterConnectorMapping[] {
|
||||||
return mapping?.map((converterMapping: ConverterConnectorMapping) => {
|
return mapping?.map((converterMapping: ConverterConnectorMapping) => {
|
||||||
const converter = this.mapConverterToLegacy(converterMapping.converter);
|
const converter = this.mapConverterToDowngradedVersion(converterMapping.converter);
|
||||||
|
|
||||||
this.cleanUpNewFields(converter as {});
|
this.cleanUpNewFields(converter as {});
|
||||||
|
|
||||||
@ -173,7 +121,7 @@ export class MqttVersionMappingUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static mapConverterToLegacy(converter: Converter): LegacyConverter {
|
private static mapConverterToDowngradedVersion(converter: Converter): LegacyConverter {
|
||||||
const { deviceInfo, ...restConverter } = converter;
|
const { deviceInfo, ...restConverter } = converter;
|
||||||
|
|
||||||
return converter.type !== ConvertorType.BYTES ? {
|
return converter.type !== ConvertorType.BYTES ? {
|
||||||
@ -216,16 +164,6 @@ export class MqttVersionMappingUtil {
|
|||||||
return SourceType.CONST;
|
return SourceType.CONST;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static cleanUpConfigJson(configurationJson: MQTTBasicConfig): void {
|
|
||||||
if (isEqual(configurationJson.requestsMapping, {})) {
|
|
||||||
delete configurationJson.requestsMapping;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isEqual(configurationJson.mapping, [])) {
|
|
||||||
delete configurationJson.mapping;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static extractConverterDeviceInfo(converter: LegacyConverter): ConnectorDeviceInfo {
|
private static extractConverterDeviceInfo(converter: LegacyConverter): ConnectorDeviceInfo {
|
||||||
const deviceNameExpression = converter.deviceNameExpression
|
const deviceNameExpression = converter.deviceNameExpression
|
||||||
|| converter.deviceNameJsonExpression
|
|| converter.deviceNameJsonExpression
|
||||||
@ -250,7 +188,7 @@ export class MqttVersionMappingUtil {
|
|||||||
} : null;
|
} : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static mapRequestToNewest(value, key: RequestType): RequestMappingData {
|
private static mapRequestToUpgradedVersion(value, key: RequestType): RequestMappingData {
|
||||||
const deviceNameExpression = value.deviceNameJsonExpression || value.deviceNameTopicExpression || null;
|
const deviceNameExpression = value.deviceNameJsonExpression || value.deviceNameTopicExpression || null;
|
||||||
const deviceProfileExpression = value.deviceTypeTopicExpression || value.deviceTypeJsonExpression || 'default';
|
const deviceProfileExpression = value.deviceTypeTopicExpression || value.deviceTypeJsonExpression || 'default';
|
||||||
const deviceProfileExpressionSource = deviceProfileExpression ? this.getTypeSourceByValue(deviceProfileExpression) : null;
|
const deviceProfileExpressionSource = deviceProfileExpression ? this.getTypeSourceByValue(deviceProfileExpression) : null;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"current": {
|
"3.5.1": {
|
||||||
"broker": {
|
"broker": {
|
||||||
"host": "127.0.0.1",
|
"host": "127.0.0.1",
|
||||||
"port": 1883,
|
"port": 1883,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user