From 6dad8e21f4a8e66836009802c11194ddab1bb02c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Feb 2023 12:31:53 +0100 Subject: [PATCH] refactor(simulator): add type shorcuts for OCPP configuration keys MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 4 ++-- .../ChargingStationConfigurationUtils.ts | 10 +++++----- src/types/index.ts | 3 ++- src/types/ocpp/1.6/Configuration.ts | 2 +- src/types/ocpp/1.6/Requests.ts | 7 +++++-- src/types/ocpp/2.0/Variables.ts | 8 +++++++- src/types/ocpp/Configuration.ts | 12 +++++++----- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 5d7acc66..27935e12 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -83,7 +83,7 @@ import { type StopTransactionResponse, SupervisionUrlDistribution, SupportedFeatureProfiles, - VendorDefaultParametersKey, + VendorParametersKey, type WSError, WebSocketCloseEventStatusCode, type WsOptions, @@ -803,7 +803,7 @@ export class ChargingStation { } private getSupervisionUrlOcppKey(): string { - return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl; + return this.stationInfo.supervisionUrlOcppKey ?? VendorParametersKey.ConnectionUrl; } private getTemplateFromFile(): ChargingStationTemplate | undefined { diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts index 6b7cfa23..9c025f8e 100644 --- a/src/charging-station/ChargingStationConfigurationUtils.ts +++ b/src/charging-station/ChargingStationConfigurationUtils.ts @@ -1,5 +1,5 @@ import type { ChargingStation } from './internal'; -import type { ConfigurationKey, StandardParametersKey } from '../types'; +import type { ConfigurationKey, ConfigurationKeyType, StandardParametersKey } from '../types'; import { logger } from '../utils'; type ConfigurationKeyOptions = { readonly?: boolean; visible?: boolean; reboot?: boolean }; @@ -13,7 +13,7 @@ export class ChargingStationConfigurationUtils { public static getConfigurationKey( chargingStation: ChargingStation, - key: string | StandardParametersKey, + key: ConfigurationKeyType, caseInsensitive = false ): ConfigurationKey | undefined { return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => { @@ -26,7 +26,7 @@ export class ChargingStationConfigurationUtils { public static addConfigurationKey( chargingStation: ChargingStation, - key: string | StandardParametersKey, + key: ConfigurationKeyType, value: string, options: ConfigurationKeyOptions = { readonly: false, @@ -65,7 +65,7 @@ export class ChargingStationConfigurationUtils { public static setConfigurationKeyValue( chargingStation: ChargingStation, - key: string | StandardParametersKey, + key: ConfigurationKeyType, value: string, caseInsensitive = false ): void { @@ -89,7 +89,7 @@ export class ChargingStationConfigurationUtils { public static deleteConfigurationKey( chargingStation: ChargingStation, - key: string | StandardParametersKey, + key: ConfigurationKeyType, params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false } ): ConfigurationKey[] | undefined { const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( diff --git a/src/types/index.ts b/src/types/index.ts index 463a7567..406cbd00 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -42,6 +42,7 @@ export { ClearChargingProfileStatus, type ConfigurationData, type ConfigurationKey, + type ConfigurationKeyType, ConfigurationStatus, ConnectorPhaseRotation, ConnectorStatus, @@ -197,7 +198,7 @@ export { type UnlockConnectorRequest, type UnlockConnectorResponse, UnlockStatus, - VendorDefaultParametersKey, + VendorParametersKey, Voltage, type WSError, WebSocketCloseEventStatusCode, diff --git a/src/types/ocpp/1.6/Configuration.ts b/src/types/ocpp/1.6/Configuration.ts index 55528099..1bd92376 100644 --- a/src/types/ocpp/1.6/Configuration.ts +++ b/src/types/ocpp/1.6/Configuration.ts @@ -54,6 +54,6 @@ export enum OCPP16StandardParametersKey { MaxChargingProfilesInstalled = 'MaxChargingProfilesInstalled', } -export enum OCPP16VendorDefaultParametersKey { +export enum OCPP16VendorParametersKey { ConnectionUrl = 'ConnectionUrl', } diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts index 6b244d04..bc451898 100644 --- a/src/types/ocpp/1.6/Requests.ts +++ b/src/types/ocpp/1.6/Requests.ts @@ -7,6 +7,7 @@ import type { OCPP16ChargingProfilePurposeType, OCPP16DiagnosticsStatus, OCPP16StandardParametersKey, + OCPP16VendorParametersKey, } from '../../internal'; export enum OCPP16RequestCommand { @@ -65,8 +66,10 @@ export interface OCPP16StatusNotificationRequest extends JsonObject { export type OCPP16ClearCacheRequest = EmptyObject; +type OCPP16ConfigurationKey = string | OCPP16StandardParametersKey | OCPP16VendorParametersKey; + export interface ChangeConfigurationRequest extends JsonObject { - key: string | OCPP16StandardParametersKey; + key: OCPP16ConfigurationKey; value: string; } @@ -85,7 +88,7 @@ export interface UnlockConnectorRequest extends JsonObject { } export interface GetConfigurationRequest extends JsonObject { - key?: (string | OCPP16StandardParametersKey)[]; + key?: OCPP16ConfigurationKey[]; } enum ResetType { diff --git a/src/types/ocpp/2.0/Variables.ts b/src/types/ocpp/2.0/Variables.ts index 9de1dcc8..0b089567 100644 --- a/src/types/ocpp/2.0/Variables.ts +++ b/src/types/ocpp/2.0/Variables.ts @@ -74,8 +74,14 @@ type ComponentType = { evse?: EVSEType; } & JsonObject; +type VariableName = + | string + | OCPP20RequiredVariableName + | OCPP20OptionalVariableName + | OCPP20VendorVariableName; + type VariableType = { - name: string | OCPP20RequiredVariableName | OCPP20OptionalVariableName | OCPP20VendorVariableName; + name: VariableName; instance?: string; } & JsonObject; diff --git a/src/types/ocpp/Configuration.ts b/src/types/ocpp/Configuration.ts index 297b403c..39739a8f 100644 --- a/src/types/ocpp/Configuration.ts +++ b/src/types/ocpp/Configuration.ts @@ -2,7 +2,7 @@ import { type JsonObject, OCPP16StandardParametersKey, OCPP16SupportedFeatureProfiles, - OCPP16VendorDefaultParametersKey, + OCPP16VendorParametersKey, OCPP20OptionalVariableName, OCPP20RequiredVariableName, OCPP20VendorVariableName, @@ -15,11 +15,11 @@ export const StandardParametersKey = { } as const; export type StandardParametersKey = OCPP16StandardParametersKey; -export const VendorDefaultParametersKey = { - ...OCPP16VendorDefaultParametersKey, +export const VendorParametersKey = { + ...OCPP16VendorParametersKey, ...OCPP20VendorVariableName, } as const; -export type VendorDefaultParametersKey = OCPP16VendorDefaultParametersKey; +export type VendorParametersKey = OCPP16VendorParametersKey; export const SupportedFeatureProfiles = { ...OCPP16SupportedFeatureProfiles, @@ -37,8 +37,10 @@ export enum ConnectorPhaseRotation { TSR = 'TSR', } +export type ConfigurationKeyType = string | StandardParametersKey | VendorParametersKey; + export type OCPPConfigurationKey = { - key: string | StandardParametersKey; + key: ConfigurationKeyType; readonly: boolean; value?: string; } & JsonObject; -- 2.34.1