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
)!
704 connectorStatus
?.chargingProfiles
!= null &&
705 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
708 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
710 const currentDate
= new Date()
711 const compositeScheduleInterval
: Interval
= {
713 end
: addSeconds(currentDate
, duration
)
715 // Get charging profiles sorted by connector id then stack level
716 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
720 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
721 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
722 for (const chargingProfile
of chargingProfiles
) {
724 chargingProfile
.chargingSchedule
?.startSchedule
== null &&
725 connectorStatus
?.transactionStarted
=== true
728 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
729 chargingProfile.chargingProfileId
730 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
732 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
733 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
736 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
737 !isDate(chargingProfile
.chargingSchedule
?.startSchedule
)
740 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
741 chargingProfile.chargingProfileId
742 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
744 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
745 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
746 chargingProfile
.chargingSchedule
?.startSchedule
750 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
751 chargingProfile
.chargingSchedule
?.duration
== null
754 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
755 chargingProfile.chargingProfileId
756 } has no duration defined and will be set to the maximum time allowed`
758 // OCPP specifies that if duration is not defined, it should be infinite
759 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
761 chargingProfile
.chargingSchedule
.startSchedule
765 !prepareChargingProfileKind(
768 compositeScheduleInterval
.start
as Date,
769 chargingStation
.logPrefix()
775 !canProceedChargingProfile(
777 compositeScheduleInterval
.start
as Date,
778 chargingStation
.logPrefix()
783 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
784 previousCompositeSchedule
,
785 chargingProfile
.chargingSchedule
,
786 compositeScheduleInterval
788 previousCompositeSchedule
= compositeSchedule
790 if (compositeSchedule
!= null) {
792 status: GenericStatus
.Accepted
,
793 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
794 scheduleStart
: compositeSchedule
.startSchedule
!,
796 chargingSchedule
: compositeSchedule
799 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
802 private handleRequestClearChargingProfile (
803 chargingStation
: ChargingStation
,
804 commandPayload
: OCPP16ClearChargingProfileRequest
805 ): OCPP16ClearChargingProfileResponse
{
807 !OCPP16ServiceUtils
.checkFeatureProfile(
809 OCPP16SupportedFeatureProfiles
.SmartCharging
,
810 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
813 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
815 const { connectorId
} = commandPayload
816 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
817 if (!chargingStation
.hasConnector(connectorId
!)) {
819 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
821 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
823 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
824 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
825 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
826 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
827 connectorStatus
!.chargingProfiles
= []
829 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
831 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
833 if (connectorId
== null) {
834 let clearedCP
= false
835 if (chargingStation
.hasEvses
) {
836 for (const evseStatus
of chargingStation
.evses
.values()) {
837 for (const status of evseStatus
.connectors
.values()) {
838 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
841 status.chargingProfiles
846 for (const id
of chargingStation
.connectors
.keys()) {
847 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
850 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
855 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
858 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
861 private async handleRequestChangeAvailability (
862 chargingStation
: ChargingStation
,
863 commandPayload
: OCPP16ChangeAvailabilityRequest
864 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
865 const { connectorId
, type } = commandPayload
866 if (!chargingStation
.hasConnector(connectorId
)) {
868 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
870 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
872 const chargePointStatus
: OCPP16ChargePointStatus
=
873 type === OCPP16AvailabilityType
.Operative
874 ? OCPP16ChargePointStatus
.Available
875 : OCPP16ChargePointStatus
.Unavailable
876 if (connectorId
=== 0) {
877 let response
: OCPP16ChangeAvailabilityResponse
878 if (chargingStation
.hasEvses
) {
879 for (const evseStatus
of chargingStation
.evses
.values()) {
880 response
= await OCPP16ServiceUtils
.changeAvailability(
882 [...evseStatus
.connectors
.keys()],
888 response
= await OCPP16ServiceUtils
.changeAvailability(
890 [...chargingStation
.connectors
.keys()],
895 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
899 (chargingStation
.isChargingStationAvailable() ||
900 (!chargingStation
.isChargingStationAvailable() &&
901 type === OCPP16AvailabilityType
.Inoperative
))
903 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
904 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
905 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
906 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
908 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
909 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
910 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
915 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
917 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
920 private async handleRequestRemoteStartTransaction (
921 chargingStation
: ChargingStation
,
922 commandPayload
: RemoteStartTransactionRequest
923 ): Promise
<GenericResponse
> {
924 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
925 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
926 return await this.notifyRemoteStartTransactionRejected(
928 transactionConnectorId
,
933 !chargingStation
.isChargingStationAvailable() ||
934 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
936 return await this.notifyRemoteStartTransactionRejected(
938 transactionConnectorId
,
942 const remoteStartTransactionLogMsg
= `
943 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
944 chargingStation.stationInfo.chargingStationId
945 }#${transactionConnectorId} for idTag '${idTag}'`
946 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
948 transactionConnectorId
,
949 OCPP16ChargePointStatus
.Preparing
951 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
952 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!
953 // Authorization check required
955 chargingStation
.getAuthorizeRemoteTxRequests() &&
956 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
958 // Authorization successful, start transaction
960 (chargingProfile
!= null &&
961 this.setRemoteStartTransactionChargingProfile(
963 transactionConnectorId
,
966 chargingProfile
== null
968 connectorStatus
.transactionRemoteStarted
= true
971 await chargingStation
.ocppRequestService
.requestHandler
<
972 OCPP16StartTransactionRequest
,
973 OCPP16StartTransactionResponse
974 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
975 connectorId
: transactionConnectorId
,
978 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
980 logger
.debug(remoteStartTransactionLogMsg
)
981 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
983 return await this.notifyRemoteStartTransactionRejected(
985 transactionConnectorId
,
989 return await this.notifyRemoteStartTransactionRejected(
991 transactionConnectorId
,
995 // No authorization check required, start transaction
997 (chargingProfile
!= null &&
998 this.setRemoteStartTransactionChargingProfile(
1000 transactionConnectorId
,
1003 chargingProfile
== null
1005 connectorStatus
.transactionRemoteStarted
= true
1008 await chargingStation
.ocppRequestService
.requestHandler
<
1009 OCPP16StartTransactionRequest
,
1010 OCPP16StartTransactionResponse
1011 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
1012 connectorId
: transactionConnectorId
,
1015 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
1017 logger
.debug(remoteStartTransactionLogMsg
)
1018 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1020 return await this.notifyRemoteStartTransactionRejected(
1022 transactionConnectorId
,
1026 return await this.notifyRemoteStartTransactionRejected(
1028 transactionConnectorId
,
1033 private async notifyRemoteStartTransactionRejected (
1034 chargingStation
: ChargingStation
,
1035 connectorId
: number,
1037 ): Promise
<GenericResponse
> {
1038 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1039 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1040 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1043 OCPP16ChargePointStatus
.Available
1047 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1049 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1052 private setRemoteStartTransactionChargingProfile (
1053 chargingStation
: ChargingStation
,
1054 connectorId
: number,
1055 chargingProfile
: OCPP16ChargingProfile
1057 if (chargingProfile
?.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1058 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1060 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1066 `${chargingStation.logPrefix()} Not allowed to set ${
1067 chargingProfile.chargingProfilePurpose
1068 } charging profile(s) at remote start transaction`
1073 private async handleRequestRemoteStopTransaction (
1074 chargingStation
: ChargingStation
,
1075 commandPayload
: RemoteStopTransactionRequest
1076 ): Promise
<GenericResponse
> {
1077 const { transactionId
} = commandPayload
1078 if (chargingStation
.hasEvses
) {
1079 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1081 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1082 if (connectorStatus
.transactionId
=== transactionId
) {
1083 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1089 for (const connectorId
of chargingStation
.connectors
.keys()) {
1092 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1094 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1099 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1101 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1104 private handleRequestUpdateFirmware (
1105 chargingStation
: ChargingStation
,
1106 commandPayload
: OCPP16UpdateFirmwareRequest
1107 ): OCPP16UpdateFirmwareResponse
{
1109 !OCPP16ServiceUtils
.checkFeatureProfile(
1111 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1112 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1116 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1118 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1120 let { retrieveDate
} = commandPayload
1122 chargingStation
.stationInfo
.firmwareStatus
!= null &&
1123 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1126 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1128 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1130 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1131 retrieveDate
= convertToDate(retrieveDate
)!
1132 const now
= Date.now()
1133 if (retrieveDate
?.getTime() <= now
) {
1134 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1138 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1140 retrieveDate
?.getTime() - now
1143 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1146 private async updateFirmwareSimulation (
1147 chargingStation
: ChargingStation
,
1151 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1154 if (chargingStation
.hasEvses
) {
1155 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1157 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1158 if (connectorStatus
?.transactionStarted
=== false) {
1159 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1162 OCPP16ChargePointStatus
.Unavailable
1169 for (const connectorId
of chargingStation
.connectors
.keys()) {
1172 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1174 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1177 OCPP16ChargePointStatus
.Unavailable
1182 await chargingStation
.ocppRequestService
.requestHandler
<
1183 OCPP16FirmwareStatusNotificationRequest
,
1184 OCPP16FirmwareStatusNotificationResponse
1185 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1186 status: OCPP16FirmwareStatus
.Downloading
1188 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1190 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1191 OCPP16FirmwareStatus
.DownloadFailed
1193 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1194 await chargingStation
.ocppRequestService
.requestHandler
<
1195 OCPP16FirmwareStatusNotificationRequest
,
1196 OCPP16FirmwareStatusNotificationResponse
1197 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1198 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1200 chargingStation
.stationInfo
.firmwareStatus
=
1201 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1204 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1205 await chargingStation
.ocppRequestService
.requestHandler
<
1206 OCPP16FirmwareStatusNotificationRequest
,
1207 OCPP16FirmwareStatusNotificationResponse
1208 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1209 status: OCPP16FirmwareStatus
.Downloaded
1211 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1212 let wasTransactionsStarted
= false
1213 let transactionsStarted
: boolean
1215 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1216 if (runningTransactions
> 0) {
1217 const waitTime
= secondsToMilliseconds(15)
1219 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1221 )} before continuing firmware update simulation`
1223 await sleep(waitTime
)
1224 transactionsStarted
= true
1225 wasTransactionsStarted
= true
1227 if (chargingStation
.hasEvses
) {
1228 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1230 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1231 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1232 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1235 OCPP16ChargePointStatus
.Unavailable
1242 for (const connectorId
of chargingStation
.connectors
.keys()) {
1245 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1246 OCPP16ChargePointStatus
.Unavailable
1248 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1251 OCPP16ChargePointStatus
.Unavailable
1256 transactionsStarted
= false
1258 } while (transactionsStarted
)
1259 !wasTransactionsStarted
&&
1260 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1261 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1264 await chargingStation
.ocppRequestService
.requestHandler
<
1265 OCPP16FirmwareStatusNotificationRequest
,
1266 OCPP16FirmwareStatusNotificationResponse
1267 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1268 status: OCPP16FirmwareStatus
.Installing
1270 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1272 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1273 OCPP16FirmwareStatus
.InstallationFailed
1275 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1276 await chargingStation
.ocppRequestService
.requestHandler
<
1277 OCPP16FirmwareStatusNotificationRequest
,
1278 OCPP16FirmwareStatusNotificationResponse
1279 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1280 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1282 chargingStation
.stationInfo
.firmwareStatus
=
1283 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1286 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1287 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1288 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1292 private async handleRequestGetDiagnostics (
1293 chargingStation
: ChargingStation
,
1294 commandPayload
: GetDiagnosticsRequest
1295 ): Promise
<GetDiagnosticsResponse
> {
1297 !OCPP16ServiceUtils
.checkFeatureProfile(
1299 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1300 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1304 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1306 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1308 const { location
} = commandPayload
1309 const uri
= new URL(location
)
1310 if (uri
.protocol
.startsWith('ftp:')) {
1311 let ftpClient
: Client
| undefined
1313 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1314 .filter((file
) => file
.endsWith('.log'))
1315 .map((file
) => join('./', file
))
1316 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`
1317 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1318 ftpClient
= new Client()
1319 const accessResponse
= await ftpClient
.access({
1321 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1322 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1323 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1325 let uploadResponse
: FTPResponse
| undefined
1326 if (accessResponse
.code
=== 220) {
1327 ftpClient
.trackProgress((info
) => {
1329 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1331 } bytes transferred from diagnostics archive ${info.name}`
1333 chargingStation
.ocppRequestService
1335 OCPP16DiagnosticsStatusNotificationRequest
,
1336 OCPP16DiagnosticsStatusNotificationResponse
1337 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1338 status: OCPP16DiagnosticsStatus
.Uploading
1342 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1343 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1349 uploadResponse
= await ftpClient
.uploadFrom(
1350 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1351 `${uri.pathname}${diagnosticsArchive}`
1353 if (uploadResponse
.code
=== 226) {
1354 await chargingStation
.ocppRequestService
.requestHandler
<
1355 OCPP16DiagnosticsStatusNotificationRequest
,
1356 OCPP16DiagnosticsStatusNotificationResponse
1357 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1358 status: OCPP16DiagnosticsStatus
.Uploaded
1360 if (ftpClient
!= null) {
1363 return { fileName
: diagnosticsArchive
}
1365 throw new OCPPError(
1366 ErrorType
.GENERIC_ERROR
,
1367 `Diagnostics transfer failed with error code ${accessResponse.code}${
1368 uploadResponse?.code != null && `|${uploadResponse?.code}
`
1370 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1373 throw new OCPPError(
1374 ErrorType
.GENERIC_ERROR
,
1375 `Diagnostics transfer failed with error code ${accessResponse.code}${
1376 uploadResponse?.code != null && `|${uploadResponse?.code}
`
1378 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1381 await chargingStation
.ocppRequestService
.requestHandler
<
1382 OCPP16DiagnosticsStatusNotificationRequest
,
1383 OCPP16DiagnosticsStatusNotificationResponse
1384 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1385 status: OCPP16DiagnosticsStatus
.UploadFailed
1387 if (ftpClient
!= null) {
1390 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1391 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1393 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1395 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1400 `${chargingStation.logPrefix()} Unsupported protocol ${
1402 } to transfer the diagnostic logs archive`
1404 await chargingStation
.ocppRequestService
.requestHandler
<
1405 OCPP16DiagnosticsStatusNotificationRequest
,
1406 OCPP16DiagnosticsStatusNotificationResponse
1407 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1408 status: OCPP16DiagnosticsStatus
.UploadFailed
1410 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1414 private handleRequestTriggerMessage (
1415 chargingStation
: ChargingStation
,
1416 commandPayload
: OCPP16TriggerMessageRequest
1417 ): OCPP16TriggerMessageResponse
{
1418 const { requestedMessage
, connectorId
} = commandPayload
1420 !OCPP16ServiceUtils
.checkFeatureProfile(
1422 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1423 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1425 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1427 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1430 !OCPP16ServiceUtils
.isConnectorIdValid(
1432 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1433 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1437 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1440 switch (requestedMessage
) {
1441 case OCPP16MessageTrigger
.BootNotification
:
1443 chargingStation
.ocppRequestService
1444 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1446 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1447 chargingStation
.bootNotificationRequest
,
1448 { skipBufferingOnError
: true, triggerMessage
: true }
1450 .then((response
) => {
1451 chargingStation
.bootNotificationResponse
= response
1453 .catch(Constants
.EMPTY_FUNCTION
)
1454 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1455 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1456 case OCPP16MessageTrigger
.Heartbeat
:
1458 chargingStation
.ocppRequestService
1459 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1461 OCPP16RequestCommand
.HEARTBEAT
,
1464 triggerMessage
: true
1467 .catch(Constants
.EMPTY_FUNCTION
)
1468 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1469 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1470 case OCPP16MessageTrigger
.StatusNotification
:
1472 if (connectorId
!= null) {
1473 chargingStation
.ocppRequestService
1474 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1476 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1479 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1480 status: chargingStation
.getConnectorStatus(connectorId
)?.status
1483 triggerMessage
: true
1486 .catch(Constants
.EMPTY_FUNCTION
)
1488 if (chargingStation
.hasEvses
) {
1489 for (const evseStatus
of chargingStation
.evses
.values()) {
1490 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1491 chargingStation
.ocppRequestService
1493 OCPP16StatusNotificationRequest
,
1494 OCPP16StatusNotificationResponse
1497 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1500 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1501 status: connectorStatus
.status
1504 triggerMessage
: true
1507 .catch(Constants
.EMPTY_FUNCTION
)
1511 for (const id
of chargingStation
.connectors
.keys()) {
1512 chargingStation
.ocppRequestService
1514 OCPP16StatusNotificationRequest
,
1515 OCPP16StatusNotificationResponse
1518 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1521 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1522 status: chargingStation
.getConnectorStatus(id
)?.status
1525 triggerMessage
: true
1528 .catch(Constants
.EMPTY_FUNCTION
)
1532 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1533 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1535 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1538 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1539 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1541 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1543 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1548 private handleRequestDataTransfer (
1549 chargingStation
: ChargingStation
,
1550 commandPayload
: OCPP16DataTransferRequest
1551 ): OCPP16DataTransferResponse
{
1552 const { vendorId
} = commandPayload
1554 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1555 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1557 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1559 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1560 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1562 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1564 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1569 private async handleRequestReserveNow (
1570 chargingStation
: ChargingStation
,
1571 commandPayload
: OCPP16ReserveNowRequest
1572 ): Promise
<OCPP16ReserveNowResponse
> {
1574 !OCPP16ServiceUtils
.checkFeatureProfile(
1576 OCPP16SupportedFeatureProfiles
.Reservation
,
1577 OCPP16IncomingRequestCommand
.RESERVE_NOW
1580 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1582 const { reservationId
, idTag
, connectorId
} = commandPayload
1583 let response
: OCPP16ReserveNowResponse
1585 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1586 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1588 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1589 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1591 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1592 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1594 await removeExpiredReservations(chargingStation
)
1595 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1596 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1597 case OCPP16ChargePointStatus
.Faulted
:
1598 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1600 case OCPP16ChargePointStatus
.Preparing
:
1601 case OCPP16ChargePointStatus
.Charging
:
1602 case OCPP16ChargePointStatus
.SuspendedEV
:
1603 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1604 case OCPP16ChargePointStatus
.Finishing
:
1605 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1607 case OCPP16ChargePointStatus
.Unavailable
:
1608 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1610 case OCPP16ChargePointStatus
.Reserved
:
1611 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1612 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1615 // eslint-disable-next-line no-fallthrough
1617 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1618 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1621 await chargingStation
.addReservation({
1622 id
: commandPayload
.reservationId
,
1625 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1630 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1631 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1632 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1633 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1635 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1637 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1642 private async handleRequestCancelReservation (
1643 chargingStation
: ChargingStation
,
1644 commandPayload
: OCPP16CancelReservationRequest
1645 ): Promise
<GenericResponse
> {
1647 !OCPP16ServiceUtils
.checkFeatureProfile(
1649 OCPP16SupportedFeatureProfiles
.Reservation
,
1650 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1653 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1656 const { reservationId
} = commandPayload
1657 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1658 if (reservation
== null) {
1660 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1662 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1664 await chargingStation
.removeReservation(
1666 ReservationTerminationReason
.RESERVATION_CANCELED
1668 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1670 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1671 return this.handleIncomingRequestError
<GenericResponse
>(
1673 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1675 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}