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