1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 import path from
'path';
5 import { URL
, fileURLToPath
} from
'url';
7 import { Client
, FTPResponse
} from
'basic-ftp';
10 import OCPPError from
'../../../exception/OCPPError';
11 import { JsonType
} from
'../../../types/JsonType';
12 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
13 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
15 ChargingProfilePurposeType
,
16 OCPP16ChargingProfile
,
17 } from
'../../../types/ocpp/1.6/ChargingProfile';
19 OCPP16StandardParametersKey
,
20 OCPP16SupportedFeatureProfiles
,
21 } from
'../../../types/ocpp/1.6/Configuration';
22 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
24 OCPP16MeterValuesRequest
,
25 OCPP16MeterValuesResponse
,
26 } from
'../../../types/ocpp/1.6/MeterValues';
28 ChangeAvailabilityRequest
,
29 ChangeConfigurationRequest
,
30 ClearChargingProfileRequest
,
31 DiagnosticsStatusNotificationRequest
,
32 GetConfigurationRequest
,
33 GetDiagnosticsRequest
,
35 OCPP16AvailabilityType
,
36 OCPP16BootNotificationRequest
,
37 OCPP16HeartbeatRequest
,
38 OCPP16IncomingRequestCommand
,
40 OCPP16StatusNotificationRequest
,
41 OCPP16TriggerMessageRequest
,
42 RemoteStartTransactionRequest
,
43 RemoteStopTransactionRequest
,
45 SetChargingProfileRequest
,
46 UnlockConnectorRequest
,
47 } from
'../../../types/ocpp/1.6/Requests';
49 ChangeAvailabilityResponse
,
50 ChangeConfigurationResponse
,
51 ClearChargingProfileResponse
,
52 DiagnosticsStatusNotificationResponse
,
53 GetConfigurationResponse
,
54 GetDiagnosticsResponse
,
55 OCPP16BootNotificationResponse
,
56 OCPP16HeartbeatResponse
,
57 OCPP16StatusNotificationResponse
,
58 OCPP16TriggerMessageResponse
,
59 SetChargingProfileResponse
,
60 UnlockConnectorResponse
,
61 } from
'../../../types/ocpp/1.6/Responses';
63 OCPP16AuthorizationStatus
,
64 OCPP16AuthorizeRequest
,
65 OCPP16AuthorizeResponse
,
66 OCPP16StartTransactionRequest
,
67 OCPP16StartTransactionResponse
,
68 OCPP16StopTransactionReason
,
69 OCPP16StopTransactionRequest
,
70 OCPP16StopTransactionResponse
,
71 } from
'../../../types/ocpp/1.6/Transaction';
72 import { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
73 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
74 import { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
75 import { DefaultResponse
} from
'../../../types/ocpp/Responses';
76 import Constants from
'../../../utils/Constants';
77 import logger from
'../../../utils/Logger';
78 import Utils from
'../../../utils/Utils';
79 import type ChargingStation from
'../../ChargingStation';
80 import { ChargingStationConfigurationUtils
} from
'../../ChargingStationConfigurationUtils';
81 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
82 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
83 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
85 const moduleName
= 'OCPP16IncomingRequestService';
87 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
88 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
90 public constructor() {
91 if (new.target
?.name
=== moduleName
) {
92 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
95 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
96 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
97 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
98 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
100 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
101 this.handleRequestGetConfiguration
.bind(this),
104 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
105 this.handleRequestChangeConfiguration
.bind(this),
108 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
109 this.handleRequestSetChargingProfile
.bind(this),
112 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
113 this.handleRequestClearChargingProfile
.bind(this),
116 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
117 this.handleRequestChangeAvailability
.bind(this),
120 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
121 this.handleRequestRemoteStartTransaction
.bind(this),
124 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
125 this.handleRequestRemoteStopTransaction
.bind(this),
127 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
128 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
132 public async incomingRequestHandler(
133 chargingStation
: ChargingStation
,
135 commandName
: OCPP16IncomingRequestCommand
,
136 commandPayload
: JsonType
138 let response
: JsonType
;
140 chargingStation
.getOcppStrictCompliance() &&
141 chargingStation
.isInPendingState() &&
142 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
143 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
146 ErrorType
.SECURITY_ERROR
,
147 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
151 )} while the charging station is in pending state on the central server`,
157 chargingStation
.isRegistered() ||
158 (!chargingStation
.getOcppStrictCompliance() && chargingStation
.isInUnknownState())
160 if (this.incomingRequestHandlers
.has(commandName
)) {
162 // Call the method to build the response
163 response
= await this.incomingRequestHandlers
.get(commandName
)(
169 logger
.error(chargingStation
.logPrefix() + ' Handle request error: %j', error
);
175 ErrorType
.NOT_IMPLEMENTED
,
176 `${commandName} is not implemented to handle request payload ${JSON.stringify(
187 ErrorType
.SECURITY_ERROR
,
188 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
192 )} while the charging station is not registered on the central server.`,
197 // Send the built response
198 await chargingStation
.ocppRequestService
.sendResponse(
206 // Simulate charging station restart
207 private handleRequestReset(
208 chargingStation
: ChargingStation
,
209 commandPayload
: ResetRequest
211 // eslint-disable-next-line @typescript-eslint/no-misused-promises
212 setImmediate(async (): Promise
<void> => {
213 await chargingStation
.reset((commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
);
216 `${chargingStation.logPrefix()} ${
218 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
219 chargingStation.stationInfo.resetTime
222 return Constants
.OCPP_RESPONSE_ACCEPTED
;
225 private handleRequestClearCache(): DefaultResponse
{
226 return Constants
.OCPP_RESPONSE_ACCEPTED
;
229 private async handleRequestUnlockConnector(
230 chargingStation
: ChargingStation
,
231 commandPayload
: UnlockConnectorRequest
232 ): Promise
<UnlockConnectorResponse
> {
233 const connectorId
= commandPayload
.connectorId
;
234 if (connectorId
=== 0) {
236 chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
238 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
240 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
241 const transactionId
= chargingStation
.getConnectorStatus(connectorId
).transactionId
;
243 chargingStation
.getBeginEndMeterValues() &&
244 chargingStation
.getOcppStrictCompliance() &&
245 !chargingStation
.getOutOfOrderEndMeterValues()
247 // FIXME: Implement OCPP version agnostic helpers
248 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
251 chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
)
253 await chargingStation
.ocppRequestService
.requestHandler
<
254 OCPP16MeterValuesRequest
,
255 OCPP16MeterValuesResponse
256 >(chargingStation
, OCPP16RequestCommand
.METER_VALUES
, {
259 meterValue
: [transactionEndMeterValue
],
262 const stopResponse
= await chargingStation
.ocppRequestService
.requestHandler
<
263 OCPP16StopTransactionRequest
,
264 OCPP16StopTransactionResponse
265 >(chargingStation
, OCPP16RequestCommand
.STOP_TRANSACTION
, {
267 meterStop
: chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
268 idTag
: chargingStation
.getTransactionIdTag(transactionId
),
269 reason
: OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
271 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
272 return Constants
.OCPP_RESPONSE_UNLOCKED
;
274 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
276 await chargingStation
.ocppRequestService
.requestHandler
<
277 OCPP16StatusNotificationRequest
,
278 OCPP16StatusNotificationResponse
279 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
281 status: OCPP16ChargePointStatus
.AVAILABLE
,
282 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
284 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
285 return Constants
.OCPP_RESPONSE_UNLOCKED
;
288 private handleRequestGetConfiguration(
289 chargingStation
: ChargingStation
,
290 commandPayload
: GetConfigurationRequest
291 ): GetConfigurationResponse
{
292 const configurationKey
: OCPPConfigurationKey
[] = [];
293 const unknownKey
: string[] = [];
294 if (Utils
.isEmptyArray(commandPayload
.key
)) {
295 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
296 if (Utils
.isUndefined(configuration
.visible
)) {
297 configuration
.visible
= true;
299 if (!configuration
.visible
) {
302 configurationKey
.push({
303 key
: configuration
.key
,
304 readonly: configuration
.readonly,
305 value
: configuration
.value
,
309 for (const key
of commandPayload
.key
) {
310 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
315 if (Utils
.isUndefined(keyFound
.visible
)) {
316 keyFound
.visible
= true;
318 if (!keyFound
.visible
) {
321 configurationKey
.push({
323 readonly: keyFound
.readonly,
324 value
: keyFound
.value
,
327 unknownKey
.push(key
);
337 private handleRequestChangeConfiguration(
338 chargingStation
: ChargingStation
,
339 commandPayload
: ChangeConfigurationRequest
340 ): ChangeConfigurationResponse
{
341 // JSON request fields type sanity check
342 if (!Utils
.isString(commandPayload
.key
)) {
344 `${chargingStation.logPrefix()} ${
345 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
346 } request key field is not a string:`,
350 if (!Utils
.isString(commandPayload
.value
)) {
352 `${chargingStation.logPrefix()} ${
353 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
354 } request value field is not a string:`,
358 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
364 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
365 } else if (keyToChange
&& keyToChange
.readonly) {
366 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
367 } else if (keyToChange
&& !keyToChange
.readonly) {
368 let valueChanged
= false;
369 if (keyToChange
.value
!== commandPayload
.value
) {
370 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
373 commandPayload
.value
,
378 let triggerHeartbeatRestart
= false;
379 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
380 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
382 OCPP16StandardParametersKey
.HeartbeatInterval
,
385 triggerHeartbeatRestart
= true;
387 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
388 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
390 OCPP16StandardParametersKey
.HeartBeatInterval
,
393 triggerHeartbeatRestart
= true;
395 if (triggerHeartbeatRestart
) {
396 chargingStation
.restartHeartbeat();
398 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
399 chargingStation
.restartWebSocketPing();
401 if (keyToChange
.reboot
) {
402 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
404 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
408 private handleRequestSetChargingProfile(
409 chargingStation
: ChargingStation
,
410 commandPayload
: SetChargingProfileRequest
411 ): SetChargingProfileResponse
{
413 !OCPP16ServiceUtils
.checkFeatureProfile(
415 OCPP16SupportedFeatureProfiles
.SmartCharging
,
416 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
419 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
421 if (!chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
423 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
424 commandPayload.connectorId
427 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
430 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
431 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
432 commandPayload
.connectorId
!== 0
434 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
437 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
438 ChargingProfilePurposeType
.TX_PROFILE
&&
439 (commandPayload
.connectorId
=== 0 ||
440 !chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
)
442 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
444 chargingStation
.setChargingProfile(
445 commandPayload
.connectorId
,
446 commandPayload
.csChargingProfiles
449 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
450 commandPayload.connectorId
451 }, dump their stack: %j`,
452 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
454 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
457 private handleRequestClearChargingProfile(
458 chargingStation
: ChargingStation
,
459 commandPayload
: ClearChargingProfileRequest
460 ): ClearChargingProfileResponse
{
462 !OCPP16ServiceUtils
.checkFeatureProfile(
464 OCPP16SupportedFeatureProfiles
.SmartCharging
,
465 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
468 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
470 const connectorStatus
= chargingStation
.getConnectorStatus(commandPayload
.connectorId
);
471 if (!connectorStatus
) {
473 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
474 commandPayload.connectorId
477 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
479 if (commandPayload
.connectorId
&& !Utils
.isEmptyArray(connectorStatus
.chargingProfiles
)) {
480 connectorStatus
.chargingProfiles
= [];
482 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
483 commandPayload.connectorId
484 }, dump their stack: %j`,
485 connectorStatus
.chargingProfiles
487 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
489 if (!commandPayload
.connectorId
) {
490 let clearedCP
= false;
491 for (const connectorId
of chargingStation
.connectors
.keys()) {
492 if (!Utils
.isEmptyArray(chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)) {
494 .getConnectorStatus(connectorId
)
495 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
496 let clearCurrentCP
= false;
497 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
498 clearCurrentCP
= true;
501 !commandPayload
.chargingProfilePurpose
&&
502 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
504 clearCurrentCP
= true;
507 !chargingProfile
.stackLevel
&&
508 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
510 clearCurrentCP
= true;
513 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
514 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
516 clearCurrentCP
= true;
518 if (clearCurrentCP
) {
519 connectorStatus
.chargingProfiles
.splice(index
, 1);
521 `${chargingStation.logPrefix()} Matching charging profile(s) cleared on connector id ${
522 commandPayload.connectorId
523 }, dump their stack: %j`,
524 connectorStatus
.chargingProfiles
532 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
535 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
538 private async handleRequestChangeAvailability(
539 chargingStation
: ChargingStation
,
540 commandPayload
: ChangeAvailabilityRequest
541 ): Promise
<ChangeAvailabilityResponse
> {
542 const connectorId
: number = commandPayload
.connectorId
;
543 if (!chargingStation
.getConnectorStatus(connectorId
)) {
545 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
547 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
549 const chargePointStatus
: OCPP16ChargePointStatus
=
550 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
551 ? OCPP16ChargePointStatus
.AVAILABLE
552 : OCPP16ChargePointStatus
.UNAVAILABLE
;
553 if (connectorId
=== 0) {
554 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
555 for (const id
of chargingStation
.connectors
.keys()) {
556 if (chargingStation
.getConnectorStatus(id
)?.transactionStarted
) {
557 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
559 chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
560 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
561 await chargingStation
.ocppRequestService
.requestHandler
<
562 OCPP16StatusNotificationRequest
,
563 OCPP16StatusNotificationResponse
564 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
566 status: chargePointStatus
,
567 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
569 chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
575 (chargingStation
.getConnectorStatus(0).availability
=== OCPP16AvailabilityType
.OPERATIVE
||
576 (chargingStation
.getConnectorStatus(0).availability
===
577 OCPP16AvailabilityType
.INOPERATIVE
&&
578 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
580 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
581 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
582 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
584 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
585 await chargingStation
.ocppRequestService
.requestHandler
<
586 OCPP16StatusNotificationRequest
,
587 OCPP16StatusNotificationResponse
588 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
590 status: chargePointStatus
,
591 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
593 chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
594 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
596 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
599 private async handleRequestRemoteStartTransaction(
600 chargingStation
: ChargingStation
,
601 commandPayload
: RemoteStartTransactionRequest
602 ): Promise
<DefaultResponse
> {
603 const transactionConnectorId
= commandPayload
.connectorId
;
604 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
605 if (transactionConnectorId
) {
606 await chargingStation
.ocppRequestService
.requestHandler
<
607 OCPP16StatusNotificationRequest
,
608 OCPP16StatusNotificationResponse
609 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
610 connectorId
: transactionConnectorId
,
611 status: OCPP16ChargePointStatus
.PREPARING
,
612 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
614 connectorStatus
.status = OCPP16ChargePointStatus
.PREPARING
;
615 if (chargingStation
.isChargingStationAvailable() && connectorStatus
) {
616 // Check if authorized
617 if (chargingStation
.getAuthorizeRemoteTxRequests()) {
618 let authorized
= false;
620 chargingStation
.getLocalAuthListEnabled() &&
621 chargingStation
.hasAuthorizedTags() &&
622 chargingStation
.authorizedTagsCache
624 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
626 .find((value
) => value
=== commandPayload
.idTag
)
628 connectorStatus
.localAuthorizeIdTag
= commandPayload
.idTag
;
629 connectorStatus
.idTagLocalAuthorized
= true;
631 } else if (chargingStation
.getMayAuthorizeAtRemoteStart()) {
632 connectorStatus
.authorizeIdTag
= commandPayload
.idTag
;
633 const authorizeResponse
: OCPP16AuthorizeResponse
=
634 await chargingStation
.ocppRequestService
.requestHandler
<
635 OCPP16AuthorizeRequest
,
636 OCPP16AuthorizeResponse
637 >(chargingStation
, OCPP16RequestCommand
.AUTHORIZE
, {
638 idTag
: commandPayload
.idTag
,
640 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
645 `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
649 // Authorization successful, start transaction
651 this.setRemoteStartTransactionChargingProfile(
653 transactionConnectorId
,
654 commandPayload
.chargingProfile
657 connectorStatus
.transactionRemoteStarted
= true;
660 await chargingStation
.ocppRequestService
.requestHandler
<
661 OCPP16StartTransactionRequest
,
662 OCPP16StartTransactionResponse
663 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
664 connectorId
: transactionConnectorId
,
665 idTag
: commandPayload
.idTag
,
667 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
670 chargingStation
.logPrefix() +
671 ' Transaction remotely STARTED on ' +
672 chargingStation
.stationInfo
.chargingStationId
+
674 transactionConnectorId
.toString() +
678 return Constants
.OCPP_RESPONSE_ACCEPTED
;
680 return this.notifyRemoteStartTransactionRejected(
682 transactionConnectorId
,
686 return this.notifyRemoteStartTransactionRejected(
688 transactionConnectorId
,
692 return this.notifyRemoteStartTransactionRejected(
694 transactionConnectorId
,
698 // No authorization check required, start transaction
700 this.setRemoteStartTransactionChargingProfile(
702 transactionConnectorId
,
703 commandPayload
.chargingProfile
706 connectorStatus
.transactionRemoteStarted
= true;
709 await chargingStation
.ocppRequestService
.requestHandler
<
710 OCPP16StartTransactionRequest
,
711 OCPP16StartTransactionResponse
712 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
713 connectorId
: transactionConnectorId
,
714 idTag
: commandPayload
.idTag
,
716 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
719 chargingStation
.logPrefix() +
720 ' Transaction remotely STARTED on ' +
721 chargingStation
.stationInfo
.chargingStationId
+
723 transactionConnectorId
.toString() +
727 return Constants
.OCPP_RESPONSE_ACCEPTED
;
729 return this.notifyRemoteStartTransactionRejected(
731 transactionConnectorId
,
735 return this.notifyRemoteStartTransactionRejected(
737 transactionConnectorId
,
741 return this.notifyRemoteStartTransactionRejected(
743 transactionConnectorId
,
747 return this.notifyRemoteStartTransactionRejected(
749 transactionConnectorId
,
754 private async notifyRemoteStartTransactionRejected(
755 chargingStation
: ChargingStation
,
758 ): Promise
<DefaultResponse
> {
760 chargingStation
.getConnectorStatus(connectorId
).status !== OCPP16ChargePointStatus
.AVAILABLE
762 await chargingStation
.ocppRequestService
.requestHandler
<
763 OCPP16StatusNotificationRequest
,
764 OCPP16StatusNotificationResponse
765 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
767 status: OCPP16ChargePointStatus
.AVAILABLE
,
768 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
770 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
773 chargingStation
.logPrefix() +
774 ' Remote starting transaction REJECTED on connector Id ' +
775 connectorId
.toString() +
779 chargingStation
.getConnectorStatus(connectorId
).availability
+
781 chargingStation
.getConnectorStatus(connectorId
).status
783 return Constants
.OCPP_RESPONSE_REJECTED
;
786 private setRemoteStartTransactionChargingProfile(
787 chargingStation
: ChargingStation
,
789 cp
: OCPP16ChargingProfile
791 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
792 chargingStation
.setChargingProfile(connectorId
, cp
);
794 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`,
795 chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
798 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
800 `${chargingStation.logPrefix()} Not allowed to set ${
801 cp.chargingProfilePurpose
802 } charging profile(s) at remote start transaction`
810 private async handleRequestRemoteStopTransaction(
811 chargingStation
: ChargingStation
,
812 commandPayload
: RemoteStopTransactionRequest
813 ): Promise
<DefaultResponse
> {
814 const transactionId
= commandPayload
.transactionId
;
815 for (const connectorId
of chargingStation
.connectors
.keys()) {
818 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
820 await chargingStation
.ocppRequestService
.requestHandler
<
821 OCPP16StatusNotificationRequest
,
822 OCPP16StatusNotificationResponse
823 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
825 status: OCPP16ChargePointStatus
.FINISHING
,
826 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
828 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.FINISHING
;
830 chargingStation
.getBeginEndMeterValues() &&
831 chargingStation
.getOcppStrictCompliance() &&
832 !chargingStation
.getOutOfOrderEndMeterValues()
834 // FIXME: Implement OCPP version agnostic helpers
835 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
838 chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
)
840 await chargingStation
.ocppRequestService
.requestHandler
<
841 OCPP16MeterValuesRequest
,
842 OCPP16MeterValuesResponse
843 >(chargingStation
, OCPP16RequestCommand
.METER_VALUES
, {
846 meterValue
: [transactionEndMeterValue
],
849 await chargingStation
.ocppRequestService
.requestHandler
<
850 OCPP16StopTransactionRequest
,
851 OCPP16StopTransactionResponse
852 >(chargingStation
, OCPP16RequestCommand
.STOP_TRANSACTION
, {
854 meterStop
: chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
855 idTag
: chargingStation
.getTransactionIdTag(transactionId
),
857 return Constants
.OCPP_RESPONSE_ACCEPTED
;
861 chargingStation
.logPrefix() +
862 ' Trying to remote stop a non existing transaction ' +
863 transactionId
.toString()
865 return Constants
.OCPP_RESPONSE_REJECTED
;
868 private async handleRequestGetDiagnostics(
869 chargingStation
: ChargingStation
,
870 commandPayload
: GetDiagnosticsRequest
871 ): Promise
<GetDiagnosticsResponse
> {
873 !OCPP16ServiceUtils
.checkFeatureProfile(
875 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
876 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
879 return Constants
.OCPP_RESPONSE_EMPTY
;
882 chargingStation
.logPrefix() +
884 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
885 ' request received: %j',
888 const uri
= new URL(commandPayload
.location
);
889 if (uri
.protocol
.startsWith('ftp:')) {
890 let ftpClient
: Client
;
893 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'))
894 .filter((file
) => file
.endsWith('.log'))
895 .map((file
) => path
.join('./', file
));
896 const diagnosticsArchive
= chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
897 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
898 ftpClient
= new Client();
899 const accessResponse
= await ftpClient
.access({
901 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
902 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
903 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
905 let uploadResponse
: FTPResponse
;
906 if (accessResponse
.code
=== 220) {
907 // eslint-disable-next-line @typescript-eslint/no-misused-promises
908 ftpClient
.trackProgress(async (info
) => {
910 `${chargingStation.logPrefix()} ${
912 } bytes transferred from diagnostics archive ${info.name}`
914 await chargingStation
.ocppRequestService
.requestHandler
<
915 DiagnosticsStatusNotificationRequest
,
916 DiagnosticsStatusNotificationResponse
917 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
918 status: OCPP16DiagnosticsStatus
.Uploading
,
921 uploadResponse
= await ftpClient
.uploadFrom(
923 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'),
926 uri
.pathname
+ diagnosticsArchive
928 if (uploadResponse
.code
=== 226) {
929 await chargingStation
.ocppRequestService
.requestHandler
<
930 DiagnosticsStatusNotificationRequest
,
931 DiagnosticsStatusNotificationResponse
932 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
933 status: OCPP16DiagnosticsStatus
.Uploaded
,
938 return { fileName
: diagnosticsArchive
};
941 ErrorType
.GENERIC_ERROR
,
942 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
943 uploadResponse?.code && '|' + uploadResponse?.code.toString()
945 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
949 ErrorType
.GENERIC_ERROR
,
950 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
951 uploadResponse?.code && '|' + uploadResponse?.code.toString()
953 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
956 await chargingStation
.ocppRequestService
.requestHandler
<
957 DiagnosticsStatusNotificationRequest
,
958 DiagnosticsStatusNotificationResponse
959 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
960 status: OCPP16DiagnosticsStatus
.UploadFailed
,
965 return this.handleIncomingRequestError(
967 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
969 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
974 `${chargingStation.logPrefix()} Unsupported protocol ${
976 } to transfer the diagnostic logs archive`
978 await chargingStation
.ocppRequestService
.requestHandler
<
979 DiagnosticsStatusNotificationRequest
,
980 DiagnosticsStatusNotificationResponse
981 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
982 status: OCPP16DiagnosticsStatus
.UploadFailed
,
984 return Constants
.OCPP_RESPONSE_EMPTY
;
988 private handleRequestTriggerMessage(
989 chargingStation
: ChargingStation
,
990 commandPayload
: OCPP16TriggerMessageRequest
991 ): OCPP16TriggerMessageResponse
{
993 !OCPP16ServiceUtils
.checkFeatureProfile(
995 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
996 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
999 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1001 // TODO: factor out the check on connector id
1002 if (commandPayload
?.connectorId
< 0) {
1004 `${chargingStation.logPrefix()} ${
1005 OCPP16IncomingRequestCommand.TRIGGER_MESSAGE
1006 } incoming request received with invalid connectorId ${commandPayload.connectorId}`
1008 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1011 switch (commandPayload
.requestedMessage
) {
1012 case MessageTrigger
.BootNotification
:
1014 chargingStation
.ocppRequestService
1015 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1017 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1019 chargePointModel
: chargingStation
.getBootNotificationRequest().chargePointModel
,
1020 chargePointVendor
: chargingStation
.getBootNotificationRequest().chargePointVendor
,
1021 chargeBoxSerialNumber
:
1022 chargingStation
.getBootNotificationRequest().chargeBoxSerialNumber
,
1023 firmwareVersion
: chargingStation
.getBootNotificationRequest().firmwareVersion
,
1024 chargePointSerialNumber
:
1025 chargingStation
.getBootNotificationRequest().chargePointSerialNumber
,
1026 iccid
: chargingStation
.getBootNotificationRequest().iccid
,
1027 imsi
: chargingStation
.getBootNotificationRequest().imsi
,
1028 meterSerialNumber
: chargingStation
.getBootNotificationRequest().meterSerialNumber
,
1029 meterType
: chargingStation
.getBootNotificationRequest().meterType
,
1031 { skipBufferingOnError
: true, triggerMessage
: true }
1034 chargingStation
.bootNotificationResponse
= value
;
1037 /* This is intentional */
1039 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1040 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1041 case MessageTrigger
.Heartbeat
:
1043 chargingStation
.ocppRequestService
1044 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1046 OCPP16RequestCommand
.HEARTBEAT
,
1049 triggerMessage
: true,
1053 /* This is intentional */
1055 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1056 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1057 case MessageTrigger
.StatusNotification
:
1059 if (commandPayload
?.connectorId
) {
1060 chargingStation
.ocppRequestService
1061 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1063 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1065 connectorId
: commandPayload
.connectorId
,
1066 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1067 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
).status,
1070 triggerMessage
: true,
1074 /* This is intentional */
1077 for (const connectorId
of chargingStation
.connectors
.keys()) {
1078 chargingStation
.ocppRequestService
1080 OCPP16StatusNotificationRequest
,
1081 OCPP16StatusNotificationResponse
1084 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1087 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1088 status: chargingStation
.getConnectorStatus(connectorId
).status,
1091 triggerMessage
: true,
1095 /* This is intentional */
1099 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1100 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1102 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1105 return this.handleIncomingRequestError(
1107 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1109 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}