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'
15 type Ajv
= _Ajv
.default
16 // eslint-disable-next-line @typescript-eslint/no-redeclare
17 const Ajv
= _Ajv
.default
18 const ajvFormats
= _ajvFormats
.default
20 const moduleName
= 'OCPPResponseService'
22 export abstract class OCPPResponseService
{
23 private static instance
: null | OCPPResponseService
= null
24 protected readonly ajv
: Ajv
25 protected readonly ajvIncomingRequest
: Ajv
26 protected emptyResponseHandler
= Constants
.EMPTY_FUNCTION
27 protected abstract payloadValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>
28 private readonly version
: OCPPVersion
29 public abstract incomingRequestResponsePayloadValidateFunctions
: Map
<
30 IncomingRequestCommand
,
31 ValidateFunction
<JsonType
>
34 protected constructor (version
: OCPPVersion
) {
35 this.version
= version
37 keywords
: ['javaType'],
38 multipleOfPrecision
: 2,
41 this.ajvIncomingRequest
= new Ajv({
42 keywords
: ['javaType'],
43 multipleOfPrecision
: 2,
45 ajvFormats(this.ajvIncomingRequest
)
46 this.responseHandler
= this.responseHandler
.bind(this)
47 this.validateResponsePayload
= this.validateResponsePayload
.bind(this)
50 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
51 if (OCPPResponseService
.instance
=== null) {
52 OCPPResponseService
.instance
= new this()
54 return OCPPResponseService
.instance
as T
57 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
58 protected validateResponsePayload
<T
extends JsonType
>(
59 chargingStation
: ChargingStation
,
60 commandName
: RequestCommand
,
63 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
66 const validate
= this.payloadValidateFunctions
.get(commandName
)
67 if (validate
?.(payload
) === true) {
71 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
75 ajvErrorsToErrorType(validate
?.errors
),
76 'Response PDU is invalid',
78 JSON
.stringify(validate
?.errors
, undefined, 2)
82 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
83 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
84 chargingStation
: ChargingStation
,
85 commandName
: RequestCommand
,
87 requestPayload
: ReqType