} 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
validate?.errors
)
throw new OCPPError(
- OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors),
+ ajvErrorsToErrorType(validate?.errors),
'Incoming request PDU is invalid',
commandName,
JSON.stringify(validate?.errors, undefined, 2)
} 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
}
const validate = this.payloadValidateFunctions.get(commandName as RequestCommand)
payload = clone<T>(payload)
- OCPPServiceUtils.convertDateToISOString<T>(payload)
+ convertDateToISOString<T>(payload)
if (validate?.(payload) === true) {
return true
}
)
// 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)
commandName as IncomingRequestCommand
)
payload = clone<T>(payload)
- OCPPServiceUtils.convertDateToISOString<T>(payload)
+ convertDateToISOString<T>(payload)
if (validate?.(payload) === true) {
return true
}
)
// 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)
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 = <T extends JsonType>(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<T>(object![key] as T)
+ }
+ }
+}
+
export const buildMeterValue = (
chargingStation: ChargingStation,
connectorId: number,
// 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
// 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
return true
}
- public static convertDateToISOString<T extends JsonType>(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<T>(object![key] as T)
- }
- }
- }
-
protected static parseJsonSchemaFile<T extends JsonType>(
relativePath: string,
ocppVersion: OCPPVersion,