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