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 ${
425 chargingStation.stationInfo?.chargingStationId
426 }#${connectorId} for idTag '${idTag}'`)
429 ${chargingStation.logPrefix()} Remote start transaction REJECTED on ${
430 chargingStation.stationInfo?.chargingStationId
431 }#${connectorId} for idTag '${idTag}'`)
436 `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`,
442 this.validatePayload
= this.validatePayload
.bind(this)
445 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
446 chargingStation
: ChargingStation
,
448 commandName
: OCPP16IncomingRequestCommand
,
449 commandPayload
: ReqType
451 let response
: ResType
453 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
454 chargingStation
.inPendingState() &&
455 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
456 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
459 ErrorType
.SECURITY_ERROR
,
460 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
464 )} while the charging station is in pending state on the central server`,
470 chargingStation
.isRegistered() ||
471 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
472 chargingStation
.inUnknownState())
475 this.incomingRequestHandlers
.has(commandName
) &&
476 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
479 this.validatePayload(chargingStation
, commandName
, commandPayload
)
480 // Call the method to build the response
481 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
482 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
489 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
497 ErrorType
.NOT_IMPLEMENTED
,
498 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
509 ErrorType
.SECURITY_ERROR
,
510 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
514 )} while the charging station is not registered on the central server.`,
519 // Send the built response
520 await chargingStation
.ocppRequestService
.sendResponse(
528 private validatePayload (
529 chargingStation
: ChargingStation
,
530 commandName
: OCPP16IncomingRequestCommand
,
531 commandPayload
: JsonType
533 if (this.payloadValidateFunctions
.has(commandName
)) {
534 return this.validateIncomingRequestPayload(chargingStation
, commandName
, commandPayload
)
537 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`
542 // Simulate charging station restart
543 private handleRequestReset (
544 chargingStation
: ChargingStation
,
545 commandPayload
: ResetRequest
547 const { type } = commandPayload
549 .reset(`${type}Reset` as OCPP16StopTransactionReason
)
550 .catch(Constants
.EMPTY_FUNCTION
)
552 `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be back online in ${formatDurationMilliSeconds(
553 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
554 chargingStation.stationInfo!.resetTime!
557 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
560 private async handleRequestUnlockConnector (
561 chargingStation
: ChargingStation
,
562 commandPayload
: UnlockConnectorRequest
563 ): Promise
<UnlockConnectorResponse
> {
564 const { connectorId
} = commandPayload
565 if (!chargingStation
.hasConnector(connectorId
)) {
567 `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId}`
569 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
571 if (connectorId
=== 0) {
572 logger
.error(`${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId}`)
573 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
575 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
576 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
578 OCPP16StopTransactionReason
.UNLOCK_COMMAND
580 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
581 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
583 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
585 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
588 OCPP16ChargePointStatus
.Available
590 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
593 private handleRequestGetConfiguration (
594 chargingStation
: ChargingStation
,
595 commandPayload
: GetConfigurationRequest
596 ): GetConfigurationResponse
{
597 const { key
} = commandPayload
598 const configurationKey
: OCPPConfigurationKey
[] = []
599 const unknownKey
: string[] = []
601 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
602 for (const configKey
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
603 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(configKey
)) {
606 configurationKey
.push({
608 readonly: configKey
.readonly,
609 value
: configKey
.value
612 } else if (isNotEmptyArray(key
)) {
613 for (const k
of key
) {
614 const keyFound
= getConfigurationKey(chargingStation
, k
, true)
615 if (keyFound
!= null) {
616 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(keyFound
)) {
619 configurationKey
.push({
621 readonly: keyFound
.readonly,
622 value
: keyFound
.value
635 private handleRequestChangeConfiguration (
636 chargingStation
: ChargingStation
,
637 commandPayload
: ChangeConfigurationRequest
638 ): ChangeConfigurationResponse
{
639 const { key
, value
} = commandPayload
640 const keyToChange
= getConfigurationKey(chargingStation
, key
, true)
641 if (keyToChange
?.readonly === true) {
642 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
643 } else if (keyToChange
?.readonly === false) {
644 let valueChanged
= false
645 if (keyToChange
.value
!== value
) {
646 setConfigurationKeyValue(chargingStation
, key
, value
, true)
649 let triggerHeartbeatRestart
= false
651 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
652 OCPP16StandardParametersKey
.HeartBeatInterval
&&
655 setConfigurationKeyValue(
657 OCPP16StandardParametersKey
.HeartbeatInterval
,
660 triggerHeartbeatRestart
= true
663 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
664 OCPP16StandardParametersKey
.HeartbeatInterval
&&
667 setConfigurationKeyValue(
669 OCPP16StandardParametersKey
.HeartBeatInterval
,
672 triggerHeartbeatRestart
= true
674 if (triggerHeartbeatRestart
) {
675 chargingStation
.restartHeartbeat()
678 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
679 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
682 chargingStation
.restartWebSocketPing()
684 if (keyToChange
.reboot
=== true) {
685 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
687 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
689 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
692 private handleRequestSetChargingProfile (
693 chargingStation
: ChargingStation
,
694 commandPayload
: SetChargingProfileRequest
695 ): SetChargingProfileResponse
{
697 !OCPP16ServiceUtils
.checkFeatureProfile(
699 OCPP16SupportedFeatureProfiles
.SmartCharging
,
700 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
703 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
705 const { connectorId
, csChargingProfiles
} = commandPayload
706 if (!chargingStation
.hasConnector(connectorId
)) {
708 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${connectorId}`
710 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
713 csChargingProfiles
.chargingProfilePurpose
===
714 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
717 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
720 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
724 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId}`
726 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
728 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
730 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
732 connectorStatus
?.transactionStarted
=== false
735 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} without a started transaction`
737 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
740 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
742 connectorStatus
?.transactionStarted
=== true &&
743 csChargingProfiles
.transactionId
!== connectorStatus
.transactionId
746 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} with a different transaction id ${
747 csChargingProfiles.transactionId
748 } than the started transaction id ${connectorStatus.transactionId}`
750 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
752 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
)
754 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
757 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
760 private handleRequestGetCompositeSchedule (
761 chargingStation
: ChargingStation
,
762 commandPayload
: OCPP16GetCompositeScheduleRequest
763 ): OCPP16GetCompositeScheduleResponse
{
765 !OCPP16ServiceUtils
.checkFeatureProfile(
767 OCPP16SupportedFeatureProfiles
.SmartCharging
,
768 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
771 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
773 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
774 if (!chargingStation
.hasConnector(connectorId
)) {
776 `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${connectorId}`
778 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
780 if (connectorId
=== 0) {
782 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`
784 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
786 if (chargingRateUnit
!= null) {
788 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`
791 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
793 isEmptyArray(connectorStatus
?.chargingProfiles
) &&
794 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
796 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
798 const currentDate
= new Date()
799 const compositeScheduleInterval
: Interval
= {
801 end
: addSeconds(currentDate
, duration
)
803 // Get charging profiles sorted by connector id then stack level
804 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
808 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
809 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
810 for (const chargingProfile
of chargingProfiles
) {
811 if (chargingProfile
.chargingSchedule
.startSchedule
== null) {
813 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
814 chargingProfile.chargingProfileId
815 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
817 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
818 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
820 if (!isDate(chargingProfile
.chargingSchedule
.startSchedule
)) {
822 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
823 chargingProfile.chargingProfileId
824 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
826 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
827 chargingProfile
.chargingSchedule
.startSchedule
830 if (chargingProfile
.chargingSchedule
.duration
== null) {
832 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
833 chargingProfile.chargingProfileId
834 } has no duration defined and will be set to the maximum time allowed`
836 // OCPP specifies that if duration is not defined, it should be infinite
837 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
839 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
840 chargingProfile
.chargingSchedule
.startSchedule
!
844 !prepareChargingProfileKind(
847 compositeScheduleInterval
.start
,
848 chargingStation
.logPrefix()
854 !canProceedChargingProfile(
856 compositeScheduleInterval
.start
,
857 chargingStation
.logPrefix()
862 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
863 previousCompositeSchedule
,
864 chargingProfile
.chargingSchedule
,
865 compositeScheduleInterval
867 previousCompositeSchedule
= compositeSchedule
869 if (compositeSchedule
!= null) {
871 status: GenericStatus
.Accepted
,
872 scheduleStart
: compositeSchedule
.startSchedule
,
874 chargingSchedule
: compositeSchedule
877 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
880 private handleRequestClearChargingProfile (
881 chargingStation
: ChargingStation
,
882 commandPayload
: OCPP16ClearChargingProfileRequest
883 ): OCPP16ClearChargingProfileResponse
{
885 !OCPP16ServiceUtils
.checkFeatureProfile(
887 OCPP16SupportedFeatureProfiles
.SmartCharging
,
888 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
891 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
893 const { connectorId
} = commandPayload
894 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
895 if (!chargingStation
.hasConnector(connectorId
!)) {
897 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
899 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
901 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
902 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
903 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
904 connectorStatus
.chargingProfiles
= []
906 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
908 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
910 if (connectorId
== null) {
911 let clearedCP
= false
912 if (chargingStation
.hasEvses
) {
913 for (const evseStatus
of chargingStation
.evses
.values()) {
914 for (const status of evseStatus
.connectors
.values()) {
915 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
918 status.chargingProfiles
923 for (const id
of chargingStation
.connectors
.keys()) {
924 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
927 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
932 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
935 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
938 private async handleRequestChangeAvailability (
939 chargingStation
: ChargingStation
,
940 commandPayload
: OCPP16ChangeAvailabilityRequest
941 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
942 const { connectorId
, type } = commandPayload
943 if (!chargingStation
.hasConnector(connectorId
)) {
945 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
947 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
949 const chargePointStatus
: OCPP16ChargePointStatus
=
950 type === OCPP16AvailabilityType
.Operative
951 ? OCPP16ChargePointStatus
.Available
952 : OCPP16ChargePointStatus
.Unavailable
953 if (connectorId
=== 0) {
954 let response
: OCPP16ChangeAvailabilityResponse
| undefined
955 if (chargingStation
.hasEvses
) {
956 for (const evseStatus
of chargingStation
.evses
.values()) {
957 response
= await OCPP16ServiceUtils
.changeAvailability(
959 [...evseStatus
.connectors
.keys()],
965 response
= await OCPP16ServiceUtils
.changeAvailability(
967 [...chargingStation
.connectors
.keys()],
972 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
976 (chargingStation
.isChargingStationAvailable() ||
977 (!chargingStation
.isChargingStationAvailable() &&
978 type === OCPP16AvailabilityType
.Inoperative
))
980 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
981 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
982 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
983 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
985 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
986 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
987 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
992 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
994 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
997 private async handleRequestRemoteStartTransaction (
998 chargingStation
: ChargingStation
,
999 commandPayload
: RemoteStartTransactionRequest
1000 ): Promise
<GenericResponse
> {
1001 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
1002 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
1003 return await this.notifyRemoteStartTransactionRejected(
1005 transactionConnectorId
,
1010 !chargingStation
.isChargingStationAvailable() ||
1011 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
1013 return await this.notifyRemoteStartTransactionRejected(
1015 transactionConnectorId
,
1019 // idTag authorization check required
1021 chargingStation
.getAuthorizeRemoteTxRequests() &&
1022 !(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
1024 return await this.notifyRemoteStartTransactionRejected(
1026 transactionConnectorId
,
1030 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1032 transactionConnectorId
,
1033 OCPP16ChargePointStatus
.Preparing
1036 chargingProfile
!= null &&
1037 !this.setRemoteStartTransactionChargingProfile(
1039 transactionConnectorId
,
1043 return await this.notifyRemoteStartTransactionRejected(
1045 transactionConnectorId
,
1050 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
1052 transactionConnectorId
,
1055 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1058 private async notifyRemoteStartTransactionRejected (
1059 chargingStation
: ChargingStation
,
1060 connectorId
: number,
1062 ): Promise
<GenericResponse
> {
1063 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1064 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1065 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1068 OCPP16ChargePointStatus
.Available
1072 `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1074 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1077 private setRemoteStartTransactionChargingProfile (
1078 chargingStation
: ChargingStation
,
1079 connectorId
: number,
1080 chargingProfile
: OCPP16ChargingProfile
1082 if (chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1083 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1085 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1091 `${chargingStation.logPrefix()} Not allowed to set ${
1092 chargingProfile.chargingProfilePurpose
1093 } charging profile(s) at remote start transaction`
1098 private async handleRequestRemoteStopTransaction (
1099 chargingStation
: ChargingStation
,
1100 commandPayload
: RemoteStopTransactionRequest
1101 ): Promise
<GenericResponse
> {
1102 const { transactionId
} = commandPayload
1103 if (chargingStation
.hasEvses
) {
1104 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1106 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1107 if (connectorStatus
.transactionId
=== transactionId
) {
1108 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1114 for (const connectorId
of chargingStation
.connectors
.keys()) {
1117 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1119 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1124 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1126 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1129 private handleRequestUpdateFirmware (
1130 chargingStation
: ChargingStation
,
1131 commandPayload
: OCPP16UpdateFirmwareRequest
1132 ): OCPP16UpdateFirmwareResponse
{
1134 !OCPP16ServiceUtils
.checkFeatureProfile(
1136 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1137 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1141 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1143 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1145 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1146 commandPayload
.retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!
1147 const { retrieveDate
} = commandPayload
1148 if (chargingStation
.stationInfo
?.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
) {
1150 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1152 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1154 const now
= Date.now()
1155 if (retrieveDate
.getTime() <= now
) {
1156 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1159 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1160 }, retrieveDate
.getTime() - now
)
1162 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1165 private async updateFirmwareSimulation (
1166 chargingStation
: ChargingStation
,
1170 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1173 if (chargingStation
.hasEvses
) {
1174 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1176 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1177 if (connectorStatus
.transactionStarted
=== false) {
1178 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1181 OCPP16ChargePointStatus
.Unavailable
1188 for (const connectorId
of chargingStation
.connectors
.keys()) {
1191 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1193 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1196 OCPP16ChargePointStatus
.Unavailable
1201 await chargingStation
.ocppRequestService
.requestHandler
<
1202 OCPP16FirmwareStatusNotificationRequest
,
1203 OCPP16FirmwareStatusNotificationResponse
1204 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1205 status: OCPP16FirmwareStatus
.Downloading
1207 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1208 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1210 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1211 OCPP16FirmwareStatus
.DownloadFailed
1213 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1214 await chargingStation
.ocppRequestService
.requestHandler
<
1215 OCPP16FirmwareStatusNotificationRequest
,
1216 OCPP16FirmwareStatusNotificationResponse
1217 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1218 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1220 chargingStation
.stationInfo
.firmwareStatus
=
1221 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1224 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1225 await chargingStation
.ocppRequestService
.requestHandler
<
1226 OCPP16FirmwareStatusNotificationRequest
,
1227 OCPP16FirmwareStatusNotificationResponse
1228 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1229 status: OCPP16FirmwareStatus
.Downloaded
1231 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1232 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1233 let wasTransactionsStarted
= false
1234 let transactionsStarted
: boolean
1236 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1237 if (runningTransactions
> 0) {
1238 const waitTime
= secondsToMilliseconds(15)
1240 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1242 )} before continuing firmware update simulation`
1244 await sleep(waitTime
)
1245 transactionsStarted
= true
1246 wasTransactionsStarted
= true
1248 if (chargingStation
.hasEvses
) {
1249 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1251 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1252 if (connectorStatus
.status !== OCPP16ChargePointStatus
.Unavailable
) {
1253 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1256 OCPP16ChargePointStatus
.Unavailable
1263 for (const connectorId
of chargingStation
.connectors
.keys()) {
1266 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1267 OCPP16ChargePointStatus
.Unavailable
1269 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1272 OCPP16ChargePointStatus
.Unavailable
1277 transactionsStarted
= false
1279 } while (transactionsStarted
)
1280 !wasTransactionsStarted
&&
1281 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1282 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1285 await chargingStation
.ocppRequestService
.requestHandler
<
1286 OCPP16FirmwareStatusNotificationRequest
,
1287 OCPP16FirmwareStatusNotificationResponse
1288 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1289 status: OCPP16FirmwareStatus
.Installing
1291 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1292 chargingStation
.stationInfo
!.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1294 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1295 OCPP16FirmwareStatus
.InstallationFailed
1297 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1298 await chargingStation
.ocppRequestService
.requestHandler
<
1299 OCPP16FirmwareStatusNotificationRequest
,
1300 OCPP16FirmwareStatusNotificationResponse
1301 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1302 status: chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1304 chargingStation
.stationInfo
.firmwareStatus
=
1305 chargingStation
.stationInfo
.firmwareUpgrade
.failureStatus
1308 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1309 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1310 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1314 private async handleRequestGetDiagnostics (
1315 chargingStation
: ChargingStation
,
1316 commandPayload
: GetDiagnosticsRequest
1317 ): Promise
<GetDiagnosticsResponse
> {
1319 !OCPP16ServiceUtils
.checkFeatureProfile(
1321 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1322 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1326 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1328 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1330 const { location
} = commandPayload
1331 const uri
= new URL(location
)
1332 if (uri
.protocol
.startsWith('ftp:')) {
1333 let ftpClient
: Client
| undefined
1335 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1336 .filter(file
=> file
.endsWith('.log'))
1337 .map(file
=> join('./', file
))
1338 const diagnosticsArchive
= `${chargingStation.stationInfo?.chargingStationId}_logs.tar.gz`
1339 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1340 ftpClient
= new Client()
1341 const accessResponse
= await ftpClient
.access({
1343 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1344 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1345 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1347 let uploadResponse
: FTPResponse
| undefined
1348 if (accessResponse
.code
=== 220) {
1349 ftpClient
.trackProgress(info
=> {
1351 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1353 } bytes transferred from diagnostics archive ${info.name}`
1355 chargingStation
.ocppRequestService
1357 OCPP16DiagnosticsStatusNotificationRequest
,
1358 OCPP16DiagnosticsStatusNotificationResponse
1359 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1360 status: OCPP16DiagnosticsStatus
.Uploading
1364 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1365 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1371 uploadResponse
= await ftpClient
.uploadFrom(
1372 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1373 `${uri.pathname}${diagnosticsArchive}`
1375 if (uploadResponse
.code
=== 226) {
1376 await chargingStation
.ocppRequestService
.requestHandler
<
1377 OCPP16DiagnosticsStatusNotificationRequest
,
1378 OCPP16DiagnosticsStatusNotificationResponse
1379 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1380 status: OCPP16DiagnosticsStatus
.Uploaded
1383 return { fileName
: diagnosticsArchive
}
1385 throw new OCPPError(
1386 ErrorType
.GENERIC_ERROR
,
1387 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse.code}`,
1388 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1391 throw new OCPPError(
1392 ErrorType
.GENERIC_ERROR
,
1393 `Diagnostics transfer failed with error code ${accessResponse.code}|${uploadResponse?.code}`,
1394 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1397 await chargingStation
.ocppRequestService
.requestHandler
<
1398 OCPP16DiagnosticsStatusNotificationRequest
,
1399 OCPP16DiagnosticsStatusNotificationResponse
1400 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1401 status: OCPP16DiagnosticsStatus
.UploadFailed
1404 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1405 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1407 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1409 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1414 `${chargingStation.logPrefix()} Unsupported protocol ${
1416 } to transfer the diagnostic logs archive`
1418 await chargingStation
.ocppRequestService
.requestHandler
<
1419 OCPP16DiagnosticsStatusNotificationRequest
,
1420 OCPP16DiagnosticsStatusNotificationResponse
1421 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1422 status: OCPP16DiagnosticsStatus
.UploadFailed
1424 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1428 private handleRequestTriggerMessage (
1429 chargingStation
: ChargingStation
,
1430 commandPayload
: OCPP16TriggerMessageRequest
1431 ): OCPP16TriggerMessageResponse
{
1432 const { requestedMessage
, connectorId
} = commandPayload
1434 !OCPP16ServiceUtils
.checkFeatureProfile(
1436 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1437 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1439 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1441 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1444 !OCPP16ServiceUtils
.isConnectorIdValid(
1446 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1447 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1451 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1454 switch (requestedMessage
) {
1455 case OCPP16MessageTrigger
.BootNotification
:
1457 chargingStation
.ocppRequestService
1458 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1460 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1461 chargingStation
.bootNotificationRequest
,
1462 { skipBufferingOnError
: true, triggerMessage
: true }
1465 chargingStation
.bootNotificationResponse
= response
1467 .catch(Constants
.EMPTY_FUNCTION
)
1468 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1469 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1470 case OCPP16MessageTrigger
.Heartbeat
:
1472 chargingStation
.ocppRequestService
1473 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1475 OCPP16RequestCommand
.HEARTBEAT
,
1478 triggerMessage
: true
1481 .catch(Constants
.EMPTY_FUNCTION
)
1482 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1483 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1484 case OCPP16MessageTrigger
.StatusNotification
:
1486 if (connectorId
!= null) {
1487 chargingStation
.ocppRequestService
1488 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1490 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1493 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1494 status: chargingStation
.getConnectorStatus(connectorId
)?.status
1497 triggerMessage
: true
1500 .catch(Constants
.EMPTY_FUNCTION
)
1501 } else if (chargingStation
.hasEvses
) {
1502 for (const evseStatus
of chargingStation
.evses
.values()) {
1503 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1504 chargingStation
.ocppRequestService
1506 OCPP16StatusNotificationRequest
,
1507 OCPP16StatusNotificationResponse
1510 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1513 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1514 status: connectorStatus
.status
1517 triggerMessage
: true
1520 .catch(Constants
.EMPTY_FUNCTION
)
1524 for (const [id
, connectorStatus
] of chargingStation
.connectors
) {
1525 chargingStation
.ocppRequestService
1527 OCPP16StatusNotificationRequest
,
1528 OCPP16StatusNotificationResponse
1531 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1534 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1535 status: connectorStatus
.status
1538 triggerMessage
: true
1541 .catch(Constants
.EMPTY_FUNCTION
)
1544 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1545 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1547 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1550 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1551 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1553 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1555 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1560 private handleRequestDataTransfer (
1561 chargingStation
: ChargingStation
,
1562 commandPayload
: OCPP16DataTransferRequest
1563 ): OCPP16DataTransferResponse
{
1564 const { vendorId
} = commandPayload
1566 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1567 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1569 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1571 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1572 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1574 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1576 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1581 private async handleRequestReserveNow (
1582 chargingStation
: ChargingStation
,
1583 commandPayload
: OCPP16ReserveNowRequest
1584 ): Promise
<OCPP16ReserveNowResponse
> {
1586 !OCPP16ServiceUtils
.checkFeatureProfile(
1588 OCPP16SupportedFeatureProfiles
.Reservation
,
1589 OCPP16IncomingRequestCommand
.RESERVE_NOW
1592 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1594 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1595 commandPayload
.expiryDate
= convertToDate(commandPayload
.expiryDate
)!
1596 const { reservationId
, idTag
, connectorId
} = commandPayload
1597 let response
: OCPP16ReserveNowResponse
1599 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1600 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1602 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1603 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1605 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1606 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1608 await removeExpiredReservations(chargingStation
)
1609 switch (chargingStation
.getConnectorStatus(connectorId
)?.status) {
1610 case OCPP16ChargePointStatus
.Faulted
:
1611 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1613 case OCPP16ChargePointStatus
.Preparing
:
1614 case OCPP16ChargePointStatus
.Charging
:
1615 case OCPP16ChargePointStatus
.SuspendedEV
:
1616 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1617 case OCPP16ChargePointStatus
.Finishing
:
1618 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1620 case OCPP16ChargePointStatus
.Unavailable
:
1621 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1623 case OCPP16ChargePointStatus
.Reserved
:
1624 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1625 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1628 // eslint-disable-next-line no-fallthrough
1630 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1631 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1634 await chargingStation
.addReservation({
1635 id
: commandPayload
.reservationId
,
1638 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1643 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1644 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1645 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1646 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1648 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1650 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1655 private async handleRequestCancelReservation (
1656 chargingStation
: ChargingStation
,
1657 commandPayload
: OCPP16CancelReservationRequest
1658 ): Promise
<GenericResponse
> {
1660 !OCPP16ServiceUtils
.checkFeatureProfile(
1662 OCPP16SupportedFeatureProfiles
.Reservation
,
1663 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1666 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1669 const { reservationId
} = commandPayload
1670 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1671 if (reservation
== null) {
1673 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1675 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1677 await chargingStation
.removeReservation(
1679 ReservationTerminationReason
.RESERVATION_CANCELED
1681 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1683 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1684 return this.handleIncomingRequestError
<GenericResponse
>(
1686 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1688 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}