1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 ChangeAvailabilityRequest
,
5 ChangeConfigurationRequest
,
6 ClearChargingProfileRequest
,
7 GetConfigurationRequest
,
10 OCPP16AvailabilityType
,
11 OCPP16IncomingRequestCommand
,
13 OCPP16TriggerMessageRequest
,
14 RemoteStartTransactionRequest
,
15 RemoteStopTransactionRequest
,
17 SetChargingProfileRequest
,
18 UnlockConnectorRequest
,
19 } from
'../../../types/ocpp/1.6/Requests';
21 ChangeAvailabilityResponse
,
22 ChangeConfigurationResponse
,
23 ClearChargingProfileResponse
,
24 GetConfigurationResponse
,
25 GetDiagnosticsResponse
,
26 OCPP16TriggerMessageResponse
,
27 SetChargingProfileResponse
,
28 UnlockConnectorResponse
,
29 } from
'../../../types/ocpp/1.6/Responses';
31 ChargingProfilePurposeType
,
32 OCPP16ChargingProfile
,
33 } from
'../../../types/ocpp/1.6/ChargingProfile';
34 import { Client
, FTPResponse
} from
'basic-ftp';
36 OCPP16AuthorizationStatus
,
37 OCPP16AuthorizeResponse
,
38 OCPP16StopTransactionReason
,
39 } from
'../../../types/ocpp/1.6/Transaction';
41 import type ChargingStation from
'../../ChargingStation';
42 import Constants from
'../../../utils/Constants';
43 import { DefaultResponse
} from
'../../../types/ocpp/Responses';
44 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
45 import { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
46 import { JsonType
} from
'../../../types/JsonType';
47 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
48 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
49 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
50 import { OCPP16StandardParametersKey
} from
'../../../types/ocpp/1.6/Configuration';
51 import { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
52 import OCPPError from
'../../../exception/OCPPError';
53 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
54 import { URL
} from
'url';
55 import Utils from
'../../../utils/Utils';
57 import logger from
'../../../utils/Logger';
58 import path from
'path';
59 import tar from
'tar';
61 const moduleName
= 'OCPP16IncomingRequestService';
63 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
64 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
66 public constructor(chargingStation
: ChargingStation
) {
67 if (new.target
?.name
=== moduleName
) {
68 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
70 super(chargingStation
);
71 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
72 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
73 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
74 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
76 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
77 this.handleRequestGetConfiguration
.bind(this),
80 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
81 this.handleRequestChangeConfiguration
.bind(this),
84 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
85 this.handleRequestSetChargingProfile
.bind(this),
88 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
89 this.handleRequestClearChargingProfile
.bind(this),
92 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
93 this.handleRequestChangeAvailability
.bind(this),
96 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
97 this.handleRequestRemoteStartTransaction
.bind(this),
100 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
101 this.handleRequestRemoteStopTransaction
.bind(this),
103 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
104 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
108 public async handleRequest(
110 commandName
: OCPP16IncomingRequestCommand
,
111 commandPayload
: JsonType
113 let result
: JsonType
;
115 this.chargingStation
.getOcppStrictCompliance() &&
116 this.chargingStation
.isInPendingState() &&
117 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
118 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
121 ErrorType
.SECURITY_ERROR
,
122 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
126 )} while the charging station is in pending state on the central server`,
131 this.chargingStation
.isRegistered() ||
132 (!this.chargingStation
.getOcppStrictCompliance() && this.chargingStation
.isInUnknownState())
134 if (this.incomingRequestHandlers
.has(commandName
)) {
136 // Call the method to build the result
137 result
= await this.incomingRequestHandlers
.get(commandName
)(commandPayload
);
140 logger
.error(this.chargingStation
.logPrefix() + ' Handle request error: %j', error
);
146 ErrorType
.NOT_IMPLEMENTED
,
147 `${commandName} is not implemented to handle request payload ${JSON.stringify(
157 ErrorType
.SECURITY_ERROR
,
158 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
162 )} while the charging station is not registered on the central server.`,
166 // Send the built result
167 await this.chargingStation
.ocppRequestService
.sendResult(messageId
, result
, commandName
);
170 // Simulate charging station restart
171 private handleRequestReset(commandPayload
: ResetRequest
): DefaultResponse
{
172 // eslint-disable-next-line @typescript-eslint/no-misused-promises
173 setImmediate(async (): Promise
<void> => {
174 await this.chargingStation
.stop(
175 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
177 await Utils
.sleep(this.chargingStation
.stationInfo
.resetTime
);
178 this.chargingStation
.start();
181 `${this.chargingStation.logPrefix()} ${
183 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
184 this.chargingStation.stationInfo.resetTime
187 return Constants
.OCPP_RESPONSE_ACCEPTED
;
190 private handleRequestClearCache(): DefaultResponse
{
191 return Constants
.OCPP_RESPONSE_ACCEPTED
;
194 private async handleRequestUnlockConnector(
195 commandPayload
: UnlockConnectorRequest
196 ): Promise
<UnlockConnectorResponse
> {
197 const connectorId
= commandPayload
.connectorId
;
198 if (connectorId
=== 0) {
200 this.chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
202 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
204 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
205 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
).transactionId
;
206 const stopResponse
= await this.chargingStation
.ocppRequestService
.sendStopTransaction(
208 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
209 this.chargingStation
.getTransactionIdTag(transactionId
),
210 OCPP16StopTransactionReason
.UNLOCK_COMMAND
212 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
213 return Constants
.OCPP_RESPONSE_UNLOCKED
;
215 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
217 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
218 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
221 status: OCPP16ChargePointStatus
.AVAILABLE
,
222 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
225 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
226 return Constants
.OCPP_RESPONSE_UNLOCKED
;
229 private handleRequestGetConfiguration(
230 commandPayload
: GetConfigurationRequest
231 ): GetConfigurationResponse
{
232 const configurationKey
: OCPPConfigurationKey
[] = [];
233 const unknownKey
: string[] = [];
234 if (Utils
.isEmptyArray(commandPayload
.key
)) {
235 for (const configuration
of this.chargingStation
.configuration
.configurationKey
) {
236 if (Utils
.isUndefined(configuration
.visible
)) {
237 configuration
.visible
= true;
239 if (!configuration
.visible
) {
242 configurationKey
.push({
243 key
: configuration
.key
,
244 readonly: configuration
.readonly,
245 value
: configuration
.value
,
249 for (const key
of commandPayload
.key
) {
250 const keyFound
= this.chargingStation
.getConfigurationKey(key
);
252 if (Utils
.isUndefined(keyFound
.visible
)) {
253 keyFound
.visible
= true;
255 if (!keyFound
.visible
) {
258 configurationKey
.push({
260 readonly: keyFound
.readonly,
261 value
: keyFound
.value
,
264 unknownKey
.push(key
);
274 private handleRequestChangeConfiguration(
275 commandPayload
: ChangeConfigurationRequest
276 ): ChangeConfigurationResponse
{
277 // JSON request fields type sanity check
278 if (!Utils
.isString(commandPayload
.key
)) {
280 `${this.chargingStation.logPrefix()} ${
281 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
282 } request key field is not a string:`,
286 if (!Utils
.isString(commandPayload
.value
)) {
288 `${this.chargingStation.logPrefix()} ${
289 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
290 } request value field is not a string:`,
294 const keyToChange
= this.chargingStation
.getConfigurationKey(commandPayload
.key
, true);
296 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
297 } else if (keyToChange
&& keyToChange
.readonly) {
298 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
299 } else if (keyToChange
&& !keyToChange
.readonly) {
300 const keyIndex
= this.chargingStation
.configuration
.configurationKey
.indexOf(keyToChange
);
301 let valueChanged
= false;
303 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
!== commandPayload
.value
305 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
= commandPayload
.value
;
308 let triggerHeartbeatRestart
= false;
309 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
310 this.chargingStation
.setConfigurationKeyValue(
311 OCPP16StandardParametersKey
.HeartbeatInterval
,
314 triggerHeartbeatRestart
= true;
316 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
317 this.chargingStation
.setConfigurationKeyValue(
318 OCPP16StandardParametersKey
.HeartBeatInterval
,
321 triggerHeartbeatRestart
= true;
323 if (triggerHeartbeatRestart
) {
324 this.chargingStation
.restartHeartbeat();
326 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
327 this.chargingStation
.restartWebSocketPing();
329 if (keyToChange
.reboot
) {
330 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
332 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
336 private handleRequestSetChargingProfile(
337 commandPayload
: SetChargingProfileRequest
338 ): SetChargingProfileResponse
{
339 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
341 `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
342 commandPayload.connectorId
345 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
348 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
349 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
350 commandPayload
.connectorId
!== 0
352 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
355 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
356 ChargingProfilePurposeType
.TX_PROFILE
&&
357 (commandPayload
.connectorId
=== 0 ||
358 !this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
)
360 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
362 this.chargingStation
.setChargingProfile(
363 commandPayload
.connectorId
,
364 commandPayload
.csChargingProfiles
367 `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`,
368 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
370 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
373 private handleRequestClearChargingProfile(
374 commandPayload
: ClearChargingProfileRequest
375 ): ClearChargingProfileResponse
{
376 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
378 `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
379 commandPayload.connectorId
382 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
385 commandPayload
.connectorId
&&
387 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
390 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
392 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
393 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
395 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
397 if (!commandPayload
.connectorId
) {
398 let clearedCP
= false;
399 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
401 !Utils
.isEmptyArray(this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)
404 .getConnectorStatus(connectorId
)
405 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
406 let clearCurrentCP
= false;
407 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
408 clearCurrentCP
= true;
411 !commandPayload
.chargingProfilePurpose
&&
412 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
414 clearCurrentCP
= true;
417 !chargingProfile
.stackLevel
&&
418 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
420 clearCurrentCP
= true;
423 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
424 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
426 clearCurrentCP
= true;
428 if (clearCurrentCP
) {
429 this.chargingStation
.getConnectorStatus(
430 commandPayload
.connectorId
431 ).chargingProfiles
[index
] = {} as OCPP16ChargingProfile
;
433 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
434 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
443 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
446 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
449 private async handleRequestChangeAvailability(
450 commandPayload
: ChangeAvailabilityRequest
451 ): Promise
<ChangeAvailabilityResponse
> {
452 const connectorId
: number = commandPayload
.connectorId
;
453 if (!this.chargingStation
.getConnectorStatus(connectorId
)) {
455 `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
457 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
459 const chargePointStatus
: OCPP16ChargePointStatus
=
460 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
461 ? OCPP16ChargePointStatus
.AVAILABLE
462 : OCPP16ChargePointStatus
.UNAVAILABLE
;
463 if (connectorId
=== 0) {
464 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
465 for (const id
of this.chargingStation
.connectors
.keys()) {
466 if (this.chargingStation
.getConnectorStatus(id
)?.transactionStarted
) {
467 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
469 this.chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
470 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
471 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
472 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
475 status: chargePointStatus
,
476 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
479 this.chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
485 (this.chargingStation
.getConnectorStatus(0).availability
===
486 OCPP16AvailabilityType
.OPERATIVE
||
487 (this.chargingStation
.getConnectorStatus(0).availability
===
488 OCPP16AvailabilityType
.INOPERATIVE
&&
489 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
491 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
492 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
493 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
495 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
496 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
497 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
498 { connectorId
, status: chargePointStatus
, errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
}
500 this.chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
501 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
503 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
506 private async handleRequestRemoteStartTransaction(
507 commandPayload
: RemoteStartTransactionRequest
508 ): Promise
<DefaultResponse
> {
509 const transactionConnectorId
: number = commandPayload
.connectorId
;
510 if (transactionConnectorId
) {
511 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
512 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
514 connectorId
: transactionConnectorId
,
515 status: OCPP16ChargePointStatus
.PREPARING
,
516 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
519 this.chargingStation
.getConnectorStatus(transactionConnectorId
).status =
520 OCPP16ChargePointStatus
.PREPARING
;
522 this.chargingStation
.isChargingStationAvailable() &&
523 this.chargingStation
.isConnectorAvailable(transactionConnectorId
)
525 // Check if authorized
526 if (this.chargingStation
.getAuthorizeRemoteTxRequests()) {
527 let authorized
= false;
529 this.chargingStation
.getLocalAuthListEnabled() &&
530 this.chargingStation
.hasAuthorizedTags() &&
531 this.chargingStation
.authorizedTags
.find((value
) => value
=== commandPayload
.idTag
)
533 this.chargingStation
.getConnectorStatus(transactionConnectorId
).localAuthorizeIdTag
=
534 commandPayload
.idTag
;
535 this.chargingStation
.getConnectorStatus(transactionConnectorId
).idTagLocalAuthorized
=
538 } else if (this.chargingStation
.getMayAuthorizeAtRemoteStart()) {
539 this.chargingStation
.getConnectorStatus(transactionConnectorId
).authorizeIdTag
=
540 commandPayload
.idTag
;
541 const authorizeResponse
: OCPP16AuthorizeResponse
=
542 (await this.chargingStation
.ocppRequestService
.sendMessageHandler(
543 OCPP16RequestCommand
.AUTHORIZE
,
545 idTag
: commandPayload
.idTag
,
547 )) as OCPP16AuthorizeResponse
;
548 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
553 `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
557 // Authorization successful, start transaction
559 this.setRemoteStartTransactionChargingProfile(
560 transactionConnectorId
,
561 commandPayload
.chargingProfile
564 this.chargingStation
.getConnectorStatus(
565 transactionConnectorId
566 ).transactionRemoteStarted
= true;
569 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
570 transactionConnectorId
,
573 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
576 this.chargingStation
.logPrefix() +
577 ' Transaction remotely STARTED on ' +
578 this.chargingStation
.stationInfo
.chargingStationId
+
580 transactionConnectorId
.toString() +
584 return Constants
.OCPP_RESPONSE_ACCEPTED
;
586 return this.notifyRemoteStartTransactionRejected(
587 transactionConnectorId
,
591 return this.notifyRemoteStartTransactionRejected(
592 transactionConnectorId
,
596 return this.notifyRemoteStartTransactionRejected(
597 transactionConnectorId
,
601 // No authorization check required, start transaction
603 this.setRemoteStartTransactionChargingProfile(
604 transactionConnectorId
,
605 commandPayload
.chargingProfile
608 this.chargingStation
.getConnectorStatus(transactionConnectorId
).transactionRemoteStarted
=
612 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
613 transactionConnectorId
,
616 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
619 this.chargingStation
.logPrefix() +
620 ' Transaction remotely STARTED on ' +
621 this.chargingStation
.stationInfo
.chargingStationId
+
623 transactionConnectorId
.toString() +
627 return Constants
.OCPP_RESPONSE_ACCEPTED
;
629 return this.notifyRemoteStartTransactionRejected(
630 transactionConnectorId
,
634 return this.notifyRemoteStartTransactionRejected(
635 transactionConnectorId
,
639 return this.notifyRemoteStartTransactionRejected(
640 transactionConnectorId
,
644 return this.notifyRemoteStartTransactionRejected(transactionConnectorId
, commandPayload
.idTag
);
647 private async notifyRemoteStartTransactionRejected(
650 ): Promise
<DefaultResponse
> {
652 this.chargingStation
.getConnectorStatus(connectorId
).status !==
653 OCPP16ChargePointStatus
.AVAILABLE
655 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
656 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
659 status: OCPP16ChargePointStatus
.AVAILABLE
,
660 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
663 this.chargingStation
.getConnectorStatus(connectorId
).status =
664 OCPP16ChargePointStatus
.AVAILABLE
;
667 this.chargingStation
.logPrefix() +
668 ' Remote starting transaction REJECTED on connector Id ' +
669 connectorId
.toString() +
673 this.chargingStation
.getConnectorStatus(connectorId
).availability
+
675 this.chargingStation
.getConnectorStatus(connectorId
).status
677 return Constants
.OCPP_RESPONSE_REJECTED
;
680 private setRemoteStartTransactionChargingProfile(
682 cp
: OCPP16ChargingProfile
684 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
685 this.chargingStation
.setChargingProfile(connectorId
, cp
);
687 `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`,
688 this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
691 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
693 `${this.chargingStation.logPrefix()} Not allowed to set ${
694 cp.chargingProfilePurpose
695 } charging profile(s) at remote start transaction`
703 private async handleRequestRemoteStopTransaction(
704 commandPayload
: RemoteStopTransactionRequest
705 ): Promise
<DefaultResponse
> {
706 const transactionId
= commandPayload
.transactionId
;
707 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
710 this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
712 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
713 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
716 status: OCPP16ChargePointStatus
.FINISHING
,
717 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
720 this.chargingStation
.getConnectorStatus(connectorId
).status =
721 OCPP16ChargePointStatus
.FINISHING
;
722 await this.chargingStation
.ocppRequestService
.sendStopTransaction(
724 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
725 this.chargingStation
.getTransactionIdTag(transactionId
)
727 return Constants
.OCPP_RESPONSE_ACCEPTED
;
731 this.chargingStation
.logPrefix() +
732 ' Trying to remote stop a non existing transaction ' +
733 transactionId
.toString()
735 return Constants
.OCPP_RESPONSE_REJECTED
;
738 private async handleRequestGetDiagnostics(
739 commandPayload
: GetDiagnosticsRequest
740 ): Promise
<GetDiagnosticsResponse
> {
742 this.chargingStation
.logPrefix() +
744 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
745 ' request received: %j',
748 const uri
= new URL(commandPayload
.location
);
749 if (uri
.protocol
.startsWith('ftp:')) {
750 let ftpClient
: Client
;
753 .readdirSync(path
.resolve(__dirname
, '../../../../'))
754 .filter((file
) => file
.endsWith('.log'))
755 .map((file
) => path
.join('./', file
));
756 const diagnosticsArchive
=
757 this.chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
758 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
759 ftpClient
= new Client();
760 const accessResponse
= await ftpClient
.access({
762 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
763 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
764 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
766 let uploadResponse
: FTPResponse
;
767 if (accessResponse
.code
=== 220) {
768 // eslint-disable-next-line @typescript-eslint/no-misused-promises
769 ftpClient
.trackProgress(async (info
) => {
771 `${this.chargingStation.logPrefix()} ${
773 } bytes transferred from diagnostics archive ${info.name}`
775 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
776 OCPP16DiagnosticsStatus
.Uploading
779 uploadResponse
= await ftpClient
.uploadFrom(
780 path
.join(path
.resolve(__dirname
, '../../../../'), diagnosticsArchive
),
781 uri
.pathname
+ diagnosticsArchive
783 if (uploadResponse
.code
=== 226) {
784 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
785 OCPP16DiagnosticsStatus
.Uploaded
790 return { fileName
: diagnosticsArchive
};
793 ErrorType
.GENERIC_ERROR
,
794 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
795 uploadResponse?.code && '|' + uploadResponse?.code.toString()
797 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
801 ErrorType
.GENERIC_ERROR
,
802 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
803 uploadResponse?.code && '|' + uploadResponse?.code.toString()
805 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
808 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
809 OCPP16DiagnosticsStatus
.UploadFailed
814 return this.handleIncomingRequestError(
815 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
817 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
822 `${this.chargingStation.logPrefix()} Unsupported protocol ${
824 } to transfer the diagnostic logs archive`
826 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
827 OCPP16DiagnosticsStatus
.UploadFailed
829 return Constants
.OCPP_RESPONSE_EMPTY
;
833 private handleRequestTriggerMessage(
834 commandPayload
: OCPP16TriggerMessageRequest
835 ): OCPP16TriggerMessageResponse
{
837 switch (commandPayload
.requestedMessage
) {
838 case MessageTrigger
.BootNotification
:
840 this.chargingStation
.ocppRequestService
842 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
845 this.chargingStation
.getBootNotificationRequest().chargePointModel
,
847 this.chargingStation
.getBootNotificationRequest().chargePointVendor
,
848 chargeBoxSerialNumber
:
849 this.chargingStation
.getBootNotificationRequest().chargeBoxSerialNumber
,
851 this.chargingStation
.getBootNotificationRequest().firmwareVersion
,
852 chargePointSerialNumber
:
853 this.chargingStation
.getBootNotificationRequest().chargePointSerialNumber
,
854 iccid
: this.chargingStation
.getBootNotificationRequest().iccid
,
855 imsi
: this.chargingStation
.getBootNotificationRequest().imsi
,
857 this.chargingStation
.getBootNotificationRequest().meterSerialNumber
,
858 meterType
: this.chargingStation
.getBootNotificationRequest().meterType
,
860 { skipBufferingOnError
: true, triggerMessage
: true }
863 /* This is intentional */
865 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
866 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
867 case MessageTrigger
.Heartbeat
:
869 this.chargingStation
.ocppRequestService
870 .sendMessageHandler(OCPP16RequestCommand
.HEARTBEAT
, null, { triggerMessage
: true })
872 /* This is intentional */
874 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
875 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
877 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
880 return this.handleIncomingRequestError(
881 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
883 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}