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
,
409 (chargingStation
: ChargingStation
, connectorId
: number, idTag
: string) => {
410 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
411 chargingStation
.getConnectorStatus(connectorId
)!.transactionRemoteStarted
= true
412 chargingStation
.ocppRequestService
413 .requestHandler
<OCPP16StartTransactionRequest
, OCPP16StartTransactionResponse
>(
415 OCPP16RequestCommand
.START_TRANSACTION
,
422 if (response
.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
424 `${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
428 `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
434 `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`,
440 this.validatePayload
= this.validatePayload
.bind(this)
443 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
444 chargingStation
: ChargingStation
,
446 commandName
: OCPP16IncomingRequestCommand
,
447 commandPayload
: ReqType
449 let response
: ResType
451 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
452 chargingStation
.inPendingState() &&
453 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
454 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
457 ErrorType
.SECURITY_ERROR
,
458 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
462 )} while the charging station is in pending state on the central server`,
468 chargingStation
.isRegistered() ||
469 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
470 chargingStation
.inUnknownState())
473 this.incomingRequestHandlers
.has(commandName
) &&
474 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
477 this.validatePayload(chargingStation
, commandName
, commandPayload
)
478 // Call the method to build the response
479 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
480 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
487 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
495 ErrorType
.NOT_IMPLEMENTED
,
496 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
507 ErrorType
.SECURITY_ERROR
,
508 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
512 )} while the charging station is not registered on the central server.`,
517 // Send the built response
518 await chargingStation
.ocppRequestService
.sendResponse(
526 private validatePayload (
527 chargingStation
: ChargingStation
,
528 commandName
: OCPP16IncomingRequestCommand
,
529 commandPayload
: JsonType
531 if (this.payloadValidateFunctions
.has(commandName
)) {
532 return this.validateIncomingRequestPayload(chargingStation
, commandName
, commandPayload
)
535 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`
540 // Simulate charging station restart
541 private handleRequestReset (
542 chargingStation
: ChargingStation
,
543 commandPayload
: ResetRequest
545 const { type } = commandPayload
547 .reset(`${type}Reset` as OCPP16StopTransactionReason
)
548 .catch(Constants
.EMPTY_FUNCTION
)
550 `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be back online in ${formatDurationMilliSeconds(
551 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
552 chargingStation.stationInfo!.resetTime!
555 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
558 private async handleRequestUnlockConnector (
559 chargingStation
: ChargingStation
,
560 commandPayload
: UnlockConnectorRequest
561 ): Promise
<UnlockConnectorResponse
> {
562 const { connectorId
} = commandPayload
563 if (!chargingStation
.hasConnector(connectorId
)) {
565 `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId}`
567 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
569 if (connectorId
=== 0) {
570 logger
.error(`${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId}`)
571 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
573 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
574 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
576 OCPP16StopTransactionReason
.UNLOCK_COMMAND
578 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
579 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
581 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
583 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
586 OCPP16ChargePointStatus
.Available
588 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
591 private handleRequestGetConfiguration (
592 chargingStation
: ChargingStation
,
593 commandPayload
: GetConfigurationRequest
594 ): GetConfigurationResponse
{
595 const { key
} = commandPayload
596 const configurationKey
: OCPPConfigurationKey
[] = []
597 const unknownKey
: string[] = []
599 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
600 for (const configKey
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
601 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(configKey
)) {
604 configurationKey
.push({
606 readonly: configKey
.readonly,
607 value
: configKey
.value
610 } else if (isNotEmptyArray(key
)) {
611 for (const k
of key
) {
612 const keyFound
= getConfigurationKey(chargingStation
, k
, true)
613 if (keyFound
!= null) {
614 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(keyFound
)) {
617 configurationKey
.push({
619 readonly: keyFound
.readonly,
620 value
: keyFound
.value
633 private handleRequestChangeConfiguration (
634 chargingStation
: ChargingStation
,
635 commandPayload
: ChangeConfigurationRequest
636 ): ChangeConfigurationResponse
{
637 const { key
, value
} = commandPayload
638 const keyToChange
= getConfigurationKey(chargingStation
, key
, true)
639 if (keyToChange
?.readonly === true) {
640 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
641 } else if (keyToChange
?.readonly === false) {
642 let valueChanged
= false
643 if (keyToChange
.value
!== value
) {
644 setConfigurationKeyValue(chargingStation
, key
, value
, true)
647 let triggerHeartbeatRestart
= false
649 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
650 OCPP16StandardParametersKey
.HeartBeatInterval
&&
653 setConfigurationKeyValue(
655 OCPP16StandardParametersKey
.HeartbeatInterval
,
658 triggerHeartbeatRestart
= true
661 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
662 OCPP16StandardParametersKey
.HeartbeatInterval
&&
665 setConfigurationKeyValue(
667 OCPP16StandardParametersKey
.HeartBeatInterval
,
670 triggerHeartbeatRestart
= true
672 if (triggerHeartbeatRestart
) {
673 chargingStation
.restartHeartbeat()
676 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
677 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
680 chargingStation
.restartWebSocketPing()
682 if (keyToChange
.reboot
=== true) {
683 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
685 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
687 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
690 private handleRequestSetChargingProfile (
691 chargingStation
: ChargingStation
,
692 commandPayload
: SetChargingProfileRequest
693 ): SetChargingProfileResponse
{
695 !OCPP16ServiceUtils
.checkFeatureProfile(
697 OCPP16SupportedFeatureProfiles
.SmartCharging
,
698 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
701 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
703 const { connectorId
, csChargingProfiles
} = commandPayload
704 if (!chargingStation
.hasConnector(connectorId
)) {
706 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${connectorId}`
708 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
711 csChargingProfiles
.chargingProfilePurpose
===
712 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
715 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
718 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
722 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId}`
724 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
726 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
728 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
730 connectorStatus
?.transactionStarted
=== false
733 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} without a started transaction`
735 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
738 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
740 connectorStatus
?.transactionStarted
=== true &&
741 csChargingProfiles
.transactionId
!== connectorStatus
.transactionId
744 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} with a different transaction id ${
745 csChargingProfiles.transactionId
746 } than the started transaction id ${connectorStatus.transactionId}`
748 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
750 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
)
752 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
755 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
758 private handleRequestGetCompositeSchedule (
759 chargingStation
: ChargingStation
,
760 commandPayload
: OCPP16GetCompositeScheduleRequest
761 ): OCPP16GetCompositeScheduleResponse
{
763 !OCPP16ServiceUtils
.checkFeatureProfile(
765 OCPP16SupportedFeatureProfiles
.SmartCharging
,
766 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
769 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
771 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
772 if (!chargingStation
.hasConnector(connectorId
)) {
774 `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${connectorId}`
776 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
778 if (connectorId
=== 0) {
780 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`
782 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
784 if (chargingRateUnit
!= null) {
786 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`
789 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
791 isEmptyArray(connectorStatus
?.chargingProfiles
) &&
792 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
794 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
796 const currentDate
= new Date()
797 const compositeScheduleInterval
: Interval
= {
799 end
: addSeconds(currentDate
, duration
)
801 // Get charging profiles sorted by connector id then stack level
802 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
806 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
807 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
808 for (const chargingProfile
of chargingProfiles
) {
809 if (chargingProfile
.chargingSchedule
.startSchedule
== null) {
811 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
812 chargingProfile.chargingProfileId
813 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
815 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
816 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
818 if (!isDate(chargingProfile
.chargingSchedule
.startSchedule
)) {
820 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
821 chargingProfile.chargingProfileId
822 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
824 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
825 chargingProfile
.chargingSchedule
.startSchedule
828 if (chargingProfile
.chargingSchedule
.duration
== null) {
830 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
831 chargingProfile.chargingProfileId
832 } has no duration defined and will be set to the maximum time allowed`
834 // OCPP specifies that if duration is not defined, it should be infinite
835 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
837 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
838 chargingProfile
.chargingSchedule
.startSchedule
!
842 !prepareChargingProfileKind(
845 compositeScheduleInterval
.start
,
846 chargingStation
.logPrefix()
852 !canProceedChargingProfile(
854 compositeScheduleInterval
.start
,
855 chargingStation
.logPrefix()
860 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
861 previousCompositeSchedule
,
862 chargingProfile
.chargingSchedule
,
863 compositeScheduleInterval
865 previousCompositeSchedule
= compositeSchedule
867 if (compositeSchedule
!= null) {
869 status: GenericStatus
.Accepted
,
870 scheduleStart
: compositeSchedule
.startSchedule
,
872 chargingSchedule
: compositeSchedule
875 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
878 private handleRequestClearChargingProfile (
879 chargingStation
: ChargingStation
,
880 commandPayload
: OCPP16ClearChargingProfileRequest
881 ): OCPP16ClearChargingProfileResponse
{
883 !OCPP16ServiceUtils
.checkFeatureProfile(
885 OCPP16SupportedFeatureProfiles
.SmartCharging
,
886 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
889 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
891 const { connectorId
} = commandPayload
892 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
893 if (!chargingStation
.hasConnector(connectorId
!)) {
895 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
897 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
899 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
900 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
901 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
902 connectorStatus
.chargingProfiles
= []
904 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
906 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
908 if (connectorId
== null) {
909 let clearedCP
= false
910 if (chargingStation
.hasEvses
) {
911 for (const evseStatus
of chargingStation
.evses
.values()) {
912 for (const status of evseStatus
.connectors
.values()) {
913 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
916 status.chargingProfiles
921 for (const id
of chargingStation
.connectors
.keys()) {
922 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
925 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
930 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
933 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
936 private async handleRequestChangeAvailability (
937 chargingStation
: ChargingStation
,
938 commandPayload
: OCPP16ChangeAvailabilityRequest
939 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
940 const { connectorId
, type } = commandPayload
941 if (!chargingStation
.hasConnector(connectorId
)) {
943 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
945 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
947 const chargePointStatus
: OCPP16ChargePointStatus
=
948 type === OCPP16AvailabilityType
.Operative
949 ? OCPP16ChargePointStatus
.Available
950 : OCPP16ChargePointStatus
.Unavailable
951 if (connectorId
=== 0) {
952 let response
: OCPP16ChangeAvailabilityResponse
| undefined
953 if (chargingStation
.hasEvses
) {
954 for (const evseStatus
of chargingStation
.evses
.values()) {
955 response
= await OCPP16ServiceUtils
.changeAvailability(
957 [...evseStatus
.connectors
.keys()],
963 response
= await OCPP16ServiceUtils
.changeAvailability(
965 [...chargingStation
.connectors
.keys()],
970 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
974 (chargingStation
.isChargingStationAvailable() ||
975 (!chargingStation
.isChargingStationAvailable() &&
976 type === OCPP16AvailabilityType
.Inoperative
))
978 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
979 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
980 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
981 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
983 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
984 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
985 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
990 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
992 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
995 private async handleRequestRemoteStartTransaction (
996 chargingStation
: ChargingStation
,
997 commandPayload
: RemoteStartTransactionRequest
998 ): Promise
<GenericResponse
> {
999 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
1000 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
1001 return await this.notifyRemoteStartTransactionRejected(
1003 transactionConnectorId
,
1008 !chargingStation
.isChargingStationAvailable() ||
1009 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
1011 return await this.notifyRemoteStartTransactionRejected(
1013 transactionConnectorId
,
1017 // idTag authorization check required
1019 chargingStation
.getAuthorizeRemoteTxRequests() &&
1020 !(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
1022 return await this.notifyRemoteStartTransactionRejected(
1024 transactionConnectorId
,
1028 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1030 transactionConnectorId
,
1031 OCPP16ChargePointStatus
.Preparing
1034 chargingProfile
!= null &&
1035 !this.setRemoteStartTransactionChargingProfile(
1037 transactionConnectorId
,
1041 return await this.notifyRemoteStartTransactionRejected(
1043 transactionConnectorId
,
1048 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
1050 transactionConnectorId
,
1053 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1056 private async notifyRemoteStartTransactionRejected (
1057 chargingStation
: ChargingStation
,
1058 connectorId
: number,
1060 ): Promise
<GenericResponse
> {
1061 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1062 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1063 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1066 OCPP16ChargePointStatus
.Available
1070 `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1072 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1075 private setRemoteStartTransactionChargingProfile (
1076 chargingStation
: ChargingStation
,
1077 connectorId
: number,
1078 chargingProfile
: OCPP16ChargingProfile
1080 if (chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1081 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1083 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1089 `${chargingStation.logPrefix()} Not allowed to set ${
1090 chargingProfile.chargingProfilePurpose
1091 } charging profile(s) at remote start transaction`
1096 private async handleRequestRemoteStopTransaction (
1097 chargingStation
: ChargingStation
,
1098 commandPayload
: RemoteStopTransactionRequest
1099 ): Promise
<GenericResponse
> {
1100 const { transactionId
} = commandPayload
1101 if (chargingStation
.hasEvses
) {
1102 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1104 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1105 if (connectorStatus
.transactionId
=== transactionId
) {
1106 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1112 for (const connectorId
of chargingStation
.connectors
.keys()) {
1115 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1117 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1122 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1124 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1127 private handleRequestUpdateFirmware (
1128 chargingStation
: ChargingStation
,
1129 commandPayload
: OCPP16UpdateFirmwareRequest
1130 ): OCPP16UpdateFirmwareResponse
{
1132 !OCPP16ServiceUtils
.checkFeatureProfile(
1134 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1135 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1139 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1141 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1143 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1144 commandPayload
.retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!
1145 const { retrieveDate
} = commandPayload
1146 if (chargingStation
.stationInfo
?.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
) {
1148 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1150 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1152 const now
= Date.now()
1153 if (retrieveDate
.getTime() <= now
) {
1154 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1157 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1158 }, retrieveDate
.getTime() - now
)
1160 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1163 private async updateFirmwareSimulation (
1164 chargingStation
: ChargingStation
,
1168 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1171 if (chargingStation
.hasEvses
) {
1172 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1174 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1175 if (connectorStatus
.transactionStarted
=== false) {
1176 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1179 OCPP16ChargePointStatus
.Unavailable
1186 for (const connectorId
of chargingStation
.connectors
.keys()) {
1189 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1191 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1194 OCPP16ChargePointStatus
.Unavailable
1199 await chargingStation
.ocppRequestService
.requestHandler
<
1200 OCPP16FirmwareStatusNotificationRequest
,
1201 OCPP16FirmwareStatusNotificationResponse
1202 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1203 status: OCPP16FirmwareStatus
.Downloading
1205 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1206 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1208 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1209 OCPP16FirmwareStatus
.DownloadFailed
1211 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1212 await chargingStation
.ocppRequestService
.requestHandler
<
1213 OCPP16FirmwareStatusNotificationRequest
,
1214 OCPP16FirmwareStatusNotificationResponse
1215 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1216 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1218 chargingStation
.stationInfo
.firmwareStatus
=
1219 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1222 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1223 await chargingStation
.ocppRequestService
.requestHandler
<
1224 OCPP16FirmwareStatusNotificationRequest
,
1225 OCPP16FirmwareStatusNotificationResponse
1226 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1227 status: OCPP16FirmwareStatus
.Downloaded
1229 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1230 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1231 let wasTransactionsStarted
= false
1232 let transactionsStarted
: boolean
1234 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1235 if (runningTransactions
> 0) {
1236 const waitTime
= secondsToMilliseconds(15)
1238 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1240 )} before continuing firmware update simulation`
1242 await sleep(waitTime
)
1243 transactionsStarted
= true
1244 wasTransactionsStarted
= true
1246 if (chargingStation
.hasEvses
) {
1247 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1249 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1250 if (connectorStatus
.status !== OCPP16ChargePointStatus
.Unavailable
) {
1251 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1254 OCPP16ChargePointStatus
.Unavailable
1261 for (const connectorId
of chargingStation
.connectors
.keys()) {
1264 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1265 OCPP16ChargePointStatus
.Unavailable
1267 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1270 OCPP16ChargePointStatus
.Unavailable
1275 transactionsStarted
= false
1277 } while (transactionsStarted
)
1278 !wasTransactionsStarted
&&
1279 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1280 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1283 await chargingStation
.ocppRequestService
.requestHandler
<
1284 OCPP16FirmwareStatusNotificationRequest
,
1285 OCPP16FirmwareStatusNotificationResponse
1286 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1287 status: OCPP16FirmwareStatus
.Installing
1289 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1290 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1292 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1293 OCPP16FirmwareStatus
.InstallationFailed
1295 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1296 await chargingStation
.ocppRequestService
.requestHandler
<
1297 OCPP16FirmwareStatusNotificationRequest
,
1298 OCPP16FirmwareStatusNotificationResponse
1299 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1300 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1302 chargingStation
.stationInfo
.firmwareStatus
=
1303 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1306 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1307 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1308 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1312 private async handleRequestGetDiagnostics (
1313 chargingStation
: ChargingStation
,
1314 commandPayload
: GetDiagnosticsRequest
1315 ): Promise
<GetDiagnosticsResponse
> {
1317 !OCPP16ServiceUtils
.checkFeatureProfile(
1319 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1320 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1324 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1326 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1328 const { location
} = commandPayload
1329 const uri
= new URL(location
)
1330 if (uri
.protocol
.startsWith('ftp:')) {
1331 let ftpClient
: Client
| undefined
1333 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1334 .filter(file
=> file
.endsWith('.log'))
1335 .map(file
=> join('./', file
))
1336 const diagnosticsArchive
= `${chargingStation.stationInfo?.chargingStationId}_logs.tar.gz`
1337 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1338 ftpClient
= new Client()
1339 const accessResponse
= await ftpClient
.access({
1341 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1342 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1343 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1345 let uploadResponse
: FTPResponse
| undefined
1346 if (accessResponse
.code
=== 220) {
1347 ftpClient
.trackProgress(info
=> {
1349 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1351 } bytes transferred from diagnostics archive ${info.name}`
1353 chargingStation
.ocppRequestService
1355 OCPP16DiagnosticsStatusNotificationRequest
,
1356 OCPP16DiagnosticsStatusNotificationResponse
1357 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1358 status: OCPP16DiagnosticsStatus
.Uploading
1362 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1363 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1369 uploadResponse
= await ftpClient
.uploadFrom(
1370 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1371 `${uri.pathname}${diagnosticsArchive}`
1373 if (uploadResponse
.code
=== 226) {
1374 await chargingStation
.ocppRequestService
.requestHandler
<
1375 OCPP16DiagnosticsStatusNotificationRequest
,
1376 OCPP16DiagnosticsStatusNotificationResponse
1377 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1378 status: OCPP16DiagnosticsStatus
.Uploaded
1381 return { fileName
: diagnosticsArchive
}
1383 throw new OCPPError(
1384 ErrorType
.GENERIC_ERROR
,
1385 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse.code}`,
1386 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1389 throw new OCPPError(
1390 ErrorType
.GENERIC_ERROR
,
1391 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse?.code}`,
1392 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1395 await chargingStation
.ocppRequestService
.requestHandler
<
1396 OCPP16DiagnosticsStatusNotificationRequest
,
1397 OCPP16DiagnosticsStatusNotificationResponse
1398 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1399 status: OCPP16DiagnosticsStatus
.UploadFailed
1402 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1403 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1405 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1407 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1412 `${chargingStation.logPrefix()} Unsupported protocol ${
1414 } to transfer the diagnostic logs archive`
1416 await chargingStation
.ocppRequestService
.requestHandler
<
1417 OCPP16DiagnosticsStatusNotificationRequest
,
1418 OCPP16DiagnosticsStatusNotificationResponse
1419 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1420 status: OCPP16DiagnosticsStatus
.UploadFailed
1422 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1426 private handleRequestTriggerMessage (
1427 chargingStation
: ChargingStation
,
1428 commandPayload
: OCPP16TriggerMessageRequest
1429 ): OCPP16TriggerMessageResponse
{
1430 const { requestedMessage
, connectorId
} = commandPayload
1432 !OCPP16ServiceUtils
.checkFeatureProfile(
1434 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1435 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1437 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1439 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1442 !OCPP16ServiceUtils
.isConnectorIdValid(
1444 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1445 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1449 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1452 switch (requestedMessage
) {
1453 case OCPP16MessageTrigger
.BootNotification
:
1455 chargingStation
.ocppRequestService
1456 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1458 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1459 chargingStation
.bootNotificationRequest
,
1460 { skipBufferingOnError
: true, triggerMessage
: true }
1463 chargingStation
.bootNotificationResponse
= response
1465 .catch(Constants
.EMPTY_FUNCTION
)
1466 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1467 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1468 case OCPP16MessageTrigger
.Heartbeat
:
1470 chargingStation
.ocppRequestService
1471 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1473 OCPP16RequestCommand
.HEARTBEAT
,
1476 triggerMessage
: true
1479 .catch(Constants
.EMPTY_FUNCTION
)
1480 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1481 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1482 case OCPP16MessageTrigger
.StatusNotification
:
1484 if (connectorId
!= null) {
1485 chargingStation
.ocppRequestService
1486 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1488 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1491 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1492 status: chargingStation
.getConnectorStatus(connectorId
)?.status
1495 triggerMessage
: true
1498 .catch(Constants
.EMPTY_FUNCTION
)
1499 } else if (chargingStation
.hasEvses
) {
1500 for (const evseStatus
of chargingStation
.evses
.values()) {
1501 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1502 chargingStation
.ocppRequestService
1504 OCPP16StatusNotificationRequest
,
1505 OCPP16StatusNotificationResponse
1508 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1511 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1512 status: connectorStatus
.status
1515 triggerMessage
: true
1518 .catch(Constants
.EMPTY_FUNCTION
)
1522 for (const [id
, connectorStatus
] of chargingStation
.connectors
) {
1523 chargingStation
.ocppRequestService
1525 OCPP16StatusNotificationRequest
,
1526 OCPP16StatusNotificationResponse
1529 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1532 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1533 status: connectorStatus
.status
1536 triggerMessage
: true
1539 .catch(Constants
.EMPTY_FUNCTION
)
1542 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1543 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1545 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1548 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1549 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1551 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1553 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1558 private handleRequestDataTransfer (
1559 chargingStation
: ChargingStation
,
1560 commandPayload
: OCPP16DataTransferRequest
1561 ): OCPP16DataTransferResponse
{
1562 const { vendorId
} = commandPayload
1564 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1565 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1567 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1569 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1570 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1572 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1574 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1579 private async handleRequestReserveNow (
1580 chargingStation
: ChargingStation
,
1581 commandPayload
: OCPP16ReserveNowRequest
1582 ): Promise
<OCPP16ReserveNowResponse
> {
1584 !OCPP16ServiceUtils
.checkFeatureProfile(
1586 OCPP16SupportedFeatureProfiles
.Reservation
,
1587 OCPP16IncomingRequestCommand
.RESERVE_NOW
1590 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1592 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1593 commandPayload
.expiryDate
= convertToDate(commandPayload
.expiryDate
)!
1594 const { reservationId
, idTag
, connectorId
} = commandPayload
1595 let response
: OCPP16ReserveNowResponse
1597 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1598 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1600 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1601 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1603 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1604 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1606 await removeExpiredReservations(chargingStation
)
1607 switch (chargingStation
.getConnectorStatus(connectorId
)?.status) {
1608 case OCPP16ChargePointStatus
.Faulted
:
1609 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1611 case OCPP16ChargePointStatus
.Preparing
:
1612 case OCPP16ChargePointStatus
.Charging
:
1613 case OCPP16ChargePointStatus
.SuspendedEV
:
1614 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1615 case OCPP16ChargePointStatus
.Finishing
:
1616 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1618 case OCPP16ChargePointStatus
.Unavailable
:
1619 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1621 case OCPP16ChargePointStatus
.Reserved
:
1622 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1623 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1626 // eslint-disable-next-line no-fallthrough
1628 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1629 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1632 await chargingStation
.addReservation({
1633 id
: commandPayload
.reservationId
,
1636 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1641 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1642 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1643 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1644 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1646 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1648 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1653 private async handleRequestCancelReservation (
1654 chargingStation
: ChargingStation
,
1655 commandPayload
: OCPP16CancelReservationRequest
1656 ): Promise
<GenericResponse
> {
1658 !OCPP16ServiceUtils
.checkFeatureProfile(
1660 OCPP16SupportedFeatureProfiles
.Reservation
,
1661 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1664 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1667 const { reservationId
} = commandPayload
1668 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1669 if (reservation
== null) {
1671 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1673 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1675 await chargingStation
.removeReservation(
1677 ReservationTerminationReason
.RESERVATION_CANCELED
1679 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1681 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1682 return this.handleIncomingRequestError
<GenericResponse
>(
1684 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1686 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}