1 import _Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv'
2 import _ajvFormats from
'ajv-formats'
4 import { OCPPConstants
} from
'./OCPPConstants.js'
5 import { OCPPServiceUtils
} from
'./OCPPServiceUtils.js'
6 import { type ChargingStation
, getIdTagsFile
} from
'../../charging-station/index.js'
7 import { OCPPError
} from
'../../exception/index.js'
11 IncomingRequestCommand
,
14 } from
'../../types/index.js'
15 import { logger
, setDefaultErrorParams
} from
'../../utils/index.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
= 'OCPPIncomingRequestService'
23 export abstract class OCPPIncomingRequestService
{
24 private static instance
: OCPPIncomingRequestService
| null = null
25 private readonly version
: OCPPVersion
26 private readonly ajv
: Ajv
27 private readonly jsonValidateFunctions
: Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>
28 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonType
>>
30 protected constructor (version
: OCPPVersion
) {
31 this.version
= version
33 keywords
: ['javaType'],
34 multipleOfPrecision
: 2
37 this.jsonValidateFunctions
= new Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>()
38 this.incomingRequestHandler
= this.incomingRequestHandler
.bind(this) as <
39 ReqType
extends JsonType
,
40 // eslint-disable-next-line @typescript-eslint/no-unused-vars
41 ResType
extends JsonType
,
43 chargingStation
: ChargingStation
,
45 commandName
: IncomingRequestCommand
,
46 commandPayload
: ReqType
48 this.validateIncomingRequestPayload
= this.validateIncomingRequestPayload
.bind(this) as <
51 chargingStation
: ChargingStation
,
52 commandName
: IncomingRequestCommand
,
53 schema
: JSONSchemaType
<T
>,
58 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
59 if (OCPPIncomingRequestService
.instance
=== null) {
60 OCPPIncomingRequestService
.instance
= new this()
62 return OCPPIncomingRequestService
.instance
as T
65 protected handleIncomingRequestError
<T
extends JsonType
>(
66 chargingStation
: ChargingStation
,
67 commandName
: IncomingRequestCommand
,
69 params
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false }
71 setDefaultErrorParams(params
)
73 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
76 if (params
.throwError
=== false && params
.errorResponse
!= null) {
77 return params
.errorResponse
79 if (params
.throwError
=== true && params
.errorResponse
== null) {
82 if (params
.throwError
=== true && params
.errorResponse
!= null) {
83 return params
.errorResponse
87 protected validateIncomingRequestPayload
<T
extends JsonType
>(
88 chargingStation
: ChargingStation
,
89 commandName
: IncomingRequestCommand
,
90 schema
: JSONSchemaType
<T
>,
93 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
96 const validate
= this.getJsonIncomingRequestValidateFunction
<T
>(commandName
, schema
)
97 if (validate(payload
)) {
101 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
105 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
106 'Incoming request PDU is invalid',
108 JSON
.stringify(validate
.errors
, undefined, 2)
112 protected handleRequestClearCache (chargingStation
: ChargingStation
): ClearCacheResponse
{
113 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
114 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
!)!)) {
115 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
117 return OCPPConstants
.OCPP_RESPONSE_REJECTED
120 private getJsonIncomingRequestValidateFunction
<T
extends JsonType
>(
121 commandName
: IncomingRequestCommand
,
122 schema
: JSONSchemaType
<T
>
123 ): ValidateFunction
<JsonType
> {
124 if (!this.jsonValidateFunctions
.has(commandName
)) {
125 this.jsonValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this))
127 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
128 return this.jsonValidateFunctions
.get(commandName
)!
131 // eslint-disable-next-line @typescript-eslint/no-unused-vars
132 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
133 chargingStation
: ChargingStation
,
135 commandName
: IncomingRequestCommand
,
136 commandPayload
: ReqType