Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c8eeb62b | 2 | |
d972af76 JB |
3 | import { createWriteStream, readdirSync } from 'node:fs'; |
4 | import { dirname, join, resolve } from 'node:path'; | |
130783a7 | 5 | import { URL, fileURLToPath } from 'node:url'; |
8114d10e | 6 | |
6c1761d4 | 7 | import type { JSONSchemaType } from 'ajv'; |
27782dbc | 8 | import { Client, type FTPResponse } from 'basic-ftp'; |
be4c6702 | 9 | import { secondsToMilliseconds } from 'date-fns'; |
d972af76 | 10 | import { create } from 'tar'; |
8114d10e | 11 | |
4c3c0d59 JB |
12 | import { OCPP16Constants } from './OCPP16Constants'; |
13 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; | |
2896e06d JB |
14 | import { |
15 | type ChargingStation, | |
fba11dc6 | 16 | checkChargingStation, |
f2d5e3d9 JB |
17 | getConfigurationKey, |
18 | setConfigurationKeyValue, | |
2896e06d | 19 | } from '../../../charging-station'; |
268a74bb | 20 | import { OCPPError } from '../../../exception'; |
e7aeea18 | 21 | import { |
27782dbc | 22 | type ChangeAvailabilityRequest, |
268a74bb | 23 | type ChangeAvailabilityResponse, |
27782dbc | 24 | type ChangeConfigurationRequest, |
268a74bb | 25 | type ChangeConfigurationResponse, |
27782dbc | 26 | type ClearChargingProfileRequest, |
268a74bb | 27 | type ClearChargingProfileResponse, |
4334db72 | 28 | type ConnectorStatus, |
268a74bb JB |
29 | ErrorType, |
30 | type GenericResponse, | |
41189456 | 31 | GenericStatus, |
27782dbc | 32 | type GetConfigurationRequest, |
268a74bb | 33 | type GetConfigurationResponse, |
27782dbc | 34 | type GetDiagnosticsRequest, |
268a74bb JB |
35 | type GetDiagnosticsResponse, |
36 | type IncomingRequestHandler, | |
37 | type JsonObject, | |
38 | type JsonType, | |
39 | OCPP16AuthorizationStatus, | |
e7aeea18 | 40 | OCPP16AvailabilityType, |
27782dbc | 41 | type OCPP16BootNotificationRequest, |
268a74bb | 42 | type OCPP16BootNotificationResponse, |
66dd3447 | 43 | type OCPP16CancelReservationRequest, |
268a74bb JB |
44 | OCPP16ChargePointErrorCode, |
45 | OCPP16ChargePointStatus, | |
46 | type OCPP16ChargingProfile, | |
0ac97927 | 47 | OCPP16ChargingProfilePurposeType, |
41189456 | 48 | type OCPP16ChargingSchedule, |
27782dbc JB |
49 | type OCPP16ClearCacheRequest, |
50 | type OCPP16DataTransferRequest, | |
268a74bb | 51 | type OCPP16DataTransferResponse, |
77b95a89 | 52 | OCPP16DataTransferVendorId, |
268a74bb | 53 | OCPP16DiagnosticsStatus, |
c9a4f9ea | 54 | type OCPP16DiagnosticsStatusNotificationRequest, |
268a74bb | 55 | type OCPP16DiagnosticsStatusNotificationResponse, |
c9a4f9ea JB |
56 | OCPP16FirmwareStatus, |
57 | type OCPP16FirmwareStatusNotificationRequest, | |
268a74bb | 58 | type OCPP16FirmwareStatusNotificationResponse, |
41189456 JB |
59 | type OCPP16GetCompositeScheduleRequest, |
60 | type OCPP16GetCompositeScheduleResponse, | |
27782dbc | 61 | type OCPP16HeartbeatRequest, |
268a74bb | 62 | type OCPP16HeartbeatResponse, |
e7aeea18 | 63 | OCPP16IncomingRequestCommand, |
c60ed4b8 | 64 | OCPP16MessageTrigger, |
94a464f9 | 65 | OCPP16RequestCommand, |
66dd3447 JB |
66 | type OCPP16ReserveNowRequest, |
67 | type OCPP16ReserveNowResponse, | |
268a74bb JB |
68 | OCPP16StandardParametersKey, |
69 | type OCPP16StartTransactionRequest, | |
70 | type OCPP16StartTransactionResponse, | |
27782dbc | 71 | type OCPP16StatusNotificationRequest, |
268a74bb JB |
72 | type OCPP16StatusNotificationResponse, |
73 | OCPP16StopTransactionReason, | |
74 | OCPP16SupportedFeatureProfiles, | |
27782dbc | 75 | type OCPP16TriggerMessageRequest, |
268a74bb | 76 | type OCPP16TriggerMessageResponse, |
27782dbc | 77 | type OCPP16UpdateFirmwareRequest, |
268a74bb JB |
78 | type OCPP16UpdateFirmwareResponse, |
79 | type OCPPConfigurationKey, | |
80 | OCPPVersion, | |
27782dbc JB |
81 | type RemoteStartTransactionRequest, |
82 | type RemoteStopTransactionRequest, | |
66dd3447 | 83 | ReservationTerminationReason, |
27782dbc JB |
84 | type ResetRequest, |
85 | type SetChargingProfileRequest, | |
27782dbc | 86 | type SetChargingProfileResponse, |
66dd3447 | 87 | type StartTransactionRequest, |
268a74bb | 88 | type UnlockConnectorRequest, |
27782dbc | 89 | type UnlockConnectorResponse, |
268a74bb | 90 | } from '../../../types'; |
9bf0ef23 JB |
91 | import { |
92 | Constants, | |
93 | convertToDate, | |
94 | convertToInt, | |
95 | formatDurationMilliSeconds, | |
96 | getRandomInteger, | |
97 | isEmptyArray, | |
98 | isNotEmptyArray, | |
99 | isNotEmptyString, | |
100 | isNullOrUndefined, | |
101 | isUndefined, | |
102 | logger, | |
103 | sleep, | |
104 | } from '../../../utils'; | |
4c3c0d59 | 105 | import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService'; |
c0560973 | 106 | |
2a115f87 | 107 | const moduleName = 'OCPP16IncomingRequestService'; |
909dcf2d | 108 | |
268a74bb | 109 | export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { |
b3fc3ff5 | 110 | protected jsonSchemas: Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
58144adb JB |
111 | private incomingRequestHandlers: Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>; |
112 | ||
08f130a0 | 113 | public constructor() { |
b768993d JB |
114 | // if (new.target?.name === moduleName) { |
115 | // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); | |
116 | // } | |
d270cc87 | 117 | super(OCPPVersion.VERSION_16); |
58144adb | 118 | this.incomingRequestHandlers = new Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>([ |
a37fc6dc JB |
119 | [ |
120 | OCPP16IncomingRequestCommand.RESET, | |
121 | this.handleRequestReset.bind(this) as unknown as IncomingRequestHandler, | |
122 | ], | |
123 | [ | |
124 | OCPP16IncomingRequestCommand.CLEAR_CACHE, | |
125 | this.handleRequestClearCache.bind(this) as IncomingRequestHandler, | |
126 | ], | |
127 | [ | |
128 | OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, | |
129 | this.handleRequestUnlockConnector.bind(this) as unknown as IncomingRequestHandler, | |
130 | ], | |
e7aeea18 JB |
131 | [ |
132 | OCPP16IncomingRequestCommand.GET_CONFIGURATION, | |
a37fc6dc | 133 | this.handleRequestGetConfiguration.bind(this) as IncomingRequestHandler, |
e7aeea18 JB |
134 | ], |
135 | [ | |
136 | OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, | |
a37fc6dc | 137 | this.handleRequestChangeConfiguration.bind(this) as unknown as IncomingRequestHandler, |
e7aeea18 | 138 | ], |
41189456 JB |
139 | [ |
140 | OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, | |
a37fc6dc | 141 | this.handleRequestGetCompositeSchedule.bind(this) as unknown as IncomingRequestHandler, |
41189456 | 142 | ], |
e7aeea18 JB |
143 | [ |
144 | OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, | |
a37fc6dc | 145 | this.handleRequestSetChargingProfile.bind(this) as unknown as IncomingRequestHandler, |
e7aeea18 JB |
146 | ], |
147 | [ | |
148 | OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, | |
a37fc6dc | 149 | this.handleRequestClearChargingProfile.bind(this) as IncomingRequestHandler, |
e7aeea18 JB |
150 | ], |
151 | [ | |
152 | OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, | |
a37fc6dc | 153 | this.handleRequestChangeAvailability.bind(this) as unknown as IncomingRequestHandler, |
e7aeea18 JB |
154 | ], |
155 | [ | |
156 | OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, | |
a37fc6dc | 157 | this.handleRequestRemoteStartTransaction.bind(this) as unknown as IncomingRequestHandler, |
e7aeea18 JB |
158 | ], |
159 | [ | |
160 | OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, | |
a37fc6dc JB |
161 | this.handleRequestRemoteStopTransaction.bind(this) as unknown as IncomingRequestHandler, |
162 | ], | |
163 | [ | |
164 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, | |
165 | this.handleRequestGetDiagnostics.bind(this) as IncomingRequestHandler, | |
166 | ], | |
167 | [ | |
168 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, | |
169 | this.handleRequestTriggerMessage.bind(this) as unknown as IncomingRequestHandler, | |
170 | ], | |
171 | [ | |
172 | OCPP16IncomingRequestCommand.DATA_TRANSFER, | |
173 | this.handleRequestDataTransfer.bind(this) as unknown as IncomingRequestHandler, | |
174 | ], | |
175 | [ | |
176 | OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, | |
177 | this.handleRequestUpdateFirmware.bind(this) as unknown as IncomingRequestHandler, | |
178 | ], | |
179 | [ | |
180 | OCPP16IncomingRequestCommand.RESERVE_NOW, | |
181 | this.handleRequestReserveNow.bind(this) as unknown as IncomingRequestHandler, | |
e7aeea18 | 182 | ], |
d193a949 JB |
183 | [ |
184 | OCPP16IncomingRequestCommand.CANCEL_RESERVATION, | |
a37fc6dc | 185 | this.handleRequestCancelReservation.bind(this) as unknown as IncomingRequestHandler, |
d193a949 | 186 | ], |
58144adb | 187 | ]); |
b52c969d JB |
188 | this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>([ |
189 | [ | |
190 | OCPP16IncomingRequestCommand.RESET, | |
130783a7 | 191 | OCPP16ServiceUtils.parseJsonSchemaFile<ResetRequest>( |
51022aa0 | 192 | 'assets/json-schemas/ocpp/1.6/Reset.json', |
1b271a54 | 193 | moduleName, |
5edd8ba0 | 194 | 'constructor', |
130783a7 | 195 | ), |
b52c969d JB |
196 | ], |
197 | [ | |
198 | OCPP16IncomingRequestCommand.CLEAR_CACHE, | |
130783a7 | 199 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16ClearCacheRequest>( |
51022aa0 | 200 | 'assets/json-schemas/ocpp/1.6/ClearCache.json', |
1b271a54 | 201 | moduleName, |
5edd8ba0 | 202 | 'constructor', |
e9a4164c | 203 | ), |
b52c969d JB |
204 | ], |
205 | [ | |
206 | OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, | |
130783a7 | 207 | OCPP16ServiceUtils.parseJsonSchemaFile<UnlockConnectorRequest>( |
51022aa0 | 208 | 'assets/json-schemas/ocpp/1.6/UnlockConnector.json', |
1b271a54 | 209 | moduleName, |
5edd8ba0 | 210 | 'constructor', |
e9a4164c | 211 | ), |
b52c969d JB |
212 | ], |
213 | [ | |
214 | OCPP16IncomingRequestCommand.GET_CONFIGURATION, | |
130783a7 | 215 | OCPP16ServiceUtils.parseJsonSchemaFile<GetConfigurationRequest>( |
51022aa0 | 216 | 'assets/json-schemas/ocpp/1.6/GetConfiguration.json', |
1b271a54 | 217 | moduleName, |
5edd8ba0 | 218 | 'constructor', |
e9a4164c | 219 | ), |
b52c969d JB |
220 | ], |
221 | [ | |
222 | OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, | |
130783a7 | 223 | OCPP16ServiceUtils.parseJsonSchemaFile<ChangeConfigurationRequest>( |
51022aa0 | 224 | 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json', |
1b271a54 | 225 | moduleName, |
5edd8ba0 | 226 | 'constructor', |
e9a4164c | 227 | ), |
b52c969d JB |
228 | ], |
229 | [ | |
230 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, | |
130783a7 | 231 | OCPP16ServiceUtils.parseJsonSchemaFile<GetDiagnosticsRequest>( |
51022aa0 | 232 | 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json', |
1b271a54 | 233 | moduleName, |
5edd8ba0 | 234 | 'constructor', |
e9a4164c | 235 | ), |
b52c969d | 236 | ], |
41189456 JB |
237 | [ |
238 | OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, | |
239 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16GetCompositeScheduleRequest>( | |
51022aa0 | 240 | 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json', |
41189456 | 241 | moduleName, |
5edd8ba0 | 242 | 'constructor', |
41189456 JB |
243 | ), |
244 | ], | |
b52c969d JB |
245 | [ |
246 | OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, | |
130783a7 | 247 | OCPP16ServiceUtils.parseJsonSchemaFile<SetChargingProfileRequest>( |
51022aa0 | 248 | 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json', |
1b271a54 | 249 | moduleName, |
5edd8ba0 | 250 | 'constructor', |
e9a4164c | 251 | ), |
b52c969d JB |
252 | ], |
253 | [ | |
254 | OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, | |
130783a7 | 255 | OCPP16ServiceUtils.parseJsonSchemaFile<ClearChargingProfileRequest>( |
51022aa0 | 256 | 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json', |
1b271a54 | 257 | moduleName, |
5edd8ba0 | 258 | 'constructor', |
e9a4164c | 259 | ), |
b52c969d JB |
260 | ], |
261 | [ | |
262 | OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, | |
130783a7 | 263 | OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityRequest>( |
51022aa0 | 264 | 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json', |
1b271a54 | 265 | moduleName, |
5edd8ba0 | 266 | 'constructor', |
e9a4164c | 267 | ), |
b52c969d JB |
268 | ], |
269 | [ | |
270 | OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, | |
130783a7 | 271 | OCPP16ServiceUtils.parseJsonSchemaFile<RemoteStartTransactionRequest>( |
51022aa0 | 272 | 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json', |
1b271a54 | 273 | moduleName, |
5edd8ba0 | 274 | 'constructor', |
e9a4164c | 275 | ), |
b52c969d JB |
276 | ], |
277 | [ | |
278 | OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, | |
130783a7 | 279 | OCPP16ServiceUtils.parseJsonSchemaFile<RemoteStopTransactionRequest>( |
51022aa0 | 280 | 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json', |
1b271a54 | 281 | moduleName, |
5edd8ba0 | 282 | 'constructor', |
e9a4164c | 283 | ), |
b52c969d JB |
284 | ], |
285 | [ | |
286 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, | |
130783a7 | 287 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16TriggerMessageRequest>( |
51022aa0 | 288 | 'assets/json-schemas/ocpp/1.6/TriggerMessage.json', |
1b271a54 | 289 | moduleName, |
5edd8ba0 | 290 | 'constructor', |
e9a4164c | 291 | ), |
b52c969d | 292 | ], |
77b95a89 JB |
293 | [ |
294 | OCPP16IncomingRequestCommand.DATA_TRANSFER, | |
130783a7 | 295 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferRequest>( |
51022aa0 | 296 | 'assets/json-schemas/ocpp/1.6/DataTransfer.json', |
1b271a54 | 297 | moduleName, |
5edd8ba0 | 298 | 'constructor', |
e9a4164c | 299 | ), |
77b95a89 | 300 | ], |
bfbda738 JB |
301 | [ |
302 | OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, | |
130783a7 | 303 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16UpdateFirmwareRequest>( |
51022aa0 | 304 | 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json', |
1b271a54 | 305 | moduleName, |
5edd8ba0 | 306 | 'constructor', |
e9a4164c | 307 | ), |
bfbda738 | 308 | ], |
d193a949 JB |
309 | [ |
310 | OCPP16IncomingRequestCommand.RESERVE_NOW, | |
311 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16ReserveNowRequest>( | |
312 | 'assets/json-schemas/ocpp/1.6/ReserveNow.json', | |
313 | moduleName, | |
5edd8ba0 | 314 | 'constructor', |
d193a949 JB |
315 | ), |
316 | ], | |
317 | [ | |
318 | OCPP16IncomingRequestCommand.CANCEL_RESERVATION, | |
319 | OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16CancelReservationRequest>( | |
320 | 'assets/json-schemas/ocpp/1.6/CancelReservation.json', | |
321 | moduleName, | |
5edd8ba0 | 322 | 'constructor', |
d193a949 JB |
323 | ), |
324 | ], | |
b52c969d | 325 | ]); |
31f59c6d JB |
326 | this.validatePayload = this.validatePayload.bind(this) as ( |
327 | chargingStation: ChargingStation, | |
328 | commandName: OCPP16IncomingRequestCommand, | |
5edd8ba0 | 329 | commandPayload: JsonType, |
31f59c6d | 330 | ) => boolean; |
58144adb JB |
331 | } |
332 | ||
9429aa42 | 333 | public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( |
08f130a0 | 334 | chargingStation: ChargingStation, |
e7aeea18 JB |
335 | messageId: string, |
336 | commandName: OCPP16IncomingRequestCommand, | |
9429aa42 | 337 | commandPayload: ReqType, |
e7aeea18 | 338 | ): Promise<void> { |
9429aa42 | 339 | let response: ResType; |
e7aeea18 | 340 | if ( |
3a13fc92 | 341 | chargingStation.getOcppStrictCompliance() === true && |
f7c2994d | 342 | chargingStation.inPendingState() === true && |
e7aeea18 JB |
343 | (commandName === OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION || |
344 | commandName === OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION) | |
345 | ) { | |
346 | throw new OCPPError( | |
347 | ErrorType.SECURITY_ERROR, | |
e3018bc4 | 348 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( |
e7aeea18 JB |
349 | commandPayload, |
350 | null, | |
5edd8ba0 | 351 | 2, |
e7aeea18 | 352 | )} while the charging station is in pending state on the central server`, |
7369e417 | 353 | commandName, |
5edd8ba0 | 354 | commandPayload, |
e7aeea18 | 355 | ); |
caad9d6b | 356 | } |
e7aeea18 | 357 | if ( |
ed6cfcff JB |
358 | chargingStation.isRegistered() === true || |
359 | (chargingStation.getOcppStrictCompliance() === false && | |
f7c2994d | 360 | chargingStation.inUnknownState() === true) |
e7aeea18 | 361 | ) { |
65554cc3 | 362 | if ( |
ed6cfcff JB |
363 | this.incomingRequestHandlers.has(commandName) === true && |
364 | OCPP16ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName) === true | |
65554cc3 | 365 | ) { |
124f3553 | 366 | try { |
9c5c4195 | 367 | this.validatePayload(chargingStation, commandName, commandPayload); |
c75a6675 | 368 | // Call the method to build the response |
9429aa42 | 369 | response = (await this.incomingRequestHandlers.get(commandName)!( |
08f130a0 | 370 | chargingStation, |
5edd8ba0 | 371 | commandPayload, |
9429aa42 | 372 | )) as ResType; |
124f3553 JB |
373 | } catch (error) { |
374 | // Log | |
6c8f5d90 | 375 | logger.error( |
66dd3447 JB |
376 | `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: |
377 | Handle incoming request error:`, | |
5edd8ba0 | 378 | error, |
6c8f5d90 | 379 | ); |
124f3553 JB |
380 | throw error; |
381 | } | |
382 | } else { | |
383 | // Throw exception | |
e7aeea18 JB |
384 | throw new OCPPError( |
385 | ErrorType.NOT_IMPLEMENTED, | |
e3018bc4 | 386 | `${commandName} is not implemented to handle request PDU ${JSON.stringify( |
e7aeea18 JB |
387 | commandPayload, |
388 | null, | |
5edd8ba0 | 389 | 2, |
e7aeea18 | 390 | )}`, |
7369e417 | 391 | commandName, |
5edd8ba0 | 392 | commandPayload, |
e7aeea18 | 393 | ); |
c0560973 JB |
394 | } |
395 | } else { | |
e7aeea18 JB |
396 | throw new OCPPError( |
397 | ErrorType.SECURITY_ERROR, | |
e3018bc4 | 398 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( |
e7aeea18 JB |
399 | commandPayload, |
400 | null, | |
5edd8ba0 | 401 | 2, |
e7aeea18 | 402 | )} while the charging station is not registered on the central server.`, |
7369e417 | 403 | commandName, |
5edd8ba0 | 404 | commandPayload, |
e7aeea18 | 405 | ); |
c0560973 | 406 | } |
c75a6675 | 407 | // Send the built response |
08f130a0 JB |
408 | await chargingStation.ocppRequestService.sendResponse( |
409 | chargingStation, | |
410 | messageId, | |
411 | response, | |
5edd8ba0 | 412 | commandName, |
08f130a0 | 413 | ); |
c0560973 JB |
414 | } |
415 | ||
9c5c4195 JB |
416 | private validatePayload( |
417 | chargingStation: ChargingStation, | |
418 | commandName: OCPP16IncomingRequestCommand, | |
5edd8ba0 | 419 | commandPayload: JsonType, |
9c5c4195 | 420 | ): boolean { |
45988780 | 421 | if (this.jsonSchemas.has(commandName) === true) { |
9c5c4195 JB |
422 | return this.validateIncomingRequestPayload( |
423 | chargingStation, | |
424 | commandName, | |
e1d9a0f4 | 425 | this.jsonSchemas.get(commandName)!, |
5edd8ba0 | 426 | commandPayload, |
9c5c4195 JB |
427 | ); |
428 | } | |
429 | logger.warn( | |
66dd3447 | 430 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found |
5edd8ba0 | 431 | for command '${commandName}' PDU validation`, |
9c5c4195 JB |
432 | ); |
433 | return false; | |
434 | } | |
435 | ||
c0560973 | 436 | // Simulate charging station restart |
08f130a0 JB |
437 | private handleRequestReset( |
438 | chargingStation: ChargingStation, | |
5edd8ba0 | 439 | commandPayload: ResetRequest, |
f03e1042 | 440 | ): GenericResponse { |
27f08ad3 JB |
441 | this.runInAsyncScope( |
442 | chargingStation.reset.bind(chargingStation) as ( | |
443 | this: ChargingStation, | |
e843aa40 | 444 | ...args: unknown[] |
27f08ad3 JB |
445 | ) => Promise<void>, |
446 | chargingStation, | |
5edd8ba0 | 447 | `${commandPayload.type}Reset` as OCPP16StopTransactionReason, |
59b6ed8d | 448 | ).catch(Constants.EMPTY_FUNCTION); |
e7aeea18 | 449 | logger.info( |
08f130a0 | 450 | `${chargingStation.logPrefix()} ${ |
e7aeea18 | 451 | commandPayload.type |
66dd3447 | 452 | } reset command received, simulating it. The station will be |
e1d9a0f4 | 453 | back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`, |
e7aeea18 | 454 | ); |
d8b1fab1 | 455 | return OCPP16Constants.OCPP_RESPONSE_ACCEPTED; |
c0560973 JB |
456 | } |
457 | ||
e7aeea18 | 458 | private async handleRequestUnlockConnector( |
08f130a0 | 459 | chargingStation: ChargingStation, |
5edd8ba0 | 460 | commandPayload: UnlockConnectorRequest, |
e7aeea18 | 461 | ): Promise<UnlockConnectorResponse> { |
c0560973 | 462 | const connectorId = commandPayload.connectorId; |
a14022a2 | 463 | if (chargingStation.hasConnector(connectorId) === false) { |
c60ed4b8 | 464 | logger.error( |
66dd3447 | 465 | `${chargingStation.logPrefix()} Trying to unlock a non existing |
5edd8ba0 | 466 | connector id ${connectorId.toString()}`, |
c60ed4b8 | 467 | ); |
d8b1fab1 | 468 | return OCPP16Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; |
c60ed4b8 | 469 | } |
c0560973 | 470 | if (connectorId === 0) { |
e7aeea18 | 471 | logger.error( |
5edd8ba0 | 472 | `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`, |
e7aeea18 | 473 | ); |
d8b1fab1 | 474 | return OCPP16Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; |
c0560973 | 475 | } |
5e3cb728 JB |
476 | if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { |
477 | const stopResponse = await chargingStation.stopTransactionOnConnector( | |
478 | connectorId, | |
5edd8ba0 | 479 | OCPP16StopTransactionReason.UNLOCK_COMMAND, |
5e3cb728 | 480 | ); |
c0560973 | 481 | if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
d8b1fab1 | 482 | return OCPP16Constants.OCPP_RESPONSE_UNLOCKED; |
c0560973 | 483 | } |
d8b1fab1 | 484 | return OCPP16Constants.OCPP_RESPONSE_UNLOCK_FAILED; |
c0560973 | 485 | } |
4ecff7ce JB |
486 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( |
487 | chargingStation, | |
ef6fa3fb | 488 | connectorId, |
5edd8ba0 | 489 | OCPP16ChargePointStatus.Available, |
4ecff7ce | 490 | ); |
d8b1fab1 | 491 | return OCPP16Constants.OCPP_RESPONSE_UNLOCKED; |
c0560973 JB |
492 | } |
493 | ||
e7aeea18 | 494 | private handleRequestGetConfiguration( |
08f130a0 | 495 | chargingStation: ChargingStation, |
5edd8ba0 | 496 | commandPayload: GetConfigurationRequest, |
e7aeea18 | 497 | ): GetConfigurationResponse { |
c0560973 JB |
498 | const configurationKey: OCPPConfigurationKey[] = []; |
499 | const unknownKey: string[] = []; | |
f568f368 JB |
500 | if (isUndefined(commandPayload.key) === true) { |
501 | for (const configuration of chargingStation.ocppConfiguration!.configurationKey!) { | |
9bf0ef23 | 502 | if (isUndefined(configuration.visible) === true) { |
7f7b65ca | 503 | configuration.visible = true; |
c0560973 | 504 | } |
da8629bb | 505 | if (configuration.visible === false) { |
c0560973 JB |
506 | continue; |
507 | } | |
508 | configurationKey.push({ | |
7f7b65ca JB |
509 | key: configuration.key, |
510 | readonly: configuration.readonly, | |
511 | value: configuration.value, | |
c0560973 JB |
512 | }); |
513 | } | |
f568f368 JB |
514 | } else if (isNotEmptyArray(commandPayload.key) === true) { |
515 | for (const key of commandPayload.key!) { | |
f2d5e3d9 | 516 | const keyFound = getConfigurationKey(chargingStation, key, true); |
c0560973 | 517 | if (keyFound) { |
9bf0ef23 | 518 | if (isUndefined(keyFound.visible) === true) { |
c0560973 JB |
519 | keyFound.visible = true; |
520 | } | |
a723e7e9 | 521 | if (keyFound.visible === false) { |
c0560973 JB |
522 | continue; |
523 | } | |
524 | configurationKey.push({ | |
525 | key: keyFound.key, | |
526 | readonly: keyFound.readonly, | |
527 | value: keyFound.value, | |
528 | }); | |
529 | } else { | |
530 | unknownKey.push(key); | |
531 | } | |
532 | } | |
533 | } | |
534 | return { | |
535 | configurationKey, | |
536 | unknownKey, | |
537 | }; | |
538 | } | |
539 | ||
e7aeea18 | 540 | private handleRequestChangeConfiguration( |
08f130a0 | 541 | chargingStation: ChargingStation, |
5edd8ba0 | 542 | commandPayload: ChangeConfigurationRequest, |
e7aeea18 | 543 | ): ChangeConfigurationResponse { |
f2d5e3d9 | 544 | const keyToChange = getConfigurationKey(chargingStation, commandPayload.key, true); |
e1d9a0f4 | 545 | if (keyToChange?.readonly === true) { |
d8b1fab1 | 546 | return OCPP16Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED; |
bd5d98e0 | 547 | } else if (keyToChange?.readonly === false) { |
c0560973 | 548 | let valueChanged = false; |
a95873d8 | 549 | if (keyToChange.value !== commandPayload.value) { |
f2d5e3d9 | 550 | setConfigurationKeyValue(chargingStation, commandPayload.key, commandPayload.value, true); |
c0560973 JB |
551 | valueChanged = true; |
552 | } | |
553 | let triggerHeartbeatRestart = false; | |
e1d9a0f4 JB |
554 | if ( |
555 | (keyToChange.key as OCPP16StandardParametersKey) === | |
556 | OCPP16StandardParametersKey.HeartBeatInterval && | |
557 | valueChanged | |
558 | ) { | |
f2d5e3d9 | 559 | setConfigurationKeyValue( |
17ac262c | 560 | chargingStation, |
e7aeea18 | 561 | OCPP16StandardParametersKey.HeartbeatInterval, |
5edd8ba0 | 562 | commandPayload.value, |
e7aeea18 | 563 | ); |
c0560973 JB |
564 | triggerHeartbeatRestart = true; |
565 | } | |
e1d9a0f4 JB |
566 | if ( |
567 | (keyToChange.key as OCPP16StandardParametersKey) === | |
568 | OCPP16StandardParametersKey.HeartbeatInterval && | |
569 | valueChanged | |
570 | ) { | |
f2d5e3d9 | 571 | setConfigurationKeyValue( |
17ac262c | 572 | chargingStation, |
e7aeea18 | 573 | OCPP16StandardParametersKey.HeartBeatInterval, |
5edd8ba0 | 574 | commandPayload.value, |
e7aeea18 | 575 | ); |
c0560973 JB |
576 | triggerHeartbeatRestart = true; |
577 | } | |
578 | if (triggerHeartbeatRestart) { | |
08f130a0 | 579 | chargingStation.restartHeartbeat(); |
c0560973 | 580 | } |
e1d9a0f4 JB |
581 | if ( |
582 | (keyToChange.key as OCPP16StandardParametersKey) === | |
583 | OCPP16StandardParametersKey.WebSocketPingInterval && | |
584 | valueChanged | |
585 | ) { | |
08f130a0 | 586 | chargingStation.restartWebSocketPing(); |
c0560973 JB |
587 | } |
588 | if (keyToChange.reboot) { | |
d8b1fab1 | 589 | return OCPP16Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; |
c0560973 | 590 | } |
d8b1fab1 | 591 | return OCPP16Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; |
c0560973 | 592 | } |
e1d9a0f4 | 593 | return OCPP16Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; |
c0560973 JB |
594 | } |
595 | ||
e7aeea18 | 596 | private handleRequestSetChargingProfile( |
08f130a0 | 597 | chargingStation: ChargingStation, |
5edd8ba0 | 598 | commandPayload: SetChargingProfileRequest, |
e7aeea18 | 599 | ): SetChargingProfileResponse { |
370ae4ee | 600 | if ( |
1789ba2c | 601 | OCPP16ServiceUtils.checkFeatureProfile( |
08f130a0 | 602 | chargingStation, |
370ae4ee | 603 | OCPP16SupportedFeatureProfiles.SmartCharging, |
5edd8ba0 | 604 | OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, |
1789ba2c | 605 | ) === false |
370ae4ee | 606 | ) { |
d8b1fab1 | 607 | return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; |
68cb8b91 | 608 | } |
a14022a2 | 609 | if (chargingStation.hasConnector(commandPayload.connectorId) === false) { |
e7aeea18 | 610 | logger.error( |
66dd3447 | 611 | `${chargingStation.logPrefix()} Trying to set charging profile(s) to a |
5edd8ba0 | 612 | non existing connector id ${commandPayload.connectorId}`, |
e7aeea18 | 613 | ); |
d8b1fab1 | 614 | return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
c0560973 | 615 | } |
e7aeea18 JB |
616 | if ( |
617 | commandPayload.csChargingProfiles.chargingProfilePurpose === | |
0ac97927 | 618 | OCPP16ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && |
e7aeea18 JB |
619 | commandPayload.connectorId !== 0 |
620 | ) { | |
d8b1fab1 | 621 | return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
c0560973 | 622 | } |
e7aeea18 JB |
623 | if ( |
624 | commandPayload.csChargingProfiles.chargingProfilePurpose === | |
0ac97927 | 625 | OCPP16ChargingProfilePurposeType.TX_PROFILE && |
e7aeea18 | 626 | (commandPayload.connectorId === 0 || |
5e3cb728 JB |
627 | chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted === |
628 | false) | |
e7aeea18 | 629 | ) { |
db0af086 | 630 | logger.error( |
66dd3447 | 631 | `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) |
5edd8ba0 | 632 | on connector ${commandPayload.connectorId} without a started transaction`, |
db0af086 | 633 | ); |
d8b1fab1 | 634 | return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
c0560973 | 635 | } |
ed3d2808 | 636 | OCPP16ServiceUtils.setChargingProfile( |
7cb5b17f | 637 | chargingStation, |
e7aeea18 | 638 | commandPayload.connectorId, |
5edd8ba0 | 639 | commandPayload.csChargingProfiles, |
e7aeea18 JB |
640 | ); |
641 | logger.debug( | |
08f130a0 | 642 | `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${ |
ad8537a7 | 643 | commandPayload.connectorId |
ad67a158 | 644 | }: %j`, |
5edd8ba0 | 645 | commandPayload.csChargingProfiles, |
e7aeea18 | 646 | ); |
d8b1fab1 | 647 | return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
c0560973 JB |
648 | } |
649 | ||
41189456 JB |
650 | private handleRequestGetCompositeSchedule( |
651 | chargingStation: ChargingStation, | |
5edd8ba0 | 652 | commandPayload: OCPP16GetCompositeScheduleRequest, |
41189456 JB |
653 | ): OCPP16GetCompositeScheduleResponse { |
654 | if ( | |
655 | OCPP16ServiceUtils.checkFeatureProfile( | |
656 | chargingStation, | |
657 | OCPP16SupportedFeatureProfiles.SmartCharging, | |
e1d9a0f4 | 658 | OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, |
41189456 JB |
659 | ) === false |
660 | ) { | |
d8b1fab1 | 661 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; |
41189456 | 662 | } |
a14022a2 | 663 | if (chargingStation.hasConnector(commandPayload.connectorId) === false) { |
41189456 | 664 | logger.error( |
66dd3447 | 665 | `${chargingStation.logPrefix()} Trying to get composite schedule to a |
5edd8ba0 | 666 | non existing connector id ${commandPayload.connectorId}`, |
41189456 | 667 | ); |
d8b1fab1 | 668 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; |
41189456 JB |
669 | } |
670 | if ( | |
9bf0ef23 | 671 | isEmptyArray(chargingStation.getConnectorStatus(commandPayload.connectorId)?.chargingProfiles) |
41189456 | 672 | ) { |
d8b1fab1 | 673 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; |
41189456 JB |
674 | } |
675 | const startDate = new Date(); | |
be4c6702 | 676 | const endDate = new Date(startDate.getTime() + secondsToMilliseconds(commandPayload.duration)); |
e1d9a0f4 JB |
677 | let compositeSchedule: OCPP16ChargingSchedule | undefined; |
678 | for (const chargingProfile of chargingStation.getConnectorStatus(commandPayload.connectorId)! | |
679 | .chargingProfiles!) { | |
41189456 JB |
680 | // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc. |
681 | if ( | |
e1d9a0f4 JB |
682 | chargingProfile.chargingSchedule.startSchedule! >= startDate && |
683 | chargingProfile.chargingSchedule.startSchedule! <= endDate | |
41189456 JB |
684 | ) { |
685 | compositeSchedule = chargingProfile.chargingSchedule; | |
686 | break; | |
687 | } | |
688 | } | |
689 | return { | |
690 | status: GenericStatus.Accepted, | |
691 | scheduleStart: compositeSchedule?.startSchedule, | |
692 | connectorId: commandPayload.connectorId, | |
693 | chargingSchedule: compositeSchedule, | |
694 | }; | |
695 | } | |
696 | ||
e7aeea18 | 697 | private handleRequestClearChargingProfile( |
08f130a0 | 698 | chargingStation: ChargingStation, |
5edd8ba0 | 699 | commandPayload: ClearChargingProfileRequest, |
e7aeea18 | 700 | ): ClearChargingProfileResponse { |
370ae4ee | 701 | if ( |
a36bad10 | 702 | OCPP16ServiceUtils.checkFeatureProfile( |
08f130a0 | 703 | chargingStation, |
370ae4ee | 704 | OCPP16SupportedFeatureProfiles.SmartCharging, |
5edd8ba0 | 705 | OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, |
a36bad10 | 706 | ) === false |
370ae4ee | 707 | ) { |
d8b1fab1 | 708 | return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
68cb8b91 | 709 | } |
e1d9a0f4 | 710 | if (chargingStation.hasConnector(commandPayload.connectorId!) === false) { |
e7aeea18 | 711 | logger.error( |
66dd3447 | 712 | `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to |
5edd8ba0 | 713 | a non existing connector id ${commandPayload.connectorId}`, |
e7aeea18 | 714 | ); |
d8b1fab1 | 715 | return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
c0560973 | 716 | } |
d812bdcb | 717 | if ( |
9bf0ef23 JB |
718 | !isNullOrUndefined(commandPayload.connectorId) && |
719 | isNotEmptyArray( | |
e1d9a0f4 | 720 | chargingStation.getConnectorStatus(commandPayload.connectorId!)?.chargingProfiles, |
4334db72 | 721 | ) |
d812bdcb | 722 | ) { |
e1d9a0f4 | 723 | chargingStation.getConnectorStatus(commandPayload.connectorId!)!.chargingProfiles = []; |
e7aeea18 | 724 | logger.debug( |
08f130a0 | 725 | `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${ |
ad8537a7 | 726 | commandPayload.connectorId |
5edd8ba0 | 727 | }`, |
e7aeea18 | 728 | ); |
d8b1fab1 | 729 | return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
c0560973 | 730 | } |
9bf0ef23 | 731 | if (isNullOrUndefined(commandPayload.connectorId)) { |
c0560973 | 732 | let clearedCP = false; |
4334db72 JB |
733 | if (chargingStation.hasEvses) { |
734 | for (const evseStatus of chargingStation.evses.values()) { | |
735 | for (const connectorStatus of evseStatus.connectors.values()) { | |
73d87be1 JB |
736 | clearedCP = OCPP16ServiceUtils.clearChargingProfiles( |
737 | chargingStation, | |
738 | commandPayload, | |
739 | connectorStatus.chargingProfiles, | |
740 | ); | |
4334db72 JB |
741 | } |
742 | } | |
743 | } else { | |
744 | for (const connectorId of chargingStation.connectors.keys()) { | |
73d87be1 JB |
745 | clearedCP = OCPP16ServiceUtils.clearChargingProfiles( |
746 | chargingStation, | |
747 | commandPayload, | |
748 | chargingStation.getConnectorStatus(connectorId)?.chargingProfiles, | |
749 | ); | |
c0560973 JB |
750 | } |
751 | } | |
752 | if (clearedCP) { | |
d8b1fab1 | 753 | return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
c0560973 JB |
754 | } |
755 | } | |
d8b1fab1 | 756 | return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
c0560973 JB |
757 | } |
758 | ||
e7aeea18 | 759 | private async handleRequestChangeAvailability( |
08f130a0 | 760 | chargingStation: ChargingStation, |
5edd8ba0 | 761 | commandPayload: ChangeAvailabilityRequest, |
e7aeea18 | 762 | ): Promise<ChangeAvailabilityResponse> { |
c0560973 | 763 | const connectorId: number = commandPayload.connectorId; |
a14022a2 | 764 | if (chargingStation.hasConnector(connectorId) === false) { |
e7aeea18 | 765 | logger.error( |
66dd3447 | 766 | `${chargingStation.logPrefix()} Trying to change the availability of a |
5edd8ba0 | 767 | non existing connector id ${connectorId.toString()}`, |
e7aeea18 | 768 | ); |
d8b1fab1 | 769 | return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; |
c0560973 | 770 | } |
e7aeea18 | 771 | const chargePointStatus: OCPP16ChargePointStatus = |
0d6f335f | 772 | commandPayload.type === OCPP16AvailabilityType.Operative |
721646e9 JB |
773 | ? OCPP16ChargePointStatus.Available |
774 | : OCPP16ChargePointStatus.Unavailable; | |
c0560973 | 775 | if (connectorId === 0) { |
d8b1fab1 JB |
776 | let response: ChangeAvailabilityResponse = |
777 | OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; | |
ded57f02 JB |
778 | const changeAvailability = async (id: number, connectorStatus: ConnectorStatus) => { |
779 | if (connectorStatus?.transactionStarted === true) { | |
d8b1fab1 | 780 | response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
c0560973 | 781 | } |
ded57f02 | 782 | connectorStatus.availability = commandPayload.type; |
d8b1fab1 | 783 | if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { |
4ecff7ce JB |
784 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( |
785 | chargingStation, | |
786 | id, | |
5edd8ba0 | 787 | chargePointStatus, |
4ecff7ce | 788 | ); |
c0560973 | 789 | } |
ded57f02 JB |
790 | }; |
791 | if (chargingStation.hasEvses) { | |
792 | for (const evseStatus of chargingStation.evses.values()) { | |
793 | for (const [id, connectorStatus] of evseStatus.connectors) { | |
794 | await changeAvailability(id, connectorStatus); | |
795 | } | |
796 | } | |
797 | } else { | |
798 | for (const id of chargingStation.connectors.keys()) { | |
e1d9a0f4 | 799 | await changeAvailability(id, chargingStation.getConnectorStatus(id)!); |
ded57f02 | 800 | } |
c0560973 JB |
801 | } |
802 | return response; | |
e7aeea18 JB |
803 | } else if ( |
804 | connectorId > 0 && | |
56eb297e JB |
805 | (chargingStation.isChargingStationAvailable() === true || |
806 | (chargingStation.isChargingStationAvailable() === false && | |
0d6f335f | 807 | commandPayload.type === OCPP16AvailabilityType.Inoperative)) |
e7aeea18 | 808 | ) { |
5e3cb728 | 809 | if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { |
e1d9a0f4 | 810 | chargingStation.getConnectorStatus(connectorId)!.availability = commandPayload.type; |
d8b1fab1 | 811 | return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
c0560973 | 812 | } |
e1d9a0f4 | 813 | chargingStation.getConnectorStatus(connectorId)!.availability = commandPayload.type; |
4ecff7ce JB |
814 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( |
815 | chargingStation, | |
ef6fa3fb | 816 | connectorId, |
5edd8ba0 | 817 | chargePointStatus, |
4ecff7ce | 818 | ); |
d8b1fab1 | 819 | return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; |
c0560973 | 820 | } |
d8b1fab1 | 821 | return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; |
c0560973 JB |
822 | } |
823 | ||
e7aeea18 | 824 | private async handleRequestRemoteStartTransaction( |
08f130a0 | 825 | chargingStation: ChargingStation, |
5edd8ba0 | 826 | commandPayload: RemoteStartTransactionRequest, |
f03e1042 | 827 | ): Promise<GenericResponse> { |
66dd3447 | 828 | const { connectorId: transactionConnectorId, idTag, chargingProfile } = commandPayload; |
d193a949 | 829 | const reserved = |
e1d9a0f4 | 830 | chargingStation.getConnectorStatus(transactionConnectorId)!.status === |
24578c31 | 831 | OCPP16ChargePointStatus.Reserved; |
66dd3447 | 832 | const reservedOnConnectorZero = |
e1d9a0f4 | 833 | chargingStation.getConnectorStatus(0)!.status === OCPP16ChargePointStatus.Reserved; |
d193a949 | 834 | if ( |
66dd3447 JB |
835 | (reserved && |
836 | !chargingStation.validateIncomingRequestWithReservation(transactionConnectorId, idTag)) || | |
837 | (reservedOnConnectorZero && !chargingStation.validateIncomingRequestWithReservation(0, idTag)) | |
d193a949 JB |
838 | ) { |
839 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; | |
840 | } | |
649287f8 JB |
841 | if (chargingStation.hasConnector(transactionConnectorId) === false) { |
842 | return this.notifyRemoteStartTransactionRejected( | |
4ecff7ce JB |
843 | chargingStation, |
844 | transactionConnectorId, | |
5edd8ba0 | 845 | idTag, |
4ecff7ce | 846 | ); |
649287f8 JB |
847 | } |
848 | if ( | |
d193a949 JB |
849 | !chargingStation.isChargingStationAvailable() || |
850 | !chargingStation.isConnectorAvailable(transactionConnectorId) | |
649287f8 JB |
851 | ) { |
852 | return this.notifyRemoteStartTransactionRejected( | |
853 | chargingStation, | |
854 | transactionConnectorId, | |
5edd8ba0 | 855 | idTag, |
649287f8 JB |
856 | ); |
857 | } | |
66dd3447 JB |
858 | const remoteStartTransactionLogMsg = ` |
859 | ${chargingStation.logPrefix()} Transaction remotely STARTED on ${ | |
5edd8ba0 JB |
860 | chargingStation.stationInfo.chargingStationId |
861 | }#${transactionConnectorId.toString()} for idTag '${idTag}'`; | |
649287f8 JB |
862 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( |
863 | chargingStation, | |
864 | transactionConnectorId, | |
5edd8ba0 | 865 | OCPP16ChargePointStatus.Preparing, |
649287f8 | 866 | ); |
e1d9a0f4 | 867 | const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId)!; |
649287f8 | 868 | // Check if authorized |
66dd3447 JB |
869 | if ( |
870 | chargingStation.getAuthorizeRemoteTxRequests() && | |
871 | (await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, transactionConnectorId, idTag)) | |
872 | ) { | |
873 | // Authorization successful, start transaction | |
874 | if ( | |
875 | this.setRemoteStartTransactionChargingProfile( | |
876 | chargingStation, | |
877 | transactionConnectorId, | |
e1d9a0f4 | 878 | chargingProfile!, |
66dd3447 JB |
879 | ) === true |
880 | ) { | |
881 | connectorStatus.transactionRemoteStarted = true; | |
882 | const startTransactionPayload: Partial<StartTransactionRequest> = { | |
883 | connectorId: transactionConnectorId, | |
178956d8 | 884 | idTag, |
66dd3447 JB |
885 | }; |
886 | if (reserved || reservedOnConnectorZero) { | |
887 | const reservation = chargingStation.getReservationBy( | |
2ca0ea90 | 888 | 'connectorId', |
5edd8ba0 | 889 | reservedOnConnectorZero ? 0 : transactionConnectorId, |
e1d9a0f4 | 890 | )!; |
282582e5 | 891 | startTransactionPayload.reservationId = reservation.reservationId; |
66dd3447 JB |
892 | await chargingStation.removeReservation( |
893 | reservation, | |
5edd8ba0 | 894 | ReservationTerminationReason.TRANSACTION_STARTED, |
66dd3447 JB |
895 | ); |
896 | } | |
e7aeea18 | 897 | if ( |
66dd3447 JB |
898 | ( |
899 | await chargingStation.ocppRequestService.requestHandler< | |
900 | OCPP16StartTransactionRequest, | |
901 | OCPP16StartTransactionResponse | |
902 | >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, startTransactionPayload) | |
903 | ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED | |
e7aeea18 | 904 | ) { |
eb79c525 | 905 | logger.debug(remoteStartTransactionLogMsg); |
66dd3447 | 906 | return OCPP16Constants.OCPP_RESPONSE_ACCEPTED; |
e060fe58 | 907 | } |
e7aeea18 | 908 | return this.notifyRemoteStartTransactionRejected( |
08f130a0 | 909 | chargingStation, |
e7aeea18 | 910 | transactionConnectorId, |
5edd8ba0 | 911 | idTag, |
e7aeea18 | 912 | ); |
c0560973 | 913 | } |
e7aeea18 | 914 | return this.notifyRemoteStartTransactionRejected( |
08f130a0 | 915 | chargingStation, |
e7aeea18 | 916 | transactionConnectorId, |
5edd8ba0 | 917 | idTag, |
e7aeea18 | 918 | ); |
c0560973 | 919 | } |
649287f8 JB |
920 | // No authorization check required, start transaction |
921 | if ( | |
922 | this.setRemoteStartTransactionChargingProfile( | |
923 | chargingStation, | |
924 | transactionConnectorId, | |
e1d9a0f4 | 925 | chargingProfile!, |
649287f8 JB |
926 | ) === true |
927 | ) { | |
928 | connectorStatus.transactionRemoteStarted = true; | |
929 | if ( | |
930 | ( | |
931 | await chargingStation.ocppRequestService.requestHandler< | |
932 | OCPP16StartTransactionRequest, | |
933 | OCPP16StartTransactionResponse | |
934 | >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, { | |
935 | connectorId: transactionConnectorId, | |
66dd3447 | 936 | idTag, |
649287f8 JB |
937 | }) |
938 | ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED | |
939 | ) { | |
eb79c525 | 940 | logger.debug(remoteStartTransactionLogMsg); |
649287f8 JB |
941 | return OCPP16Constants.OCPP_RESPONSE_ACCEPTED; |
942 | } | |
943 | return this.notifyRemoteStartTransactionRejected( | |
944 | chargingStation, | |
945 | transactionConnectorId, | |
5edd8ba0 | 946 | idTag, |
649287f8 JB |
947 | ); |
948 | } | |
08f130a0 JB |
949 | return this.notifyRemoteStartTransactionRejected( |
950 | chargingStation, | |
951 | transactionConnectorId, | |
5edd8ba0 | 952 | idTag, |
08f130a0 | 953 | ); |
a7fc8211 JB |
954 | } |
955 | ||
e7aeea18 | 956 | private async notifyRemoteStartTransactionRejected( |
08f130a0 | 957 | chargingStation: ChargingStation, |
e7aeea18 | 958 | connectorId: number, |
5edd8ba0 | 959 | idTag: string, |
f03e1042 | 960 | ): Promise<GenericResponse> { |
e7aeea18 | 961 | if ( |
721646e9 | 962 | chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.Available |
e7aeea18 | 963 | ) { |
4ecff7ce JB |
964 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( |
965 | chargingStation, | |
ef6fa3fb | 966 | connectorId, |
5edd8ba0 | 967 | OCPP16ChargePointStatus.Available, |
4ecff7ce | 968 | ); |
e060fe58 | 969 | } |
e7aeea18 | 970 | logger.warn( |
66dd3447 | 971 | `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id |
5edd8ba0 JB |
972 | ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus( |
973 | connectorId, | |
974 | )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`, | |
e7aeea18 | 975 | ); |
d8b1fab1 | 976 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; |
c0560973 JB |
977 | } |
978 | ||
e7aeea18 | 979 | private setRemoteStartTransactionChargingProfile( |
08f130a0 | 980 | chargingStation: ChargingStation, |
e7aeea18 | 981 | connectorId: number, |
55f2ab60 | 982 | chargingProfile: OCPP16ChargingProfile, |
e7aeea18 | 983 | ): boolean { |
55f2ab60 JB |
984 | if ( |
985 | chargingProfile && | |
986 | chargingProfile.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE | |
987 | ) { | |
988 | OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, chargingProfile); | |
e7aeea18 | 989 | logger.debug( |
66dd3447 JB |
990 | `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction |
991 | on connector id ${connectorId}: %j`, | |
55f2ab60 | 992 | chargingProfile, |
e7aeea18 | 993 | ); |
a7fc8211 | 994 | return true; |
55f2ab60 JB |
995 | } else if ( |
996 | chargingProfile && | |
997 | chargingProfile.chargingProfilePurpose !== OCPP16ChargingProfilePurposeType.TX_PROFILE | |
998 | ) { | |
e7aeea18 | 999 | logger.warn( |
08f130a0 | 1000 | `${chargingStation.logPrefix()} Not allowed to set ${ |
55f2ab60 | 1001 | chargingProfile.chargingProfilePurpose |
5edd8ba0 | 1002 | } charging profile(s) at remote start transaction`, |
e7aeea18 | 1003 | ); |
a7fc8211 JB |
1004 | return false; |
1005 | } | |
e1d9a0f4 | 1006 | return true; |
a7fc8211 JB |
1007 | } |
1008 | ||
e7aeea18 | 1009 | private async handleRequestRemoteStopTransaction( |
08f130a0 | 1010 | chargingStation: ChargingStation, |
5edd8ba0 | 1011 | commandPayload: RemoteStopTransactionRequest, |
f03e1042 | 1012 | ): Promise<GenericResponse> { |
c0560973 | 1013 | const transactionId = commandPayload.transactionId; |
ded57f02 JB |
1014 | const remoteStopTransaction = async (connectorId: number): Promise<GenericResponse> => { |
1015 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( | |
1016 | chargingStation, | |
1017 | connectorId, | |
5edd8ba0 | 1018 | OCPP16ChargePointStatus.Finishing, |
ded57f02 JB |
1019 | ); |
1020 | const stopResponse = await chargingStation.stopTransactionOnConnector( | |
1021 | connectorId, | |
5edd8ba0 | 1022 | OCPP16StopTransactionReason.REMOTE, |
ded57f02 JB |
1023 | ); |
1024 | if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
1025 | return OCPP16Constants.OCPP_RESPONSE_ACCEPTED; | |
1026 | } | |
1027 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; | |
1028 | }; | |
1029 | if (chargingStation.hasEvses) { | |
1030 | for (const [evseId, evseStatus] of chargingStation.evses) { | |
1031 | if (evseId > 0) { | |
1032 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
1033 | if (connectorStatus.transactionId === transactionId) { | |
1034 | return remoteStopTransaction(connectorId); | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | } | |
1039 | } else { | |
1040 | for (const connectorId of chargingStation.connectors.keys()) { | |
1041 | if ( | |
1042 | connectorId > 0 && | |
1043 | chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId | |
1044 | ) { | |
1045 | return remoteStopTransaction(connectorId); | |
ef6fa3fb | 1046 | } |
c0560973 JB |
1047 | } |
1048 | } | |
44b9b577 | 1049 | logger.warn( |
66dd3447 | 1050 | `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id: |
5edd8ba0 | 1051 | ${transactionId.toString()}`, |
e7aeea18 | 1052 | ); |
d8b1fab1 | 1053 | return OCPP16Constants.OCPP_RESPONSE_REJECTED; |
c0560973 | 1054 | } |
47e22477 | 1055 | |
b03df580 JB |
1056 | private handleRequestUpdateFirmware( |
1057 | chargingStation: ChargingStation, | |
5edd8ba0 | 1058 | commandPayload: OCPP16UpdateFirmwareRequest, |
b03df580 JB |
1059 | ): OCPP16UpdateFirmwareResponse { |
1060 | if ( | |
1061 | OCPP16ServiceUtils.checkFeatureProfile( | |
1062 | chargingStation, | |
1063 | OCPP16SupportedFeatureProfiles.FirmwareManagement, | |
5edd8ba0 | 1064 | OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, |
b03df580 JB |
1065 | ) === false |
1066 | ) { | |
5d280aae | 1067 | logger.warn( |
66dd3447 | 1068 | `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: |
5edd8ba0 | 1069 | Cannot simulate firmware update: feature profile not supported`, |
5d280aae | 1070 | ); |
d8b1fab1 | 1071 | return OCPP16Constants.OCPP_RESPONSE_EMPTY; |
5d280aae JB |
1072 | } |
1073 | if ( | |
9bf0ef23 | 1074 | !isNullOrUndefined(chargingStation.stationInfo.firmwareStatus) && |
5d280aae JB |
1075 | chargingStation.stationInfo.firmwareStatus !== OCPP16FirmwareStatus.Installed |
1076 | ) { | |
1077 | logger.warn( | |
66dd3447 | 1078 | `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: |
5edd8ba0 | 1079 | Cannot simulate firmware update: firmware update is already in progress`, |
5d280aae | 1080 | ); |
d8b1fab1 | 1081 | return OCPP16Constants.OCPP_RESPONSE_EMPTY; |
b03df580 | 1082 | } |
e1d9a0f4 | 1083 | const retrieveDate = convertToDate(commandPayload.retrieveDate)!; |
2c7bdc61 | 1084 | const now = Date.now(); |
72092cfc | 1085 | if (retrieveDate?.getTime() <= now) { |
27f08ad3 | 1086 | this.runInAsyncScope( |
62340a29 | 1087 | this.updateFirmwareSimulation.bind(this) as ( |
27f08ad3 | 1088 | this: OCPP16IncomingRequestService, |
e843aa40 | 1089 | ...args: unknown[] |
27f08ad3 JB |
1090 | ) => Promise<void>, |
1091 | this, | |
5edd8ba0 | 1092 | chargingStation, |
59b6ed8d | 1093 | ).catch(Constants.EMPTY_FUNCTION); |
c9a4f9ea | 1094 | } else { |
5edd8ba0 JB |
1095 | setTimeout( |
1096 | () => { | |
1097 | this.runInAsyncScope( | |
1098 | this.updateFirmwareSimulation.bind(this) as ( | |
1099 | this: OCPP16IncomingRequestService, | |
e843aa40 | 1100 | ...args: unknown[] |
5edd8ba0 JB |
1101 | ) => Promise<void>, |
1102 | this, | |
1103 | chargingStation, | |
1104 | ).catch(Constants.EMPTY_FUNCTION); | |
1105 | }, | |
1106 | retrieveDate?.getTime() - now, | |
1107 | ); | |
c9a4f9ea | 1108 | } |
d8b1fab1 | 1109 | return OCPP16Constants.OCPP_RESPONSE_EMPTY; |
c9a4f9ea JB |
1110 | } |
1111 | ||
62340a29 | 1112 | private async updateFirmwareSimulation( |
c9a4f9ea | 1113 | chargingStation: ChargingStation, |
90293abb | 1114 | maxDelay = 30, |
5edd8ba0 | 1115 | minDelay = 15, |
c9a4f9ea | 1116 | ): Promise<void> { |
fba11dc6 | 1117 | if (checkChargingStation(chargingStation, chargingStation.logPrefix()) === false) { |
1bf29f5b JB |
1118 | return; |
1119 | } | |
ded57f02 JB |
1120 | if (chargingStation.hasEvses) { |
1121 | for (const [evseId, evseStatus] of chargingStation.evses) { | |
1122 | if (evseId > 0) { | |
1123 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
1124 | if (connectorStatus?.transactionStarted === false) { | |
1125 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( | |
1126 | chargingStation, | |
1127 | connectorId, | |
5edd8ba0 | 1128 | OCPP16ChargePointStatus.Unavailable, |
ded57f02 JB |
1129 | ); |
1130 | } | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | } else { | |
1135 | for (const connectorId of chargingStation.connectors.keys()) { | |
1136 | if ( | |
1137 | connectorId > 0 && | |
1138 | chargingStation.getConnectorStatus(connectorId)?.transactionStarted === false | |
1139 | ) { | |
1140 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( | |
1141 | chargingStation, | |
1142 | connectorId, | |
5edd8ba0 | 1143 | OCPP16ChargePointStatus.Unavailable, |
ded57f02 JB |
1144 | ); |
1145 | } | |
c9a4f9ea JB |
1146 | } |
1147 | } | |
93f0c2c8 JB |
1148 | await chargingStation.ocppRequestService.requestHandler< |
1149 | OCPP16FirmwareStatusNotificationRequest, | |
1150 | OCPP16FirmwareStatusNotificationResponse | |
1151 | >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
1152 | status: OCPP16FirmwareStatus.Downloading, | |
1153 | }); | |
1154 | chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Downloading; | |
5d280aae | 1155 | if ( |
93f0c2c8 JB |
1156 | chargingStation.stationInfo?.firmwareUpgrade?.failureStatus === |
1157 | OCPP16FirmwareStatus.DownloadFailed | |
5d280aae | 1158 | ) { |
be4c6702 | 1159 | await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))); |
5d280aae JB |
1160 | await chargingStation.ocppRequestService.requestHandler< |
1161 | OCPP16FirmwareStatusNotificationRequest, | |
1162 | OCPP16FirmwareStatusNotificationResponse | |
1163 | >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
15748260 | 1164 | status: chargingStation.stationInfo?.firmwareUpgrade?.failureStatus, |
5d280aae | 1165 | }); |
93f0c2c8 JB |
1166 | chargingStation.stationInfo.firmwareStatus = |
1167 | chargingStation.stationInfo?.firmwareUpgrade?.failureStatus; | |
5d280aae JB |
1168 | return; |
1169 | } | |
be4c6702 | 1170 | await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))); |
c9a4f9ea JB |
1171 | await chargingStation.ocppRequestService.requestHandler< |
1172 | OCPP16FirmwareStatusNotificationRequest, | |
1173 | OCPP16FirmwareStatusNotificationResponse | |
1174 | >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
1175 | status: OCPP16FirmwareStatus.Downloaded, | |
1176 | }); | |
1177 | chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Downloaded; | |
380ccc42 | 1178 | let wasTransactionsStarted = false; |
62340a29 JB |
1179 | let transactionsStarted: boolean; |
1180 | do { | |
ded57f02 JB |
1181 | const runningTransactions = chargingStation.getNumberOfRunningTransactions(); |
1182 | if (runningTransactions > 0) { | |
be4c6702 | 1183 | const waitTime = secondsToMilliseconds(15); |
62340a29 | 1184 | logger.debug( |
66dd3447 | 1185 | `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: |
be4c6702 JB |
1186 | ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds( |
1187 | waitTime, | |
1188 | )} before continuing firmware update simulation`, | |
62340a29 | 1189 | ); |
9bf0ef23 | 1190 | await sleep(waitTime); |
62340a29 | 1191 | transactionsStarted = true; |
380ccc42 | 1192 | wasTransactionsStarted = true; |
62340a29 | 1193 | } else { |
ded57f02 JB |
1194 | if (chargingStation.hasEvses) { |
1195 | for (const [evseId, evseStatus] of chargingStation.evses) { | |
1196 | if (evseId > 0) { | |
1197 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
1198 | if (connectorStatus?.status !== OCPP16ChargePointStatus.Unavailable) { | |
1199 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( | |
1200 | chargingStation, | |
1201 | connectorId, | |
5edd8ba0 | 1202 | OCPP16ChargePointStatus.Unavailable, |
ded57f02 JB |
1203 | ); |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | } | |
1208 | } else { | |
1209 | for (const connectorId of chargingStation.connectors.keys()) { | |
1210 | if ( | |
1211 | connectorId > 0 && | |
1212 | chargingStation.getConnectorStatus(connectorId)?.status !== | |
1213 | OCPP16ChargePointStatus.Unavailable | |
1214 | ) { | |
1215 | await OCPP16ServiceUtils.sendAndSetConnectorStatus( | |
1216 | chargingStation, | |
1217 | connectorId, | |
5edd8ba0 | 1218 | OCPP16ChargePointStatus.Unavailable, |
ded57f02 JB |
1219 | ); |
1220 | } | |
62340a29 JB |
1221 | } |
1222 | } | |
1223 | transactionsStarted = false; | |
1224 | } | |
1225 | } while (transactionsStarted); | |
be4c6702 JB |
1226 | !wasTransactionsStarted && |
1227 | (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)))); | |
fba11dc6 | 1228 | if (checkChargingStation(chargingStation, chargingStation.logPrefix()) === false) { |
1bf29f5b JB |
1229 | return; |
1230 | } | |
c9a4f9ea JB |
1231 | await chargingStation.ocppRequestService.requestHandler< |
1232 | OCPP16FirmwareStatusNotificationRequest, | |
1233 | OCPP16FirmwareStatusNotificationResponse | |
1234 | >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
1235 | status: OCPP16FirmwareStatus.Installing, | |
1236 | }); | |
1237 | chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Installing; | |
93f0c2c8 JB |
1238 | if ( |
1239 | chargingStation.stationInfo?.firmwareUpgrade?.failureStatus === | |
1240 | OCPP16FirmwareStatus.InstallationFailed | |
1241 | ) { | |
be4c6702 | 1242 | await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))); |
93f0c2c8 JB |
1243 | await chargingStation.ocppRequestService.requestHandler< |
1244 | OCPP16FirmwareStatusNotificationRequest, | |
1245 | OCPP16FirmwareStatusNotificationResponse | |
1246 | >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
1247 | status: chargingStation.stationInfo?.firmwareUpgrade?.failureStatus, | |
1248 | }); | |
1249 | chargingStation.stationInfo.firmwareStatus = | |
1250 | chargingStation.stationInfo?.firmwareUpgrade?.failureStatus; | |
1251 | return; | |
1252 | } | |
15748260 | 1253 | if (chargingStation.stationInfo?.firmwareUpgrade?.reset === true) { |
be4c6702 | 1254 | await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))); |
5d280aae JB |
1255 | await chargingStation.reset(OCPP16StopTransactionReason.REBOOT); |
1256 | } | |
b03df580 JB |
1257 | } |
1258 | ||
e7aeea18 | 1259 | private async handleRequestGetDiagnostics( |
08f130a0 | 1260 | chargingStation: ChargingStation, |
5edd8ba0 | 1261 | commandPayload: GetDiagnosticsRequest, |
e7aeea18 | 1262 | ): Promise<GetDiagnosticsResponse> { |
68cb8b91 | 1263 | if ( |
1789ba2c | 1264 | OCPP16ServiceUtils.checkFeatureProfile( |
08f130a0 | 1265 | chargingStation, |
370ae4ee | 1266 | OCPP16SupportedFeatureProfiles.FirmwareManagement, |
5edd8ba0 | 1267 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, |
1789ba2c | 1268 | ) === false |
68cb8b91 | 1269 | ) { |
90293abb | 1270 | logger.warn( |
66dd3447 | 1271 | `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: |
5edd8ba0 | 1272 | Cannot get diagnostics: feature profile not supported`, |
90293abb | 1273 | ); |
d8b1fab1 | 1274 | return OCPP16Constants.OCPP_RESPONSE_EMPTY; |
68cb8b91 | 1275 | } |
a3868ec4 | 1276 | const uri = new URL(commandPayload.location); |
47e22477 | 1277 | if (uri.protocol.startsWith('ftp:')) { |
e1d9a0f4 | 1278 | let ftpClient: Client | undefined; |
47e22477 | 1279 | try { |
d972af76 | 1280 | const logFiles = readdirSync(resolve(dirname(fileURLToPath(import.meta.url)), '../')) |
72092cfc | 1281 | .filter((file) => file.endsWith('.log')) |
d972af76 | 1282 | .map((file) => join('./', file)); |
44eb6026 | 1283 | const diagnosticsArchive = `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`; |
d972af76 | 1284 | create({ gzip: true }, logFiles).pipe(createWriteStream(diagnosticsArchive)); |
47e22477 JB |
1285 | ftpClient = new Client(); |
1286 | const accessResponse = await ftpClient.access({ | |
1287 | host: uri.host, | |
9bf0ef23 JB |
1288 | ...(isNotEmptyString(uri.port) && { port: convertToInt(uri.port) }), |
1289 | ...(isNotEmptyString(uri.username) && { user: uri.username }), | |
1290 | ...(isNotEmptyString(uri.password) && { password: uri.password }), | |
47e22477 | 1291 | }); |
e1d9a0f4 | 1292 | let uploadResponse: FTPResponse | undefined; |
47e22477 | 1293 | if (accessResponse.code === 220) { |
72092cfc | 1294 | ftpClient.trackProgress((info) => { |
e7aeea18 | 1295 | logger.info( |
08f130a0 | 1296 | `${chargingStation.logPrefix()} ${ |
e7aeea18 | 1297 | info.bytes / 1024 |
5edd8ba0 | 1298 | } bytes transferred from diagnostics archive ${info.name}`, |
e7aeea18 | 1299 | ); |
6a8329b4 JB |
1300 | chargingStation.ocppRequestService |
1301 | .requestHandler< | |
1302 | OCPP16DiagnosticsStatusNotificationRequest, | |
1303 | OCPP16DiagnosticsStatusNotificationResponse | |
1304 | >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { | |
1305 | status: OCPP16DiagnosticsStatus.Uploading, | |
1306 | }) | |
72092cfc | 1307 | .catch((error) => { |
6a8329b4 | 1308 | logger.error( |
66dd3447 JB |
1309 | `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: |
1310 | Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`, | |
5edd8ba0 | 1311 | error, |
6a8329b4 JB |
1312 | ); |
1313 | }); | |
47e22477 | 1314 | }); |
e7aeea18 | 1315 | uploadResponse = await ftpClient.uploadFrom( |
d972af76 | 1316 | join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), diagnosticsArchive), |
5edd8ba0 | 1317 | `${uri.pathname}${diagnosticsArchive}`, |
e7aeea18 | 1318 | ); |
47e22477 | 1319 | if (uploadResponse.code === 226) { |
08f130a0 | 1320 | await chargingStation.ocppRequestService.requestHandler< |
c9a4f9ea JB |
1321 | OCPP16DiagnosticsStatusNotificationRequest, |
1322 | OCPP16DiagnosticsStatusNotificationResponse | |
08f130a0 | 1323 | >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { |
ef6fa3fb JB |
1324 | status: OCPP16DiagnosticsStatus.Uploaded, |
1325 | }); | |
47e22477 JB |
1326 | if (ftpClient) { |
1327 | ftpClient.close(); | |
1328 | } | |
1329 | return { fileName: diagnosticsArchive }; | |
1330 | } | |
e7aeea18 JB |
1331 | throw new OCPPError( |
1332 | ErrorType.GENERIC_ERROR, | |
1333 | `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${ | |
44eb6026 | 1334 | uploadResponse?.code && `|${uploadResponse?.code.toString()}` |
e7aeea18 | 1335 | }`, |
5edd8ba0 | 1336 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, |
e7aeea18 | 1337 | ); |
47e22477 | 1338 | } |
e7aeea18 JB |
1339 | throw new OCPPError( |
1340 | ErrorType.GENERIC_ERROR, | |
1341 | `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${ | |
44eb6026 | 1342 | uploadResponse?.code && `|${uploadResponse?.code.toString()}` |
e7aeea18 | 1343 | }`, |
5edd8ba0 | 1344 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, |
e7aeea18 | 1345 | ); |
47e22477 | 1346 | } catch (error) { |
08f130a0 | 1347 | await chargingStation.ocppRequestService.requestHandler< |
c9a4f9ea JB |
1348 | OCPP16DiagnosticsStatusNotificationRequest, |
1349 | OCPP16DiagnosticsStatusNotificationResponse | |
08f130a0 | 1350 | >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { |
ef6fa3fb JB |
1351 | status: OCPP16DiagnosticsStatus.UploadFailed, |
1352 | }); | |
47e22477 JB |
1353 | if (ftpClient) { |
1354 | ftpClient.close(); | |
1355 | } | |
e1d9a0f4 | 1356 | return this.handleIncomingRequestError<GetDiagnosticsResponse>( |
08f130a0 | 1357 | chargingStation, |
e7aeea18 JB |
1358 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, |
1359 | error as Error, | |
5edd8ba0 | 1360 | { errorResponse: OCPP16Constants.OCPP_RESPONSE_EMPTY }, |
e1d9a0f4 | 1361 | )!; |
47e22477 JB |
1362 | } |
1363 | } else { | |
e7aeea18 | 1364 | logger.error( |
08f130a0 | 1365 | `${chargingStation.logPrefix()} Unsupported protocol ${ |
e7aeea18 | 1366 | uri.protocol |
5edd8ba0 | 1367 | } to transfer the diagnostic logs archive`, |
e7aeea18 | 1368 | ); |
08f130a0 | 1369 | await chargingStation.ocppRequestService.requestHandler< |
c9a4f9ea JB |
1370 | OCPP16DiagnosticsStatusNotificationRequest, |
1371 | OCPP16DiagnosticsStatusNotificationResponse | |
08f130a0 | 1372 | >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { |
ef6fa3fb JB |
1373 | status: OCPP16DiagnosticsStatus.UploadFailed, |
1374 | }); | |
d8b1fab1 | 1375 | return OCPP16Constants.OCPP_RESPONSE_EMPTY; |
47e22477 JB |
1376 | } |
1377 | } | |
802cfa13 | 1378 | |
e7aeea18 | 1379 | private handleRequestTriggerMessage( |
08f130a0 | 1380 | chargingStation: ChargingStation, |
5edd8ba0 | 1381 | commandPayload: OCPP16TriggerMessageRequest, |
e7aeea18 | 1382 | ): OCPP16TriggerMessageResponse { |
370ae4ee JB |
1383 | if ( |
1384 | !OCPP16ServiceUtils.checkFeatureProfile( | |
08f130a0 | 1385 | chargingStation, |
370ae4ee | 1386 | OCPP16SupportedFeatureProfiles.RemoteTrigger, |
5edd8ba0 | 1387 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, |
c60ed4b8 JB |
1388 | ) || |
1389 | !OCPP16ServiceUtils.isMessageTriggerSupported( | |
1390 | chargingStation, | |
5edd8ba0 | 1391 | commandPayload.requestedMessage, |
370ae4ee JB |
1392 | ) |
1393 | ) { | |
d8b1fab1 | 1394 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; |
68cb8b91 | 1395 | } |
c60ed4b8 | 1396 | if ( |
4caa7e67 | 1397 | !OCPP16ServiceUtils.isConnectorIdValid( |
c60ed4b8 JB |
1398 | chargingStation, |
1399 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, | |
e1d9a0f4 | 1400 | commandPayload.connectorId!, |
c60ed4b8 JB |
1401 | ) |
1402 | ) { | |
d8b1fab1 | 1403 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED; |
dc661702 | 1404 | } |
802cfa13 JB |
1405 | try { |
1406 | switch (commandPayload.requestedMessage) { | |
c60ed4b8 | 1407 | case OCPP16MessageTrigger.BootNotification: |
802cfa13 | 1408 | setTimeout(() => { |
08f130a0 | 1409 | chargingStation.ocppRequestService |
f7f98c68 | 1410 | .requestHandler<OCPP16BootNotificationRequest, OCPP16BootNotificationResponse>( |
08f130a0 | 1411 | chargingStation, |
6a8b180d | 1412 | OCPP16RequestCommand.BOOT_NOTIFICATION, |
8bfbc743 | 1413 | chargingStation.bootNotificationRequest, |
5edd8ba0 | 1414 | { skipBufferingOnError: true, triggerMessage: true }, |
e7aeea18 | 1415 | ) |
72092cfc | 1416 | .then((response) => { |
8bfbc743 | 1417 | chargingStation.bootNotificationResponse = response; |
ae711c83 | 1418 | }) |
59b6ed8d | 1419 | .catch(Constants.EMPTY_FUNCTION); |
d8b1fab1 JB |
1420 | }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY); |
1421 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; | |
c60ed4b8 | 1422 | case OCPP16MessageTrigger.Heartbeat: |
802cfa13 | 1423 | setTimeout(() => { |
08f130a0 | 1424 | chargingStation.ocppRequestService |
f7f98c68 | 1425 | .requestHandler<OCPP16HeartbeatRequest, OCPP16HeartbeatResponse>( |
08f130a0 | 1426 | chargingStation, |
ef6fa3fb JB |
1427 | OCPP16RequestCommand.HEARTBEAT, |
1428 | null, | |
1429 | { | |
1430 | triggerMessage: true, | |
5edd8ba0 | 1431 | }, |
ef6fa3fb | 1432 | ) |
59b6ed8d | 1433 | .catch(Constants.EMPTY_FUNCTION); |
d8b1fab1 JB |
1434 | }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY); |
1435 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; | |
c60ed4b8 | 1436 | case OCPP16MessageTrigger.StatusNotification: |
dc661702 | 1437 | setTimeout(() => { |
9bf0ef23 | 1438 | if (!isNullOrUndefined(commandPayload?.connectorId)) { |
08f130a0 | 1439 | chargingStation.ocppRequestService |
dc661702 | 1440 | .requestHandler<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>( |
08f130a0 | 1441 | chargingStation, |
dc661702 JB |
1442 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
1443 | { | |
1444 | connectorId: commandPayload.connectorId, | |
1445 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
e1d9a0f4 | 1446 | status: chargingStation.getConnectorStatus(commandPayload.connectorId!)?.status, |
dc661702 JB |
1447 | }, |
1448 | { | |
1449 | triggerMessage: true, | |
5edd8ba0 | 1450 | }, |
dc661702 | 1451 | ) |
59b6ed8d | 1452 | .catch(Constants.EMPTY_FUNCTION); |
dc661702 | 1453 | } else { |
ded57f02 JB |
1454 | // eslint-disable-next-line no-lonely-if |
1455 | if (chargingStation.hasEvses) { | |
1456 | for (const evseStatus of chargingStation.evses.values()) { | |
1457 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
1458 | chargingStation.ocppRequestService | |
1459 | .requestHandler< | |
1460 | OCPP16StatusNotificationRequest, | |
1461 | OCPP16StatusNotificationResponse | |
1462 | >( | |
1463 | chargingStation, | |
1464 | OCPP16RequestCommand.STATUS_NOTIFICATION, | |
1465 | { | |
1466 | connectorId, | |
1467 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
1468 | status: connectorStatus.status, | |
1469 | }, | |
1470 | { | |
1471 | triggerMessage: true, | |
5edd8ba0 | 1472 | }, |
ded57f02 JB |
1473 | ) |
1474 | .catch(Constants.EMPTY_FUNCTION); | |
1475 | } | |
1476 | } | |
1477 | } else { | |
1478 | for (const connectorId of chargingStation.connectors.keys()) { | |
1479 | chargingStation.ocppRequestService | |
1480 | .requestHandler< | |
1481 | OCPP16StatusNotificationRequest, | |
1482 | OCPP16StatusNotificationResponse | |
1483 | >( | |
1484 | chargingStation, | |
1485 | OCPP16RequestCommand.STATUS_NOTIFICATION, | |
1486 | { | |
1487 | connectorId, | |
1488 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
1489 | status: chargingStation.getConnectorStatus(connectorId)?.status, | |
1490 | }, | |
1491 | { | |
1492 | triggerMessage: true, | |
5edd8ba0 | 1493 | }, |
ded57f02 JB |
1494 | ) |
1495 | .catch(Constants.EMPTY_FUNCTION); | |
1496 | } | |
dc661702 JB |
1497 | } |
1498 | } | |
d8b1fab1 JB |
1499 | }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY); |
1500 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; | |
802cfa13 | 1501 | default: |
d8b1fab1 | 1502 | return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; |
802cfa13 JB |
1503 | } |
1504 | } catch (error) { | |
e1d9a0f4 | 1505 | return this.handleIncomingRequestError<OCPP16TriggerMessageResponse>( |
08f130a0 | 1506 | chargingStation, |
e7aeea18 JB |
1507 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, |
1508 | error as Error, | |
5edd8ba0 | 1509 | { errorResponse: OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }, |
e1d9a0f4 | 1510 | )!; |
802cfa13 JB |
1511 | } |
1512 | } | |
77b95a89 JB |
1513 | |
1514 | private handleRequestDataTransfer( | |
1515 | chargingStation: ChargingStation, | |
5edd8ba0 | 1516 | commandPayload: OCPP16DataTransferRequest, |
77b95a89 JB |
1517 | ): OCPP16DataTransferResponse { |
1518 | try { | |
1519 | if (Object.values(OCPP16DataTransferVendorId).includes(commandPayload.vendorId)) { | |
b63b4a73 | 1520 | return OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED; |
77b95a89 | 1521 | } |
b63b4a73 | 1522 | return OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID; |
77b95a89 | 1523 | } catch (error) { |
e1d9a0f4 | 1524 | return this.handleIncomingRequestError<OCPP16DataTransferResponse>( |
77b95a89 JB |
1525 | chargingStation, |
1526 | OCPP16IncomingRequestCommand.DATA_TRANSFER, | |
1527 | error as Error, | |
5edd8ba0 | 1528 | { errorResponse: OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED }, |
e1d9a0f4 | 1529 | )!; |
77b95a89 JB |
1530 | } |
1531 | } | |
24578c31 JB |
1532 | |
1533 | private async handleRequestReserveNow( | |
1534 | chargingStation: ChargingStation, | |
5edd8ba0 | 1535 | commandPayload: OCPP16ReserveNowRequest, |
24578c31 | 1536 | ): Promise<OCPP16ReserveNowResponse> { |
66dd3447 JB |
1537 | if ( |
1538 | !OCPP16ServiceUtils.checkFeatureProfile( | |
1539 | chargingStation, | |
1540 | OCPP16SupportedFeatureProfiles.Reservation, | |
5edd8ba0 | 1541 | OCPP16IncomingRequestCommand.RESERVE_NOW, |
66dd3447 JB |
1542 | ) |
1543 | ) { | |
178956d8 | 1544 | return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; |
66dd3447 | 1545 | } |
24578c31 | 1546 | const { reservationId, idTag, connectorId } = commandPayload; |
24578c31 JB |
1547 | let response: OCPP16ReserveNowResponse; |
1548 | try { | |
66dd3447 | 1549 | if (!chargingStation.isConnectorAvailable(connectorId) && connectorId > 0) { |
178956d8 | 1550 | return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; |
24578c31 | 1551 | } |
66dd3447 | 1552 | if (connectorId === 0 && !chargingStation.getReservationOnConnectorId0Enabled()) { |
178956d8 | 1553 | return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; |
24578c31 | 1554 | } |
66dd3447 | 1555 | if (!(await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, connectorId, idTag))) { |
178956d8 | 1556 | return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; |
24578c31 | 1557 | } |
e1d9a0f4 | 1558 | switch (chargingStation.getConnectorStatus(connectorId)!.status) { |
178956d8 JB |
1559 | case OCPP16ChargePointStatus.Faulted: |
1560 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED; | |
24578c31 | 1561 | break; |
178956d8 JB |
1562 | case OCPP16ChargePointStatus.Preparing: |
1563 | case OCPP16ChargePointStatus.Charging: | |
1564 | case OCPP16ChargePointStatus.SuspendedEV: | |
1565 | case OCPP16ChargePointStatus.SuspendedEVSE: | |
1566 | case OCPP16ChargePointStatus.Finishing: | |
1567 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; | |
24578c31 | 1568 | break; |
178956d8 JB |
1569 | case OCPP16ChargePointStatus.Unavailable: |
1570 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_UNAVAILABLE; | |
24578c31 | 1571 | break; |
178956d8 | 1572 | case OCPP16ChargePointStatus.Reserved: |
66dd3447 | 1573 | if (!chargingStation.isConnectorReservable(reservationId, idTag, connectorId)) { |
178956d8 | 1574 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; |
24578c31 JB |
1575 | break; |
1576 | } | |
1577 | // eslint-disable-next-line no-fallthrough | |
1578 | default: | |
66dd3447 | 1579 | if (!chargingStation.isConnectorReservable(reservationId, idTag)) { |
178956d8 | 1580 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; |
d193a949 JB |
1581 | break; |
1582 | } | |
1583 | await chargingStation.addReservation({ | |
1584 | id: commandPayload.reservationId, | |
1585 | ...commandPayload, | |
1586 | }); | |
178956d8 | 1587 | response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_ACCEPTED; |
24578c31 JB |
1588 | break; |
1589 | } | |
1590 | return response; | |
1591 | } catch (error) { | |
e1d9a0f4 JB |
1592 | chargingStation.getConnectorStatus(connectorId)!.status = OCPP16ChargePointStatus.Available; |
1593 | return this.handleIncomingRequestError<OCPP16ReserveNowResponse>( | |
24578c31 JB |
1594 | chargingStation, |
1595 | OCPP16IncomingRequestCommand.RESERVE_NOW, | |
1596 | error as Error, | |
5edd8ba0 | 1597 | { errorResponse: OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED }, |
e1d9a0f4 | 1598 | )!; |
24578c31 JB |
1599 | } |
1600 | } | |
1601 | ||
1602 | private async handleRequestCancelReservation( | |
1603 | chargingStation: ChargingStation, | |
5edd8ba0 | 1604 | commandPayload: OCPP16CancelReservationRequest, |
b1f1b0f6 | 1605 | ): Promise<GenericResponse> { |
66dd3447 JB |
1606 | if ( |
1607 | !OCPP16ServiceUtils.checkFeatureProfile( | |
1608 | chargingStation, | |
1609 | OCPP16SupportedFeatureProfiles.Reservation, | |
5edd8ba0 | 1610 | OCPP16IncomingRequestCommand.CANCEL_RESERVATION, |
66dd3447 JB |
1611 | ) |
1612 | ) { | |
178956d8 | 1613 | return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; |
66dd3447 | 1614 | } |
24578c31 | 1615 | try { |
d193a949 | 1616 | const { reservationId } = commandPayload; |
2ca0ea90 | 1617 | const reservation = chargingStation.getReservationBy('reservationId', reservationId); |
af4339e1 | 1618 | if (isUndefined(reservation)) { |
24578c31 | 1619 | logger.error( |
66dd3447 | 1620 | `${chargingStation.logPrefix()} Reservation with ID ${reservationId} |
5edd8ba0 | 1621 | does not exist on charging station`, |
24578c31 | 1622 | ); |
178956d8 | 1623 | return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; |
24578c31 | 1624 | } |
ec9f36cc | 1625 | await chargingStation.removeReservation( |
e1d9a0f4 | 1626 | reservation!, |
5edd8ba0 | 1627 | ReservationTerminationReason.RESERVATION_CANCELED, |
ec9f36cc | 1628 | ); |
178956d8 | 1629 | return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED; |
24578c31 | 1630 | } catch (error) { |
e1d9a0f4 | 1631 | return this.handleIncomingRequestError<GenericResponse>( |
24578c31 JB |
1632 | chargingStation, |
1633 | OCPP16IncomingRequestCommand.CANCEL_RESERVATION, | |
1634 | error as Error, | |
5edd8ba0 | 1635 | { errorResponse: OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED }, |
e1d9a0f4 | 1636 | )!; |
24578c31 JB |
1637 | } |
1638 | } | |
c0560973 | 1639 | } |