1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { createWriteStream
, readdirSync
} from
'node:fs';
4 import { dirname
, join
, resolve
} from
'node:path';
5 import { URL
, fileURLToPath
} from
'node:url';
7 import type { JSONSchemaType
} from
'ajv';
8 import { Client
, type FTPResponse
} from
'basic-ftp';
9 import { create
} from
'tar';
11 import { OCPP16Constants
} from
'./OCPP16Constants';
12 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
15 ChargingStationConfigurationUtils
,
17 } from
'../../../charging-station';
18 import { OCPPError
} from
'../../../exception';
20 type ChangeAvailabilityRequest
,
21 type ChangeAvailabilityResponse
,
22 type ChangeConfigurationRequest
,
23 type ChangeConfigurationResponse
,
24 type ClearChargingProfileRequest
,
25 type ClearChargingProfileResponse
,
30 type GetConfigurationRequest
,
31 type GetConfigurationResponse
,
32 type GetDiagnosticsRequest
,
33 type GetDiagnosticsResponse
,
34 type IncomingRequestHandler
,
37 OCPP16AuthorizationStatus
,
38 OCPP16AvailabilityType
,
39 type OCPP16BootNotificationRequest
,
40 type OCPP16BootNotificationResponse
,
41 type OCPP16CancelReservationRequest
,
42 OCPP16ChargePointErrorCode
,
43 OCPP16ChargePointStatus
,
44 type OCPP16ChargingProfile
,
45 OCPP16ChargingProfilePurposeType
,
46 type OCPP16ChargingSchedule
,
47 type OCPP16ClearCacheRequest
,
48 type OCPP16DataTransferRequest
,
49 type OCPP16DataTransferResponse
,
50 OCPP16DataTransferVendorId
,
51 OCPP16DiagnosticsStatus
,
52 type OCPP16DiagnosticsStatusNotificationRequest
,
53 type OCPP16DiagnosticsStatusNotificationResponse
,
55 type OCPP16FirmwareStatusNotificationRequest
,
56 type OCPP16FirmwareStatusNotificationResponse
,
57 type OCPP16GetCompositeScheduleRequest
,
58 type OCPP16GetCompositeScheduleResponse
,
59 type OCPP16HeartbeatRequest
,
60 type OCPP16HeartbeatResponse
,
61 OCPP16IncomingRequestCommand
,
64 type OCPP16ReserveNowRequest
,
65 type OCPP16ReserveNowResponse
,
66 OCPP16StandardParametersKey
,
67 type OCPP16StartTransactionRequest
,
68 type OCPP16StartTransactionResponse
,
69 type OCPP16StatusNotificationRequest
,
70 type OCPP16StatusNotificationResponse
,
71 OCPP16StopTransactionReason
,
72 OCPP16SupportedFeatureProfiles
,
73 type OCPP16TriggerMessageRequest
,
74 type OCPP16TriggerMessageResponse
,
75 type OCPP16UpdateFirmwareRequest
,
76 type OCPP16UpdateFirmwareResponse
,
77 type OCPPConfigurationKey
,
79 type RemoteStartTransactionRequest
,
80 type RemoteStopTransactionRequest
,
82 ReservationTerminationReason
,
84 type SetChargingProfileRequest
,
85 type SetChargingProfileResponse
,
86 type StartTransactionRequest
,
87 type UnlockConnectorRequest
,
88 type UnlockConnectorResponse
,
89 } from
'../../../types';
94 formatDurationMilliSeconds
,
103 } from
'../../../utils';
104 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
106 const moduleName
= 'OCPP16IncomingRequestService';
108 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
109 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
110 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
112 public constructor() {
113 // if (new.target?.name === moduleName) {
114 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
116 super(OCPPVersion
.VERSION_16
);
117 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
118 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
119 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
120 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
122 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
123 this.handleRequestGetConfiguration
.bind(this),
126 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
127 this.handleRequestChangeConfiguration
.bind(this),
130 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
131 this.handleRequestGetCompositeSchedule
.bind(this),
134 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
135 this.handleRequestSetChargingProfile
.bind(this),
138 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
139 this.handleRequestClearChargingProfile
.bind(this),
142 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
143 this.handleRequestChangeAvailability
.bind(this),
146 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
147 this.handleRequestRemoteStartTransaction
.bind(this),
150 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
151 this.handleRequestRemoteStopTransaction
.bind(this),
153 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
154 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
155 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
156 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
157 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
159 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
160 this.handleRequestCancelReservation
.bind(this),
163 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
165 OCPP16IncomingRequestCommand
.RESET
,
166 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
167 'assets/json-schemas/ocpp/1.6/Reset.json',
173 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
174 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
175 'assets/json-schemas/ocpp/1.6/ClearCache.json',
181 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
182 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
183 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
189 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
190 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
191 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
197 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
198 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
199 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
205 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
207 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
213 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
214 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
215 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
221 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
222 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
223 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
229 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
231 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
237 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
238 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
239 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
245 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
246 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
247 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
253 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
255 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
261 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
262 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
263 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
269 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
270 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
271 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
277 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
279 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
285 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
286 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
287 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
293 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
294 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
295 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
301 this.validatePayload
= this.validatePayload
.bind(this) as (
302 chargingStation
: ChargingStation
,
303 commandName
: OCPP16IncomingRequestCommand
,
304 commandPayload
: JsonType
,
308 public async incomingRequestHandler(
309 chargingStation
: ChargingStation
,
311 commandName
: OCPP16IncomingRequestCommand
,
312 commandPayload
: JsonType
,
314 let response
: JsonType
;
316 chargingStation
.getOcppStrictCompliance() === true &&
317 chargingStation
.inPendingState() === true &&
318 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
319 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
322 ErrorType
.SECURITY_ERROR
,
323 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
327 )} while the charging station is in pending state on the central server`,
333 chargingStation
.isRegistered() === true ||
334 (chargingStation
.getOcppStrictCompliance() === false &&
335 chargingStation
.inUnknownState() === true)
338 this.incomingRequestHandlers
.has(commandName
) === true &&
339 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
342 this.validatePayload(chargingStation
, commandName
, commandPayload
);
343 // Call the method to build the response
344 response
= await this.incomingRequestHandlers
.get(commandName
)(
351 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
352 Handle incoming request error:`,
360 ErrorType
.NOT_IMPLEMENTED
,
361 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
372 ErrorType
.SECURITY_ERROR
,
373 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
377 )} while the charging station is not registered on the central server.`,
382 // Send the built response
383 await chargingStation
.ocppRequestService
.sendResponse(
391 private validatePayload(
392 chargingStation
: ChargingStation
,
393 commandName
: OCPP16IncomingRequestCommand
,
394 commandPayload
: JsonType
,
396 if (this.jsonSchemas
.has(commandName
) === true) {
397 return this.validateIncomingRequestPayload(
400 this.jsonSchemas
.get(commandName
),
405 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
406 for command '${commandName}' PDU validation`,
411 // Simulate charging station restart
412 private handleRequestReset(
413 chargingStation
: ChargingStation
,
414 commandPayload
: ResetRequest
,
416 this.runInAsyncScope(
417 chargingStation
.reset
.bind(chargingStation
) as (
418 this: ChargingStation
,
422 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
,
423 ).catch(Constants
.EMPTY_FUNCTION
);
425 `${chargingStation.logPrefix()} ${
427 } reset command received, simulating it. The station will be
428 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime)}`,
430 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
433 private async handleRequestUnlockConnector(
434 chargingStation
: ChargingStation
,
435 commandPayload
: UnlockConnectorRequest
,
436 ): Promise
<UnlockConnectorResponse
> {
437 const connectorId
= commandPayload
.connectorId
;
438 if (chargingStation
.hasConnector(connectorId
) === false) {
440 `${chargingStation.logPrefix()} Trying to unlock a non existing
441 connector id ${connectorId.toString()}`,
443 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
445 if (connectorId
=== 0) {
447 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
449 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
451 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
452 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
454 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
456 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
457 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
459 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
461 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
464 OCPP16ChargePointStatus
.Available
,
466 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
469 private handleRequestGetConfiguration(
470 chargingStation
: ChargingStation
,
471 commandPayload
: GetConfigurationRequest
,
472 ): GetConfigurationResponse
{
473 const configurationKey
: OCPPConfigurationKey
[] = [];
474 const unknownKey
: string[] = [];
475 if (isUndefined(commandPayload
.key
) === true) {
476 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
477 if (isUndefined(configuration
.visible
) === true) {
478 configuration
.visible
= true;
480 if (configuration
.visible
=== false) {
483 configurationKey
.push({
484 key
: configuration
.key
,
485 readonly: configuration
.readonly,
486 value
: configuration
.value
,
489 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
490 for (const key
of commandPayload
.key
) {
491 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
497 if (isUndefined(keyFound
.visible
) === true) {
498 keyFound
.visible
= true;
500 if (keyFound
.visible
=== false) {
503 configurationKey
.push({
505 readonly: keyFound
.readonly,
506 value
: keyFound
.value
,
509 unknownKey
.push(key
);
519 private handleRequestChangeConfiguration(
520 chargingStation
: ChargingStation
,
521 commandPayload
: ChangeConfigurationRequest
,
522 ): ChangeConfigurationResponse
{
523 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
529 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
530 } else if (keyToChange
?.readonly === true) {
531 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
532 } else if (keyToChange
?.readonly === false) {
533 let valueChanged
= false;
534 if (keyToChange
.value
!== commandPayload
.value
) {
535 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
538 commandPayload
.value
,
543 let triggerHeartbeatRestart
= false;
544 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
545 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
547 OCPP16StandardParametersKey
.HeartbeatInterval
,
548 commandPayload
.value
,
550 triggerHeartbeatRestart
= true;
552 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
553 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
555 OCPP16StandardParametersKey
.HeartBeatInterval
,
556 commandPayload
.value
,
558 triggerHeartbeatRestart
= true;
560 if (triggerHeartbeatRestart
) {
561 chargingStation
.restartHeartbeat();
563 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
564 chargingStation
.restartWebSocketPing();
566 if (keyToChange
.reboot
) {
567 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
569 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
573 private handleRequestSetChargingProfile(
574 chargingStation
: ChargingStation
,
575 commandPayload
: SetChargingProfileRequest
,
576 ): SetChargingProfileResponse
{
578 OCPP16ServiceUtils
.checkFeatureProfile(
580 OCPP16SupportedFeatureProfiles
.SmartCharging
,
581 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
584 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
586 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
588 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
589 non existing connector id ${commandPayload.connectorId}`,
591 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
594 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
595 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
596 commandPayload
.connectorId
!== 0
598 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
601 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
602 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
603 (commandPayload
.connectorId
=== 0 ||
604 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
608 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
609 on connector ${commandPayload.connectorId} without a started transaction`,
611 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
613 OCPP16ServiceUtils
.setChargingProfile(
615 commandPayload
.connectorId
,
616 commandPayload
.csChargingProfiles
,
619 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
620 commandPayload.connectorId
622 commandPayload
.csChargingProfiles
,
624 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
627 private handleRequestGetCompositeSchedule(
628 chargingStation
: ChargingStation
,
629 commandPayload
: OCPP16GetCompositeScheduleRequest
,
630 ): OCPP16GetCompositeScheduleResponse
{
632 OCPP16ServiceUtils
.checkFeatureProfile(
634 OCPP16SupportedFeatureProfiles
.SmartCharging
,
635 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
638 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
640 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
642 `${chargingStation.logPrefix()} Trying to get composite schedule to a
643 non existing connector id ${commandPayload.connectorId}`,
645 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
648 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
650 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
652 const startDate
= new Date();
653 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
654 let compositeSchedule
: OCPP16ChargingSchedule
;
655 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
657 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
659 chargingProfile
.chargingSchedule
?.startSchedule
>= startDate
&&
660 chargingProfile
.chargingSchedule
?.startSchedule
<= endDate
662 compositeSchedule
= chargingProfile
.chargingSchedule
;
667 status: GenericStatus
.Accepted
,
668 scheduleStart
: compositeSchedule
?.startSchedule
,
669 connectorId
: commandPayload
.connectorId
,
670 chargingSchedule
: compositeSchedule
,
674 private handleRequestClearChargingProfile(
675 chargingStation
: ChargingStation
,
676 commandPayload
: ClearChargingProfileRequest
,
677 ): ClearChargingProfileResponse
{
679 OCPP16ServiceUtils
.checkFeatureProfile(
681 OCPP16SupportedFeatureProfiles
.SmartCharging
,
682 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
685 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
687 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
689 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
690 a non existing connector id ${commandPayload.connectorId}`,
692 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
695 !isNullOrUndefined(commandPayload
.connectorId
) &&
697 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
,
700 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
702 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
703 commandPayload.connectorId
706 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
708 if (isNullOrUndefined(commandPayload
.connectorId
)) {
709 let clearedCP
= false;
710 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
711 if (isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
712 connectorStatus
?.chargingProfiles
?.forEach(
713 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
714 let clearCurrentCP
= false;
715 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
716 clearCurrentCP
= true;
719 !commandPayload
.chargingProfilePurpose
&&
720 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
722 clearCurrentCP
= true;
725 !chargingProfile
.stackLevel
&&
726 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
728 clearCurrentCP
= true;
731 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
732 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
734 clearCurrentCP
= true;
736 if (clearCurrentCP
) {
737 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
739 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
748 if (chargingStation
.hasEvses
) {
749 for (const evseStatus
of chargingStation
.evses
.values()) {
750 for (const connectorStatus
of evseStatus
.connectors
.values()) {
751 clearChargingProfiles(connectorStatus
);
755 for (const connectorId
of chargingStation
.connectors
.keys()) {
756 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
));
760 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
763 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
766 private async handleRequestChangeAvailability(
767 chargingStation
: ChargingStation
,
768 commandPayload
: ChangeAvailabilityRequest
,
769 ): Promise
<ChangeAvailabilityResponse
> {
770 const connectorId
: number = commandPayload
.connectorId
;
771 if (chargingStation
.hasConnector(connectorId
) === false) {
773 `${chargingStation.logPrefix()} Trying to change the availability of a
774 non existing connector id ${connectorId.toString()}`,
776 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
778 const chargePointStatus
: OCPP16ChargePointStatus
=
779 commandPayload
.type === OCPP16AvailabilityType
.Operative
780 ? OCPP16ChargePointStatus
.Available
781 : OCPP16ChargePointStatus
.Unavailable
;
782 if (connectorId
=== 0) {
783 let response
: ChangeAvailabilityResponse
=
784 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
785 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
786 if (connectorStatus
?.transactionStarted
=== true) {
787 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
789 connectorStatus
.availability
= commandPayload
.type;
790 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
791 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
798 if (chargingStation
.hasEvses
) {
799 for (const evseStatus
of chargingStation
.evses
.values()) {
800 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
801 await changeAvailability(id
, connectorStatus
);
805 for (const id
of chargingStation
.connectors
.keys()) {
806 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
));
812 (chargingStation
.isChargingStationAvailable() === true ||
813 (chargingStation
.isChargingStationAvailable() === false &&
814 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
816 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
817 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
818 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
820 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
821 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
826 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
828 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
831 private async handleRequestRemoteStartTransaction(
832 chargingStation
: ChargingStation
,
833 commandPayload
: RemoteStartTransactionRequest
,
834 ): Promise
<GenericResponse
> {
835 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
837 chargingStation
.getConnectorStatus(transactionConnectorId
).status ===
838 OCPP16ChargePointStatus
.Reserved
;
839 const reservedOnConnectorZero
=
840 chargingStation
.getConnectorStatus(0).status === OCPP16ChargePointStatus
.Reserved
;
843 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
844 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
846 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
848 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
849 return this.notifyRemoteStartTransactionRejected(
851 transactionConnectorId
,
856 !chargingStation
.isChargingStationAvailable() ||
857 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
859 return this.notifyRemoteStartTransactionRejected(
861 transactionConnectorId
,
865 const remoteStartTransactionLogMsg
= `
866 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
867 chargingStation.stationInfo.chargingStationId
868 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
869 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
871 transactionConnectorId
,
872 OCPP16ChargePointStatus
.Preparing
,
874 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
875 // Check if authorized
877 chargingStation
.getAuthorizeRemoteTxRequests() &&
878 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
880 // Authorization successful, start transaction
882 this.setRemoteStartTransactionChargingProfile(
884 transactionConnectorId
,
888 connectorStatus
.transactionRemoteStarted
= true;
889 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
890 connectorId
: transactionConnectorId
,
893 if (reserved
|| reservedOnConnectorZero
) {
894 const reservation
= chargingStation
.getReservationBy(
895 ReservationFilterKey
.CONNECTOR_ID
,
896 reservedOnConnectorZero
? 0 : transactionConnectorId
,
898 startTransactionPayload
.reservationId
= reservation
.id
;
899 await chargingStation
.removeReservation(
901 ReservationTerminationReason
.TRANSACTION_STARTED
,
906 await chargingStation
.ocppRequestService
.requestHandler
<
907 OCPP16StartTransactionRequest
,
908 OCPP16StartTransactionResponse
909 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
910 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
912 logger
.debug(remoteStartTransactionLogMsg
);
913 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
915 return this.notifyRemoteStartTransactionRejected(
917 transactionConnectorId
,
921 return this.notifyRemoteStartTransactionRejected(
923 transactionConnectorId
,
927 // No authorization check required, start transaction
929 this.setRemoteStartTransactionChargingProfile(
931 transactionConnectorId
,
935 connectorStatus
.transactionRemoteStarted
= true;
938 await chargingStation
.ocppRequestService
.requestHandler
<
939 OCPP16StartTransactionRequest
,
940 OCPP16StartTransactionResponse
941 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
942 connectorId
: transactionConnectorId
,
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
,
963 private async notifyRemoteStartTransactionRejected(
964 chargingStation
: ChargingStation
,
967 ): Promise
<GenericResponse
> {
969 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
971 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
974 OCPP16ChargePointStatus
.Available
,
978 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
979 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
981 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
983 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
986 private setRemoteStartTransactionChargingProfile(
987 chargingStation
: ChargingStation
,
989 cp
: OCPP16ChargingProfile
,
991 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
992 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
994 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
995 on connector id ${connectorId}: %j`,
999 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1001 `${chargingStation.logPrefix()} Not allowed to set ${
1002 cp.chargingProfilePurpose
1003 } charging profile(s) at remote start transaction`,
1011 private async handleRequestRemoteStopTransaction(
1012 chargingStation
: ChargingStation
,
1013 commandPayload
: RemoteStopTransactionRequest
,
1014 ): Promise
<GenericResponse
> {
1015 const transactionId
= commandPayload
.transactionId
;
1016 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1017 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1020 OCPP16ChargePointStatus
.Finishing
,
1022 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1024 OCPP16StopTransactionReason
.REMOTE
,
1026 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1027 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1029 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1031 if (chargingStation
.hasEvses
) {
1032 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1034 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1035 if (connectorStatus
.transactionId
=== transactionId
) {
1036 return remoteStopTransaction(connectorId
);
1042 for (const connectorId
of chargingStation
.connectors
.keys()) {
1045 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1047 return remoteStopTransaction(connectorId
);
1052 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1053 ${transactionId.toString()}`,
1055 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1058 private handleRequestUpdateFirmware(
1059 chargingStation
: ChargingStation
,
1060 commandPayload
: OCPP16UpdateFirmwareRequest
,
1061 ): OCPP16UpdateFirmwareResponse
{
1063 OCPP16ServiceUtils
.checkFeatureProfile(
1065 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1066 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1070 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1071 Cannot simulate firmware update: feature profile not supported`,
1073 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1076 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1077 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1080 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1081 Cannot simulate firmware update: firmware update is already in progress`,
1083 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1085 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
);
1086 const now
= Date.now();
1087 if (retrieveDate
?.getTime() <= now
) {
1088 this.runInAsyncScope(
1089 this.updateFirmwareSimulation
.bind(this) as (
1090 this: OCPP16IncomingRequestService
,
1095 ).catch(Constants
.EMPTY_FUNCTION
);
1099 this.runInAsyncScope(
1100 this.updateFirmwareSimulation
.bind(this) as (
1101 this: OCPP16IncomingRequestService
,
1106 ).catch(Constants
.EMPTY_FUNCTION
);
1108 retrieveDate
?.getTime() - now
,
1111 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1114 private async updateFirmwareSimulation(
1115 chargingStation
: ChargingStation
,
1119 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1122 if (chargingStation
.hasEvses
) {
1123 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1125 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1126 if (connectorStatus
?.transactionStarted
=== false) {
1127 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1130 OCPP16ChargePointStatus
.Unavailable
,
1137 for (const connectorId
of chargingStation
.connectors
.keys()) {
1140 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1142 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1145 OCPP16ChargePointStatus
.Unavailable
,
1150 await chargingStation
.ocppRequestService
.requestHandler
<
1151 OCPP16FirmwareStatusNotificationRequest
,
1152 OCPP16FirmwareStatusNotificationResponse
1153 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1154 status: OCPP16FirmwareStatus
.Downloading
,
1156 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1158 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1159 OCPP16FirmwareStatus
.DownloadFailed
1161 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1162 await chargingStation
.ocppRequestService
.requestHandler
<
1163 OCPP16FirmwareStatusNotificationRequest
,
1164 OCPP16FirmwareStatusNotificationResponse
1165 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1166 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1168 chargingStation
.stationInfo
.firmwareStatus
=
1169 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1172 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1173 await chargingStation
.ocppRequestService
.requestHandler
<
1174 OCPP16FirmwareStatusNotificationRequest
,
1175 OCPP16FirmwareStatusNotificationResponse
1176 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1177 status: OCPP16FirmwareStatus
.Downloaded
,
1179 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1180 let wasTransactionsStarted
= false;
1181 let transactionsStarted
: boolean;
1183 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1184 if (runningTransactions
> 0) {
1185 const waitTime
= 15 * 1000;
1187 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1188 ${runningTransactions} transaction(s) in progress, waiting ${
1190 } seconds before continuing firmware update simulation`,
1192 await sleep(waitTime
);
1193 transactionsStarted
= true;
1194 wasTransactionsStarted
= true;
1196 if (chargingStation
.hasEvses
) {
1197 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1199 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1200 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1201 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1204 OCPP16ChargePointStatus
.Unavailable
,
1211 for (const connectorId
of chargingStation
.connectors
.keys()) {
1214 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1215 OCPP16ChargePointStatus
.Unavailable
1217 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1220 OCPP16ChargePointStatus
.Unavailable
,
1225 transactionsStarted
= false;
1227 } while (transactionsStarted
);
1228 !wasTransactionsStarted
&& (await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000));
1229 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1232 await chargingStation
.ocppRequestService
.requestHandler
<
1233 OCPP16FirmwareStatusNotificationRequest
,
1234 OCPP16FirmwareStatusNotificationResponse
1235 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1236 status: OCPP16FirmwareStatus
.Installing
,
1238 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1240 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1241 OCPP16FirmwareStatus
.InstallationFailed
1243 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1244 await chargingStation
.ocppRequestService
.requestHandler
<
1245 OCPP16FirmwareStatusNotificationRequest
,
1246 OCPP16FirmwareStatusNotificationResponse
1247 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1248 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1250 chargingStation
.stationInfo
.firmwareStatus
=
1251 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1254 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1255 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1256 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1260 private async handleRequestGetDiagnostics(
1261 chargingStation
: ChargingStation
,
1262 commandPayload
: GetDiagnosticsRequest
,
1263 ): Promise
<GetDiagnosticsResponse
> {
1265 OCPP16ServiceUtils
.checkFeatureProfile(
1267 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1268 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1272 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1273 Cannot get diagnostics: feature profile not supported`,
1275 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1277 const uri
= new URL(commandPayload
.location
);
1278 if (uri
.protocol
.startsWith('ftp:')) {
1279 let ftpClient
: Client
;
1281 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1282 .filter((file
) => file
.endsWith('.log'))
1283 .map((file
) => join('./', file
));
1284 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1285 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1286 ftpClient
= new Client();
1287 const accessResponse
= await ftpClient
.access({
1289 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1290 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1291 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1293 let uploadResponse
: FTPResponse
;
1294 if (accessResponse
.code
=== 220) {
1295 ftpClient
.trackProgress((info
) => {
1297 `${chargingStation.logPrefix()} ${
1299 } bytes transferred from diagnostics archive ${info.name}`,
1301 chargingStation
.ocppRequestService
1303 OCPP16DiagnosticsStatusNotificationRequest
,
1304 OCPP16DiagnosticsStatusNotificationResponse
1305 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1306 status: OCPP16DiagnosticsStatus
.Uploading
,
1310 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1311 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1316 uploadResponse
= await ftpClient
.uploadFrom(
1317 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1318 `${uri.pathname}${diagnosticsArchive}`,
1320 if (uploadResponse
.code
=== 226) {
1321 await chargingStation
.ocppRequestService
.requestHandler
<
1322 OCPP16DiagnosticsStatusNotificationRequest
,
1323 OCPP16DiagnosticsStatusNotificationResponse
1324 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1325 status: OCPP16DiagnosticsStatus
.Uploaded
,
1330 return { fileName
: diagnosticsArchive
};
1332 throw new OCPPError(
1333 ErrorType
.GENERIC_ERROR
,
1334 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1335 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1337 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1340 throw new OCPPError(
1341 ErrorType
.GENERIC_ERROR
,
1342 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1343 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1345 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1348 await chargingStation
.ocppRequestService
.requestHandler
<
1349 OCPP16DiagnosticsStatusNotificationRequest
,
1350 OCPP16DiagnosticsStatusNotificationResponse
1351 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1352 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1357 return this.handleIncomingRequestError(
1359 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1361 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1366 `${chargingStation.logPrefix()} Unsupported protocol ${
1368 } to transfer the diagnostic logs archive`,
1370 await chargingStation
.ocppRequestService
.requestHandler
<
1371 OCPP16DiagnosticsStatusNotificationRequest
,
1372 OCPP16DiagnosticsStatusNotificationResponse
1373 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1374 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1376 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1380 private handleRequestTriggerMessage(
1381 chargingStation
: ChargingStation
,
1382 commandPayload
: OCPP16TriggerMessageRequest
,
1383 ): OCPP16TriggerMessageResponse
{
1385 !OCPP16ServiceUtils
.checkFeatureProfile(
1387 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1388 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1390 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1392 commandPayload
.requestedMessage
,
1395 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1398 !OCPP16ServiceUtils
.isConnectorIdValid(
1400 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1401 commandPayload
.connectorId
,
1404 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1407 switch (commandPayload
.requestedMessage
) {
1408 case OCPP16MessageTrigger
.BootNotification
:
1410 chargingStation
.ocppRequestService
1411 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1413 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1414 chargingStation
.bootNotificationRequest
,
1415 { skipBufferingOnError
: true, triggerMessage
: true },
1417 .then((response
) => {
1418 chargingStation
.bootNotificationResponse
= response
;
1420 .catch(Constants
.EMPTY_FUNCTION
);
1421 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1422 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1423 case OCPP16MessageTrigger
.Heartbeat
:
1425 chargingStation
.ocppRequestService
1426 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1428 OCPP16RequestCommand
.HEARTBEAT
,
1431 triggerMessage
: true,
1434 .catch(Constants
.EMPTY_FUNCTION
);
1435 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1436 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1437 case OCPP16MessageTrigger
.StatusNotification
:
1439 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1440 chargingStation
.ocppRequestService
1441 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1443 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1445 connectorId
: commandPayload
.connectorId
,
1446 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1447 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.status,
1450 triggerMessage
: true,
1453 .catch(Constants
.EMPTY_FUNCTION
);
1455 // eslint-disable-next-line no-lonely-if
1456 if (chargingStation
.hasEvses
) {
1457 for (const evseStatus
of chargingStation
.evses
.values()) {
1458 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1459 chargingStation
.ocppRequestService
1461 OCPP16StatusNotificationRequest
,
1462 OCPP16StatusNotificationResponse
1465 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1468 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1469 status: connectorStatus
.status,
1472 triggerMessage
: true,
1475 .catch(Constants
.EMPTY_FUNCTION
);
1479 for (const connectorId
of chargingStation
.connectors
.keys()) {
1480 chargingStation
.ocppRequestService
1482 OCPP16StatusNotificationRequest
,
1483 OCPP16StatusNotificationResponse
1486 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1489 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1490 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1493 triggerMessage
: true,
1496 .catch(Constants
.EMPTY_FUNCTION
);
1500 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1501 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1503 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1506 return this.handleIncomingRequestError(
1508 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1510 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1515 private handleRequestDataTransfer(
1516 chargingStation
: ChargingStation
,
1517 commandPayload
: OCPP16DataTransferRequest
,
1518 ): OCPP16DataTransferResponse
{
1520 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1521 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1523 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1525 return this.handleIncomingRequestError(
1527 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1529 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1534 private async handleRequestReserveNow(
1535 chargingStation
: ChargingStation
,
1536 commandPayload
: OCPP16ReserveNowRequest
,
1537 ): Promise
<OCPP16ReserveNowResponse
> {
1539 !OCPP16ServiceUtils
.checkFeatureProfile(
1541 OCPP16SupportedFeatureProfiles
.Reservation
,
1542 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1545 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1547 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1548 let response
: OCPP16ReserveNowResponse
;
1550 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1551 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1553 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1554 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1556 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1557 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1559 switch (chargingStation
.getConnectorStatus(connectorId
).status) {
1560 case OCPP16ChargePointStatus
.Faulted
:
1561 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1563 case OCPP16ChargePointStatus
.Preparing
:
1564 case OCPP16ChargePointStatus
.Charging
:
1565 case OCPP16ChargePointStatus
.SuspendedEV
:
1566 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1567 case OCPP16ChargePointStatus
.Finishing
:
1568 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1570 case OCPP16ChargePointStatus
.Unavailable
:
1571 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1573 case OCPP16ChargePointStatus
.Reserved
:
1574 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1575 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1578 // eslint-disable-next-line no-fallthrough
1580 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1581 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1584 await chargingStation
.addReservation({
1585 id
: commandPayload
.reservationId
,
1588 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1593 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.Available
;
1594 return this.handleIncomingRequestError(
1596 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1598 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1603 private async handleRequestCancelReservation(
1604 chargingStation
: ChargingStation
,
1605 commandPayload
: OCPP16CancelReservationRequest
,
1606 ): Promise
<GenericResponse
> {
1608 !OCPP16ServiceUtils
.checkFeatureProfile(
1610 OCPP16SupportedFeatureProfiles
.Reservation
,
1611 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1614 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1617 const { reservationId
} = commandPayload
;
1618 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1621 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1622 does not exist on charging station`,
1624 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1626 await chargingStation
.removeReservation(
1628 ReservationTerminationReason
.RESERVATION_CANCELED
,
1630 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1632 return this.handleIncomingRequestError(
1634 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1636 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},