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 for (const id
of evseStatus
.connectors
.keys()) {
778 response
= await OCPP16ServiceUtils
.changeAvailability(
787 for (const id
of chargingStation
.connectors
.keys()) {
788 response
= await OCPP16ServiceUtils
.changeAvailability(
799 (chargingStation
.isChargingStationAvailable() === true ||
800 (chargingStation
.isChargingStationAvailable() === false &&
801 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
803 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
804 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
805 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
807 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
808 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
813 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
815 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
818 private async handleRequestRemoteStartTransaction(
819 chargingStation
: ChargingStation
,
820 commandPayload
: RemoteStartTransactionRequest
,
821 ): Promise
<GenericResponse
> {
822 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
823 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
824 return this.notifyRemoteStartTransactionRejected(
826 transactionConnectorId
,
831 !chargingStation
.isChargingStationAvailable() ||
832 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
834 return this.notifyRemoteStartTransactionRejected(
836 transactionConnectorId
,
841 (chargingStation
.getConnectorStatus(transactionConnectorId
)?.status ===
842 OCPP16ChargePointStatus
.Reserved
&&
843 chargingStation
.getReservationBy('connectorId', transactionConnectorId
)?.idTag
!== idTag
) ||
844 (chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
&&
845 chargingStation
.getReservationBy('connectorId', 0)?.idTag
!== idTag
)
847 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
849 const remoteStartTransactionLogMsg
= `
850 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
851 chargingStation.stationInfo.chargingStationId
852 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
853 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
855 transactionConnectorId
,
856 OCPP16ChargePointStatus
.Preparing
,
858 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
859 // Authorization check required
861 chargingStation
.getAuthorizeRemoteTxRequests() === true &&
862 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
864 // Authorization successful, start transaction
866 this.setRemoteStartTransactionChargingProfile(
868 transactionConnectorId
,
872 connectorStatus
.transactionRemoteStarted
= true;
875 await chargingStation
.ocppRequestService
.requestHandler
<
876 OCPP16StartTransactionRequest
,
877 OCPP16StartTransactionResponse
878 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
879 connectorId
: transactionConnectorId
,
881 reservationId
: chargingStation
.getReservationBy(
883 chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
885 : transactionConnectorId
,
888 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
890 logger
.debug(remoteStartTransactionLogMsg
);
891 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
893 return this.notifyRemoteStartTransactionRejected(
895 transactionConnectorId
,
899 return this.notifyRemoteStartTransactionRejected(
901 transactionConnectorId
,
905 // No authorization check required, start transaction
907 this.setRemoteStartTransactionChargingProfile(
909 transactionConnectorId
,
913 connectorStatus
.transactionRemoteStarted
= true;
916 await chargingStation
.ocppRequestService
.requestHandler
<
917 OCPP16StartTransactionRequest
,
918 OCPP16StartTransactionResponse
919 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
920 connectorId
: transactionConnectorId
,
922 reservationId
: chargingStation
.getReservationBy(
924 chargingStation
.getConnectorStatus(0)?.status === OCPP16ChargePointStatus
.Reserved
926 : transactionConnectorId
,
929 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
931 logger
.debug(remoteStartTransactionLogMsg
);
932 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
934 return this.notifyRemoteStartTransactionRejected(
936 transactionConnectorId
,
940 return this.notifyRemoteStartTransactionRejected(
942 transactionConnectorId
,
947 private async notifyRemoteStartTransactionRejected(
948 chargingStation
: ChargingStation
,
951 ): Promise
<GenericResponse
> {
953 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
955 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
958 OCPP16ChargePointStatus
.Available
,
962 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
963 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
965 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
967 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
970 private setRemoteStartTransactionChargingProfile(
971 chargingStation
: ChargingStation
,
973 chargingProfile
: OCPP16ChargingProfile
,
977 chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
979 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
);
981 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
982 on connector id ${connectorId}: %j`,
988 chargingProfile
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
991 `${chargingStation.logPrefix()} Not allowed to set ${
992 chargingProfile.chargingProfilePurpose
993 } charging profile(s) at remote start transaction`,
1000 private async handleRequestRemoteStopTransaction(
1001 chargingStation
: ChargingStation
,
1002 commandPayload
: RemoteStopTransactionRequest
,
1003 ): Promise
<GenericResponse
> {
1004 const transactionId
= commandPayload
.transactionId
;
1005 if (chargingStation
.hasEvses
) {
1006 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1008 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1009 if (connectorStatus
.transactionId
=== transactionId
) {
1010 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1016 for (const connectorId
of chargingStation
.connectors
.keys()) {
1019 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1021 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1026 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1027 ${transactionId.toString()}`,
1029 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1032 private handleRequestUpdateFirmware(
1033 chargingStation
: ChargingStation
,
1034 commandPayload
: OCPP16UpdateFirmwareRequest
,
1035 ): OCPP16UpdateFirmwareResponse
{
1037 OCPP16ServiceUtils
.checkFeatureProfile(
1039 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1040 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1044 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1045 Cannot simulate firmware update: feature profile not supported`,
1047 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1050 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1051 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1054 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1055 Cannot simulate firmware update: firmware update is already in progress`,
1057 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1059 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1060 const now
= Date.now();
1061 if (retrieveDate
?.getTime() <= now
) {
1062 this.runInAsyncScope(
1063 this.updateFirmwareSimulation
.bind(this) as (
1064 this: OCPP16IncomingRequestService
,
1069 ).catch(Constants
.EMPTY_FUNCTION
);
1073 this.runInAsyncScope(
1074 this.updateFirmwareSimulation
.bind(this) as (
1075 this: OCPP16IncomingRequestService
,
1080 ).catch(Constants
.EMPTY_FUNCTION
);
1082 retrieveDate
?.getTime() - now
,
1085 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1088 private async updateFirmwareSimulation(
1089 chargingStation
: ChargingStation
,
1093 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1096 if (chargingStation
.hasEvses
) {
1097 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1099 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1100 if (connectorStatus
?.transactionStarted
=== false) {
1101 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1104 OCPP16ChargePointStatus
.Unavailable
,
1111 for (const connectorId
of chargingStation
.connectors
.keys()) {
1114 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1116 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1119 OCPP16ChargePointStatus
.Unavailable
,
1124 await chargingStation
.ocppRequestService
.requestHandler
<
1125 OCPP16FirmwareStatusNotificationRequest
,
1126 OCPP16FirmwareStatusNotificationResponse
1127 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1128 status: OCPP16FirmwareStatus
.Downloading
,
1130 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1132 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1133 OCPP16FirmwareStatus
.DownloadFailed
1135 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1136 await chargingStation
.ocppRequestService
.requestHandler
<
1137 OCPP16FirmwareStatusNotificationRequest
,
1138 OCPP16FirmwareStatusNotificationResponse
1139 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1140 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1142 chargingStation
.stationInfo
.firmwareStatus
=
1143 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1146 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1147 await chargingStation
.ocppRequestService
.requestHandler
<
1148 OCPP16FirmwareStatusNotificationRequest
,
1149 OCPP16FirmwareStatusNotificationResponse
1150 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1151 status: OCPP16FirmwareStatus
.Downloaded
,
1153 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1154 let wasTransactionsStarted
= false;
1155 let transactionsStarted
: boolean;
1157 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1158 if (runningTransactions
> 0) {
1159 const waitTime
= secondsToMilliseconds(15);
1161 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1162 ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1164 )} before continuing firmware update simulation`,
1166 await sleep(waitTime
);
1167 transactionsStarted
= true;
1168 wasTransactionsStarted
= true;
1170 if (chargingStation
.hasEvses
) {
1171 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1173 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1174 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1175 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1178 OCPP16ChargePointStatus
.Unavailable
,
1185 for (const connectorId
of chargingStation
.connectors
.keys()) {
1188 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1189 OCPP16ChargePointStatus
.Unavailable
1191 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1194 OCPP16ChargePointStatus
.Unavailable
,
1199 transactionsStarted
= false;
1201 } while (transactionsStarted
);
1202 !wasTransactionsStarted
&&
1203 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))));
1204 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1207 await chargingStation
.ocppRequestService
.requestHandler
<
1208 OCPP16FirmwareStatusNotificationRequest
,
1209 OCPP16FirmwareStatusNotificationResponse
1210 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1211 status: OCPP16FirmwareStatus
.Installing
,
1213 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1215 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1216 OCPP16FirmwareStatus
.InstallationFailed
1218 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1219 await chargingStation
.ocppRequestService
.requestHandler
<
1220 OCPP16FirmwareStatusNotificationRequest
,
1221 OCPP16FirmwareStatusNotificationResponse
1222 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1223 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1225 chargingStation
.stationInfo
.firmwareStatus
=
1226 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1229 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1230 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1231 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1235 private async handleRequestGetDiagnostics(
1236 chargingStation
: ChargingStation
,
1237 commandPayload
: GetDiagnosticsRequest
,
1238 ): Promise
<GetDiagnosticsResponse
> {
1240 OCPP16ServiceUtils
.checkFeatureProfile(
1242 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1243 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1247 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1248 Cannot get diagnostics: feature profile not supported`,
1250 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1252 const uri
= new URL(commandPayload
.location
);
1253 if (uri
.protocol
.startsWith('ftp:')) {
1254 let ftpClient
: Client
| undefined;
1256 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1257 .filter((file
) => file
.endsWith('.log'))
1258 .map((file
) => join('./', file
));
1259 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1260 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1261 ftpClient
= new Client();
1262 const accessResponse
= await ftpClient
.access({
1264 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1265 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1266 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1268 let uploadResponse
: FTPResponse
| undefined;
1269 if (accessResponse
.code
=== 220) {
1270 ftpClient
.trackProgress((info
) => {
1272 `${chargingStation.logPrefix()} ${
1274 } bytes transferred from diagnostics archive ${info.name}`,
1276 chargingStation
.ocppRequestService
1278 OCPP16DiagnosticsStatusNotificationRequest
,
1279 OCPP16DiagnosticsStatusNotificationResponse
1280 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1281 status: OCPP16DiagnosticsStatus
.Uploading
,
1285 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1286 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1291 uploadResponse
= await ftpClient
.uploadFrom(
1292 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1293 `${uri.pathname}${diagnosticsArchive}`,
1295 if (uploadResponse
.code
=== 226) {
1296 await chargingStation
.ocppRequestService
.requestHandler
<
1297 OCPP16DiagnosticsStatusNotificationRequest
,
1298 OCPP16DiagnosticsStatusNotificationResponse
1299 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1300 status: OCPP16DiagnosticsStatus
.Uploaded
,
1305 return { fileName
: diagnosticsArchive
};
1307 throw new OCPPError(
1308 ErrorType
.GENERIC_ERROR
,
1309 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1310 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1312 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1315 throw new OCPPError(
1316 ErrorType
.GENERIC_ERROR
,
1317 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1318 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1320 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1323 await chargingStation
.ocppRequestService
.requestHandler
<
1324 OCPP16DiagnosticsStatusNotificationRequest
,
1325 OCPP16DiagnosticsStatusNotificationResponse
1326 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1327 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1332 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1334 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1336 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1341 `${chargingStation.logPrefix()} Unsupported protocol ${
1343 } to transfer the diagnostic logs archive`,
1345 await chargingStation
.ocppRequestService
.requestHandler
<
1346 OCPP16DiagnosticsStatusNotificationRequest
,
1347 OCPP16DiagnosticsStatusNotificationResponse
1348 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1349 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1351 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1355 private handleRequestTriggerMessage(
1356 chargingStation
: ChargingStation
,
1357 commandPayload
: OCPP16TriggerMessageRequest
,
1358 ): OCPP16TriggerMessageResponse
{
1360 !OCPP16ServiceUtils
.checkFeatureProfile(
1362 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1363 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1365 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1367 commandPayload
.requestedMessage
,
1370 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1373 !OCPP16ServiceUtils
.isConnectorIdValid(
1375 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1376 commandPayload
.connectorId
!,
1379 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1382 switch (commandPayload
.requestedMessage
) {
1383 case OCPP16MessageTrigger
.BootNotification
:
1385 chargingStation
.ocppRequestService
1386 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1388 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1389 chargingStation
.bootNotificationRequest
,
1390 { skipBufferingOnError
: true, triggerMessage
: true },
1392 .then((response
) => {
1393 chargingStation
.bootNotificationResponse
= response
;
1395 .catch(Constants
.EMPTY_FUNCTION
);
1396 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1397 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1398 case OCPP16MessageTrigger
.Heartbeat
:
1400 chargingStation
.ocppRequestService
1401 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1403 OCPP16RequestCommand
.HEARTBEAT
,
1406 triggerMessage
: true,
1409 .catch(Constants
.EMPTY_FUNCTION
);
1410 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1411 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1412 case OCPP16MessageTrigger
.StatusNotification
:
1414 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1415 chargingStation
.ocppRequestService
1416 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1418 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1420 connectorId
: commandPayload
.connectorId
,
1421 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1422 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1425 triggerMessage
: true,
1428 .catch(Constants
.EMPTY_FUNCTION
);
1430 // eslint-disable-next-line no-lonely-if
1431 if (chargingStation
.hasEvses
) {
1432 for (const evseStatus
of chargingStation
.evses
.values()) {
1433 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1434 chargingStation
.ocppRequestService
1436 OCPP16StatusNotificationRequest
,
1437 OCPP16StatusNotificationResponse
1440 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1443 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1444 status: connectorStatus
.status,
1447 triggerMessage
: true,
1450 .catch(Constants
.EMPTY_FUNCTION
);
1454 for (const connectorId
of chargingStation
.connectors
.keys()) {
1455 chargingStation
.ocppRequestService
1457 OCPP16StatusNotificationRequest
,
1458 OCPP16StatusNotificationResponse
1461 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1464 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1465 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1468 triggerMessage
: true,
1471 .catch(Constants
.EMPTY_FUNCTION
);
1475 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1476 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1478 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1481 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1483 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1485 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1490 private handleRequestDataTransfer(
1491 chargingStation
: ChargingStation
,
1492 commandPayload
: OCPP16DataTransferRequest
,
1493 ): OCPP16DataTransferResponse
{
1495 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1496 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1498 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1500 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1502 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1504 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1509 private async handleRequestReserveNow(
1510 chargingStation
: ChargingStation
,
1511 commandPayload
: OCPP16ReserveNowRequest
,
1512 ): Promise
<OCPP16ReserveNowResponse
> {
1514 !OCPP16ServiceUtils
.checkFeatureProfile(
1516 OCPP16SupportedFeatureProfiles
.Reservation
,
1517 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1520 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1522 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1523 let response
: OCPP16ReserveNowResponse
;
1525 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1526 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1528 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1529 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1531 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1532 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1534 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1535 case OCPP16ChargePointStatus
.Faulted
:
1536 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1538 case OCPP16ChargePointStatus
.Preparing
:
1539 case OCPP16ChargePointStatus
.Charging
:
1540 case OCPP16ChargePointStatus
.SuspendedEV
:
1541 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1542 case OCPP16ChargePointStatus
.Finishing
:
1543 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1545 case OCPP16ChargePointStatus
.Unavailable
:
1546 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1548 case OCPP16ChargePointStatus
.Reserved
:
1549 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1550 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1553 // eslint-disable-next-line no-fallthrough
1555 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1556 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1559 await chargingStation
.addReservation({
1560 id
: commandPayload
.reservationId
,
1563 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1568 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1569 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1571 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1573 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1578 private async handleRequestCancelReservation(
1579 chargingStation
: ChargingStation
,
1580 commandPayload
: OCPP16CancelReservationRequest
,
1581 ): Promise
<GenericResponse
> {
1583 !OCPP16ServiceUtils
.checkFeatureProfile(
1585 OCPP16SupportedFeatureProfiles
.Reservation
,
1586 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1589 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1592 const { reservationId
} = commandPayload
;
1593 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
);
1594 if (isUndefined(reservation
)) {
1596 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1597 does not exist on charging station`,
1599 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1601 await chargingStation
.removeReservation(
1603 ReservationTerminationReason
.RESERVATION_CANCELED
,
1605 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1607 return this.handleIncomingRequestError
<GenericResponse
>(
1609 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1611 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},