type SetChargingProfileResponse,
type UnlockConnectorResponse
} from '../../../types/index.js'
-import { Constants, convertToInt, logger } from '../../../utils/index.js'
+import { Constants, convertToInt, isAsyncFunction, logger } from '../../../utils/index.js'
import { OCPPResponseService } from '../OCPPResponseService.js'
const moduleName = 'OCPP16ResponseService'
export class OCPP16ResponseService extends OCPPResponseService {
- public jsonSchemasIncomingRequestResponseValidateFunction: Map<
+ public incomingRequestResponsePayloadValidateFunctions: Map<
OCPP16IncomingRequestCommand,
ValidateFunction<JsonType>
>
- protected jsonSchemasValidateFunction: Map<OCPP16RequestCommand, ValidateFunction<JsonType>>
+ protected payloadValidateFunctions: Map<OCPP16RequestCommand, ValidateFunction<JsonType>>
private readonly responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>
public constructor () {
[OCPP16RequestCommand.DATA_TRANSFER, this.emptyResponseHandler],
[OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, this.emptyResponseHandler]
])
- this.jsonSchemasValidateFunction = new Map<OCPP16RequestCommand, ValidateFunction<JsonType>>([
+ this.payloadValidateFunctions = new Map<OCPP16RequestCommand, ValidateFunction<JsonType>>([
[
OCPP16RequestCommand.BOOT_NOTIFICATION,
this.ajv
.bind(this)
]
])
- this.jsonSchemasIncomingRequestResponseValidateFunction = new Map<
+ this.incomingRequestResponsePayloadValidateFunctions = new Map<
OCPP16IncomingRequestCommand,
ValidateFunction<JsonType>
>([
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:`,
payload,
undefined,
2
- )} while the charging station is not registered on the central server.`,
+ )} while the charging station is not registered on the central server`,
commandName,
payload
)
commandName: OCPP16RequestCommand,
payload: JsonType
): boolean {
- if (this.jsonSchemasValidateFunction.has(commandName)) {
+ if (this.payloadValidateFunctions.has(commandName)) {
return this.validateResponsePayload(chargingStation, commandName, payload)
}
logger.warn(
logger.debug(
`${chargingStation.logPrefix()} idTag '${requestPayload.idTag}' rejected with status '${
payload.idTagInfo.status
- }`
+ }'`
)
}
} else {