1 import _Ajv
, { type ValidateFunction
} from
'ajv'
2 import _ajvFormats from
'ajv-formats'
4 import type { ChargingStation
} from
'../../charging-station/index.js'
6 IncomingRequestCommand
,
10 } from
'../../types/index.js'
12 import { OCPPError
} from
'../../exception/index.js'
13 import { Constants
, logger
} from
'../../utils/index.js'
14 import { ajvErrorsToErrorType
} from
'./OCPPServiceUtils.js'
16 type Ajv
= _Ajv
.default
17 // eslint-disable-next-line @typescript-eslint/no-redeclare
18 const Ajv
= _Ajv
.default
19 const ajvFormats
= _ajvFormats
.default
21 const moduleName
= 'OCPPResponseService'
23 export abstract class OCPPResponseService
{
24 private static instance
: null | OCPPResponseService
= null
25 public abstract incomingRequestResponsePayloadValidateFunctions
: Map
<
26 IncomingRequestCommand
,
27 ValidateFunction
<JsonType
>
30 protected readonly ajv
: Ajv
31 protected readonly ajvIncomingRequest
: Ajv
32 protected emptyResponseHandler
= Constants
.EMPTY_FUNCTION
33 protected abstract payloadValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>
34 private readonly version
: OCPPVersion
36 protected constructor (version
: OCPPVersion
) {
37 this.version
= version
39 keywords
: ['javaType'],
40 multipleOfPrecision
: 2,
43 this.ajvIncomingRequest
= new Ajv({
44 keywords
: ['javaType'],
45 multipleOfPrecision
: 2,
47 ajvFormats(this.ajvIncomingRequest
)
48 this.responseHandler
= this.responseHandler
.bind(this)
49 this.validateResponsePayload
= this.validateResponsePayload
.bind(this)
52 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
53 OCPPResponseService
.instance
??= new this()
54 return OCPPResponseService
.instance
as T
57 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
58 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
59 chargingStation
: ChargingStation
,
60 commandName
: RequestCommand
,
62 requestPayload
: ReqType
65 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
66 protected validateResponsePayload
<T
extends JsonType
>(
67 chargingStation
: ChargingStation
,
68 commandName
: RequestCommand
,
71 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
74 const validate
= this.payloadValidateFunctions
.get(commandName
)
75 if (validate
?.(payload
) === true) {
79 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
83 ajvErrorsToErrorType(validate
?.errors
),
84 'Response PDU is invalid',
86 JSON
.stringify(validate
?.errors
, undefined, 2)