1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { createWriteStream
, readdirSync
} from
'node:fs'
4 import { dirname
, join
, resolve
} from
'node:path'
5 import { URL
, fileURLToPath
} from
'node:url'
7 import type { ValidateFunction
} from
'ajv'
8 import { Client
, type FTPResponse
} from
'basic-ftp'
16 import { maxTime
} from
'date-fns/constants'
17 import { create
} from
'tar'
19 import { OCPP16Constants
} from
'./OCPP16Constants.js'
20 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils.js'
23 canProceedChargingProfile
,
26 getConnectorChargingProfiles
,
27 prepareChargingProfileKind
,
28 removeExpiredReservations
,
29 setConfigurationKeyValue
30 } from
'../../../charging-station/index.js'
31 import { OCPPError
} from
'../../../exception/index.js'
33 type ChangeConfigurationRequest
,
34 type ChangeConfigurationResponse
,
38 type GetConfigurationRequest
,
39 type GetConfigurationResponse
,
40 type GetDiagnosticsRequest
,
41 type GetDiagnosticsResponse
,
42 type IncomingRequestHandler
,
44 OCPP16AuthorizationStatus
,
45 OCPP16AvailabilityType
,
46 type OCPP16BootNotificationRequest
,
47 type OCPP16BootNotificationResponse
,
48 type OCPP16CancelReservationRequest
,
49 type OCPP16ChangeAvailabilityRequest
,
50 type OCPP16ChangeAvailabilityResponse
,
51 OCPP16ChargePointErrorCode
,
52 OCPP16ChargePointStatus
,
53 type OCPP16ChargingProfile
,
54 OCPP16ChargingProfilePurposeType
,
55 type OCPP16ChargingSchedule
,
56 type OCPP16ClearCacheRequest
,
57 type OCPP16ClearChargingProfileRequest
,
58 type OCPP16ClearChargingProfileResponse
,
59 type OCPP16DataTransferRequest
,
60 type OCPP16DataTransferResponse
,
61 OCPP16DataTransferVendorId
,
62 OCPP16DiagnosticsStatus
,
63 type OCPP16DiagnosticsStatusNotificationRequest
,
64 type OCPP16DiagnosticsStatusNotificationResponse
,
66 type OCPP16FirmwareStatusNotificationRequest
,
67 type OCPP16FirmwareStatusNotificationResponse
,
68 type OCPP16GetCompositeScheduleRequest
,
69 type OCPP16GetCompositeScheduleResponse
,
70 type OCPP16HeartbeatRequest
,
71 type OCPP16HeartbeatResponse
,
72 OCPP16IncomingRequestCommand
,
75 type OCPP16ReserveNowRequest
,
76 type OCPP16ReserveNowResponse
,
77 OCPP16StandardParametersKey
,
78 type OCPP16StartTransactionRequest
,
79 type OCPP16StartTransactionResponse
,
80 type OCPP16StatusNotificationRequest
,
81 type OCPP16StatusNotificationResponse
,
82 OCPP16StopTransactionReason
,
83 OCPP16SupportedFeatureProfiles
,
84 type OCPP16TriggerMessageRequest
,
85 type OCPP16TriggerMessageResponse
,
86 type OCPP16UpdateFirmwareRequest
,
87 type OCPP16UpdateFirmwareResponse
,
88 type OCPPConfigurationKey
,
90 type RemoteStartTransactionRequest
,
91 type RemoteStopTransactionRequest
,
92 ReservationTerminationReason
,
94 type SetChargingProfileRequest
,
95 type SetChargingProfileResponse
,
96 type UnlockConnectorRequest
,
97 type UnlockConnectorResponse
98 } from
'../../../types/index.js'
103 formatDurationMilliSeconds
,
110 } from
'../../../utils/index.js'
111 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService.js'
113 const moduleName
= 'OCPP16IncomingRequestService'
115 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
116 protected payloadValidateFunctions
: Map
<OCPP16IncomingRequestCommand
, ValidateFunction
<JsonType
>>
118 private readonly incomingRequestHandlers
: Map
<
119 OCPP16IncomingRequestCommand
,
120 IncomingRequestHandler
123 public constructor () {
124 // if (new.target.name === moduleName) {
125 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
127 super(OCPPVersion
.VERSION_16
)
128 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
130 OCPP16IncomingRequestCommand
.RESET
,
131 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
134 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
135 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
138 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
139 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
142 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
143 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
146 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
147 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
150 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
151 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
154 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
155 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
158 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
159 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
162 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
163 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
166 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
167 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
170 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
171 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
174 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
175 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
178 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
179 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
182 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
183 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
186 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
187 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
190 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
191 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
194 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
195 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
198 this.payloadValidateFunctions
= new Map
<
199 OCPP16IncomingRequestCommand
,
200 ValidateFunction
<JsonType
>
203 OCPP16IncomingRequestCommand
.RESET
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
207 'assets/json-schemas/ocpp/1.6/Reset.json',
215 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
218 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
219 'assets/json-schemas/ocpp/1.6/ClearCache.json',
227 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
231 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
239 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
242 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
243 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
251 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
255 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
263 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
266 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
267 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
275 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
279 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
287 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
290 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
291 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
299 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
302 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearChargingProfileRequest
>(
303 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
311 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
314 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ChangeAvailabilityRequest
>(
315 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
323 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
326 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
327 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
335 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
338 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
339 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
347 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
350 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
351 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
359 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
362 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
363 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
371 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
374 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
375 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
383 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
386 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
387 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
395 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
398 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
399 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
408 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
410 chargingStation
: ChargingStation
,
411 request
: RemoteStartTransactionRequest
,
412 response
: GenericResponse
414 if (response
.status === GenericStatus
.Accepted
) {
415 const { connectorId
, idTag
} = request
416 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
417 chargingStation
.getConnectorStatus(connectorId
)!.transactionRemoteStarted
= true
418 chargingStation
.ocppRequestService
419 .requestHandler
<OCPP16StartTransactionRequest
, OCPP16StartTransactionResponse
>(
421 OCPP16RequestCommand
.START_TRANSACTION
,
428 if (response
.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
430 `${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
434 `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
440 `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`,
448 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
449 (chargingStation
: ChargingStation
, request
: OCPP16TriggerMessageRequest
) => {
450 const { requestedMessage
, connectorId
} = request
451 const errorHandler
= (error
: Error): void => {
453 `${chargingStation.logPrefix()} ${moduleName}.constructor: Trigger ${requestedMessage} error:`,
457 switch (requestedMessage
) {
458 case OCPP16MessageTrigger
.BootNotification
:
459 chargingStation
.ocppRequestService
460 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
462 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
463 chargingStation
.bootNotificationRequest
,
464 { skipBufferingOnError
: true, triggerMessage
: true }
467 chargingStation
.bootNotificationResponse
= response
471 case OCPP16MessageTrigger
.Heartbeat
:
472 chargingStation
.ocppRequestService
473 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
475 OCPP16RequestCommand
.HEARTBEAT
,
483 case OCPP16MessageTrigger
.StatusNotification
:
484 if (connectorId
!= null) {
485 chargingStation
.ocppRequestService
486 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
488 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
491 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
492 status: chargingStation
.getConnectorStatus(connectorId
)?.status
499 } else if (chargingStation
.hasEvses
) {
500 for (const evseStatus
of chargingStation
.evses
.values()) {
501 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
502 chargingStation
.ocppRequestService
504 OCPP16StatusNotificationRequest
,
505 OCPP16StatusNotificationResponse
508 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
511 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
512 status: connectorStatus
.status
522 for (const [id
, connectorStatus
] of chargingStation
.connectors
) {
523 chargingStation
.ocppRequestService
525 OCPP16StatusNotificationRequest
,
526 OCPP16StatusNotificationResponse
529 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
532 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
533 status: connectorStatus
.status
546 this.validatePayload
= this.validatePayload
.bind(this)
549 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
550 chargingStation
: ChargingStation
,
552 commandName
: OCPP16IncomingRequestCommand
,
553 commandPayload
: ReqType
555 let response
: ResType
557 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
558 chargingStation
.inPendingState() &&
559 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
560 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
563 ErrorType
.SECURITY_ERROR
,
564 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
568 )} while the charging station is in pending state on the central server`,
574 chargingStation
.isRegistered() ||
575 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
576 chargingStation
.inUnknownState())
579 this.incomingRequestHandlers
.has(commandName
) &&
580 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
583 this.validatePayload(chargingStation
, commandName
, commandPayload
)
584 // Call the method to build the response
585 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
586 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
590 this.emit(commandName
, chargingStation
, commandPayload
, response
)
594 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
602 ErrorType
.NOT_IMPLEMENTED
,
603 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
614 ErrorType
.SECURITY_ERROR
,
615 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
619 )} while the charging station is not registered on the central server.`,
624 // Send the built response
625 await chargingStation
.ocppRequestService
.sendResponse(
633 private validatePayload (
634 chargingStation
: ChargingStation
,
635 commandName
: OCPP16IncomingRequestCommand
,
636 commandPayload
: JsonType
638 if (this.payloadValidateFunctions
.has(commandName
)) {
639 return this.validateIncomingRequestPayload(chargingStation
, commandName
, commandPayload
)
642 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`
647 // Simulate charging station restart
648 private handleRequestReset (
649 chargingStation
: ChargingStation
,
650 commandPayload
: ResetRequest
652 const { type } = commandPayload
654 .reset(`${type}Reset` as OCPP16StopTransactionReason
)
655 .catch(Constants
.EMPTY_FUNCTION
)
657 `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be back online in ${formatDurationMilliSeconds(
658 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
659 chargingStation.stationInfo!.resetTime!
662 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
665 private async handleRequestUnlockConnector (
666 chargingStation
: ChargingStation
,
667 commandPayload
: UnlockConnectorRequest
668 ): Promise
<UnlockConnectorResponse
> {
669 const { connectorId
} = commandPayload
670 if (!chargingStation
.hasConnector(connectorId
)) {
672 `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId}`
674 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
676 if (connectorId
=== 0) {
677 logger
.error(`${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId}`)
678 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
680 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
681 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
683 OCPP16StopTransactionReason
.UNLOCK_COMMAND
685 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
686 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
688 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
690 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
693 OCPP16ChargePointStatus
.Available
695 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
698 private handleRequestGetConfiguration (
699 chargingStation
: ChargingStation
,
700 commandPayload
: GetConfigurationRequest
701 ): GetConfigurationResponse
{
702 const { key
} = commandPayload
703 const configurationKey
: OCPPConfigurationKey
[] = []
704 const unknownKey
: string[] = []
706 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
707 for (const configKey
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
708 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(configKey
)) {
711 configurationKey
.push({
713 readonly: configKey
.readonly,
714 value
: configKey
.value
717 } else if (isNotEmptyArray(key
)) {
718 for (const k
of key
) {
719 const keyFound
= getConfigurationKey(chargingStation
, k
, true)
720 if (keyFound
!= null) {
721 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(keyFound
)) {
724 configurationKey
.push({
726 readonly: keyFound
.readonly,
727 value
: keyFound
.value
740 private handleRequestChangeConfiguration (
741 chargingStation
: ChargingStation
,
742 commandPayload
: ChangeConfigurationRequest
743 ): ChangeConfigurationResponse
{
744 const { key
, value
} = commandPayload
745 const keyToChange
= getConfigurationKey(chargingStation
, key
, true)
746 if (keyToChange
?.readonly === true) {
747 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
748 } else if (keyToChange
?.readonly === false) {
749 let valueChanged
= false
750 if (keyToChange
.value
!== value
) {
751 setConfigurationKeyValue(chargingStation
, key
, value
, true)
754 let triggerHeartbeatRestart
= false
756 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
757 OCPP16StandardParametersKey
.HeartBeatInterval
&&
760 setConfigurationKeyValue(
762 OCPP16StandardParametersKey
.HeartbeatInterval
,
765 triggerHeartbeatRestart
= true
768 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
769 OCPP16StandardParametersKey
.HeartbeatInterval
&&
772 setConfigurationKeyValue(
774 OCPP16StandardParametersKey
.HeartBeatInterval
,
777 triggerHeartbeatRestart
= true
779 if (triggerHeartbeatRestart
) {
780 chargingStation
.restartHeartbeat()
783 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
784 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
787 chargingStation
.restartWebSocketPing()
789 if (keyToChange
.reboot
=== true) {
790 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
792 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
794 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
797 private handleRequestSetChargingProfile (
798 chargingStation
: ChargingStation
,
799 commandPayload
: SetChargingProfileRequest
800 ): SetChargingProfileResponse
{
802 !OCPP16ServiceUtils
.checkFeatureProfile(
804 OCPP16SupportedFeatureProfiles
.SmartCharging
,
805 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
808 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
810 const { connectorId
, csChargingProfiles
} = commandPayload
811 if (!chargingStation
.hasConnector(connectorId
)) {
813 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${connectorId}`
815 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
818 csChargingProfiles
.chargingProfilePurpose
===
819 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
822 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
825 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
829 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId}`
831 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
833 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
835 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
837 connectorStatus
?.transactionStarted
=== false
840 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} without a started transaction`
842 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
845 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
847 connectorStatus
?.transactionStarted
=== true &&
848 csChargingProfiles
.transactionId
!== connectorStatus
.transactionId
851 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} with a different transaction id ${
852 csChargingProfiles.transactionId
853 } than the started transaction id ${connectorStatus.transactionId}`
855 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
857 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
)
859 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
862 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
865 private handleRequestGetCompositeSchedule (
866 chargingStation
: ChargingStation
,
867 commandPayload
: OCPP16GetCompositeScheduleRequest
868 ): OCPP16GetCompositeScheduleResponse
{
870 !OCPP16ServiceUtils
.checkFeatureProfile(
872 OCPP16SupportedFeatureProfiles
.SmartCharging
,
873 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
876 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
878 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
879 if (!chargingStation
.hasConnector(connectorId
)) {
881 `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${connectorId}`
883 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
885 if (connectorId
=== 0) {
887 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`
889 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
891 if (chargingRateUnit
!= null) {
893 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`
896 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
898 isEmptyArray(connectorStatus
?.chargingProfiles
) &&
899 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
901 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
903 const currentDate
= new Date()
904 const compositeScheduleInterval
: Interval
= {
906 end
: addSeconds(currentDate
, duration
)
908 // Get charging profiles sorted by connector id then stack level
909 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
913 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
914 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
915 for (const chargingProfile
of chargingProfiles
) {
916 if (chargingProfile
.chargingSchedule
.startSchedule
== null) {
918 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
919 chargingProfile.chargingProfileId
920 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
922 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
923 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
925 if (!isDate(chargingProfile
.chargingSchedule
.startSchedule
)) {
927 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
928 chargingProfile.chargingProfileId
929 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
931 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
932 chargingProfile
.chargingSchedule
.startSchedule
935 if (chargingProfile
.chargingSchedule
.duration
== null) {
937 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
938 chargingProfile.chargingProfileId
939 } has no duration defined and will be set to the maximum time allowed`
941 // OCPP specifies that if duration is not defined, it should be infinite
942 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
944 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
945 chargingProfile
.chargingSchedule
.startSchedule
!
949 !prepareChargingProfileKind(
952 compositeScheduleInterval
.start
,
953 chargingStation
.logPrefix()
959 !canProceedChargingProfile(
961 compositeScheduleInterval
.start
,
962 chargingStation
.logPrefix()
967 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
968 previousCompositeSchedule
,
969 chargingProfile
.chargingSchedule
,
970 compositeScheduleInterval
972 previousCompositeSchedule
= compositeSchedule
974 if (compositeSchedule
!= null) {
976 status: GenericStatus
.Accepted
,
977 scheduleStart
: compositeSchedule
.startSchedule
,
979 chargingSchedule
: compositeSchedule
982 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
985 private handleRequestClearChargingProfile (
986 chargingStation
: ChargingStation
,
987 commandPayload
: OCPP16ClearChargingProfileRequest
988 ): OCPP16ClearChargingProfileResponse
{
990 !OCPP16ServiceUtils
.checkFeatureProfile(
992 OCPP16SupportedFeatureProfiles
.SmartCharging
,
993 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
996 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
998 const { connectorId
} = commandPayload
999 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1000 if (!chargingStation
.hasConnector(connectorId
!)) {
1002 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
1004 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
1006 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1007 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
1008 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
1009 connectorStatus
.chargingProfiles
= []
1011 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
1013 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
1015 if (connectorId
== null) {
1016 let clearedCP
= false
1017 if (chargingStation
.hasEvses
) {
1018 for (const evseStatus
of chargingStation
.evses
.values()) {
1019 for (const status of evseStatus
.connectors
.values()) {
1020 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
1023 status.chargingProfiles
1028 for (const id
of chargingStation
.connectors
.keys()) {
1029 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
1032 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
1037 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
1040 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
1043 private async handleRequestChangeAvailability (
1044 chargingStation
: ChargingStation
,
1045 commandPayload
: OCPP16ChangeAvailabilityRequest
1046 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
1047 const { connectorId
, type } = commandPayload
1048 if (!chargingStation
.hasConnector(connectorId
)) {
1050 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
1052 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
1054 const chargePointStatus
: OCPP16ChargePointStatus
=
1055 type === OCPP16AvailabilityType
.Operative
1056 ? OCPP16ChargePointStatus
.Available
1057 : OCPP16ChargePointStatus
.Unavailable
1058 if (connectorId
=== 0) {
1059 let response
: OCPP16ChangeAvailabilityResponse
| undefined
1060 if (chargingStation
.hasEvses
) {
1061 for (const evseStatus
of chargingStation
.evses
.values()) {
1062 response
= await OCPP16ServiceUtils
.changeAvailability(
1064 [...evseStatus
.connectors
.keys()],
1070 response
= await OCPP16ServiceUtils
.changeAvailability(
1072 [...chargingStation
.connectors
.keys()],
1077 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1081 (chargingStation
.isChargingStationAvailable() ||
1082 (!chargingStation
.isChargingStationAvailable() &&
1083 type === OCPP16AvailabilityType
.Inoperative
))
1085 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1086 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1087 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
1088 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
1090 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1091 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
1092 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1097 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
1099 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
1102 private async handleRequestRemoteStartTransaction (
1103 chargingStation
: ChargingStation
,
1104 commandPayload
: RemoteStartTransactionRequest
1105 ): Promise
<GenericResponse
> {
1106 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
1107 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
1108 return await this.notifyRemoteStartTransactionRejected(
1110 transactionConnectorId
,
1115 !chargingStation
.isChargingStationAvailable() ||
1116 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
1118 return await this.notifyRemoteStartTransactionRejected(
1120 transactionConnectorId
,
1124 // idTag authorization check required
1126 chargingStation
.getAuthorizeRemoteTxRequests() &&
1127 !(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
1129 return await this.notifyRemoteStartTransactionRejected(
1131 transactionConnectorId
,
1135 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1137 transactionConnectorId
,
1138 OCPP16ChargePointStatus
.Preparing
1141 chargingProfile
!= null &&
1142 !this.setRemoteStartTransactionChargingProfile(
1144 transactionConnectorId
,
1148 return await this.notifyRemoteStartTransactionRejected(
1150 transactionConnectorId
,
1154 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1157 private async notifyRemoteStartTransactionRejected (
1158 chargingStation
: ChargingStation
,
1159 connectorId
: number,
1161 ): Promise
<GenericResponse
> {
1162 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1163 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1164 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1167 OCPP16ChargePointStatus
.Available
1171 `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1173 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1176 private setRemoteStartTransactionChargingProfile (
1177 chargingStation
: ChargingStation
,
1178 connectorId
: number,
1179 chargingProfile
: OCPP16ChargingProfile
1181 if (chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1182 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1184 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1190 `${chargingStation.logPrefix()} Not allowed to set ${
1191 chargingProfile.chargingProfilePurpose
1192 } charging profile(s) at remote start transaction`
1197 private async handleRequestRemoteStopTransaction (
1198 chargingStation
: ChargingStation
,
1199 commandPayload
: RemoteStopTransactionRequest
1200 ): Promise
<GenericResponse
> {
1201 const { transactionId
} = commandPayload
1202 if (chargingStation
.hasEvses
) {
1203 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1205 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1206 if (connectorStatus
.transactionId
=== transactionId
) {
1207 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1213 for (const connectorId
of chargingStation
.connectors
.keys()) {
1216 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1218 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1223 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1225 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1228 private handleRequestUpdateFirmware (
1229 chargingStation
: ChargingStation
,
1230 commandPayload
: OCPP16UpdateFirmwareRequest
1231 ): OCPP16UpdateFirmwareResponse
{
1233 !OCPP16ServiceUtils
.checkFeatureProfile(
1235 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1236 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1240 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1242 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1244 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1245 commandPayload
.retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!
1246 const { retrieveDate
} = commandPayload
1247 if (chargingStation
.stationInfo
?.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
) {
1249 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1251 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1253 const now
= Date.now()
1254 if (retrieveDate
.getTime() <= now
) {
1255 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1258 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1259 }, retrieveDate
.getTime() - now
)
1261 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1264 private async updateFirmwareSimulation (
1265 chargingStation
: ChargingStation
,
1269 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1272 if (chargingStation
.hasEvses
) {
1273 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1275 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1276 if (connectorStatus
.transactionStarted
=== false) {
1277 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1280 OCPP16ChargePointStatus
.Unavailable
1287 for (const connectorId
of chargingStation
.connectors
.keys()) {
1290 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1292 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1295 OCPP16ChargePointStatus
.Unavailable
1300 await chargingStation
.ocppRequestService
.requestHandler
<
1301 OCPP16FirmwareStatusNotificationRequest
,
1302 OCPP16FirmwareStatusNotificationResponse
1303 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1304 status: OCPP16FirmwareStatus
.Downloading
1306 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1307 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1309 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1310 OCPP16FirmwareStatus
.DownloadFailed
1312 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1313 await chargingStation
.ocppRequestService
.requestHandler
<
1314 OCPP16FirmwareStatusNotificationRequest
,
1315 OCPP16FirmwareStatusNotificationResponse
1316 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1317 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1319 chargingStation
.stationInfo
.firmwareStatus
=
1320 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1323 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1324 await chargingStation
.ocppRequestService
.requestHandler
<
1325 OCPP16FirmwareStatusNotificationRequest
,
1326 OCPP16FirmwareStatusNotificationResponse
1327 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1328 status: OCPP16FirmwareStatus
.Downloaded
1330 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1331 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1332 let wasTransactionsStarted
= false
1333 let transactionsStarted
: boolean
1335 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1336 if (runningTransactions
> 0) {
1337 const waitTime
= secondsToMilliseconds(15)
1339 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1341 )} before continuing firmware update simulation`
1343 await sleep(waitTime
)
1344 transactionsStarted
= true
1345 wasTransactionsStarted
= true
1347 if (chargingStation
.hasEvses
) {
1348 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1350 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1351 if (connectorStatus
.status !== OCPP16ChargePointStatus
.Unavailable
) {
1352 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1355 OCPP16ChargePointStatus
.Unavailable
1362 for (const connectorId
of chargingStation
.connectors
.keys()) {
1365 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1366 OCPP16ChargePointStatus
.Unavailable
1368 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1371 OCPP16ChargePointStatus
.Unavailable
1376 transactionsStarted
= false
1378 } while (transactionsStarted
)
1379 !wasTransactionsStarted
&&
1380 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1381 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1384 await chargingStation
.ocppRequestService
.requestHandler
<
1385 OCPP16FirmwareStatusNotificationRequest
,
1386 OCPP16FirmwareStatusNotificationResponse
1387 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1388 status: OCPP16FirmwareStatus
.Installing
1390 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1391 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1393 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1394 OCPP16FirmwareStatus
.InstallationFailed
1396 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1397 await chargingStation
.ocppRequestService
.requestHandler
<
1398 OCPP16FirmwareStatusNotificationRequest
,
1399 OCPP16FirmwareStatusNotificationResponse
1400 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1401 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1403 chargingStation
.stationInfo
.firmwareStatus
=
1404 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1407 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1408 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1409 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1413 private async handleRequestGetDiagnostics (
1414 chargingStation
: ChargingStation
,
1415 commandPayload
: GetDiagnosticsRequest
1416 ): Promise
<GetDiagnosticsResponse
> {
1418 !OCPP16ServiceUtils
.checkFeatureProfile(
1420 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1421 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1425 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1427 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1429 const { location
} = commandPayload
1430 const uri
= new URL(location
)
1431 if (uri
.protocol
.startsWith('ftp:')) {
1432 let ftpClient
: Client
| undefined
1434 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1435 .filter(file
=> file
.endsWith('.log'))
1436 .map(file
=> join('./', file
))
1437 const diagnosticsArchive
= `${chargingStation.stationInfo?.chargingStationId}_logs.tar.gz`
1438 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1439 ftpClient
= new Client()
1440 const accessResponse
= await ftpClient
.access({
1442 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1443 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1444 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1446 let uploadResponse
: FTPResponse
| undefined
1447 if (accessResponse
.code
=== 220) {
1448 ftpClient
.trackProgress(info
=> {
1450 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1452 } bytes transferred from diagnostics archive ${info.name}`
1454 chargingStation
.ocppRequestService
1456 OCPP16DiagnosticsStatusNotificationRequest
,
1457 OCPP16DiagnosticsStatusNotificationResponse
1458 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1459 status: OCPP16DiagnosticsStatus
.Uploading
1463 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1464 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1470 uploadResponse
= await ftpClient
.uploadFrom(
1471 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1472 `${uri.pathname}${diagnosticsArchive}`
1474 if (uploadResponse
.code
=== 226) {
1475 await chargingStation
.ocppRequestService
.requestHandler
<
1476 OCPP16DiagnosticsStatusNotificationRequest
,
1477 OCPP16DiagnosticsStatusNotificationResponse
1478 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1479 status: OCPP16DiagnosticsStatus
.Uploaded
1482 return { fileName
: diagnosticsArchive
}
1484 throw new OCPPError(
1485 ErrorType
.GENERIC_ERROR
,
1486 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse.code}`,
1487 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1490 throw new OCPPError(
1491 ErrorType
.GENERIC_ERROR
,
1492 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse?.code}`,
1493 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1496 await chargingStation
.ocppRequestService
.requestHandler
<
1497 OCPP16DiagnosticsStatusNotificationRequest
,
1498 OCPP16DiagnosticsStatusNotificationResponse
1499 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1500 status: OCPP16DiagnosticsStatus
.UploadFailed
1503 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1504 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1506 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1508 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1513 `${chargingStation.logPrefix()} Unsupported protocol ${
1515 } to transfer the diagnostic logs archive`
1517 await chargingStation
.ocppRequestService
.requestHandler
<
1518 OCPP16DiagnosticsStatusNotificationRequest
,
1519 OCPP16DiagnosticsStatusNotificationResponse
1520 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1521 status: OCPP16DiagnosticsStatus
.UploadFailed
1523 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1527 private handleRequestTriggerMessage (
1528 chargingStation
: ChargingStation
,
1529 commandPayload
: OCPP16TriggerMessageRequest
1530 ): OCPP16TriggerMessageResponse
{
1531 const { requestedMessage
, connectorId
} = commandPayload
1533 !OCPP16ServiceUtils
.checkFeatureProfile(
1535 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1536 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1538 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1540 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1543 !OCPP16ServiceUtils
.isConnectorIdValid(
1545 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1546 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1550 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1552 switch (requestedMessage
) {
1553 case OCPP16MessageTrigger
.BootNotification
:
1554 case OCPP16MessageTrigger
.Heartbeat
:
1555 case OCPP16MessageTrigger
.StatusNotification
:
1556 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1558 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1562 private handleRequestDataTransfer (
1563 chargingStation
: ChargingStation
,
1564 commandPayload
: OCPP16DataTransferRequest
1565 ): OCPP16DataTransferResponse
{
1566 const { vendorId
} = commandPayload
1568 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1569 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1571 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1573 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1574 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1576 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1578 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1583 private async handleRequestReserveNow (
1584 chargingStation
: ChargingStation
,
1585 commandPayload
: OCPP16ReserveNowRequest
1586 ): Promise
<OCPP16ReserveNowResponse
> {
1588 !OCPP16ServiceUtils
.checkFeatureProfile(
1590 OCPP16SupportedFeatureProfiles
.Reservation
,
1591 OCPP16IncomingRequestCommand
.RESERVE_NOW
1594 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1596 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1597 commandPayload
.expiryDate
= convertToDate(commandPayload
.expiryDate
)!
1598 const { reservationId
, idTag
, connectorId
} = commandPayload
1599 let response
: OCPP16ReserveNowResponse
1601 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1602 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1604 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1605 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1607 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1608 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1610 await removeExpiredReservations(chargingStation
)
1611 switch (chargingStation
.getConnectorStatus(connectorId
)?.status) {
1612 case OCPP16ChargePointStatus
.Faulted
:
1613 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1615 case OCPP16ChargePointStatus
.Preparing
:
1616 case OCPP16ChargePointStatus
.Charging
:
1617 case OCPP16ChargePointStatus
.SuspendedEV
:
1618 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1619 case OCPP16ChargePointStatus
.Finishing
:
1620 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1622 case OCPP16ChargePointStatus
.Unavailable
:
1623 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1625 case OCPP16ChargePointStatus
.Reserved
:
1626 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1627 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1630 // eslint-disable-next-line no-fallthrough
1632 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1633 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1636 await chargingStation
.addReservation({
1637 id
: commandPayload
.reservationId
,
1640 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1645 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1646 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1647 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1648 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1650 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1652 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1657 private async handleRequestCancelReservation (
1658 chargingStation
: ChargingStation
,
1659 commandPayload
: OCPP16CancelReservationRequest
1660 ): Promise
<GenericResponse
> {
1662 !OCPP16ServiceUtils
.checkFeatureProfile(
1664 OCPP16SupportedFeatureProfiles
.Reservation
,
1665 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1668 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1671 const { reservationId
} = commandPayload
1672 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1673 if (reservation
== null) {
1675 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1677 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1679 await chargingStation
.removeReservation(
1681 ReservationTerminationReason
.RESERVATION_CANCELED
1683 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1685 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1686 return this.handleIncomingRequestError
<GenericResponse
>(
1688 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1690 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}