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