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 configKey
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
506 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(configKey
)) {
509 configurationKey
.push({
511 readonly: configKey
.readonly,
512 value
: configKey
.value
515 } else if (isNotEmptyArray(key
)) {
516 for (const k
of key
) {
517 const keyFound
= getConfigurationKey(chargingStation
, k
, true)
518 if (keyFound
!= null) {
519 if (!OCPP16ServiceUtils
.isConfigurationKeyVisible(keyFound
)) {
522 configurationKey
.push({
524 readonly: keyFound
.readonly,
525 value
: keyFound
.value
538 private handleRequestChangeConfiguration (
539 chargingStation
: ChargingStation
,
540 commandPayload
: ChangeConfigurationRequest
541 ): ChangeConfigurationResponse
{
542 const { key
, value
} = commandPayload
543 const keyToChange
= getConfigurationKey(chargingStation
, key
, true)
544 if (keyToChange
?.readonly === true) {
545 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
546 } else if (keyToChange
?.readonly === false) {
547 let valueChanged
= false
548 if (keyToChange
.value
!== value
) {
549 setConfigurationKeyValue(chargingStation
, key
, value
, true)
552 let triggerHeartbeatRestart
= false
554 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
555 OCPP16StandardParametersKey
.HeartBeatInterval
&&
558 setConfigurationKeyValue(
560 OCPP16StandardParametersKey
.HeartbeatInterval
,
563 triggerHeartbeatRestart
= true
566 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
567 OCPP16StandardParametersKey
.HeartbeatInterval
&&
570 setConfigurationKeyValue(
572 OCPP16StandardParametersKey
.HeartBeatInterval
,
575 triggerHeartbeatRestart
= true
577 if (triggerHeartbeatRestart
) {
578 chargingStation
.restartHeartbeat()
581 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
582 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
585 chargingStation
.restartWebSocketPing()
587 if (keyToChange
.reboot
=== true) {
588 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
590 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
592 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
595 private handleRequestSetChargingProfile (
596 chargingStation
: ChargingStation
,
597 commandPayload
: SetChargingProfileRequest
598 ): SetChargingProfileResponse
{
600 !OCPP16ServiceUtils
.checkFeatureProfile(
602 OCPP16SupportedFeatureProfiles
.SmartCharging
,
603 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
606 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
608 const { connectorId
, csChargingProfiles
} = commandPayload
609 if (!chargingStation
.hasConnector(connectorId
)) {
611 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${connectorId}`
613 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
616 csChargingProfiles
.chargingProfilePurpose
===
617 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
620 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
623 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
627 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId}`
629 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
631 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
633 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
635 connectorStatus
?.transactionStarted
=== false
638 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} without a started transaction`
640 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
643 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
645 connectorStatus
?.transactionStarted
=== true &&
646 csChargingProfiles
.transactionId
!== connectorStatus
?.transactionId
649 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${connectorId} with a different transaction id ${
650 csChargingProfiles.transactionId
651 } than the started transaction id ${connectorStatus?.transactionId}`
653 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
655 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
)
657 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
660 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
663 private handleRequestGetCompositeSchedule (
664 chargingStation
: ChargingStation
,
665 commandPayload
: OCPP16GetCompositeScheduleRequest
666 ): OCPP16GetCompositeScheduleResponse
{
668 !OCPP16ServiceUtils
.checkFeatureProfile(
670 OCPP16SupportedFeatureProfiles
.SmartCharging
,
671 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
674 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
676 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
677 if (!chargingStation
.hasConnector(connectorId
)) {
679 `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${connectorId}`
681 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
683 if (connectorId
=== 0) {
685 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`
687 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
689 if (chargingRateUnit
!= null) {
691 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`
694 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
695 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)!
697 isEmptyArray(connectorStatus
?.chargingProfiles
) &&
698 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
)
700 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
702 const currentDate
= new Date()
703 const compositeScheduleInterval
: Interval
= {
705 end
: addSeconds(currentDate
, duration
)
707 // Get charging profiles sorted by connector id then stack level
708 const chargingProfiles
: OCPP16ChargingProfile
[] = getConnectorChargingProfiles(
712 let previousCompositeSchedule
: OCPP16ChargingSchedule
| undefined
713 let compositeSchedule
: OCPP16ChargingSchedule
| undefined
714 for (const chargingProfile
of chargingProfiles
) {
716 chargingProfile
.chargingSchedule
?.startSchedule
== null &&
717 connectorStatus
?.transactionStarted
=== true
720 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
721 chargingProfile.chargingProfileId
722 } has no startSchedule defined. Trying to set it to the connector current transaction start date`
724 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
725 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
728 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
729 !isDate(chargingProfile
.chargingSchedule
?.startSchedule
)
732 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
733 chargingProfile.chargingProfileId
734 } startSchedule property is not a Date instance. Trying to convert it to a Date instance`
736 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
737 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
738 chargingProfile
.chargingSchedule
?.startSchedule
742 chargingProfile
.chargingSchedule
?.startSchedule
!= null &&
743 chargingProfile
.chargingSchedule
?.duration
== null
746 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
747 chargingProfile.chargingProfileId
748 } has no duration defined and will be set to the maximum time allowed`
750 // OCPP specifies that if duration is not defined, it should be infinite
751 chargingProfile
.chargingSchedule
.duration
= differenceInSeconds(
753 chargingProfile
.chargingSchedule
.startSchedule
757 !prepareChargingProfileKind(
760 compositeScheduleInterval
.start
as Date,
761 chargingStation
.logPrefix()
767 !canProceedChargingProfile(
769 compositeScheduleInterval
.start
as Date,
770 chargingStation
.logPrefix()
775 compositeSchedule
= OCPP16ServiceUtils
.composeChargingSchedules(
776 previousCompositeSchedule
,
777 chargingProfile
.chargingSchedule
,
778 compositeScheduleInterval
780 previousCompositeSchedule
= compositeSchedule
782 if (compositeSchedule
!= null) {
784 status: GenericStatus
.Accepted
,
785 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
786 scheduleStart
: compositeSchedule
.startSchedule
!,
788 chargingSchedule
: compositeSchedule
791 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
794 private handleRequestClearChargingProfile (
795 chargingStation
: ChargingStation
,
796 commandPayload
: OCPP16ClearChargingProfileRequest
797 ): OCPP16ClearChargingProfileResponse
{
799 !OCPP16ServiceUtils
.checkFeatureProfile(
801 OCPP16SupportedFeatureProfiles
.SmartCharging
,
802 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
805 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
807 const { connectorId
} = commandPayload
808 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
809 if (!chargingStation
.hasConnector(connectorId
!)) {
811 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}`
813 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
815 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
816 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
!)
817 if (connectorId
!= null && isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
818 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
819 connectorStatus
!.chargingProfiles
= []
821 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`
823 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
825 if (connectorId
== null) {
826 let clearedCP
= false
827 if (chargingStation
.hasEvses
) {
828 for (const evseStatus
of chargingStation
.evses
.values()) {
829 for (const status of evseStatus
.connectors
.values()) {
830 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
833 status.chargingProfiles
838 for (const id
of chargingStation
.connectors
.keys()) {
839 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
842 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
847 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
850 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
853 private async handleRequestChangeAvailability (
854 chargingStation
: ChargingStation
,
855 commandPayload
: OCPP16ChangeAvailabilityRequest
856 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
857 const { connectorId
, type } = commandPayload
858 if (!chargingStation
.hasConnector(connectorId
)) {
860 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId}`
862 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
864 const chargePointStatus
: OCPP16ChargePointStatus
=
865 type === OCPP16AvailabilityType
.Operative
866 ? OCPP16ChargePointStatus
.Available
867 : OCPP16ChargePointStatus
.Unavailable
868 if (connectorId
=== 0) {
869 let response
: OCPP16ChangeAvailabilityResponse
870 if (chargingStation
.hasEvses
) {
871 for (const evseStatus
of chargingStation
.evses
.values()) {
872 response
= await OCPP16ServiceUtils
.changeAvailability(
874 [...evseStatus
.connectors
.keys()],
880 response
= await OCPP16ServiceUtils
.changeAvailability(
882 [...chargingStation
.connectors
.keys()],
887 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
891 (chargingStation
.isChargingStationAvailable() ||
892 (!chargingStation
.isChargingStationAvailable() &&
893 type === OCPP16AvailabilityType
.Inoperative
))
895 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
896 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
897 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
898 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
900 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
901 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type
902 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
907 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
909 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
912 private async handleRequestRemoteStartTransaction (
913 chargingStation
: ChargingStation
,
914 commandPayload
: RemoteStartTransactionRequest
915 ): Promise
<GenericResponse
> {
916 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
917 if (!chargingStation
.hasConnector(transactionConnectorId
)) {
918 return await this.notifyRemoteStartTransactionRejected(
920 transactionConnectorId
,
925 !chargingStation
.isChargingStationAvailable() ||
926 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
928 return await this.notifyRemoteStartTransactionRejected(
930 transactionConnectorId
,
934 const remoteStartTransactionLogMsg
= `
935 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
936 chargingStation.stationInfo.chargingStationId
937 }#${transactionConnectorId} for idTag '${idTag}'`
938 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
940 transactionConnectorId
,
941 OCPP16ChargePointStatus
.Preparing
943 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
944 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!
945 // Authorization check required
947 chargingStation
.getAuthorizeRemoteTxRequests() &&
948 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
950 // Authorization successful, start transaction
952 (chargingProfile
!= null &&
953 this.setRemoteStartTransactionChargingProfile(
955 transactionConnectorId
,
958 chargingProfile
== null
960 connectorStatus
.transactionRemoteStarted
= true
963 await chargingStation
.ocppRequestService
.requestHandler
<
964 OCPP16StartTransactionRequest
,
965 OCPP16StartTransactionResponse
966 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
967 connectorId
: transactionConnectorId
,
970 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
972 logger
.debug(remoteStartTransactionLogMsg
)
973 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
975 return await this.notifyRemoteStartTransactionRejected(
977 transactionConnectorId
,
981 return await this.notifyRemoteStartTransactionRejected(
983 transactionConnectorId
,
987 // No authorization check required, start transaction
989 (chargingProfile
!= null &&
990 this.setRemoteStartTransactionChargingProfile(
992 transactionConnectorId
,
995 chargingProfile
== null
997 connectorStatus
.transactionRemoteStarted
= true
1000 await chargingStation
.ocppRequestService
.requestHandler
<
1001 OCPP16StartTransactionRequest
,
1002 OCPP16StartTransactionResponse
1003 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
1004 connectorId
: transactionConnectorId
,
1007 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
1009 logger
.debug(remoteStartTransactionLogMsg
)
1010 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
1012 return await this.notifyRemoteStartTransactionRejected(
1014 transactionConnectorId
,
1018 return await this.notifyRemoteStartTransactionRejected(
1020 transactionConnectorId
,
1025 private async notifyRemoteStartTransactionRejected (
1026 chargingStation
: ChargingStation
,
1027 connectorId
: number,
1029 ): Promise
<GenericResponse
> {
1030 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)
1031 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Available
) {
1032 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1035 OCPP16ChargePointStatus
.Available
1039 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
1041 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1044 private setRemoteStartTransactionChargingProfile (
1045 chargingStation
: ChargingStation
,
1046 connectorId
: number,
1047 chargingProfile
: OCPP16ChargingProfile
1049 if (chargingProfile
?.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1050 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
)
1052 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`,
1058 `${chargingStation.logPrefix()} Not allowed to set ${
1059 chargingProfile.chargingProfilePurpose
1060 } charging profile(s) at remote start transaction`
1065 private async handleRequestRemoteStopTransaction (
1066 chargingStation
: ChargingStation
,
1067 commandPayload
: RemoteStopTransactionRequest
1068 ): Promise
<GenericResponse
> {
1069 const { transactionId
} = commandPayload
1070 if (chargingStation
.hasEvses
) {
1071 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1073 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1074 if (connectorStatus
.transactionId
=== transactionId
) {
1075 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1081 for (const connectorId
of chargingStation
.connectors
.keys()) {
1084 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1086 return await OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
)
1091 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
1093 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
1096 private handleRequestUpdateFirmware (
1097 chargingStation
: ChargingStation
,
1098 commandPayload
: OCPP16UpdateFirmwareRequest
1099 ): OCPP16UpdateFirmwareResponse
{
1101 !OCPP16ServiceUtils
.checkFeatureProfile(
1103 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1104 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1108 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: feature profile not supported`
1110 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1112 let { retrieveDate
} = commandPayload
1114 chargingStation
.stationInfo
.firmwareStatus
!= null &&
1115 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1118 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress`
1120 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1122 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1123 retrieveDate
= convertToDate(retrieveDate
)!
1124 const now
= Date.now()
1125 if (retrieveDate
?.getTime() <= now
) {
1126 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1130 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
)
1132 retrieveDate
?.getTime() - now
1135 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1138 private async updateFirmwareSimulation (
1139 chargingStation
: ChargingStation
,
1143 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1146 if (chargingStation
.hasEvses
) {
1147 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1149 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1150 if (connectorStatus
?.transactionStarted
=== false) {
1151 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1154 OCPP16ChargePointStatus
.Unavailable
1161 for (const connectorId
of chargingStation
.connectors
.keys()) {
1164 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1166 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1169 OCPP16ChargePointStatus
.Unavailable
1174 await chargingStation
.ocppRequestService
.requestHandler
<
1175 OCPP16FirmwareStatusNotificationRequest
,
1176 OCPP16FirmwareStatusNotificationResponse
1177 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1178 status: OCPP16FirmwareStatus
.Downloading
1180 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
1182 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1183 OCPP16FirmwareStatus
.DownloadFailed
1185 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1186 await chargingStation
.ocppRequestService
.requestHandler
<
1187 OCPP16FirmwareStatusNotificationRequest
,
1188 OCPP16FirmwareStatusNotificationResponse
1189 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1190 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1192 chargingStation
.stationInfo
.firmwareStatus
=
1193 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1196 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1197 await chargingStation
.ocppRequestService
.requestHandler
<
1198 OCPP16FirmwareStatusNotificationRequest
,
1199 OCPP16FirmwareStatusNotificationResponse
1200 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1201 status: OCPP16FirmwareStatus
.Downloaded
1203 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
1204 let wasTransactionsStarted
= false
1205 let transactionsStarted
: boolean
1207 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions()
1208 if (runningTransactions
> 0) {
1209 const waitTime
= secondsToMilliseconds(15)
1211 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation: ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1213 )} before continuing firmware update simulation`
1215 await sleep(waitTime
)
1216 transactionsStarted
= true
1217 wasTransactionsStarted
= true
1219 if (chargingStation
.hasEvses
) {
1220 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1222 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1223 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1224 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1227 OCPP16ChargePointStatus
.Unavailable
1234 for (const connectorId
of chargingStation
.connectors
.keys()) {
1237 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1238 OCPP16ChargePointStatus
.Unavailable
1240 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1243 OCPP16ChargePointStatus
.Unavailable
1248 transactionsStarted
= false
1250 } while (transactionsStarted
)
1251 !wasTransactionsStarted
&&
1252 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))))
1253 if (!checkChargingStation(chargingStation
, chargingStation
.logPrefix())) {
1256 await chargingStation
.ocppRequestService
.requestHandler
<
1257 OCPP16FirmwareStatusNotificationRequest
,
1258 OCPP16FirmwareStatusNotificationResponse
1259 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1260 status: OCPP16FirmwareStatus
.Installing
1262 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
1264 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1265 OCPP16FirmwareStatus
.InstallationFailed
1267 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1268 await chargingStation
.ocppRequestService
.requestHandler
<
1269 OCPP16FirmwareStatusNotificationRequest
,
1270 OCPP16FirmwareStatusNotificationResponse
1271 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1272 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1274 chargingStation
.stationInfo
.firmwareStatus
=
1275 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
1278 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1279 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)))
1280 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
)
1284 private async handleRequestGetDiagnostics (
1285 chargingStation
: ChargingStation
,
1286 commandPayload
: GetDiagnosticsRequest
1287 ): Promise
<GetDiagnosticsResponse
> {
1289 !OCPP16ServiceUtils
.checkFeatureProfile(
1291 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1292 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1296 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Cannot get diagnostics: feature profile not supported`
1298 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1300 const { location
} = commandPayload
1301 const uri
= new URL(location
)
1302 if (uri
.protocol
.startsWith('ftp:')) {
1303 let ftpClient
: Client
| undefined
1305 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1306 .filter((file
) => file
.endsWith('.log'))
1307 .map((file
) => join('./', file
))
1308 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`
1309 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
))
1310 ftpClient
= new Client()
1311 const accessResponse
= await ftpClient
.access({
1313 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1314 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1315 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
})
1317 let uploadResponse
: FTPResponse
| undefined
1318 if (accessResponse
.code
=== 220) {
1319 ftpClient
.trackProgress((info
) => {
1321 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1323 } bytes transferred from diagnostics archive ${info.name}`
1325 chargingStation
.ocppRequestService
1327 OCPP16DiagnosticsStatusNotificationRequest
,
1328 OCPP16DiagnosticsStatusNotificationResponse
1329 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1330 status: OCPP16DiagnosticsStatus
.Uploading
1334 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: Error while sending '${
1335 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION
1341 uploadResponse
= await ftpClient
.uploadFrom(
1342 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1343 `${uri.pathname}${diagnosticsArchive}`
1345 if (uploadResponse
.code
=== 226) {
1346 await chargingStation
.ocppRequestService
.requestHandler
<
1347 OCPP16DiagnosticsStatusNotificationRequest
,
1348 OCPP16DiagnosticsStatusNotificationResponse
1349 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1350 status: OCPP16DiagnosticsStatus
.Uploaded
1352 if (ftpClient
!= null) {
1355 return { fileName
: diagnosticsArchive
}
1357 throw new OCPPError(
1358 ErrorType
.GENERIC_ERROR
,
1359 `Diagnostics transfer failed with error code ${accessResponse.code}${
1360 uploadResponse?.code != null && `|${uploadResponse?.code}
`
1362 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
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 await chargingStation
.ocppRequestService
.requestHandler
<
1374 OCPP16DiagnosticsStatusNotificationRequest
,
1375 OCPP16DiagnosticsStatusNotificationResponse
1376 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1377 status: OCPP16DiagnosticsStatus
.UploadFailed
1379 if (ftpClient
!= null) {
1382 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1383 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1385 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1387 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1392 `${chargingStation.logPrefix()} Unsupported protocol ${
1394 } to transfer the diagnostic logs archive`
1396 await chargingStation
.ocppRequestService
.requestHandler
<
1397 OCPP16DiagnosticsStatusNotificationRequest
,
1398 OCPP16DiagnosticsStatusNotificationResponse
1399 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1400 status: OCPP16DiagnosticsStatus
.UploadFailed
1402 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
1406 private handleRequestTriggerMessage (
1407 chargingStation
: ChargingStation
,
1408 commandPayload
: OCPP16TriggerMessageRequest
1409 ): OCPP16TriggerMessageResponse
{
1410 const { requestedMessage
, connectorId
} = commandPayload
1412 !OCPP16ServiceUtils
.checkFeatureProfile(
1414 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1415 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1417 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1419 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1422 !OCPP16ServiceUtils
.isConnectorIdValid(
1424 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1425 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1429 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
1432 switch (requestedMessage
) {
1433 case OCPP16MessageTrigger
.BootNotification
:
1435 chargingStation
.ocppRequestService
1436 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1438 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1439 chargingStation
.bootNotificationRequest
,
1440 { skipBufferingOnError
: true, triggerMessage
: true }
1442 .then((response
) => {
1443 chargingStation
.bootNotificationResponse
= response
1445 .catch(Constants
.EMPTY_FUNCTION
)
1446 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1447 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1448 case OCPP16MessageTrigger
.Heartbeat
:
1450 chargingStation
.ocppRequestService
1451 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1453 OCPP16RequestCommand
.HEARTBEAT
,
1456 triggerMessage
: true
1459 .catch(Constants
.EMPTY_FUNCTION
)
1460 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1461 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1462 case OCPP16MessageTrigger
.StatusNotification
:
1464 if (connectorId
!= null) {
1465 chargingStation
.ocppRequestService
1466 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1468 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1471 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1472 status: chargingStation
.getConnectorStatus(connectorId
)?.status
1475 triggerMessage
: true
1478 .catch(Constants
.EMPTY_FUNCTION
)
1480 if (chargingStation
.hasEvses
) {
1481 for (const evseStatus
of chargingStation
.evses
.values()) {
1482 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1483 chargingStation
.ocppRequestService
1485 OCPP16StatusNotificationRequest
,
1486 OCPP16StatusNotificationResponse
1489 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1492 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1493 status: connectorStatus
.status
1496 triggerMessage
: true
1499 .catch(Constants
.EMPTY_FUNCTION
)
1503 for (const id
of chargingStation
.connectors
.keys()) {
1504 chargingStation
.ocppRequestService
1506 OCPP16StatusNotificationRequest
,
1507 OCPP16StatusNotificationResponse
1510 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1513 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1514 status: chargingStation
.getConnectorStatus(id
)?.status
1517 triggerMessage
: true
1520 .catch(Constants
.EMPTY_FUNCTION
)
1524 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
)
1525 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
1527 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
1530 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1531 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1533 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1535 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1540 private handleRequestDataTransfer (
1541 chargingStation
: ChargingStation
,
1542 commandPayload
: OCPP16DataTransferRequest
1543 ): OCPP16DataTransferResponse
{
1544 const { vendorId
} = commandPayload
1546 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1547 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
1549 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
1551 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1552 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1554 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1556 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1561 private async handleRequestReserveNow (
1562 chargingStation
: ChargingStation
,
1563 commandPayload
: OCPP16ReserveNowRequest
1564 ): Promise
<OCPP16ReserveNowResponse
> {
1566 !OCPP16ServiceUtils
.checkFeatureProfile(
1568 OCPP16SupportedFeatureProfiles
.Reservation
,
1569 OCPP16IncomingRequestCommand
.RESERVE_NOW
1572 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1574 const { reservationId
, idTag
, connectorId
} = commandPayload
1575 let response
: OCPP16ReserveNowResponse
1577 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1578 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1580 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1581 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1583 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1584 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
1586 await removeExpiredReservations(chargingStation
)
1587 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1588 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1589 case OCPP16ChargePointStatus
.Faulted
:
1590 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
1592 case OCPP16ChargePointStatus
.Preparing
:
1593 case OCPP16ChargePointStatus
.Charging
:
1594 case OCPP16ChargePointStatus
.SuspendedEV
:
1595 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1596 case OCPP16ChargePointStatus
.Finishing
:
1597 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1599 case OCPP16ChargePointStatus
.Unavailable
:
1600 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
1602 case OCPP16ChargePointStatus
.Reserved
:
1603 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1604 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1607 // eslint-disable-next-line no-fallthrough
1609 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1610 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
1613 await chargingStation
.addReservation({
1614 id
: commandPayload
.reservationId
,
1617 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
1622 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1623 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
1624 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1625 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1627 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1629 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1634 private async handleRequestCancelReservation (
1635 chargingStation
: ChargingStation
,
1636 commandPayload
: OCPP16CancelReservationRequest
1637 ): Promise
<GenericResponse
> {
1639 !OCPP16ServiceUtils
.checkFeatureProfile(
1641 OCPP16SupportedFeatureProfiles
.Reservation
,
1642 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1645 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1648 const { reservationId
} = commandPayload
1649 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
)
1650 if (reservation
== null) {
1652 `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station`
1654 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
1656 await chargingStation
.removeReservation(
1658 ReservationTerminationReason
.RESERVATION_CANCELED
1660 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
1662 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1663 return this.handleIncomingRequestError
<GenericResponse
>(
1665 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1667 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}