1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { createWriteStream
, readdirSync
} from
'node:fs';
4 import { dirname
, join
, resolve
} from
'node:path';
5 import { URL
, fileURLToPath
} from
'node:url';
7 import type { JSONSchemaType
} from
'ajv';
8 import { Client
, type FTPResponse
} from
'basic-ftp';
9 import { create
} from
'tar';
11 import { OCPP16Constants
} from
'./OCPP16Constants';
12 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
15 ChargingStationConfigurationUtils
,
17 } from
'../../../charging-station';
18 import { OCPPError
} from
'../../../exception';
20 type ChangeAvailabilityRequest
,
21 type ChangeAvailabilityResponse
,
22 type ChangeConfigurationRequest
,
23 type ChangeConfigurationResponse
,
24 type ClearChargingProfileRequest
,
25 type ClearChargingProfileResponse
,
30 type GetConfigurationRequest
,
31 type GetConfigurationResponse
,
32 type GetDiagnosticsRequest
,
33 type GetDiagnosticsResponse
,
34 type IncomingRequestHandler
,
37 OCPP16AuthorizationStatus
,
38 OCPP16AvailabilityType
,
39 type OCPP16BootNotificationRequest
,
40 type OCPP16BootNotificationResponse
,
41 type OCPP16CancelReservationRequest
,
42 OCPP16ChargePointErrorCode
,
43 OCPP16ChargePointStatus
,
44 type OCPP16ChargingProfile
,
45 OCPP16ChargingProfilePurposeType
,
46 type OCPP16ChargingSchedule
,
47 type OCPP16ClearCacheRequest
,
48 type OCPP16DataTransferRequest
,
49 type OCPP16DataTransferResponse
,
50 OCPP16DataTransferVendorId
,
51 OCPP16DiagnosticsStatus
,
52 type OCPP16DiagnosticsStatusNotificationRequest
,
53 type OCPP16DiagnosticsStatusNotificationResponse
,
55 type OCPP16FirmwareStatusNotificationRequest
,
56 type OCPP16FirmwareStatusNotificationResponse
,
57 type OCPP16GetCompositeScheduleRequest
,
58 type OCPP16GetCompositeScheduleResponse
,
59 type OCPP16HeartbeatRequest
,
60 type OCPP16HeartbeatResponse
,
61 OCPP16IncomingRequestCommand
,
64 type OCPP16ReserveNowRequest
,
65 type OCPP16ReserveNowResponse
,
66 OCPP16StandardParametersKey
,
67 type OCPP16StartTransactionRequest
,
68 type OCPP16StartTransactionResponse
,
69 type OCPP16StatusNotificationRequest
,
70 type OCPP16StatusNotificationResponse
,
71 OCPP16StopTransactionReason
,
72 OCPP16SupportedFeatureProfiles
,
73 type OCPP16TriggerMessageRequest
,
74 type OCPP16TriggerMessageResponse
,
75 type OCPP16UpdateFirmwareRequest
,
76 type OCPP16UpdateFirmwareResponse
,
77 type OCPPConfigurationKey
,
79 type RemoteStartTransactionRequest
,
80 type RemoteStopTransactionRequest
,
82 ReservationTerminationReason
,
84 type SetChargingProfileRequest
,
85 type SetChargingProfileResponse
,
86 type StartTransactionRequest
,
87 type UnlockConnectorRequest
,
88 type UnlockConnectorResponse
,
89 } from
'../../../types';
94 formatDurationMilliSeconds
,
103 } from
'../../../utils';
104 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
106 const moduleName
= 'OCPP16IncomingRequestService';
108 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
109 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
110 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
112 public constructor() {
113 // if (new.target?.name === moduleName) {
114 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
116 super(OCPPVersion
.VERSION_16
);
117 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
119 OCPP16IncomingRequestCommand
.RESET
,
120 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
,
123 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
124 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
,
127 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
128 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
,
131 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
132 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
,
135 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
136 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
,
139 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
140 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
,
143 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
144 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
,
147 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
148 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
,
151 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
152 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
,
155 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
156 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
159 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
160 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
163 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
164 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
,
167 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
168 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
,
171 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
172 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
,
175 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
176 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
,
179 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
180 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
,
183 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
184 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
,
187 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
189 OCPP16IncomingRequestCommand
.RESET
,
190 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
191 'assets/json-schemas/ocpp/1.6/Reset.json',
197 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
198 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
199 'assets/json-schemas/ocpp/1.6/ClearCache.json',
205 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
207 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
213 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
214 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
215 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
221 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
222 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
223 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
229 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
231 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
237 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
238 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
239 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
245 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
246 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
247 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
253 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
255 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
261 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
262 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
263 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
269 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
270 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
271 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
277 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
279 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
285 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
286 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
287 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
293 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
294 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
295 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
301 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
302 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
303 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
309 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
310 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
311 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
317 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
318 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
319 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
325 this.validatePayload
= this.validatePayload
.bind(this) as (
326 chargingStation
: ChargingStation
,
327 commandName
: OCPP16IncomingRequestCommand
,
328 commandPayload
: JsonType
,
332 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
333 chargingStation
: ChargingStation
,
335 commandName
: OCPP16IncomingRequestCommand
,
336 commandPayload
: ReqType
,
338 let response
: ResType
;
340 chargingStation
.getOcppStrictCompliance() === true &&
341 chargingStation
.inPendingState() === true &&
342 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
343 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
346 ErrorType
.SECURITY_ERROR
,
347 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
351 )} while the charging station is in pending state on the central server`,
357 chargingStation
.isRegistered() === true ||
358 (chargingStation
.getOcppStrictCompliance() === false &&
359 chargingStation
.inUnknownState() === true)
362 this.incomingRequestHandlers
.has(commandName
) === true &&
363 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
366 this.validatePayload(chargingStation
, commandName
, commandPayload
);
367 // Call the method to build the response
368 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
375 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
376 Handle incoming request error:`,
384 ErrorType
.NOT_IMPLEMENTED
,
385 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
396 ErrorType
.SECURITY_ERROR
,
397 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
401 )} while the charging station is not registered on the central server.`,
406 // Send the built response
407 await chargingStation
.ocppRequestService
.sendResponse(
415 private validatePayload(
416 chargingStation
: ChargingStation
,
417 commandName
: OCPP16IncomingRequestCommand
,
418 commandPayload
: JsonType
,
420 if (this.jsonSchemas
.has(commandName
) === true) {
421 return this.validateIncomingRequestPayload(
424 this.jsonSchemas
.get(commandName
)!,
429 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
430 for command '${commandName}' PDU validation`,
435 // Simulate charging station restart
436 private handleRequestReset(
437 chargingStation
: ChargingStation
,
438 commandPayload
: ResetRequest
,
440 this.runInAsyncScope(
441 chargingStation
.reset
.bind(chargingStation
) as (
442 this: ChargingStation
,
446 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
,
447 ).catch(Constants
.EMPTY_FUNCTION
);
449 `${chargingStation.logPrefix()} ${
451 } reset command received, simulating it. The station will be
452 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`,
454 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
457 private async handleRequestUnlockConnector(
458 chargingStation
: ChargingStation
,
459 commandPayload
: UnlockConnectorRequest
,
460 ): Promise
<UnlockConnectorResponse
> {
461 const connectorId
= commandPayload
.connectorId
;
462 if (chargingStation
.hasConnector(connectorId
) === false) {
464 `${chargingStation.logPrefix()} Trying to unlock a non existing
465 connector id ${connectorId.toString()}`,
467 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
469 if (connectorId
=== 0) {
471 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
473 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
475 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
476 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
478 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
480 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
481 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
483 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
485 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
488 OCPP16ChargePointStatus
.Available
,
490 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
493 private handleRequestGetConfiguration(
494 chargingStation
: ChargingStation
,
495 commandPayload
: GetConfigurationRequest
,
496 ): GetConfigurationResponse
{
497 const configurationKey
: OCPPConfigurationKey
[] = [];
498 const unknownKey
: string[] = [];
499 if (isUndefined(commandPayload
.key
) === true) {
500 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
501 if (isUndefined(configuration
.visible
) === true) {
502 configuration
.visible
= true;
504 if (configuration
.visible
=== false) {
507 configurationKey
.push({
508 key
: configuration
.key
,
509 readonly: configuration
.readonly,
510 value
: configuration
.value
,
513 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
514 for (const key
of commandPayload
.key
!) {
515 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
521 if (isUndefined(keyFound
.visible
) === true) {
522 keyFound
.visible
= true;
524 if (keyFound
.visible
=== false) {
527 configurationKey
.push({
529 readonly: keyFound
.readonly,
530 value
: keyFound
.value
,
533 unknownKey
.push(key
);
543 private handleRequestChangeConfiguration(
544 chargingStation
: ChargingStation
,
545 commandPayload
: ChangeConfigurationRequest
,
546 ): ChangeConfigurationResponse
{
547 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
552 if (keyToChange
?.readonly === true) {
553 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
554 } else if (keyToChange
?.readonly === false) {
555 let valueChanged
= false;
556 if (keyToChange
.value
!== commandPayload
.value
) {
557 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
560 commandPayload
.value
,
565 let triggerHeartbeatRestart
= false;
567 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
568 OCPP16StandardParametersKey
.HeartBeatInterval
&&
571 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
573 OCPP16StandardParametersKey
.HeartbeatInterval
,
574 commandPayload
.value
,
576 triggerHeartbeatRestart
= true;
579 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
580 OCPP16StandardParametersKey
.HeartbeatInterval
&&
583 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
585 OCPP16StandardParametersKey
.HeartBeatInterval
,
586 commandPayload
.value
,
588 triggerHeartbeatRestart
= true;
590 if (triggerHeartbeatRestart
) {
591 chargingStation
.restartHeartbeat();
594 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
595 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
598 chargingStation
.restartWebSocketPing();
600 if (keyToChange
.reboot
) {
601 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
603 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
605 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
608 private handleRequestSetChargingProfile(
609 chargingStation
: ChargingStation
,
610 commandPayload
: SetChargingProfileRequest
,
611 ): SetChargingProfileResponse
{
613 OCPP16ServiceUtils
.checkFeatureProfile(
615 OCPP16SupportedFeatureProfiles
.SmartCharging
,
616 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
619 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
621 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
623 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
624 non existing connector id ${commandPayload.connectorId}`,
626 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
629 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
630 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
631 commandPayload
.connectorId
!== 0
633 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
636 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
637 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
638 (commandPayload
.connectorId
=== 0 ||
639 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
643 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
644 on connector ${commandPayload.connectorId} without a started transaction`,
646 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
648 OCPP16ServiceUtils
.setChargingProfile(
650 commandPayload
.connectorId
,
651 commandPayload
.csChargingProfiles
,
654 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
655 commandPayload.connectorId
657 commandPayload
.csChargingProfiles
,
659 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
662 private handleRequestGetCompositeSchedule(
663 chargingStation
: ChargingStation
,
664 commandPayload
: OCPP16GetCompositeScheduleRequest
,
665 ): OCPP16GetCompositeScheduleResponse
{
667 OCPP16ServiceUtils
.checkFeatureProfile(
669 OCPP16SupportedFeatureProfiles
.SmartCharging
,
670 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
673 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
675 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
677 `${chargingStation.logPrefix()} Trying to get composite schedule to a
678 non existing connector id ${commandPayload.connectorId}`,
680 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
683 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
685 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
687 const startDate
= new Date();
688 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
689 let compositeSchedule
: OCPP16ChargingSchedule
| undefined;
690 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)!
691 .chargingProfiles
!) {
692 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
694 chargingProfile
.chargingSchedule
.startSchedule
! >= startDate
&&
695 chargingProfile
.chargingSchedule
.startSchedule
! <= endDate
697 compositeSchedule
= chargingProfile
.chargingSchedule
;
702 status: GenericStatus
.Accepted
,
703 scheduleStart
: compositeSchedule
?.startSchedule
,
704 connectorId
: commandPayload
.connectorId
,
705 chargingSchedule
: compositeSchedule
,
709 private handleRequestClearChargingProfile(
710 chargingStation
: ChargingStation
,
711 commandPayload
: ClearChargingProfileRequest
,
712 ): ClearChargingProfileResponse
{
714 OCPP16ServiceUtils
.checkFeatureProfile(
716 OCPP16SupportedFeatureProfiles
.SmartCharging
,
717 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
720 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
722 if (chargingStation
.hasConnector(commandPayload
.connectorId
!) === false) {
724 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
725 a non existing connector id ${commandPayload.connectorId}`,
727 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
730 !isNullOrUndefined(commandPayload
.connectorId
) &&
732 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.chargingProfiles
,
735 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)!.chargingProfiles
= [];
737 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
738 commandPayload.connectorId
741 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
743 if (isNullOrUndefined(commandPayload
.connectorId
)) {
744 let clearedCP
= false;
745 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
746 if (isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
747 connectorStatus
?.chargingProfiles
?.forEach(
748 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
749 let clearCurrentCP
= false;
750 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
751 clearCurrentCP
= true;
754 !commandPayload
.chargingProfilePurpose
&&
755 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
757 clearCurrentCP
= true;
760 !chargingProfile
.stackLevel
&&
761 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
763 clearCurrentCP
= true;
766 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
767 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
769 clearCurrentCP
= true;
771 if (clearCurrentCP
) {
772 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
774 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
783 if (chargingStation
.hasEvses
) {
784 for (const evseStatus
of chargingStation
.evses
.values()) {
785 for (const connectorStatus
of evseStatus
.connectors
.values()) {
786 clearChargingProfiles(connectorStatus
);
790 for (const connectorId
of chargingStation
.connectors
.keys()) {
791 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
)!);
795 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
798 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
801 private async handleRequestChangeAvailability(
802 chargingStation
: ChargingStation
,
803 commandPayload
: ChangeAvailabilityRequest
,
804 ): Promise
<ChangeAvailabilityResponse
> {
805 const connectorId
: number = commandPayload
.connectorId
;
806 if (chargingStation
.hasConnector(connectorId
) === false) {
808 `${chargingStation.logPrefix()} Trying to change the availability of a
809 non existing connector id ${connectorId.toString()}`,
811 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
813 const chargePointStatus
: OCPP16ChargePointStatus
=
814 commandPayload
.type === OCPP16AvailabilityType
.Operative
815 ? OCPP16ChargePointStatus
.Available
816 : OCPP16ChargePointStatus
.Unavailable
;
817 if (connectorId
=== 0) {
818 let response
: ChangeAvailabilityResponse
=
819 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
820 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
821 if (connectorStatus
?.transactionStarted
=== true) {
822 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
824 connectorStatus
.availability
= commandPayload
.type;
825 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
826 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
833 if (chargingStation
.hasEvses
) {
834 for (const evseStatus
of chargingStation
.evses
.values()) {
835 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
836 await changeAvailability(id
, connectorStatus
);
840 for (const id
of chargingStation
.connectors
.keys()) {
841 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
)!);
847 (chargingStation
.isChargingStationAvailable() === true ||
848 (chargingStation
.isChargingStationAvailable() === false &&
849 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
851 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
852 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
853 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
855 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
856 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
861 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
863 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
866 private async handleRequestRemoteStartTransaction(
867 chargingStation
: ChargingStation
,
868 commandPayload
: RemoteStartTransactionRequest
,
869 ): Promise
<GenericResponse
> {
870 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
872 chargingStation
.getConnectorStatus(transactionConnectorId
)!.status ===
873 OCPP16ChargePointStatus
.Reserved
;
874 const reservedOnConnectorZero
=
875 chargingStation
.getConnectorStatus(0)!.status === OCPP16ChargePointStatus
.Reserved
;
878 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
879 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
881 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
883 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
884 return this.notifyRemoteStartTransactionRejected(
886 transactionConnectorId
,
891 !chargingStation
.isChargingStationAvailable() ||
892 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
894 return this.notifyRemoteStartTransactionRejected(
896 transactionConnectorId
,
900 const remoteStartTransactionLogMsg
= `
901 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
902 chargingStation.stationInfo.chargingStationId
903 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
904 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
906 transactionConnectorId
,
907 OCPP16ChargePointStatus
.Preparing
,
909 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
910 // Check if authorized
912 chargingStation
.getAuthorizeRemoteTxRequests() &&
913 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
915 // Authorization successful, start transaction
917 this.setRemoteStartTransactionChargingProfile(
919 transactionConnectorId
,
923 connectorStatus
.transactionRemoteStarted
= true;
924 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
925 connectorId
: transactionConnectorId
,
928 if (reserved
|| reservedOnConnectorZero
) {
929 const reservation
= chargingStation
.getReservationBy(
930 ReservationFilterKey
.CONNECTOR_ID
,
931 reservedOnConnectorZero
? 0 : transactionConnectorId
,
933 startTransactionPayload
.reservationId
= reservation
.id
;
934 await chargingStation
.removeReservation(
936 ReservationTerminationReason
.TRANSACTION_STARTED
,
941 await chargingStation
.ocppRequestService
.requestHandler
<
942 OCPP16StartTransactionRequest
,
943 OCPP16StartTransactionResponse
944 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
945 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
947 logger
.debug(remoteStartTransactionLogMsg
);
948 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
950 return this.notifyRemoteStartTransactionRejected(
952 transactionConnectorId
,
956 return this.notifyRemoteStartTransactionRejected(
958 transactionConnectorId
,
962 // No authorization check required, start transaction
964 this.setRemoteStartTransactionChargingProfile(
966 transactionConnectorId
,
970 connectorStatus
.transactionRemoteStarted
= true;
973 await chargingStation
.ocppRequestService
.requestHandler
<
974 OCPP16StartTransactionRequest
,
975 OCPP16StartTransactionResponse
976 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
977 connectorId
: transactionConnectorId
,
980 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
982 logger
.debug(remoteStartTransactionLogMsg
);
983 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
985 return this.notifyRemoteStartTransactionRejected(
987 transactionConnectorId
,
991 return this.notifyRemoteStartTransactionRejected(
993 transactionConnectorId
,
998 private async notifyRemoteStartTransactionRejected(
999 chargingStation
: ChargingStation
,
1000 connectorId
: number,
1002 ): Promise
<GenericResponse
> {
1004 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
1006 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1009 OCPP16ChargePointStatus
.Available
,
1013 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
1014 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
1016 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
1018 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1021 private setRemoteStartTransactionChargingProfile(
1022 chargingStation
: ChargingStation
,
1023 connectorId
: number,
1024 cp
: OCPP16ChargingProfile
,
1026 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1027 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
1029 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
1030 on connector id ${connectorId}: %j`,
1034 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1036 `${chargingStation.logPrefix()} Not allowed to set ${
1037 cp.chargingProfilePurpose
1038 } charging profile(s) at remote start transaction`,
1045 private async handleRequestRemoteStopTransaction(
1046 chargingStation
: ChargingStation
,
1047 commandPayload
: RemoteStopTransactionRequest
,
1048 ): Promise
<GenericResponse
> {
1049 const transactionId
= commandPayload
.transactionId
;
1050 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1051 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1054 OCPP16ChargePointStatus
.Finishing
,
1056 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1058 OCPP16StopTransactionReason
.REMOTE
,
1060 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1061 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1063 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1065 if (chargingStation
.hasEvses
) {
1066 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1068 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1069 if (connectorStatus
.transactionId
=== transactionId
) {
1070 return remoteStopTransaction(connectorId
);
1076 for (const connectorId
of chargingStation
.connectors
.keys()) {
1079 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1081 return remoteStopTransaction(connectorId
);
1086 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1087 ${transactionId.toString()}`,
1089 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1092 private handleRequestUpdateFirmware(
1093 chargingStation
: ChargingStation
,
1094 commandPayload
: OCPP16UpdateFirmwareRequest
,
1095 ): OCPP16UpdateFirmwareResponse
{
1097 OCPP16ServiceUtils
.checkFeatureProfile(
1099 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1100 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1104 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1105 Cannot simulate firmware update: feature profile not supported`,
1107 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1110 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1111 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1114 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1115 Cannot simulate firmware update: firmware update is already in progress`,
1117 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1119 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1120 const now
= Date.now();
1121 if (retrieveDate
?.getTime() <= now
) {
1122 this.runInAsyncScope(
1123 this.updateFirmwareSimulation
.bind(this) as (
1124 this: OCPP16IncomingRequestService
,
1129 ).catch(Constants
.EMPTY_FUNCTION
);
1133 this.runInAsyncScope(
1134 this.updateFirmwareSimulation
.bind(this) as (
1135 this: OCPP16IncomingRequestService
,
1140 ).catch(Constants
.EMPTY_FUNCTION
);
1142 retrieveDate
?.getTime() - now
,
1145 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1148 private async updateFirmwareSimulation(
1149 chargingStation
: ChargingStation
,
1153 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1156 if (chargingStation
.hasEvses
) {
1157 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1159 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1160 if (connectorStatus
?.transactionStarted
=== false) {
1161 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1164 OCPP16ChargePointStatus
.Unavailable
,
1171 for (const connectorId
of chargingStation
.connectors
.keys()) {
1174 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1176 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1179 OCPP16ChargePointStatus
.Unavailable
,
1184 await chargingStation
.ocppRequestService
.requestHandler
<
1185 OCPP16FirmwareStatusNotificationRequest
,
1186 OCPP16FirmwareStatusNotificationResponse
1187 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1188 status: OCPP16FirmwareStatus
.Downloading
,
1190 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1192 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1193 OCPP16FirmwareStatus
.DownloadFailed
1195 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1196 await chargingStation
.ocppRequestService
.requestHandler
<
1197 OCPP16FirmwareStatusNotificationRequest
,
1198 OCPP16FirmwareStatusNotificationResponse
1199 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1200 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1202 chargingStation
.stationInfo
.firmwareStatus
=
1203 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1206 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1207 await chargingStation
.ocppRequestService
.requestHandler
<
1208 OCPP16FirmwareStatusNotificationRequest
,
1209 OCPP16FirmwareStatusNotificationResponse
1210 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1211 status: OCPP16FirmwareStatus
.Downloaded
,
1213 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1214 let wasTransactionsStarted
= false;
1215 let transactionsStarted
: boolean;
1217 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1218 if (runningTransactions
> 0) {
1219 const waitTime
= 15 * 1000;
1221 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1222 ${runningTransactions} transaction(s) in progress, waiting ${
1224 } seconds before continuing firmware update simulation`,
1226 await sleep(waitTime
);
1227 transactionsStarted
= true;
1228 wasTransactionsStarted
= true;
1230 if (chargingStation
.hasEvses
) {
1231 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1233 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1234 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1235 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1238 OCPP16ChargePointStatus
.Unavailable
,
1245 for (const connectorId
of chargingStation
.connectors
.keys()) {
1248 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1249 OCPP16ChargePointStatus
.Unavailable
1251 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1254 OCPP16ChargePointStatus
.Unavailable
,
1259 transactionsStarted
= false;
1261 } while (transactionsStarted
);
1262 !wasTransactionsStarted
&& (await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000));
1263 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1266 await chargingStation
.ocppRequestService
.requestHandler
<
1267 OCPP16FirmwareStatusNotificationRequest
,
1268 OCPP16FirmwareStatusNotificationResponse
1269 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1270 status: OCPP16FirmwareStatus
.Installing
,
1272 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1274 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1275 OCPP16FirmwareStatus
.InstallationFailed
1277 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1278 await chargingStation
.ocppRequestService
.requestHandler
<
1279 OCPP16FirmwareStatusNotificationRequest
,
1280 OCPP16FirmwareStatusNotificationResponse
1281 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1282 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1284 chargingStation
.stationInfo
.firmwareStatus
=
1285 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1288 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1289 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1290 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1294 private async handleRequestGetDiagnostics(
1295 chargingStation
: ChargingStation
,
1296 commandPayload
: GetDiagnosticsRequest
,
1297 ): Promise
<GetDiagnosticsResponse
> {
1299 OCPP16ServiceUtils
.checkFeatureProfile(
1301 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1302 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1306 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1307 Cannot get diagnostics: feature profile not supported`,
1309 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1311 const uri
= new URL(commandPayload
.location
);
1312 if (uri
.protocol
.startsWith('ftp:')) {
1313 let ftpClient
: Client
| undefined;
1315 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1316 .filter((file
) => file
.endsWith('.log'))
1317 .map((file
) => join('./', file
));
1318 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1319 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1320 ftpClient
= new Client();
1321 const accessResponse
= await ftpClient
.access({
1323 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1324 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1325 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1327 let uploadResponse
: FTPResponse
| undefined;
1328 if (accessResponse
.code
=== 220) {
1329 ftpClient
.trackProgress((info
) => {
1331 `${chargingStation.logPrefix()} ${
1333 } bytes transferred from diagnostics archive ${info.name}`,
1335 chargingStation
.ocppRequestService
1337 OCPP16DiagnosticsStatusNotificationRequest
,
1338 OCPP16DiagnosticsStatusNotificationResponse
1339 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1340 status: OCPP16DiagnosticsStatus
.Uploading
,
1344 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1345 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1350 uploadResponse
= await ftpClient
.uploadFrom(
1351 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1352 `${uri.pathname}${diagnosticsArchive}`,
1354 if (uploadResponse
.code
=== 226) {
1355 await chargingStation
.ocppRequestService
.requestHandler
<
1356 OCPP16DiagnosticsStatusNotificationRequest
,
1357 OCPP16DiagnosticsStatusNotificationResponse
1358 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1359 status: OCPP16DiagnosticsStatus
.Uploaded
,
1364 return { fileName
: diagnosticsArchive
};
1366 throw new OCPPError(
1367 ErrorType
.GENERIC_ERROR
,
1368 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1369 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1371 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1374 throw new OCPPError(
1375 ErrorType
.GENERIC_ERROR
,
1376 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1377 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1379 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1382 await chargingStation
.ocppRequestService
.requestHandler
<
1383 OCPP16DiagnosticsStatusNotificationRequest
,
1384 OCPP16DiagnosticsStatusNotificationResponse
1385 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1386 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1391 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1393 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1395 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1400 `${chargingStation.logPrefix()} Unsupported protocol ${
1402 } to transfer the diagnostic logs archive`,
1404 await chargingStation
.ocppRequestService
.requestHandler
<
1405 OCPP16DiagnosticsStatusNotificationRequest
,
1406 OCPP16DiagnosticsStatusNotificationResponse
1407 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1408 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1410 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1414 private handleRequestTriggerMessage(
1415 chargingStation
: ChargingStation
,
1416 commandPayload
: OCPP16TriggerMessageRequest
,
1417 ): OCPP16TriggerMessageResponse
{
1419 !OCPP16ServiceUtils
.checkFeatureProfile(
1421 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1422 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1424 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1426 commandPayload
.requestedMessage
,
1429 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1432 !OCPP16ServiceUtils
.isConnectorIdValid(
1434 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1435 commandPayload
.connectorId
!,
1438 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1441 switch (commandPayload
.requestedMessage
) {
1442 case OCPP16MessageTrigger
.BootNotification
:
1444 chargingStation
.ocppRequestService
1445 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1447 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1448 chargingStation
.bootNotificationRequest
,
1449 { skipBufferingOnError
: true, triggerMessage
: true },
1451 .then((response
) => {
1452 chargingStation
.bootNotificationResponse
= response
;
1454 .catch(Constants
.EMPTY_FUNCTION
);
1455 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1456 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1457 case OCPP16MessageTrigger
.Heartbeat
:
1459 chargingStation
.ocppRequestService
1460 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1462 OCPP16RequestCommand
.HEARTBEAT
,
1465 triggerMessage
: true,
1468 .catch(Constants
.EMPTY_FUNCTION
);
1469 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1470 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1471 case OCPP16MessageTrigger
.StatusNotification
:
1473 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1474 chargingStation
.ocppRequestService
1475 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1477 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1479 connectorId
: commandPayload
.connectorId
,
1480 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1481 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1484 triggerMessage
: true,
1487 .catch(Constants
.EMPTY_FUNCTION
);
1489 // eslint-disable-next-line no-lonely-if
1490 if (chargingStation
.hasEvses
) {
1491 for (const evseStatus
of chargingStation
.evses
.values()) {
1492 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1493 chargingStation
.ocppRequestService
1495 OCPP16StatusNotificationRequest
,
1496 OCPP16StatusNotificationResponse
1499 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1502 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1503 status: connectorStatus
.status,
1506 triggerMessage
: true,
1509 .catch(Constants
.EMPTY_FUNCTION
);
1513 for (const connectorId
of chargingStation
.connectors
.keys()) {
1514 chargingStation
.ocppRequestService
1516 OCPP16StatusNotificationRequest
,
1517 OCPP16StatusNotificationResponse
1520 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1523 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1524 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1527 triggerMessage
: true,
1530 .catch(Constants
.EMPTY_FUNCTION
);
1534 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1535 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1537 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1540 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1542 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1544 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1549 private handleRequestDataTransfer(
1550 chargingStation
: ChargingStation
,
1551 commandPayload
: OCPP16DataTransferRequest
,
1552 ): OCPP16DataTransferResponse
{
1554 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1555 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1557 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1559 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1561 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1563 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1568 private async handleRequestReserveNow(
1569 chargingStation
: ChargingStation
,
1570 commandPayload
: OCPP16ReserveNowRequest
,
1571 ): Promise
<OCPP16ReserveNowResponse
> {
1573 !OCPP16ServiceUtils
.checkFeatureProfile(
1575 OCPP16SupportedFeatureProfiles
.Reservation
,
1576 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1579 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1581 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1582 let response
: OCPP16ReserveNowResponse
;
1584 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1585 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1587 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1588 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1590 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1591 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1593 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1594 case OCPP16ChargePointStatus
.Faulted
:
1595 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1597 case OCPP16ChargePointStatus
.Preparing
:
1598 case OCPP16ChargePointStatus
.Charging
:
1599 case OCPP16ChargePointStatus
.SuspendedEV
:
1600 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1601 case OCPP16ChargePointStatus
.Finishing
:
1602 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1604 case OCPP16ChargePointStatus
.Unavailable
:
1605 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1607 case OCPP16ChargePointStatus
.Reserved
:
1608 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1609 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1612 // eslint-disable-next-line no-fallthrough
1614 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1615 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1618 await chargingStation
.addReservation({
1619 id
: commandPayload
.reservationId
,
1622 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1627 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1628 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1630 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1632 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1637 private async handleRequestCancelReservation(
1638 chargingStation
: ChargingStation
,
1639 commandPayload
: OCPP16CancelReservationRequest
,
1640 ): Promise
<GenericResponse
> {
1642 !OCPP16ServiceUtils
.checkFeatureProfile(
1644 OCPP16SupportedFeatureProfiles
.Reservation
,
1645 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1648 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1651 const { reservationId
} = commandPayload
;
1652 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1655 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1656 does not exist on charging station`,
1658 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1660 await chargingStation
.removeReservation(
1662 ReservationTerminationReason
.RESERVATION_CANCELED
,
1664 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1666 return this.handleIncomingRequestError
<GenericResponse
>(
1668 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1670 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},