build: properly workaround Ajv TS type definitions bug
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
CommitLineData
f5a1ff8c
JB
1import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
2import _ajvFormats from 'ajv-formats';
e3018bc4 3
a6ef1ece
JB
4import { OCPPConstants } from './OCPPConstants.js';
5import { OCPPServiceUtils } from './OCPPServiceUtils.js';
6import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js';
7import { OCPPError } from '../../exception/index.js';
268a74bb
JB
8import type {
9 ClearCacheResponse,
10 HandleErrorParams,
11 IncomingRequestCommand,
268a74bb
JB
12 JsonType,
13 OCPPVersion,
a6ef1ece
JB
14} from '../../types/index.js';
15import { logger, setDefaultErrorParams } from '../../utils/index.js';
f5a1ff8c
JB
16type Ajv = _Ajv.default;
17const Ajv = _Ajv.default;
18const ajvFormats = _ajvFormats.default;
c0560973 19
e3018bc4
JB
20const moduleName = 'OCPPIncomingRequestService';
21
d1ff8599 22export abstract class OCPPIncomingRequestService {
08f130a0 23 private static instance: OCPPIncomingRequestService | null = null;
d270cc87 24 private readonly version: OCPPVersion;
012ae1a9 25 private readonly ajv: Ajv;
291b5ec8
JB
26 private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>>;
27 protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>>;
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 });
e3018bc4 35 ajvFormats(this.ajv);
291b5ec8 36 this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>();
9429aa42
JB
37 this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as <
38 ReqType extends JsonType,
39 // eslint-disable-next-line @typescript-eslint/no-unused-vars
40 ResType extends JsonType,
41 >(
31f59c6d
JB
42 chargingStation: ChargingStation,
43 messageId: string,
44 commandName: IncomingRequestCommand,
9429aa42 45 commandPayload: ReqType,
31f59c6d
JB
46 ) => Promise<void>;
47 this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
5edd8ba0 48 T extends JsonType,
31f59c6d
JB
49 >(
50 chargingStation: ChargingStation,
51 commandName: IncomingRequestCommand,
52 schema: JSONSchemaType<T>,
5edd8ba0 53 payload: T,
31f59c6d 54 ) => boolean;
c0560973
JB
55 }
56
08f130a0 57 public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
1ca780f9 58 if (OCPPIncomingRequestService.instance === null) {
08f130a0 59 OCPPIncomingRequestService.instance = new this();
9f2e3130 60 }
08f130a0 61 return OCPPIncomingRequestService.instance as T;
9f2e3130
JB
62 }
63
7b5dbe91 64 protected handleIncomingRequestError<T extends JsonType>(
08f130a0 65 chargingStation: ChargingStation,
e7aeea18
JB
66 commandName: IncomingRequestCommand,
67 error: Error,
5edd8ba0 68 params: HandleErrorParams<T> = { throwError: true, consoleOut: false },
51581a20 69 ): T | undefined {
fa5995d6 70 setDefaultErrorParams(params);
e7aeea18 71 logger.error(
60ddad53 72 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
5edd8ba0 73 error,
e7aeea18 74 );
717c1e56
JB
75 if (!params?.throwError && params?.errorResponse) {
76 return params?.errorResponse;
e64c0923 77 }
717c1e56 78 if (params?.throwError && !params?.errorResponse) {
e0a50bcd
JB
79 throw error;
80 }
717c1e56
JB
81 if (params?.throwError && params?.errorResponse) {
82 return params?.errorResponse;
83 }
47e22477
JB
84 }
85
e3018bc4
JB
86 protected validateIncomingRequestPayload<T extends JsonType>(
87 chargingStation: ChargingStation,
88 commandName: IncomingRequestCommand,
89 schema: JSONSchemaType<T>,
5edd8ba0 90 payload: T,
e3018bc4 91 ): boolean {
5398cecf 92 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
e3018bc4
JB
93 return true;
94 }
0b0ca54f 95 const validate = this.getJsonIncomingRequestValidateFunction<T>(commandName, schema);
e3018bc4
JB
96 if (validate(payload)) {
97 return true;
98 }
99 logger.error(
45988780 100 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
5edd8ba0 101 validate.errors,
e3018bc4
JB
102 );
103 throw new OCPPError(
9ff486f4 104 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
e3018bc4
JB
105 'Incoming request PDU is invalid',
106 commandName,
4ed03b6e 107 JSON.stringify(validate.errors, undefined, 2),
e3018bc4
JB
108 );
109 }
110
22e0d48e 111 protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse {
e1d9a0f4 112 if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) {
26a17d93
JB
113 return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
114 }
115 return OCPPConstants.OCPP_RESPONSE_REJECTED;
22e0d48e
JB
116 }
117
0b0ca54f
JB
118 private getJsonIncomingRequestValidateFunction<T extends JsonType>(
119 commandName: IncomingRequestCommand,
120 schema: JSONSchemaType<T>,
121 ) {
122 if (this.jsonValidateFunctions.has(commandName) === false) {
123 this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
124 }
125 return this.jsonValidateFunctions.get(commandName)!;
126 }
127
9429aa42
JB
128 // eslint-disable-next-line @typescript-eslint/no-unused-vars
129 public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
08f130a0 130 chargingStation: ChargingStation,
e7aeea18
JB
131 messageId: string,
132 commandName: IncomingRequestCommand,
9429aa42 133 commandPayload: ReqType,
e7aeea18 134 ): Promise<void>;
c0560973 135}