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