import _Ajv, { type ValidateFunction } from 'ajv'
import _ajvFormats from 'ajv-formats'
-import { OCPPConstants } from './OCPPConstants.js'
-import type { OCPPResponseService } from './OCPPResponseService.js'
-import { OCPPServiceUtils } from './OCPPServiceUtils.js'
import type { ChargingStation } from '../../charging-station/index.js'
import { OCPPError } from '../../exception/index.js'
import { PerformanceStatistics } from '../../performance/index.js'
handleSendMessageError,
logger
} from '../../utils/index.js'
+import { OCPPConstants } from './OCPPConstants.js'
+import type { OCPPResponseService } from './OCPPResponseService.js'
+import { OCPPServiceUtils } from './OCPPServiceUtils.js'
type Ajv = _Ajv.default
// eslint-disable-next-line @typescript-eslint/no-redeclare
const Ajv = _Ajv.default
private readonly version: OCPPVersion
private readonly ocppResponseService: OCPPResponseService
protected readonly ajv: Ajv
- protected abstract jsonSchemasValidateFunction: Map<RequestCommand, ValidateFunction<JsonType>>
+ protected abstract payloadValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>
protected constructor (version: OCPPVersion, ocppResponseService: OCPPResponseService) {
this.version = version
if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
return true
}
- if (!this.jsonSchemasValidateFunction.has(commandName as RequestCommand)) {
+ if (!this.payloadValidateFunctions.has(commandName as RequestCommand)) {
logger.warn(
`${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: No JSON schema found for command '${commandName}' PDU validation`
)
return true
}
- const validate = this.jsonSchemasValidateFunction.get(commandName as RequestCommand)
+ const validate = this.payloadValidateFunctions.get(commandName as RequestCommand)
payload = clone<T>(payload)
OCPPServiceUtils.convertDateToISOString<T>(payload)
if (validate?.(payload) === true) {
return true
}
if (
- !this.ocppResponseService.jsonSchemasIncomingRequestResponseValidateFunction.has(
+ !this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.has(
commandName as IncomingRequestCommand
)
) {
)
return true
}
- const validate =
- this.ocppResponseService.jsonSchemasIncomingRequestResponseValidateFunction.get(
- commandName as IncomingRequestCommand
- )
+ const validate = this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.get(
+ commandName as IncomingRequestCommand
+ )
payload = clone<T>(payload)
OCPPServiceUtils.convertDateToISOString<T>(payload)
if (validate?.(payload) === true) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this
// Send a message through wsConnection
- return await new Promise<ResponseType>((resolve, reject) => {
+ return await new Promise<ResponseType>((resolve, reject: (reason?: unknown) => void) => {
/**
* Function that will receive the request's response
*
}
const handleSendError = (ocppError: OCPPError): void => {
- if (params?.skipBufferingOnError === false) {
+ if (params.skipBufferingOnError === false) {
// Buffer
chargingStation.bufferMessage(messageToSend)
if (messageType === MessageType.CALL_MESSAGE) {
- this.cacheRequestPromise(
+ this.setCachedRequest(
chargingStation,
messageId,
messagePayload as JsonType,
)
}
} else if (
- params?.skipBufferingOnError === true &&
+ params.skipBufferingOnError === true &&
messageType === MessageType.CALL_MESSAGE
) {
// Remove request from the cache
`Timeout ${formatDurationMilliSeconds(
OCPPConstants.OCPP_WEBSOCKET_TIMEOUT
)} reached for ${
- params?.skipBufferingOnError === false ? '' : 'non '
+ params.skipBufferingOnError === false ? '' : 'non '
}buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
(messagePayload as OCPPError).details
)} payload: ${messageToSend}`
)
if (messageType === MessageType.CALL_MESSAGE) {
- this.cacheRequestPromise(
+ this.setCachedRequest(
chargingStation,
messageId,
messagePayload as JsonType,
new OCPPError(
ErrorType.GENERIC_ERROR,
`WebSocket errored for ${
- params?.skipBufferingOnError === false ? '' : 'non '
+ params.skipBufferingOnError === false ? '' : 'non '
}buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
{ name: error.name, message: error.message, stack: error.stack }
new OCPPError(
ErrorType.GENERIC_ERROR,
`WebSocket closed for ${
- params?.skipBufferingOnError === false ? '' : 'non '
+ params.skipBufferingOnError === false ? '' : 'non '
}buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
(messagePayload as OCPPError).details
(messagePayload as OCPPError).code,
(messagePayload as OCPPError).message,
(messagePayload as OCPPError).details ?? {
- command: (messagePayload as OCPPError).command ?? commandName
+ command: (messagePayload as OCPPError).command
}
] satisfies ErrorResponse)
break
return messageToSend
}
- private cacheRequestPromise (
+ private setCachedRequest (
chargingStation: ChargingStation,
messageId: string,
messagePayload: JsonType,
])
}
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
chargingStation: ChargingStation,
commandName: RequestCommand,
- // FIXME: should be ReqType
- commandParams?: JsonType,
+ commandParams?: ReqType,
params?: RequestParams
): Promise<ResType>
}