From f4b3f35d653138a946d74d5f3c313275ee9c03b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 7 Mar 2024 17:12:55 +0100 Subject: [PATCH] refactor: refine type definitions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/types/AutomaticTransactionGenerator.ts | 4 ++- src/types/ChargingStationOcppConfiguration.ts | 5 +-- src/types/ChargingStationTemplate.ts | 5 +-- .../MeasurandPerPhaseSampledValueTemplates.ts | 2 +- src/types/Statistics.ts | 4 +-- src/types/ocpp/1.6/Requests.ts | 4 +-- src/types/ocpp/2.0/Common.ts | 20 +++++------ src/types/ocpp/2.0/Requests.ts | 24 ++++++------- src/types/ocpp/2.0/Responses.ts | 20 +++++------ src/types/ocpp/2.0/Variables.ts | 20 +++++------ src/types/ocpp/Configuration.ts | 4 +-- .../actions/AddChargingStations.vue | 2 +- ui/web/src/main.ts | 8 +++-- ui/web/src/shims-vue.d.ts | 6 ++-- ui/web/src/types/ChargingStationType.ts | 26 +++++++------- ui/web/src/types/ConfigurationType.ts | 4 +-- ui/web/src/views/ChargingStationsView.vue | 36 ++++++++++++------- 17 files changed, 105 insertions(+), 89 deletions(-) diff --git a/src/types/AutomaticTransactionGenerator.ts b/src/types/AutomaticTransactionGenerator.ts index 1592c550..2ff04c09 100644 --- a/src/types/AutomaticTransactionGenerator.ts +++ b/src/types/AutomaticTransactionGenerator.ts @@ -1,10 +1,12 @@ +import type { JsonObject } from './JsonType.js' + export enum IdTagDistribution { RANDOM = 'random', ROUND_ROBIN = 'round-robin', CONNECTOR_AFFINITY = 'connector-affinity' } -export interface AutomaticTransactionGeneratorConfiguration { +export interface AutomaticTransactionGeneratorConfiguration extends JsonObject { enable: boolean minDuration: number maxDuration: number diff --git a/src/types/ChargingStationOcppConfiguration.ts b/src/types/ChargingStationOcppConfiguration.ts index 2ea2c53e..fdf03a40 100644 --- a/src/types/ChargingStationOcppConfiguration.ts +++ b/src/types/ChargingStationOcppConfiguration.ts @@ -1,10 +1,11 @@ +import type { JsonObject } from './JsonType.js' import type { OCPPConfigurationKey } from './ocpp/Configuration.js' -export type ConfigurationKey = OCPPConfigurationKey & { +export interface ConfigurationKey extends OCPPConfigurationKey { visible?: boolean reboot?: boolean } -export interface ChargingStationOcppConfiguration { +export interface ChargingStationOcppConfiguration extends JsonObject { configurationKey?: ConfigurationKey[] } diff --git a/src/types/ChargingStationTemplate.ts b/src/types/ChargingStationTemplate.ts index d17fd58f..d20dfdc1 100644 --- a/src/types/ChargingStationTemplate.ts +++ b/src/types/ChargingStationTemplate.ts @@ -6,6 +6,7 @@ import type { AutomaticTransactionGeneratorConfiguration } from './AutomaticTran import type { ChargingStationOcppConfiguration } from './ChargingStationOcppConfiguration.js' import type { ConnectorStatus } from './ConnectorStatus.js' import type { EvseTemplate } from './Evse.js' +import type { JsonObject } from './JsonType.js' import type { OCPPProtocol } from './ocpp/OCPPProtocol.js' import type { OCPPVersion } from './ocpp/OCPPVersion.js' import type { @@ -41,7 +42,7 @@ export enum Voltage { export type WsOptions = ClientOptions & ClientRequestArgs -export interface FirmwareUpgrade { +export interface FirmwareUpgrade extends JsonObject { versionUpgrade?: { patternGroup?: number step?: number @@ -50,7 +51,7 @@ export interface FirmwareUpgrade { failureStatus?: FirmwareStatus } -interface CommandsSupport { +interface CommandsSupport extends JsonObject { incomingCommands: Record outgoingCommands?: Record } diff --git a/src/types/MeasurandPerPhaseSampledValueTemplates.ts b/src/types/MeasurandPerPhaseSampledValueTemplates.ts index 5da56b48..99572673 100644 --- a/src/types/MeasurandPerPhaseSampledValueTemplates.ts +++ b/src/types/MeasurandPerPhaseSampledValueTemplates.ts @@ -1,6 +1,6 @@ import type { SampledValue } from './ocpp/MeterValues.js' -export type SampledValueTemplate = SampledValue & { +export interface SampledValueTemplate extends SampledValue { fluctuationPercent?: number minimumValue?: number } diff --git a/src/types/Statistics.ts b/src/types/Statistics.ts index 0c7375d9..ecb3c2bf 100644 --- a/src/types/Statistics.ts +++ b/src/types/Statistics.ts @@ -23,14 +23,14 @@ export type StatisticsData = Partial<{ stdDevTimeMeasurement: number }> -export type Statistics = { +export interface Statistics extends WorkerData { id: string name: string uri: string createdAt: Date updatedAt?: Date statisticsData: Map -} & WorkerData +} export interface InternalTemplateStatistics { configured: number diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts index 32d9757f..26dd03f7 100644 --- a/src/types/ocpp/1.6/Requests.ts +++ b/src/types/ocpp/1.6/Requests.ts @@ -148,9 +148,9 @@ export enum OCPP16FirmwareStatus { Installed = 'Installed' } -export type OCPP16FirmwareStatusNotificationRequest = { +export interface OCPP16FirmwareStatusNotificationRequest extends JsonObject { status: OCPP16FirmwareStatus -} & JsonObject +} export interface GetDiagnosticsRequest extends JsonObject { location: string diff --git a/src/types/ocpp/2.0/Common.ts b/src/types/ocpp/2.0/Common.ts index 9d13b2a8..785c6046 100644 --- a/src/types/ocpp/2.0/Common.ts +++ b/src/types/ocpp/2.0/Common.ts @@ -94,33 +94,33 @@ export enum CertificateSigningUseEnumType { export type CertificateSignedStatusEnumType = GenericStatusEnumType -export type CertificateHashDataType = { +export interface CertificateHashDataType extends JsonObject { hashAlgorithm: HashAlgorithmEnumType issuerNameHash: string issuerKeyHash: string serialNumber: string -} & JsonObject +} -export type CertificateHashDataChainType = { +export interface CertificateHashDataChainType extends JsonObject { certificateType: GetCertificateIdUseEnumType certificateHashData: CertificateHashDataType childCertificateHashData?: CertificateHashDataType -} & JsonObject +} -export type OCSPRequestDataType = { +export interface OCSPRequestDataType extends JsonObject { hashAlgorithm: HashAlgorithmEnumType issuerNameHash: string issuerKeyHash: string serialNumber: string responderURL: string -} & JsonObject +} -export type StatusInfoType = { +export interface StatusInfoType extends JsonObject { reasonCode: string additionalInfo?: string -} & JsonObject +} -export type EVSEType = { +export interface EVSEType extends JsonObject { id: number connectorId?: string -} & JsonObject +} diff --git a/src/types/ocpp/2.0/Requests.ts b/src/types/ocpp/2.0/Requests.ts index ef7a5161..d2c87287 100644 --- a/src/types/ocpp/2.0/Requests.ts +++ b/src/types/ocpp/2.0/Requests.ts @@ -19,40 +19,40 @@ export enum OCPP20IncomingRequestCommand { REQUEST_STOP_TRANSACTION = 'RequestStopTransaction' } -type ModemType = { +interface ModemType extends JsonObject { iccid?: string imsi?: string -} & JsonObject +} -type ChargingStationType = { +interface ChargingStationType extends JsonObject { serialNumber?: string model: string vendorName: string firmwareVersion?: string modem?: ModemType -} & JsonObject +} -export type OCPP20BootNotificationRequest = { +export interface OCPP20BootNotificationRequest extends JsonObject { reason: BootReasonEnumType chargingStation: ChargingStationType -} & JsonObject +} export type OCPP20HeartbeatRequest = EmptyObject export type OCPP20ClearCacheRequest = EmptyObject -export type OCPP20StatusNotificationRequest = { +export interface OCPP20StatusNotificationRequest extends JsonObject { timestamp: Date connectorStatus: OCPP20ConnectorStatusEnumType evseId: number connectorId: number -} & JsonObject +} -export type OCPP20SetVariablesRequest = { +export interface OCPP20SetVariablesRequest extends JsonObject { setVariableData: OCPP20SetVariableDataType[] -} & JsonObject +} -export type OCPP20InstallCertificateRequest = { +export interface OCPP20InstallCertificateRequest extends JsonObject { certificateType: InstallCertificateUseEnumType certificate: string -} & JsonObject +} diff --git a/src/types/ocpp/2.0/Responses.ts b/src/types/ocpp/2.0/Responses.ts index dbeb1e3f..51d6ad44 100644 --- a/src/types/ocpp/2.0/Responses.ts +++ b/src/types/ocpp/2.0/Responses.ts @@ -8,29 +8,29 @@ import type { EmptyObject } from '../../EmptyObject.js' import type { JsonObject } from '../../JsonType.js' import type { RegistrationStatusEnumType } from '../Common.js' -export type OCPP20BootNotificationResponse = { +export interface OCPP20BootNotificationResponse extends JsonObject { currentTime: Date status: RegistrationStatusEnumType interval: number statusInfo?: StatusInfoType -} & JsonObject +} -export type OCPP20HeartbeatResponse = { +export interface OCPP20HeartbeatResponse extends JsonObject { currentTime: Date -} & JsonObject +} -export type OCPP20ClearCacheResponse = { +export interface OCPP20ClearCacheResponse extends JsonObject { status: GenericStatusEnumType statusInfo?: StatusInfoType -} & JsonObject +} export type OCPP20StatusNotificationResponse = EmptyObject -export type OCPP20SetVariablesResponse = { +export interface OCPP20SetVariablesResponse extends JsonObject { setVariableResult: OCPP20SetVariableResultType[] -} & JsonObject +} -export type OCPP20InstallCertificateResponse = { +export interface OCPP20InstallCertificateResponse extends JsonObject { status: InstallCertificateStatusEnumType statusInfo?: StatusInfoType -} & JsonObject +} diff --git a/src/types/ocpp/2.0/Variables.ts b/src/types/ocpp/2.0/Variables.ts index b5cd53e5..439abf35 100644 --- a/src/types/ocpp/2.0/Variables.ts +++ b/src/types/ocpp/2.0/Variables.ts @@ -69,11 +69,11 @@ enum AttributeEnumType { MaxSet = 'MaxSet' } -type ComponentType = { +interface ComponentType extends JsonObject { name: string | OCPP20ComponentName instance?: string evse?: EVSEType -} & JsonObject +} type VariableName = | string @@ -81,17 +81,17 @@ type VariableName = | OCPP20OptionalVariableName | OCPP20VendorVariableName -type VariableType = { +interface VariableType extends JsonObject { name: VariableName instance?: string -} & JsonObject +} -export type OCPP20SetVariableDataType = { +export interface OCPP20SetVariableDataType extends JsonObject { attributeType?: AttributeEnumType attributeValue: string component: ComponentType variable: VariableType -} & JsonObject +} enum SetVariableStatusEnumType { Accepted = 'Accepted', @@ -102,15 +102,15 @@ enum SetVariableStatusEnumType { RebootRequired = 'RebootRequired' } -export type OCPP20SetVariableResultType = { +export interface OCPP20SetVariableResultType extends JsonObject { attributeType?: AttributeEnumType attributeStatus: SetVariableStatusEnumType component: ComponentType variable: VariableType attributeStatusInfo?: StatusInfoType -} & JsonObject +} -export type OCPP20ComponentVariableType = { +export interface OCPP20ComponentVariableType extends JsonObject { component: ComponentType variable?: VariableType -} & JsonObject +} diff --git a/src/types/ocpp/Configuration.ts b/src/types/ocpp/Configuration.ts index ac8a0945..c1742348 100644 --- a/src/types/ocpp/Configuration.ts +++ b/src/types/ocpp/Configuration.ts @@ -44,8 +44,8 @@ export enum ConnectorPhaseRotation { export type ConfigurationKeyType = string | StandardParametersKey | VendorParametersKey -export type OCPPConfigurationKey = { +export interface OCPPConfigurationKey extends JsonObject { key: ConfigurationKeyType readonly: boolean value?: string -} & JsonObject +} diff --git a/ui/web/src/components/actions/AddChargingStations.vue b/ui/web/src/components/actions/AddChargingStations.vue index f638232b..6e3c2932 100644 --- a/ui/web/src/components/actions/AddChargingStations.vue +++ b/ui/web/src/components/actions/AddChargingStations.vue @@ -118,7 +118,7 @@ const state = ref<{ enableStatistics: false }) -watch(getCurrentInstance()?.appContext.config.globalProperties.$templates, () => { +watch(getCurrentInstance()!.appContext.config.globalProperties.$templates, () => { state.value.renderTemplates = randomUUID() }) diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index 640adf9f..f69f2c46 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,6 +1,6 @@ import { type App as AppType, createApp, ref } from 'vue' import ToastPlugin from 'vue-toast-notification' -import type { ChargingStationData, ConfigurationData } from '@/types' +import type { ChargingStationData, ConfigurationData, UIServerConfigurationSection } from '@/types' import { router } from '@/router' import { UIClient, getFromLocalStorage, setToLocalStorage } from '@/composables' import App from '@/App.vue' @@ -30,13 +30,15 @@ const initializeApp = (app: AppType, config: ConfigurationData) => { if ( getFromLocalStorage('uiServerConfigurationIndex', undefined) == null || getFromLocalStorage('uiServerConfigurationIndex', 0) > - app.config.globalProperties.$configuration.value.uiServer.length - 1 + (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[]) + .length - + 1 ) { setToLocalStorage('uiServerConfigurationIndex', 0) } if (app.config.globalProperties.$uiClient == null) { app.config.globalProperties.$uiClient = UIClient.getInstance( - app.config.globalProperties.$configuration.value.uiServer[ + (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])[ getFromLocalStorage('uiServerConfigurationIndex', 0) ] ) diff --git a/ui/web/src/shims-vue.d.ts b/ui/web/src/shims-vue.d.ts index 1b934bff..6f0d76e3 100644 --- a/ui/web/src/shims-vue.d.ts +++ b/ui/web/src/shims-vue.d.ts @@ -6,9 +6,9 @@ declare module 'vue' { RouterView: (typeof import('vue-router'))['RouterView'] } interface ComponentCustomProperties { - $configuration: Ref - $templates: Ref - $chargingStations: Ref + $configuration: import('vue').Ref + $templates: import('vue').Ref + $chargingStations: import('vue').Ref $uiClient: import('@/composables').UIClient } } diff --git a/ui/web/src/types/ChargingStationType.ts b/ui/web/src/types/ChargingStationType.ts index 6814737e..167e3a1a 100644 --- a/ui/web/src/types/ChargingStationType.ts +++ b/ui/web/src/types/ChargingStationType.ts @@ -6,7 +6,7 @@ export enum IdTagDistribution { CONNECTOR_AFFINITY = 'connector-affinity' } -export interface AutomaticTransactionGeneratorConfiguration { +export interface AutomaticTransactionGeneratorConfiguration extends JsonObject { enable: boolean minDuration: number maxDuration: number @@ -19,12 +19,12 @@ export interface AutomaticTransactionGeneratorConfiguration { idTagDistribution?: IdTagDistribution } -export interface ChargingStationAutomaticTransactionGeneratorConfiguration { +export interface ChargingStationAutomaticTransactionGeneratorConfiguration extends JsonObject { automaticTransactionGenerator?: AutomaticTransactionGeneratorConfiguration automaticTransactionGeneratorStatuses?: Status[] } -export type ChargingStationData = { +export interface ChargingStationData extends JsonObject { started: boolean stationInfo: ChargingStationInfo connectors: ConnectorStatus[] @@ -50,7 +50,7 @@ export enum OCPP16FirmwareStatus { Installed = 'Installed' } -export interface FirmwareUpgrade { +export interface FirmwareUpgrade extends JsonObject { versionUpgrade?: { patternGroup?: number step?: number @@ -74,7 +74,7 @@ export interface ChargingStationOptions extends JsonObject { stopTransactionsOnStopped?: boolean } -export type ChargingStationInfo = { +export interface ChargingStationInfo extends JsonObject { hashId: string templateIndex: number templateName: string @@ -137,20 +137,20 @@ export type ChargingStationInfo = { messageTriggerSupport?: Record } -export interface ChargingStationOcppConfiguration { +export interface ChargingStationOcppConfiguration extends JsonObject { configurationKey?: ConfigurationKey[] } -export type ConfigurationKey = OCPPConfigurationKey & { +export interface ConfigurationKey extends OCPPConfigurationKey { visible?: boolean reboot?: boolean } -export type OCPPConfigurationKey = { +export interface OCPPConfigurationKey extends JsonObject { key: string readonly: boolean value?: string -} & JsonObject +} export enum OCPP16IncomingRequestCommand { RESET = 'Reset', @@ -216,7 +216,7 @@ export const MessageTrigger = { } as const export type MessageTrigger = OCPP16MessageTrigger -type CommandsSupport = { +interface CommandsSupport extends JsonObject { incomingCommands: Record outgoingCommands?: Record } @@ -250,7 +250,7 @@ export enum AmpereUnits { AMPERE = 'A' } -export type ConnectorStatus = { +export interface ConnectorStatus extends JsonObject { availability: AvailabilityType bootStatus?: ChargePointStatus status?: ChargePointStatus @@ -266,7 +266,7 @@ export type ConnectorStatus = { transactionEnergyActiveImportRegisterValue?: number // In Wh } -export type EvseStatus = { +export interface EvseStatus extends JsonObject { availability: AvailabilityType connectors?: ConnectorStatus[] } @@ -291,7 +291,7 @@ export enum OCPP16ChargePointStatus { } export type ChargePointStatus = OCPP16ChargePointStatus -export type Status = { +export interface Status extends JsonObject { start?: boolean startDate?: Date lastRunDate?: Date diff --git a/ui/web/src/types/ConfigurationType.ts b/ui/web/src/types/ConfigurationType.ts index 18ea267a..1c8de85b 100644 --- a/ui/web/src/types/ConfigurationType.ts +++ b/ui/web/src/types/ConfigurationType.ts @@ -1,10 +1,10 @@ import type { AuthenticationType, Protocol, ProtocolVersion } from './UIProtocol' -export type ConfigurationData = { +export interface ConfigurationData { uiServer: UIServerConfigurationSection | UIServerConfigurationSection[] } -export type UIServerConfigurationSection = { +export interface UIServerConfigurationSection { name?: string host: string port: number diff --git a/ui/web/src/views/ChargingStationsView.vue b/ui/web/src/views/ChargingStationsView.vue index 320731f7..56200d2e 100644 --- a/ui/web/src/views/ChargingStationsView.vue +++ b/ui/web/src/views/ChargingStationsView.vue @@ -13,7 +13,11 @@ if ( getFromLocalStorage('uiServerConfigurationIndex', 0) !== state.uiServerIndex ) { - $uiClient.setConfiguration($configuration.value.uiServer[state.uiServerIndex]) + $uiClient.setConfiguration( + ($configuration.value.uiServer as UIServerConfigurationSection[])[ + state.uiServerIndex + ] + ) registerWSEventListeners() $uiClient.registerWSEventListener( 'open', @@ -33,7 +37,7 @@ 0 ) $uiClient.setConfiguration( - $configuration.value.uiServer[ + ($configuration.value.uiServer as UIServerConfigurationSection[])[ getFromLocalStorage('uiServerConfigurationIndex', 0) ] ) @@ -111,7 +115,12 @@ import { computed, getCurrentInstance, onMounted, onUnmounted, ref, watch } from 'vue' import { useToast } from 'vue-toast-notification' import CSTable from '@/components/charging-stations/CSTable.vue' -import type { ResponsePayload, SimulatorState, UIServerConfigurationSection } from '@/types' +import type { + ChargingStationData, + ResponsePayload, + SimulatorState, + UIServerConfigurationSection +} from '@/types' import Container from '@/components/Container.vue' import ReloadButton from '@/components/buttons/ReloadButton.vue' import { @@ -164,7 +173,7 @@ const clearToggleButtons = (): void => { const app = getCurrentInstance() -watch(app?.appContext.config.globalProperties.$chargingStations, () => { +watch(app!.appContext.config.globalProperties.$chargingStations, () => { state.value.renderChargingStations = randomUUID() }) @@ -213,7 +222,7 @@ const getTemplates = (): void => { .listTemplates() .then((response: ResponsePayload) => { if (app != null) { - app.appContext.config.globalProperties.$templates.value = response.templates + app.appContext.config.globalProperties.$templates.value = response.templates as string[] } }) .catch((error: Error) => { @@ -234,7 +243,8 @@ const getChargingStations = (): void => { .listChargingStations() .then((response: ResponsePayload) => { if (app != null) { - app.appContext.config.globalProperties.$chargingStations.value = response.chargingStations + app.appContext.config.globalProperties.$chargingStations.value = + response.chargingStations as ChargingStationData[] } }) .catch((error: Error) => { @@ -274,13 +284,13 @@ onUnmounted(() => { unregisterWSEventListeners() }) -const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = - app?.appContext.config.globalProperties.$configuration.value.uiServer.map( - (configuration: UIServerConfigurationSection, index: number) => ({ - index, - configuration - }) - ) +const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = ( + app?.appContext.config.globalProperties.$configuration.value + .uiServer as UIServerConfigurationSection[] +).map((configuration: UIServerConfigurationSection, index: number) => ({ + index, + configuration +})) const startSimulator = (): void => { uiClient -- 2.34.1