1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import fs from
'node:fs';
4 import path from
'node:path';
5 import { URL
, fileURLToPath
} from
'node:url';
7 import type { JSONSchemaType
} from
'ajv';
8 import { Client
, type FTPResponse
} from
'basic-ftp';
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';
90 import { Constants
, Utils
, logger
} from
'../../../utils';
91 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
93 const moduleName
= 'OCPP16IncomingRequestService';
95 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
96 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
97 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
99 public constructor() {
100 // if (new.target?.name === moduleName) {
101 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
103 super(OCPPVersion
.VERSION_16
);
104 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
105 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
106 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
107 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
109 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
110 this.handleRequestGetConfiguration
.bind(this),
113 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
114 this.handleRequestChangeConfiguration
.bind(this),
117 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
118 this.handleRequestGetCompositeSchedule
.bind(this),
121 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
122 this.handleRequestSetChargingProfile
.bind(this),
125 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
126 this.handleRequestClearChargingProfile
.bind(this),
129 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
130 this.handleRequestChangeAvailability
.bind(this),
133 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
134 this.handleRequestRemoteStartTransaction
.bind(this),
137 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
138 this.handleRequestRemoteStopTransaction
.bind(this),
140 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
141 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
142 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
143 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
144 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
146 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
147 this.handleRequestCancelReservation
.bind(this),
150 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
152 OCPP16IncomingRequestCommand
.RESET
,
153 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
154 'assets/json-schemas/ocpp/1.6/Reset.json',
160 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
161 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
162 'assets/json-schemas/ocpp/1.6/ClearCache.json',
168 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
169 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
170 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
176 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
177 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
178 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
184 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
185 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
186 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
192 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
193 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
194 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
200 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
201 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
202 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
208 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
209 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
210 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
216 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
217 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
218 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
224 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
225 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
226 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
232 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
233 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
234 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
240 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
241 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
242 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
248 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
249 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
250 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
256 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
257 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
258 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
264 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
265 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
266 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
272 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
273 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
274 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
280 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
281 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
282 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
288 this.validatePayload
= this.validatePayload
.bind(this) as (
289 chargingStation
: ChargingStation
,
290 commandName
: OCPP16IncomingRequestCommand
,
291 commandPayload
: JsonType
295 public async incomingRequestHandler(
296 chargingStation
: ChargingStation
,
298 commandName
: OCPP16IncomingRequestCommand
,
299 commandPayload
: JsonType
301 let response
: JsonType
;
303 chargingStation
.getOcppStrictCompliance() === true &&
304 chargingStation
.inPendingState() === true &&
305 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
306 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
309 ErrorType
.SECURITY_ERROR
,
310 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
314 )} while the charging station is in pending state on the central server`,
320 chargingStation
.isRegistered() === true ||
321 (chargingStation
.getOcppStrictCompliance() === false &&
322 chargingStation
.inUnknownState() === true)
325 this.incomingRequestHandlers
.has(commandName
) === true &&
326 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
329 this.validatePayload(chargingStation
, commandName
, commandPayload
);
330 // Call the method to build the response
331 response
= await this.incomingRequestHandlers
.get(commandName
)(
338 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
339 Handle incoming request error:`,
347 ErrorType
.NOT_IMPLEMENTED
,
348 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
359 ErrorType
.SECURITY_ERROR
,
360 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
364 )} while the charging station is not registered on the central server.`,
369 // Send the built response
370 await chargingStation
.ocppRequestService
.sendResponse(
378 private validatePayload(
379 chargingStation
: ChargingStation
,
380 commandName
: OCPP16IncomingRequestCommand
,
381 commandPayload
: JsonType
383 if (this.jsonSchemas
.has(commandName
) === true) {
384 return this.validateIncomingRequestPayload(
387 this.jsonSchemas
.get(commandName
),
392 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
393 for command '${commandName}' PDU validation`
398 // Simulate charging station restart
399 private handleRequestReset(
400 chargingStation
: ChargingStation
,
401 commandPayload
: ResetRequest
403 this.runInAsyncScope(
404 chargingStation
.reset
.bind(chargingStation
) as (
405 this: ChargingStation
,
409 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
410 ).catch(Constants
.EMPTY_FUNCTION
);
412 `${chargingStation.logPrefix()} ${
414 } reset command received, simulating it. The station will be
415 back online in ${Utils.formatDurationMilliSeconds(chargingStation.stationInfo.resetTime)}`
417 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
420 private async handleRequestUnlockConnector(
421 chargingStation
: ChargingStation
,
422 commandPayload
: UnlockConnectorRequest
423 ): Promise
<UnlockConnectorResponse
> {
424 const connectorId
= commandPayload
.connectorId
;
425 if (chargingStation
.hasConnector(connectorId
) === false) {
427 `${chargingStation.logPrefix()} Trying to unlock a non existing
428 connector id ${connectorId.toString()}`
430 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
432 if (connectorId
=== 0) {
434 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`
436 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
438 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
439 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
441 OCPP16StopTransactionReason
.UNLOCK_COMMAND
443 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
444 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
446 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
448 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
451 OCPP16ChargePointStatus
.Available
453 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
456 private handleRequestGetConfiguration(
457 chargingStation
: ChargingStation
,
458 commandPayload
: GetConfigurationRequest
459 ): GetConfigurationResponse
{
460 const configurationKey
: OCPPConfigurationKey
[] = [];
461 const unknownKey
: string[] = [];
462 if (Utils
.isUndefined(commandPayload
.key
) === true) {
463 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
464 if (Utils
.isUndefined(configuration
.visible
) === true) {
465 configuration
.visible
= true;
467 if (configuration
.visible
=== false) {
470 configurationKey
.push({
471 key
: configuration
.key
,
472 readonly: configuration
.readonly,
473 value
: configuration
.value
,
476 } else if (Utils
.isNotEmptyArray(commandPayload
.key
) === true) {
477 for (const key
of commandPayload
.key
) {
478 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
484 if (Utils
.isUndefined(keyFound
.visible
) === true) {
485 keyFound
.visible
= true;
487 if (keyFound
.visible
=== false) {
490 configurationKey
.push({
492 readonly: keyFound
.readonly,
493 value
: keyFound
.value
,
496 unknownKey
.push(key
);
506 private handleRequestChangeConfiguration(
507 chargingStation
: ChargingStation
,
508 commandPayload
: ChangeConfigurationRequest
509 ): ChangeConfigurationResponse
{
510 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
516 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
517 } else if (keyToChange
?.readonly === true) {
518 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
519 } else if (keyToChange
?.readonly === false) {
520 let valueChanged
= false;
521 if (keyToChange
.value
!== commandPayload
.value
) {
522 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
525 commandPayload
.value
,
530 let triggerHeartbeatRestart
= false;
531 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
532 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
534 OCPP16StandardParametersKey
.HeartbeatInterval
,
537 triggerHeartbeatRestart
= true;
539 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
540 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
542 OCPP16StandardParametersKey
.HeartBeatInterval
,
545 triggerHeartbeatRestart
= true;
547 if (triggerHeartbeatRestart
) {
548 chargingStation
.restartHeartbeat();
550 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
551 chargingStation
.restartWebSocketPing();
553 if (keyToChange
.reboot
) {
554 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
556 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
560 private handleRequestSetChargingProfile(
561 chargingStation
: ChargingStation
,
562 commandPayload
: SetChargingProfileRequest
563 ): SetChargingProfileResponse
{
565 OCPP16ServiceUtils
.checkFeatureProfile(
567 OCPP16SupportedFeatureProfiles
.SmartCharging
,
568 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
571 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
573 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
575 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
576 non existing connector id ${commandPayload.connectorId}`
578 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
581 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
582 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
583 commandPayload
.connectorId
!== 0
585 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
588 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
589 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
590 (commandPayload
.connectorId
=== 0 ||
591 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
595 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
596 on connector ${commandPayload.connectorId} without a started transaction`
598 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
600 OCPP16ServiceUtils
.setChargingProfile(
602 commandPayload
.connectorId
,
603 commandPayload
.csChargingProfiles
606 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
607 commandPayload.connectorId
609 commandPayload
.csChargingProfiles
611 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
614 private handleRequestGetCompositeSchedule(
615 chargingStation
: ChargingStation
,
616 commandPayload
: OCPP16GetCompositeScheduleRequest
617 ): OCPP16GetCompositeScheduleResponse
{
619 OCPP16ServiceUtils
.checkFeatureProfile(
621 OCPP16SupportedFeatureProfiles
.SmartCharging
,
622 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
625 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
627 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
629 `${chargingStation.logPrefix()} Trying to get composite schedule to a
630 non existing connector id ${commandPayload.connectorId}`
632 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
636 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
639 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
641 const startDate
= new Date();
642 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
643 let compositeSchedule
: OCPP16ChargingSchedule
;
644 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
646 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
648 chargingProfile
.chargingSchedule
?.startSchedule
>= startDate
&&
649 chargingProfile
.chargingSchedule
?.startSchedule
<= endDate
651 compositeSchedule
= chargingProfile
.chargingSchedule
;
656 status: GenericStatus
.Accepted
,
657 scheduleStart
: compositeSchedule
?.startSchedule
,
658 connectorId
: commandPayload
.connectorId
,
659 chargingSchedule
: compositeSchedule
,
663 private handleRequestClearChargingProfile(
664 chargingStation
: ChargingStation
,
665 commandPayload
: ClearChargingProfileRequest
666 ): ClearChargingProfileResponse
{
668 OCPP16ServiceUtils
.checkFeatureProfile(
670 OCPP16SupportedFeatureProfiles
.SmartCharging
,
671 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
674 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
676 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
678 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
679 a non existing connector id ${commandPayload.connectorId}`
681 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
684 !Utils
.isNullOrUndefined(commandPayload
.connectorId
) &&
685 Utils
.isNotEmptyArray(
686 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
689 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
691 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
692 commandPayload.connectorId
695 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
697 if (Utils
.isNullOrUndefined(commandPayload
.connectorId
)) {
698 let clearedCP
= false;
699 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
700 if (Utils
.isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
701 connectorStatus
?.chargingProfiles
?.forEach(
702 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
703 let clearCurrentCP
= false;
704 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
705 clearCurrentCP
= true;
708 !commandPayload
.chargingProfilePurpose
&&
709 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
711 clearCurrentCP
= true;
714 !chargingProfile
.stackLevel
&&
715 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
717 clearCurrentCP
= true;
720 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
721 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
723 clearCurrentCP
= true;
725 if (clearCurrentCP
) {
726 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
728 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
737 if (chargingStation
.hasEvses
) {
738 for (const evseStatus
of chargingStation
.evses
.values()) {
739 for (const connectorStatus
of evseStatus
.connectors
.values()) {
740 clearChargingProfiles(connectorStatus
);
744 for (const connectorId
of chargingStation
.connectors
.keys()) {
745 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
));
749 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
752 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
755 private async handleRequestChangeAvailability(
756 chargingStation
: ChargingStation
,
757 commandPayload
: ChangeAvailabilityRequest
758 ): Promise
<ChangeAvailabilityResponse
> {
759 const connectorId
: number = commandPayload
.connectorId
;
760 if (chargingStation
.hasConnector(connectorId
) === false) {
762 `${chargingStation.logPrefix()} Trying to change the availability of a
763 non existing connector id ${connectorId.toString()}`
765 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
767 const chargePointStatus
: OCPP16ChargePointStatus
=
768 commandPayload
.type === OCPP16AvailabilityType
.Operative
769 ? OCPP16ChargePointStatus
.Available
770 : OCPP16ChargePointStatus
.Unavailable
;
771 if (connectorId
=== 0) {
772 let response
: ChangeAvailabilityResponse
=
773 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
774 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
775 if (connectorStatus
?.transactionStarted
=== true) {
776 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
778 connectorStatus
.availability
= commandPayload
.type;
779 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
780 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
787 if (chargingStation
.hasEvses
) {
788 for (const evseStatus
of chargingStation
.evses
.values()) {
789 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
790 await changeAvailability(id
, connectorStatus
);
794 for (const id
of chargingStation
.connectors
.keys()) {
795 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
));
801 (chargingStation
.isChargingStationAvailable() === true ||
802 (chargingStation
.isChargingStationAvailable() === false &&
803 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
805 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
806 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
807 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
809 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
810 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
815 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
817 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
820 private async handleRequestRemoteStartTransaction(
821 chargingStation
: ChargingStation
,
822 commandPayload
: RemoteStartTransactionRequest
823 ): Promise
<GenericResponse
> {
824 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
826 chargingStation
.getConnectorStatus(transactionConnectorId
).status ===
827 OCPP16ChargePointStatus
.Reserved
;
828 const reservedOnConnectorZero
=
829 chargingStation
.getConnectorStatus(0).status === OCPP16ChargePointStatus
.Reserved
;
832 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
833 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
835 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
837 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
838 return this.notifyRemoteStartTransactionRejected(
840 transactionConnectorId
,
845 !chargingStation
.isChargingStationAvailable() ||
846 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
848 return this.notifyRemoteStartTransactionRejected(
850 transactionConnectorId
,
854 const remoteStartTransactionLogMsg
= `
855 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
856 chargingStation.stationInfo.chargingStationId
857 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
858 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
860 transactionConnectorId
,
861 OCPP16ChargePointStatus
.Preparing
863 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
864 // Check if authorized
866 chargingStation
.getAuthorizeRemoteTxRequests() &&
867 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
869 // Authorization successful, start transaction
871 this.setRemoteStartTransactionChargingProfile(
873 transactionConnectorId
,
877 connectorStatus
.transactionRemoteStarted
= true;
878 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
879 connectorId
: transactionConnectorId
,
882 if (reserved
|| reservedOnConnectorZero
) {
883 const reservation
= chargingStation
.getReservationBy(
884 ReservationFilterKey
.CONNECTOR_ID
,
885 reservedOnConnectorZero
? 0 : transactionConnectorId
887 startTransactionPayload
.reservationId
= reservation
.id
;
888 await chargingStation
.removeReservation(
890 ReservationTerminationReason
.TRANSACTION_STARTED
895 await chargingStation
.ocppRequestService
.requestHandler
<
896 OCPP16StartTransactionRequest
,
897 OCPP16StartTransactionResponse
898 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
899 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
901 logger
.debug(remoteStartTransactionLogMsg
);
902 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
904 return this.notifyRemoteStartTransactionRejected(
906 transactionConnectorId
,
910 return this.notifyRemoteStartTransactionRejected(
912 transactionConnectorId
,
916 // No authorization check required, start transaction
918 this.setRemoteStartTransactionChargingProfile(
920 transactionConnectorId
,
924 connectorStatus
.transactionRemoteStarted
= true;
927 await chargingStation
.ocppRequestService
.requestHandler
<
928 OCPP16StartTransactionRequest
,
929 OCPP16StartTransactionResponse
930 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
931 connectorId
: transactionConnectorId
,
934 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
936 logger
.debug(remoteStartTransactionLogMsg
);
937 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
939 return this.notifyRemoteStartTransactionRejected(
941 transactionConnectorId
,
945 return this.notifyRemoteStartTransactionRejected(
947 transactionConnectorId
,
952 private async notifyRemoteStartTransactionRejected(
953 chargingStation
: ChargingStation
,
956 ): Promise
<GenericResponse
> {
958 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
960 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
963 OCPP16ChargePointStatus
.Available
967 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
968 ${connectorId.toString()}, idTag '${idTag}', availability '${
969 chargingStation.getConnectorStatus(connectorId)?.availability
970 }', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`
972 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
975 private setRemoteStartTransactionChargingProfile(
976 chargingStation
: ChargingStation
,
978 cp
: OCPP16ChargingProfile
980 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
981 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
983 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
984 on connector id ${connectorId}: %j`,
988 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
990 `${chargingStation.logPrefix()} Not allowed to set ${
991 cp.chargingProfilePurpose
992 } charging profile(s) at remote start transaction`
1000 private async handleRequestRemoteStopTransaction(
1001 chargingStation
: ChargingStation
,
1002 commandPayload
: RemoteStopTransactionRequest
1003 ): Promise
<GenericResponse
> {
1004 const transactionId
= commandPayload
.transactionId
;
1005 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1006 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1009 OCPP16ChargePointStatus
.Finishing
1011 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1013 OCPP16StopTransactionReason
.REMOTE
1015 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1016 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1018 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1020 if (chargingStation
.hasEvses
) {
1021 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1023 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1024 if (connectorStatus
.transactionId
=== transactionId
) {
1025 return remoteStopTransaction(connectorId
);
1031 for (const connectorId
of chargingStation
.connectors
.keys()) {
1034 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1036 return remoteStopTransaction(connectorId
);
1041 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1042 ${transactionId.toString()}`
1044 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1047 private handleRequestUpdateFirmware(
1048 chargingStation
: ChargingStation
,
1049 commandPayload
: OCPP16UpdateFirmwareRequest
1050 ): OCPP16UpdateFirmwareResponse
{
1052 OCPP16ServiceUtils
.checkFeatureProfile(
1054 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1055 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1059 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1060 Cannot simulate firmware update: feature profile not supported`
1062 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1065 !Utils
.isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1066 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1069 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1070 Cannot simulate firmware update: firmware update is already in progress`
1072 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1074 const retrieveDate
= Utils
.convertToDate(commandPayload
.retrieveDate
);
1075 const now
= Date.now();
1076 if (retrieveDate
?.getTime() <= now
) {
1077 this.runInAsyncScope(
1078 this.updateFirmwareSimulation
.bind(this) as (
1079 this: OCPP16IncomingRequestService
,
1084 ).catch(Constants
.EMPTY_FUNCTION
);
1087 this.runInAsyncScope(
1088 this.updateFirmwareSimulation
.bind(this) as (
1089 this: OCPP16IncomingRequestService
,
1094 ).catch(Constants
.EMPTY_FUNCTION
);
1095 }, retrieveDate
?.getTime() - now
);
1097 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1100 private async updateFirmwareSimulation(
1101 chargingStation
: ChargingStation
,
1106 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1111 if (chargingStation
.hasEvses
) {
1112 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1114 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1115 if (connectorStatus
?.transactionStarted
=== false) {
1116 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1119 OCPP16ChargePointStatus
.Unavailable
1126 for (const connectorId
of chargingStation
.connectors
.keys()) {
1129 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1131 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1134 OCPP16ChargePointStatus
.Unavailable
1139 await chargingStation
.ocppRequestService
.requestHandler
<
1140 OCPP16FirmwareStatusNotificationRequest
,
1141 OCPP16FirmwareStatusNotificationResponse
1142 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1143 status: OCPP16FirmwareStatus
.Downloading
,
1145 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1147 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1148 OCPP16FirmwareStatus
.DownloadFailed
1150 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1151 await chargingStation
.ocppRequestService
.requestHandler
<
1152 OCPP16FirmwareStatusNotificationRequest
,
1153 OCPP16FirmwareStatusNotificationResponse
1154 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1155 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1157 chargingStation
.stationInfo
.firmwareStatus
=
1158 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1161 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1162 await chargingStation
.ocppRequestService
.requestHandler
<
1163 OCPP16FirmwareStatusNotificationRequest
,
1164 OCPP16FirmwareStatusNotificationResponse
1165 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1166 status: OCPP16FirmwareStatus
.Downloaded
,
1168 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1169 let wasTransactionsStarted
= false;
1170 let transactionsStarted
: boolean;
1172 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1173 if (runningTransactions
> 0) {
1174 const waitTime
= 15 * 1000;
1176 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1177 ${runningTransactions} transaction(s) in progress, waiting ${
1179 } seconds before continuing firmware update simulation`
1181 await Utils
.sleep(waitTime
);
1182 transactionsStarted
= true;
1183 wasTransactionsStarted
= true;
1185 if (chargingStation
.hasEvses
) {
1186 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1188 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1189 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1190 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1193 OCPP16ChargePointStatus
.Unavailable
1200 for (const connectorId
of chargingStation
.connectors
.keys()) {
1203 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1204 OCPP16ChargePointStatus
.Unavailable
1206 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1209 OCPP16ChargePointStatus
.Unavailable
1214 transactionsStarted
= false;
1216 } while (transactionsStarted
);
1217 !wasTransactionsStarted
&&
1218 (await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000));
1220 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1225 await chargingStation
.ocppRequestService
.requestHandler
<
1226 OCPP16FirmwareStatusNotificationRequest
,
1227 OCPP16FirmwareStatusNotificationResponse
1228 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1229 status: OCPP16FirmwareStatus
.Installing
,
1231 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1233 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1234 OCPP16FirmwareStatus
.InstallationFailed
1236 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1237 await chargingStation
.ocppRequestService
.requestHandler
<
1238 OCPP16FirmwareStatusNotificationRequest
,
1239 OCPP16FirmwareStatusNotificationResponse
1240 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1241 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1243 chargingStation
.stationInfo
.firmwareStatus
=
1244 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1247 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1248 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1249 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1253 private async handleRequestGetDiagnostics(
1254 chargingStation
: ChargingStation
,
1255 commandPayload
: GetDiagnosticsRequest
1256 ): Promise
<GetDiagnosticsResponse
> {
1258 OCPP16ServiceUtils
.checkFeatureProfile(
1260 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1261 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1265 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1266 Cannot get diagnostics: feature profile not supported`
1268 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1270 const uri
= new URL(commandPayload
.location
);
1271 if (uri
.protocol
.startsWith('ftp:')) {
1272 let ftpClient
: Client
;
1275 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'))
1276 .filter((file
) => file
.endsWith('.log'))
1277 .map((file
) => path
.join('./', file
));
1278 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1279 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
1280 ftpClient
= new Client();
1281 const accessResponse
= await ftpClient
.access({
1283 ...(Utils
.isNotEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1284 ...(Utils
.isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1285 ...(Utils
.isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1287 let uploadResponse
: FTPResponse
;
1288 if (accessResponse
.code
=== 220) {
1289 ftpClient
.trackProgress((info
) => {
1291 `${chargingStation.logPrefix()} ${
1293 } bytes transferred from diagnostics archive ${info.name}`
1295 chargingStation
.ocppRequestService
1297 OCPP16DiagnosticsStatusNotificationRequest
,
1298 OCPP16DiagnosticsStatusNotificationResponse
1299 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1300 status: OCPP16DiagnosticsStatus
.Uploading
,
1304 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1305 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1310 uploadResponse
= await ftpClient
.uploadFrom(
1312 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
1315 `${uri.pathname}${diagnosticsArchive}`
1317 if (uploadResponse
.code
=== 226) {
1318 await chargingStation
.ocppRequestService
.requestHandler
<
1319 OCPP16DiagnosticsStatusNotificationRequest
,
1320 OCPP16DiagnosticsStatusNotificationResponse
1321 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1322 status: OCPP16DiagnosticsStatus
.Uploaded
,
1327 return { fileName
: diagnosticsArchive
};
1329 throw new OCPPError(
1330 ErrorType
.GENERIC_ERROR
,
1331 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1332 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1334 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1337 throw new OCPPError(
1338 ErrorType
.GENERIC_ERROR
,
1339 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1340 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1342 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1345 await chargingStation
.ocppRequestService
.requestHandler
<
1346 OCPP16DiagnosticsStatusNotificationRequest
,
1347 OCPP16DiagnosticsStatusNotificationResponse
1348 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1349 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1354 return this.handleIncomingRequestError(
1356 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1358 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1363 `${chargingStation.logPrefix()} Unsupported protocol ${
1365 } to transfer the diagnostic logs archive`
1367 await chargingStation
.ocppRequestService
.requestHandler
<
1368 OCPP16DiagnosticsStatusNotificationRequest
,
1369 OCPP16DiagnosticsStatusNotificationResponse
1370 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1371 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1373 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1377 private handleRequestTriggerMessage(
1378 chargingStation
: ChargingStation
,
1379 commandPayload
: OCPP16TriggerMessageRequest
1380 ): OCPP16TriggerMessageResponse
{
1382 !OCPP16ServiceUtils
.checkFeatureProfile(
1384 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1385 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1387 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1389 commandPayload
.requestedMessage
1392 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1395 !OCPP16ServiceUtils
.isConnectorIdValid(
1397 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1398 commandPayload
.connectorId
1401 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1404 switch (commandPayload
.requestedMessage
) {
1405 case OCPP16MessageTrigger
.BootNotification
:
1407 chargingStation
.ocppRequestService
1408 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1410 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1411 chargingStation
.bootNotificationRequest
,
1412 { skipBufferingOnError
: true, triggerMessage
: true }
1414 .then((response
) => {
1415 chargingStation
.bootNotificationResponse
= response
;
1417 .catch(Constants
.EMPTY_FUNCTION
);
1418 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1419 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1420 case OCPP16MessageTrigger
.Heartbeat
:
1422 chargingStation
.ocppRequestService
1423 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1425 OCPP16RequestCommand
.HEARTBEAT
,
1428 triggerMessage
: true,
1431 .catch(Constants
.EMPTY_FUNCTION
);
1432 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1433 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1434 case OCPP16MessageTrigger
.StatusNotification
:
1436 if (!Utils
.isNullOrUndefined(commandPayload
?.connectorId
)) {
1437 chargingStation
.ocppRequestService
1438 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1440 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1442 connectorId
: commandPayload
.connectorId
,
1443 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1444 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.status,
1447 triggerMessage
: true,
1450 .catch(Constants
.EMPTY_FUNCTION
);
1452 // eslint-disable-next-line no-lonely-if
1453 if (chargingStation
.hasEvses
) {
1454 for (const evseStatus
of chargingStation
.evses
.values()) {
1455 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1456 chargingStation
.ocppRequestService
1458 OCPP16StatusNotificationRequest
,
1459 OCPP16StatusNotificationResponse
1462 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1465 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1466 status: connectorStatus
.status,
1469 triggerMessage
: true,
1472 .catch(Constants
.EMPTY_FUNCTION
);
1476 for (const connectorId
of chargingStation
.connectors
.keys()) {
1477 chargingStation
.ocppRequestService
1479 OCPP16StatusNotificationRequest
,
1480 OCPP16StatusNotificationResponse
1483 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1486 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1487 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1490 triggerMessage
: true,
1493 .catch(Constants
.EMPTY_FUNCTION
);
1497 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1498 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1500 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1503 return this.handleIncomingRequestError(
1505 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1507 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1512 private handleRequestDataTransfer(
1513 chargingStation
: ChargingStation
,
1514 commandPayload
: OCPP16DataTransferRequest
1515 ): OCPP16DataTransferResponse
{
1517 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1518 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1520 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1522 return this.handleIncomingRequestError(
1524 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1526 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1531 private async handleRequestReserveNow(
1532 chargingStation
: ChargingStation
,
1533 commandPayload
: OCPP16ReserveNowRequest
1534 ): Promise
<OCPP16ReserveNowResponse
> {
1536 !OCPP16ServiceUtils
.checkFeatureProfile(
1538 OCPP16SupportedFeatureProfiles
.Reservation
,
1539 OCPP16IncomingRequestCommand
.RESERVE_NOW
1542 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1544 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1545 let response
: OCPP16ReserveNowResponse
;
1547 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1548 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1550 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1551 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1553 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1554 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1556 switch (chargingStation
.getConnectorStatus(connectorId
).status) {
1557 case OCPP16ChargePointStatus
.Faulted
:
1558 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1560 case OCPP16ChargePointStatus
.Preparing
:
1561 case OCPP16ChargePointStatus
.Charging
:
1562 case OCPP16ChargePointStatus
.SuspendedEV
:
1563 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1564 case OCPP16ChargePointStatus
.Finishing
:
1565 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1567 case OCPP16ChargePointStatus
.Unavailable
:
1568 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1570 case OCPP16ChargePointStatus
.Reserved
:
1571 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1572 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1575 // eslint-disable-next-line no-fallthrough
1577 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1578 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1581 await chargingStation
.addReservation({
1582 id
: commandPayload
.reservationId
,
1585 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1590 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.Available
;
1591 return this.handleIncomingRequestError(
1593 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1595 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1600 private async handleRequestCancelReservation(
1601 chargingStation
: ChargingStation
,
1602 commandPayload
: OCPP16CancelReservationRequest
1603 ): Promise
<GenericResponse
> {
1605 !OCPP16ServiceUtils
.checkFeatureProfile(
1607 OCPP16SupportedFeatureProfiles
.Reservation
,
1608 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1611 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1614 const { reservationId
} = commandPayload
;
1615 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1618 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1619 does not exist on charging station`
1621 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1623 await chargingStation
.removeReservation(
1625 ReservationTerminationReason
.RESERVATION_CANCELED
1627 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1629 return this.handleIncomingRequestError(
1631 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1633 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}