Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
e7aeea18 JB |
3 | import { |
4 | ChangeAvailabilityRequest, | |
5 | ChangeConfigurationRequest, | |
6 | ClearChargingProfileRequest, | |
7 | GetConfigurationRequest, | |
8 | GetDiagnosticsRequest, | |
9 | MessageTrigger, | |
10 | OCPP16AvailabilityType, | |
11 | OCPP16IncomingRequestCommand, | |
94a464f9 | 12 | OCPP16RequestCommand, |
e7aeea18 JB |
13 | OCPP16TriggerMessageRequest, |
14 | RemoteStartTransactionRequest, | |
15 | RemoteStopTransactionRequest, | |
16 | ResetRequest, | |
17 | SetChargingProfileRequest, | |
18 | UnlockConnectorRequest, | |
19 | } from '../../../types/ocpp/1.6/Requests'; | |
20 | import { | |
21 | ChangeAvailabilityResponse, | |
22 | ChangeConfigurationResponse, | |
23 | ClearChargingProfileResponse, | |
f22266fd | 24 | DiagnosticsStatusNotificationResponse, |
e7aeea18 JB |
25 | GetConfigurationResponse, |
26 | GetDiagnosticsResponse, | |
f22266fd JB |
27 | OCPP16BootNotificationResponse, |
28 | OCPP16HeartbeatResponse, | |
29 | OCPP16StatusNotificationResponse, | |
e7aeea18 JB |
30 | OCPP16TriggerMessageResponse, |
31 | SetChargingProfileResponse, | |
32 | UnlockConnectorResponse, | |
33 | } from '../../../types/ocpp/1.6/Responses'; | |
34 | import { | |
35 | ChargingProfilePurposeType, | |
36 | OCPP16ChargingProfile, | |
37 | } from '../../../types/ocpp/1.6/ChargingProfile'; | |
47e22477 | 38 | import { Client, FTPResponse } from 'basic-ftp'; |
e7aeea18 JB |
39 | import { |
40 | OCPP16AuthorizationStatus, | |
2e3d65ae | 41 | OCPP16AuthorizeResponse, |
e7454a1f | 42 | OCPP16StartTransactionResponse, |
e7aeea18 | 43 | OCPP16StopTransactionReason, |
68c993d5 | 44 | OCPP16StopTransactionResponse, |
e7aeea18 | 45 | } from '../../../types/ocpp/1.6/Transaction'; |
c0560973 | 46 | |
73b9adec | 47 | import type ChargingStation from '../../ChargingStation'; |
c0560973 | 48 | import Constants from '../../../utils/Constants'; |
9ccca265 | 49 | import { DefaultResponse } from '../../../types/ocpp/Responses'; |
c0560973 | 50 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
58144adb | 51 | import { IncomingRequestHandler } from '../../../types/ocpp/Requests'; |
d1888640 | 52 | import { JsonType } from '../../../types/JsonType'; |
93b4a429 | 53 | import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; |
c0560973 | 54 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; |
47e22477 | 55 | import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; |
f22266fd | 56 | import { OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; |
68c993d5 | 57 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
c0560973 JB |
58 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
59 | import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; | |
e58068fd | 60 | import OCPPError from '../../../exception/OCPPError'; |
c0560973 | 61 | import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; |
a3868ec4 | 62 | import { URL } from 'url'; |
c0560973 | 63 | import Utils from '../../../utils/Utils'; |
47e22477 | 64 | import fs from 'fs'; |
9f2e3130 | 65 | import logger from '../../../utils/Logger'; |
47e22477 JB |
66 | import path from 'path'; |
67 | import tar from 'tar'; | |
c0560973 | 68 | |
2a115f87 | 69 | const moduleName = 'OCPP16IncomingRequestService'; |
909dcf2d | 70 | |
c0560973 | 71 | export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { |
58144adb JB |
72 | private incomingRequestHandlers: Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>; |
73 | ||
9f2e3130 | 74 | public constructor(chargingStation: ChargingStation) { |
909dcf2d | 75 | if (new.target?.name === moduleName) { |
06127450 | 76 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
9f2e3130 | 77 | } |
58144adb JB |
78 | super(chargingStation); |
79 | this.incomingRequestHandlers = new Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>([ | |
80 | [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)], | |
81 | [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], | |
82 | [OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, this.handleRequestUnlockConnector.bind(this)], | |
e7aeea18 JB |
83 | [ |
84 | OCPP16IncomingRequestCommand.GET_CONFIGURATION, | |
85 | this.handleRequestGetConfiguration.bind(this), | |
86 | ], | |
87 | [ | |
88 | OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, | |
89 | this.handleRequestChangeConfiguration.bind(this), | |
90 | ], | |
91 | [ | |
92 | OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, | |
93 | this.handleRequestSetChargingProfile.bind(this), | |
94 | ], | |
95 | [ | |
96 | OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, | |
97 | this.handleRequestClearChargingProfile.bind(this), | |
98 | ], | |
99 | [ | |
100 | OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, | |
101 | this.handleRequestChangeAvailability.bind(this), | |
102 | ], | |
103 | [ | |
104 | OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, | |
105 | this.handleRequestRemoteStartTransaction.bind(this), | |
106 | ], | |
107 | [ | |
108 | OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, | |
109 | this.handleRequestRemoteStopTransaction.bind(this), | |
110 | ], | |
734d790d | 111 | [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)], |
e7aeea18 | 112 | [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)], |
58144adb JB |
113 | ]); |
114 | } | |
115 | ||
e7aeea18 JB |
116 | public async handleRequest( |
117 | messageId: string, | |
118 | commandName: OCPP16IncomingRequestCommand, | |
119 | commandPayload: JsonType | |
120 | ): Promise<void> { | |
d1888640 | 121 | let result: JsonType; |
e7aeea18 JB |
122 | if ( |
123 | this.chargingStation.getOcppStrictCompliance() && | |
124 | this.chargingStation.isInPendingState() && | |
125 | (commandName === OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION || | |
126 | commandName === OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION) | |
127 | ) { | |
128 | throw new OCPPError( | |
129 | ErrorType.SECURITY_ERROR, | |
130 | `${commandName} cannot be issued to handle request payload ${JSON.stringify( | |
131 | commandPayload, | |
132 | null, | |
133 | 2 | |
134 | )} while the charging station is in pending state on the central server`, | |
135 | commandName | |
136 | ); | |
caad9d6b | 137 | } |
e7aeea18 JB |
138 | if ( |
139 | this.chargingStation.isRegistered() || | |
140 | (!this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInUnknownState()) | |
141 | ) { | |
124f3553 JB |
142 | if (this.incomingRequestHandlers.has(commandName)) { |
143 | try { | |
144 | // Call the method to build the result | |
145 | result = await this.incomingRequestHandlers.get(commandName)(commandPayload); | |
146 | } catch (error) { | |
147 | // Log | |
9f2e3130 | 148 | logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error); |
124f3553 JB |
149 | throw error; |
150 | } | |
151 | } else { | |
152 | // Throw exception | |
e7aeea18 JB |
153 | throw new OCPPError( |
154 | ErrorType.NOT_IMPLEMENTED, | |
155 | `${commandName} is not implemented to handle request payload ${JSON.stringify( | |
156 | commandPayload, | |
157 | null, | |
158 | 2 | |
159 | )}`, | |
160 | commandName | |
161 | ); | |
c0560973 JB |
162 | } |
163 | } else { | |
e7aeea18 JB |
164 | throw new OCPPError( |
165 | ErrorType.SECURITY_ERROR, | |
166 | `${commandName} cannot be issued to handle request payload ${JSON.stringify( | |
167 | commandPayload, | |
168 | null, | |
169 | 2 | |
170 | )} while the charging station is not registered on the central server.`, | |
171 | commandName | |
172 | ); | |
c0560973 | 173 | } |
de3dbcf5 JB |
174 | // Send the built result |
175 | await this.chargingStation.ocppRequestService.sendResult(messageId, result, commandName); | |
c0560973 JB |
176 | } |
177 | ||
178 | // Simulate charging station restart | |
179 | private handleRequestReset(commandPayload: ResetRequest): DefaultResponse { | |
71623267 JB |
180 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
181 | setImmediate(async (): Promise<void> => { | |
e7aeea18 JB |
182 | await this.chargingStation.stop( |
183 | (commandPayload.type + 'Reset') as OCPP16StopTransactionReason | |
184 | ); | |
c0560973 | 185 | await Utils.sleep(this.chargingStation.stationInfo.resetTime); |
71623267 | 186 | this.chargingStation.start(); |
c0560973 | 187 | }); |
e7aeea18 JB |
188 | logger.info( |
189 | `${this.chargingStation.logPrefix()} ${ | |
190 | commandPayload.type | |
191 | } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds( | |
192 | this.chargingStation.stationInfo.resetTime | |
193 | )}` | |
194 | ); | |
c0560973 JB |
195 | return Constants.OCPP_RESPONSE_ACCEPTED; |
196 | } | |
197 | ||
198 | private handleRequestClearCache(): DefaultResponse { | |
199 | return Constants.OCPP_RESPONSE_ACCEPTED; | |
200 | } | |
201 | ||
e7aeea18 JB |
202 | private async handleRequestUnlockConnector( |
203 | commandPayload: UnlockConnectorRequest | |
204 | ): Promise<UnlockConnectorResponse> { | |
c0560973 JB |
205 | const connectorId = commandPayload.connectorId; |
206 | if (connectorId === 0) { | |
e7aeea18 JB |
207 | logger.error( |
208 | this.chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString() | |
209 | ); | |
c0560973 JB |
210 | return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; |
211 | } | |
734d790d JB |
212 | if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { |
213 | const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; | |
68c993d5 JB |
214 | if ( |
215 | this.chargingStation.getBeginEndMeterValues() && | |
216 | this.chargingStation.getOcppStrictCompliance() && | |
217 | !this.chargingStation.getOutOfOrderEndMeterValues() | |
218 | ) { | |
219 | // FIXME: Implement OCPP version agnostic helpers | |
220 | const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( | |
221 | this.chargingStation, | |
222 | connectorId, | |
223 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) | |
224 | ); | |
f22266fd | 225 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>( |
3a33b6a9 JB |
226 | OCPP16RequestCommand.METER_VALUES, |
227 | { | |
228 | connectorId, | |
229 | transactionId, | |
230 | meterValue: transactionEndMeterValue, | |
231 | } | |
68c993d5 JB |
232 | ); |
233 | } | |
f22266fd JB |
234 | const stopResponse = |
235 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StopTransactionResponse>( | |
236 | OCPP16RequestCommand.STOP_TRANSACTION, | |
237 | { | |
238 | transactionId, | |
239 | meterStop: | |
240 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), | |
241 | idTag: this.chargingStation.getTransactionIdTag(transactionId), | |
242 | reason: OCPP16StopTransactionReason.UNLOCK_COMMAND, | |
243 | } | |
244 | ); | |
c0560973 JB |
245 | if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
246 | return Constants.OCPP_RESPONSE_UNLOCKED; | |
247 | } | |
248 | return Constants.OCPP_RESPONSE_UNLOCK_FAILED; | |
249 | } | |
f22266fd | 250 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
251 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
252 | { | |
253 | connectorId, | |
254 | status: OCPP16ChargePointStatus.AVAILABLE, | |
255 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
256 | } | |
e7aeea18 | 257 | ); |
734d790d | 258 | this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
c0560973 JB |
259 | return Constants.OCPP_RESPONSE_UNLOCKED; |
260 | } | |
261 | ||
e7aeea18 JB |
262 | private handleRequestGetConfiguration( |
263 | commandPayload: GetConfigurationRequest | |
264 | ): GetConfigurationResponse { | |
c0560973 JB |
265 | const configurationKey: OCPPConfigurationKey[] = []; |
266 | const unknownKey: string[] = []; | |
267 | if (Utils.isEmptyArray(commandPayload.key)) { | |
7f7b65ca JB |
268 | for (const configuration of this.chargingStation.ocppConfiguration.configurationKey) { |
269 | if (Utils.isUndefined(configuration.visible)) { | |
270 | configuration.visible = true; | |
c0560973 | 271 | } |
7f7b65ca | 272 | if (!configuration.visible) { |
c0560973 JB |
273 | continue; |
274 | } | |
275 | configurationKey.push({ | |
7f7b65ca JB |
276 | key: configuration.key, |
277 | readonly: configuration.readonly, | |
278 | value: configuration.value, | |
c0560973 JB |
279 | }); |
280 | } | |
281 | } else { | |
282 | for (const key of commandPayload.key) { | |
283 | const keyFound = this.chargingStation.getConfigurationKey(key); | |
284 | if (keyFound) { | |
285 | if (Utils.isUndefined(keyFound.visible)) { | |
286 | keyFound.visible = true; | |
287 | } | |
288 | if (!keyFound.visible) { | |
289 | continue; | |
290 | } | |
291 | configurationKey.push({ | |
292 | key: keyFound.key, | |
293 | readonly: keyFound.readonly, | |
294 | value: keyFound.value, | |
295 | }); | |
296 | } else { | |
297 | unknownKey.push(key); | |
298 | } | |
299 | } | |
300 | } | |
301 | return { | |
302 | configurationKey, | |
303 | unknownKey, | |
304 | }; | |
305 | } | |
306 | ||
e7aeea18 JB |
307 | private handleRequestChangeConfiguration( |
308 | commandPayload: ChangeConfigurationRequest | |
309 | ): ChangeConfigurationResponse { | |
c0560973 JB |
310 | // JSON request fields type sanity check |
311 | if (!Utils.isString(commandPayload.key)) { | |
e7aeea18 JB |
312 | logger.error( |
313 | `${this.chargingStation.logPrefix()} ${ | |
78085c42 | 314 | OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION |
e7aeea18 JB |
315 | } request key field is not a string:`, |
316 | commandPayload | |
317 | ); | |
c0560973 JB |
318 | } |
319 | if (!Utils.isString(commandPayload.value)) { | |
e7aeea18 JB |
320 | logger.error( |
321 | `${this.chargingStation.logPrefix()} ${ | |
78085c42 | 322 | OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION |
e7aeea18 JB |
323 | } request value field is not a string:`, |
324 | commandPayload | |
325 | ); | |
c0560973 JB |
326 | } |
327 | const keyToChange = this.chargingStation.getConfigurationKey(commandPayload.key, true); | |
328 | if (!keyToChange) { | |
329 | return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; | |
330 | } else if (keyToChange && keyToChange.readonly) { | |
331 | return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED; | |
332 | } else if (keyToChange && !keyToChange.readonly) { | |
c0560973 | 333 | let valueChanged = false; |
a95873d8 JB |
334 | if (keyToChange.value !== commandPayload.value) { |
335 | this.chargingStation.setConfigurationKeyValue( | |
336 | commandPayload.key, | |
337 | commandPayload.value, | |
338 | true | |
339 | ); | |
c0560973 JB |
340 | valueChanged = true; |
341 | } | |
342 | let triggerHeartbeatRestart = false; | |
343 | if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) { | |
e7aeea18 JB |
344 | this.chargingStation.setConfigurationKeyValue( |
345 | OCPP16StandardParametersKey.HeartbeatInterval, | |
346 | commandPayload.value | |
347 | ); | |
c0560973 JB |
348 | triggerHeartbeatRestart = true; |
349 | } | |
350 | if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) { | |
e7aeea18 JB |
351 | this.chargingStation.setConfigurationKeyValue( |
352 | OCPP16StandardParametersKey.HeartBeatInterval, | |
353 | commandPayload.value | |
354 | ); | |
c0560973 JB |
355 | triggerHeartbeatRestart = true; |
356 | } | |
357 | if (triggerHeartbeatRestart) { | |
358 | this.chargingStation.restartHeartbeat(); | |
359 | } | |
360 | if (keyToChange.key === OCPP16StandardParametersKey.WebSocketPingInterval && valueChanged) { | |
361 | this.chargingStation.restartWebSocketPing(); | |
362 | } | |
363 | if (keyToChange.reboot) { | |
364 | return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; | |
365 | } | |
366 | return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; | |
367 | } | |
368 | } | |
369 | ||
e7aeea18 JB |
370 | private handleRequestSetChargingProfile( |
371 | commandPayload: SetChargingProfileRequest | |
372 | ): SetChargingProfileResponse { | |
734d790d | 373 | if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) { |
e7aeea18 JB |
374 | logger.error( |
375 | `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${ | |
376 | commandPayload.connectorId | |
377 | }` | |
378 | ); | |
c0560973 JB |
379 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
380 | } | |
e7aeea18 JB |
381 | if ( |
382 | commandPayload.csChargingProfiles.chargingProfilePurpose === | |
383 | ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && | |
384 | commandPayload.connectorId !== 0 | |
385 | ) { | |
c0560973 JB |
386 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
387 | } | |
e7aeea18 JB |
388 | if ( |
389 | commandPayload.csChargingProfiles.chargingProfilePurpose === | |
390 | ChargingProfilePurposeType.TX_PROFILE && | |
391 | (commandPayload.connectorId === 0 || | |
392 | !this.chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted) | |
393 | ) { | |
c0560973 JB |
394 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
395 | } | |
e7aeea18 JB |
396 | this.chargingStation.setChargingProfile( |
397 | commandPayload.connectorId, | |
398 | commandPayload.csChargingProfiles | |
399 | ); | |
400 | logger.debug( | |
401 | `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`, | |
402 | this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles | |
403 | ); | |
c0560973 JB |
404 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
405 | } | |
406 | ||
e7aeea18 JB |
407 | private handleRequestClearChargingProfile( |
408 | commandPayload: ClearChargingProfileRequest | |
409 | ): ClearChargingProfileResponse { | |
658e2d16 JB |
410 | const connectorStatus = this.chargingStation.getConnectorStatus(commandPayload.connectorId); |
411 | if (!connectorStatus) { | |
e7aeea18 JB |
412 | logger.error( |
413 | `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${ | |
414 | commandPayload.connectorId | |
415 | }` | |
416 | ); | |
c0560973 JB |
417 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
418 | } | |
658e2d16 JB |
419 | if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) { |
420 | connectorStatus.chargingProfiles = []; | |
e7aeea18 JB |
421 | logger.debug( |
422 | `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, | |
658e2d16 | 423 | connectorStatus.chargingProfiles |
e7aeea18 | 424 | ); |
c0560973 JB |
425 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
426 | } | |
427 | if (!commandPayload.connectorId) { | |
428 | let clearedCP = false; | |
734d790d | 429 | for (const connectorId of this.chargingStation.connectors.keys()) { |
e7aeea18 JB |
430 | if ( |
431 | !Utils.isEmptyArray(this.chargingStation.getConnectorStatus(connectorId).chargingProfiles) | |
432 | ) { | |
433 | this.chargingStation | |
434 | .getConnectorStatus(connectorId) | |
435 | .chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { | |
436 | let clearCurrentCP = false; | |
437 | if (chargingProfile.chargingProfileId === commandPayload.id) { | |
438 | clearCurrentCP = true; | |
439 | } | |
440 | if ( | |
441 | !commandPayload.chargingProfilePurpose && | |
442 | chargingProfile.stackLevel === commandPayload.stackLevel | |
443 | ) { | |
444 | clearCurrentCP = true; | |
445 | } | |
446 | if ( | |
447 | !chargingProfile.stackLevel && | |
448 | chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose | |
449 | ) { | |
450 | clearCurrentCP = true; | |
451 | } | |
452 | if ( | |
453 | chargingProfile.stackLevel === commandPayload.stackLevel && | |
454 | chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose | |
455 | ) { | |
456 | clearCurrentCP = true; | |
457 | } | |
458 | if (clearCurrentCP) { | |
658e2d16 | 459 | connectorStatus.chargingProfiles[index] = {} as OCPP16ChargingProfile; |
e7aeea18 JB |
460 | logger.debug( |
461 | `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, | |
658e2d16 | 462 | connectorStatus.chargingProfiles |
e7aeea18 JB |
463 | ); |
464 | clearedCP = true; | |
465 | } | |
466 | }); | |
c0560973 JB |
467 | } |
468 | } | |
469 | if (clearedCP) { | |
470 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; | |
471 | } | |
472 | } | |
473 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; | |
474 | } | |
475 | ||
e7aeea18 JB |
476 | private async handleRequestChangeAvailability( |
477 | commandPayload: ChangeAvailabilityRequest | |
478 | ): Promise<ChangeAvailabilityResponse> { | |
c0560973 | 479 | const connectorId: number = commandPayload.connectorId; |
734d790d | 480 | if (!this.chargingStation.getConnectorStatus(connectorId)) { |
e7aeea18 JB |
481 | logger.error( |
482 | `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}` | |
483 | ); | |
c0560973 JB |
484 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; |
485 | } | |
e7aeea18 JB |
486 | const chargePointStatus: OCPP16ChargePointStatus = |
487 | commandPayload.type === OCPP16AvailabilityType.OPERATIVE | |
488 | ? OCPP16ChargePointStatus.AVAILABLE | |
489 | : OCPP16ChargePointStatus.UNAVAILABLE; | |
c0560973 JB |
490 | if (connectorId === 0) { |
491 | let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; | |
734d790d JB |
492 | for (const id of this.chargingStation.connectors.keys()) { |
493 | if (this.chargingStation.getConnectorStatus(id)?.transactionStarted) { | |
c0560973 JB |
494 | response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
495 | } | |
734d790d | 496 | this.chargingStation.getConnectorStatus(id).availability = commandPayload.type; |
c0560973 | 497 | if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { |
f22266fd | 498 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
499 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
500 | { | |
501 | connectorId: id, | |
502 | status: chargePointStatus, | |
503 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
504 | } | |
e7aeea18 | 505 | ); |
734d790d | 506 | this.chargingStation.getConnectorStatus(id).status = chargePointStatus; |
c0560973 JB |
507 | } |
508 | } | |
509 | return response; | |
e7aeea18 JB |
510 | } else if ( |
511 | connectorId > 0 && | |
512 | (this.chargingStation.getConnectorStatus(0).availability === | |
513 | OCPP16AvailabilityType.OPERATIVE || | |
514 | (this.chargingStation.getConnectorStatus(0).availability === | |
515 | OCPP16AvailabilityType.INOPERATIVE && | |
516 | commandPayload.type === OCPP16AvailabilityType.INOPERATIVE)) | |
517 | ) { | |
734d790d JB |
518 | if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { |
519 | this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; | |
c0560973 JB |
520 | return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; |
521 | } | |
734d790d | 522 | this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; |
f22266fd | 523 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
524 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
525 | { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR } | |
e7aeea18 | 526 | ); |
734d790d | 527 | this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus; |
c0560973 JB |
528 | return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; |
529 | } | |
530 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; | |
531 | } | |
532 | ||
e7aeea18 JB |
533 | private async handleRequestRemoteStartTransaction( |
534 | commandPayload: RemoteStartTransactionRequest | |
535 | ): Promise<DefaultResponse> { | |
658e2d16 JB |
536 | const transactionConnectorId = commandPayload.connectorId; |
537 | const connectorStatus = this.chargingStation.getConnectorStatus(transactionConnectorId); | |
a7fc8211 | 538 | if (transactionConnectorId) { |
f22266fd | 539 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
540 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
541 | { | |
542 | connectorId: transactionConnectorId, | |
543 | status: OCPP16ChargePointStatus.PREPARING, | |
544 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
545 | } | |
e7aeea18 | 546 | ); |
658e2d16 JB |
547 | connectorStatus.status = OCPP16ChargePointStatus.PREPARING; |
548 | if (this.chargingStation.isChargingStationAvailable() && connectorStatus) { | |
e060fe58 | 549 | // Check if authorized |
a7fc8211 JB |
550 | if (this.chargingStation.getAuthorizeRemoteTxRequests()) { |
551 | let authorized = false; | |
e7aeea18 JB |
552 | if ( |
553 | this.chargingStation.getLocalAuthListEnabled() && | |
554 | this.chargingStation.hasAuthorizedTags() && | |
555 | this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag) | |
556 | ) { | |
658e2d16 JB |
557 | connectorStatus.localAuthorizeIdTag = commandPayload.idTag; |
558 | connectorStatus.idTagLocalAuthorized = true; | |
36f6a92e | 559 | authorized = true; |
71068fb9 | 560 | } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) { |
658e2d16 | 561 | connectorStatus.authorizeIdTag = commandPayload.idTag; |
2e3d65ae | 562 | const authorizeResponse: OCPP16AuthorizeResponse = |
f22266fd | 563 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16AuthorizeResponse>( |
2e3d65ae JB |
564 | OCPP16RequestCommand.AUTHORIZE, |
565 | { | |
566 | idTag: commandPayload.idTag, | |
567 | } | |
f22266fd | 568 | ); |
a7fc8211 JB |
569 | if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
570 | authorized = true; | |
a7fc8211 | 571 | } |
71068fb9 | 572 | } else { |
e7aeea18 JB |
573 | logger.warn( |
574 | `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled` | |
575 | ); | |
a7fc8211 JB |
576 | } |
577 | if (authorized) { | |
578 | // Authorization successful, start transaction | |
e7aeea18 JB |
579 | if ( |
580 | this.setRemoteStartTransactionChargingProfile( | |
581 | transactionConnectorId, | |
582 | commandPayload.chargingProfile | |
583 | ) | |
584 | ) { | |
658e2d16 | 585 | connectorStatus.transactionRemoteStarted = true; |
e7aeea18 JB |
586 | if ( |
587 | ( | |
f22266fd | 588 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StartTransactionResponse>( |
e7454a1f JB |
589 | OCPP16RequestCommand.START_TRANSACTION, |
590 | { | |
591 | connectorId: transactionConnectorId, | |
592 | idTag: commandPayload.idTag, | |
593 | } | |
f22266fd | 594 | ) |
e7aeea18 JB |
595 | ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED |
596 | ) { | |
597 | logger.debug( | |
598 | this.chargingStation.logPrefix() + | |
599 | ' Transaction remotely STARTED on ' + | |
600 | this.chargingStation.stationInfo.chargingStationId + | |
601 | '#' + | |
602 | transactionConnectorId.toString() + | |
603 | ' for idTag ' + | |
604 | commandPayload.idTag | |
605 | ); | |
e060fe58 JB |
606 | return Constants.OCPP_RESPONSE_ACCEPTED; |
607 | } | |
e7aeea18 JB |
608 | return this.notifyRemoteStartTransactionRejected( |
609 | transactionConnectorId, | |
610 | commandPayload.idTag | |
611 | ); | |
e060fe58 | 612 | } |
e7aeea18 JB |
613 | return this.notifyRemoteStartTransactionRejected( |
614 | transactionConnectorId, | |
615 | commandPayload.idTag | |
616 | ); | |
a7fc8211 | 617 | } |
e7aeea18 JB |
618 | return this.notifyRemoteStartTransactionRejected( |
619 | transactionConnectorId, | |
620 | commandPayload.idTag | |
621 | ); | |
36f6a92e | 622 | } |
a7fc8211 | 623 | // No authorization check required, start transaction |
e7aeea18 JB |
624 | if ( |
625 | this.setRemoteStartTransactionChargingProfile( | |
626 | transactionConnectorId, | |
627 | commandPayload.chargingProfile | |
628 | ) | |
629 | ) { | |
658e2d16 | 630 | connectorStatus.transactionRemoteStarted = true; |
e7aeea18 JB |
631 | if ( |
632 | ( | |
f22266fd | 633 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StartTransactionResponse>( |
e7454a1f JB |
634 | OCPP16RequestCommand.START_TRANSACTION, |
635 | { | |
636 | connectorId: transactionConnectorId, | |
637 | idTag: commandPayload.idTag, | |
638 | } | |
f22266fd | 639 | ) |
e7aeea18 JB |
640 | ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED |
641 | ) { | |
642 | logger.debug( | |
643 | this.chargingStation.logPrefix() + | |
644 | ' Transaction remotely STARTED on ' + | |
645 | this.chargingStation.stationInfo.chargingStationId + | |
646 | '#' + | |
647 | transactionConnectorId.toString() + | |
648 | ' for idTag ' + | |
649 | commandPayload.idTag | |
650 | ); | |
e060fe58 JB |
651 | return Constants.OCPP_RESPONSE_ACCEPTED; |
652 | } | |
e7aeea18 JB |
653 | return this.notifyRemoteStartTransactionRejected( |
654 | transactionConnectorId, | |
655 | commandPayload.idTag | |
656 | ); | |
e060fe58 | 657 | } |
e7aeea18 JB |
658 | return this.notifyRemoteStartTransactionRejected( |
659 | transactionConnectorId, | |
660 | commandPayload.idTag | |
661 | ); | |
c0560973 | 662 | } |
e7aeea18 JB |
663 | return this.notifyRemoteStartTransactionRejected( |
664 | transactionConnectorId, | |
665 | commandPayload.idTag | |
666 | ); | |
c0560973 | 667 | } |
57939a9d | 668 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
a7fc8211 JB |
669 | } |
670 | ||
e7aeea18 JB |
671 | private async notifyRemoteStartTransactionRejected( |
672 | connectorId: number, | |
673 | idTag: string | |
674 | ): Promise<DefaultResponse> { | |
675 | if ( | |
676 | this.chargingStation.getConnectorStatus(connectorId).status !== | |
677 | OCPP16ChargePointStatus.AVAILABLE | |
678 | ) { | |
f22266fd | 679 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
680 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
681 | { | |
682 | connectorId, | |
683 | status: OCPP16ChargePointStatus.AVAILABLE, | |
684 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
685 | } | |
e7aeea18 JB |
686 | ); |
687 | this.chargingStation.getConnectorStatus(connectorId).status = | |
688 | OCPP16ChargePointStatus.AVAILABLE; | |
e060fe58 | 689 | } |
e7aeea18 JB |
690 | logger.warn( |
691 | this.chargingStation.logPrefix() + | |
692 | ' Remote starting transaction REJECTED on connector Id ' + | |
693 | connectorId.toString() + | |
694 | ', idTag ' + | |
695 | idTag + | |
696 | ', availability ' + | |
697 | this.chargingStation.getConnectorStatus(connectorId).availability + | |
698 | ', status ' + | |
699 | this.chargingStation.getConnectorStatus(connectorId).status | |
700 | ); | |
c0560973 JB |
701 | return Constants.OCPP_RESPONSE_REJECTED; |
702 | } | |
703 | ||
e7aeea18 JB |
704 | private setRemoteStartTransactionChargingProfile( |
705 | connectorId: number, | |
706 | cp: OCPP16ChargingProfile | |
707 | ): boolean { | |
a7fc8211 JB |
708 | if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { |
709 | this.chargingStation.setChargingProfile(connectorId, cp); | |
e7aeea18 JB |
710 | logger.debug( |
711 | `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, | |
712 | this.chargingStation.getConnectorStatus(connectorId).chargingProfiles | |
713 | ); | |
a7fc8211 JB |
714 | return true; |
715 | } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { | |
e7aeea18 JB |
716 | logger.warn( |
717 | `${this.chargingStation.logPrefix()} Not allowed to set ${ | |
718 | cp.chargingProfilePurpose | |
719 | } charging profile(s) at remote start transaction` | |
720 | ); | |
a7fc8211 | 721 | return false; |
e060fe58 JB |
722 | } else if (!cp) { |
723 | return true; | |
a7fc8211 JB |
724 | } |
725 | } | |
726 | ||
e7aeea18 JB |
727 | private async handleRequestRemoteStopTransaction( |
728 | commandPayload: RemoteStopTransactionRequest | |
729 | ): Promise<DefaultResponse> { | |
c0560973 | 730 | const transactionId = commandPayload.transactionId; |
734d790d | 731 | for (const connectorId of this.chargingStation.connectors.keys()) { |
e7aeea18 JB |
732 | if ( |
733 | connectorId > 0 && | |
734 | this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId | |
735 | ) { | |
f22266fd | 736 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>( |
93b4a429 JB |
737 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
738 | { | |
739 | connectorId, | |
740 | status: OCPP16ChargePointStatus.FINISHING, | |
741 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, | |
742 | } | |
e7aeea18 JB |
743 | ); |
744 | this.chargingStation.getConnectorStatus(connectorId).status = | |
745 | OCPP16ChargePointStatus.FINISHING; | |
68c993d5 JB |
746 | if ( |
747 | this.chargingStation.getBeginEndMeterValues() && | |
748 | this.chargingStation.getOcppStrictCompliance() && | |
749 | !this.chargingStation.getOutOfOrderEndMeterValues() | |
750 | ) { | |
751 | // FIXME: Implement OCPP version agnostic helpers | |
752 | const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( | |
753 | this.chargingStation, | |
754 | connectorId, | |
755 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) | |
756 | ); | |
f22266fd | 757 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>( |
3a33b6a9 JB |
758 | OCPP16RequestCommand.METER_VALUES, |
759 | { | |
760 | connectorId, | |
761 | transactionId, | |
762 | meterValue: transactionEndMeterValue, | |
763 | } | |
68c993d5 JB |
764 | ); |
765 | } | |
f22266fd | 766 | await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StopTransactionResponse>( |
68c993d5 JB |
767 | OCPP16RequestCommand.STOP_TRANSACTION, |
768 | { | |
769 | transactionId, | |
770 | meterStop: | |
771 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), | |
772 | idTag: this.chargingStation.getTransactionIdTag(transactionId), | |
773 | } | |
e7aeea18 | 774 | ); |
c0560973 JB |
775 | return Constants.OCPP_RESPONSE_ACCEPTED; |
776 | } | |
777 | } | |
e7aeea18 JB |
778 | logger.info( |
779 | this.chargingStation.logPrefix() + | |
780 | ' Trying to remote stop a non existing transaction ' + | |
781 | transactionId.toString() | |
782 | ); | |
c0560973 JB |
783 | return Constants.OCPP_RESPONSE_REJECTED; |
784 | } | |
47e22477 | 785 | |
e7aeea18 JB |
786 | private async handleRequestGetDiagnostics( |
787 | commandPayload: GetDiagnosticsRequest | |
788 | ): Promise<GetDiagnosticsResponse> { | |
789 | logger.debug( | |
790 | this.chargingStation.logPrefix() + | |
791 | ' ' + | |
792 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS + | |
793 | ' request received: %j', | |
794 | commandPayload | |
795 | ); | |
a3868ec4 | 796 | const uri = new URL(commandPayload.location); |
47e22477 JB |
797 | if (uri.protocol.startsWith('ftp:')) { |
798 | let ftpClient: Client; | |
799 | try { | |
e7aeea18 JB |
800 | const logFiles = fs |
801 | .readdirSync(path.resolve(__dirname, '../../../../')) | |
802 | .filter((file) => file.endsWith('.log')) | |
803 | .map((file) => path.join('./', file)); | |
804 | const diagnosticsArchive = | |
805 | this.chargingStation.stationInfo.chargingStationId + '_logs.tar.gz'; | |
47e22477 JB |
806 | tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive)); |
807 | ftpClient = new Client(); | |
808 | const accessResponse = await ftpClient.access({ | |
809 | host: uri.host, | |
e8191622 JB |
810 | ...(!Utils.isEmptyString(uri.port) && { port: Utils.convertToInt(uri.port) }), |
811 | ...(!Utils.isEmptyString(uri.username) && { user: uri.username }), | |
812 | ...(!Utils.isEmptyString(uri.password) && { password: uri.password }), | |
47e22477 JB |
813 | }); |
814 | let uploadResponse: FTPResponse; | |
815 | if (accessResponse.code === 220) { | |
816 | // eslint-disable-next-line @typescript-eslint/no-misused-promises | |
817 | ftpClient.trackProgress(async (info) => { | |
e7aeea18 JB |
818 | logger.info( |
819 | `${this.chargingStation.logPrefix()} ${ | |
820 | info.bytes / 1024 | |
821 | } bytes transferred from diagnostics archive ${info.name}` | |
822 | ); | |
f22266fd | 823 | await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>( |
bad85ee4 JB |
824 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, |
825 | { | |
826 | status: OCPP16DiagnosticsStatus.Uploading, | |
827 | } | |
e7aeea18 | 828 | ); |
47e22477 | 829 | }); |
e7aeea18 JB |
830 | uploadResponse = await ftpClient.uploadFrom( |
831 | path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), | |
832 | uri.pathname + diagnosticsArchive | |
833 | ); | |
47e22477 | 834 | if (uploadResponse.code === 226) { |
f22266fd | 835 | await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>( |
bad85ee4 JB |
836 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, |
837 | { | |
838 | status: OCPP16DiagnosticsStatus.Uploaded, | |
839 | } | |
e7aeea18 | 840 | ); |
47e22477 JB |
841 | if (ftpClient) { |
842 | ftpClient.close(); | |
843 | } | |
844 | return { fileName: diagnosticsArchive }; | |
845 | } | |
e7aeea18 JB |
846 | throw new OCPPError( |
847 | ErrorType.GENERIC_ERROR, | |
848 | `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${ | |
849 | uploadResponse?.code && '|' + uploadResponse?.code.toString() | |
850 | }`, | |
851 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS | |
852 | ); | |
47e22477 | 853 | } |
e7aeea18 JB |
854 | throw new OCPPError( |
855 | ErrorType.GENERIC_ERROR, | |
856 | `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${ | |
857 | uploadResponse?.code && '|' + uploadResponse?.code.toString() | |
858 | }`, | |
859 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS | |
860 | ); | |
47e22477 | 861 | } catch (error) { |
f22266fd | 862 | await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>( |
bad85ee4 JB |
863 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, |
864 | { | |
865 | status: OCPP16DiagnosticsStatus.UploadFailed, | |
866 | } | |
e7aeea18 | 867 | ); |
47e22477 JB |
868 | if (ftpClient) { |
869 | ftpClient.close(); | |
870 | } | |
e7aeea18 JB |
871 | return this.handleIncomingRequestError( |
872 | OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, | |
873 | error as Error, | |
874 | { errorResponse: Constants.OCPP_RESPONSE_EMPTY } | |
875 | ); | |
47e22477 JB |
876 | } |
877 | } else { | |
e7aeea18 JB |
878 | logger.error( |
879 | `${this.chargingStation.logPrefix()} Unsupported protocol ${ | |
880 | uri.protocol | |
881 | } to transfer the diagnostic logs archive` | |
882 | ); | |
f22266fd | 883 | await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>( |
bad85ee4 JB |
884 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, |
885 | { | |
886 | status: OCPP16DiagnosticsStatus.UploadFailed, | |
887 | } | |
e7aeea18 | 888 | ); |
47e22477 JB |
889 | return Constants.OCPP_RESPONSE_EMPTY; |
890 | } | |
891 | } | |
802cfa13 | 892 | |
e7aeea18 JB |
893 | private handleRequestTriggerMessage( |
894 | commandPayload: OCPP16TriggerMessageRequest | |
895 | ): OCPP16TriggerMessageResponse { | |
802cfa13 JB |
896 | try { |
897 | switch (commandPayload.requestedMessage) { | |
898 | case MessageTrigger.BootNotification: | |
899 | setTimeout(() => { | |
e7aeea18 | 900 | this.chargingStation.ocppRequestService |
f22266fd | 901 | .sendMessageHandler<OCPP16BootNotificationResponse>( |
6a8b180d JB |
902 | OCPP16RequestCommand.BOOT_NOTIFICATION, |
903 | { | |
904 | chargePointModel: | |
905 | this.chargingStation.getBootNotificationRequest().chargePointModel, | |
906 | chargePointVendor: | |
907 | this.chargingStation.getBootNotificationRequest().chargePointVendor, | |
908 | chargeBoxSerialNumber: | |
909 | this.chargingStation.getBootNotificationRequest().chargeBoxSerialNumber, | |
910 | firmwareVersion: | |
911 | this.chargingStation.getBootNotificationRequest().firmwareVersion, | |
912 | chargePointSerialNumber: | |
913 | this.chargingStation.getBootNotificationRequest().chargePointSerialNumber, | |
914 | iccid: this.chargingStation.getBootNotificationRequest().iccid, | |
915 | imsi: this.chargingStation.getBootNotificationRequest().imsi, | |
916 | meterSerialNumber: | |
917 | this.chargingStation.getBootNotificationRequest().meterSerialNumber, | |
918 | meterType: this.chargingStation.getBootNotificationRequest().meterType, | |
919 | }, | |
920 | { skipBufferingOnError: true, triggerMessage: true } | |
e7aeea18 | 921 | ) |
ae711c83 JB |
922 | .then((value) => { |
923 | this.chargingStation.bootNotificationResponse = value; | |
924 | }) | |
e7aeea18 JB |
925 | .catch(() => { |
926 | /* This is intentional */ | |
927 | }); | |
802cfa13 JB |
928 | }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); |
929 | return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; | |
930 | case MessageTrigger.Heartbeat: | |
931 | setTimeout(() => { | |
e7aeea18 | 932 | this.chargingStation.ocppRequestService |
f22266fd JB |
933 | .sendMessageHandler<OCPP16HeartbeatResponse>(OCPP16RequestCommand.HEARTBEAT, null, { |
934 | triggerMessage: true, | |
935 | }) | |
e7aeea18 JB |
936 | .catch(() => { |
937 | /* This is intentional */ | |
938 | }); | |
802cfa13 JB |
939 | }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); |
940 | return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; | |
941 | default: | |
942 | return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; | |
943 | } | |
944 | } catch (error) { | |
e7aeea18 JB |
945 | return this.handleIncomingRequestError( |
946 | OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, | |
947 | error as Error, | |
948 | { errorResponse: Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED } | |
949 | ); | |
802cfa13 JB |
950 | } |
951 | } | |
c0560973 | 952 | } |