1 import _Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv'
2 import _ajvFormats from
'ajv-formats'
4 import { OCPPServiceUtils
} from
'./OCPPServiceUtils.js'
5 import type { ChargingStation
} from
'../../charging-station/index.js'
6 import { OCPPError
} from
'../../exception/index.js'
8 IncomingRequestCommand
,
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
19 const moduleName
= 'OCPPResponseService'
21 export abstract class OCPPResponseService
{
22 private static instance
: OCPPResponseService
| null = null
24 public jsonIncomingRequestResponseValidateFunctions
: Map
<
25 IncomingRequestCommand
,
26 ValidateFunction
<JsonType
>
29 private readonly version
: OCPPVersion
30 private readonly ajv
: Ajv
31 private readonly jsonRequestValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>
33 public abstract jsonIncomingRequestResponseSchemas
: Map
<
34 IncomingRequestCommand
,
35 JSONSchemaType
<JsonType
>
38 protected constructor (version
: OCPPVersion
) {
39 this.version
= version
41 keywords
: ['javaType'],
42 multipleOfPrecision
: 2
45 this.jsonRequestValidateFunctions
= new Map
<RequestCommand
, ValidateFunction
<JsonType
>>()
46 this.jsonIncomingRequestResponseValidateFunctions
= new Map
<
47 IncomingRequestCommand
,
48 ValidateFunction
<JsonType
>
50 this.responseHandler
= this.responseHandler
.bind(this)
51 this.validateResponsePayload
= this.validateResponsePayload
.bind(this)
54 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
55 if (OCPPResponseService
.instance
=== null) {
56 OCPPResponseService
.instance
= new this()
58 return OCPPResponseService
.instance
as T
61 protected validateResponsePayload
<T
extends JsonType
>(
62 chargingStation
: ChargingStation
,
63 commandName
: RequestCommand
,
64 schema
: JSONSchemaType
<T
>,
67 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
70 const validate
= this.getJsonRequestValidateFunction
<T
>(commandName
, schema
)
71 if (validate(payload
)) {
75 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
79 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
80 'Response PDU is invalid',
82 JSON
.stringify(validate
.errors
, undefined, 2)
86 protected emptyResponseHandler
= Constants
.EMPTY_FUNCTION
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))
95 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
96 return this.jsonRequestValidateFunctions
.get(commandName
)!
99 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
100 chargingStation
: ChargingStation
,
101 commandName
: RequestCommand
,
103 requestPayload
: ReqType