1 import { EventEmitter
} from
'node:events'
3 import _Ajv
, { type ValidateFunction
} from
'ajv'
4 import _ajvFormats from
'ajv-formats'
6 import { type ChargingStation
, getIdTagsFile
} from
'../../charging-station/index.js'
7 import { OCPPError
} from
'../../exception/index.js'
10 IncomingRequestCommand
,
13 } from
'../../types/index.js'
14 import { logger
} from
'../../utils/index.js'
15 import { OCPPConstants
} from
'./OCPPConstants.js'
16 import { ajvErrorsToErrorType
} from
'./OCPPServiceUtils.js'
17 type Ajv
= _Ajv
.default
18 // eslint-disable-next-line @typescript-eslint/no-redeclare
19 const Ajv
= _Ajv
.default
20 const ajvFormats
= _ajvFormats
.default
22 const moduleName
= 'OCPPIncomingRequestService'
24 export abstract class OCPPIncomingRequestService
extends EventEmitter
{
25 private static instance
: OCPPIncomingRequestService
| null = null
26 private readonly version
: OCPPVersion
27 protected readonly ajv
: Ajv
28 protected abstract payloadValidateFunctions
: Map
<
29 IncomingRequestCommand
,
30 ValidateFunction
<JsonType
>
33 protected constructor (version
: OCPPVersion
) {
35 this.version
= version
37 keywords
: ['javaType'],
38 multipleOfPrecision
: 2
41 this.incomingRequestHandler
= this.incomingRequestHandler
.bind(this)
42 this.validateIncomingRequestPayload
= this.validateIncomingRequestPayload
.bind(this)
45 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
46 if (OCPPIncomingRequestService
.instance
=== null) {
47 OCPPIncomingRequestService
.instance
= new this()
49 return OCPPIncomingRequestService
.instance
as T
52 protected validateIncomingRequestPayload
<T
extends JsonType
>(
53 chargingStation
: ChargingStation
,
54 commandName
: IncomingRequestCommand
,
57 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
60 const validate
= this.payloadValidateFunctions
.get(commandName
)
61 if (validate
?.(payload
) === true) {
65 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
69 ajvErrorsToErrorType(validate
?.errors
),
70 'Incoming request PDU is invalid',
72 JSON
.stringify(validate
?.errors
, undefined, 2)
76 protected handleRequestClearCache (chargingStation
: ChargingStation
): ClearCacheResponse
{
77 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
78 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
!)!)) {
79 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
81 return OCPPConstants
.OCPP_RESPONSE_REJECTED
84 // eslint-disable-next-line @typescript-eslint/no-unused-vars
85 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
86 chargingStation
: ChargingStation
,
88 commandName
: IncomingRequestCommand
,
89 commandPayload
: ReqType