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 { secondsToMilliseconds
} from
'date-fns';
10 import { create
} from
'tar';
12 import { OCPP16Constants
} from
'./OCPP16Constants';
13 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
18 setConfigurationKeyValue
,
19 } from
'../../../charging-station';
20 import { OCPPError
} from
'../../../exception';
22 type ChangeConfigurationRequest
,
23 type ChangeConfigurationResponse
,
24 type ClearChargingProfileRequest
,
25 type ClearChargingProfileResponse
,
29 type GetConfigurationRequest
,
30 type GetConfigurationResponse
,
31 type GetDiagnosticsRequest
,
32 type GetDiagnosticsResponse
,
33 type IncomingRequestHandler
,
36 OCPP16AuthorizationStatus
,
37 OCPP16AvailabilityType
,
38 type OCPP16BootNotificationRequest
,
39 type OCPP16BootNotificationResponse
,
40 type OCPP16CancelReservationRequest
,
41 type OCPP16ChangeAvailabilityRequest
,
42 type OCPP16ChangeAvailabilityResponse
,
43 OCPP16ChargePointErrorCode
,
44 OCPP16ChargePointStatus
,
45 type OCPP16ChargingProfile
,
46 OCPP16ChargingProfilePurposeType
,
47 type OCPP16ChargingSchedule
,
48 type OCPP16ClearCacheRequest
,
49 type OCPP16DataTransferRequest
,
50 type OCPP16DataTransferResponse
,
51 OCPP16DataTransferVendorId
,
52 OCPP16DiagnosticsStatus
,
53 type OCPP16DiagnosticsStatusNotificationRequest
,
54 type OCPP16DiagnosticsStatusNotificationResponse
,
56 type OCPP16FirmwareStatusNotificationRequest
,
57 type OCPP16FirmwareStatusNotificationResponse
,
58 type OCPP16GetCompositeScheduleRequest
,
59 type OCPP16GetCompositeScheduleResponse
,
60 type OCPP16HeartbeatRequest
,
61 type OCPP16HeartbeatResponse
,
62 OCPP16IncomingRequestCommand
,
65 type OCPP16ReserveNowRequest
,
66 type OCPP16ReserveNowResponse
,
67 OCPP16StandardParametersKey
,
68 type OCPP16StartTransactionRequest
,
69 type OCPP16StartTransactionResponse
,
70 type OCPP16StatusNotificationRequest
,
71 type OCPP16StatusNotificationResponse
,
72 OCPP16StopTransactionReason
,
73 OCPP16SupportedFeatureProfiles
,
74 type OCPP16TriggerMessageRequest
,
75 type OCPP16TriggerMessageResponse
,
76 type OCPP16UpdateFirmwareRequest
,
77 type OCPP16UpdateFirmwareResponse
,
78 type OCPPConfigurationKey
,
80 type RemoteStartTransactionRequest
,
81 type RemoteStopTransactionRequest
,
82 ReservationTerminationReason
,
84 type SetChargingProfileRequest
,
85 type SetChargingProfileResponse
,
86 type UnlockConnectorRequest
,
87 type UnlockConnectorResponse
,
88 } from
'../../../types';
93 formatDurationMilliSeconds
,
102 } from
'../../../utils';
103 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
105 const moduleName
= 'OCPP16IncomingRequestService';
107 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
108 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
109 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
111 public constructor() {
112 // if (new.target?.name === moduleName) {
113 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
115 super(OCPPVersion
.VERSION_16
);
116 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
118 OCPP16IncomingRequestCommand
.RESET
,
119 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
,
122 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
123 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
,
126 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
127 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
,
130 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
131 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
,
134 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
135 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
,
138 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
139 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
,
142 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
143 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
,
146 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
147 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
,
150 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
151 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
,
154 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
155 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
158 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
159 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
162 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
163 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
,
166 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
167 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
,
170 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
171 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
,
174 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
175 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
,
178 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
179 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
,
182 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
183 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
,
186 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
188 OCPP16IncomingRequestCommand
.RESET
,
189 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
190 'assets/json-schemas/ocpp/1.6/Reset.json',
196 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
197 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
198 'assets/json-schemas/ocpp/1.6/ClearCache.json',
204 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
205 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
206 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
212 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
213 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
214 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
220 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
221 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
222 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
228 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
229 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
230 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
236 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
237 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
238 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
244 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
245 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
246 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
252 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
253 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
254 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
260 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
261 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ChangeAvailabilityRequest
>(
262 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
268 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
269 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
270 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
276 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
277 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
278 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
284 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
285 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
286 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
292 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
293 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
294 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
300 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
301 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
302 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
308 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
309 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
310 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
316 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
317 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
318 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
324 this.validatePayload
= this.validatePayload
.bind(this) as (
325 chargingStation
: ChargingStation
,
326 commandName
: OCPP16IncomingRequestCommand
,
327 commandPayload
: JsonType
,
331 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
332 chargingStation
: ChargingStation
,
334 commandName
: OCPP16IncomingRequestCommand
,
335 commandPayload
: ReqType
,
337 let response
: ResType
;
339 chargingStation
.getOcppStrictCompliance() === true &&
340 chargingStation
.inPendingState() === true &&
341 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
342 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
345 ErrorType
.SECURITY_ERROR
,
346 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
350 )} while the charging station is in pending state on the central server`,
356 chargingStation
.isRegistered() === true ||
357 (chargingStation
.getOcppStrictCompliance() === false &&
358 chargingStation
.inUnknownState() === true)
361 this.incomingRequestHandlers
.has(commandName
) === true &&
362 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
365 this.validatePayload(chargingStation
, commandName
, commandPayload
);
366 // Call the method to build the response
367 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
374 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
375 Handle incoming request error:`,
383 ErrorType
.NOT_IMPLEMENTED
,
384 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
395 ErrorType
.SECURITY_ERROR
,
396 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
400 )} while the charging station is not registered on the central server.`,
405 // Send the built response
406 await chargingStation
.ocppRequestService
.sendResponse(
414 private validatePayload(
415 chargingStation
: ChargingStation
,
416 commandName
: OCPP16IncomingRequestCommand
,
417 commandPayload
: JsonType
,
419 if (this.jsonSchemas
.has(commandName
) === true) {
420 return this.validateIncomingRequestPayload(
423 this.jsonSchemas
.get(commandName
)!,
428 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
429 for command '${commandName}' PDU validation`,
434 // Simulate charging station restart
435 private handleRequestReset(
436 chargingStation
: ChargingStation
,
437 commandPayload
: ResetRequest
,
439 this.runInAsyncScope(
440 chargingStation
.reset
.bind(chargingStation
) as (
441 this: ChargingStation
,
445 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
,
446 ).catch(Constants
.EMPTY_FUNCTION
);
448 `${chargingStation.logPrefix()} ${
450 } reset command received, simulating it. The station will be
451 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`,
453 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
456 private async handleRequestUnlockConnector(
457 chargingStation
: ChargingStation
,
458 commandPayload
: UnlockConnectorRequest
,
459 ): Promise
<UnlockConnectorResponse
> {
460 const connectorId
= commandPayload
.connectorId
;
461 if (chargingStation
.hasConnector(connectorId
) === false) {
463 `${chargingStation.logPrefix()} Trying to unlock a non existing
464 connector id ${connectorId.toString()}`,
466 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
468 if (connectorId
=== 0) {
470 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
472 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
474 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
475 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
477 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
479 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
480 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
482 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
484 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
487 OCPP16ChargePointStatus
.Available
,
489 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
492 private handleRequestGetConfiguration(
493 chargingStation
: ChargingStation
,
494 commandPayload
: GetConfigurationRequest
,
495 ): GetConfigurationResponse
{
496 const configurationKey
: OCPPConfigurationKey
[] = [];
497 const unknownKey
: string[] = [];
498 if (isUndefined(commandPayload
.key
) === true) {
499 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
500 if (isUndefined(configuration
.visible
) === true) {
501 configuration
.visible
= true;
503 if (configuration
.visible
=== false) {
506 configurationKey
.push({
507 key
: configuration
.key
,
508 readonly: configuration
.readonly,
509 value
: configuration
.value
,
512 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
513 for (const key
of commandPayload
.key
!) {
514 const keyFound
= getConfigurationKey(chargingStation
, key
, true);
516 if (isUndefined(keyFound
.visible
) === true) {
517 keyFound
.visible
= true;
519 if (keyFound
.visible
=== false) {
522 configurationKey
.push({
524 readonly: keyFound
.readonly,
525 value
: keyFound
.value
,
528 unknownKey
.push(key
);
538 private handleRequestChangeConfiguration(
539 chargingStation
: ChargingStation
,
540 commandPayload
: ChangeConfigurationRequest
,
541 ): ChangeConfigurationResponse
{
542 const keyToChange
= getConfigurationKey(chargingStation
, commandPayload
.key
, true);
543 if (keyToChange
?.readonly === true) {
544 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
545 } else if (keyToChange
?.readonly === false) {
546 let valueChanged
= false;
547 if (keyToChange
.value
!== commandPayload
.value
) {
548 setConfigurationKeyValue(chargingStation
, commandPayload
.key
, commandPayload
.value
, true);
551 let triggerHeartbeatRestart
= false;
553 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
554 OCPP16StandardParametersKey
.HeartBeatInterval
&&
557 setConfigurationKeyValue(
559 OCPP16StandardParametersKey
.HeartbeatInterval
,
560 commandPayload
.value
,
562 triggerHeartbeatRestart
= true;
565 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
566 OCPP16StandardParametersKey
.HeartbeatInterval
&&
569 setConfigurationKeyValue(
571 OCPP16StandardParametersKey
.HeartBeatInterval
,
572 commandPayload
.value
,
574 triggerHeartbeatRestart
= true;
576 if (triggerHeartbeatRestart
) {
577 chargingStation
.restartHeartbeat();
580 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
581 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
584 chargingStation
.restartWebSocketPing();
586 if (keyToChange
.reboot
) {
587 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
589 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
591 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
594 private handleRequestSetChargingProfile(
595 chargingStation
: ChargingStation
,
596 commandPayload
: SetChargingProfileRequest
,
597 ): SetChargingProfileResponse
{
599 OCPP16ServiceUtils
.checkFeatureProfile(
601 OCPP16SupportedFeatureProfiles
.SmartCharging
,
602 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
605 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
607 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
609 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
610 non existing connector id ${commandPayload.connectorId}`,
612 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
615 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
616 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
617 commandPayload
.connectorId
!== 0
619 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
622 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
623 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
624 (commandPayload
.connectorId
=== 0 ||
625 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
629 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
630 on connector ${commandPayload.connectorId} without a started transaction`,
632 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
634 OCPP16ServiceUtils
.setChargingProfile(
636 commandPayload
.connectorId
,
637 commandPayload
.csChargingProfiles
,
640 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
641 commandPayload.connectorId
643 commandPayload
.csChargingProfiles
,
645 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
648 private handleRequestGetCompositeSchedule(
649 chargingStation
: ChargingStation
,
650 commandPayload
: OCPP16GetCompositeScheduleRequest
,
651 ): OCPP16GetCompositeScheduleResponse
{
653 OCPP16ServiceUtils
.checkFeatureProfile(
655 OCPP16SupportedFeatureProfiles
.SmartCharging
,
656 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
659 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
661 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
663 `${chargingStation.logPrefix()} Trying to get composite schedule to a
664 non existing connector id ${commandPayload.connectorId}`,
666 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
669 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
671 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
673 const startDate
= new Date();
674 const endDate
= new Date(startDate
.getTime() + secondsToMilliseconds(commandPayload
.duration
));
675 let compositeSchedule
: OCPP16ChargingSchedule
| undefined;
676 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)!
677 .chargingProfiles
!) {
678 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
680 chargingProfile
.chargingSchedule
.startSchedule
! >= startDate
&&
681 chargingProfile
.chargingSchedule
.startSchedule
! <= endDate
683 compositeSchedule
= chargingProfile
.chargingSchedule
;
688 status: GenericStatus
.Accepted
,
689 scheduleStart
: compositeSchedule
?.startSchedule
,
690 connectorId
: commandPayload
.connectorId
,
691 chargingSchedule
: compositeSchedule
,
695 private handleRequestClearChargingProfile(
696 chargingStation
: ChargingStation
,
697 commandPayload
: ClearChargingProfileRequest
,
698 ): ClearChargingProfileResponse
{
700 OCPP16ServiceUtils
.checkFeatureProfile(
702 OCPP16SupportedFeatureProfiles
.SmartCharging
,
703 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
706 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
708 if (chargingStation
.hasConnector(commandPayload
.connectorId
!) === false) {
710 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
711 a non existing connector id ${commandPayload.connectorId}`,
713 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
716 !isNullOrUndefined(commandPayload
.connectorId
) &&
718 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.chargingProfiles
,
721 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)!.chargingProfiles
= [];
723 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
724 commandPayload.connectorId
727 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
729 if (isNullOrUndefined(commandPayload
.connectorId
)) {
730 let clearedCP
= false;
731 if (chargingStation
.hasEvses
) {
732 for (const evseStatus
of chargingStation
.evses
.values()) {
733 for (const connectorStatus
of evseStatus
.connectors
.values()) {
734 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
737 connectorStatus
.chargingProfiles
,
742 for (const connectorId
of chargingStation
.connectors
.keys()) {
743 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
746 chargingStation
.getConnectorStatus(connectorId
)?.chargingProfiles
,
751 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
754 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
757 private async handleRequestChangeAvailability(
758 chargingStation
: ChargingStation
,
759 commandPayload
: OCPP16ChangeAvailabilityRequest
,
760 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
761 const connectorId
: number = commandPayload
.connectorId
;
762 if (chargingStation
.hasConnector(connectorId
) === false) {
764 `${chargingStation.logPrefix()} Trying to change the availability of a
765 non existing connector id ${connectorId.toString()}`,
767 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
769 const chargePointStatus
: OCPP16ChargePointStatus
=
770 commandPayload
.type === OCPP16AvailabilityType
.Operative
771 ? OCPP16ChargePointStatus
.Available
772 : OCPP16ChargePointStatus
.Unavailable
;
773 if (connectorId
=== 0) {
774 let response
: OCPP16ChangeAvailabilityResponse
;
775 if (chargingStation
.hasEvses
) {
776 for (const evseStatus
of chargingStation
.evses
.values()) {
777 response
= await OCPP16ServiceUtils
.changeAvailability(
779 [...evseStatus
.connectors
.keys()],
785 response
= await OCPP16ServiceUtils
.changeAvailability(
787 [...chargingStation
.connectors
.keys()],
795 (chargingStation
.isChargingStationAvailable() === true ||
796 (chargingStation
.isChargingStationAvailable() === false &&
797 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
799 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
800 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
801 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
803 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
804 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
809 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
811 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
814 private async handleRequestRemoteStartTransaction(
815 chargingStation
: ChargingStation
,
816 commandPayload
: RemoteStartTransactionRequest
,
817 ): Promise
<GenericResponse
> {
818 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
819 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
820 return this.notifyRemoteStartTransactionRejected(
822 transactionConnectorId
,
827 !chargingStation
.isChargingStationAvailable() ||
828 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
830 return this.notifyRemoteStartTransactionRejected(
832 transactionConnectorId
,
837 (chargingStation
.getConnectorStatus(transactionConnectorId
)?.status ===
838 OCPP16ChargePointStatus
.Reserved
&&
839 chargingStation
.getReservationBy('connectorId', transactionConnectorId
)?.idTag
!== idTag
) ||
840 (chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
&&
841 chargingStation
.getReservationBy('connectorId', 0)?.idTag
!== idTag
)
843 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
845 const remoteStartTransactionLogMsg
= `
846 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
847 chargingStation.stationInfo.chargingStationId
848 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
849 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
851 transactionConnectorId
,
852 OCPP16ChargePointStatus
.Preparing
,
854 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
855 // Authorization check required
857 chargingStation
.getAuthorizeRemoteTxRequests() === true &&
858 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
860 // Authorization successful, start transaction
862 this.setRemoteStartTransactionChargingProfile(
864 transactionConnectorId
,
868 connectorStatus
.transactionRemoteStarted
= true;
871 await chargingStation
.ocppRequestService
.requestHandler
<
872 OCPP16StartTransactionRequest
,
873 OCPP16StartTransactionResponse
874 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
875 connectorId
: transactionConnectorId
,
877 reservationId
: chargingStation
.getReservationBy(
879 chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
881 : transactionConnectorId
,
884 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
886 logger
.debug(remoteStartTransactionLogMsg
);
887 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
889 return this.notifyRemoteStartTransactionRejected(
891 transactionConnectorId
,
895 return this.notifyRemoteStartTransactionRejected(
897 transactionConnectorId
,
901 // No authorization check required, start transaction
903 this.setRemoteStartTransactionChargingProfile(
905 transactionConnectorId
,
909 connectorStatus
.transactionRemoteStarted
= true;
912 await chargingStation
.ocppRequestService
.requestHandler
<
913 OCPP16StartTransactionRequest
,
914 OCPP16StartTransactionResponse
915 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
916 connectorId
: transactionConnectorId
,
918 reservationId
: chargingStation
.getReservationBy(
920 chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
922 : transactionConnectorId
,
925 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
927 logger
.debug(remoteStartTransactionLogMsg
);
928 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
930 return this.notifyRemoteStartTransactionRejected(
932 transactionConnectorId
,
936 return this.notifyRemoteStartTransactionRejected(
938 transactionConnectorId
,
943 private async notifyRemoteStartTransactionRejected(
944 chargingStation
: ChargingStation
,
947 ): Promise
<GenericResponse
> {
949 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
951 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
954 OCPP16ChargePointStatus
.Available
,
958 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
959 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
961 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
963 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
966 private setRemoteStartTransactionChargingProfile(
967 chargingStation
: ChargingStation
,
969 chargingProfile
: OCPP16ChargingProfile
,
973 chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
975 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
);
977 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
978 on connector id ${connectorId}: %j`,
984 chargingProfile
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
987 `${chargingStation.logPrefix()} Not allowed to set ${
988 chargingProfile.chargingProfilePurpose
989 } charging profile(s) at remote start transaction`,
996 private async handleRequestRemoteStopTransaction(
997 chargingStation
: ChargingStation
,
998 commandPayload
: RemoteStopTransactionRequest
,
999 ): Promise
<GenericResponse
> {
1000 const transactionId
= commandPayload
.transactionId
;
1001 if (chargingStation
.hasEvses
) {
1002 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1004 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1005 if (connectorStatus
.transactionId
=== transactionId
) {
1006 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1012 for (const connectorId
of chargingStation
.connectors
.keys()) {
1015 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1017 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1022 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1023 ${transactionId.toString()}`,
1025 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1028 private handleRequestUpdateFirmware(
1029 chargingStation
: ChargingStation
,
1030 commandPayload
: OCPP16UpdateFirmwareRequest
,
1031 ): OCPP16UpdateFirmwareResponse
{
1033 OCPP16ServiceUtils
.checkFeatureProfile(
1035 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1036 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1040 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1041 Cannot simulate firmware update: feature profile not supported`,
1043 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1046 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1047 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1050 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1051 Cannot simulate firmware update: firmware update is already in progress`,
1053 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1055 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1056 const now
= Date.now();
1057 if (retrieveDate
?.getTime() <= now
) {
1058 this.runInAsyncScope(
1059 this.updateFirmwareSimulation
.bind(this) as (
1060 this: OCPP16IncomingRequestService
,
1065 ).catch(Constants
.EMPTY_FUNCTION
);
1069 this.runInAsyncScope(
1070 this.updateFirmwareSimulation
.bind(this) as (
1071 this: OCPP16IncomingRequestService
,
1076 ).catch(Constants
.EMPTY_FUNCTION
);
1078 retrieveDate
?.getTime() - now
,
1081 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1084 private async updateFirmwareSimulation(
1085 chargingStation
: ChargingStation
,
1089 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1092 if (chargingStation
.hasEvses
) {
1093 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1095 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1096 if (connectorStatus
?.transactionStarted
=== false) {
1097 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1100 OCPP16ChargePointStatus
.Unavailable
,
1107 for (const connectorId
of chargingStation
.connectors
.keys()) {
1110 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1112 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1115 OCPP16ChargePointStatus
.Unavailable
,
1120 await chargingStation
.ocppRequestService
.requestHandler
<
1121 OCPP16FirmwareStatusNotificationRequest
,
1122 OCPP16FirmwareStatusNotificationResponse
1123 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1124 status: OCPP16FirmwareStatus
.Downloading
,
1126 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1128 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1129 OCPP16FirmwareStatus
.DownloadFailed
1131 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1132 await chargingStation
.ocppRequestService
.requestHandler
<
1133 OCPP16FirmwareStatusNotificationRequest
,
1134 OCPP16FirmwareStatusNotificationResponse
1135 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1136 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1138 chargingStation
.stationInfo
.firmwareStatus
=
1139 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1142 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1143 await chargingStation
.ocppRequestService
.requestHandler
<
1144 OCPP16FirmwareStatusNotificationRequest
,
1145 OCPP16FirmwareStatusNotificationResponse
1146 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1147 status: OCPP16FirmwareStatus
.Downloaded
,
1149 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1150 let wasTransactionsStarted
= false;
1151 let transactionsStarted
: boolean;
1153 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1154 if (runningTransactions
> 0) {
1155 const waitTime
= secondsToMilliseconds(15);
1157 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1158 ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1160 )} before continuing firmware update simulation`,
1162 await sleep(waitTime
);
1163 transactionsStarted
= true;
1164 wasTransactionsStarted
= true;
1166 if (chargingStation
.hasEvses
) {
1167 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1169 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1170 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1171 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1174 OCPP16ChargePointStatus
.Unavailable
,
1181 for (const connectorId
of chargingStation
.connectors
.keys()) {
1184 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1185 OCPP16ChargePointStatus
.Unavailable
1187 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1190 OCPP16ChargePointStatus
.Unavailable
,
1195 transactionsStarted
= false;
1197 } while (transactionsStarted
);
1198 !wasTransactionsStarted
&&
1199 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))));
1200 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1203 await chargingStation
.ocppRequestService
.requestHandler
<
1204 OCPP16FirmwareStatusNotificationRequest
,
1205 OCPP16FirmwareStatusNotificationResponse
1206 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1207 status: OCPP16FirmwareStatus
.Installing
,
1209 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1211 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1212 OCPP16FirmwareStatus
.InstallationFailed
1214 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1215 await chargingStation
.ocppRequestService
.requestHandler
<
1216 OCPP16FirmwareStatusNotificationRequest
,
1217 OCPP16FirmwareStatusNotificationResponse
1218 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1219 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1221 chargingStation
.stationInfo
.firmwareStatus
=
1222 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1225 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1226 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1227 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1231 private async handleRequestGetDiagnostics(
1232 chargingStation
: ChargingStation
,
1233 commandPayload
: GetDiagnosticsRequest
,
1234 ): Promise
<GetDiagnosticsResponse
> {
1236 OCPP16ServiceUtils
.checkFeatureProfile(
1238 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1239 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1243 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1244 Cannot get diagnostics: feature profile not supported`,
1246 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1248 const uri
= new URL(commandPayload
.location
);
1249 if (uri
.protocol
.startsWith('ftp:')) {
1250 let ftpClient
: Client
| undefined;
1252 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1253 .filter((file
) => file
.endsWith('.log'))
1254 .map((file
) => join('./', file
));
1255 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1256 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1257 ftpClient
= new Client();
1258 const accessResponse
= await ftpClient
.access({
1260 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1261 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1262 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1264 let uploadResponse
: FTPResponse
| undefined;
1265 if (accessResponse
.code
=== 220) {
1266 ftpClient
.trackProgress((info
) => {
1268 `${chargingStation.logPrefix()} ${
1270 } bytes transferred from diagnostics archive ${info.name}`,
1272 chargingStation
.ocppRequestService
1274 OCPP16DiagnosticsStatusNotificationRequest
,
1275 OCPP16DiagnosticsStatusNotificationResponse
1276 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1277 status: OCPP16DiagnosticsStatus
.Uploading
,
1281 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1282 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1287 uploadResponse
= await ftpClient
.uploadFrom(
1288 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1289 `${uri.pathname}${diagnosticsArchive}`,
1291 if (uploadResponse
.code
=== 226) {
1292 await chargingStation
.ocppRequestService
.requestHandler
<
1293 OCPP16DiagnosticsStatusNotificationRequest
,
1294 OCPP16DiagnosticsStatusNotificationResponse
1295 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1296 status: OCPP16DiagnosticsStatus
.Uploaded
,
1301 return { fileName
: diagnosticsArchive
};
1303 throw new OCPPError(
1304 ErrorType
.GENERIC_ERROR
,
1305 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1306 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1308 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1311 throw new OCPPError(
1312 ErrorType
.GENERIC_ERROR
,
1313 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1314 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1316 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1319 await chargingStation
.ocppRequestService
.requestHandler
<
1320 OCPP16DiagnosticsStatusNotificationRequest
,
1321 OCPP16DiagnosticsStatusNotificationResponse
1322 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1323 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1328 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1330 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1332 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1337 `${chargingStation.logPrefix()} Unsupported protocol ${
1339 } to transfer the diagnostic logs archive`,
1341 await chargingStation
.ocppRequestService
.requestHandler
<
1342 OCPP16DiagnosticsStatusNotificationRequest
,
1343 OCPP16DiagnosticsStatusNotificationResponse
1344 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1345 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1347 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1351 private handleRequestTriggerMessage(
1352 chargingStation
: ChargingStation
,
1353 commandPayload
: OCPP16TriggerMessageRequest
,
1354 ): OCPP16TriggerMessageResponse
{
1356 !OCPP16ServiceUtils
.checkFeatureProfile(
1358 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1359 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1361 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1363 commandPayload
.requestedMessage
,
1366 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1369 !OCPP16ServiceUtils
.isConnectorIdValid(
1371 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1372 commandPayload
.connectorId
!,
1375 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1378 switch (commandPayload
.requestedMessage
) {
1379 case OCPP16MessageTrigger
.BootNotification
:
1381 chargingStation
.ocppRequestService
1382 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1384 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1385 chargingStation
.bootNotificationRequest
,
1386 { skipBufferingOnError
: true, triggerMessage
: true },
1388 .then((response
) => {
1389 chargingStation
.bootNotificationResponse
= response
;
1391 .catch(Constants
.EMPTY_FUNCTION
);
1392 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1393 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1394 case OCPP16MessageTrigger
.Heartbeat
:
1396 chargingStation
.ocppRequestService
1397 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1399 OCPP16RequestCommand
.HEARTBEAT
,
1402 triggerMessage
: true,
1405 .catch(Constants
.EMPTY_FUNCTION
);
1406 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1407 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1408 case OCPP16MessageTrigger
.StatusNotification
:
1410 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1411 chargingStation
.ocppRequestService
1412 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1414 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1416 connectorId
: commandPayload
.connectorId
,
1417 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1418 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1421 triggerMessage
: true,
1424 .catch(Constants
.EMPTY_FUNCTION
);
1426 // eslint-disable-next-line no-lonely-if
1427 if (chargingStation
.hasEvses
) {
1428 for (const evseStatus
of chargingStation
.evses
.values()) {
1429 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1430 chargingStation
.ocppRequestService
1432 OCPP16StatusNotificationRequest
,
1433 OCPP16StatusNotificationResponse
1436 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1439 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1440 status: connectorStatus
.status,
1443 triggerMessage
: true,
1446 .catch(Constants
.EMPTY_FUNCTION
);
1450 for (const connectorId
of chargingStation
.connectors
.keys()) {
1451 chargingStation
.ocppRequestService
1453 OCPP16StatusNotificationRequest
,
1454 OCPP16StatusNotificationResponse
1457 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1460 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1461 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1464 triggerMessage
: true,
1467 .catch(Constants
.EMPTY_FUNCTION
);
1471 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1472 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1474 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1477 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1479 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1481 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1486 private handleRequestDataTransfer(
1487 chargingStation
: ChargingStation
,
1488 commandPayload
: OCPP16DataTransferRequest
,
1489 ): OCPP16DataTransferResponse
{
1491 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1492 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1494 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1496 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1498 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1500 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1505 private async handleRequestReserveNow(
1506 chargingStation
: ChargingStation
,
1507 commandPayload
: OCPP16ReserveNowRequest
,
1508 ): Promise
<OCPP16ReserveNowResponse
> {
1510 !OCPP16ServiceUtils
.checkFeatureProfile(
1512 OCPP16SupportedFeatureProfiles
.Reservation
,
1513 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1516 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1518 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1519 let response
: OCPP16ReserveNowResponse
;
1521 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1522 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1524 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1525 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1527 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1528 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1530 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1531 case OCPP16ChargePointStatus
.Faulted
:
1532 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1534 case OCPP16ChargePointStatus
.Preparing
:
1535 case OCPP16ChargePointStatus
.Charging
:
1536 case OCPP16ChargePointStatus
.SuspendedEV
:
1537 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1538 case OCPP16ChargePointStatus
.Finishing
:
1539 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1541 case OCPP16ChargePointStatus
.Unavailable
:
1542 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1544 case OCPP16ChargePointStatus
.Reserved
:
1545 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1546 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1549 // eslint-disable-next-line no-fallthrough
1551 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1552 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1555 await chargingStation
.addReservation({
1556 id
: commandPayload
.reservationId
,
1559 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1564 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1565 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1567 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1569 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1574 private async handleRequestCancelReservation(
1575 chargingStation
: ChargingStation
,
1576 commandPayload
: OCPP16CancelReservationRequest
,
1577 ): Promise
<GenericResponse
> {
1579 !OCPP16ServiceUtils
.checkFeatureProfile(
1581 OCPP16SupportedFeatureProfiles
.Reservation
,
1582 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1585 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1588 const { reservationId
} = commandPayload
;
1589 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
);
1590 if (isUndefined(reservation
)) {
1592 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1593 does not exist on charging station`,
1595 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1597 await chargingStation
.removeReservation(
1599 ReservationTerminationReason
.RESERVATION_CANCELED
,
1601 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1603 return this.handleIncomingRequestError
<GenericResponse
>(
1605 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1607 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},