From 618d82b7ac0385babd566ae97fcb9b8032e4c841 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Mon, 9 Sep 2024 17:42:46 +0300 Subject: [PATCH] Changed approach to GatewayConnector generic --- ...ay-connector-version-processor.abstract.ts | 14 +++++------ .../mqtt-version-processor.abstract.ts | 24 ++++++++++++------- .../models/gateway-configuration.models.ts | 4 +++- .../gateway/gateway-connectors.component.ts | 6 ++--- .../lib/gateway/gateway-widget.models.ts | 16 ++++++++----- .../gateway-connector-version-mapping.util.ts | 8 +++---- 6 files changed, 43 insertions(+), 29 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/gateway-connector-version-processor.abstract.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/gateway-connector-version-processor.abstract.ts index 31b8ba8cf8..923cf1920b 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/gateway-connector-version-processor.abstract.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/gateway-connector-version-processor.abstract.ts @@ -4,23 +4,23 @@ export abstract class GatewayConnectorVersionProcessor { gatewayVersion: number; configVersion: number; - protected constructor(protected gatewayVersionStr: string, protected connector: GatewayConnector) { + protected constructor(protected gatewayVersionStr: string, protected connector: GatewayConnector) { this.gatewayVersion = this.parseVersion(this.gatewayVersionStr); this.configVersion = this.parseVersion(connector.configVersion); } - getProcessedByVersion(): BasicConfig { + getProcessedByVersion(): GatewayConnector { if (this.isVersionUpdateNeeded()) { - return this.configVersion < this.gatewayVersion + return !this.configVersion || this.configVersion < this.gatewayVersion ? this.getUpgradedVersion() : this.getDowngradedVersion(); } - return this.connector.configurationJson as unknown as BasicConfig; + return this.connector; } private isVersionUpdateNeeded(): boolean { - if (!this.gatewayVersion || !this.configVersion) { + if (!this.gatewayVersion) { return false; } @@ -31,6 +31,6 @@ export abstract class GatewayConnectorVersionProcessor { return Number(version?.replace(/\./g, '')); } - abstract getDowngradedVersion(): BasicConfig; - abstract getUpgradedVersion(): BasicConfig; + abstract getDowngradedVersion(): GatewayConnector; + abstract getUpgradedVersion(): GatewayConnector; } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract.ts index 875b238d7e..99591130a5 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract.ts @@ -15,11 +15,11 @@ export class MqttVersionProcessor extends GatewayConnectorVersionProcessor ) { super(gatewayVersionStr, connector); } - getUpgradedVersion(): MQTTBasicConfig_v3_5_2 { + getUpgradedVersion(): GatewayConnector { const { connectRequests, disconnectRequests, @@ -46,10 +46,14 @@ export class MqttVersionProcessor extends GatewayConnectorVersionProcessor; } - getDowngradedVersion(): MQTTLegacyBasicConfig { + getDowngradedVersion(): GatewayConnector { const { requestsMapping, mapping, ...restConfig } = this.connector.configurationJson as MQTTBasicConfig_v3_5_2; const updatedRequestsMapping = @@ -57,10 +61,14 @@ export class MqttVersionProcessor extends GatewayConnectorVersionProcessor; } private cleanUpConfigJson(configurationJson: MQTTBasicConfig_v3_5_2): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/configuration/models/gateway-configuration.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/configuration/models/gateway-configuration.models.ts index a23d8e7900..bfdd46997e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/configuration/models/gateway-configuration.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/configuration/models/gateway-configuration.models.ts @@ -16,7 +16,9 @@ import { ConfigurationModes, - GatewayConnector, LocalLogsConfigs, LogSavingPeriod, + GatewayConnector, + LocalLogsConfigs, + LogSavingPeriod, SecurityTypes, StorageTypes } from '@home/components/widget/lib/gateway/gateway-widget.models'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts index b6e2dfc093..cbaeed8888 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts @@ -732,14 +732,14 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie this.connectorForm.enable(); } - const connectorState = { + const connectorState = GatewayConnectorVersionMappingUtil.getConfig({ configuration: '', key: 'auto', configurationJson: {} as ConnectorBaseConfig, ...connector, - }; + }, this.gatewayVersion); - connectorState.basicConfig = GatewayConnectorVersionMappingUtil.getConfig(connectorState, this.gatewayVersion); + connectorState.basicConfig = connectorState.configurationJson; this.initialConnector = connectorState; this.updateConnector(connectorState); } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts index 38040f15d3..94d8ce7d2a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts @@ -124,14 +124,14 @@ export interface GatewayConnectorBase { configVersion?: string; } -export interface GatewayConnector extends GatewayConnectorBase { - configurationJson: ConnectorBaseConfig; - basicConfig?: ConnectorBaseConfig; +export interface GatewayConnector extends GatewayConnectorBase { + configurationJson: BaseConfig; + basicConfig?: BaseConfig; } export interface GatewayVersionedDefaultConfig { - legacy: GatewayConnector; - '3.5.1': GatewayConnector; + legacy: GatewayConnector; + '3.5.1': GatewayConnector; } export interface DataMapping { @@ -192,7 +192,11 @@ export type ConnectorMapping = DeviceConnectorMapping | RequestMappingValue | Co export type ConnectorMappingFormValue = DeviceConnectorMapping | RequestMappingFormValue | ConverterMappingFormValue; -export type ConnectorBaseConfig = ConnectorBaseInfo | MQTTBasicConfig | OPCBasicConfig | ModbusBasicConfig; +export type ConnectorBaseConfig = ConnectorBaseConfig_v_3_5_2 | ConnectorLegacyConfig; + +export type ConnectorLegacyConfig = ConnectorBaseInfo | MQTTLegacyBasicConfig | OPCBasicConfig | ModbusBasicConfig; + +export type ConnectorBaseConfig_v_3_5_2 = ConnectorBaseInfo | MQTTBasicConfig_v3_5_2; export interface ConnectorBaseInfo { name: string; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/utils/gateway-connector-version-mapping.util.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/utils/gateway-connector-version-mapping.util.ts index ab9e393fee..9de53e506d 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/utils/gateway-connector-version-mapping.util.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/utils/gateway-connector-version-mapping.util.ts @@ -17,18 +17,18 @@ import { ConnectorBaseConfig, ConnectorType, - GatewayConnector, + GatewayConnector, MQTTBasicConfig, } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { MqttVersionProcessor } from '@home/components/widget/lib/gateway/abstract/mqtt-version-processor.abstract'; export abstract class GatewayConnectorVersionMappingUtil { - static getConfig(connector: GatewayConnector, gatewayVersion: string): ConnectorBaseConfig { + static getConfig(connector: GatewayConnector, gatewayVersion: string): GatewayConnector { switch(connector.type) { case ConnectorType.MQTT: - return new MqttVersionProcessor(gatewayVersion, connector).getProcessedByVersion(); + return new MqttVersionProcessor(gatewayVersion, connector as GatewayConnector).getProcessedByVersion(); default: - return connector.configurationJson as ConnectorBaseConfig; + return connector; } } }