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
,
31 type GetConfigurationRequest
,
32 type GetConfigurationResponse
,
33 type GetDiagnosticsRequest
,
34 type GetDiagnosticsResponse
,
35 type IncomingRequestHandler
,
38 OCPP16AuthorizationStatus
,
39 OCPP16AvailabilityType
,
40 type OCPP16BootNotificationRequest
,
41 type OCPP16BootNotificationResponse
,
42 type OCPP16CancelReservationRequest
,
43 type OCPP16CancelReservationResponse
,
44 OCPP16ChargePointErrorCode
,
45 OCPP16ChargePointStatus
,
46 type OCPP16ChargingProfile
,
47 OCPP16ChargingProfilePurposeType
,
48 type OCPP16ChargingSchedule
,
49 type OCPP16ClearCacheRequest
,
50 type OCPP16DataTransferRequest
,
51 type OCPP16DataTransferResponse
,
52 OCPP16DataTransferStatus
,
53 OCPP16DataTransferVendorId
,
54 OCPP16DiagnosticsStatus
,
55 type OCPP16DiagnosticsStatusNotificationRequest
,
56 type OCPP16DiagnosticsStatusNotificationResponse
,
58 type OCPP16FirmwareStatusNotificationRequest
,
59 type OCPP16FirmwareStatusNotificationResponse
,
60 type OCPP16GetCompositeScheduleRequest
,
61 type OCPP16GetCompositeScheduleResponse
,
62 type OCPP16HeartbeatRequest
,
63 type OCPP16HeartbeatResponse
,
64 OCPP16IncomingRequestCommand
,
67 type OCPP16ReserveNowRequest
,
68 type OCPP16ReserveNowResponse
,
69 OCPP16StandardParametersKey
,
70 type OCPP16StartTransactionRequest
,
71 type OCPP16StartTransactionResponse
,
72 type OCPP16StatusNotificationRequest
,
73 type OCPP16StatusNotificationResponse
,
74 OCPP16StopTransactionReason
,
75 OCPP16SupportedFeatureProfiles
,
76 type OCPP16TriggerMessageRequest
,
77 type OCPP16TriggerMessageResponse
,
78 type OCPP16UpdateFirmwareRequest
,
79 type OCPP16UpdateFirmwareResponse
,
80 type OCPPConfigurationKey
,
82 type RemoteStartTransactionRequest
,
83 type RemoteStopTransactionRequest
,
85 ReservationTerminationReason
,
87 type SetChargingProfileRequest
,
88 type SetChargingProfileResponse
,
89 type StartTransactionRequest
,
90 type UnlockConnectorRequest
,
91 type UnlockConnectorResponse
,
92 } from
'../../../types';
93 import { Constants
, Utils
, logger
} from
'../../../utils';
94 import { OCPPConstants
} from
'../OCPPConstants';
95 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
97 const moduleName
= 'OCPP16IncomingRequestService';
99 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
100 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
101 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
103 public constructor() {
104 // if (new.target?.name === moduleName) {
105 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
107 super(OCPPVersion
.VERSION_16
);
108 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
109 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
110 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
111 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
113 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
114 this.handleRequestGetConfiguration
.bind(this),
117 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
118 this.handleRequestChangeConfiguration
.bind(this),
121 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
122 this.handleRequestGetCompositeSchedule
.bind(this),
125 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
126 this.handleRequestSetChargingProfile
.bind(this),
129 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
130 this.handleRequestClearChargingProfile
.bind(this),
133 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
134 this.handleRequestChangeAvailability
.bind(this),
137 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
138 this.handleRequestRemoteStartTransaction
.bind(this),
141 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
142 this.handleRequestRemoteStopTransaction
.bind(this),
144 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
145 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
146 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
147 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
148 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
150 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
151 this.handleRequestCancelReservation
.bind(this),
154 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
156 OCPP16IncomingRequestCommand
.RESET
,
157 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
158 'assets/json-schemas/ocpp/1.6/Reset.json',
164 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
165 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
166 'assets/json-schemas/ocpp/1.6/ClearCache.json',
172 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
173 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
174 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
180 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
181 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
182 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
188 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
189 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
190 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
196 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
197 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
198 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
204 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
205 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
206 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
212 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
213 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
214 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
220 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
221 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
222 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
228 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
229 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
230 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
236 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
237 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
238 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
244 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
245 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
246 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
252 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
253 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
254 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
260 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
261 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
262 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
268 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
269 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
270 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
276 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
277 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
278 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
284 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
285 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
286 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
292 this.validatePayload
= this.validatePayload
.bind(this) as (
293 chargingStation
: ChargingStation
,
294 commandName
: OCPP16IncomingRequestCommand
,
295 commandPayload
: JsonType
299 public async incomingRequestHandler(
300 chargingStation
: ChargingStation
,
302 commandName
: OCPP16IncomingRequestCommand
,
303 commandPayload
: JsonType
305 let response
: JsonType
;
307 chargingStation
.getOcppStrictCompliance() === true &&
308 chargingStation
.inPendingState() === true &&
309 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
310 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
313 ErrorType
.SECURITY_ERROR
,
314 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
318 )} while the charging station is in pending state on the central server`,
324 chargingStation
.isRegistered() === true ||
325 (chargingStation
.getOcppStrictCompliance() === false &&
326 chargingStation
.inUnknownState() === true)
329 this.incomingRequestHandlers
.has(commandName
) === true &&
330 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
333 this.validatePayload(chargingStation
, commandName
, commandPayload
);
334 // Call the method to build the response
335 response
= await this.incomingRequestHandlers
.get(commandName
)(
342 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
343 Handle incoming request error:`,
351 ErrorType
.NOT_IMPLEMENTED
,
352 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
363 ErrorType
.SECURITY_ERROR
,
364 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
368 )} while the charging station is not registered on the central server.`,
373 // Send the built response
374 await chargingStation
.ocppRequestService
.sendResponse(
382 private validatePayload(
383 chargingStation
: ChargingStation
,
384 commandName
: OCPP16IncomingRequestCommand
,
385 commandPayload
: JsonType
387 if (this.jsonSchemas
.has(commandName
) === true) {
388 return this.validateIncomingRequestPayload(
391 this.jsonSchemas
.get(commandName
),
396 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
397 for command '${commandName}' PDU validation`
402 // Simulate charging station restart
403 private handleRequestReset(
404 chargingStation
: ChargingStation
,
405 commandPayload
: ResetRequest
407 this.runInAsyncScope(
408 chargingStation
.reset
.bind(chargingStation
) as (
409 this: ChargingStation
,
413 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
414 ).catch(Constants
.EMPTY_FUNCTION
);
416 `${chargingStation.logPrefix()} ${
418 } reset command received, simulating it. The station will be
419 back online in ${Utils.formatDurationMilliSeconds(chargingStation.stationInfo.resetTime)}`
421 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
424 private async handleRequestUnlockConnector(
425 chargingStation
: ChargingStation
,
426 commandPayload
: UnlockConnectorRequest
427 ): Promise
<UnlockConnectorResponse
> {
428 const connectorId
= commandPayload
.connectorId
;
429 if (chargingStation
.hasConnector(connectorId
) === false) {
431 `${chargingStation.logPrefix()} Trying to unlock a non existing
432 connector id ${connectorId.toString()}`
434 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
436 if (connectorId
=== 0) {
438 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`
440 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
442 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
443 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
445 OCPP16StopTransactionReason
.UNLOCK_COMMAND
447 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
448 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
450 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
452 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
455 OCPP16ChargePointStatus
.Available
457 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
460 private handleRequestGetConfiguration(
461 chargingStation
: ChargingStation
,
462 commandPayload
: GetConfigurationRequest
463 ): GetConfigurationResponse
{
464 const configurationKey
: OCPPConfigurationKey
[] = [];
465 const unknownKey
: string[] = [];
466 if (Utils
.isUndefined(commandPayload
.key
) === true) {
467 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
468 if (Utils
.isUndefined(configuration
.visible
) === true) {
469 configuration
.visible
= true;
471 if (configuration
.visible
=== false) {
474 configurationKey
.push({
475 key
: configuration
.key
,
476 readonly: configuration
.readonly,
477 value
: configuration
.value
,
480 } else if (Utils
.isNotEmptyArray(commandPayload
.key
) === true) {
481 for (const key
of commandPayload
.key
) {
482 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
488 if (Utils
.isUndefined(keyFound
.visible
) === true) {
489 keyFound
.visible
= true;
491 if (keyFound
.visible
=== false) {
494 configurationKey
.push({
496 readonly: keyFound
.readonly,
497 value
: keyFound
.value
,
500 unknownKey
.push(key
);
510 private handleRequestChangeConfiguration(
511 chargingStation
: ChargingStation
,
512 commandPayload
: ChangeConfigurationRequest
513 ): ChangeConfigurationResponse
{
514 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
520 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
521 } else if (keyToChange
?.readonly === true) {
522 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
523 } else if (keyToChange
?.readonly === false) {
524 let valueChanged
= false;
525 if (keyToChange
.value
!== commandPayload
.value
) {
526 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
529 commandPayload
.value
,
534 let triggerHeartbeatRestart
= false;
535 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
536 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
538 OCPP16StandardParametersKey
.HeartbeatInterval
,
541 triggerHeartbeatRestart
= true;
543 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
544 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
546 OCPP16StandardParametersKey
.HeartBeatInterval
,
549 triggerHeartbeatRestart
= true;
551 if (triggerHeartbeatRestart
) {
552 chargingStation
.restartHeartbeat();
554 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
555 chargingStation
.restartWebSocketPing();
557 if (keyToChange
.reboot
) {
558 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
560 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
564 private handleRequestSetChargingProfile(
565 chargingStation
: ChargingStation
,
566 commandPayload
: SetChargingProfileRequest
567 ): SetChargingProfileResponse
{
569 OCPP16ServiceUtils
.checkFeatureProfile(
571 OCPP16SupportedFeatureProfiles
.SmartCharging
,
572 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
575 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
577 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
579 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
580 non existing connector id ${commandPayload.connectorId}`
582 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
585 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
586 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
587 commandPayload
.connectorId
!== 0
589 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
592 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
593 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
594 (commandPayload
.connectorId
=== 0 ||
595 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
599 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
600 on connector ${commandPayload.connectorId} without a started transaction`
602 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
604 OCPP16ServiceUtils
.setChargingProfile(
606 commandPayload
.connectorId
,
607 commandPayload
.csChargingProfiles
610 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
611 commandPayload.connectorId
613 commandPayload
.csChargingProfiles
615 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
618 private handleRequestGetCompositeSchedule(
619 chargingStation
: ChargingStation
,
620 commandPayload
: OCPP16GetCompositeScheduleRequest
621 ): OCPP16GetCompositeScheduleResponse
{
623 OCPP16ServiceUtils
.checkFeatureProfile(
625 OCPP16SupportedFeatureProfiles
.SmartCharging
,
626 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
629 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
631 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
633 `${chargingStation.logPrefix()} Trying to get composite schedule to a
634 non existing connector id ${commandPayload.connectorId}`
636 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
640 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
643 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
645 const startDate
= new Date();
646 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
647 let compositeSchedule
: OCPP16ChargingSchedule
;
648 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
650 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
652 chargingProfile
.chargingSchedule
?.startSchedule
>= startDate
&&
653 chargingProfile
.chargingSchedule
?.startSchedule
<= endDate
655 compositeSchedule
= chargingProfile
.chargingSchedule
;
660 status: GenericStatus
.Accepted
,
661 scheduleStart
: compositeSchedule
?.startSchedule
,
662 connectorId
: commandPayload
.connectorId
,
663 chargingSchedule
: compositeSchedule
,
667 private handleRequestClearChargingProfile(
668 chargingStation
: ChargingStation
,
669 commandPayload
: ClearChargingProfileRequest
670 ): ClearChargingProfileResponse
{
672 OCPP16ServiceUtils
.checkFeatureProfile(
674 OCPP16SupportedFeatureProfiles
.SmartCharging
,
675 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
678 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
680 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
682 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
683 a non existing connector id ${commandPayload.connectorId}`
685 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
688 !Utils
.isNullOrUndefined(commandPayload
.connectorId
) &&
689 Utils
.isNotEmptyArray(
690 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
693 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
695 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
696 commandPayload.connectorId
699 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
701 if (Utils
.isNullOrUndefined(commandPayload
.connectorId
)) {
702 let clearedCP
= false;
703 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
704 if (Utils
.isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
705 connectorStatus
?.chargingProfiles
?.forEach(
706 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
707 let clearCurrentCP
= false;
708 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
709 clearCurrentCP
= true;
712 !commandPayload
.chargingProfilePurpose
&&
713 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
715 clearCurrentCP
= true;
718 !chargingProfile
.stackLevel
&&
719 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
721 clearCurrentCP
= true;
724 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
725 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
727 clearCurrentCP
= true;
729 if (clearCurrentCP
) {
730 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
732 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
741 if (chargingStation
.hasEvses
) {
742 for (const evseStatus
of chargingStation
.evses
.values()) {
743 for (const connectorStatus
of evseStatus
.connectors
.values()) {
744 clearChargingProfiles(connectorStatus
);
748 for (const connectorId
of chargingStation
.connectors
.keys()) {
749 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
));
753 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
756 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
759 private async handleRequestChangeAvailability(
760 chargingStation
: ChargingStation
,
761 commandPayload
: ChangeAvailabilityRequest
762 ): Promise
<ChangeAvailabilityResponse
> {
763 const connectorId
: number = commandPayload
.connectorId
;
764 if (chargingStation
.hasConnector(connectorId
) === false) {
766 `${chargingStation.logPrefix()} Trying to change the availability of a
767 non existing connector id ${connectorId.toString()}`
769 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
771 const chargePointStatus
: OCPP16ChargePointStatus
=
772 commandPayload
.type === OCPP16AvailabilityType
.Operative
773 ? OCPP16ChargePointStatus
.Available
774 : OCPP16ChargePointStatus
.Unavailable
;
775 if (connectorId
=== 0) {
776 let response
: ChangeAvailabilityResponse
=
777 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
778 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
779 if (connectorStatus
?.transactionStarted
=== true) {
780 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
782 connectorStatus
.availability
= commandPayload
.type;
783 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
784 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
791 if (chargingStation
.hasEvses
) {
792 for (const evseStatus
of chargingStation
.evses
.values()) {
793 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
794 await changeAvailability(id
, connectorStatus
);
798 for (const id
of chargingStation
.connectors
.keys()) {
799 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
));
805 (chargingStation
.isChargingStationAvailable() === true ||
806 (chargingStation
.isChargingStationAvailable() === false &&
807 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
809 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
810 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
811 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
813 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
814 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
819 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
821 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
824 private async handleRequestRemoteStartTransaction(
825 chargingStation
: ChargingStation
,
826 commandPayload
: RemoteStartTransactionRequest
827 ): Promise
<GenericResponse
> {
828 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
830 chargingStation
.getConnectorStatus(transactionConnectorId
).status ===
831 OCPP16ChargePointStatus
.Reserved
;
832 const reservedOnConnectorZero
=
833 chargingStation
.getConnectorStatus(0).status === OCPP16ChargePointStatus
.Reserved
;
836 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
837 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
839 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
841 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
842 return this.notifyRemoteStartTransactionRejected(
844 transactionConnectorId
,
849 !chargingStation
.isChargingStationAvailable() ||
850 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
852 return this.notifyRemoteStartTransactionRejected(
854 transactionConnectorId
,
858 const remoteStartTransactionLogMsg
= `
859 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
860 chargingStation.stationInfo.chargingStationId
861 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
862 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
864 transactionConnectorId
,
865 OCPP16ChargePointStatus
.Preparing
867 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
868 // Check if authorized
870 chargingStation
.getAuthorizeRemoteTxRequests() &&
871 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
873 // Authorization successful, start transaction
875 this.setRemoteStartTransactionChargingProfile(
877 transactionConnectorId
,
881 connectorStatus
.transactionRemoteStarted
= true;
882 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
883 connectorId
: transactionConnectorId
,
886 if (reserved
|| reservedOnConnectorZero
) {
887 const reservation
= chargingStation
.getReservationBy(
888 ReservationFilterKey
.CONNECTOR_ID
,
889 reservedOnConnectorZero
? 0 : transactionConnectorId
891 startTransactionPayload
.reservationId
= reservation
.id
;
892 await chargingStation
.removeReservation(
894 ReservationTerminationReason
.TRANSACTION_STARTED
899 await chargingStation
.ocppRequestService
.requestHandler
<
900 OCPP16StartTransactionRequest
,
901 OCPP16StartTransactionResponse
902 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
903 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
905 logger
.debug(remoteStartTransactionLogMsg
);
906 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
908 return this.notifyRemoteStartTransactionRejected(
910 transactionConnectorId
,
914 return this.notifyRemoteStartTransactionRejected(
916 transactionConnectorId
,
920 // No authorization check required, start transaction
922 this.setRemoteStartTransactionChargingProfile(
924 transactionConnectorId
,
928 connectorStatus
.transactionRemoteStarted
= true;
931 await chargingStation
.ocppRequestService
.requestHandler
<
932 OCPP16StartTransactionRequest
,
933 OCPP16StartTransactionResponse
934 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
935 connectorId
: transactionConnectorId
,
938 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
940 logger
.debug(remoteStartTransactionLogMsg
);
941 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
943 return this.notifyRemoteStartTransactionRejected(
945 transactionConnectorId
,
949 return this.notifyRemoteStartTransactionRejected(
951 transactionConnectorId
,
956 private async notifyRemoteStartTransactionRejected(
957 chargingStation
: ChargingStation
,
960 ): Promise
<GenericResponse
> {
962 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
964 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
967 OCPP16ChargePointStatus
.Available
971 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
972 ${connectorId.toString()}, idTag '${idTag}', availability '${
973 chargingStation.getConnectorStatus(connectorId)?.availability
974 }', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`
976 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
979 private setRemoteStartTransactionChargingProfile(
980 chargingStation
: ChargingStation
,
982 cp
: OCPP16ChargingProfile
984 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
985 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
987 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
988 on connector id ${connectorId}: %j`,
992 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
994 `${chargingStation.logPrefix()} Not allowed to set ${
995 cp.chargingProfilePurpose
996 } charging profile(s) at remote start transaction`
1004 private async handleRequestRemoteStopTransaction(
1005 chargingStation
: ChargingStation
,
1006 commandPayload
: RemoteStopTransactionRequest
1007 ): Promise
<GenericResponse
> {
1008 const transactionId
= commandPayload
.transactionId
;
1009 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1010 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1013 OCPP16ChargePointStatus
.Finishing
1015 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1017 OCPP16StopTransactionReason
.REMOTE
1019 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1020 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1022 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1024 if (chargingStation
.hasEvses
) {
1025 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1027 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1028 if (connectorStatus
.transactionId
=== transactionId
) {
1029 return remoteStopTransaction(connectorId
);
1035 for (const connectorId
of chargingStation
.connectors
.keys()) {
1038 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1040 return remoteStopTransaction(connectorId
);
1045 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1046 ${transactionId.toString()}`
1048 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1051 private handleRequestUpdateFirmware(
1052 chargingStation
: ChargingStation
,
1053 commandPayload
: OCPP16UpdateFirmwareRequest
1054 ): OCPP16UpdateFirmwareResponse
{
1056 OCPP16ServiceUtils
.checkFeatureProfile(
1058 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1059 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1063 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1064 Cannot simulate firmware update: feature profile not supported`
1066 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1069 !Utils
.isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1070 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1073 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1074 Cannot simulate firmware update: firmware update is already in progress`
1076 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1078 const retrieveDate
= Utils
.convertToDate(commandPayload
.retrieveDate
);
1079 const now
= Date.now();
1080 if (retrieveDate
?.getTime() <= now
) {
1081 this.runInAsyncScope(
1082 this.updateFirmwareSimulation
.bind(this) as (
1083 this: OCPP16IncomingRequestService
,
1088 ).catch(Constants
.EMPTY_FUNCTION
);
1091 this.updateFirmwareSimulation(chargingStation
).catch(Constants
.EMPTY_FUNCTION
);
1092 }, retrieveDate
?.getTime() - now
);
1094 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1097 private async updateFirmwareSimulation(
1098 chargingStation
: ChargingStation
,
1103 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1108 if (chargingStation
.hasEvses
) {
1109 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1111 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1112 if (connectorStatus
?.transactionStarted
=== false) {
1113 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1116 OCPP16ChargePointStatus
.Unavailable
1123 for (const connectorId
of chargingStation
.connectors
.keys()) {
1126 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1128 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1131 OCPP16ChargePointStatus
.Unavailable
1136 await chargingStation
.ocppRequestService
.requestHandler
<
1137 OCPP16FirmwareStatusNotificationRequest
,
1138 OCPP16FirmwareStatusNotificationResponse
1139 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1140 status: OCPP16FirmwareStatus
.Downloading
,
1142 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1144 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1145 OCPP16FirmwareStatus
.DownloadFailed
1147 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1148 await chargingStation
.ocppRequestService
.requestHandler
<
1149 OCPP16FirmwareStatusNotificationRequest
,
1150 OCPP16FirmwareStatusNotificationResponse
1151 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1152 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1154 chargingStation
.stationInfo
.firmwareStatus
=
1155 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1158 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1159 await chargingStation
.ocppRequestService
.requestHandler
<
1160 OCPP16FirmwareStatusNotificationRequest
,
1161 OCPP16FirmwareStatusNotificationResponse
1162 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1163 status: OCPP16FirmwareStatus
.Downloaded
,
1165 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1166 let wasTransactionsStarted
= false;
1167 let transactionsStarted
: boolean;
1169 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1170 if (runningTransactions
> 0) {
1171 const waitTime
= 15 * 1000;
1173 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1174 ${runningTransactions} transaction(s) in progress, waiting ${
1176 } seconds before continuing firmware update simulation`
1178 await Utils
.sleep(waitTime
);
1179 transactionsStarted
= true;
1180 wasTransactionsStarted
= true;
1182 if (chargingStation
.hasEvses
) {
1183 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1185 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1186 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1187 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1190 OCPP16ChargePointStatus
.Unavailable
1197 for (const connectorId
of chargingStation
.connectors
.keys()) {
1200 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1201 OCPP16ChargePointStatus
.Unavailable
1203 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1206 OCPP16ChargePointStatus
.Unavailable
1211 transactionsStarted
= false;
1213 } while (transactionsStarted
);
1214 !wasTransactionsStarted
&&
1215 (await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000));
1217 ChargingStationUtils
.checkChargingStation(chargingStation
, chargingStation
.logPrefix()) ===
1222 await chargingStation
.ocppRequestService
.requestHandler
<
1223 OCPP16FirmwareStatusNotificationRequest
,
1224 OCPP16FirmwareStatusNotificationResponse
1225 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1226 status: OCPP16FirmwareStatus
.Installing
,
1228 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1230 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1231 OCPP16FirmwareStatus
.InstallationFailed
1233 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1234 await chargingStation
.ocppRequestService
.requestHandler
<
1235 OCPP16FirmwareStatusNotificationRequest
,
1236 OCPP16FirmwareStatusNotificationResponse
1237 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1238 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1240 chargingStation
.stationInfo
.firmwareStatus
=
1241 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1244 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1245 await Utils
.sleep(Utils
.getRandomInteger(maxDelay
, minDelay
) * 1000);
1246 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1250 private async handleRequestGetDiagnostics(
1251 chargingStation
: ChargingStation
,
1252 commandPayload
: GetDiagnosticsRequest
1253 ): Promise
<GetDiagnosticsResponse
> {
1255 OCPP16ServiceUtils
.checkFeatureProfile(
1257 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1258 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1262 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1263 Cannot get diagnostics: feature profile not supported`
1265 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1267 const uri
= new URL(commandPayload
.location
);
1268 if (uri
.protocol
.startsWith('ftp:')) {
1269 let ftpClient
: Client
;
1272 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'))
1273 .filter((file
) => file
.endsWith('.log'))
1274 .map((file
) => path
.join('./', file
));
1275 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1276 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
1277 ftpClient
= new Client();
1278 const accessResponse
= await ftpClient
.access({
1280 ...(Utils
.isNotEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1281 ...(Utils
.isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1282 ...(Utils
.isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1284 let uploadResponse
: FTPResponse
;
1285 if (accessResponse
.code
=== 220) {
1286 ftpClient
.trackProgress((info
) => {
1288 `${chargingStation.logPrefix()} ${
1290 } bytes transferred from diagnostics archive ${info.name}`
1292 chargingStation
.ocppRequestService
1294 OCPP16DiagnosticsStatusNotificationRequest
,
1295 OCPP16DiagnosticsStatusNotificationResponse
1296 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1297 status: OCPP16DiagnosticsStatus
.Uploading
,
1301 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1302 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1307 uploadResponse
= await ftpClient
.uploadFrom(
1309 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
1312 `${uri.pathname}${diagnosticsArchive}`
1314 if (uploadResponse
.code
=== 226) {
1315 await chargingStation
.ocppRequestService
.requestHandler
<
1316 OCPP16DiagnosticsStatusNotificationRequest
,
1317 OCPP16DiagnosticsStatusNotificationResponse
1318 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1319 status: OCPP16DiagnosticsStatus
.Uploaded
,
1324 return { fileName
: diagnosticsArchive
};
1326 throw new OCPPError(
1327 ErrorType
.GENERIC_ERROR
,
1328 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1329 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1331 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1334 throw new OCPPError(
1335 ErrorType
.GENERIC_ERROR
,
1336 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1337 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1339 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1342 await chargingStation
.ocppRequestService
.requestHandler
<
1343 OCPP16DiagnosticsStatusNotificationRequest
,
1344 OCPP16DiagnosticsStatusNotificationResponse
1345 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1346 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1351 return this.handleIncomingRequestError(
1353 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1355 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1360 `${chargingStation.logPrefix()} Unsupported protocol ${
1362 } to transfer the diagnostic logs archive`
1364 await chargingStation
.ocppRequestService
.requestHandler
<
1365 OCPP16DiagnosticsStatusNotificationRequest
,
1366 OCPP16DiagnosticsStatusNotificationResponse
1367 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1368 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1370 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1374 private handleRequestTriggerMessage(
1375 chargingStation
: ChargingStation
,
1376 commandPayload
: OCPP16TriggerMessageRequest
1377 ): OCPP16TriggerMessageResponse
{
1379 !OCPP16ServiceUtils
.checkFeatureProfile(
1381 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1382 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1384 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1386 commandPayload
.requestedMessage
1389 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1392 !OCPP16ServiceUtils
.isConnectorIdValid(
1394 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1395 commandPayload
.connectorId
1398 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1401 switch (commandPayload
.requestedMessage
) {
1402 case OCPP16MessageTrigger
.BootNotification
:
1404 chargingStation
.ocppRequestService
1405 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1407 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1408 chargingStation
.bootNotificationRequest
,
1409 { skipBufferingOnError
: true, triggerMessage
: true }
1411 .then((response
) => {
1412 chargingStation
.bootNotificationResponse
= response
;
1414 .catch(Constants
.EMPTY_FUNCTION
);
1415 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1416 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1417 case OCPP16MessageTrigger
.Heartbeat
:
1419 chargingStation
.ocppRequestService
1420 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1422 OCPP16RequestCommand
.HEARTBEAT
,
1425 triggerMessage
: true,
1428 .catch(Constants
.EMPTY_FUNCTION
);
1429 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1430 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1431 case OCPP16MessageTrigger
.StatusNotification
:
1433 if (!Utils
.isNullOrUndefined(commandPayload
?.connectorId
)) {
1434 chargingStation
.ocppRequestService
1435 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1437 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1439 connectorId
: commandPayload
.connectorId
,
1440 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1441 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.status,
1444 triggerMessage
: true,
1447 .catch(Constants
.EMPTY_FUNCTION
);
1449 // eslint-disable-next-line no-lonely-if
1450 if (chargingStation
.hasEvses
) {
1451 for (const evseStatus
of chargingStation
.evses
.values()) {
1452 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1453 chargingStation
.ocppRequestService
1455 OCPP16StatusNotificationRequest
,
1456 OCPP16StatusNotificationResponse
1459 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1462 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1463 status: connectorStatus
.status,
1466 triggerMessage
: true,
1469 .catch(Constants
.EMPTY_FUNCTION
);
1473 for (const connectorId
of chargingStation
.connectors
.keys()) {
1474 chargingStation
.ocppRequestService
1476 OCPP16StatusNotificationRequest
,
1477 OCPP16StatusNotificationResponse
1480 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1483 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1484 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1487 triggerMessage
: true,
1490 .catch(Constants
.EMPTY_FUNCTION
);
1494 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1495 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1497 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1500 return this.handleIncomingRequestError(
1502 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1504 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1509 private handleRequestDataTransfer(
1510 chargingStation
: ChargingStation
,
1511 commandPayload
: OCPP16DataTransferRequest
1512 ): OCPP16DataTransferResponse
{
1514 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1516 status: OCPP16DataTransferStatus
.ACCEPTED
,
1520 status: OCPP16DataTransferStatus
.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 OCPPConstants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1545 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1546 let response
: OCPP16ReserveNowResponse
;
1548 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1549 return OCPPConstants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1551 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1552 return OCPPConstants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1554 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1555 return OCPPConstants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1557 switch (chargingStation
.getConnectorStatus(connectorId
).status) {
1558 case ConnectorStatusEnum
.Faulted
:
1559 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1561 case ConnectorStatusEnum
.Occupied
:
1562 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1564 case ConnectorStatusEnum
.Unavailable
:
1565 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1567 case ConnectorStatusEnum
.Reserved
:
1568 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1569 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1572 // eslint-disable-next-line no-fallthrough
1574 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1575 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1578 await chargingStation
.addReservation({
1579 id
: commandPayload
.reservationId
,
1582 response
= OCPPConstants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1587 chargingStation
.getConnectorStatus(connectorId
).status = ConnectorStatusEnum
.Available
;
1588 return this.handleIncomingRequestError(
1590 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1592 { errorResponse
: OCPPConstants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1597 private async handleRequestCancelReservation(
1598 chargingStation
: ChargingStation
,
1599 commandPayload
: OCPP16CancelReservationRequest
1600 ): Promise
<OCPP16CancelReservationResponse
> {
1602 !OCPP16ServiceUtils
.checkFeatureProfile(
1604 OCPP16SupportedFeatureProfiles
.Reservation
,
1605 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1608 return OCPPConstants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1611 const { reservationId
} = commandPayload
;
1612 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1615 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1616 does not exist on charging station`
1618 return OCPPConstants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1620 await chargingStation
.removeReservation(reservation
);
1621 return OCPPConstants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1623 return this.handleIncomingRequestError(
1625 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1627 { errorResponse
: OCPPConstants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}