refactor: use the same empty function everywhere
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
1 import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'
2 import _ajvFormats from 'ajv-formats'
3
4 import { OCPPServiceUtils } from './OCPPServiceUtils.js'
5 import type { ChargingStation } from '../../charging-station/index.js'
6 import { OCPPError } from '../../exception/index.js'
7 import type {
8 IncomingRequestCommand,
9 JsonType,
10 OCPPVersion,
11 RequestCommand
12 } from '../../types/index.js'
13 import { Constants, logger } from '../../utils/index.js'
14 type Ajv = _Ajv.default
15 // eslint-disable-next-line @typescript-eslint/no-redeclare
16 const Ajv = _Ajv.default
17 const ajvFormats = _ajvFormats.default
18
19 const moduleName = 'OCPPResponseService'
20
21 export abstract class OCPPResponseService {
22 private static instance: OCPPResponseService | null = null
23
24 public jsonIncomingRequestResponseValidateFunctions: Map<
25 IncomingRequestCommand,
26 ValidateFunction<JsonType>
27 >
28
29 private readonly version: OCPPVersion
30 private readonly ajv: Ajv
31 private readonly jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>
32
33 public abstract jsonIncomingRequestResponseSchemas: Map<
34 IncomingRequestCommand,
35 JSONSchemaType<JsonType>
36 >
37
38 protected constructor (version: OCPPVersion) {
39 this.version = version
40 this.ajv = new Ajv({
41 keywords: ['javaType'],
42 multipleOfPrecision: 2
43 })
44 ajvFormats(this.ajv)
45 this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>()
46 this.jsonIncomingRequestResponseValidateFunctions = new Map<
47 IncomingRequestCommand,
48 ValidateFunction<JsonType>
49 >()
50 this.responseHandler = this.responseHandler.bind(this) as <
51 ReqType extends JsonType,
52 ResType extends JsonType
53 >(
54 chargingStation: ChargingStation,
55 commandName: RequestCommand,
56 payload: ResType,
57 requestPayload: ReqType
58 ) => Promise<void>
59 this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
60 chargingStation: ChargingStation,
61 commandName: RequestCommand,
62 schema: JSONSchemaType<T>,
63 payload: T
64 ) => boolean
65 }
66
67 public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
68 if (OCPPResponseService.instance === null) {
69 OCPPResponseService.instance = new this()
70 }
71 return OCPPResponseService.instance as T
72 }
73
74 protected validateResponsePayload<T extends JsonType>(
75 chargingStation: ChargingStation,
76 commandName: RequestCommand,
77 schema: JSONSchemaType<T>,
78 payload: T
79 ): boolean {
80 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
81 return true
82 }
83 const validate = this.getJsonRequestValidateFunction<T>(commandName, schema)
84 if (validate(payload)) {
85 return true
86 }
87 logger.error(
88 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
89 validate.errors
90 )
91 throw new OCPPError(
92 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
93 'Response PDU is invalid',
94 commandName,
95 JSON.stringify(validate.errors, undefined, 2)
96 )
97 }
98
99 protected emptyResponseHandler = Constants.EMPTY_FUNCTION
100
101 private getJsonRequestValidateFunction<T extends JsonType>(
102 commandName: RequestCommand,
103 schema: JSONSchemaType<T>
104 ): ValidateFunction<JsonType> {
105 if (!this.jsonRequestValidateFunctions.has(commandName)) {
106 this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this))
107 }
108 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109 return this.jsonRequestValidateFunctions.get(commandName)!
110 }
111
112 public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
113 chargingStation: ChargingStation,
114 commandName: RequestCommand,
115 payload: ResType,
116 requestPayload: ReqType
117 ): Promise<void>
118 }