1 import _Ajv
, { 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
23 private readonly version
: OCPPVersion
24 protected readonly ajv
: Ajv
25 protected abstract jsonSchemasValidateFunction
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>
26 public abstract jsonSchemasIncomingRequestResponseValidateFunction
: Map
<
27 IncomingRequestCommand
,
28 ValidateFunction
<JsonType
>
31 protected constructor (version
: OCPPVersion
) {
32 this.version
= version
34 keywords
: ['javaType'],
35 multipleOfPrecision
: 2
38 this.responseHandler
= this.responseHandler
.bind(this)
39 this.validateResponsePayload
= this.validateResponsePayload
.bind(this)
42 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
43 if (OCPPResponseService
.instance
=== null) {
44 OCPPResponseService
.instance
= new this()
46 return OCPPResponseService
.instance
as T
49 protected validateResponsePayload
<T
extends JsonType
>(
50 chargingStation
: ChargingStation
,
51 commandName
: RequestCommand
,
54 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
57 const validate
= this.jsonSchemasValidateFunction
.get(commandName
)
58 if (validate
?.(payload
) === true) {
62 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
66 OCPPServiceUtils
.ajvErrorsToErrorType(validate
?.errors
),
67 'Response PDU is invalid',
69 JSON
.stringify(validate
?.errors
, undefined, 2)
73 protected emptyResponseHandler
= Constants
.EMPTY_FUNCTION
75 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
76 chargingStation
: ChargingStation
,
77 commandName
: RequestCommand
,
79 requestPayload
: ReqType