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 { isWithinInterval
, secondsToMilliseconds
} from
'date-fns';
10 import { create
} from
'tar';
12 import { OCPP16Constants
} from
'./OCPP16Constants';
13 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
18 removeExpiredReservations
,
19 setConfigurationKeyValue
,
20 } from
'../../../charging-station';
21 import { OCPPError
} from
'../../../exception';
23 type ChangeConfigurationRequest
,
24 type ChangeConfigurationResponse
,
25 type ClearChargingProfileRequest
,
26 type ClearChargingProfileResponse
,
30 type GetConfigurationRequest
,
31 type GetConfigurationResponse
,
32 type GetDiagnosticsRequest
,
33 type GetDiagnosticsResponse
,
34 type IncomingRequestHandler
,
37 OCPP16AuthorizationStatus
,
38 OCPP16AvailabilityType
,
39 type OCPP16BootNotificationRequest
,
40 type OCPP16BootNotificationResponse
,
41 type OCPP16CancelReservationRequest
,
42 type OCPP16ChangeAvailabilityRequest
,
43 type OCPP16ChangeAvailabilityResponse
,
44 OCPP16ChargePointErrorCode
,
45 OCPP16ChargePointStatus
,
46 type OCPP16ChargingProfile
,
47 OCPP16ChargingProfilePurposeType
,
48 type OCPP16ChargingSchedule
,
49 type OCPP16ClearCacheRequest
,
50 type OCPP16DataTransferRequest
,
51 type OCPP16DataTransferResponse
,
52 OCPP16DataTransferVendorId
,
53 OCPP16DiagnosticsStatus
,
54 type OCPP16DiagnosticsStatusNotificationRequest
,
55 type OCPP16DiagnosticsStatusNotificationResponse
,
57 type OCPP16FirmwareStatusNotificationRequest
,
58 type OCPP16FirmwareStatusNotificationResponse
,
59 type OCPP16GetCompositeScheduleRequest
,
60 type OCPP16GetCompositeScheduleResponse
,
61 type OCPP16HeartbeatRequest
,
62 type OCPP16HeartbeatResponse
,
63 OCPP16IncomingRequestCommand
,
66 type OCPP16ReserveNowRequest
,
67 type OCPP16ReserveNowResponse
,
68 OCPP16StandardParametersKey
,
69 type OCPP16StartTransactionRequest
,
70 type OCPP16StartTransactionResponse
,
71 type OCPP16StatusNotificationRequest
,
72 type OCPP16StatusNotificationResponse
,
73 OCPP16StopTransactionReason
,
74 OCPP16SupportedFeatureProfiles
,
75 type OCPP16TriggerMessageRequest
,
76 type OCPP16TriggerMessageResponse
,
77 type OCPP16UpdateFirmwareRequest
,
78 type OCPP16UpdateFirmwareResponse
,
79 type OCPPConfigurationKey
,
81 type RemoteStartTransactionRequest
,
82 type RemoteStopTransactionRequest
,
83 ReservationTerminationReason
,
85 type SetChargingProfileRequest
,
86 type SetChargingProfileResponse
,
87 type UnlockConnectorRequest
,
88 type UnlockConnectorResponse
,
89 } from
'../../../types';
94 formatDurationMilliSeconds
,
103 } from
'../../../utils';
104 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
106 const moduleName
= 'OCPP16IncomingRequestService';
108 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
109 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
110 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
112 public constructor() {
113 // if (new.target?.name === moduleName) {
114 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
116 super(OCPPVersion
.VERSION_16
);
117 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
119 OCPP16IncomingRequestCommand
.RESET
,
120 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
,
123 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
124 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
,
127 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
128 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
,
131 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
132 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
,
135 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
136 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
,
139 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
140 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
,
143 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
144 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
,
147 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
148 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
,
151 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
152 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
,
155 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
156 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
159 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
160 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
163 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
164 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
,
167 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
168 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
,
171 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
172 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
,
175 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
176 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
,
179 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
180 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
,
183 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
184 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
,
187 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
189 OCPP16IncomingRequestCommand
.RESET
,
190 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
191 'assets/json-schemas/ocpp/1.6/Reset.json',
197 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
198 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
199 'assets/json-schemas/ocpp/1.6/ClearCache.json',
205 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
207 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
213 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
214 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
215 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
221 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
222 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
223 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
229 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
231 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
237 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
238 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
239 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
245 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
246 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
247 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
253 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
255 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
261 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
262 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ChangeAvailabilityRequest
>(
263 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
269 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
270 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
271 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
277 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
279 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
285 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
286 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
287 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
293 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
294 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
295 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
301 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
302 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
303 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
309 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
310 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
311 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
317 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
318 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
319 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
325 this.validatePayload
= this.validatePayload
.bind(this) as (
326 chargingStation
: ChargingStation
,
327 commandName
: OCPP16IncomingRequestCommand
,
328 commandPayload
: JsonType
,
332 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
333 chargingStation
: ChargingStation
,
335 commandName
: OCPP16IncomingRequestCommand
,
336 commandPayload
: ReqType
,
338 let response
: ResType
;
340 chargingStation
.getOcppStrictCompliance() === true &&
341 chargingStation
.inPendingState() === true &&
342 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
343 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
346 ErrorType
.SECURITY_ERROR
,
347 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
351 )} while the charging station is in pending state on the central server`,
357 chargingStation
.isRegistered() === true ||
358 (chargingStation
.getOcppStrictCompliance() === false &&
359 chargingStation
.inUnknownState() === true)
362 this.incomingRequestHandlers
.has(commandName
) === true &&
363 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
366 this.validatePayload(chargingStation
, commandName
, commandPayload
);
367 // Call the method to build the response
368 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
375 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
376 Handle incoming request error:`,
384 ErrorType
.NOT_IMPLEMENTED
,
385 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
396 ErrorType
.SECURITY_ERROR
,
397 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
401 )} while the charging station is not registered on the central server.`,
406 // Send the built response
407 await chargingStation
.ocppRequestService
.sendResponse(
415 private validatePayload(
416 chargingStation
: ChargingStation
,
417 commandName
: OCPP16IncomingRequestCommand
,
418 commandPayload
: JsonType
,
420 if (this.jsonSchemas
.has(commandName
) === true) {
421 return this.validateIncomingRequestPayload(
424 this.jsonSchemas
.get(commandName
)!,
429 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
430 for command '${commandName}' PDU validation`,
435 // Simulate charging station restart
436 private handleRequestReset(
437 chargingStation
: ChargingStation
,
438 commandPayload
: ResetRequest
,
440 this.runInAsyncScope(
441 chargingStation
.reset
.bind(chargingStation
) as (
442 this: ChargingStation
,
446 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
,
447 ).catch(Constants
.EMPTY_FUNCTION
);
449 `${chargingStation.logPrefix()} ${
451 } reset command received, simulating it. The station will be
452 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`,
454 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
457 private async handleRequestUnlockConnector(
458 chargingStation
: ChargingStation
,
459 commandPayload
: UnlockConnectorRequest
,
460 ): Promise
<UnlockConnectorResponse
> {
461 const connectorId
= commandPayload
.connectorId
;
462 if (chargingStation
.hasConnector(connectorId
) === false) {
464 `${chargingStation.logPrefix()} Trying to unlock a non existing
465 connector id ${connectorId.toString()}`,
467 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
469 if (connectorId
=== 0) {
471 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
473 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
475 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
476 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
478 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
480 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
481 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
483 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
485 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
488 OCPP16ChargePointStatus
.Available
,
490 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
493 private handleRequestGetConfiguration(
494 chargingStation
: ChargingStation
,
495 commandPayload
: GetConfigurationRequest
,
496 ): GetConfigurationResponse
{
497 const configurationKey
: OCPPConfigurationKey
[] = [];
498 const unknownKey
: string[] = [];
499 if (isUndefined(commandPayload
.key
) === true) {
500 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
501 if (isUndefined(configuration
.visible
) === true) {
502 configuration
.visible
= true;
504 if (configuration
.visible
=== false) {
507 configurationKey
.push({
508 key
: configuration
.key
,
509 readonly: configuration
.readonly,
510 value
: configuration
.value
,
513 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
514 for (const key
of commandPayload
.key
!) {
515 const keyFound
= getConfigurationKey(chargingStation
, key
, true);
517 if (isUndefined(keyFound
.visible
) === true) {
518 keyFound
.visible
= true;
520 if (keyFound
.visible
=== false) {
523 configurationKey
.push({
525 readonly: keyFound
.readonly,
526 value
: keyFound
.value
,
529 unknownKey
.push(key
);
539 private handleRequestChangeConfiguration(
540 chargingStation
: ChargingStation
,
541 commandPayload
: ChangeConfigurationRequest
,
542 ): ChangeConfigurationResponse
{
543 const keyToChange
= getConfigurationKey(chargingStation
, commandPayload
.key
, true);
544 if (keyToChange
?.readonly === true) {
545 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
546 } else if (keyToChange
?.readonly === false) {
547 let valueChanged
= false;
548 if (keyToChange
.value
!== commandPayload
.value
) {
549 setConfigurationKeyValue(chargingStation
, commandPayload
.key
, commandPayload
.value
, true);
552 let triggerHeartbeatRestart
= false;
554 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
555 OCPP16StandardParametersKey
.HeartBeatInterval
&&
558 setConfigurationKeyValue(
560 OCPP16StandardParametersKey
.HeartbeatInterval
,
561 commandPayload
.value
,
563 triggerHeartbeatRestart
= true;
566 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
567 OCPP16StandardParametersKey
.HeartbeatInterval
&&
570 setConfigurationKeyValue(
572 OCPP16StandardParametersKey
.HeartBeatInterval
,
573 commandPayload
.value
,
575 triggerHeartbeatRestart
= true;
577 if (triggerHeartbeatRestart
) {
578 chargingStation
.restartHeartbeat();
581 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
582 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
585 chargingStation
.restartWebSocketPing();
587 if (keyToChange
.reboot
) {
588 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
590 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
592 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
595 private handleRequestSetChargingProfile(
596 chargingStation
: ChargingStation
,
597 commandPayload
: SetChargingProfileRequest
,
598 ): SetChargingProfileResponse
{
600 OCPP16ServiceUtils
.checkFeatureProfile(
602 OCPP16SupportedFeatureProfiles
.SmartCharging
,
603 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
606 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
608 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
610 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
611 non existing connector id ${commandPayload.connectorId}`,
613 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
616 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
617 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
618 commandPayload
.connectorId
!== 0
620 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
623 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
624 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
625 (commandPayload
.connectorId
=== 0 ||
626 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
630 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
631 on connector ${commandPayload.connectorId} without a started transaction`,
633 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
635 OCPP16ServiceUtils
.setChargingProfile(
637 commandPayload
.connectorId
,
638 commandPayload
.csChargingProfiles
,
641 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
642 commandPayload.connectorId
644 commandPayload
.csChargingProfiles
,
646 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
649 private handleRequestGetCompositeSchedule(
650 chargingStation
: ChargingStation
,
651 commandPayload
: OCPP16GetCompositeScheduleRequest
,
652 ): OCPP16GetCompositeScheduleResponse
{
654 OCPP16ServiceUtils
.checkFeatureProfile(
656 OCPP16SupportedFeatureProfiles
.SmartCharging
,
657 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
660 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
662 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
664 `${chargingStation.logPrefix()} Trying to get composite schedule to a
665 non existing connector id ${commandPayload.connectorId}`,
667 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
670 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
672 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
674 const startDate
= new Date();
675 const endDate
= new Date(startDate
.getTime() + secondsToMilliseconds(commandPayload
.duration
));
676 let compositeSchedule
: OCPP16ChargingSchedule
| undefined;
677 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)!
678 .chargingProfiles
!) {
679 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
681 isWithinInterval(chargingProfile
.chargingSchedule
.startSchedule
!, {
686 compositeSchedule
= chargingProfile
.chargingSchedule
;
691 status: GenericStatus
.Accepted
,
692 scheduleStart
: compositeSchedule
?.startSchedule
,
693 connectorId
: commandPayload
.connectorId
,
694 chargingSchedule
: compositeSchedule
,
698 private handleRequestClearChargingProfile(
699 chargingStation
: ChargingStation
,
700 commandPayload
: ClearChargingProfileRequest
,
701 ): ClearChargingProfileResponse
{
703 OCPP16ServiceUtils
.checkFeatureProfile(
705 OCPP16SupportedFeatureProfiles
.SmartCharging
,
706 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
709 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
711 if (chargingStation
.hasConnector(commandPayload
.connectorId
!) === false) {
713 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
714 a non existing connector id ${commandPayload.connectorId}`,
716 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
719 !isNullOrUndefined(commandPayload
.connectorId
) &&
721 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.chargingProfiles
,
724 chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)!.chargingProfiles
= [];
726 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
727 commandPayload.connectorId
730 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
732 if (isNullOrUndefined(commandPayload
.connectorId
)) {
733 let clearedCP
= false;
734 if (chargingStation
.hasEvses
) {
735 for (const evseStatus
of chargingStation
.evses
.values()) {
736 for (const connectorStatus
of evseStatus
.connectors
.values()) {
737 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
740 connectorStatus
.chargingProfiles
,
745 for (const connectorId
of chargingStation
.connectors
.keys()) {
746 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
749 chargingStation
.getConnectorStatus(connectorId
)?.chargingProfiles
,
754 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
757 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
760 private async handleRequestChangeAvailability(
761 chargingStation
: ChargingStation
,
762 commandPayload
: OCPP16ChangeAvailabilityRequest
,
763 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
764 const connectorId
: number = commandPayload
.connectorId
;
765 if (chargingStation
.hasConnector(connectorId
) === false) {
767 `${chargingStation.logPrefix()} Trying to change the availability of a
768 non existing connector id ${connectorId.toString()}`,
770 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
772 const chargePointStatus
: OCPP16ChargePointStatus
=
773 commandPayload
.type === OCPP16AvailabilityType
.Operative
774 ? OCPP16ChargePointStatus
.Available
775 : OCPP16ChargePointStatus
.Unavailable
;
776 if (connectorId
=== 0) {
777 let response
: OCPP16ChangeAvailabilityResponse
;
778 if (chargingStation
.hasEvses
) {
779 for (const evseStatus
of chargingStation
.evses
.values()) {
780 response
= await OCPP16ServiceUtils
.changeAvailability(
782 [...evseStatus
.connectors
.keys()],
788 response
= await OCPP16ServiceUtils
.changeAvailability(
790 [...chargingStation
.connectors
.keys()],
798 (chargingStation
.isChargingStationAvailable() === true ||
799 (chargingStation
.isChargingStationAvailable() === false &&
800 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
802 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
803 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
804 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
806 chargingStation
.getConnectorStatus(connectorId
)!.availability
= commandPayload
.type;
807 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
812 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
814 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
817 private async handleRequestRemoteStartTransaction(
818 chargingStation
: ChargingStation
,
819 commandPayload
: RemoteStartTransactionRequest
,
820 ): Promise
<GenericResponse
> {
821 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
822 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
823 return this.notifyRemoteStartTransactionRejected(
825 transactionConnectorId
,
830 !chargingStation
.isChargingStationAvailable() ||
831 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
833 return this.notifyRemoteStartTransactionRejected(
835 transactionConnectorId
,
839 const remoteStartTransactionLogMsg
= `
840 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
841 chargingStation.stationInfo.chargingStationId
842 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
843 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
845 transactionConnectorId
,
846 OCPP16ChargePointStatus
.Preparing
,
848 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
849 // Authorization check required
851 chargingStation
.getAuthorizeRemoteTxRequests() === true &&
852 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
854 // Authorization successful, start transaction
856 this.setRemoteStartTransactionChargingProfile(
858 transactionConnectorId
,
862 connectorStatus
.transactionRemoteStarted
= true;
865 await chargingStation
.ocppRequestService
.requestHandler
<
866 OCPP16StartTransactionRequest
,
867 OCPP16StartTransactionResponse
868 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
869 connectorId
: transactionConnectorId
,
872 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
874 logger
.debug(remoteStartTransactionLogMsg
);
875 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
877 return this.notifyRemoteStartTransactionRejected(
879 transactionConnectorId
,
883 return this.notifyRemoteStartTransactionRejected(
885 transactionConnectorId
,
889 // No authorization check required, start transaction
891 this.setRemoteStartTransactionChargingProfile(
893 transactionConnectorId
,
897 connectorStatus
.transactionRemoteStarted
= true;
900 await chargingStation
.ocppRequestService
.requestHandler
<
901 OCPP16StartTransactionRequest
,
902 OCPP16StartTransactionResponse
903 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
904 connectorId
: transactionConnectorId
,
907 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
909 logger
.debug(remoteStartTransactionLogMsg
);
910 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
912 return this.notifyRemoteStartTransactionRejected(
914 transactionConnectorId
,
918 return this.notifyRemoteStartTransactionRejected(
920 transactionConnectorId
,
925 private async notifyRemoteStartTransactionRejected(
926 chargingStation
: ChargingStation
,
929 ): Promise
<GenericResponse
> {
931 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
933 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
936 OCPP16ChargePointStatus
.Available
,
940 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
941 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
943 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
945 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
948 private setRemoteStartTransactionChargingProfile(
949 chargingStation
: ChargingStation
,
951 chargingProfile
: OCPP16ChargingProfile
,
955 chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
957 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
);
959 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
960 on connector id ${connectorId}: %j`,
966 chargingProfile
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
969 `${chargingStation.logPrefix()} Not allowed to set ${
970 chargingProfile.chargingProfilePurpose
971 } charging profile(s) at remote start transaction`,
978 private async handleRequestRemoteStopTransaction(
979 chargingStation
: ChargingStation
,
980 commandPayload
: RemoteStopTransactionRequest
,
981 ): Promise
<GenericResponse
> {
982 const transactionId
= commandPayload
.transactionId
;
983 if (chargingStation
.hasEvses
) {
984 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
986 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
987 if (connectorStatus
.transactionId
=== transactionId
) {
988 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
994 for (const connectorId
of chargingStation
.connectors
.keys()) {
997 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
999 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1004 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id
1005 ${transactionId.toString()}`,
1007 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1010 private handleRequestUpdateFirmware(
1011 chargingStation
: ChargingStation
,
1012 commandPayload
: OCPP16UpdateFirmwareRequest
,
1013 ): OCPP16UpdateFirmwareResponse
{
1015 OCPP16ServiceUtils
.checkFeatureProfile(
1017 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1018 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1022 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1023 Cannot simulate firmware update: feature profile not supported`,
1025 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1028 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1029 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1032 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1033 Cannot simulate firmware update: firmware update is already in progress`,
1035 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1037 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
)!;
1038 const now
= Date.now();
1039 if (retrieveDate
?.getTime() <= now
) {
1040 this.runInAsyncScope(
1041 this.updateFirmwareSimulation
.bind(this) as (
1042 this: OCPP16IncomingRequestService
,
1047 ).catch(Constants
.EMPTY_FUNCTION
);
1051 this.runInAsyncScope(
1052 this.updateFirmwareSimulation
.bind(this) as (
1053 this: OCPP16IncomingRequestService
,
1058 ).catch(Constants
.EMPTY_FUNCTION
);
1060 retrieveDate
?.getTime() - now
,
1063 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1066 private async updateFirmwareSimulation(
1067 chargingStation
: ChargingStation
,
1071 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1074 if (chargingStation
.hasEvses
) {
1075 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1077 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1078 if (connectorStatus
?.transactionStarted
=== false) {
1079 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1082 OCPP16ChargePointStatus
.Unavailable
,
1089 for (const connectorId
of chargingStation
.connectors
.keys()) {
1092 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1094 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1097 OCPP16ChargePointStatus
.Unavailable
,
1102 await chargingStation
.ocppRequestService
.requestHandler
<
1103 OCPP16FirmwareStatusNotificationRequest
,
1104 OCPP16FirmwareStatusNotificationResponse
1105 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1106 status: OCPP16FirmwareStatus
.Downloading
,
1108 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1110 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1111 OCPP16FirmwareStatus
.DownloadFailed
1113 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1114 await chargingStation
.ocppRequestService
.requestHandler
<
1115 OCPP16FirmwareStatusNotificationRequest
,
1116 OCPP16FirmwareStatusNotificationResponse
1117 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1118 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1120 chargingStation
.stationInfo
.firmwareStatus
=
1121 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1124 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1125 await chargingStation
.ocppRequestService
.requestHandler
<
1126 OCPP16FirmwareStatusNotificationRequest
,
1127 OCPP16FirmwareStatusNotificationResponse
1128 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1129 status: OCPP16FirmwareStatus
.Downloaded
,
1131 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1132 let wasTransactionsStarted
= false;
1133 let transactionsStarted
: boolean;
1135 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1136 if (runningTransactions
> 0) {
1137 const waitTime
= secondsToMilliseconds(15);
1139 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1140 ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1142 )} before continuing firmware update simulation`,
1144 await sleep(waitTime
);
1145 transactionsStarted
= true;
1146 wasTransactionsStarted
= true;
1148 if (chargingStation
.hasEvses
) {
1149 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1151 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1152 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1153 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1156 OCPP16ChargePointStatus
.Unavailable
,
1163 for (const connectorId
of chargingStation
.connectors
.keys()) {
1166 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1167 OCPP16ChargePointStatus
.Unavailable
1169 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1172 OCPP16ChargePointStatus
.Unavailable
,
1177 transactionsStarted
= false;
1179 } while (transactionsStarted
);
1180 !wasTransactionsStarted
&&
1181 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))));
1182 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1185 await chargingStation
.ocppRequestService
.requestHandler
<
1186 OCPP16FirmwareStatusNotificationRequest
,
1187 OCPP16FirmwareStatusNotificationResponse
1188 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1189 status: OCPP16FirmwareStatus
.Installing
,
1191 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1193 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1194 OCPP16FirmwareStatus
.InstallationFailed
1196 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1197 await chargingStation
.ocppRequestService
.requestHandler
<
1198 OCPP16FirmwareStatusNotificationRequest
,
1199 OCPP16FirmwareStatusNotificationResponse
1200 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1201 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1203 chargingStation
.stationInfo
.firmwareStatus
=
1204 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1207 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1208 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1209 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1213 private async handleRequestGetDiagnostics(
1214 chargingStation
: ChargingStation
,
1215 commandPayload
: GetDiagnosticsRequest
,
1216 ): Promise
<GetDiagnosticsResponse
> {
1218 OCPP16ServiceUtils
.checkFeatureProfile(
1220 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1221 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1225 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1226 Cannot get diagnostics: feature profile not supported`,
1228 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1230 const uri
= new URL(commandPayload
.location
);
1231 if (uri
.protocol
.startsWith('ftp:')) {
1232 let ftpClient
: Client
| undefined;
1234 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1235 .filter((file
) => file
.endsWith('.log'))
1236 .map((file
) => join('./', file
));
1237 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1238 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1239 ftpClient
= new Client();
1240 const accessResponse
= await ftpClient
.access({
1242 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1243 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1244 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1246 let uploadResponse
: FTPResponse
| undefined;
1247 if (accessResponse
.code
=== 220) {
1248 ftpClient
.trackProgress((info
) => {
1250 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1252 } bytes transferred from diagnostics archive ${info.name}`,
1254 chargingStation
.ocppRequestService
1256 OCPP16DiagnosticsStatusNotificationRequest
,
1257 OCPP16DiagnosticsStatusNotificationResponse
1258 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1259 status: OCPP16DiagnosticsStatus
.Uploading
,
1263 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1264 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1269 uploadResponse
= await ftpClient
.uploadFrom(
1270 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1271 `${uri.pathname}${diagnosticsArchive}`,
1273 if (uploadResponse
.code
=== 226) {
1274 await chargingStation
.ocppRequestService
.requestHandler
<
1275 OCPP16DiagnosticsStatusNotificationRequest
,
1276 OCPP16DiagnosticsStatusNotificationResponse
1277 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1278 status: OCPP16DiagnosticsStatus
.Uploaded
,
1283 return { fileName
: diagnosticsArchive
};
1285 throw new OCPPError(
1286 ErrorType
.GENERIC_ERROR
,
1287 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1288 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1290 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1293 throw new OCPPError(
1294 ErrorType
.GENERIC_ERROR
,
1295 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1296 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1298 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1301 await chargingStation
.ocppRequestService
.requestHandler
<
1302 OCPP16DiagnosticsStatusNotificationRequest
,
1303 OCPP16DiagnosticsStatusNotificationResponse
1304 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1305 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1310 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1312 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1314 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1319 `${chargingStation.logPrefix()} Unsupported protocol ${
1321 } to transfer the diagnostic logs archive`,
1323 await chargingStation
.ocppRequestService
.requestHandler
<
1324 OCPP16DiagnosticsStatusNotificationRequest
,
1325 OCPP16DiagnosticsStatusNotificationResponse
1326 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1327 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1329 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1333 private handleRequestTriggerMessage(
1334 chargingStation
: ChargingStation
,
1335 commandPayload
: OCPP16TriggerMessageRequest
,
1336 ): OCPP16TriggerMessageResponse
{
1338 !OCPP16ServiceUtils
.checkFeatureProfile(
1340 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1341 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1343 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1345 commandPayload
.requestedMessage
,
1348 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1351 !OCPP16ServiceUtils
.isConnectorIdValid(
1353 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1354 commandPayload
.connectorId
!,
1357 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1360 switch (commandPayload
.requestedMessage
) {
1361 case OCPP16MessageTrigger
.BootNotification
:
1363 chargingStation
.ocppRequestService
1364 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1366 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1367 chargingStation
.bootNotificationRequest
,
1368 { skipBufferingOnError
: true, triggerMessage
: true },
1370 .then((response
) => {
1371 chargingStation
.bootNotificationResponse
= response
;
1373 .catch(Constants
.EMPTY_FUNCTION
);
1374 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1375 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1376 case OCPP16MessageTrigger
.Heartbeat
:
1378 chargingStation
.ocppRequestService
1379 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1381 OCPP16RequestCommand
.HEARTBEAT
,
1384 triggerMessage
: true,
1387 .catch(Constants
.EMPTY_FUNCTION
);
1388 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1389 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1390 case OCPP16MessageTrigger
.StatusNotification
:
1392 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1393 chargingStation
.ocppRequestService
1394 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1396 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1398 connectorId
: commandPayload
.connectorId
,
1399 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1400 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
!)?.status,
1403 triggerMessage
: true,
1406 .catch(Constants
.EMPTY_FUNCTION
);
1408 // eslint-disable-next-line no-lonely-if
1409 if (chargingStation
.hasEvses
) {
1410 for (const evseStatus
of chargingStation
.evses
.values()) {
1411 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1412 chargingStation
.ocppRequestService
1414 OCPP16StatusNotificationRequest
,
1415 OCPP16StatusNotificationResponse
1418 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1421 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1422 status: connectorStatus
.status,
1425 triggerMessage
: true,
1428 .catch(Constants
.EMPTY_FUNCTION
);
1432 for (const connectorId
of chargingStation
.connectors
.keys()) {
1433 chargingStation
.ocppRequestService
1435 OCPP16StatusNotificationRequest
,
1436 OCPP16StatusNotificationResponse
1439 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1442 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1443 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1446 triggerMessage
: true,
1449 .catch(Constants
.EMPTY_FUNCTION
);
1453 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1454 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1456 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1459 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1461 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1463 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1468 private handleRequestDataTransfer(
1469 chargingStation
: ChargingStation
,
1470 commandPayload
: OCPP16DataTransferRequest
,
1471 ): OCPP16DataTransferResponse
{
1473 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1474 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1476 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1478 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1480 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1482 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1487 private async handleRequestReserveNow(
1488 chargingStation
: ChargingStation
,
1489 commandPayload
: OCPP16ReserveNowRequest
,
1490 ): Promise
<OCPP16ReserveNowResponse
> {
1492 !OCPP16ServiceUtils
.checkFeatureProfile(
1494 OCPP16SupportedFeatureProfiles
.Reservation
,
1495 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1498 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1500 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1501 let response
: OCPP16ReserveNowResponse
;
1503 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1504 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1506 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1507 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1509 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1510 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1512 await removeExpiredReservations(chargingStation
);
1513 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1514 case OCPP16ChargePointStatus
.Faulted
:
1515 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1517 case OCPP16ChargePointStatus
.Preparing
:
1518 case OCPP16ChargePointStatus
.Charging
:
1519 case OCPP16ChargePointStatus
.SuspendedEV
:
1520 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1521 case OCPP16ChargePointStatus
.Finishing
:
1522 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1524 case OCPP16ChargePointStatus
.Unavailable
:
1525 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1527 case OCPP16ChargePointStatus
.Reserved
:
1528 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1529 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1532 // eslint-disable-next-line no-fallthrough
1534 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1535 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1538 await chargingStation
.addReservation({
1539 id
: commandPayload
.reservationId
,
1542 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1547 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1548 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1550 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1552 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1557 private async handleRequestCancelReservation(
1558 chargingStation
: ChargingStation
,
1559 commandPayload
: OCPP16CancelReservationRequest
,
1560 ): Promise
<GenericResponse
> {
1562 !OCPP16ServiceUtils
.checkFeatureProfile(
1564 OCPP16SupportedFeatureProfiles
.Reservation
,
1565 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1568 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1571 const { reservationId
} = commandPayload
;
1572 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
);
1573 if (isUndefined(reservation
)) {
1575 `${chargingStation.logPrefix()} Reservation with id ${reservationId}
1576 does not exist on charging station`,
1578 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1580 await chargingStation
.removeReservation(
1582 ReservationTerminationReason
.RESERVATION_CANCELED
,
1584 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1586 return this.handleIncomingRequestError
<GenericResponse
>(
1588 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1590 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},