]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blame - src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
chore(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20IncomingRequestService.ts
CommitLineData
a19b897d 1// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
953d6b02 2
24d15716 3import type { ValidateFunction } from 'ajv'
953d6b02 4
66a7748d 5import type { ChargingStation } from '../../../charging-station/index.js'
0749233f 6
66a7748d 7import { OCPPError } from '../../../exception/index.js'
d270cc87 8import {
268a74bb
JB
9 ErrorType,
10 type IncomingRequestHandler,
268a74bb 11 type JsonType,
81533a20 12 type OCPP20ClearCacheRequest,
d270cc87 13 OCPP20IncomingRequestCommand,
d1f5bfd8 14 OCPPVersion,
66a7748d 15} from '../../../types/index.js'
bcf95df1 16import { isAsyncFunction, logger } from '../../../utils/index.js'
66a7748d 17import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js'
4c3f6c20 18import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js'
953d6b02 19
66a7748d 20const moduleName = 'OCPP20IncomingRequestService'
953d6b02 21
268a74bb 22export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
d5490a13 23 protected payloadValidateFunctions: Map<OCPP20IncomingRequestCommand, ValidateFunction<JsonType>>
24d15716 24
66a7748d 25 private readonly incomingRequestHandlers: Map<
d1f5bfd8
JB
26 OCPP20IncomingRequestCommand,
27 IncomingRequestHandler
66a7748d 28 >
953d6b02 29
66a7748d 30 public constructor () {
5199f9fd
JB
31 // if (new.target.name === moduleName) {
32 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
b768993d 33 // }
1feac591 34 super(OCPPVersion.VERSION_201)
d270cc87 35 this.incomingRequestHandlers = new Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>([
d1f5bfd8 36 [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
66a7748d 37 ])
d5490a13 38 this.payloadValidateFunctions = new Map<
d1f5bfd8
JB
39 OCPP20IncomingRequestCommand,
40 ValidateFunction<JsonType>
24d15716 41 >([
d270cc87
JB
42 [
43 OCPP20IncomingRequestCommand.CLEAR_CACHE,
d712a9a3
JB
44 this.ajv.compile(
45 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
46 'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
47 moduleName,
48 'constructor'
49 )
50 ),
d1f5bfd8 51 ],
66a7748d 52 ])
ba9a56a6 53 this.validatePayload = this.validatePayload.bind(this)
953d6b02
JB
54 }
55
01841aad 56 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
9429aa42 57 public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
953d6b02
JB
58 chargingStation: ChargingStation,
59 messageId: string,
60 commandName: OCPP20IncomingRequestCommand,
66a7748d 61 commandPayload: ReqType
953d6b02 62 ): Promise<void> {
66a7748d 63 let response: ResType
953d6b02 64 if (
5398cecf 65 chargingStation.stationInfo?.ocppStrictCompliance === true &&
66a7748d 66 chargingStation.inPendingState() &&
81533a20
JB
67 (commandName === OCPP20IncomingRequestCommand.REQUEST_START_TRANSACTION ||
68 commandName === OCPP20IncomingRequestCommand.REQUEST_STOP_TRANSACTION)
953d6b02
JB
69 ) {
70 throw new OCPPError(
71 ErrorType.SECURITY_ERROR,
72 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
73 commandPayload,
4ed03b6e 74 undefined,
66a7748d 75 2
953d6b02
JB
76 )} while the charging station is in pending state on the central server`,
77 commandName,
66a7748d
JB
78 commandPayload
79 )
953d6b02
JB
80 }
81 if (
b72d1749
JB
82 chargingStation.inAcceptedState() ||
83 chargingStation.inPendingState() ||
5398cecf 84 (chargingStation.stationInfo?.ocppStrictCompliance === false &&
66a7748d 85 chargingStation.inUnknownState())
953d6b02
JB
86 ) {
87 if (
66a7748d
JB
88 this.incomingRequestHandlers.has(commandName) &&
89 OCPP20ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName)
953d6b02
JB
90 ) {
91 try {
66a7748d 92 this.validatePayload(chargingStation, commandName, commandPayload)
953d6b02 93 // Call the method to build the response
66a7748d 94 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bcf95df1
JB
95 const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)!
96 if (isAsyncFunction(incomingRequestHandler)) {
97 response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType
98 } else {
99 response = incomingRequestHandler(chargingStation, commandPayload) as ResType
100 }
953d6b02
JB
101 } catch (error) {
102 // Log
103 logger.error(
104 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
66a7748d
JB
105 error
106 )
107 throw error
953d6b02
JB
108 }
109 } else {
110 // Throw exception
111 throw new OCPPError(
112 ErrorType.NOT_IMPLEMENTED,
3024d5b2 113 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
953d6b02 114 commandPayload,
4ed03b6e 115 undefined,
66a7748d 116 2
953d6b02
JB
117 )}`,
118 commandName,
66a7748d
JB
119 commandPayload
120 )
953d6b02
JB
121 }
122 } else {
123 throw new OCPPError(
124 ErrorType.SECURITY_ERROR,
125 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
126 commandPayload,
4ed03b6e 127 undefined,
66a7748d 128 2
412cece8 129 )} while the charging station is not registered on the central server`,
953d6b02 130 commandName,
66a7748d
JB
131 commandPayload
132 )
953d6b02
JB
133 }
134 // Send the built response
135 await chargingStation.ocppRequestService.sendResponse(
136 chargingStation,
137 messageId,
138 response,
66a7748d
JB
139 commandName
140 )
868c6830 141 // Emit command name event to allow delayed handling
1b7eb386 142 this.emit(commandName, chargingStation, commandPayload, response)
953d6b02 143 }
c4a89082
JB
144
145 private validatePayload (
146 chargingStation: ChargingStation,
147 commandName: OCPP20IncomingRequestCommand,
148 commandPayload: JsonType
149 ): boolean {
150 if (this.payloadValidateFunctions.has(commandName)) {
151 return this.validateIncomingRequestPayload(chargingStation, commandName, commandPayload)
152 }
153 logger.warn(
154 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`
155 )
156 return false
157 }
953d6b02 158}