Permit to run code in async scope in the OCPP stack
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
1 import { AsyncResource } from 'async_hooks';
2
3 import type { JSONSchemaType } from 'ajv';
4 import Ajv from 'ajv-draft-04';
5 import ajvFormats from 'ajv-formats';
6
7 import OCPPError from '../../exception/OCPPError';
8 import type { HandleErrorParams } from '../../types/Error';
9 import type { JsonType } from '../../types/JsonType';
10 import type { IncomingRequestCommand } from '../../types/ocpp/Requests';
11 import logger from '../../utils/Logger';
12 import type ChargingStation from '../ChargingStation';
13 import { OCPPServiceUtils } from './OCPPServiceUtils';
14
15 const moduleName = 'OCPPIncomingRequestService';
16
17 export default abstract class OCPPIncomingRequestService {
18 private static instance: OCPPIncomingRequestService | null = null;
19 protected asyncResource: AsyncResource;
20 private ajv: Ajv;
21
22 protected constructor() {
23 this.asyncResource = new AsyncResource(moduleName);
24 this.ajv = new Ajv();
25 ajvFormats(this.ajv);
26 this.incomingRequestHandler.bind(this);
27 this.validateIncomingRequestPayload.bind(this);
28 }
29
30 public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
31 if (OCPPIncomingRequestService.instance === null) {
32 OCPPIncomingRequestService.instance = new this();
33 }
34 return OCPPIncomingRequestService.instance as T;
35 }
36
37 protected handleIncomingRequestError<T>(
38 chargingStation: ChargingStation,
39 commandName: IncomingRequestCommand,
40 error: Error,
41 params: HandleErrorParams<T> = { throwError: true }
42 ): T {
43 logger.error(
44 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
45 error
46 );
47 if (!params?.throwError && params?.errorResponse) {
48 return params?.errorResponse;
49 }
50 if (params?.throwError && !params?.errorResponse) {
51 throw error;
52 }
53 if (params?.throwError && params?.errorResponse) {
54 return params?.errorResponse;
55 }
56 }
57
58 protected validateIncomingRequestPayload<T extends JsonType>(
59 chargingStation: ChargingStation,
60 commandName: IncomingRequestCommand,
61 schema: JSONSchemaType<T>,
62 payload: T
63 ): boolean {
64 if (chargingStation.getPayloadSchemaValidation() === false) {
65 return true;
66 }
67 const validate = this.ajv.compile(schema);
68 if (validate(payload)) {
69 return true;
70 }
71 logger.error(
72 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Incoming request PDU is invalid: %j`,
73 validate.errors
74 );
75 throw new OCPPError(
76 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
77 'Incoming request PDU is invalid',
78 commandName,
79 JSON.stringify(validate.errors, null, 2)
80 );
81 }
82
83 public abstract incomingRequestHandler(
84 chargingStation: ChargingStation,
85 messageId: string,
86 commandName: IncomingRequestCommand,
87 commandPayload: JsonType
88 ): Promise<void>;
89 }