From: Jérôme Benoit Date: Sun, 16 Jul 2023 15:31:53 +0000 (+0200) Subject: refactor: cleanup null checks and helpers X-Git-Tag: v1.2.18~25 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f568f36861d62d759a95b17fbe10380eca88a71b;p=e-mobility-charging-stations-simulator.git refactor: cleanup null checks and helpers Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts index 52972a2a..b56411e3 100644 --- a/src/charging-station/ChargingStationConfigurationUtils.ts +++ b/src/charging-station/ChargingStationConfigurationUtils.ts @@ -89,9 +89,9 @@ export class ChargingStationConfigurationUtils { key, caseInsensitive, ); - if (keyFound && chargingStation.ocppConfiguration?.configurationKey) { - chargingStation.ocppConfiguration.configurationKey[ - chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound) + if (keyFound) { + chargingStation.ocppConfiguration!.configurationKey![ + chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) ].value = value; chargingStation.saveOcppConfiguration(); } else { diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 4fdfe230..0fae41f7 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -565,7 +565,6 @@ const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplat ? getMaxNumberOfConnectors(stationTemplate.Connectors) - 1 : getMaxNumberOfConnectors(stationTemplate.Connectors); } else if (stationTemplate.Evses && !stationTemplate.Connectors) { - configuredMaxConnectors = 0; for (const evse in stationTemplate.Evses) { if (evse === '0') { continue; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index b151dc5a..6e583095 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -472,11 +472,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ): GetConfigurationResponse { const configurationKey: OCPPConfigurationKey[] = []; const unknownKey: string[] = []; - if ( - chargingStation.ocppConfiguration?.configurationKey && - isUndefined(commandPayload.key) === true - ) { - for (const configuration of chargingStation.ocppConfiguration.configurationKey) { + if (isUndefined(commandPayload.key) === true) { + for (const configuration of chargingStation.ocppConfiguration!.configurationKey!) { if (isUndefined(configuration.visible) === true) { configuration.visible = true; } @@ -489,8 +486,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { value: configuration.value, }); } - } else if (commandPayload.key && isNotEmptyArray(commandPayload.key) === true) { - for (const key of commandPayload.key) { + } else if (isNotEmptyArray(commandPayload.key) === true) { + for (const key of commandPayload.key!) { const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, key, diff --git a/ui/web/src/components/charging-stations/CSConnector.vue b/ui/web/src/components/charging-stations/CSConnector.vue index 05980e41..3cf324c9 100644 --- a/ui/web/src/components/charging-stations/CSConnector.vue +++ b/ui/web/src/components/charging-stations/CSConnector.vue @@ -9,7 +9,7 @@ :visibility="state.isIdTagModalVisible" :id-tag="state.idTag" @close="hideIdTagModal()" - @done="Utils.compose(state.transaction, hideIdTagModal)()" + @done="compose(state.transaction, hideIdTagModal)()" > Start Transaction --> @@ -28,7 +28,7 @@ import Button from '../buttons/Button.vue'; // import IdTagInputModal from './IdTagInputModal.vue'; import type { ConnectorStatus } from '@/types'; import UIClient from '@/composables/UIClient'; -// import Utils from '@/composables/Utils'; +// import { compose } from '@/composables/Utils'; const props = defineProps<{ hashId: string; diff --git a/ui/web/src/components/charging-stations/CSData.vue b/ui/web/src/components/charging-stations/CSData.vue index 040ba02b..ab9c8208 100644 --- a/ui/web/src/components/charging-stations/CSData.vue +++ b/ui/web/src/components/charging-stations/CSData.vue @@ -21,7 +21,7 @@ // import { reactive } from 'vue'; import CSConnector from './CSConnector.vue'; import type { ChargingStationData, ChargingStationInfo, ConnectorStatus } from '@/types'; -import Utils from '@/composables/Utils'; +import { ifUndefined } from '@/composables/Utils'; const props = defineProps<{ chargingStation: ChargingStationData; @@ -59,7 +59,7 @@ function getHashId(): string { return getInfo().hashId; } function getId(): string { - return Utils.ifUndefined(getInfo().chargingStationId, 'Ø'); + return ifUndefined(getInfo().chargingStationId, 'Ø'); } function getModel(): string { return getInfo().chargePointModel; @@ -68,7 +68,7 @@ function getVendor(): string { return getInfo().chargePointVendor; } function getFirmwareVersion(): string { - return Utils.ifUndefined(getInfo().firmwareVersion, 'Ø'); + return ifUndefined(getInfo().firmwareVersion, 'Ø'); } function getStarted(): string { return props.chargingStation.started === true ? 'Yes' : 'No'; diff --git a/ui/web/src/composables/UIClient.ts b/ui/web/src/composables/UIClient.ts index 3e7856e4..ccd16c21 100644 --- a/ui/web/src/composables/UIClient.ts +++ b/ui/web/src/composables/UIClient.ts @@ -1,4 +1,4 @@ -import Utils from './Utils'; +import { promiseWithTimeout } from './Utils'; import { ProcedureName, type ProtocolResponse, @@ -146,7 +146,7 @@ export default class UIClient { data: RequestPayload ): Promise { let uuid: string; - return Utils.promiseWithTimeout( + return promiseWithTimeout( new Promise((resolve, reject) => { uuid = crypto.randomUUID(); const msg = JSON.stringify([uuid, command, data]); diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index ac217a70..cf4baf5d 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,56 +1,52 @@ import util from 'node:util'; -export default class Utils { - // STATE - public static isUndefined(value: unknown): boolean { - return typeof value === 'undefined'; +const isUndefined = (value: unknown): boolean => { + return typeof value === 'undefined'; +}; + +export const ifUndefined = (value: T | undefined, isValue: T): T => { + if (isUndefined(value) === true) return isValue; + return value as T; +}; + +// const isIterable = (obj: T): boolean => { +// if (obj === null || obj === undefined) { +// return false; +// } +// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; +// }; + +// const ifNotIterableDo = (obj: T, cb: () => void): void => { +// if (isIterable(obj) === false) cb(); +// }; + +const isPromisePending = (promise: Promise): boolean => { + return util.inspect(promise).includes('pending'); +}; + +export const promiseWithTimeout = ( + promise: Promise, + timeoutMs: number, + timeoutError: Error, + timeoutCallback: () => void = () => { + /* This is intentional */ } - - public static ifUndefined(value: T | undefined, isValue: T): T { - if (Utils.isUndefined(value) === true) return isValue; - return value as T; - } - - public static isIterable(obj: T): boolean { - if (obj === null || obj === undefined) { - return false; - } - return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; - } - - // public static ifNotIterableDo(obj: T, cb: () => void): void { - // if (this.isIterable(obj) === false) cb(); - // } - - public static isPromisePending(promise: Promise): boolean { - return util.inspect(promise).includes('pending'); - } - - public static async promiseWithTimeout( - promise: Promise, - timeoutMs: number, - timeoutError: Error, - timeoutCallback: () => void = () => { - /* This is intentional */ - } - ): Promise { - // Create a timeout promise that rejects in timeout milliseconds - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => { - if (Utils.isPromisePending(promise)) { - timeoutCallback(); - // FIXME: The original promise shall be canceled - } - reject(timeoutError); - }, timeoutMs); - }); - - // Returns a race between timeout promise and the passed promise - return Promise.race([promise, timeoutPromise]); - } - - // FUNCTIONAL - // public static compose(...fns: ((arg: T) => T)[]): (x: T) => T { - // return (x: T) => fns.reduceRight((y, fn) => fn(y), x); - // } -} +): Promise => { + // Create a timeout promise that rejects in timeout milliseconds + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + if (isPromisePending(promise)) { + timeoutCallback(); + // FIXME: The original promise shall be canceled + } + reject(timeoutError); + }, timeoutMs); + }); + + // Returns a race between timeout promise and the passed promise + return Promise.race([promise, timeoutPromise]); +}; + +// export const compose = (...fns: ((arg: T) => T)[]): ((x: T) => T) => { +// return (x: T) => fns.reduceRight((y, fn) => fn(y), x); +// };