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