fix: ensure simulator will stop after a timeout (120s)
[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';
2896e06d 8import { type ChargingStation, ChargingStationUtils } 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);
31f59c6d
JB
36 this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as (
37 chargingStation: ChargingStation,
38 messageId: string,
39 commandName: IncomingRequestCommand,
40 commandPayload: JsonType
41 ) => Promise<void>;
42 this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
43 T extends JsonType
44 >(
45 chargingStation: ChargingStation,
46 commandName: IncomingRequestCommand,
47 schema: JSONSchemaType<T>,
48 payload: T
49 ) => boolean;
c0560973
JB
50 }
51
08f130a0 52 public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
1ca780f9 53 if (OCPPIncomingRequestService.instance === null) {
08f130a0 54 OCPPIncomingRequestService.instance = new this();
9f2e3130 55 }
08f130a0 56 return OCPPIncomingRequestService.instance as T;
9f2e3130
JB
57 }
58
7b5dbe91 59 protected handleIncomingRequestError<T extends JsonType>(
08f130a0 60 chargingStation: ChargingStation,
e7aeea18
JB
61 commandName: IncomingRequestCommand,
62 error: Error,
7b5dbe91 63 params: HandleErrorParams<T> = { throwError: true, consoleOut: false }
51581a20 64 ): T | undefined {
fa5995d6 65 setDefaultErrorParams(params);
e7aeea18 66 logger.error(
60ddad53 67 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
e7aeea18
JB
68 error
69 );
717c1e56
JB
70 if (!params?.throwError && params?.errorResponse) {
71 return params?.errorResponse;
e64c0923 72 }
717c1e56 73 if (params?.throwError && !params?.errorResponse) {
e0a50bcd
JB
74 throw error;
75 }
717c1e56
JB
76 if (params?.throwError && params?.errorResponse) {
77 return params?.errorResponse;
78 }
47e22477
JB
79 }
80
e3018bc4
JB
81 protected validateIncomingRequestPayload<T extends JsonType>(
82 chargingStation: ChargingStation,
83 commandName: IncomingRequestCommand,
84 schema: JSONSchemaType<T>,
85 payload: T
86 ): boolean {
0638ddd2 87 if (chargingStation.getPayloadSchemaValidation() === false) {
e3018bc4
JB
88 return true;
89 }
90 const validate = this.ajv.compile(schema);
91 if (validate(payload)) {
92 return true;
93 }
94 logger.error(
45988780 95 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
e3018bc4
JB
96 validate.errors
97 );
98 throw new OCPPError(
01a4dcbb 99 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
e3018bc4
JB
100 'Incoming request PDU is invalid',
101 commandName,
102 JSON.stringify(validate.errors, null, 2)
103 );
104 }
105
22e0d48e 106 protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse {
f911a4af 107 chargingStation.idTagsCache.deleteIdTags(
e302df1d 108 ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo)
22e0d48e
JB
109 );
110 return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
111 }
112
f7f98c68 113 public abstract incomingRequestHandler(
08f130a0 114 chargingStation: ChargingStation,
e7aeea18
JB
115 messageId: string,
116 commandName: IncomingRequestCommand,
5cc4b63b 117 commandPayload: JsonType
e7aeea18 118 ): Promise<void>;
c0560973 119}