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 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1006 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1009 OCPP16ChargePointStatus
.Finishing
,
1011 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1013 OCPP16StopTransactionReason
.REMOTE
,
1015 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1016 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1018 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1020 if (chargingStation
.hasEvses
) {
1021 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1023 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1024 if (connectorStatus
.transactionId
=== transactionId
) {
1025 return remoteStopTransaction(connectorId
);
1031 for (const connectorId
of chargingStation
.connectors
.keys()) {
1034 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1036 return remoteStopTransaction(connectorId
);
1041 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1042 ${transactionId.toString()}`,
1044 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1047 private handleRequestUpdateFirmware(
1048 chargingStation
: ChargingStation
,
1049 commandPayload
: OCPP16UpdateFirmwareRequest
,
1050 ): OCPP16UpdateFirmwareResponse
{
1052 OCPP16ServiceUtils
.checkFeatureProfile(
1054 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1055 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1059 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1060 Cannot simulate firmware update: feature profile not supported`,
1062 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1065 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1066 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1069 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1070 Cannot simulate firmware update: firmware update is already in progress`,
1072 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1074 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1075 const now
= Date.now();
1076 if (retrieveDate
?.getTime() <= now
) {
1077 this.runInAsyncScope(
1078 this.updateFirmwareSimulation
.bind(this) as (
1079 this: OCPP16IncomingRequestService
,
1084 ).catch(Constants
.EMPTY_FUNCTION
);
1088 this.runInAsyncScope(
1089 this.updateFirmwareSimulation
.bind(this) as (
1090 this: OCPP16IncomingRequestService
,
1095 ).catch(Constants
.EMPTY_FUNCTION
);
1097 retrieveDate
?.getTime() - now
,
1100 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1103 private async updateFirmwareSimulation(
1104 chargingStation
: ChargingStation
,
1108 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1111 if (chargingStation
.hasEvses
) {
1112 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1114 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1115 if (connectorStatus
?.transactionStarted
=== false) {
1116 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1119 OCPP16ChargePointStatus
.Unavailable
,
1126 for (const connectorId
of chargingStation
.connectors
.keys()) {
1129 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1131 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1134 OCPP16ChargePointStatus
.Unavailable
,
1139 await chargingStation
.ocppRequestService
.requestHandler
<
1140 OCPP16FirmwareStatusNotificationRequest
,
1141 OCPP16FirmwareStatusNotificationResponse
1142 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1143 status: OCPP16FirmwareStatus
.Downloading
,
1145 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1147 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1148 OCPP16FirmwareStatus
.DownloadFailed
1150 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1151 await chargingStation
.ocppRequestService
.requestHandler
<
1152 OCPP16FirmwareStatusNotificationRequest
,
1153 OCPP16FirmwareStatusNotificationResponse
1154 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1155 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1157 chargingStation
.stationInfo
.firmwareStatus
=
1158 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1161 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1162 await chargingStation
.ocppRequestService
.requestHandler
<
1163 OCPP16FirmwareStatusNotificationRequest
,
1164 OCPP16FirmwareStatusNotificationResponse
1165 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1166 status: OCPP16FirmwareStatus
.Downloaded
,
1168 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1169 let wasTransactionsStarted
= false;
1170 let transactionsStarted
: boolean;
1172 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1173 if (runningTransactions
> 0) {
1174 const waitTime
= secondsToMilliseconds(15);
1176 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1177 ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1179 )} before continuing firmware update simulation`,
1181 await sleep(waitTime
);
1182 transactionsStarted
= true;
1183 wasTransactionsStarted
= true;
1185 if (chargingStation
.hasEvses
) {
1186 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1188 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1189 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1190 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1193 OCPP16ChargePointStatus
.Unavailable
,
1200 for (const connectorId
of chargingStation
.connectors
.keys()) {
1203 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1204 OCPP16ChargePointStatus
.Unavailable
1206 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1209 OCPP16ChargePointStatus
.Unavailable
,
1214 transactionsStarted
= false;
1216 } while (transactionsStarted
);
1217 !wasTransactionsStarted
&&
1218 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))));
1219 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1222 await chargingStation
.ocppRequestService
.requestHandler
<
1223 OCPP16FirmwareStatusNotificationRequest
,
1224 OCPP16FirmwareStatusNotificationResponse
1225 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1226 status: OCPP16FirmwareStatus
.Installing
,
1228 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1230 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1231 OCPP16FirmwareStatus
.InstallationFailed
1233 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1234 await chargingStation
.ocppRequestService
.requestHandler
<
1235 OCPP16FirmwareStatusNotificationRequest
,
1236 OCPP16FirmwareStatusNotificationResponse
1237 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1238 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1240 chargingStation
.stationInfo
.firmwareStatus
=
1241 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1244 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1245 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1246 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1250 private async handleRequestGetDiagnostics(
1251 chargingStation
: ChargingStation
,
1252 commandPayload
: GetDiagnosticsRequest
,
1253 ): Promise
<GetDiagnosticsResponse
> {
1255 OCPP16ServiceUtils
.checkFeatureProfile(
1257 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1258 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1262 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1263 Cannot get diagnostics: feature profile not supported`,
1265 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1267 const uri
= new URL(commandPayload
.location
);
1268 if (uri
.protocol
.startsWith('ftp:')) {
1269 let ftpClient
: Client
| undefined;
1271 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1272 .filter((file
) => file
.endsWith('.log'))
1273 .map((file
) => join('./', file
));
1274 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1275 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1276 ftpClient
= new Client();
1277 const accessResponse
= await ftpClient
.access({
1279 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1280 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1281 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1283 let uploadResponse
: FTPResponse
| undefined;
1284 if (accessResponse
.code
=== 220) {
1285 ftpClient
.trackProgress((info
) => {
1287 `${chargingStation.logPrefix()} ${
1289 } bytes transferred from diagnostics archive ${info.name}`,
1291 chargingStation
.ocppRequestService
1293 OCPP16DiagnosticsStatusNotificationRequest
,
1294 OCPP16DiagnosticsStatusNotificationResponse
1295 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1296 status: OCPP16DiagnosticsStatus
.Uploading
,
1300 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1301 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1306 uploadResponse
= await ftpClient
.uploadFrom(
1307 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1308 `${uri.pathname}${diagnosticsArchive}`,
1310 if (uploadResponse
.code
=== 226) {
1311 await chargingStation
.ocppRequestService
.requestHandler
<
1312 OCPP16DiagnosticsStatusNotificationRequest
,
1313 OCPP16DiagnosticsStatusNotificationResponse
1314 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1315 status: OCPP16DiagnosticsStatus
.Uploaded
,
1320 return { fileName
: diagnosticsArchive
};
1322 throw new OCPPError(
1323 ErrorType
.GENERIC_ERROR
,
1324 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1325 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1327 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1330 throw new OCPPError(
1331 ErrorType
.GENERIC_ERROR
,
1332 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1333 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1335 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1338 await chargingStation
.ocppRequestService
.requestHandler
<
1339 OCPP16DiagnosticsStatusNotificationRequest
,
1340 OCPP16DiagnosticsStatusNotificationResponse
1341 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1342 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1347 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1349 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1351 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1356 `${chargingStation.logPrefix()} Unsupported protocol ${
1358 } to transfer the diagnostic logs archive`,
1360 await chargingStation
.ocppRequestService
.requestHandler
<
1361 OCPP16DiagnosticsStatusNotificationRequest
,
1362 OCPP16DiagnosticsStatusNotificationResponse
1363 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1364 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1366 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1370 private handleRequestTriggerMessage(
1371 chargingStation
: ChargingStation
,
1372 commandPayload
: OCPP16TriggerMessageRequest
,
1373 ): OCPP16TriggerMessageResponse
{
1375 !OCPP16ServiceUtils
.checkFeatureProfile(
1377 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1378 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1380 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1382 commandPayload
.requestedMessage
,
1385 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1388 !OCPP16ServiceUtils
.isConnectorIdValid(
1390 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1391 commandPayload
.connectorId
!,
1394 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1397 switch (commandPayload
.requestedMessage
) {
1398 case OCPP16MessageTrigger
.BootNotification
:
1400 chargingStation
.ocppRequestService
1401 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1403 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1404 chargingStation
.bootNotificationRequest
,
1405 { skipBufferingOnError
: true, triggerMessage
: true },
1407 .then((response
) => {
1408 chargingStation
.bootNotificationResponse
= response
;
1410 .catch(Constants
.EMPTY_FUNCTION
);
1411 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1412 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1413 case OCPP16MessageTrigger
.Heartbeat
:
1415 chargingStation
.ocppRequestService
1416 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1418 OCPP16RequestCommand
.HEARTBEAT
,
1421 triggerMessage
: true,
1424 .catch(Constants
.EMPTY_FUNCTION
);
1425 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1426 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1427 case OCPP16MessageTrigger
.StatusNotification
:
1429 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1430 chargingStation
.ocppRequestService
1431 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1433 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1435 connectorId
: commandPayload
.connectorId
,
1436 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1437 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1440 triggerMessage
: true,
1443 .catch(Constants
.EMPTY_FUNCTION
);
1445 // eslint-disable-next-line no-lonely-if
1446 if (chargingStation
.hasEvses
) {
1447 for (const evseStatus
of chargingStation
.evses
.values()) {
1448 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1449 chargingStation
.ocppRequestService
1451 OCPP16StatusNotificationRequest
,
1452 OCPP16StatusNotificationResponse
1455 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1458 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1459 status: connectorStatus
.status,
1462 triggerMessage
: true,
1465 .catch(Constants
.EMPTY_FUNCTION
);
1469 for (const connectorId
of chargingStation
.connectors
.keys()) {
1470 chargingStation
.ocppRequestService
1472 OCPP16StatusNotificationRequest
,
1473 OCPP16StatusNotificationResponse
1476 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1479 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1480 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1483 triggerMessage
: true,
1486 .catch(Constants
.EMPTY_FUNCTION
);
1490 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1491 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1493 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1496 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1498 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1500 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1505 private handleRequestDataTransfer(
1506 chargingStation
: ChargingStation
,
1507 commandPayload
: OCPP16DataTransferRequest
,
1508 ): OCPP16DataTransferResponse
{
1510 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1511 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1513 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1515 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1517 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1519 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1524 private async handleRequestReserveNow(
1525 chargingStation
: ChargingStation
,
1526 commandPayload
: OCPP16ReserveNowRequest
,
1527 ): Promise
<OCPP16ReserveNowResponse
> {
1529 !OCPP16ServiceUtils
.checkFeatureProfile(
1531 OCPP16SupportedFeatureProfiles
.Reservation
,
1532 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1535 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1537 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1538 let response
: OCPP16ReserveNowResponse
;
1540 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1541 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1543 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1544 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1546 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1547 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1549 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1550 case OCPP16ChargePointStatus
.Faulted
:
1551 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1553 case OCPP16ChargePointStatus
.Preparing
:
1554 case OCPP16ChargePointStatus
.Charging
:
1555 case OCPP16ChargePointStatus
.SuspendedEV
:
1556 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1557 case OCPP16ChargePointStatus
.Finishing
:
1558 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1560 case OCPP16ChargePointStatus
.Unavailable
:
1561 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1563 case OCPP16ChargePointStatus
.Reserved
:
1564 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1565 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1568 // eslint-disable-next-line no-fallthrough
1570 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1571 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1574 await chargingStation
.addReservation({
1575 id
: commandPayload
.reservationId
,
1578 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1583 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1584 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1586 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1588 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1593 private async handleRequestCancelReservation(
1594 chargingStation
: ChargingStation
,
1595 commandPayload
: OCPP16CancelReservationRequest
,
1596 ): Promise
<GenericResponse
> {
1598 !OCPP16ServiceUtils
.checkFeatureProfile(
1600 OCPP16SupportedFeatureProfiles
.Reservation
,
1601 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1604 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1607 const { reservationId
} = commandPayload
;
1608 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
);
1609 if (isUndefined(reservation
)) {
1611 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1612 does not exist on charging station`,
1614 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1616 await chargingStation
.removeReservation(
1618 ReservationTerminationReason
.RESERVATION_CANCELED
,
1620 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1622 return this.handleIncomingRequestError
<GenericResponse
>(
1624 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1626 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},