RegistrationStatusEnumType,
type ResponseHandler
} from '../../../types/index.js'
-import { logger } from '../../../utils/index.js'
+import { isAsyncFunction, logger } from '../../../utils/index.js'
import { OCPPResponseService } from '../OCPPResponseService.js'
const moduleName = 'OCPP20ResponseService'
export class OCPP20ResponseService extends OCPPResponseService {
- public jsonSchemasIncomingRequestResponseValidateFunction: Map<
+ public incomingRequestResponsePayloadValidateFunctions: Map<
OCPP20IncomingRequestCommand,
ValidateFunction<JsonType>
>
- protected jsonSchemasValidateFunction: Map<OCPP20RequestCommand, ValidateFunction<JsonType>>
+ protected payloadValidateFunctions: Map<OCPP20RequestCommand, ValidateFunction<JsonType>>
private readonly responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>
public constructor () {
[OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler],
[OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler]
])
- this.jsonSchemasValidateFunction = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([
+ this.payloadValidateFunctions = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([
[
OCPP20RequestCommand.BOOT_NOTIFICATION,
this.ajv
.bind(this)
]
])
- this.jsonSchemasIncomingRequestResponseValidateFunction = new Map<
+ this.incomingRequestResponsePayloadValidateFunctions = new Map<
OCPP20IncomingRequestCommand,
ValidateFunction<JsonType>
>([
[
OCPP20IncomingRequestCommand.CLEAR_CACHE,
- this.ajv
+ this.ajvIncomingRequest
.compile(
OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
try {
this.validatePayload(chargingStation, commandName, payload)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload)
+ const responseHandler = this.responseHandlers.get(commandName)!
+ if (isAsyncFunction(responseHandler)) {
+ await responseHandler(chargingStation, payload, requestPayload)
+ } else {
+ (
+ responseHandler as (
+ chargingStation: ChargingStation,
+ payload: JsonType,
+ requestPayload?: JsonType
+ ) => void
+ )(chargingStation, payload, requestPayload)
+ }
} catch (error) {
logger.error(
`${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
commandName: OCPP20RequestCommand,
payload: JsonType
): boolean {
- if (this.jsonSchemasValidateFunction.has(commandName)) {
+ if (this.payloadValidateFunctions.has(commandName)) {
return this.validateResponsePayload(chargingStation, commandName, payload)
}
logger.warn(