a80bc2b730564d24110fbbc5026688ddf0e46df1
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
1 import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
2 import _ajvFormats from 'ajv-formats';
3
4 import { OCPPServiceUtils } from './OCPPServiceUtils.js';
5 import type { ChargingStation } from '../../charging-station/index.js';
6 import { OCPPError } from '../../exception/index.js';
7 import type {
8 IncomingRequestCommand,
9 JsonType,
10 OCPPVersion,
11 RequestCommand,
12 } from '../../types/index.js';
13 import { logger } from '../../utils/index.js';
14 type Ajv = _Ajv.default;
15 const Ajv = _Ajv.default;
16 const ajvFormats = _ajvFormats.default;
17
18 const moduleName = 'OCPPResponseService';
19
20 export abstract class OCPPResponseService {
21 private static instance: OCPPResponseService | null = null;
22
23 public jsonIncomingRequestResponseValidateFunctions: Map<
24 IncomingRequestCommand,
25 ValidateFunction<JsonType>
26 >;
27
28 private readonly version: OCPPVersion;
29 private readonly ajv: Ajv;
30 private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>;
31
32 public abstract jsonIncomingRequestResponseSchemas: Map<
33 IncomingRequestCommand,
34 JSONSchemaType<JsonType>
35 >;
36
37 protected constructor(version: OCPPVersion) {
38 this.version = version;
39 this.ajv = new Ajv({
40 keywords: ['javaType'],
41 multipleOfPrecision: 2,
42 });
43 ajvFormats(this.ajv);
44 this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>();
45 this.jsonIncomingRequestResponseValidateFunctions = new Map<
46 IncomingRequestCommand,
47 ValidateFunction<JsonType>
48 >();
49 this.responseHandler = this.responseHandler.bind(this) as <
50 ReqType extends JsonType,
51 ResType extends JsonType,
52 >(
53 chargingStation: ChargingStation,
54 commandName: RequestCommand,
55 payload: ResType,
56 requestPayload: ReqType,
57 ) => Promise<void>;
58 this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
59 chargingStation: ChargingStation,
60 commandName: RequestCommand,
61 schema: JSONSchemaType<T>,
62 payload: T,
63 ) => boolean;
64 }
65
66 public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
67 if (OCPPResponseService.instance === null) {
68 OCPPResponseService.instance = new this();
69 }
70 return OCPPResponseService.instance as T;
71 }
72
73 protected validateResponsePayload<T extends JsonType>(
74 chargingStation: ChargingStation,
75 commandName: RequestCommand,
76 schema: JSONSchemaType<T>,
77 payload: T,
78 ): boolean {
79 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
80 return true;
81 }
82 const validate = this.getJsonRequestValidateFunction<T>(commandName, schema);
83 if (validate(payload)) {
84 return true;
85 }
86 logger.error(
87 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
88 validate.errors,
89 );
90 throw new OCPPError(
91 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
92 'Response PDU is invalid',
93 commandName,
94 JSON.stringify(validate.errors, undefined, 2),
95 );
96 }
97
98 protected emptyResponseHandler() {
99 /* This is intentional */
100 }
101
102 private getJsonRequestValidateFunction<T extends JsonType>(
103 commandName: RequestCommand,
104 schema: JSONSchemaType<T>,
105 ) {
106 if (this.jsonRequestValidateFunctions.has(commandName) === false) {
107 this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
108 }
109 return this.jsonRequestValidateFunctions.get(commandName)!;
110 }
111
112 public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
113 chargingStation: ChargingStation,
114 commandName: RequestCommand,
115 payload: ResType,
116 requestPayload: ReqType,
117 ): Promise<void>;
118 }