1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 import path from
'path';
5 import { URL
, fileURLToPath
} from
'url';
7 import type { JSONSchemaType
} from
'ajv';
8 import { Client
, FTPResponse
} from
'basic-ftp';
11 import OCPPError from
'../../../exception/OCPPError';
12 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
13 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
14 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
16 ChargingProfilePurposeType
,
17 OCPP16ChargingProfile
,
18 } from
'../../../types/ocpp/1.6/ChargingProfile';
20 OCPP16StandardParametersKey
,
21 OCPP16SupportedFeatureProfiles
,
22 } from
'../../../types/ocpp/1.6/Configuration';
23 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
25 ChangeAvailabilityRequest
,
26 ChangeConfigurationRequest
,
27 ClearChargingProfileRequest
,
28 DiagnosticsStatusNotificationRequest
,
29 GetConfigurationRequest
,
30 GetDiagnosticsRequest
,
32 OCPP16AvailabilityType
,
33 OCPP16BootNotificationRequest
,
34 OCPP16ClearCacheRequest
,
35 OCPP16HeartbeatRequest
,
36 OCPP16IncomingRequestCommand
,
38 OCPP16StatusNotificationRequest
,
39 OCPP16TriggerMessageRequest
,
40 RemoteStartTransactionRequest
,
41 RemoteStopTransactionRequest
,
43 SetChargingProfileRequest
,
44 UnlockConnectorRequest
,
45 } from
'../../../types/ocpp/1.6/Requests';
47 ChangeAvailabilityResponse
,
48 ChangeConfigurationResponse
,
49 ClearChargingProfileResponse
,
50 DiagnosticsStatusNotificationResponse
,
51 GetConfigurationResponse
,
52 GetDiagnosticsResponse
,
53 OCPP16BootNotificationResponse
,
54 OCPP16HeartbeatResponse
,
55 OCPP16StatusNotificationResponse
,
56 OCPP16TriggerMessageResponse
,
57 SetChargingProfileResponse
,
58 UnlockConnectorResponse
,
59 } from
'../../../types/ocpp/1.6/Responses';
61 OCPP16AuthorizationStatus
,
62 OCPP16AuthorizeRequest
,
63 OCPP16AuthorizeResponse
,
64 OCPP16StartTransactionRequest
,
65 OCPP16StartTransactionResponse
,
66 OCPP16StopTransactionReason
,
67 } from
'../../../types/ocpp/1.6/Transaction';
68 import type { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
69 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
70 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
71 import type { DefaultResponse
} from
'../../../types/ocpp/Responses';
72 import Constants from
'../../../utils/Constants';
73 import logger from
'../../../utils/Logger';
74 import Utils from
'../../../utils/Utils';
75 import type ChargingStation from
'../../ChargingStation';
76 import { ChargingStationConfigurationUtils
} from
'../../ChargingStationConfigurationUtils';
77 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
78 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
79 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
81 const moduleName
= 'OCPP16IncomingRequestService';
83 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
84 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
85 private jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
87 public constructor() {
88 if (new.target
?.name
=== moduleName
) {
89 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
92 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
93 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
94 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
95 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
97 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
98 this.handleRequestGetConfiguration
.bind(this),
101 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
102 this.handleRequestChangeConfiguration
.bind(this),
105 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
106 this.handleRequestSetChargingProfile
.bind(this),
109 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
110 this.handleRequestClearChargingProfile
.bind(this),
113 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
114 this.handleRequestChangeAvailability
.bind(this),
117 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
118 this.handleRequestRemoteStartTransaction
.bind(this),
121 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
122 this.handleRequestRemoteStopTransaction
.bind(this),
124 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
125 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
127 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
129 OCPP16IncomingRequestCommand
.RESET
,
133 path
.dirname(fileURLToPath(import.meta
.url
)),
134 '../../../assets/json-schemas/ocpp/1.6/Reset.json'
138 ) as JSONSchemaType
<ResetRequest
>,
141 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
145 path
.dirname(fileURLToPath(import.meta
.url
)),
146 '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
150 ) as JSONSchemaType
<OCPP16ClearCacheRequest
>,
153 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
157 path
.dirname(fileURLToPath(import.meta
.url
)),
158 '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
162 ) as JSONSchemaType
<UnlockConnectorRequest
>,
165 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
169 path
.dirname(fileURLToPath(import.meta
.url
)),
170 '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
174 ) as JSONSchemaType
<GetConfigurationRequest
>,
177 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
181 path
.dirname(fileURLToPath(import.meta
.url
)),
182 '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
186 ) as JSONSchemaType
<ChangeConfigurationRequest
>,
189 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
193 path
.dirname(fileURLToPath(import.meta
.url
)),
194 '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
198 ) as JSONSchemaType
<GetDiagnosticsRequest
>,
201 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
205 path
.dirname(fileURLToPath(import.meta
.url
)),
206 '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
210 ) as JSONSchemaType
<SetChargingProfileRequest
>,
213 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
217 path
.dirname(fileURLToPath(import.meta
.url
)),
218 '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
222 ) as JSONSchemaType
<ClearChargingProfileRequest
>,
225 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
229 path
.dirname(fileURLToPath(import.meta
.url
)),
230 '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
234 ) as JSONSchemaType
<ChangeAvailabilityRequest
>,
237 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
241 path
.dirname(fileURLToPath(import.meta
.url
)),
242 '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
246 ) as JSONSchemaType
<RemoteStartTransactionRequest
>,
249 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
253 path
.dirname(fileURLToPath(import.meta
.url
)),
254 '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
258 ) as JSONSchemaType
<RemoteStopTransactionRequest
>,
261 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
265 path
.dirname(fileURLToPath(import.meta
.url
)),
266 '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
270 ) as JSONSchemaType
<OCPP16TriggerMessageRequest
>,
273 this.validatePayload
.bind(this);
276 public async incomingRequestHandler(
277 chargingStation
: ChargingStation
,
279 commandName
: OCPP16IncomingRequestCommand
,
280 commandPayload
: JsonType
282 let response
: JsonType
;
284 chargingStation
.getOcppStrictCompliance() &&
285 chargingStation
.isInPendingState() &&
286 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
287 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
290 ErrorType
.SECURITY_ERROR
,
291 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
295 )} while the charging station is in pending state on the central server`,
301 chargingStation
.isRegistered() ||
302 (!chargingStation
.getOcppStrictCompliance() && chargingStation
.isInUnknownState())
305 this.incomingRequestHandlers
.has(commandName
) &&
306 ChargingStationUtils
.isIncomingRequestCommandSupported(commandName
, chargingStation
)
309 this.validatePayload(chargingStation
, commandName
, commandPayload
);
310 // Call the method to build the response
311 response
= await this.incomingRequestHandlers
.get(commandName
)(
318 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
326 ErrorType
.NOT_IMPLEMENTED
,
327 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
338 ErrorType
.SECURITY_ERROR
,
339 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
343 )} while the charging station is not registered on the central server.`,
348 // Send the built response
349 await chargingStation
.ocppRequestService
.sendResponse(
357 private validatePayload(
358 chargingStation
: ChargingStation
,
359 commandName
: OCPP16IncomingRequestCommand
,
360 commandPayload
: JsonType
362 if (this.jsonSchemas
.has(commandName
)) {
363 return this.validateIncomingRequestPayload(
366 this.jsonSchemas
.get(commandName
),
371 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
376 // Simulate charging station restart
377 private handleRequestReset(
378 chargingStation
: ChargingStation
,
379 commandPayload
: ResetRequest
381 // eslint-disable-next-line @typescript-eslint/no-misused-promises
382 setImmediate(async (): Promise
<void> => {
383 if (chargingStation
.getNumberOfRunningTransactions() > 0) {
384 await chargingStation
.stopRunningTransactions(
385 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
388 await chargingStation
.reset();
391 `${chargingStation.logPrefix()} ${
393 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
394 chargingStation.stationInfo.resetTime
397 return Constants
.OCPP_RESPONSE_ACCEPTED
;
400 private handleRequestClearCache(): DefaultResponse
{
401 return Constants
.OCPP_RESPONSE_ACCEPTED
;
404 private async handleRequestUnlockConnector(
405 chargingStation
: ChargingStation
,
406 commandPayload
: UnlockConnectorRequest
407 ): Promise
<UnlockConnectorResponse
> {
408 const connectorId
= commandPayload
.connectorId
;
409 if (connectorId
=== 0) {
411 chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
413 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
415 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
416 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
418 OCPP16StopTransactionReason
.UNLOCK_COMMAND
420 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
421 return Constants
.OCPP_RESPONSE_UNLOCKED
;
423 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
425 await chargingStation
.ocppRequestService
.requestHandler
<
426 OCPP16StatusNotificationRequest
,
427 OCPP16StatusNotificationResponse
428 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
430 status: OCPP16ChargePointStatus
.AVAILABLE
,
431 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
433 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
434 return Constants
.OCPP_RESPONSE_UNLOCKED
;
437 private handleRequestGetConfiguration(
438 chargingStation
: ChargingStation
,
439 commandPayload
: GetConfigurationRequest
440 ): GetConfigurationResponse
{
441 const configurationKey
: OCPPConfigurationKey
[] = [];
442 const unknownKey
: string[] = [];
443 if (Utils
.isEmptyArray(commandPayload
.key
)) {
444 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
445 if (Utils
.isUndefined(configuration
.visible
)) {
446 configuration
.visible
= true;
448 if (!configuration
.visible
) {
451 configurationKey
.push({
452 key
: configuration
.key
,
453 readonly: configuration
.readonly,
454 value
: configuration
.value
,
458 for (const key
of commandPayload
.key
) {
459 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
464 if (Utils
.isUndefined(keyFound
.visible
)) {
465 keyFound
.visible
= true;
467 if (!keyFound
.visible
) {
470 configurationKey
.push({
472 readonly: keyFound
.readonly,
473 value
: keyFound
.value
,
476 unknownKey
.push(key
);
486 private handleRequestChangeConfiguration(
487 chargingStation
: ChargingStation
,
488 commandPayload
: ChangeConfigurationRequest
489 ): ChangeConfigurationResponse
{
490 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
496 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
497 } else if (keyToChange
&& keyToChange
.readonly) {
498 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
499 } else if (keyToChange
&& !keyToChange
.readonly) {
500 let valueChanged
= false;
501 if (keyToChange
.value
!== commandPayload
.value
) {
502 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
505 commandPayload
.value
,
510 let triggerHeartbeatRestart
= false;
511 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
512 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
514 OCPP16StandardParametersKey
.HeartbeatInterval
,
517 triggerHeartbeatRestart
= true;
519 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
520 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
522 OCPP16StandardParametersKey
.HeartBeatInterval
,
525 triggerHeartbeatRestart
= true;
527 if (triggerHeartbeatRestart
) {
528 chargingStation
.restartHeartbeat();
530 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
531 chargingStation
.restartWebSocketPing();
533 if (keyToChange
.reboot
) {
534 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
536 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
540 private handleRequestSetChargingProfile(
541 chargingStation
: ChargingStation
,
542 commandPayload
: SetChargingProfileRequest
543 ): SetChargingProfileResponse
{
545 !OCPP16ServiceUtils
.checkFeatureProfile(
547 OCPP16SupportedFeatureProfiles
.SmartCharging
,
548 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
551 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
553 if (!chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
555 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
556 commandPayload.connectorId
559 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
562 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
563 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
564 commandPayload
.connectorId
!== 0
566 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
569 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
570 ChargingProfilePurposeType
.TX_PROFILE
&&
571 (commandPayload
.connectorId
=== 0 ||
572 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
575 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
577 chargingStation
.setChargingProfile(
578 commandPayload
.connectorId
,
579 commandPayload
.csChargingProfiles
582 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
583 commandPayload.connectorId
584 }, dump their stack: %j`,
585 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
587 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
590 private handleRequestClearChargingProfile(
591 chargingStation
: ChargingStation
,
592 commandPayload
: ClearChargingProfileRequest
593 ): ClearChargingProfileResponse
{
595 !OCPP16ServiceUtils
.checkFeatureProfile(
597 OCPP16SupportedFeatureProfiles
.SmartCharging
,
598 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
601 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
603 const connectorStatus
= chargingStation
.getConnectorStatus(commandPayload
.connectorId
);
604 if (!connectorStatus
) {
606 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
607 commandPayload.connectorId
610 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
612 if (commandPayload
.connectorId
&& !Utils
.isEmptyArray(connectorStatus
.chargingProfiles
)) {
613 connectorStatus
.chargingProfiles
= [];
615 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
616 commandPayload.connectorId
617 }, dump their stack: %j`,
618 connectorStatus
.chargingProfiles
620 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
622 if (!commandPayload
.connectorId
) {
623 let clearedCP
= false;
624 for (const connectorId
of chargingStation
.connectors
.keys()) {
625 if (!Utils
.isEmptyArray(chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)) {
627 .getConnectorStatus(connectorId
)
628 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
629 let clearCurrentCP
= false;
630 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
631 clearCurrentCP
= true;
634 !commandPayload
.chargingProfilePurpose
&&
635 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
637 clearCurrentCP
= true;
640 !chargingProfile
.stackLevel
&&
641 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
643 clearCurrentCP
= true;
646 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
647 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
649 clearCurrentCP
= true;
651 if (clearCurrentCP
) {
652 connectorStatus
.chargingProfiles
.splice(index
, 1);
654 `${chargingStation.logPrefix()} Matching charging profile(s) cleared on connector id ${
655 commandPayload.connectorId
656 }, dump their stack: %j`,
657 connectorStatus
.chargingProfiles
665 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
668 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
671 private async handleRequestChangeAvailability(
672 chargingStation
: ChargingStation
,
673 commandPayload
: ChangeAvailabilityRequest
674 ): Promise
<ChangeAvailabilityResponse
> {
675 const connectorId
: number = commandPayload
.connectorId
;
676 if (!chargingStation
.getConnectorStatus(connectorId
)) {
678 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
680 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
682 const chargePointStatus
: OCPP16ChargePointStatus
=
683 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
684 ? OCPP16ChargePointStatus
.AVAILABLE
685 : OCPP16ChargePointStatus
.UNAVAILABLE
;
686 if (connectorId
=== 0) {
687 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
688 for (const id
of chargingStation
.connectors
.keys()) {
689 if (chargingStation
.getConnectorStatus(id
)?.transactionStarted
=== true) {
690 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
692 chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
693 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
694 await chargingStation
.ocppRequestService
.requestHandler
<
695 OCPP16StatusNotificationRequest
,
696 OCPP16StatusNotificationResponse
697 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
699 status: chargePointStatus
,
700 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
702 chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
708 (chargingStation
.getConnectorStatus(0).availability
=== OCPP16AvailabilityType
.OPERATIVE
||
709 (chargingStation
.getConnectorStatus(0).availability
===
710 OCPP16AvailabilityType
.INOPERATIVE
&&
711 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
713 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
714 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
715 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
717 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
718 await chargingStation
.ocppRequestService
.requestHandler
<
719 OCPP16StatusNotificationRequest
,
720 OCPP16StatusNotificationResponse
721 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
723 status: chargePointStatus
,
724 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
726 chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
727 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
729 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
732 private async handleRequestRemoteStartTransaction(
733 chargingStation
: ChargingStation
,
734 commandPayload
: RemoteStartTransactionRequest
735 ): Promise
<DefaultResponse
> {
736 const transactionConnectorId
= commandPayload
.connectorId
;
737 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
738 if (transactionConnectorId
) {
739 const remoteStartTransactionLogMsg
=
740 chargingStation
.logPrefix() +
741 ' Transaction remotely STARTED on ' +
742 chargingStation
.stationInfo
.chargingStationId
+
744 transactionConnectorId
.toString() +
746 commandPayload
.idTag
+
748 await chargingStation
.ocppRequestService
.requestHandler
<
749 OCPP16StatusNotificationRequest
,
750 OCPP16StatusNotificationResponse
751 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
752 connectorId
: transactionConnectorId
,
753 status: OCPP16ChargePointStatus
.PREPARING
,
754 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
756 connectorStatus
.status = OCPP16ChargePointStatus
.PREPARING
;
757 if (chargingStation
.isChargingStationAvailable() && connectorStatus
) {
758 // Check if authorized
759 if (chargingStation
.getAuthorizeRemoteTxRequests()) {
760 let authorized
= false;
762 chargingStation
.getLocalAuthListEnabled() &&
763 chargingStation
.hasAuthorizedTags() &&
764 chargingStation
.authorizedTagsCache
766 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
768 .find((value
) => value
=== commandPayload
.idTag
)
770 connectorStatus
.localAuthorizeIdTag
= commandPayload
.idTag
;
771 connectorStatus
.idTagLocalAuthorized
= true;
773 } else if (chargingStation
.getMustAuthorizeAtRemoteStart()) {
774 connectorStatus
.authorizeIdTag
= commandPayload
.idTag
;
775 const authorizeResponse
: OCPP16AuthorizeResponse
=
776 await chargingStation
.ocppRequestService
.requestHandler
<
777 OCPP16AuthorizeRequest
,
778 OCPP16AuthorizeResponse
779 >(chargingStation
, OCPP16RequestCommand
.AUTHORIZE
, {
780 idTag
: commandPayload
.idTag
,
782 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
787 `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
791 // Authorization successful, start transaction
793 this.setRemoteStartTransactionChargingProfile(
795 transactionConnectorId
,
796 commandPayload
.chargingProfile
799 connectorStatus
.transactionRemoteStarted
= true;
802 await chargingStation
.ocppRequestService
.requestHandler
<
803 OCPP16StartTransactionRequest
,
804 OCPP16StartTransactionResponse
805 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
806 connectorId
: transactionConnectorId
,
807 idTag
: commandPayload
.idTag
,
809 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
811 logger
.debug(remoteStartTransactionLogMsg
);
812 return Constants
.OCPP_RESPONSE_ACCEPTED
;
814 return this.notifyRemoteStartTransactionRejected(
816 transactionConnectorId
,
820 return this.notifyRemoteStartTransactionRejected(
822 transactionConnectorId
,
826 return this.notifyRemoteStartTransactionRejected(
828 transactionConnectorId
,
832 // No authorization check required, start transaction
834 this.setRemoteStartTransactionChargingProfile(
836 transactionConnectorId
,
837 commandPayload
.chargingProfile
840 connectorStatus
.transactionRemoteStarted
= true;
843 await chargingStation
.ocppRequestService
.requestHandler
<
844 OCPP16StartTransactionRequest
,
845 OCPP16StartTransactionResponse
846 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
847 connectorId
: transactionConnectorId
,
848 idTag
: commandPayload
.idTag
,
850 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
852 logger
.debug(remoteStartTransactionLogMsg
);
853 return Constants
.OCPP_RESPONSE_ACCEPTED
;
855 return this.notifyRemoteStartTransactionRejected(
857 transactionConnectorId
,
861 return this.notifyRemoteStartTransactionRejected(
863 transactionConnectorId
,
867 return this.notifyRemoteStartTransactionRejected(
869 transactionConnectorId
,
873 return this.notifyRemoteStartTransactionRejected(
875 transactionConnectorId
,
880 private async notifyRemoteStartTransactionRejected(
881 chargingStation
: ChargingStation
,
884 ): Promise
<DefaultResponse
> {
886 chargingStation
.getConnectorStatus(connectorId
).status !== OCPP16ChargePointStatus
.AVAILABLE
888 await chargingStation
.ocppRequestService
.requestHandler
<
889 OCPP16StatusNotificationRequest
,
890 OCPP16StatusNotificationResponse
891 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
893 status: OCPP16ChargePointStatus
.AVAILABLE
,
894 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
896 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
899 chargingStation
.logPrefix() +
900 ' Remote starting transaction REJECTED on connector Id ' +
901 connectorId
.toString() +
904 "', availability '" +
905 chargingStation
.getConnectorStatus(connectorId
).availability
+
907 chargingStation
.getConnectorStatus(connectorId
).status +
910 return Constants
.OCPP_RESPONSE_REJECTED
;
913 private setRemoteStartTransactionChargingProfile(
914 chargingStation
: ChargingStation
,
916 cp
: OCPP16ChargingProfile
918 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
919 chargingStation
.setChargingProfile(connectorId
, cp
);
921 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`,
922 chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
925 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
927 `${chargingStation.logPrefix()} Not allowed to set ${
928 cp.chargingProfilePurpose
929 } charging profile(s) at remote start transaction`
937 private async handleRequestRemoteStopTransaction(
938 chargingStation
: ChargingStation
,
939 commandPayload
: RemoteStopTransactionRequest
940 ): Promise
<DefaultResponse
> {
941 const transactionId
= commandPayload
.transactionId
;
942 for (const connectorId
of chargingStation
.connectors
.keys()) {
945 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
947 await chargingStation
.ocppRequestService
.requestHandler
<
948 OCPP16StatusNotificationRequest
,
949 OCPP16StatusNotificationResponse
950 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
952 status: OCPP16ChargePointStatus
.FINISHING
,
953 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
955 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.FINISHING
;
956 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
958 OCPP16StopTransactionReason
.REMOTE
960 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
961 return Constants
.OCPP_RESPONSE_ACCEPTED
;
963 return Constants
.OCPP_RESPONSE_REJECTED
;
967 chargingStation
.logPrefix() +
968 ' Trying to remote stop a non existing transaction ' +
969 transactionId
.toString()
971 return Constants
.OCPP_RESPONSE_REJECTED
;
974 private async handleRequestGetDiagnostics(
975 chargingStation
: ChargingStation
,
976 commandPayload
: GetDiagnosticsRequest
977 ): Promise
<GetDiagnosticsResponse
> {
979 !OCPP16ServiceUtils
.checkFeatureProfile(
981 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
982 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
985 return Constants
.OCPP_RESPONSE_EMPTY
;
988 chargingStation
.logPrefix() +
990 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
991 ' request received: %j',
994 const uri
= new URL(commandPayload
.location
);
995 if (uri
.protocol
.startsWith('ftp:')) {
996 let ftpClient
: Client
;
999 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'))
1000 .filter((file
) => file
.endsWith('.log'))
1001 .map((file
) => path
.join('./', file
));
1002 const diagnosticsArchive
= chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
1003 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
1004 ftpClient
= new Client();
1005 const accessResponse
= await ftpClient
.access({
1007 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1008 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
1009 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
1011 let uploadResponse
: FTPResponse
;
1012 if (accessResponse
.code
=== 220) {
1013 // eslint-disable-next-line @typescript-eslint/no-misused-promises
1014 ftpClient
.trackProgress(async (info
) => {
1016 `${chargingStation.logPrefix()} ${
1018 } bytes transferred from diagnostics archive ${info.name}`
1020 await chargingStation
.ocppRequestService
.requestHandler
<
1021 DiagnosticsStatusNotificationRequest
,
1022 DiagnosticsStatusNotificationResponse
1023 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1024 status: OCPP16DiagnosticsStatus
.Uploading
,
1027 uploadResponse
= await ftpClient
.uploadFrom(
1029 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'),
1032 uri
.pathname
+ diagnosticsArchive
1034 if (uploadResponse
.code
=== 226) {
1035 await chargingStation
.ocppRequestService
.requestHandler
<
1036 DiagnosticsStatusNotificationRequest
,
1037 DiagnosticsStatusNotificationResponse
1038 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1039 status: OCPP16DiagnosticsStatus
.Uploaded
,
1044 return { fileName
: diagnosticsArchive
};
1046 throw new OCPPError(
1047 ErrorType
.GENERIC_ERROR
,
1048 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1049 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1051 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1054 throw new OCPPError(
1055 ErrorType
.GENERIC_ERROR
,
1056 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1057 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1059 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1062 await chargingStation
.ocppRequestService
.requestHandler
<
1063 DiagnosticsStatusNotificationRequest
,
1064 DiagnosticsStatusNotificationResponse
1065 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1066 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1071 return this.handleIncomingRequestError(
1073 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1075 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
1080 `${chargingStation.logPrefix()} Unsupported protocol ${
1082 } to transfer the diagnostic logs archive`
1084 await chargingStation
.ocppRequestService
.requestHandler
<
1085 DiagnosticsStatusNotificationRequest
,
1086 DiagnosticsStatusNotificationResponse
1087 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1088 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1090 return Constants
.OCPP_RESPONSE_EMPTY
;
1094 private handleRequestTriggerMessage(
1095 chargingStation
: ChargingStation
,
1096 commandPayload
: OCPP16TriggerMessageRequest
1097 ): OCPP16TriggerMessageResponse
{
1099 !OCPP16ServiceUtils
.checkFeatureProfile(
1101 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1102 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1105 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1107 // TODO: factor out the check on connector id
1108 if (commandPayload
?.connectorId
< 0) {
1110 `${chargingStation.logPrefix()} ${
1111 OCPP16IncomingRequestCommand.TRIGGER_MESSAGE
1112 } incoming request received with invalid connectorId ${commandPayload.connectorId}`
1114 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1117 switch (commandPayload
.requestedMessage
) {
1118 case MessageTrigger
.BootNotification
:
1120 chargingStation
.ocppRequestService
1121 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1123 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1125 chargePointModel
: chargingStation
.bootNotificationRequest
.chargePointModel
,
1126 chargePointVendor
: chargingStation
.bootNotificationRequest
.chargePointVendor
,
1127 chargeBoxSerialNumber
:
1128 chargingStation
.bootNotificationRequest
.chargeBoxSerialNumber
,
1129 firmwareVersion
: chargingStation
.bootNotificationRequest
.firmwareVersion
,
1130 chargePointSerialNumber
:
1131 chargingStation
.bootNotificationRequest
.chargePointSerialNumber
,
1132 iccid
: chargingStation
.bootNotificationRequest
.iccid
,
1133 imsi
: chargingStation
.bootNotificationRequest
.imsi
,
1134 meterSerialNumber
: chargingStation
.bootNotificationRequest
.meterSerialNumber
,
1135 meterType
: chargingStation
.bootNotificationRequest
.meterType
,
1137 { skipBufferingOnError
: true, triggerMessage
: true }
1140 chargingStation
.bootNotificationResponse
= value
;
1143 /* This is intentional */
1145 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1146 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1147 case MessageTrigger
.Heartbeat
:
1149 chargingStation
.ocppRequestService
1150 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1152 OCPP16RequestCommand
.HEARTBEAT
,
1155 triggerMessage
: true,
1159 /* This is intentional */
1161 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1162 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1163 case MessageTrigger
.StatusNotification
:
1165 if (commandPayload
?.connectorId
) {
1166 chargingStation
.ocppRequestService
1167 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1169 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1171 connectorId
: commandPayload
.connectorId
,
1172 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1173 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
).status,
1176 triggerMessage
: true,
1180 /* This is intentional */
1183 for (const connectorId
of chargingStation
.connectors
.keys()) {
1184 chargingStation
.ocppRequestService
1186 OCPP16StatusNotificationRequest
,
1187 OCPP16StatusNotificationResponse
1190 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1193 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1194 status: chargingStation
.getConnectorStatus(connectorId
).status,
1197 triggerMessage
: true,
1201 /* This is intentional */
1205 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1206 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1208 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1211 return this.handleIncomingRequestError(
1213 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1215 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}