refactor: factor out JSON schema validation function getter
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
CommitLineData
ec0eebcc 1import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
844e496b
JB
2import ajvFormats from 'ajv-formats';
3
4c3c0d59 4import { OCPPServiceUtils } from './OCPPServiceUtils';
2896e06d 5import type { ChargingStation } from '../../charging-station';
268a74bb 6import { OCPPError } from '../../exception';
291b5ec8 7import type { IncomingRequestCommand, JsonType, OCPPVersion, RequestCommand } from '../../types';
60a74391 8import { logger } from '../../utils';
c0560973 9
e3018bc4
JB
10const moduleName = 'OCPPResponseService';
11
268a74bb 12export abstract class OCPPResponseService {
08f130a0 13 private static instance: OCPPResponseService | null = null;
ec0eebcc
JB
14
15 public jsonIncomingRequestResponseValidateFunctions: Map<
16 IncomingRequestCommand,
291b5ec8 17 ValidateFunction<JsonType>
ec0eebcc
JB
18 >;
19
d270cc87 20 private readonly version: OCPPVersion;
012ae1a9 21 private readonly ajv: Ajv;
291b5ec8 22 private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>;
ec0eebcc 23
b3fc3ff5
JB
24 public abstract jsonIncomingRequestResponseSchemas: Map<
25 IncomingRequestCommand,
291b5ec8 26 JSONSchemaType<JsonType>
b3fc3ff5 27 >;
10068088 28
d270cc87
JB
29 protected constructor(version: OCPPVersion) {
30 this.version = version;
45988780 31 this.ajv = new Ajv({
98fc1389 32 keywords: ['javaType'],
45988780
JB
33 multipleOfPrecision: 2,
34 });
844e496b 35 ajvFormats(this.ajv);
291b5ec8 36 this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>();
ec0eebcc
JB
37 this.jsonIncomingRequestResponseValidateFunctions = new Map<
38 IncomingRequestCommand,
291b5ec8 39 ValidateFunction<JsonType>
ec0eebcc 40 >();
9429aa42
JB
41 this.responseHandler = this.responseHandler.bind(this) as <
42 ReqType extends JsonType,
43 ResType extends JsonType,
44 >(
31f59c6d
JB
45 chargingStation: ChargingStation,
46 commandName: RequestCommand,
9429aa42
JB
47 payload: ResType,
48 requestPayload: ReqType,
31f59c6d
JB
49 ) => Promise<void>;
50 this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
51 chargingStation: ChargingStation,
52 commandName: RequestCommand,
53 schema: JSONSchemaType<T>,
5edd8ba0 54 payload: T,
31f59c6d 55 ) => boolean;
c0560973
JB
56 }
57
08f130a0 58 public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
1ca780f9 59 if (OCPPResponseService.instance === null) {
08f130a0 60 OCPPResponseService.instance = new this();
9f2e3130 61 }
08f130a0 62 return OCPPResponseService.instance as T;
9f2e3130
JB
63 }
64
844e496b
JB
65 protected validateResponsePayload<T extends JsonType>(
66 chargingStation: ChargingStation,
67 commandName: RequestCommand,
68 schema: JSONSchemaType<T>,
5edd8ba0 69 payload: T,
844e496b 70 ): boolean {
5398cecf 71 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
844e496b
JB
72 return true;
73 }
0b0ca54f 74 const validate = this.getJsonRequestValidateFunction<T>(commandName, schema);
844e496b
JB
75 if (validate(payload)) {
76 return true;
77 }
78 logger.error(
45988780 79 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
5edd8ba0 80 validate.errors,
844e496b
JB
81 );
82 throw new OCPPError(
9ff486f4 83 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
844e496b
JB
84 'Response PDU is invalid',
85 commandName,
4ed03b6e 86 JSON.stringify(validate.errors, undefined, 2),
844e496b
JB
87 );
88 }
89
81e3cc38 90 protected emptyResponseHandler() {
bd770dde 91 /* This is intentional */
81e3cc38 92 }
b52c969d 93
0b0ca54f
JB
94 private getJsonRequestValidateFunction<T extends JsonType>(
95 commandName: RequestCommand,
96 schema: JSONSchemaType<T>,
97 ) {
98 if (this.jsonRequestValidateFunctions.has(commandName) === false) {
99 this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
100 }
101 return this.jsonRequestValidateFunctions.get(commandName)!;
102 }
103
9429aa42 104 public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
08f130a0 105 chargingStation: ChargingStation,
e7aeea18 106 commandName: RequestCommand,
9429aa42
JB
107 payload: ResType,
108 requestPayload: ReqType,
e7aeea18 109 ): Promise<void>;
c0560973 110}