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