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 type OCPP16CancelReservationResponse
,
43 OCPP16ChargePointErrorCode
,
44 OCPP16ChargePointStatus
,
45 type OCPP16ChargingProfile
,
46 OCPP16ChargingProfilePurposeType
,
47 type OCPP16ChargingSchedule
,
48 type OCPP16ClearCacheRequest
,
49 type OCPP16DataTransferRequest
,
50 type OCPP16DataTransferResponse
,
51 OCPP16DataTransferVendorId
,
52 OCPP16DiagnosticsStatus
,
53 type OCPP16DiagnosticsStatusNotificationRequest
,
54 type OCPP16DiagnosticsStatusNotificationResponse
,
56 type OCPP16FirmwareStatusNotificationRequest
,
57 type OCPP16FirmwareStatusNotificationResponse
,
58 type OCPP16GetCompositeScheduleRequest
,
59 type OCPP16GetCompositeScheduleResponse
,
60 type OCPP16HeartbeatRequest
,
61 type OCPP16HeartbeatResponse
,
62 OCPP16IncomingRequestCommand
,
65 type OCPP16ReserveNowRequest
,
66 type OCPP16ReserveNowResponse
,
67 OCPP16StandardParametersKey
,
68 type OCPP16StartTransactionRequest
,
69 type OCPP16StartTransactionResponse
,
70 type OCPP16StatusNotificationRequest
,
71 type OCPP16StatusNotificationResponse
,
72 OCPP16StopTransactionReason
,
73 OCPP16SupportedFeatureProfiles
,
74 type OCPP16TriggerMessageRequest
,
75 type OCPP16TriggerMessageResponse
,
76 type OCPP16UpdateFirmwareRequest
,
77 type OCPP16UpdateFirmwareResponse
,
78 type OCPPConfigurationKey
,
80 type RemoteStartTransactionRequest
,
81 type RemoteStopTransactionRequest
,
83 ReservationTerminationReason
,
85 type SetChargingProfileRequest
,
86 type SetChargingProfileResponse
,
87 type StartTransactionRequest
,
88 type UnlockConnectorRequest
,
89 type UnlockConnectorResponse
,
90 } from
'../../../types';
91 import { Constants
, Utils
, logger
} from
'../../../utils';
92 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
94 const moduleName
= 'OCPP16IncomingRequestService';
96 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
97 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
98 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
100 public constructor() {
101 // if (new.target?.name === moduleName) {
102 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
104 super(OCPPVersion
.VERSION_16
);
105 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
106 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
107 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
108 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
110 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
111 this.handleRequestGetConfiguration
.bind(this),
114 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
115 this.handleRequestChangeConfiguration
.bind(this),
118 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
119 this.handleRequestGetCompositeSchedule
.bind(this),
122 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
123 this.handleRequestSetChargingProfile
.bind(this),
126 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
127 this.handleRequestClearChargingProfile
.bind(this),
130 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
131 this.handleRequestChangeAvailability
.bind(this),
134 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
135 this.handleRequestRemoteStartTransaction
.bind(this),
138 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
139 this.handleRequestRemoteStopTransaction
.bind(this),
141 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
142 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
143 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
144 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
145 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
147 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
148 this.handleRequestCancelReservation
.bind(this),
151 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
153 OCPP16IncomingRequestCommand
.RESET
,
154 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
155 'assets/json-schemas/ocpp/1.6/Reset.json',
161 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
162 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
163 'assets/json-schemas/ocpp/1.6/ClearCache.json',
169 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
170 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
171 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
177 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
178 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
179 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
185 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
186 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
187 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
193 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
194 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
195 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
201 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
202 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
203 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
209 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
210 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
211 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
217 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
218 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
219 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
225 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
226 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
227 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
233 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
234 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
235 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
241 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
242 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
243 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
249 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
250 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
251 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
257 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
258 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
259 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
265 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
266 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
267 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
273 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
274 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
275 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
281 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
282 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
283 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
289 this.validatePayload
= this.validatePayload
.bind(this) as (
290 chargingStation
: ChargingStation
,
291 commandName
: OCPP16IncomingRequestCommand
,
292 commandPayload
: JsonType
296 public async incomingRequestHandler(
297 chargingStation
: ChargingStation
,
299 commandName
: OCPP16IncomingRequestCommand
,
300 commandPayload
: JsonType
302 let response
: JsonType
;
304 chargingStation
.getOcppStrictCompliance() === true &&
305 chargingStation
.inPendingState() === true &&
306 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
307 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
310 ErrorType
.SECURITY_ERROR
,
311 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
315 )} while the charging station is in pending state on the central server`,
321 chargingStation
.isRegistered() === true ||
322 (chargingStation
.getOcppStrictCompliance() === false &&
323 chargingStation
.inUnknownState() === true)
326 this.incomingRequestHandlers
.has(commandName
) === true &&
327 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
330 this.validatePayload(chargingStation
, commandName
, commandPayload
);
331 // Call the method to build the response
332 response
= await this.incomingRequestHandlers
.get(commandName
)(
339 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
340 Handle incoming request error:`,
348 ErrorType
.NOT_IMPLEMENTED
,
349 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
360 ErrorType
.SECURITY_ERROR
,
361 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
365 )} while the charging station is not registered on the central server.`,
370 // Send the built response
371 await chargingStation
.ocppRequestService
.sendResponse(
379 private validatePayload(
380 chargingStation
: ChargingStation
,
381 commandName
: OCPP16IncomingRequestCommand
,
382 commandPayload
: JsonType
384 if (this.jsonSchemas
.has(commandName
) === true) {
385 return this.validateIncomingRequestPayload(
388 this.jsonSchemas
.get(commandName
),
393 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
394 for command '${commandName}' PDU validation`
399 // Simulate charging station restart
400 private handleRequestReset(
401 chargingStation
: ChargingStation
,
402 commandPayload
: ResetRequest
404 this.runInAsyncScope(
405 chargingStation
.reset
.bind(chargingStation
) as (
406 this: ChargingStation
,
410 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
411 ).catch(Constants
.EMPTY_FUNCTION
);
413 `${chargingStation.logPrefix()} ${
415 } reset command received, simulating it. The station will be
416 back online in ${Utils.formatDurationMilliSeconds(chargingStation.stationInfo.resetTime)}`
418 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
421 private async handleRequestUnlockConnector(
422 chargingStation
: ChargingStation
,
423 commandPayload
: UnlockConnectorRequest
424 ): Promise
<UnlockConnectorResponse
> {
425 const connectorId
= commandPayload
.connectorId
;
426 if (chargingStation
.hasConnector(connectorId
) === false) {
428 `${chargingStation.logPrefix()} Trying to unlock a non existing
429 connector id ${connectorId.toString()}`
431 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
433 if (connectorId
=== 0) {
435 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`
437 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
439 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
440 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
442 OCPP16StopTransactionReason
.UNLOCK_COMMAND
444 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
445 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
447 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
449 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
452 OCPP16ChargePointStatus
.Available
454 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
457 private handleRequestGetConfiguration(
458 chargingStation
: ChargingStation
,
459 commandPayload
: GetConfigurationRequest
460 ): GetConfigurationResponse
{
461 const configurationKey
: OCPPConfigurationKey
[] = [];
462 const unknownKey
: string[] = [];
463 if (Utils
.isUndefined(commandPayload
.key
) === true) {
464 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
465 if (Utils
.isUndefined(configuration
.visible
) === true) {
466 configuration
.visible
= true;
468 if (configuration
.visible
=== false) {
471 configurationKey
.push({
472 key
: configuration
.key
,
473 readonly: configuration
.readonly,
474 value
: configuration
.value
,
477 } else if (Utils
.isNotEmptyArray(commandPayload
.key
) === true) {
478 for (const key
of commandPayload
.key
) {
479 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
485 if (Utils
.isUndefined(keyFound
.visible
) === true) {
486 keyFound
.visible
= true;
488 if (keyFound
.visible
=== false) {
491 configurationKey
.push({
493 readonly: keyFound
.readonly,
494 value
: keyFound
.value
,
497 unknownKey
.push(key
);
507 private handleRequestChangeConfiguration(
508 chargingStation
: ChargingStation
,
509 commandPayload
: ChangeConfigurationRequest
510 ): ChangeConfigurationResponse
{
511 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
517 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
518 } else if (keyToChange
?.readonly === true) {
519 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
520 } else if (keyToChange
?.readonly === false) {
521 let valueChanged
= false;
522 if (keyToChange
.value
!== commandPayload
.value
) {
523 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
526 commandPayload
.value
,
531 let triggerHeartbeatRestart
= false;
532 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
533 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
535 OCPP16StandardParametersKey
.HeartbeatInterval
,
538 triggerHeartbeatRestart
= true;
540 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
541 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
543 OCPP16StandardParametersKey
.HeartBeatInterval
,
546 triggerHeartbeatRestart
= true;
548 if (triggerHeartbeatRestart
) {
549 chargingStation
.restartHeartbeat();
551 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
552 chargingStation
.restartWebSocketPing();
554 if (keyToChange
.reboot
) {
555 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
557 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
561 private handleRequestSetChargingProfile(
562 chargingStation
: ChargingStation
,
563 commandPayload
: SetChargingProfileRequest
564 ): SetChargingProfileResponse
{
566 OCPP16ServiceUtils
.checkFeatureProfile(
568 OCPP16SupportedFeatureProfiles
.SmartCharging
,
569 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
572 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
574 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
576 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
577 non existing connector id ${commandPayload.connectorId}`
579 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
582 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
583 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
584 commandPayload
.connectorId
!== 0
586 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
589 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
590 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
591 (commandPayload
.connectorId
=== 0 ||
592 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
596 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
597 on connector ${commandPayload.connectorId} without a started transaction`
599 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
601 OCPP16ServiceUtils
.setChargingProfile(
603 commandPayload
.connectorId
,
604 commandPayload
.csChargingProfiles
607 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
608 commandPayload.connectorId
610 commandPayload
.csChargingProfiles
612 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
615 private handleRequestGetCompositeSchedule(
616 chargingStation
: ChargingStation
,
617 commandPayload
: OCPP16GetCompositeScheduleRequest
618 ): OCPP16GetCompositeScheduleResponse
{
620 OCPP16ServiceUtils
.checkFeatureProfile(
622 OCPP16SupportedFeatureProfiles
.SmartCharging
,
623 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
626 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
628 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
630 `${chargingStation.logPrefix()} Trying to get composite schedule to a
631 non existing connector id ${commandPayload.connectorId}`
633 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
637 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
640 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
642 const startDate
= new Date();
643 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
644 let compositeSchedule
: OCPP16ChargingSchedule
;
645 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
647 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
649 chargingProfile
.chargingSchedule
?.startSchedule
>= startDate
&&
650 chargingProfile
.chargingSchedule
?.startSchedule
<= endDate
652 compositeSchedule
= chargingProfile
.chargingSchedule
;
657 status: GenericStatus
.Accepted
,
658 scheduleStart
: compositeSchedule
?.startSchedule
,
659 connectorId
: commandPayload
.connectorId
,
660 chargingSchedule
: compositeSchedule
,
664 private handleRequestClearChargingProfile(
665 chargingStation
: ChargingStation
,
666 commandPayload
: ClearChargingProfileRequest
667 ): ClearChargingProfileResponse
{
669 OCPP16ServiceUtils
.checkFeatureProfile(
671 OCPP16SupportedFeatureProfiles
.SmartCharging
,
672 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
675 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
677 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
679 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
680 a non existing connector id ${commandPayload.connectorId}`
682 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
685 !Utils
.isNullOrUndefined(commandPayload
.connectorId
) &&
686 Utils
.isNotEmptyArray(
687 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
690 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
692 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
693 commandPayload.connectorId
696 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
698 if (Utils
.isNullOrUndefined(commandPayload
.connectorId
)) {
699 let clearedCP
= false;
700 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
701 if (Utils
.isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
702 connectorStatus
?.chargingProfiles
?.forEach(
703 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
704 let clearCurrentCP
= false;
705 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
706 clearCurrentCP
= true;
709 !commandPayload
.chargingProfilePurpose
&&
710 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
712 clearCurrentCP
= true;
715 !chargingProfile
.stackLevel
&&
716 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
718 clearCurrentCP
= true;
721 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
722 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
724 clearCurrentCP
= true;
726 if (clearCurrentCP
) {
727 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
729 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
738 if (chargingStation
.hasEvses
) {
739 for (const evseStatus
of chargingStation
.evses
.values()) {
740 for (const connectorStatus
of evseStatus
.connectors
.values()) {
741 clearChargingProfiles(connectorStatus
);
745 for (const connectorId
of chargingStation
.connectors
.keys()) {
746 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
));
750 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
753 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
756 private async handleRequestChangeAvailability(
757 chargingStation
: ChargingStation
,
758 commandPayload
: ChangeAvailabilityRequest
759 ): Promise
<ChangeAvailabilityResponse
> {
760 const connectorId
: number = commandPayload
.connectorId
;
761 if (chargingStation
.hasConnector(connectorId
) === false) {
763 `${chargingStation.logPrefix()} Trying to change the availability of a
764 non existing connector id ${connectorId.toString()}`
766 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
768 const chargePointStatus
: OCPP16ChargePointStatus
=
769 commandPayload
.type === OCPP16AvailabilityType
.Operative
770 ? OCPP16ChargePointStatus
.Available
771 : OCPP16ChargePointStatus
.Unavailable
;
772 if (connectorId
=== 0) {
773 let response
: ChangeAvailabilityResponse
=
774 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
775 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
776 if (connectorStatus
?.transactionStarted
=== true) {
777 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
779 connectorStatus
.availability
= commandPayload
.type;
780 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
781 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
788 if (chargingStation
.hasEvses
) {
789 for (const evseStatus
of chargingStation
.evses
.values()) {
790 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
791 await changeAvailability(id
, connectorStatus
);
795 for (const id
of chargingStation
.connectors
.keys()) {
796 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
));
802 (chargingStation
.isChargingStationAvailable() === true ||
803 (chargingStation
.isChargingStationAvailable() === false &&
804 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
806 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
807 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
808 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
810 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
811 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
816 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
818 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
821 private async handleRequestRemoteStartTransaction(
822 chargingStation
: ChargingStation
,
823 commandPayload
: RemoteStartTransactionRequest
824 ): Promise
<GenericResponse
> {
825 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
827 chargingStation
.getConnectorStatus(transactionConnectorId
).status ===
828 OCPP16ChargePointStatus
.Reserved
;
829 const reservedOnConnectorZero
=
830 chargingStation
.getConnectorStatus(0).status === OCPP16ChargePointStatus
.Reserved
;
833 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
834 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
836 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
838 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
839 return this.notifyRemoteStartTransactionRejected(
841 transactionConnectorId
,
846 !chargingStation
.isChargingStationAvailable() ||
847 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
849 return this.notifyRemoteStartTransactionRejected(
851 transactionConnectorId
,
855 const remoteStartTransactionLogMsg
= `
856 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
857 chargingStation.stationInfo.chargingStationId
858 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
859 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
861 transactionConnectorId
,
862 OCPP16ChargePointStatus
.Preparing
864 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
865 // Check if authorized
867 chargingStation
.getAuthorizeRemoteTxRequests() &&
868 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
870 // Authorization successful, start transaction
872 this.setRemoteStartTransactionChargingProfile(
874 transactionConnectorId
,
878 connectorStatus
.transactionRemoteStarted
= true;
879 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
880 connectorId
: transactionConnectorId
,
883 if (reserved
|| reservedOnConnectorZero
) {
884 const reservation
= chargingStation
.getReservationBy(
885 ReservationFilterKey
.CONNECTOR_ID
,
886 reservedOnConnectorZero
? 0 : transactionConnectorId
888 startTransactionPayload
.reservationId
= reservation
.id
;
889 await chargingStation
.removeReservation(
891 ReservationTerminationReason
.TRANSACTION_STARTED
896 await chargingStation
.ocppRequestService
.requestHandler
<
897 OCPP16StartTransactionRequest
,
898 OCPP16StartTransactionResponse
899 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
900 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
902 logger
.debug(remoteStartTransactionLogMsg
);
903 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
905 return this.notifyRemoteStartTransactionRejected(
907 transactionConnectorId
,
911 return this.notifyRemoteStartTransactionRejected(
913 transactionConnectorId
,
917 // No authorization check required, start transaction
919 this.setRemoteStartTransactionChargingProfile(
921 transactionConnectorId
,
925 connectorStatus
.transactionRemoteStarted
= true;
928 await chargingStation
.ocppRequestService
.requestHandler
<
929 OCPP16StartTransactionRequest
,
930 OCPP16StartTransactionResponse
931 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
932 connectorId
: transactionConnectorId
,
935 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
937 logger
.debug(remoteStartTransactionLogMsg
);
938 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
940 return this.notifyRemoteStartTransactionRejected(
942 transactionConnectorId
,
946 return this.notifyRemoteStartTransactionRejected(
948 transactionConnectorId
,
953 private async notifyRemoteStartTransactionRejected(
954 chargingStation
: ChargingStation
,
957 ): Promise
<GenericResponse
> {
959 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
961 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
964 OCPP16ChargePointStatus
.Available
968 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
969 ${connectorId.toString()}, idTag '${idTag}', availability '${
970 chargingStation.getConnectorStatus(connectorId)?.availability
971 }', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`
973 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
976 private setRemoteStartTransactionChargingProfile(
977 chargingStation
: ChargingStation
,
979 cp
: OCPP16ChargingProfile
981 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
982 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
984 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
985 on connector id ${connectorId}: %j`,
989 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
991 `${chargingStation.logPrefix()} Not allowed to set ${
992 cp.chargingProfilePurpose
993 } charging profile(s) at remote start transaction`
1001 private async handleRequestRemoteStopTransaction(
1002 chargingStation
: ChargingStation
,
1003 commandPayload
: RemoteStopTransactionRequest
1004 ): Promise
<GenericResponse
> {
1005 const transactionId
= commandPayload
.transactionId
;
1006 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1007 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1010 OCPP16ChargePointStatus
.Finishing
1012 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1014 OCPP16StopTransactionReason
.REMOTE
1016 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1017 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1019 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1021 if (chargingStation
.hasEvses
) {
1022 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1024 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1025 if (connectorStatus
.transactionId
=== transactionId
) {
1026 return remoteStopTransaction(connectorId
);
1032 for (const connectorId
of chargingStation
.connectors
.keys()) {
1035 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1037 return remoteStopTransaction(connectorId
);
1042 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1043 ${transactionId.toString()}`
1045 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1048 private handleRequestUpdateFirmware(
1049 chargingStation
: ChargingStation
,
1050 commandPayload
: OCPP16UpdateFirmwareRequest
1051 ): OCPP16UpdateFirmwareResponse
{
1053 OCPP16ServiceUtils
.checkFeatureProfile(
1055 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1056 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1060 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1061 Cannot simulate firmware update: feature profile not supported`
1063 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1066 !Utils
.isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1067 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1070 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1071 Cannot simulate firmware update: firmware update is already in progress`
1073 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1075 const retrieveDate
= Utils
.convertToDate(commandPayload
.retrieveDate
);
1076 const now
= Date.now();
1077 if (retrieveDate
?.getTime() <= now
) {
1078 this.runInAsyncScope(
1079 this.updateFirmwareSimulation
.bind(this) as (
1080 this: OCPP16IncomingRequestService
,
1085 ).catch(Constants
.EMPTY_FUNCTION
);
1088 this.runInAsyncScope(
1089 this.updateFirmwareSimulation
.bind(this) as (
1090 this: OCPP16IncomingRequestService
,
1095 ).catch(Constants
.EMPTY_FUNCTION
);
1096 }, retrieveDate
?.getTime() - now
);
1098 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1101 private async updateFirmwareSimulation(
1102 chargingStation
: ChargingStation
,
1107 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1112 if (chargingStation
.hasEvses
) {
1113 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1115 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1116 if (connectorStatus
?.transactionStarted
=== false) {
1117 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1120 OCPP16ChargePointStatus
.Unavailable
1127 for (const connectorId
of chargingStation
.connectors
.keys()) {
1130 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1132 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1135 OCPP16ChargePointStatus
.Unavailable
1140 await chargingStation
.ocppRequestService
.requestHandler
<
1141 OCPP16FirmwareStatusNotificationRequest
,
1142 OCPP16FirmwareStatusNotificationResponse
1143 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1144 status: OCPP16FirmwareStatus
.Downloading
,
1146 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1148 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1149 OCPP16FirmwareStatus
.DownloadFailed
1151 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1152 await chargingStation
.ocppRequestService
.requestHandler
<
1153 OCPP16FirmwareStatusNotificationRequest
,
1154 OCPP16FirmwareStatusNotificationResponse
1155 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1156 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1158 chargingStation
.stationInfo
.firmwareStatus
=
1159 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1162 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1163 await chargingStation
.ocppRequestService
.requestHandler
<
1164 OCPP16FirmwareStatusNotificationRequest
,
1165 OCPP16FirmwareStatusNotificationResponse
1166 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1167 status: OCPP16FirmwareStatus
.Downloaded
,
1169 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1170 let wasTransactionsStarted
= false;
1171 let transactionsStarted
: boolean;
1173 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1174 if (runningTransactions
> 0) {
1175 const waitTime
= 15 * 1000;
1177 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1178 ${runningTransactions} transaction(s) in progress, waiting ${
1180 } seconds before continuing firmware update simulation`
1182 await Utils
.sleep(waitTime
);
1183 transactionsStarted
= true;
1184 wasTransactionsStarted
= true;
1186 if (chargingStation
.hasEvses
) {
1187 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1189 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1190 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1191 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1194 OCPP16ChargePointStatus
.Unavailable
1201 for (const connectorId
of chargingStation
.connectors
.keys()) {
1204 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1205 OCPP16ChargePointStatus
.Unavailable
1207 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1210 OCPP16ChargePointStatus
.Unavailable
1215 transactionsStarted
= false;
1217 } while (transactionsStarted
);
1218 !wasTransactionsStarted
&&
1219 (await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000));
1221 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1226 await chargingStation
.ocppRequestService
.requestHandler
<
1227 OCPP16FirmwareStatusNotificationRequest
,
1228 OCPP16FirmwareStatusNotificationResponse
1229 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1230 status: OCPP16FirmwareStatus
.Installing
,
1232 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1234 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1235 OCPP16FirmwareStatus
.InstallationFailed
1237 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1238 await chargingStation
.ocppRequestService
.requestHandler
<
1239 OCPP16FirmwareStatusNotificationRequest
,
1240 OCPP16FirmwareStatusNotificationResponse
1241 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1242 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1244 chargingStation
.stationInfo
.firmwareStatus
=
1245 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1248 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1249 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1250 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1254 private async handleRequestGetDiagnostics(
1255 chargingStation
: ChargingStation
,
1256 commandPayload
: GetDiagnosticsRequest
1257 ): Promise
<GetDiagnosticsResponse
> {
1259 OCPP16ServiceUtils
.checkFeatureProfile(
1261 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1262 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1266 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1267 Cannot get diagnostics: feature profile not supported`
1269 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1271 const uri
= new URL(commandPayload
.location
);
1272 if (uri
.protocol
.startsWith('ftp:')) {
1273 let ftpClient
: Client
;
1276 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'))
1277 .filter((file
) => file
.endsWith('.log'))
1278 .map((file
) => path
.join('./', file
));
1279 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1280 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
1281 ftpClient
= new Client();
1282 const accessResponse
= await ftpClient
.access({
1284 ...(Utils
.isNotEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1285 ...(Utils
.isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1286 ...(Utils
.isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1288 let uploadResponse
: FTPResponse
;
1289 if (accessResponse
.code
=== 220) {
1290 ftpClient
.trackProgress((info
) => {
1292 `${chargingStation.logPrefix()} ${
1294 } bytes transferred from diagnostics archive ${info.name}`
1296 chargingStation
.ocppRequestService
1298 OCPP16DiagnosticsStatusNotificationRequest
,
1299 OCPP16DiagnosticsStatusNotificationResponse
1300 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1301 status: OCPP16DiagnosticsStatus
.Uploading
,
1305 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1306 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1311 uploadResponse
= await ftpClient
.uploadFrom(
1313 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
1316 `${uri.pathname}${diagnosticsArchive}`
1318 if (uploadResponse
.code
=== 226) {
1319 await chargingStation
.ocppRequestService
.requestHandler
<
1320 OCPP16DiagnosticsStatusNotificationRequest
,
1321 OCPP16DiagnosticsStatusNotificationResponse
1322 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1323 status: OCPP16DiagnosticsStatus
.Uploaded
,
1328 return { fileName
: diagnosticsArchive
};
1330 throw new OCPPError(
1331 ErrorType
.GENERIC_ERROR
,
1332 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1333 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1335 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1338 throw new OCPPError(
1339 ErrorType
.GENERIC_ERROR
,
1340 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1341 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1343 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1346 await chargingStation
.ocppRequestService
.requestHandler
<
1347 OCPP16DiagnosticsStatusNotificationRequest
,
1348 OCPP16DiagnosticsStatusNotificationResponse
1349 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1350 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1355 return this.handleIncomingRequestError(
1357 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1359 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1364 `${chargingStation.logPrefix()} Unsupported protocol ${
1366 } to transfer the diagnostic logs archive`
1368 await chargingStation
.ocppRequestService
.requestHandler
<
1369 OCPP16DiagnosticsStatusNotificationRequest
,
1370 OCPP16DiagnosticsStatusNotificationResponse
1371 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1372 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1374 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1378 private handleRequestTriggerMessage(
1379 chargingStation
: ChargingStation
,
1380 commandPayload
: OCPP16TriggerMessageRequest
1381 ): OCPP16TriggerMessageResponse
{
1383 !OCPP16ServiceUtils
.checkFeatureProfile(
1385 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1386 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1388 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1390 commandPayload
.requestedMessage
1393 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1396 !OCPP16ServiceUtils
.isConnectorIdValid(
1398 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1399 commandPayload
.connectorId
1402 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1405 switch (commandPayload
.requestedMessage
) {
1406 case OCPP16MessageTrigger
.BootNotification
:
1408 chargingStation
.ocppRequestService
1409 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1411 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1412 chargingStation
.bootNotificationRequest
,
1413 { skipBufferingOnError
: true, triggerMessage
: true }
1415 .then((response
) => {
1416 chargingStation
.bootNotificationResponse
= response
;
1418 .catch(Constants
.EMPTY_FUNCTION
);
1419 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1420 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1421 case OCPP16MessageTrigger
.Heartbeat
:
1423 chargingStation
.ocppRequestService
1424 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1426 OCPP16RequestCommand
.HEARTBEAT
,
1429 triggerMessage
: true,
1432 .catch(Constants
.EMPTY_FUNCTION
);
1433 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1434 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1435 case OCPP16MessageTrigger
.StatusNotification
:
1437 if (!Utils
.isNullOrUndefined(commandPayload
?.connectorId
)) {
1438 chargingStation
.ocppRequestService
1439 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1441 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1443 connectorId
: commandPayload
.connectorId
,
1444 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1445 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.status,
1448 triggerMessage
: true,
1451 .catch(Constants
.EMPTY_FUNCTION
);
1453 // eslint-disable-next-line no-lonely-if
1454 if (chargingStation
.hasEvses
) {
1455 for (const evseStatus
of chargingStation
.evses
.values()) {
1456 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1457 chargingStation
.ocppRequestService
1459 OCPP16StatusNotificationRequest
,
1460 OCPP16StatusNotificationResponse
1463 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1466 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1467 status: connectorStatus
.status,
1470 triggerMessage
: true,
1473 .catch(Constants
.EMPTY_FUNCTION
);
1477 for (const connectorId
of chargingStation
.connectors
.keys()) {
1478 chargingStation
.ocppRequestService
1480 OCPP16StatusNotificationRequest
,
1481 OCPP16StatusNotificationResponse
1484 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1487 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1488 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1491 triggerMessage
: true,
1494 .catch(Constants
.EMPTY_FUNCTION
);
1498 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1499 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1501 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1504 return this.handleIncomingRequestError(
1506 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1508 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1513 private handleRequestDataTransfer(
1514 chargingStation
: ChargingStation
,
1515 commandPayload
: OCPP16DataTransferRequest
1516 ): OCPP16DataTransferResponse
{
1518 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1519 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1521 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1523 return this.handleIncomingRequestError(
1525 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1527 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1532 private async handleRequestReserveNow(
1533 chargingStation
: ChargingStation
,
1534 commandPayload
: OCPP16ReserveNowRequest
1535 ): Promise
<OCPP16ReserveNowResponse
> {
1537 !OCPP16ServiceUtils
.checkFeatureProfile(
1539 OCPP16SupportedFeatureProfiles
.Reservation
,
1540 OCPP16IncomingRequestCommand
.RESERVE_NOW
1543 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1545 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1546 let response
: OCPP16ReserveNowResponse
;
1548 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1549 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1551 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1552 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1554 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1555 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1557 switch (chargingStation
.getConnectorStatus(connectorId
).status) {
1558 case OCPP16ChargePointStatus
.Faulted
:
1559 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1561 case OCPP16ChargePointStatus
.Preparing
:
1562 case OCPP16ChargePointStatus
.Charging
:
1563 case OCPP16ChargePointStatus
.SuspendedEV
:
1564 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1565 case OCPP16ChargePointStatus
.Finishing
:
1566 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1568 case OCPP16ChargePointStatus
.Unavailable
:
1569 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1571 case OCPP16ChargePointStatus
.Reserved
:
1572 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1573 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1576 // eslint-disable-next-line no-fallthrough
1578 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1579 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1582 await chargingStation
.addReservation({
1583 id
: commandPayload
.reservationId
,
1586 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1591 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.Available
;
1592 return this.handleIncomingRequestError(
1594 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1596 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1601 private async handleRequestCancelReservation(
1602 chargingStation
: ChargingStation
,
1603 commandPayload
: OCPP16CancelReservationRequest
1604 ): Promise
<OCPP16CancelReservationResponse
> {
1606 !OCPP16ServiceUtils
.checkFeatureProfile(
1608 OCPP16SupportedFeatureProfiles
.Reservation
,
1609 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1612 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1615 const { reservationId
} = commandPayload
;
1616 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1619 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1620 does not exist on charging station`
1622 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1624 await chargingStation
.removeReservation(reservation
);
1625 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1627 return this.handleIncomingRequestError(
1629 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1631 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}