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';
9 import { create
} from
'tar';
11 import { OCPP16Constants
} from
'./OCPP16Constants';
12 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
15 ChargingStationConfigurationUtils
,
17 } from
'../../../charging-station';
18 import { OCPPError
} from
'../../../exception';
20 type ChangeAvailabilityRequest
,
21 type ChangeAvailabilityResponse
,
22 type ChangeConfigurationRequest
,
23 type ChangeConfigurationResponse
,
24 type ClearChargingProfileRequest
,
25 type ClearChargingProfileResponse
,
30 type GetConfigurationRequest
,
31 type GetConfigurationResponse
,
32 type GetDiagnosticsRequest
,
33 type GetDiagnosticsResponse
,
34 type IncomingRequestHandler
,
37 OCPP16AuthorizationStatus
,
38 OCPP16AvailabilityType
,
39 type OCPP16BootNotificationRequest
,
40 type OCPP16BootNotificationResponse
,
41 type OCPP16CancelReservationRequest
,
42 OCPP16ChargePointErrorCode
,
43 OCPP16ChargePointStatus
,
44 type OCPP16ChargingProfile
,
45 OCPP16ChargingProfilePurposeType
,
46 type OCPP16ChargingSchedule
,
47 type OCPP16ClearCacheRequest
,
48 type OCPP16DataTransferRequest
,
49 type OCPP16DataTransferResponse
,
50 OCPP16DataTransferVendorId
,
51 OCPP16DiagnosticsStatus
,
52 type OCPP16DiagnosticsStatusNotificationRequest
,
53 type OCPP16DiagnosticsStatusNotificationResponse
,
55 type OCPP16FirmwareStatusNotificationRequest
,
56 type OCPP16FirmwareStatusNotificationResponse
,
57 type OCPP16GetCompositeScheduleRequest
,
58 type OCPP16GetCompositeScheduleResponse
,
59 type OCPP16HeartbeatRequest
,
60 type OCPP16HeartbeatResponse
,
61 OCPP16IncomingRequestCommand
,
64 type OCPP16ReserveNowRequest
,
65 type OCPP16ReserveNowResponse
,
66 OCPP16StandardParametersKey
,
67 type OCPP16StartTransactionRequest
,
68 type OCPP16StartTransactionResponse
,
69 type OCPP16StatusNotificationRequest
,
70 type OCPP16StatusNotificationResponse
,
71 OCPP16StopTransactionReason
,
72 OCPP16SupportedFeatureProfiles
,
73 type OCPP16TriggerMessageRequest
,
74 type OCPP16TriggerMessageResponse
,
75 type OCPP16UpdateFirmwareRequest
,
76 type OCPP16UpdateFirmwareResponse
,
77 type OCPPConfigurationKey
,
79 type RemoteStartTransactionRequest
,
80 type RemoteStopTransactionRequest
,
82 ReservationTerminationReason
,
84 type SetChargingProfileRequest
,
85 type SetChargingProfileResponse
,
86 type StartTransactionRequest
,
87 type UnlockConnectorRequest
,
88 type UnlockConnectorResponse
,
89 } from
'../../../types';
94 formatDurationMilliSeconds
,
103 } from
'../../../utils';
104 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
106 const moduleName
= 'OCPP16IncomingRequestService';
108 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
109 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
110 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
112 public constructor() {
113 // if (new.target?.name === moduleName) {
114 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
116 super(OCPPVersion
.VERSION_16
);
117 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
118 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
119 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
120 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
122 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
123 this.handleRequestGetConfiguration
.bind(this),
126 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
127 this.handleRequestChangeConfiguration
.bind(this),
130 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
131 this.handleRequestGetCompositeSchedule
.bind(this),
134 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
135 this.handleRequestSetChargingProfile
.bind(this),
138 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
139 this.handleRequestClearChargingProfile
.bind(this),
142 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
143 this.handleRequestChangeAvailability
.bind(this),
146 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
147 this.handleRequestRemoteStartTransaction
.bind(this),
150 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
151 this.handleRequestRemoteStopTransaction
.bind(this),
153 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
154 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
155 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
156 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
157 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
159 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
160 this.handleRequestCancelReservation
.bind(this),
163 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
165 OCPP16IncomingRequestCommand
.RESET
,
166 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
167 'assets/json-schemas/ocpp/1.6/Reset.json',
173 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
174 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
175 'assets/json-schemas/ocpp/1.6/ClearCache.json',
181 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
182 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
183 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
189 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
190 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
191 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
197 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
198 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
199 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
205 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
207 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
213 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
214 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
215 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
221 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
222 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
223 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
229 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
231 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
237 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
238 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
239 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
245 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
246 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
247 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
253 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
255 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
261 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
262 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
263 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
269 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
270 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
271 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
277 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
279 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
285 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
286 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
287 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
293 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
294 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
295 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
301 this.validatePayload
= this.validatePayload
.bind(this) as (
302 chargingStation
: ChargingStation
,
303 commandName
: OCPP16IncomingRequestCommand
,
304 commandPayload
: JsonType
,
308 public async incomingRequestHandler(
309 chargingStation
: ChargingStation
,
311 commandName
: OCPP16IncomingRequestCommand
,
312 commandPayload
: JsonType
,
314 let response
: JsonType
;
316 chargingStation
.getOcppStrictCompliance() === true &&
317 chargingStation
.inPendingState() === true &&
318 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
319 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
322 ErrorType
.SECURITY_ERROR
,
323 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
327 )} while the charging station is in pending state on the central server`,
333 chargingStation
.isRegistered() === true ||
334 (chargingStation
.getOcppStrictCompliance() === false &&
335 chargingStation
.inUnknownState() === true)
338 this.incomingRequestHandlers
.has(commandName
) === true &&
339 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
342 this.validatePayload(chargingStation
, commandName
, commandPayload
);
343 // Call the method to build the response
344 response
= await this.incomingRequestHandlers
.get(commandName
)!(
351 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
352 Handle incoming request error:`,
360 ErrorType
.NOT_IMPLEMENTED
,
361 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
372 ErrorType
.SECURITY_ERROR
,
373 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
377 )} while the charging station is not registered on the central server.`,
382 // Send the built response
383 await chargingStation
.ocppRequestService
.sendResponse(
391 private validatePayload(
392 chargingStation
: ChargingStation
,
393 commandName
: OCPP16IncomingRequestCommand
,
394 commandPayload
: JsonType
,
396 if (this.jsonSchemas
.has(commandName
) === true) {
397 return this.validateIncomingRequestPayload(
400 this.jsonSchemas
.get(commandName
)!,
405 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
406 for command '${commandName}' PDU validation`,
411 // Simulate charging station restart
412 private handleRequestReset(
413 chargingStation
: ChargingStation
,
414 commandPayload
: ResetRequest
,
416 this.runInAsyncScope(
417 chargingStation
.reset
.bind(chargingStation
) as (
418 this: ChargingStation
,
422 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
,
423 ).catch(Constants
.EMPTY_FUNCTION
);
425 `${chargingStation.logPrefix()} ${
427 } reset command received, simulating it. The station will be
428 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`,
430 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
433 private async handleRequestUnlockConnector(
434 chargingStation
: ChargingStation
,
435 commandPayload
: UnlockConnectorRequest
,
436 ): Promise
<UnlockConnectorResponse
> {
437 const connectorId
= commandPayload
.connectorId
;
438 if (chargingStation
.hasConnector(connectorId
) === false) {
440 `${chargingStation.logPrefix()} Trying to unlock a non existing
441 connector id ${connectorId.toString()}`,
443 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
445 if (connectorId
=== 0) {
447 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
449 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
451 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
452 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
454 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
456 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
457 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
459 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
461 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
464 OCPP16ChargePointStatus
.Available
,
466 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
469 private handleRequestGetConfiguration(
470 chargingStation
: ChargingStation
,
471 commandPayload
: GetConfigurationRequest
,
472 ): GetConfigurationResponse
{
473 const configurationKey
: OCPPConfigurationKey
[] = [];
474 const unknownKey
: string[] = [];
476 chargingStation
.ocppConfiguration
?.configurationKey
&&
477 isUndefined(commandPayload
.key
) === true
479 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
480 if (isUndefined(configuration
.visible
) === true) {
481 configuration
.visible
= true;
483 if (configuration
.visible
=== false) {
486 configurationKey
.push({
487 key
: configuration
.key
,
488 readonly: configuration
.readonly,
489 value
: configuration
.value
,
492 } else if (commandPayload
.key
&& isNotEmptyArray(commandPayload
.key
) === true) {
493 for (const key
of commandPayload
.key
) {
494 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
500 if (isUndefined(keyFound
.visible
) === true) {
501 keyFound
.visible
= true;
503 if (keyFound
.visible
=== false) {
506 configurationKey
.push({
508 readonly: keyFound
.readonly,
509 value
: keyFound
.value
,
512 unknownKey
.push(key
);
522 private handleRequestChangeConfiguration(
523 chargingStation
: ChargingStation
,
524 commandPayload
: ChangeConfigurationRequest
,
525 ): ChangeConfigurationResponse
{
526 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
531 if (keyToChange
?.readonly === true) {
532 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
533 } else if (keyToChange
?.readonly === false) {
534 let valueChanged
= false;
535 if (keyToChange
.value
!== commandPayload
.value
) {
536 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
539 commandPayload
.value
,
544 let triggerHeartbeatRestart
= false;
546 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
547 OCPP16StandardParametersKey
.HeartBeatInterval
&&
550 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
552 OCPP16StandardParametersKey
.HeartbeatInterval
,
553 commandPayload
.value
,
555 triggerHeartbeatRestart
= true;
558 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
559 OCPP16StandardParametersKey
.HeartbeatInterval
&&
562 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
564 OCPP16StandardParametersKey
.HeartBeatInterval
,
565 commandPayload
.value
,
567 triggerHeartbeatRestart
= true;
569 if (triggerHeartbeatRestart
) {
570 chargingStation
.restartHeartbeat();
573 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
574 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
577 chargingStation
.restartWebSocketPing();
579 if (keyToChange
.reboot
) {
580 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
582 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
584 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
587 private handleRequestSetChargingProfile(
588 chargingStation
: ChargingStation
,
589 commandPayload
: SetChargingProfileRequest
,
590 ): SetChargingProfileResponse
{
592 OCPP16ServiceUtils
.checkFeatureProfile(
594 OCPP16SupportedFeatureProfiles
.SmartCharging
,
595 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
598 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
600 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
602 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
603 non existing connector id ${commandPayload.connectorId}`,
605 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
608 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
609 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
610 commandPayload
.connectorId
!== 0
612 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
615 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
616 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
617 (commandPayload
.connectorId
=== 0 ||
618 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
622 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
623 on connector ${commandPayload.connectorId} without a started transaction`,
625 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
627 OCPP16ServiceUtils
.setChargingProfile(
629 commandPayload
.connectorId
,
630 commandPayload
.csChargingProfiles
,
633 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
634 commandPayload.connectorId
636 commandPayload
.csChargingProfiles
,
638 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
641 private handleRequestGetCompositeSchedule(
642 chargingStation
: ChargingStation
,
643 commandPayload
: OCPP16GetCompositeScheduleRequest
,
644 ): OCPP16GetCompositeScheduleResponse
{
646 OCPP16ServiceUtils
.checkFeatureProfile(
648 OCPP16SupportedFeatureProfiles
.SmartCharging
,
649 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
652 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
654 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
656 `${chargingStation.logPrefix()} Trying to get composite schedule to a
657 non existing connector id ${commandPayload.connectorId}`,
659 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
662 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
664 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
666 const startDate
= new Date();
667 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
668 let compositeSchedule
: OCPP16ChargingSchedule
| undefined;
669 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)!
670 .chargingProfiles
!) {
671 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
673 chargingProfile
.chargingSchedule
.startSchedule
! >= startDate
&&
674 chargingProfile
.chargingSchedule
.startSchedule
! <= endDate
676 compositeSchedule
= chargingProfile
.chargingSchedule
;
681 status: GenericStatus
.Accepted
,
682 scheduleStart
: compositeSchedule
?.startSchedule
,
683 connectorId
: commandPayload
.connectorId
,
684 chargingSchedule
: compositeSchedule
,
688 private handleRequestClearChargingProfile(
689 chargingStation
: ChargingStation
,
690 commandPayload
: ClearChargingProfileRequest
,
691 ): ClearChargingProfileResponse
{
693 OCPP16ServiceUtils
.checkFeatureProfile(
695 OCPP16SupportedFeatureProfiles
.SmartCharging
,
696 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
699 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
701 if (chargingStation
.hasConnector(commandPayload
.connectorId
!) === false) {
703 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
704 a non existing connector id ${commandPayload.connectorId}`,
706 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
709 !isNullOrUndefined(commandPayload
.connectorId
) &&
711 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.chargingProfiles
,
714 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)!.chargingProfiles
= [];
716 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
717 commandPayload.connectorId
720 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
722 if (isNullOrUndefined(commandPayload
.connectorId
)) {
723 let clearedCP
= false;
724 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
725 if (isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
726 connectorStatus
?.chargingProfiles
?.forEach(
727 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
728 let clearCurrentCP
= false;
729 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
730 clearCurrentCP
= true;
733 !commandPayload
.chargingProfilePurpose
&&
734 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
736 clearCurrentCP
= true;
739 !chargingProfile
.stackLevel
&&
740 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
742 clearCurrentCP
= true;
745 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
746 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
748 clearCurrentCP
= true;
750 if (clearCurrentCP
) {
751 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
753 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
762 if (chargingStation
.hasEvses
) {
763 for (const evseStatus
of chargingStation
.evses
.values()) {
764 for (const connectorStatus
of evseStatus
.connectors
.values()) {
765 clearChargingProfiles(connectorStatus
);
769 for (const connectorId
of chargingStation
.connectors
.keys()) {
770 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
)!);
774 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
777 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
780 private async handleRequestChangeAvailability(
781 chargingStation
: ChargingStation
,
782 commandPayload
: ChangeAvailabilityRequest
,
783 ): Promise
<ChangeAvailabilityResponse
> {
784 const connectorId
: number = commandPayload
.connectorId
;
785 if (chargingStation
.hasConnector(connectorId
) === false) {
787 `${chargingStation.logPrefix()} Trying to change the availability of a
788 non existing connector id ${connectorId.toString()}`,
790 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
792 const chargePointStatus
: OCPP16ChargePointStatus
=
793 commandPayload
.type === OCPP16AvailabilityType
.Operative
794 ? OCPP16ChargePointStatus
.Available
795 : OCPP16ChargePointStatus
.Unavailable
;
796 if (connectorId
=== 0) {
797 let response
: ChangeAvailabilityResponse
=
798 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
799 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
800 if (connectorStatus
?.transactionStarted
=== true) {
801 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
803 connectorStatus
.availability
= commandPayload
.type;
804 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
805 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
812 if (chargingStation
.hasEvses
) {
813 for (const evseStatus
of chargingStation
.evses
.values()) {
814 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
815 await changeAvailability(id
, connectorStatus
);
819 for (const id
of chargingStation
.connectors
.keys()) {
820 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
)!);
826 (chargingStation
.isChargingStationAvailable() === true ||
827 (chargingStation
.isChargingStationAvailable() === false &&
828 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
830 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
831 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
832 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
834 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
835 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
840 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
842 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
845 private async handleRequestRemoteStartTransaction(
846 chargingStation
: ChargingStation
,
847 commandPayload
: RemoteStartTransactionRequest
,
848 ): Promise
<GenericResponse
> {
849 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
851 chargingStation
.getConnectorStatus(transactionConnectorId
)!.status ===
852 OCPP16ChargePointStatus
.Reserved
;
853 const reservedOnConnectorZero
=
854 chargingStation
.getConnectorStatus(0)!.status === OCPP16ChargePointStatus
.Reserved
;
857 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
858 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
860 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
862 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
863 return this.notifyRemoteStartTransactionRejected(
865 transactionConnectorId
,
870 !chargingStation
.isChargingStationAvailable() ||
871 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
873 return this.notifyRemoteStartTransactionRejected(
875 transactionConnectorId
,
879 const remoteStartTransactionLogMsg
= `
880 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
881 chargingStation.stationInfo.chargingStationId
882 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
883 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
885 transactionConnectorId
,
886 OCPP16ChargePointStatus
.Preparing
,
888 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
889 // Check if authorized
891 chargingStation
.getAuthorizeRemoteTxRequests() &&
892 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
894 // Authorization successful, start transaction
896 this.setRemoteStartTransactionChargingProfile(
898 transactionConnectorId
,
902 connectorStatus
.transactionRemoteStarted
= true;
903 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
904 connectorId
: transactionConnectorId
,
907 if (reserved
|| reservedOnConnectorZero
) {
908 const reservation
= chargingStation
.getReservationBy(
909 ReservationFilterKey
.CONNECTOR_ID
,
910 reservedOnConnectorZero
? 0 : transactionConnectorId
,
912 startTransactionPayload
.reservationId
= reservation
.id
;
913 await chargingStation
.removeReservation(
915 ReservationTerminationReason
.TRANSACTION_STARTED
,
920 await chargingStation
.ocppRequestService
.requestHandler
<
921 OCPP16StartTransactionRequest
,
922 OCPP16StartTransactionResponse
923 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
924 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
926 logger
.debug(remoteStartTransactionLogMsg
);
927 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
929 return this.notifyRemoteStartTransactionRejected(
931 transactionConnectorId
,
935 return this.notifyRemoteStartTransactionRejected(
937 transactionConnectorId
,
941 // No authorization check required, start transaction
943 this.setRemoteStartTransactionChargingProfile(
945 transactionConnectorId
,
949 connectorStatus
.transactionRemoteStarted
= true;
952 await chargingStation
.ocppRequestService
.requestHandler
<
953 OCPP16StartTransactionRequest
,
954 OCPP16StartTransactionResponse
955 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
956 connectorId
: transactionConnectorId
,
959 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
961 logger
.debug(remoteStartTransactionLogMsg
);
962 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
964 return this.notifyRemoteStartTransactionRejected(
966 transactionConnectorId
,
970 return this.notifyRemoteStartTransactionRejected(
972 transactionConnectorId
,
977 private async notifyRemoteStartTransactionRejected(
978 chargingStation
: ChargingStation
,
981 ): Promise
<GenericResponse
> {
983 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
985 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
988 OCPP16ChargePointStatus
.Available
,
992 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
993 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
995 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
997 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1000 private setRemoteStartTransactionChargingProfile(
1001 chargingStation
: ChargingStation
,
1002 connectorId
: number,
1003 cp
: OCPP16ChargingProfile
,
1005 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1006 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
1008 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
1009 on connector id ${connectorId}: %j`,
1013 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1015 `${chargingStation.logPrefix()} Not allowed to set ${
1016 cp.chargingProfilePurpose
1017 } charging profile(s) at remote start transaction`,
1024 private async handleRequestRemoteStopTransaction(
1025 chargingStation
: ChargingStation
,
1026 commandPayload
: RemoteStopTransactionRequest
,
1027 ): Promise
<GenericResponse
> {
1028 const transactionId
= commandPayload
.transactionId
;
1029 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1030 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1033 OCPP16ChargePointStatus
.Finishing
,
1035 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1037 OCPP16StopTransactionReason
.REMOTE
,
1039 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1040 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1042 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1044 if (chargingStation
.hasEvses
) {
1045 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1047 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1048 if (connectorStatus
.transactionId
=== transactionId
) {
1049 return remoteStopTransaction(connectorId
);
1055 for (const connectorId
of chargingStation
.connectors
.keys()) {
1058 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1060 return remoteStopTransaction(connectorId
);
1065 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1066 ${transactionId.toString()}`,
1068 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1071 private handleRequestUpdateFirmware(
1072 chargingStation
: ChargingStation
,
1073 commandPayload
: OCPP16UpdateFirmwareRequest
,
1074 ): OCPP16UpdateFirmwareResponse
{
1076 OCPP16ServiceUtils
.checkFeatureProfile(
1078 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1079 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1083 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1084 Cannot simulate firmware update: feature profile not supported`,
1086 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1089 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1090 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1093 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1094 Cannot simulate firmware update: firmware update is already in progress`,
1096 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1098 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1099 const now
= Date.now();
1100 if (retrieveDate
?.getTime() <= now
) {
1101 this.runInAsyncScope(
1102 this.updateFirmwareSimulation
.bind(this) as (
1103 this: OCPP16IncomingRequestService
,
1108 ).catch(Constants
.EMPTY_FUNCTION
);
1112 this.runInAsyncScope(
1113 this.updateFirmwareSimulation
.bind(this) as (
1114 this: OCPP16IncomingRequestService
,
1119 ).catch(Constants
.EMPTY_FUNCTION
);
1121 retrieveDate
?.getTime() - now
,
1124 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1127 private async updateFirmwareSimulation(
1128 chargingStation
: ChargingStation
,
1132 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1135 if (chargingStation
.hasEvses
) {
1136 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1138 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1139 if (connectorStatus
?.transactionStarted
=== false) {
1140 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1143 OCPP16ChargePointStatus
.Unavailable
,
1150 for (const connectorId
of chargingStation
.connectors
.keys()) {
1153 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1155 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1158 OCPP16ChargePointStatus
.Unavailable
,
1163 await chargingStation
.ocppRequestService
.requestHandler
<
1164 OCPP16FirmwareStatusNotificationRequest
,
1165 OCPP16FirmwareStatusNotificationResponse
1166 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1167 status: OCPP16FirmwareStatus
.Downloading
,
1169 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1171 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1172 OCPP16FirmwareStatus
.DownloadFailed
1174 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1175 await chargingStation
.ocppRequestService
.requestHandler
<
1176 OCPP16FirmwareStatusNotificationRequest
,
1177 OCPP16FirmwareStatusNotificationResponse
1178 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1179 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1181 chargingStation
.stationInfo
.firmwareStatus
=
1182 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1185 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1186 await chargingStation
.ocppRequestService
.requestHandler
<
1187 OCPP16FirmwareStatusNotificationRequest
,
1188 OCPP16FirmwareStatusNotificationResponse
1189 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1190 status: OCPP16FirmwareStatus
.Downloaded
,
1192 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1193 let wasTransactionsStarted
= false;
1194 let transactionsStarted
: boolean;
1196 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1197 if (runningTransactions
> 0) {
1198 const waitTime
= 15 * 1000;
1200 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1201 ${runningTransactions} transaction(s) in progress, waiting ${
1203 } seconds before continuing firmware update simulation`,
1205 await sleep(waitTime
);
1206 transactionsStarted
= true;
1207 wasTransactionsStarted
= true;
1209 if (chargingStation
.hasEvses
) {
1210 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1212 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1213 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1214 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1217 OCPP16ChargePointStatus
.Unavailable
,
1224 for (const connectorId
of chargingStation
.connectors
.keys()) {
1227 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1228 OCPP16ChargePointStatus
.Unavailable
1230 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1233 OCPP16ChargePointStatus
.Unavailable
,
1238 transactionsStarted
= false;
1240 } while (transactionsStarted
);
1241 !wasTransactionsStarted
&& (await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000));
1242 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1245 await chargingStation
.ocppRequestService
.requestHandler
<
1246 OCPP16FirmwareStatusNotificationRequest
,
1247 OCPP16FirmwareStatusNotificationResponse
1248 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1249 status: OCPP16FirmwareStatus
.Installing
,
1251 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1253 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1254 OCPP16FirmwareStatus
.InstallationFailed
1256 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1257 await chargingStation
.ocppRequestService
.requestHandler
<
1258 OCPP16FirmwareStatusNotificationRequest
,
1259 OCPP16FirmwareStatusNotificationResponse
1260 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1261 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1263 chargingStation
.stationInfo
.firmwareStatus
=
1264 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1267 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1268 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1269 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1273 private async handleRequestGetDiagnostics(
1274 chargingStation
: ChargingStation
,
1275 commandPayload
: GetDiagnosticsRequest
,
1276 ): Promise
<GetDiagnosticsResponse
> {
1278 OCPP16ServiceUtils
.checkFeatureProfile(
1280 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1281 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1285 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1286 Cannot get diagnostics: feature profile not supported`,
1288 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1290 const uri
= new URL(commandPayload
.location
);
1291 if (uri
.protocol
.startsWith('ftp:')) {
1292 let ftpClient
: Client
| undefined;
1294 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1295 .filter((file
) => file
.endsWith('.log'))
1296 .map((file
) => join('./', file
));
1297 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1298 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1299 ftpClient
= new Client();
1300 const accessResponse
= await ftpClient
.access({
1302 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1303 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1304 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1306 let uploadResponse
: FTPResponse
| undefined;
1307 if (accessResponse
.code
=== 220) {
1308 ftpClient
.trackProgress((info
) => {
1310 `${chargingStation.logPrefix()} ${
1312 } bytes transferred from diagnostics archive ${info.name}`,
1314 chargingStation
.ocppRequestService
1316 OCPP16DiagnosticsStatusNotificationRequest
,
1317 OCPP16DiagnosticsStatusNotificationResponse
1318 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1319 status: OCPP16DiagnosticsStatus
.Uploading
,
1323 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1324 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1329 uploadResponse
= await ftpClient
.uploadFrom(
1330 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1331 `${uri.pathname}${diagnosticsArchive}`,
1333 if (uploadResponse
.code
=== 226) {
1334 await chargingStation
.ocppRequestService
.requestHandler
<
1335 OCPP16DiagnosticsStatusNotificationRequest
,
1336 OCPP16DiagnosticsStatusNotificationResponse
1337 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1338 status: OCPP16DiagnosticsStatus
.Uploaded
,
1343 return { fileName
: diagnosticsArchive
};
1345 throw new OCPPError(
1346 ErrorType
.GENERIC_ERROR
,
1347 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1348 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1350 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1353 throw new OCPPError(
1354 ErrorType
.GENERIC_ERROR
,
1355 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1356 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1358 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1361 await chargingStation
.ocppRequestService
.requestHandler
<
1362 OCPP16DiagnosticsStatusNotificationRequest
,
1363 OCPP16DiagnosticsStatusNotificationResponse
1364 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1365 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1370 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1372 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1374 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1379 `${chargingStation.logPrefix()} Unsupported protocol ${
1381 } to transfer the diagnostic logs archive`,
1383 await chargingStation
.ocppRequestService
.requestHandler
<
1384 OCPP16DiagnosticsStatusNotificationRequest
,
1385 OCPP16DiagnosticsStatusNotificationResponse
1386 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1387 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1389 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1393 private handleRequestTriggerMessage(
1394 chargingStation
: ChargingStation
,
1395 commandPayload
: OCPP16TriggerMessageRequest
,
1396 ): OCPP16TriggerMessageResponse
{
1398 !OCPP16ServiceUtils
.checkFeatureProfile(
1400 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1401 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1403 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1405 commandPayload
.requestedMessage
,
1408 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1411 !OCPP16ServiceUtils
.isConnectorIdValid(
1413 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1414 commandPayload
.connectorId
!,
1417 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1420 switch (commandPayload
.requestedMessage
) {
1421 case OCPP16MessageTrigger
.BootNotification
:
1423 chargingStation
.ocppRequestService
1424 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1426 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1427 chargingStation
.bootNotificationRequest
,
1428 { skipBufferingOnError
: true, triggerMessage
: true },
1430 .then((response
) => {
1431 chargingStation
.bootNotificationResponse
= response
;
1433 .catch(Constants
.EMPTY_FUNCTION
);
1434 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1435 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1436 case OCPP16MessageTrigger
.Heartbeat
:
1438 chargingStation
.ocppRequestService
1439 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1441 OCPP16RequestCommand
.HEARTBEAT
,
1444 triggerMessage
: true,
1447 .catch(Constants
.EMPTY_FUNCTION
);
1448 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1449 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1450 case OCPP16MessageTrigger
.StatusNotification
:
1452 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1453 chargingStation
.ocppRequestService
1454 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1456 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1458 connectorId
: commandPayload
.connectorId
,
1459 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1460 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1463 triggerMessage
: true,
1466 .catch(Constants
.EMPTY_FUNCTION
);
1468 // eslint-disable-next-line no-lonely-if
1469 if (chargingStation
.hasEvses
) {
1470 for (const evseStatus
of chargingStation
.evses
.values()) {
1471 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1472 chargingStation
.ocppRequestService
1474 OCPP16StatusNotificationRequest
,
1475 OCPP16StatusNotificationResponse
1478 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1481 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1482 status: connectorStatus
.status,
1485 triggerMessage
: true,
1488 .catch(Constants
.EMPTY_FUNCTION
);
1492 for (const connectorId
of chargingStation
.connectors
.keys()) {
1493 chargingStation
.ocppRequestService
1495 OCPP16StatusNotificationRequest
,
1496 OCPP16StatusNotificationResponse
1499 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1502 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1503 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1506 triggerMessage
: true,
1509 .catch(Constants
.EMPTY_FUNCTION
);
1513 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1514 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1516 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1519 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1521 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1523 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1528 private handleRequestDataTransfer(
1529 chargingStation
: ChargingStation
,
1530 commandPayload
: OCPP16DataTransferRequest
,
1531 ): OCPP16DataTransferResponse
{
1533 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1534 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1536 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1538 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1540 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1542 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1547 private async handleRequestReserveNow(
1548 chargingStation
: ChargingStation
,
1549 commandPayload
: OCPP16ReserveNowRequest
,
1550 ): Promise
<OCPP16ReserveNowResponse
> {
1552 !OCPP16ServiceUtils
.checkFeatureProfile(
1554 OCPP16SupportedFeatureProfiles
.Reservation
,
1555 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1558 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1560 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1561 let response
: OCPP16ReserveNowResponse
;
1563 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1564 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1566 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1567 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1569 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1570 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1572 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1573 case OCPP16ChargePointStatus
.Faulted
:
1574 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1576 case OCPP16ChargePointStatus
.Preparing
:
1577 case OCPP16ChargePointStatus
.Charging
:
1578 case OCPP16ChargePointStatus
.SuspendedEV
:
1579 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1580 case OCPP16ChargePointStatus
.Finishing
:
1581 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1583 case OCPP16ChargePointStatus
.Unavailable
:
1584 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1586 case OCPP16ChargePointStatus
.Reserved
:
1587 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1588 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1591 // eslint-disable-next-line no-fallthrough
1593 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1594 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1597 await chargingStation
.addReservation({
1598 id
: commandPayload
.reservationId
,
1601 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1606 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1607 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1609 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1611 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1616 private async handleRequestCancelReservation(
1617 chargingStation
: ChargingStation
,
1618 commandPayload
: OCPP16CancelReservationRequest
,
1619 ): Promise
<GenericResponse
> {
1621 !OCPP16ServiceUtils
.checkFeatureProfile(
1623 OCPP16SupportedFeatureProfiles
.Reservation
,
1624 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1627 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1630 const { reservationId
} = commandPayload
;
1631 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1634 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1635 does not exist on charging station`,
1637 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1639 await chargingStation
.removeReservation(
1641 ReservationTerminationReason
.RESERVATION_CANCELED
,
1643 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1645 return this.handleIncomingRequestError
<GenericResponse
>(
1647 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1649 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},