Apply dependencies update
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
CommitLineData
e6159ce8
JB
1import { AsyncResource } from 'async_hooks';
2
d270cc87 3import Ajv, { type JSONSchemaType } from 'ajv';
e3018bc4
JB
4import ajvFormats from 'ajv-formats';
5
6import OCPPError from '../../exception/OCPPError';
6c1761d4 7import type { HandleErrorParams } from '../../types/Error';
b3fc3ff5 8import type { JsonObject, JsonType } from '../../types/JsonType';
b0342994 9import type { OCPPVersion } from '../../types/ocpp/OCPPVersion';
6c1761d4 10import type { IncomingRequestCommand } from '../../types/ocpp/Requests';
22e0d48e 11import type { ClearCacheResponse } from '../../types/ocpp/Responses';
9f2e3130 12import logger from '../../utils/Logger';
8114d10e 13import type ChargingStation from '../ChargingStation';
22e0d48e
JB
14import { ChargingStationUtils } from '../ChargingStationUtils';
15import OCPPConstants from './OCPPConstants';
cbb3711f 16import { OCPPServiceUtils } from './OCPPServiceUtils';
c0560973 17
e3018bc4
JB
18const moduleName = 'OCPPIncomingRequestService';
19
c0560973 20export default abstract class OCPPIncomingRequestService {
08f130a0 21 private static instance: OCPPIncomingRequestService | null = null;
e6159ce8 22 protected asyncResource: AsyncResource;
d270cc87 23 private readonly version: OCPPVersion;
012ae1a9 24 private readonly ajv: Ajv;
b3fc3ff5 25 protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>;
10068088 26
d270cc87
JB
27 protected constructor(version: OCPPVersion) {
28 this.version = version;
45988780 29 this.ajv = new Ajv({
98fc1389 30 keywords: ['javaType'],
45988780
JB
31 multipleOfPrecision: 2,
32 });
e3018bc4 33 ajvFormats(this.ajv);
d270cc87 34 this.asyncResource = new AsyncResource(moduleName);
9952c548
JB
35 this.incomingRequestHandler.bind(this);
36 this.validateIncomingRequestPayload.bind(this);
c0560973
JB
37 }
38
08f130a0 39 public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
1ca780f9 40 if (OCPPIncomingRequestService.instance === null) {
08f130a0 41 OCPPIncomingRequestService.instance = new this();
9f2e3130 42 }
08f130a0 43 return OCPPIncomingRequestService.instance as T;
9f2e3130
JB
44 }
45
e7aeea18 46 protected handleIncomingRequestError<T>(
08f130a0 47 chargingStation: ChargingStation,
e7aeea18
JB
48 commandName: IncomingRequestCommand,
49 error: Error,
50 params: HandleErrorParams<T> = { throwError: true }
51581a20 51 ): T | undefined {
e7aeea18 52 logger.error(
60ddad53 53 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
e7aeea18
JB
54 error
55 );
717c1e56
JB
56 if (!params?.throwError && params?.errorResponse) {
57 return params?.errorResponse;
e64c0923 58 }
717c1e56 59 if (params?.throwError && !params?.errorResponse) {
e0a50bcd
JB
60 throw error;
61 }
717c1e56
JB
62 if (params?.throwError && params?.errorResponse) {
63 return params?.errorResponse;
64 }
47e22477
JB
65 }
66
e3018bc4
JB
67 protected validateIncomingRequestPayload<T extends JsonType>(
68 chargingStation: ChargingStation,
69 commandName: IncomingRequestCommand,
70 schema: JSONSchemaType<T>,
71 payload: T
72 ): boolean {
0638ddd2 73 if (chargingStation.getPayloadSchemaValidation() === false) {
e3018bc4
JB
74 return true;
75 }
76 const validate = this.ajv.compile(schema);
77 if (validate(payload)) {
78 return true;
79 }
80 logger.error(
45988780 81 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
e3018bc4
JB
82 validate.errors
83 );
84 throw new OCPPError(
01a4dcbb 85 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
e3018bc4
JB
86 'Incoming request PDU is invalid',
87 commandName,
88 JSON.stringify(validate.errors, null, 2)
89 );
90 }
91
22e0d48e
JB
92 protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse {
93 chargingStation.authorizedTagsCache.deleteAuthorizedTags(
94 ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo)
95 );
96 return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
97 }
98
f7f98c68 99 public abstract incomingRequestHandler(
08f130a0 100 chargingStation: ChargingStation,
e7aeea18
JB
101 messageId: string,
102 commandName: IncomingRequestCommand,
5cc4b63b 103 commandPayload: JsonType
e7aeea18 104 ): Promise<void>;
c0560973 105}