refactor: cleanup unneeded type casting
[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)
51 this.validateResponsePayload = this.validateResponsePayload.bind(this)
52 }
53
54 public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
55 if (OCPPResponseService.instance === null) {
56 OCPPResponseService.instance = new this()
57 }
58 return OCPPResponseService.instance as T
59 }
60
61 protected validateResponsePayload<T extends JsonType>(
62 chargingStation: ChargingStation,
63 commandName: RequestCommand,
64 schema: JSONSchemaType<T>,
65 payload: T
66 ): boolean {
67 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
68 return true
69 }
70 const validate = this.getJsonRequestValidateFunction<T>(commandName, schema)
71 if (validate(payload)) {
72 return true
73 }
74 logger.error(
75 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
76 validate.errors
77 )
78 throw new OCPPError(
79 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
80 'Response PDU is invalid',
81 commandName,
82 JSON.stringify(validate.errors, undefined, 2)
83 )
84 }
85
86 protected emptyResponseHandler = Constants.EMPTY_FUNCTION
87
88 private getJsonRequestValidateFunction<T extends JsonType>(
89 commandName: RequestCommand,
90 schema: JSONSchemaType<T>
91 ): ValidateFunction<JsonType> {
92 if (!this.jsonRequestValidateFunctions.has(commandName)) {
93 this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this))
94 }
95 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
96 return this.jsonRequestValidateFunctions.get(commandName)!
97 }
98
99 public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
100 chargingStation: ChargingStation,
101 commandName: RequestCommand,
102 payload: ResType,
103 requestPayload: ReqType
104 ): Promise<void>
105 }