1 // Partial Copyright Jerome Benoit. 2021-2023. 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 { JSONSchemaType
} 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 jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonType
>>
117 private readonly incomingRequestHandlers
: Map
<
118 OCPP16IncomingRequestCommand
,
119 IncomingRequestHandler
122 public constructor () {
123 // if (new.target?.name === moduleName) {
124 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`)
126 super(OCPPVersion
.VERSION_16
)
127 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
129 OCPP16IncomingRequestCommand
.RESET
,
130 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
133 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
134 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
137 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
138 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
141 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
142 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
145 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
146 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
149 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
150 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
153 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
154 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
157 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
158 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
161 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
162 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
165 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
166 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
169 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
170 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
173 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
174 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
177 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
178 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
181 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
182 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
185 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
186 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
189 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
190 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
193 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
194 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
197 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonType
>>([
199 OCPP16IncomingRequestCommand
.RESET
,
200 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
201 'assets/json-schemas/ocpp/1.6/Reset.json',
207 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
208 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
209 'assets/json-schemas/ocpp/1.6/ClearCache.json',
215 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
216 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
217 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
223 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
224 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
225 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
231 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
232 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
233 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
239 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
240 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
241 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
247 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
248 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
249 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
255 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
256 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
257 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
263 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
264 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearChargingProfileRequest
>(
265 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
271 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
272 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ChangeAvailabilityRequest
>(
273 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
279 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
280 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
281 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
287 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
288 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
289 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
295 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
296 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
297 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
303 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
304 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
305 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
311 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
312 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
313 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
319 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
320 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
321 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
327 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
328 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
329 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
335 this.validatePayload
= this.validatePayload
.bind(this) as (
336 chargingStation
: ChargingStation
,
337 commandName
: OCPP16IncomingRequestCommand
,
338 commandPayload
: JsonType
342 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
343 chargingStation
: ChargingStation
,
345 commandName
: OCPP16IncomingRequestCommand
,
346 commandPayload
: ReqType
348 let response
: ResType
350 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
351 chargingStation
.inPendingState() &&
352 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
353 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
356 ErrorType
.SECURITY_ERROR
,
357 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
361 )} while the charging station is in pending state on the central server`,
367 chargingStation
.isRegistered() ||
368 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
369 chargingStation
.inUnknownState())
372 this.incomingRequestHandlers
.has(commandName
) &&
373 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
376 this.validatePayload(chargingStation
, commandName
, commandPayload
)
377 // Call the method to build the response
378 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
379 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
386 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
394 ErrorType
.NOT_IMPLEMENTED
,
395 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
406 ErrorType
.SECURITY_ERROR
,
407 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
411 )} while the charging station is not registered on the central server.`,
416 // Send the built response
417 await chargingStation
.ocppRequestService
.sendResponse(
425 private validatePayload (
426 chargingStation
: ChargingStation
,
427 commandName
: OCPP16IncomingRequestCommand
,
428 commandPayload
: JsonType
430 if (this.jsonSchemas
.has(commandName
)) {
431 return this.validateIncomingRequestPayload(
434 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
435 this.jsonSchemas
.get(commandName
)!,
440 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`
445 // Simulate charging station restart
446 private handleRequestReset (
447 chargingStation
: ChargingStation
,
448 commandPayload
: ResetRequest
450 const { type } = commandPayload
452 .reset(`${type}Reset` as OCPP16StopTransactionReason
)
453 .catch(Constants
.EMPTY_FUNCTION
)
455 `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be back online in ${formatDurationMilliSeconds(
456 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
457 chargingStation.stationInfo.resetTime!
460 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
463 private async handleRequestUnlockConnector (
464 chargingStation
: ChargingStation
,
465 commandPayload
: UnlockConnectorRequest
466 ): Promise
<UnlockConnectorResponse
> {
467 const { connectorId
} = commandPayload
468 if (!chargingStation
.hasConnector(connectorId
)) {
470 `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId}`
472 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
474 if (connectorId
=== 0) {
475 logger
.error(`${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId}`)
476 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
478 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
479 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
481 OCPP16StopTransactionReason
.UNLOCK_COMMAND
483 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
484 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
486 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
488 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
491 OCPP16ChargePointStatus
.Available
493 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
496 private handleRequestGetConfiguration (
497 chargingStation
: ChargingStation
,
498 commandPayload
: GetConfigurationRequest
499 ): GetConfigurationResponse
{
500 const { key
} = commandPayload
501 const configurationKey
: OCPPConfigurationKey
[] = []
502 const unknownKey
: string[] = []
504 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
505 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
506 if (configuration
.visible
== null) {
507 configuration
.visible
= true
509 if (!configuration
.visible
) {
512 configurationKey
.push({
513 key
: configuration
.key
,
514 readonly: configuration
.readonly,
515 value
: configuration
.value
518 } else if (isNotEmptyArray(key
)) {
519 for (const k
of key
) {
520 const keyFound
= getConfigurationKey(chargingStation
, k
, true)
521 if (keyFound
!= null) {
522 if (keyFound
.visible
== null) {
523 keyFound
.visible
= true
525 if (!keyFound
.visible
) {
528 configurationKey
.push({
530 readonly: keyFound
.readonly,
531 value
: keyFound
.value
544 private handleRequestChangeConfiguration (
545 chargingStation
: ChargingStation
,
546 commandPayload
: ChangeConfigurationRequest
547 ): ChangeConfigurationResponse
{
548 const { key
, value
} = commandPayload
549 const keyToChange
= getConfigurationKey(chargingStation
, key
, true)
550 if (keyToChange
?.readonly === true) {
551 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
552 } else if (keyToChange
?.readonly === false) {
553 let valueChanged
= false
554 if (keyToChange
.value
!== value
) {
555 setConfigurationKeyValue(chargingStation
, key
, value
, true)
558 let triggerHeartbeatRestart
= false
560 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
561 OCPP16StandardParametersKey
.HeartBeatInterval
&&
564 setConfigurationKeyValue(
566 OCPP16StandardParametersKey
.HeartbeatInterval
,
569 triggerHeartbeatRestart
= true
572 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
573 OCPP16StandardParametersKey
.HeartbeatInterval
&&
576 setConfigurationKeyValue(
578 OCPP16StandardParametersKey
.HeartBeatInterval
,
581 triggerHeartbeatRestart
= true
583 if (triggerHeartbeatRestart
) {
584 chargingStation
.restartHeartbeat()
587 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
588 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
591 chargingStation
.restartWebSocketPing()
593 if (keyToChange
.reboot
=== true) {
594 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
596 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
598 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
601 private handleRequestSetChargingProfile (
602 chargingStation
: ChargingStation
,
603 commandPayload
: SetChargingProfileRequest
604 ): SetChargingProfileResponse
{
606 !OCPP16ServiceUtils
.checkFeatureProfile(
608 OCPP16SupportedFeatureProfiles
.SmartCharging
,
609 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
612 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
614 const { connectorId
, csChargingProfiles
} = commandPayload
615 if (!chargingStation
.hasConnector(connectorId
)) {
617 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${connectorId}`
619 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
622 csChargingProfiles
.chargingProfilePurpose
===
623 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
626 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
629 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
633 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId}`
635 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
637 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
639 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
641 connectorStatus
?.transactionStarted
=== false
644 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} without a started transaction`
646 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
649 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
651 connectorStatus
?.transactionStarted
=== true &&
652 csChargingProfiles
.transactionId
!== connectorStatus
?.transactionId
655 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} with a different transaction id ${
656 csChargingProfiles.transactionId
657 } than the started transaction id ${connectorStatus?.transactionId}`
659 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
661 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
)
663 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
666 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
669 private handleRequestGetCompositeSchedule (
670 chargingStation
: ChargingStation
,
671 commandPayload
: OCPP16GetCompositeScheduleRequest
672 ): OCPP16GetCompositeScheduleResponse
{
674 !OCPP16ServiceUtils
.checkFeatureProfile(
676 OCPP16SupportedFeatureProfiles
.SmartCharging
,
677 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
680 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
682 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
683 if (!chargingStation
.hasConnector(connectorId
)) {
685 `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${connectorId}`
687 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
689 if (connectorId
=== 0) {
691 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`
693 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
695 if (chargingRateUnit
!= null) {
697 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`
700 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
701 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)!
703 isEmptyArray(connectorStatus
?.chargingProfiles
) &&
704 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
706 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
708 const currentDate
= new Date()
709 const compositeScheduleInterval
: Interval
= {
711 end
: addSeconds(currentDate
, duration
)
713 // Get charging profiles sorted by connector id then stack level
714 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
718 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
719 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
720 for (const chargingProfile
of chargingProfiles
) {
722 chargingProfile
.chargingSchedule
?.startSchedule
== null &&
723 connectorStatus
?.transactionStarted
=== true
726 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
727 chargingProfile.chargingProfileId
728 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
730 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
731 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
734 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
735 !isDate(chargingProfile
.chargingSchedule
?.startSchedule
)
738 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
739 chargingProfile.chargingProfileId
740 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
742 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
743 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
744 chargingProfile
.chargingSchedule
?.startSchedule
748 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
749 chargingProfile
.chargingSchedule
?.duration
== null
752 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
753 chargingProfile.chargingProfileId
754 } has no duration defined and will be set to the maximum time allowed`
756 // OCPP specifies that if duration is not defined, it should be infinite
757 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
759 chargingProfile
.chargingSchedule
.startSchedule
763 !prepareChargingProfileKind(
766 compositeScheduleInterval
.start
as Date,
767 chargingStation
.logPrefix()
773 !canProceedChargingProfile(
775 compositeScheduleInterval
.start
as Date,
776 chargingStation
.logPrefix()
781 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
782 previousCompositeSchedule
,
783 chargingProfile
.chargingSchedule
,
784 compositeScheduleInterval
786 previousCompositeSchedule
= compositeSchedule
788 if (compositeSchedule
!= null) {
790 status: GenericStatus
.Accepted
,
791 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
792 scheduleStart
: compositeSchedule
.startSchedule
!,
794 chargingSchedule
: compositeSchedule
797 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
800 private handleRequestClearChargingProfile (
801 chargingStation
: ChargingStation
,
802 commandPayload
: OCPP16ClearChargingProfileRequest
803 ): OCPP16ClearChargingProfileResponse
{
805 !OCPP16ServiceUtils
.checkFeatureProfile(
807 OCPP16SupportedFeatureProfiles
.SmartCharging
,
808 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
811 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
813 const { connectorId
} = commandPayload
814 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
815 if (!chargingStation
.hasConnector(connectorId
!)) {
817 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
819 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
821 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
822 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
823 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
824 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
825 connectorStatus
!.chargingProfiles
= []
827 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
829 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
831 if (connectorId
== null) {
832 let clearedCP
= false
833 if (chargingStation
.hasEvses
) {
834 for (const evseStatus
of chargingStation
.evses
.values()) {
835 for (const status of evseStatus
.connectors
.values()) {
836 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
839 status.chargingProfiles
844 for (const id
of chargingStation
.connectors
.keys()) {
845 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
848 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
853 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
856 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
859 private async handleRequestChangeAvailability (
860 chargingStation
: ChargingStation
,
861 commandPayload
: OCPP16ChangeAvailabilityRequest
862 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
863 const { connectorId
, type } = commandPayload
864 if (!chargingStation
.hasConnector(connectorId
)) {
866 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
868 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
870 const chargePointStatus
: OCPP16ChargePointStatus
=
871 type === OCPP16AvailabilityType
.Operative
872 ? OCPP16ChargePointStatus
.Available
873 : OCPP16ChargePointStatus
.Unavailable
874 if (connectorId
=== 0) {
875 let response
: OCPP16ChangeAvailabilityResponse
876 if (chargingStation
.hasEvses
) {
877 for (const evseStatus
of chargingStation
.evses
.values()) {
878 response
= await OCPP16ServiceUtils
.changeAvailability(
880 [...evseStatus
.connectors
.keys()],
886 response
= await OCPP16ServiceUtils
.changeAvailability(
888 [...chargingStation
.connectors
.keys()],
893 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
897 (chargingStation
.isChargingStationAvailable() ||
898 (!chargingStation
.isChargingStationAvailable() &&
899 type === OCPP16AvailabilityType
.Inoperative
))
901 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
902 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
903 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
904 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
906 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
907 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
908 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
913 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
915 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
918 private async handleRequestRemoteStartTransaction (
919 chargingStation
: ChargingStation
,
920 commandPayload
: RemoteStartTransactionRequest
921 ): Promise
<GenericResponse
> {
922 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
923 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
924 return await this.notifyRemoteStartTransactionRejected(
926 transactionConnectorId
,
931 !chargingStation
.isChargingStationAvailable() ||
932 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
934 return await this.notifyRemoteStartTransactionRejected(
936 transactionConnectorId
,
940 const remoteStartTransactionLogMsg
= `
941 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
942 chargingStation.stationInfo.chargingStationId
943 }#${transactionConnectorId} for idTag '${idTag}'`
944 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
946 transactionConnectorId
,
947 OCPP16ChargePointStatus
.Preparing
949 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
950 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!
951 // Authorization check required
953 chargingStation
.getAuthorizeRemoteTxRequests() &&
954 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
956 // Authorization successful, start transaction
958 (chargingProfile
!= null &&
959 this.setRemoteStartTransactionChargingProfile(
961 transactionConnectorId
,
964 chargingProfile
== null
966 connectorStatus
.transactionRemoteStarted
= true
969 await chargingStation
.ocppRequestService
.requestHandler
<
970 OCPP16StartTransactionRequest
,
971 OCPP16StartTransactionResponse
972 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
973 connectorId
: transactionConnectorId
,
976 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
978 logger
.debug(remoteStartTransactionLogMsg
)
979 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
981 return await this.notifyRemoteStartTransactionRejected(
983 transactionConnectorId
,
987 return await this.notifyRemoteStartTransactionRejected(
989 transactionConnectorId
,
993 // No authorization check required, start transaction
995 (chargingProfile
!= null &&
996 this.setRemoteStartTransactionChargingProfile(
998 transactionConnectorId
,
1001 chargingProfile
== null
1003 connectorStatus
.transactionRemoteStarted
= true
1006 await chargingStation
.ocppRequestService
.requestHandler
<
1007 OCPP16StartTransactionRequest
,
1008 OCPP16StartTransactionResponse
1009 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
1010 connectorId
: transactionConnectorId
,
1013 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
1015 logger
.debug(remoteStartTransactionLogMsg
)
1016 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1018 return await this.notifyRemoteStartTransactionRejected(
1020 transactionConnectorId
,
1024 return await this.notifyRemoteStartTransactionRejected(
1026 transactionConnectorId
,
1031 private async notifyRemoteStartTransactionRejected (
1032 chargingStation
: ChargingStation
,
1033 connectorId
: number,
1035 ): Promise
<GenericResponse
> {
1036 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1037 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1038 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1041 OCPP16ChargePointStatus
.Available
1045 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1047 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1050 private setRemoteStartTransactionChargingProfile (
1051 chargingStation
: ChargingStation
,
1052 connectorId
: number,
1053 chargingProfile
: OCPP16ChargingProfile
1055 if (chargingProfile
?.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1056 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1058 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1064 `${chargingStation.logPrefix()} Not allowed to set ${
1065 chargingProfile.chargingProfilePurpose
1066 } charging profile(s) at remote start transaction`
1071 private async handleRequestRemoteStopTransaction (
1072 chargingStation
: ChargingStation
,
1073 commandPayload
: RemoteStopTransactionRequest
1074 ): Promise
<GenericResponse
> {
1075 const { transactionId
} = commandPayload
1076 if (chargingStation
.hasEvses
) {
1077 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1079 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1080 if (connectorStatus
.transactionId
=== transactionId
) {
1081 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1087 for (const connectorId
of chargingStation
.connectors
.keys()) {
1090 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1092 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1097 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1099 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1102 private handleRequestUpdateFirmware (
1103 chargingStation
: ChargingStation
,
1104 commandPayload
: OCPP16UpdateFirmwareRequest
1105 ): OCPP16UpdateFirmwareResponse
{
1107 !OCPP16ServiceUtils
.checkFeatureProfile(
1109 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1110 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1114 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1116 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1118 let { retrieveDate
} = commandPayload
1120 chargingStation
.stationInfo
.firmwareStatus
!= null &&
1121 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1124 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1126 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1128 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1129 retrieveDate
= convertToDate(retrieveDate
)!
1130 const now
= Date.now()
1131 if (retrieveDate
?.getTime() <= now
) {
1132 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1136 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1138 retrieveDate
?.getTime() - now
1141 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1144 private async updateFirmwareSimulation (
1145 chargingStation
: ChargingStation
,
1149 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1152 if (chargingStation
.hasEvses
) {
1153 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1155 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1156 if (connectorStatus
?.transactionStarted
=== false) {
1157 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1160 OCPP16ChargePointStatus
.Unavailable
1167 for (const connectorId
of chargingStation
.connectors
.keys()) {
1170 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1172 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1175 OCPP16ChargePointStatus
.Unavailable
1180 await chargingStation
.ocppRequestService
.requestHandler
<
1181 OCPP16FirmwareStatusNotificationRequest
,
1182 OCPP16FirmwareStatusNotificationResponse
1183 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1184 status: OCPP16FirmwareStatus
.Downloading
1186 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1188 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1189 OCPP16FirmwareStatus
.DownloadFailed
1191 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1192 await chargingStation
.ocppRequestService
.requestHandler
<
1193 OCPP16FirmwareStatusNotificationRequest
,
1194 OCPP16FirmwareStatusNotificationResponse
1195 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1196 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1198 chargingStation
.stationInfo
.firmwareStatus
=
1199 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1202 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1203 await chargingStation
.ocppRequestService
.requestHandler
<
1204 OCPP16FirmwareStatusNotificationRequest
,
1205 OCPP16FirmwareStatusNotificationResponse
1206 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1207 status: OCPP16FirmwareStatus
.Downloaded
1209 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1210 let wasTransactionsStarted
= false
1211 let transactionsStarted
: boolean
1213 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1214 if (runningTransactions
> 0) {
1215 const waitTime
= secondsToMilliseconds(15)
1217 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1219 )} before continuing firmware update simulation`
1221 await sleep(waitTime
)
1222 transactionsStarted
= true
1223 wasTransactionsStarted
= true
1225 if (chargingStation
.hasEvses
) {
1226 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1228 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1229 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1230 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1233 OCPP16ChargePointStatus
.Unavailable
1240 for (const connectorId
of chargingStation
.connectors
.keys()) {
1243 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1244 OCPP16ChargePointStatus
.Unavailable
1246 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1249 OCPP16ChargePointStatus
.Unavailable
1254 transactionsStarted
= false
1256 } while (transactionsStarted
)
1257 !wasTransactionsStarted
&&
1258 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1259 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1262 await chargingStation
.ocppRequestService
.requestHandler
<
1263 OCPP16FirmwareStatusNotificationRequest
,
1264 OCPP16FirmwareStatusNotificationResponse
1265 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1266 status: OCPP16FirmwareStatus
.Installing
1268 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1270 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1271 OCPP16FirmwareStatus
.InstallationFailed
1273 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1274 await chargingStation
.ocppRequestService
.requestHandler
<
1275 OCPP16FirmwareStatusNotificationRequest
,
1276 OCPP16FirmwareStatusNotificationResponse
1277 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1278 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1280 chargingStation
.stationInfo
.firmwareStatus
=
1281 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1284 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1285 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1286 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1290 private async handleRequestGetDiagnostics (
1291 chargingStation
: ChargingStation
,
1292 commandPayload
: GetDiagnosticsRequest
1293 ): Promise
<GetDiagnosticsResponse
> {
1295 !OCPP16ServiceUtils
.checkFeatureProfile(
1297 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1298 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1302 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1304 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1306 const { location
} = commandPayload
1307 const uri
= new URL(location
)
1308 if (uri
.protocol
.startsWith('ftp:')) {
1309 let ftpClient
: Client
| undefined
1311 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1312 .filter((file
) => file
.endsWith('.log'))
1313 .map((file
) => join('./', file
))
1314 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`
1315 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1316 ftpClient
= new Client()
1317 const accessResponse
= await ftpClient
.access({
1319 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1320 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1321 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1323 let uploadResponse
: FTPResponse
| undefined
1324 if (accessResponse
.code
=== 220) {
1325 ftpClient
.trackProgress((info
) => {
1327 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1329 } bytes transferred from diagnostics archive ${info.name}`
1331 chargingStation
.ocppRequestService
1333 OCPP16DiagnosticsStatusNotificationRequest
,
1334 OCPP16DiagnosticsStatusNotificationResponse
1335 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1336 status: OCPP16DiagnosticsStatus
.Uploading
1340 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1341 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1347 uploadResponse
= await ftpClient
.uploadFrom(
1348 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1349 `${uri.pathname}${diagnosticsArchive}`
1351 if (uploadResponse
.code
=== 226) {
1352 await chargingStation
.ocppRequestService
.requestHandler
<
1353 OCPP16DiagnosticsStatusNotificationRequest
,
1354 OCPP16DiagnosticsStatusNotificationResponse
1355 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1356 status: OCPP16DiagnosticsStatus
.Uploaded
1358 if (ftpClient
!= null) {
1361 return { fileName
: diagnosticsArchive
}
1363 throw new OCPPError(
1364 ErrorType
.GENERIC_ERROR
,
1365 `Diagnostics transfer failed with error code ${accessResponse.code}${
1366 uploadResponse?.code != null && `|${uploadResponse?.code}
`
1368 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1371 throw new OCPPError(
1372 ErrorType
.GENERIC_ERROR
,
1373 `Diagnostics transfer failed with error code ${accessResponse.code}${
1374 uploadResponse?.code != null && `|${uploadResponse?.code}
`
1376 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1379 await chargingStation
.ocppRequestService
.requestHandler
<
1380 OCPP16DiagnosticsStatusNotificationRequest
,
1381 OCPP16DiagnosticsStatusNotificationResponse
1382 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1383 status: OCPP16DiagnosticsStatus
.UploadFailed
1385 if (ftpClient
!= null) {
1388 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1389 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1391 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1393 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1398 `${chargingStation.logPrefix()} Unsupported protocol ${
1400 } to transfer the diagnostic logs archive`
1402 await chargingStation
.ocppRequestService
.requestHandler
<
1403 OCPP16DiagnosticsStatusNotificationRequest
,
1404 OCPP16DiagnosticsStatusNotificationResponse
1405 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1406 status: OCPP16DiagnosticsStatus
.UploadFailed
1408 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1412 private handleRequestTriggerMessage (
1413 chargingStation
: ChargingStation
,
1414 commandPayload
: OCPP16TriggerMessageRequest
1415 ): OCPP16TriggerMessageResponse
{
1416 const { requestedMessage
, connectorId
} = commandPayload
1418 !OCPP16ServiceUtils
.checkFeatureProfile(
1420 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1421 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1423 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1425 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1428 !OCPP16ServiceUtils
.isConnectorIdValid(
1430 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1431 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1435 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1438 switch (requestedMessage
) {
1439 case OCPP16MessageTrigger
.BootNotification
:
1441 chargingStation
.ocppRequestService
1442 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1444 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1445 chargingStation
.bootNotificationRequest
,
1446 { skipBufferingOnError
: true, triggerMessage
: true }
1448 .then((response
) => {
1449 chargingStation
.bootNotificationResponse
= response
1451 .catch(Constants
.EMPTY_FUNCTION
)
1452 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1453 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1454 case OCPP16MessageTrigger
.Heartbeat
:
1456 chargingStation
.ocppRequestService
1457 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1459 OCPP16RequestCommand
.HEARTBEAT
,
1462 triggerMessage
: true
1465 .catch(Constants
.EMPTY_FUNCTION
)
1466 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1467 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1468 case OCPP16MessageTrigger
.StatusNotification
:
1470 if (connectorId
!= null) {
1471 chargingStation
.ocppRequestService
1472 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1474 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1477 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1478 status: chargingStation
.getConnectorStatus(connectorId
)?.status
1481 triggerMessage
: true
1484 .catch(Constants
.EMPTY_FUNCTION
)
1486 if (chargingStation
.hasEvses
) {
1487 for (const evseStatus
of chargingStation
.evses
.values()) {
1488 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1489 chargingStation
.ocppRequestService
1491 OCPP16StatusNotificationRequest
,
1492 OCPP16StatusNotificationResponse
1495 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1498 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1499 status: connectorStatus
.status
1502 triggerMessage
: true
1505 .catch(Constants
.EMPTY_FUNCTION
)
1509 for (const id
of chargingStation
.connectors
.keys()) {
1510 chargingStation
.ocppRequestService
1512 OCPP16StatusNotificationRequest
,
1513 OCPP16StatusNotificationResponse
1516 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1519 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1520 status: chargingStation
.getConnectorStatus(id
)?.status
1523 triggerMessage
: true
1526 .catch(Constants
.EMPTY_FUNCTION
)
1530 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1531 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1533 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1536 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1537 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1539 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1541 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1546 private handleRequestDataTransfer (
1547 chargingStation
: ChargingStation
,
1548 commandPayload
: OCPP16DataTransferRequest
1549 ): OCPP16DataTransferResponse
{
1550 const { vendorId
} = commandPayload
1552 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1553 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1555 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1557 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1558 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1560 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1562 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1567 private async handleRequestReserveNow (
1568 chargingStation
: ChargingStation
,
1569 commandPayload
: OCPP16ReserveNowRequest
1570 ): Promise
<OCPP16ReserveNowResponse
> {
1572 !OCPP16ServiceUtils
.checkFeatureProfile(
1574 OCPP16SupportedFeatureProfiles
.Reservation
,
1575 OCPP16IncomingRequestCommand
.RESERVE_NOW
1578 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1580 const { reservationId
, idTag
, connectorId
} = commandPayload
1581 let response
: OCPP16ReserveNowResponse
1583 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1584 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1586 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1587 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1589 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1590 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1592 await removeExpiredReservations(chargingStation
)
1593 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1594 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1595 case OCPP16ChargePointStatus
.Faulted
:
1596 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1598 case OCPP16ChargePointStatus
.Preparing
:
1599 case OCPP16ChargePointStatus
.Charging
:
1600 case OCPP16ChargePointStatus
.SuspendedEV
:
1601 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1602 case OCPP16ChargePointStatus
.Finishing
:
1603 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1605 case OCPP16ChargePointStatus
.Unavailable
:
1606 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1608 case OCPP16ChargePointStatus
.Reserved
:
1609 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1610 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1613 // eslint-disable-next-line no-fallthrough
1615 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1616 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1619 await chargingStation
.addReservation({
1620 id
: commandPayload
.reservationId
,
1623 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1628 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1629 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1630 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1631 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1633 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1635 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1640 private async handleRequestCancelReservation (
1641 chargingStation
: ChargingStation
,
1642 commandPayload
: OCPP16CancelReservationRequest
1643 ): Promise
<GenericResponse
> {
1645 !OCPP16ServiceUtils
.checkFeatureProfile(
1647 OCPP16SupportedFeatureProfiles
.Reservation
,
1648 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1651 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1654 const { reservationId
} = commandPayload
1655 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1656 if (reservation
== null) {
1658 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1660 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1662 await chargingStation
.removeReservation(
1664 ReservationTerminationReason
.RESERVATION_CANCELED
1666 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1668 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1669 return this.handleIncomingRequestError
<GenericResponse
>(
1671 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1673 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}