From 01b82de5b532cd149eccab7b82fe86a741289581 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 5 Jun 2024 22:04:25 +0200 Subject: [PATCH] refactor: cleanup OCPP utils MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPIncomingRequestService.ts | 4 +- .../ocpp/OCPPRequestService.ts | 14 ++-- .../ocpp/OCPPResponseService.ts | 4 +- src/charging-station/ocpp/OCPPServiceUtils.ts | 65 +++++++++---------- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 8cfc8bc8..06165c2f 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -14,7 +14,7 @@ import type { } from '../../types/index.js' import { logger, setDefaultErrorParams } from '../../utils/index.js' import { OCPPConstants } from './OCPPConstants.js' -import { OCPPServiceUtils } from './OCPPServiceUtils.js' +import { ajvErrorsToErrorType } from './OCPPServiceUtils.js' type Ajv = _Ajv.default // eslint-disable-next-line @typescript-eslint/no-redeclare const Ajv = _Ajv.default @@ -89,7 +89,7 @@ export abstract class OCPPIncomingRequestService extends EventEmitter { validate?.errors ) throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), + ajvErrorsToErrorType(validate?.errors), 'Incoming request PDU is invalid', commandName, JSON.stringify(validate?.errors, undefined, 2) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index d396f354..48aca2cc 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -28,7 +28,11 @@ import { } from '../../utils/index.js' import { OCPPConstants } from './OCPPConstants.js' import type { OCPPResponseService } from './OCPPResponseService.js' -import { getMessageTypeString, OCPPServiceUtils } from './OCPPServiceUtils.js' +import { + ajvErrorsToErrorType, + convertDateToISOString, + getMessageTypeString +} from './OCPPServiceUtils.js' type Ajv = _Ajv.default // eslint-disable-next-line @typescript-eslint/no-redeclare const Ajv = _Ajv.default @@ -183,7 +187,7 @@ export abstract class OCPPRequestService { } const validate = this.payloadValidateFunctions.get(commandName as RequestCommand) payload = clone(payload) - OCPPServiceUtils.convertDateToISOString(payload) + convertDateToISOString(payload) if (validate?.(payload) === true) { return true } @@ -193,7 +197,7 @@ export abstract class OCPPRequestService { ) // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), + ajvErrorsToErrorType(validate?.errors), 'Request PDU is invalid', commandName, JSON.stringify(validate?.errors, undefined, 2) @@ -222,7 +226,7 @@ export abstract class OCPPRequestService { commandName as IncomingRequestCommand ) payload = clone(payload) - OCPPServiceUtils.convertDateToISOString(payload) + convertDateToISOString(payload) if (validate?.(payload) === true) { return true } @@ -232,7 +236,7 @@ export abstract class OCPPRequestService { ) // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), + ajvErrorsToErrorType(validate?.errors), 'Response PDU is invalid', commandName, JSON.stringify(validate?.errors, undefined, 2) diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 0e9c6164..00d539b7 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -10,7 +10,7 @@ import type { RequestCommand } from '../../types/index.js' import { Constants, logger } from '../../utils/index.js' -import { OCPPServiceUtils } from './OCPPServiceUtils.js' +import { ajvErrorsToErrorType } from './OCPPServiceUtils.js' type Ajv = _Ajv.default // eslint-disable-next-line @typescript-eslint/no-redeclare const Ajv = _Ajv.default @@ -69,7 +69,7 @@ export abstract class OCPPResponseService { validate?.errors ) throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), + ajvErrorsToErrorType(validate?.errors), 'Response PDU is invalid', commandName, JSON.stringify(validate?.errors, undefined, 2) diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 72d52d52..9031cf59 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -264,6 +264,38 @@ const checkConnectorStatusTransition = ( return transitionAllowed } +export const ajvErrorsToErrorType = (errors: ErrorObject[] | undefined | null): ErrorType => { + if (isNotEmptyArray(errors)) { + for (const error of errors as DefinedError[]) { + switch (error.keyword) { + case 'type': + return ErrorType.TYPE_CONSTRAINT_VIOLATION + case 'dependencies': + case 'required': + return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION + case 'pattern': + case 'format': + return ErrorType.PROPERTY_CONSTRAINT_VIOLATION + } + } + } + return ErrorType.FORMAT_VIOLATION +} + +export const convertDateToISOString = (object: T): void => { + for (const key in object) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion + if (isDate(object![key])) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion + (object![key] as string) = (object![key] as Date).toISOString() + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-condition + } else if (typeof object![key] === 'object' && object![key] !== null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion + convertDateToISOString(object![key] as T) + } + } +} + export const buildMeterValue = ( chargingStation: ChargingStation, connectorId: number, @@ -1262,7 +1294,6 @@ const getMeasurandDefaultLocation = ( // eslint-disable-next-line @typescript-eslint/no-extraneous-class export class OCPPServiceUtils { public static readonly sendAndSetConnectorStatus = sendAndSetConnectorStatus - public static readonly restoreConnectorStatus = restoreConnectorStatus public static readonly isIdTagAuthorized = isIdTagAuthorized public static readonly buildTransactionEndMeterValue = buildTransactionEndMeterValue protected static getSampledValueTemplate = getSampledValueTemplate @@ -1272,24 +1303,6 @@ export class OCPPServiceUtils { // This is intentional } - public static ajvErrorsToErrorType (errors: ErrorObject[] | undefined | null): ErrorType { - if (isNotEmptyArray(errors)) { - for (const error of errors as DefinedError[]) { - switch (error.keyword) { - case 'type': - return ErrorType.TYPE_CONSTRAINT_VIOLATION - case 'dependencies': - case 'required': - return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION - case 'pattern': - case 'format': - return ErrorType.PROPERTY_CONSTRAINT_VIOLATION - } - } - } - return ErrorType.FORMAT_VIOLATION - } - public static isRequestCommandSupported ( chargingStation: ChargingStation, command: RequestCommand @@ -1364,20 +1377,6 @@ export class OCPPServiceUtils { return true } - public static convertDateToISOString(object: T): void { - for (const key in object) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - if (isDate(object![key])) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - (object![key] as string) = (object![key] as Date).toISOString() - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-condition - } else if (typeof object![key] === 'object' && object![key] !== null) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - OCPPServiceUtils.convertDateToISOString(object![key] as T) - } - } - } - protected static parseJsonSchemaFile( relativePath: string, ocppVersion: OCPPVersion, -- 2.34.1