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
<ReqType
extends JsonType
, ResType
extends JsonType
>(
309 chargingStation
: ChargingStation
,
311 commandName
: OCPP16IncomingRequestCommand
,
312 commandPayload
: ReqType
,
314 let response
: ResType
;
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[] = [];
475 if (isUndefined(commandPayload
.key
) === true) {
476 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
477 if (isUndefined(configuration
.visible
) === true) {
478 configuration
.visible
= true;
480 if (configuration
.visible
=== false) {
483 configurationKey
.push({
484 key
: configuration
.key
,
485 readonly: configuration
.readonly,
486 value
: configuration
.value
,
489 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
490 for (const key
of commandPayload
.key
!) {
491 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
497 if (isUndefined(keyFound
.visible
) === true) {
498 keyFound
.visible
= true;
500 if (keyFound
.visible
=== false) {
503 configurationKey
.push({
505 readonly: keyFound
.readonly,
506 value
: keyFound
.value
,
509 unknownKey
.push(key
);
519 private handleRequestChangeConfiguration(
520 chargingStation
: ChargingStation
,
521 commandPayload
: ChangeConfigurationRequest
,
522 ): ChangeConfigurationResponse
{
523 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
528 if (keyToChange
?.readonly === true) {
529 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
530 } else if (keyToChange
?.readonly === false) {
531 let valueChanged
= false;
532 if (keyToChange
.value
!== commandPayload
.value
) {
533 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
536 commandPayload
.value
,
541 let triggerHeartbeatRestart
= false;
543 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
544 OCPP16StandardParametersKey
.HeartBeatInterval
&&
547 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
549 OCPP16StandardParametersKey
.HeartbeatInterval
,
550 commandPayload
.value
,
552 triggerHeartbeatRestart
= true;
555 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
556 OCPP16StandardParametersKey
.HeartbeatInterval
&&
559 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
561 OCPP16StandardParametersKey
.HeartBeatInterval
,
562 commandPayload
.value
,
564 triggerHeartbeatRestart
= true;
566 if (triggerHeartbeatRestart
) {
567 chargingStation
.restartHeartbeat();
570 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
571 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
574 chargingStation
.restartWebSocketPing();
576 if (keyToChange
.reboot
) {
577 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
579 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
581 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
584 private handleRequestSetChargingProfile(
585 chargingStation
: ChargingStation
,
586 commandPayload
: SetChargingProfileRequest
,
587 ): SetChargingProfileResponse
{
589 OCPP16ServiceUtils
.checkFeatureProfile(
591 OCPP16SupportedFeatureProfiles
.SmartCharging
,
592 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
595 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
597 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
599 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
600 non existing connector id ${commandPayload.connectorId}`,
602 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
605 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
606 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
607 commandPayload
.connectorId
!== 0
609 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
612 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
613 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
614 (commandPayload
.connectorId
=== 0 ||
615 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
619 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
620 on connector ${commandPayload.connectorId} without a started transaction`,
622 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
624 OCPP16ServiceUtils
.setChargingProfile(
626 commandPayload
.connectorId
,
627 commandPayload
.csChargingProfiles
,
630 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
631 commandPayload.connectorId
633 commandPayload
.csChargingProfiles
,
635 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
638 private handleRequestGetCompositeSchedule(
639 chargingStation
: ChargingStation
,
640 commandPayload
: OCPP16GetCompositeScheduleRequest
,
641 ): OCPP16GetCompositeScheduleResponse
{
643 OCPP16ServiceUtils
.checkFeatureProfile(
645 OCPP16SupportedFeatureProfiles
.SmartCharging
,
646 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
649 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
651 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
653 `${chargingStation.logPrefix()} Trying to get composite schedule to a
654 non existing connector id ${commandPayload.connectorId}`,
656 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
659 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
661 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
663 const startDate
= new Date();
664 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
665 let compositeSchedule
: OCPP16ChargingSchedule
| undefined;
666 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)!
667 .chargingProfiles
!) {
668 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
670 chargingProfile
.chargingSchedule
.startSchedule
! >= startDate
&&
671 chargingProfile
.chargingSchedule
.startSchedule
! <= endDate
673 compositeSchedule
= chargingProfile
.chargingSchedule
;
678 status: GenericStatus
.Accepted
,
679 scheduleStart
: compositeSchedule
?.startSchedule
,
680 connectorId
: commandPayload
.connectorId
,
681 chargingSchedule
: compositeSchedule
,
685 private handleRequestClearChargingProfile(
686 chargingStation
: ChargingStation
,
687 commandPayload
: ClearChargingProfileRequest
,
688 ): ClearChargingProfileResponse
{
690 OCPP16ServiceUtils
.checkFeatureProfile(
692 OCPP16SupportedFeatureProfiles
.SmartCharging
,
693 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
696 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
698 if (chargingStation
.hasConnector(commandPayload
.connectorId
!) === false) {
700 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
701 a non existing connector id ${commandPayload.connectorId}`,
703 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
706 !isNullOrUndefined(commandPayload
.connectorId
) &&
708 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.chargingProfiles
,
711 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)!.chargingProfiles
= [];
713 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
714 commandPayload.connectorId
717 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
719 if (isNullOrUndefined(commandPayload
.connectorId
)) {
720 let clearedCP
= false;
721 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
722 if (isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
723 connectorStatus
?.chargingProfiles
?.forEach(
724 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
725 let clearCurrentCP
= false;
726 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
727 clearCurrentCP
= true;
730 !commandPayload
.chargingProfilePurpose
&&
731 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
733 clearCurrentCP
= true;
736 !chargingProfile
.stackLevel
&&
737 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
739 clearCurrentCP
= true;
742 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
743 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
745 clearCurrentCP
= true;
747 if (clearCurrentCP
) {
748 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
750 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
759 if (chargingStation
.hasEvses
) {
760 for (const evseStatus
of chargingStation
.evses
.values()) {
761 for (const connectorStatus
of evseStatus
.connectors
.values()) {
762 clearChargingProfiles(connectorStatus
);
766 for (const connectorId
of chargingStation
.connectors
.keys()) {
767 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
)!);
771 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
774 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
777 private async handleRequestChangeAvailability(
778 chargingStation
: ChargingStation
,
779 commandPayload
: ChangeAvailabilityRequest
,
780 ): Promise
<ChangeAvailabilityResponse
> {
781 const connectorId
: number = commandPayload
.connectorId
;
782 if (chargingStation
.hasConnector(connectorId
) === false) {
784 `${chargingStation.logPrefix()} Trying to change the availability of a
785 non existing connector id ${connectorId.toString()}`,
787 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
789 const chargePointStatus
: OCPP16ChargePointStatus
=
790 commandPayload
.type === OCPP16AvailabilityType
.Operative
791 ? OCPP16ChargePointStatus
.Available
792 : OCPP16ChargePointStatus
.Unavailable
;
793 if (connectorId
=== 0) {
794 let response
: ChangeAvailabilityResponse
=
795 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
796 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
797 if (connectorStatus
?.transactionStarted
=== true) {
798 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
800 connectorStatus
.availability
= commandPayload
.type;
801 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
802 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
809 if (chargingStation
.hasEvses
) {
810 for (const evseStatus
of chargingStation
.evses
.values()) {
811 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
812 await changeAvailability(id
, connectorStatus
);
816 for (const id
of chargingStation
.connectors
.keys()) {
817 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
)!);
823 (chargingStation
.isChargingStationAvailable() === true ||
824 (chargingStation
.isChargingStationAvailable() === false &&
825 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
827 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
828 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
829 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
831 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
832 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
837 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
839 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
842 private async handleRequestRemoteStartTransaction(
843 chargingStation
: ChargingStation
,
844 commandPayload
: RemoteStartTransactionRequest
,
845 ): Promise
<GenericResponse
> {
846 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
848 chargingStation
.getConnectorStatus(transactionConnectorId
)!.status ===
849 OCPP16ChargePointStatus
.Reserved
;
850 const reservedOnConnectorZero
=
851 chargingStation
.getConnectorStatus(0)!.status === OCPP16ChargePointStatus
.Reserved
;
854 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
855 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
857 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
859 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
860 return this.notifyRemoteStartTransactionRejected(
862 transactionConnectorId
,
867 !chargingStation
.isChargingStationAvailable() ||
868 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
870 return this.notifyRemoteStartTransactionRejected(
872 transactionConnectorId
,
876 const remoteStartTransactionLogMsg
= `
877 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
878 chargingStation.stationInfo.chargingStationId
879 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
880 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
882 transactionConnectorId
,
883 OCPP16ChargePointStatus
.Preparing
,
885 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
886 // Check if authorized
888 chargingStation
.getAuthorizeRemoteTxRequests() &&
889 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
891 // Authorization successful, start transaction
893 this.setRemoteStartTransactionChargingProfile(
895 transactionConnectorId
,
899 connectorStatus
.transactionRemoteStarted
= true;
900 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
901 connectorId
: transactionConnectorId
,
904 if (reserved
|| reservedOnConnectorZero
) {
905 const reservation
= chargingStation
.getReservationBy(
906 ReservationFilterKey
.CONNECTOR_ID
,
907 reservedOnConnectorZero
? 0 : transactionConnectorId
,
909 startTransactionPayload
.reservationId
= reservation
.id
;
910 await chargingStation
.removeReservation(
912 ReservationTerminationReason
.TRANSACTION_STARTED
,
917 await chargingStation
.ocppRequestService
.requestHandler
<
918 OCPP16StartTransactionRequest
,
919 OCPP16StartTransactionResponse
920 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
921 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
923 logger
.debug(remoteStartTransactionLogMsg
);
924 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
926 return this.notifyRemoteStartTransactionRejected(
928 transactionConnectorId
,
932 return this.notifyRemoteStartTransactionRejected(
934 transactionConnectorId
,
938 // No authorization check required, start transaction
940 this.setRemoteStartTransactionChargingProfile(
942 transactionConnectorId
,
946 connectorStatus
.transactionRemoteStarted
= true;
949 await chargingStation
.ocppRequestService
.requestHandler
<
950 OCPP16StartTransactionRequest
,
951 OCPP16StartTransactionResponse
952 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
953 connectorId
: transactionConnectorId
,
956 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
958 logger
.debug(remoteStartTransactionLogMsg
);
959 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
961 return this.notifyRemoteStartTransactionRejected(
963 transactionConnectorId
,
967 return this.notifyRemoteStartTransactionRejected(
969 transactionConnectorId
,
974 private async notifyRemoteStartTransactionRejected(
975 chargingStation
: ChargingStation
,
978 ): Promise
<GenericResponse
> {
980 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
982 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
985 OCPP16ChargePointStatus
.Available
,
989 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
990 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
992 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
994 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
997 private setRemoteStartTransactionChargingProfile(
998 chargingStation
: ChargingStation
,
1000 cp
: OCPP16ChargingProfile
,
1002 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1003 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
1005 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
1006 on connector id ${connectorId}: %j`,
1010 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1012 `${chargingStation.logPrefix()} Not allowed to set ${
1013 cp.chargingProfilePurpose
1014 } charging profile(s) at remote start transaction`,
1021 private async handleRequestRemoteStopTransaction(
1022 chargingStation
: ChargingStation
,
1023 commandPayload
: RemoteStopTransactionRequest
,
1024 ): Promise
<GenericResponse
> {
1025 const transactionId
= commandPayload
.transactionId
;
1026 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1027 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1030 OCPP16ChargePointStatus
.Finishing
,
1032 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1034 OCPP16StopTransactionReason
.REMOTE
,
1036 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1037 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1039 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1041 if (chargingStation
.hasEvses
) {
1042 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1044 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1045 if (connectorStatus
.transactionId
=== transactionId
) {
1046 return remoteStopTransaction(connectorId
);
1052 for (const connectorId
of chargingStation
.connectors
.keys()) {
1055 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1057 return remoteStopTransaction(connectorId
);
1062 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1063 ${transactionId.toString()}`,
1065 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1068 private handleRequestUpdateFirmware(
1069 chargingStation
: ChargingStation
,
1070 commandPayload
: OCPP16UpdateFirmwareRequest
,
1071 ): OCPP16UpdateFirmwareResponse
{
1073 OCPP16ServiceUtils
.checkFeatureProfile(
1075 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1076 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1080 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1081 Cannot simulate firmware update: feature profile not supported`,
1083 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1086 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1087 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1090 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1091 Cannot simulate firmware update: firmware update is already in progress`,
1093 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1095 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1096 const now
= Date.now();
1097 if (retrieveDate
?.getTime() <= now
) {
1098 this.runInAsyncScope(
1099 this.updateFirmwareSimulation
.bind(this) as (
1100 this: OCPP16IncomingRequestService
,
1105 ).catch(Constants
.EMPTY_FUNCTION
);
1109 this.runInAsyncScope(
1110 this.updateFirmwareSimulation
.bind(this) as (
1111 this: OCPP16IncomingRequestService
,
1116 ).catch(Constants
.EMPTY_FUNCTION
);
1118 retrieveDate
?.getTime() - now
,
1121 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1124 private async updateFirmwareSimulation(
1125 chargingStation
: ChargingStation
,
1129 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1132 if (chargingStation
.hasEvses
) {
1133 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1135 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1136 if (connectorStatus
?.transactionStarted
=== false) {
1137 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1140 OCPP16ChargePointStatus
.Unavailable
,
1147 for (const connectorId
of chargingStation
.connectors
.keys()) {
1150 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1152 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1155 OCPP16ChargePointStatus
.Unavailable
,
1160 await chargingStation
.ocppRequestService
.requestHandler
<
1161 OCPP16FirmwareStatusNotificationRequest
,
1162 OCPP16FirmwareStatusNotificationResponse
1163 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1164 status: OCPP16FirmwareStatus
.Downloading
,
1166 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1168 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1169 OCPP16FirmwareStatus
.DownloadFailed
1171 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1172 await chargingStation
.ocppRequestService
.requestHandler
<
1173 OCPP16FirmwareStatusNotificationRequest
,
1174 OCPP16FirmwareStatusNotificationResponse
1175 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1176 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1178 chargingStation
.stationInfo
.firmwareStatus
=
1179 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1182 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1183 await chargingStation
.ocppRequestService
.requestHandler
<
1184 OCPP16FirmwareStatusNotificationRequest
,
1185 OCPP16FirmwareStatusNotificationResponse
1186 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1187 status: OCPP16FirmwareStatus
.Downloaded
,
1189 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1190 let wasTransactionsStarted
= false;
1191 let transactionsStarted
: boolean;
1193 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1194 if (runningTransactions
> 0) {
1195 const waitTime
= 15 * 1000;
1197 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1198 ${runningTransactions} transaction(s) in progress, waiting ${
1200 } seconds before continuing firmware update simulation`,
1202 await sleep(waitTime
);
1203 transactionsStarted
= true;
1204 wasTransactionsStarted
= true;
1206 if (chargingStation
.hasEvses
) {
1207 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1209 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1210 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1211 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1214 OCPP16ChargePointStatus
.Unavailable
,
1221 for (const connectorId
of chargingStation
.connectors
.keys()) {
1224 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1225 OCPP16ChargePointStatus
.Unavailable
1227 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1230 OCPP16ChargePointStatus
.Unavailable
,
1235 transactionsStarted
= false;
1237 } while (transactionsStarted
);
1238 !wasTransactionsStarted
&& (await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000));
1239 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1242 await chargingStation
.ocppRequestService
.requestHandler
<
1243 OCPP16FirmwareStatusNotificationRequest
,
1244 OCPP16FirmwareStatusNotificationResponse
1245 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1246 status: OCPP16FirmwareStatus
.Installing
,
1248 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1250 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1251 OCPP16FirmwareStatus
.InstallationFailed
1253 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1254 await chargingStation
.ocppRequestService
.requestHandler
<
1255 OCPP16FirmwareStatusNotificationRequest
,
1256 OCPP16FirmwareStatusNotificationResponse
1257 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1258 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1260 chargingStation
.stationInfo
.firmwareStatus
=
1261 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1264 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1265 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1266 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1270 private async handleRequestGetDiagnostics(
1271 chargingStation
: ChargingStation
,
1272 commandPayload
: GetDiagnosticsRequest
,
1273 ): Promise
<GetDiagnosticsResponse
> {
1275 OCPP16ServiceUtils
.checkFeatureProfile(
1277 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1278 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1282 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1283 Cannot get diagnostics: feature profile not supported`,
1285 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1287 const uri
= new URL(commandPayload
.location
);
1288 if (uri
.protocol
.startsWith('ftp:')) {
1289 let ftpClient
: Client
| undefined;
1291 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1292 .filter((file
) => file
.endsWith('.log'))
1293 .map((file
) => join('./', file
));
1294 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1295 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1296 ftpClient
= new Client();
1297 const accessResponse
= await ftpClient
.access({
1299 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1300 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1301 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1303 let uploadResponse
: FTPResponse
| undefined;
1304 if (accessResponse
.code
=== 220) {
1305 ftpClient
.trackProgress((info
) => {
1307 `${chargingStation.logPrefix()} ${
1309 } bytes transferred from diagnostics archive ${info.name}`,
1311 chargingStation
.ocppRequestService
1313 OCPP16DiagnosticsStatusNotificationRequest
,
1314 OCPP16DiagnosticsStatusNotificationResponse
1315 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1316 status: OCPP16DiagnosticsStatus
.Uploading
,
1320 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1321 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1326 uploadResponse
= await ftpClient
.uploadFrom(
1327 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1328 `${uri.pathname}${diagnosticsArchive}`,
1330 if (uploadResponse
.code
=== 226) {
1331 await chargingStation
.ocppRequestService
.requestHandler
<
1332 OCPP16DiagnosticsStatusNotificationRequest
,
1333 OCPP16DiagnosticsStatusNotificationResponse
1334 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1335 status: OCPP16DiagnosticsStatus
.Uploaded
,
1340 return { fileName
: diagnosticsArchive
};
1342 throw new OCPPError(
1343 ErrorType
.GENERIC_ERROR
,
1344 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1345 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1347 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1350 throw new OCPPError(
1351 ErrorType
.GENERIC_ERROR
,
1352 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1353 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1355 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1358 await chargingStation
.ocppRequestService
.requestHandler
<
1359 OCPP16DiagnosticsStatusNotificationRequest
,
1360 OCPP16DiagnosticsStatusNotificationResponse
1361 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1362 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1367 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1369 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1371 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1376 `${chargingStation.logPrefix()} Unsupported protocol ${
1378 } to transfer the diagnostic logs archive`,
1380 await chargingStation
.ocppRequestService
.requestHandler
<
1381 OCPP16DiagnosticsStatusNotificationRequest
,
1382 OCPP16DiagnosticsStatusNotificationResponse
1383 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1384 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1386 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1390 private handleRequestTriggerMessage(
1391 chargingStation
: ChargingStation
,
1392 commandPayload
: OCPP16TriggerMessageRequest
,
1393 ): OCPP16TriggerMessageResponse
{
1395 !OCPP16ServiceUtils
.checkFeatureProfile(
1397 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1398 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1400 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1402 commandPayload
.requestedMessage
,
1405 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1408 !OCPP16ServiceUtils
.isConnectorIdValid(
1410 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1411 commandPayload
.connectorId
!,
1414 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1417 switch (commandPayload
.requestedMessage
) {
1418 case OCPP16MessageTrigger
.BootNotification
:
1420 chargingStation
.ocppRequestService
1421 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1423 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1424 chargingStation
.bootNotificationRequest
,
1425 { skipBufferingOnError
: true, triggerMessage
: true },
1427 .then((response
) => {
1428 chargingStation
.bootNotificationResponse
= response
;
1430 .catch(Constants
.EMPTY_FUNCTION
);
1431 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1432 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1433 case OCPP16MessageTrigger
.Heartbeat
:
1435 chargingStation
.ocppRequestService
1436 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1438 OCPP16RequestCommand
.HEARTBEAT
,
1441 triggerMessage
: true,
1444 .catch(Constants
.EMPTY_FUNCTION
);
1445 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1446 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1447 case OCPP16MessageTrigger
.StatusNotification
:
1449 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1450 chargingStation
.ocppRequestService
1451 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1453 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1455 connectorId
: commandPayload
.connectorId
,
1456 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1457 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1460 triggerMessage
: true,
1463 .catch(Constants
.EMPTY_FUNCTION
);
1465 // eslint-disable-next-line no-lonely-if
1466 if (chargingStation
.hasEvses
) {
1467 for (const evseStatus
of chargingStation
.evses
.values()) {
1468 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1469 chargingStation
.ocppRequestService
1471 OCPP16StatusNotificationRequest
,
1472 OCPP16StatusNotificationResponse
1475 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1478 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1479 status: connectorStatus
.status,
1482 triggerMessage
: true,
1485 .catch(Constants
.EMPTY_FUNCTION
);
1489 for (const connectorId
of chargingStation
.connectors
.keys()) {
1490 chargingStation
.ocppRequestService
1492 OCPP16StatusNotificationRequest
,
1493 OCPP16StatusNotificationResponse
1496 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1499 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1500 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1503 triggerMessage
: true,
1506 .catch(Constants
.EMPTY_FUNCTION
);
1510 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1511 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1513 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1516 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1518 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1520 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1525 private handleRequestDataTransfer(
1526 chargingStation
: ChargingStation
,
1527 commandPayload
: OCPP16DataTransferRequest
,
1528 ): OCPP16DataTransferResponse
{
1530 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1531 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1533 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1535 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1537 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1539 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1544 private async handleRequestReserveNow(
1545 chargingStation
: ChargingStation
,
1546 commandPayload
: OCPP16ReserveNowRequest
,
1547 ): Promise
<OCPP16ReserveNowResponse
> {
1549 !OCPP16ServiceUtils
.checkFeatureProfile(
1551 OCPP16SupportedFeatureProfiles
.Reservation
,
1552 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1555 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1557 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1558 let response
: OCPP16ReserveNowResponse
;
1560 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1561 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1563 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1564 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1566 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1567 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1569 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1570 case OCPP16ChargePointStatus
.Faulted
:
1571 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1573 case OCPP16ChargePointStatus
.Preparing
:
1574 case OCPP16ChargePointStatus
.Charging
:
1575 case OCPP16ChargePointStatus
.SuspendedEV
:
1576 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1577 case OCPP16ChargePointStatus
.Finishing
:
1578 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1580 case OCPP16ChargePointStatus
.Unavailable
:
1581 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1583 case OCPP16ChargePointStatus
.Reserved
:
1584 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1585 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1588 // eslint-disable-next-line no-fallthrough
1590 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1591 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1594 await chargingStation
.addReservation({
1595 id
: commandPayload
.reservationId
,
1598 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1603 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1604 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1606 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1608 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1613 private async handleRequestCancelReservation(
1614 chargingStation
: ChargingStation
,
1615 commandPayload
: OCPP16CancelReservationRequest
,
1616 ): Promise
<GenericResponse
> {
1618 !OCPP16ServiceUtils
.checkFeatureProfile(
1620 OCPP16SupportedFeatureProfiles
.Reservation
,
1621 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1624 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1627 const { reservationId
} = commandPayload
;
1628 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1631 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1632 does not exist on charging station`,
1634 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1636 await chargingStation
.removeReservation(
1638 ReservationTerminationReason
.RESERVATION_CANCELED
,
1640 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1642 return this.handleIncomingRequestError
<GenericResponse
>(
1644 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1646 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},