-import type { JSONSchemaType } from 'ajv';
-import Ajv from 'ajv-draft-04';
+import Ajv, { type JSONSchemaType } from 'ajv';
import ajvFormats from 'ajv-formats';
import OCPPError from '../../exception/OCPPError';
import type { JsonObject, JsonType } from '../../types/JsonType';
import { ErrorType } from '../../types/ocpp/ErrorType';
import { MessageType } from '../../types/ocpp/MessageType';
+import type { OCPPVersion } from '../../types/ocpp/OCPPVersion';
import {
type ErrorCallback,
type IncomingRequestCommand,
export default abstract class OCPPRequestService {
private static instance: OCPPRequestService | null = null;
+ private readonly version: OCPPVersion;
private readonly ajv: Ajv;
private readonly ocppResponseService: OCPPResponseService;
- protected constructor(ocppResponseService: OCPPResponseService) {
- this.ocppResponseService = ocppResponseService;
- this.ajv = new Ajv();
+ protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) {
+ this.version = version;
+ this.ajv = new Ajv({
+ keywords: ['javaType'],
+ multipleOfPrecision: 2,
+ });
ajvFormats(this.ajv);
+ this.ocppResponseService = ocppResponseService;
this.requestHandler.bind(this);
this.sendMessage.bind(this);
this.sendResponse.bind(this);
protected validateRequestPayload<T extends JsonType>(
chargingStation: ChargingStation,
- commandName: RequestCommand,
- schema: JSONSchemaType<T>,
+ commandName: RequestCommand | IncomingRequestCommand,
payload: T
): boolean {
if (chargingStation.getPayloadSchemaValidation() === false) {
return true;
}
+ const schema = this.getRequestPayloadValidationSchema(chargingStation, commandName);
+ if (schema === false) {
+ return true;
+ }
const validate = this.ajv.compile(schema);
+ OCPPServiceUtils.convertDateToISOString<T>(payload);
if (validate(payload)) {
return true;
}
logger.error(
- `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`,
+ `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Command '${commandName}' request PDU is invalid: %j`,
validate.errors
);
// OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
// Check if wsConnection opened
if (chargingStation.isWebSocketConnectionOpened() === true) {
// Yes: Send Message
- const beginId = PerformanceStatistics.beginMeasure(commandName);
+ const beginId = PerformanceStatistics.beginMeasure(commandName as string);
// FIXME: Handle sending error
chargingStation.wsConnection.send(messageToSend);
- PerformanceStatistics.endMeasure(commandName, beginId);
+ PerformanceStatistics.endMeasure(commandName as string, beginId);
logger.debug(
`${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
messageType
commandName,
messagePayload as JsonType,
]);
+ this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonType);
messageToSend = JSON.stringify([
messageType,
messageId,
// Response
case MessageType.CALL_RESULT_MESSAGE:
// Build response
+ // FIXME: Validate response payload
messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response);
break;
// Error Message
commandParams?: JsonType,
params?: RequestParams
): Promise<ResType>;
+
+ protected abstract getRequestPayloadValidationSchema(
+ chargingStation: ChargingStation,
+ commandName: RequestCommand | IncomingRequestCommand
+ ): JSONSchemaType<JsonObject> | false;
}