Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
953d6b02 JB |
2 | |
3 | import type { JSONSchemaType } from 'ajv'; | |
4 | ||
2896e06d | 5 | import type { ChargingStation } from '../../../charging-station'; |
268a74bb | 6 | import { OCPPError } from '../../../exception'; |
d270cc87 | 7 | import { |
268a74bb JB |
8 | ErrorType, |
9 | type IncomingRequestHandler, | |
10 | type JsonObject, | |
11 | type JsonType, | |
81533a20 | 12 | type OCPP20ClearCacheRequest, |
d270cc87 | 13 | OCPP20IncomingRequestCommand, |
268a74bb JB |
14 | OCPPVersion, |
15 | } from '../../../types'; | |
60a74391 | 16 | import { logger } from '../../../utils'; |
2896e06d | 17 | import { OCPP20ServiceUtils, OCPPIncomingRequestService } from '../internal'; |
953d6b02 JB |
18 | |
19 | const moduleName = 'OCPP20IncomingRequestService'; | |
20 | ||
268a74bb | 21 | export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { |
b3fc3ff5 | 22 | protected jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
953d6b02 | 23 | private incomingRequestHandlers: Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>; |
953d6b02 JB |
24 | |
25 | public constructor() { | |
b768993d JB |
26 | // if (new.target?.name === moduleName) { |
27 | // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); | |
28 | // } | |
d270cc87 JB |
29 | super(OCPPVersion.VERSION_20); |
30 | this.incomingRequestHandlers = new Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>([ | |
31 | [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], | |
32 | ]); | |
33 | this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([ | |
34 | [ | |
35 | OCPP20IncomingRequestCommand.CLEAR_CACHE, | |
130783a7 | 36 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>( |
1b271a54 JB |
37 | '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json', |
38 | moduleName, | |
39 | 'constructor' | |
e9a4164c | 40 | ), |
d270cc87 JB |
41 | ], |
42 | ]); | |
31f59c6d JB |
43 | this.validatePayload = this.validatePayload.bind(this) as ( |
44 | chargingStation: ChargingStation, | |
45 | commandName: OCPP20IncomingRequestCommand, | |
46 | commandPayload: JsonType | |
47 | ) => boolean; | |
953d6b02 JB |
48 | } |
49 | ||
50 | public async incomingRequestHandler( | |
51 | chargingStation: ChargingStation, | |
52 | messageId: string, | |
53 | commandName: OCPP20IncomingRequestCommand, | |
54 | commandPayload: JsonType | |
55 | ): Promise<void> { | |
56 | let response: JsonType; | |
57 | if ( | |
58 | chargingStation.getOcppStrictCompliance() === true && | |
81533a20 JB |
59 | chargingStation.isInPendingState() === true && |
60 | (commandName === OCPP20IncomingRequestCommand.REQUEST_START_TRANSACTION || | |
61 | commandName === OCPP20IncomingRequestCommand.REQUEST_STOP_TRANSACTION) | |
953d6b02 JB |
62 | ) { |
63 | throw new OCPPError( | |
64 | ErrorType.SECURITY_ERROR, | |
65 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( | |
66 | commandPayload, | |
67 | null, | |
68 | 2 | |
69 | )} while the charging station is in pending state on the central server`, | |
70 | commandName, | |
71 | commandPayload | |
72 | ); | |
73 | } | |
74 | if ( | |
75 | chargingStation.isRegistered() === true || | |
76 | (chargingStation.getOcppStrictCompliance() === false && | |
77 | chargingStation.isInUnknownState() === true) | |
78 | ) { | |
79 | if ( | |
80 | this.incomingRequestHandlers.has(commandName) === true && | |
81 | OCPP20ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName) === true | |
82 | ) { | |
83 | try { | |
84 | this.validatePayload(chargingStation, commandName, commandPayload); | |
85 | // Call the method to build the response | |
86 | response = await this.incomingRequestHandlers.get(commandName)( | |
87 | chargingStation, | |
88 | commandPayload | |
89 | ); | |
90 | } catch (error) { | |
91 | // Log | |
92 | logger.error( | |
93 | `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`, | |
94 | error | |
95 | ); | |
96 | throw error; | |
97 | } | |
98 | } else { | |
99 | // Throw exception | |
100 | throw new OCPPError( | |
101 | ErrorType.NOT_IMPLEMENTED, | |
102 | `${commandName} is not implemented to handle request PDU ${JSON.stringify( | |
103 | commandPayload, | |
104 | null, | |
105 | 2 | |
106 | )}`, | |
107 | commandName, | |
108 | commandPayload | |
109 | ); | |
110 | } | |
111 | } else { | |
112 | throw new OCPPError( | |
113 | ErrorType.SECURITY_ERROR, | |
114 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( | |
115 | commandPayload, | |
116 | null, | |
117 | 2 | |
118 | )} while the charging station is not registered on the central server.`, | |
119 | commandName, | |
120 | commandPayload | |
121 | ); | |
122 | } | |
123 | // Send the built response | |
124 | await chargingStation.ocppRequestService.sendResponse( | |
125 | chargingStation, | |
126 | messageId, | |
127 | response, | |
128 | commandName | |
129 | ); | |
130 | } | |
131 | ||
132 | private validatePayload( | |
133 | chargingStation: ChargingStation, | |
134 | commandName: OCPP20IncomingRequestCommand, | |
135 | commandPayload: JsonType | |
136 | ): boolean { | |
45988780 | 137 | if (this.jsonSchemas.has(commandName) === true) { |
953d6b02 JB |
138 | return this.validateIncomingRequestPayload( |
139 | chargingStation, | |
140 | commandName, | |
141 | this.jsonSchemas.get(commandName), | |
142 | commandPayload | |
143 | ); | |
144 | } | |
145 | logger.warn( | |
b3fc3ff5 | 146 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation` |
953d6b02 JB |
147 | ); |
148 | return false; | |
149 | } | |
150 | } |