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';
15 secondsToMilliseconds
,
17 import { create
} from
'tar';
19 import { OCPP16Constants
} from
'./OCPP16Constants';
20 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
23 canProceedChargingProfile
,
26 prepareChargingProfileKind
,
27 removeExpiredReservations
,
28 setConfigurationKeyValue
,
29 } from
'../../../charging-station';
30 import { OCPPError
} from
'../../../exception';
32 type ChangeConfigurationRequest
,
33 type ChangeConfigurationResponse
,
35 type ClearChargingProfileRequest
,
36 type ClearChargingProfileResponse
,
40 type GetConfigurationRequest
,
41 type GetConfigurationResponse
,
42 type GetDiagnosticsRequest
,
43 type GetDiagnosticsResponse
,
44 type IncomingRequestHandler
,
47 OCPP16AuthorizationStatus
,
48 OCPP16AvailabilityType
,
49 type OCPP16BootNotificationRequest
,
50 type OCPP16BootNotificationResponse
,
51 type OCPP16CancelReservationRequest
,
52 type OCPP16ChangeAvailabilityRequest
,
53 type OCPP16ChangeAvailabilityResponse
,
54 OCPP16ChargePointErrorCode
,
55 OCPP16ChargePointStatus
,
56 type OCPP16ChargingProfile
,
57 OCPP16ChargingProfilePurposeType
,
58 type OCPP16ChargingSchedule
,
59 type OCPP16ClearCacheRequest
,
60 type OCPP16DataTransferRequest
,
61 type OCPP16DataTransferResponse
,
62 OCPP16DataTransferVendorId
,
63 OCPP16DiagnosticsStatus
,
64 type OCPP16DiagnosticsStatusNotificationRequest
,
65 type OCPP16DiagnosticsStatusNotificationResponse
,
67 type OCPP16FirmwareStatusNotificationRequest
,
68 type OCPP16FirmwareStatusNotificationResponse
,
69 type OCPP16GetCompositeScheduleRequest
,
70 type OCPP16GetCompositeScheduleResponse
,
71 type OCPP16HeartbeatRequest
,
72 type OCPP16HeartbeatResponse
,
73 OCPP16IncomingRequestCommand
,
76 type OCPP16ReserveNowRequest
,
77 type OCPP16ReserveNowResponse
,
78 OCPP16StandardParametersKey
,
79 type OCPP16StartTransactionRequest
,
80 type OCPP16StartTransactionResponse
,
81 type OCPP16StatusNotificationRequest
,
82 type OCPP16StatusNotificationResponse
,
83 OCPP16StopTransactionReason
,
84 OCPP16SupportedFeatureProfiles
,
85 type OCPP16TriggerMessageRequest
,
86 type OCPP16TriggerMessageResponse
,
87 type OCPP16UpdateFirmwareRequest
,
88 type OCPP16UpdateFirmwareResponse
,
89 type OCPPConfigurationKey
,
91 type RemoteStartTransactionRequest
,
92 type RemoteStopTransactionRequest
,
93 ReservationTerminationReason
,
95 type SetChargingProfileRequest
,
96 type SetChargingProfileResponse
,
97 type UnlockConnectorRequest
,
98 type UnlockConnectorResponse
,
99 } from
'../../../types';
105 formatDurationMilliSeconds
,
115 } from
'../../../utils';
116 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
118 const moduleName
= 'OCPP16IncomingRequestService';
120 export class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
121 protected jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
122 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
124 public constructor() {
125 // if (new.target?.name === moduleName) {
126 // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
128 super(OCPPVersion
.VERSION_16
);
129 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
131 OCPP16IncomingRequestCommand
.RESET
,
132 this.handleRequestReset
.bind(this) as unknown
as IncomingRequestHandler
,
135 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
136 this.handleRequestClearCache
.bind(this) as IncomingRequestHandler
,
139 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
140 this.handleRequestUnlockConnector
.bind(this) as unknown
as IncomingRequestHandler
,
143 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
144 this.handleRequestGetConfiguration
.bind(this) as IncomingRequestHandler
,
147 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
148 this.handleRequestChangeConfiguration
.bind(this) as unknown
as IncomingRequestHandler
,
151 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
152 this.handleRequestGetCompositeSchedule
.bind(this) as unknown
as IncomingRequestHandler
,
155 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
156 this.handleRequestSetChargingProfile
.bind(this) as unknown
as IncomingRequestHandler
,
159 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
160 this.handleRequestClearChargingProfile
.bind(this) as IncomingRequestHandler
,
163 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
164 this.handleRequestChangeAvailability
.bind(this) as unknown
as IncomingRequestHandler
,
167 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
168 this.handleRequestRemoteStartTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
171 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
172 this.handleRequestRemoteStopTransaction
.bind(this) as unknown
as IncomingRequestHandler
,
175 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
176 this.handleRequestGetDiagnostics
.bind(this) as IncomingRequestHandler
,
179 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
180 this.handleRequestTriggerMessage
.bind(this) as unknown
as IncomingRequestHandler
,
183 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
184 this.handleRequestDataTransfer
.bind(this) as unknown
as IncomingRequestHandler
,
187 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
188 this.handleRequestUpdateFirmware
.bind(this) as unknown
as IncomingRequestHandler
,
191 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
192 this.handleRequestReserveNow
.bind(this) as unknown
as IncomingRequestHandler
,
195 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
196 this.handleRequestCancelReservation
.bind(this) as unknown
as IncomingRequestHandler
,
199 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
201 OCPP16IncomingRequestCommand
.RESET
,
202 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
203 'assets/json-schemas/ocpp/1.6/Reset.json',
209 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
210 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
211 'assets/json-schemas/ocpp/1.6/ClearCache.json',
217 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
218 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
219 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
225 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
226 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
227 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
233 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
234 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
235 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
241 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
242 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
243 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
249 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
250 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
251 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
257 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
258 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
259 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
265 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
266 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
267 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
273 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
274 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ChangeAvailabilityRequest
>(
275 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
281 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
282 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
283 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
289 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
290 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
291 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
297 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
298 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
299 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
305 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
306 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
307 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
313 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
314 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
315 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
321 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
322 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
323 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
329 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
330 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
331 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
337 this.validatePayload
= this.validatePayload
.bind(this) as (
338 chargingStation
: ChargingStation
,
339 commandName
: OCPP16IncomingRequestCommand
,
340 commandPayload
: JsonType
,
344 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
345 chargingStation
: ChargingStation
,
347 commandName
: OCPP16IncomingRequestCommand
,
348 commandPayload
: ReqType
,
350 let response
: ResType
;
352 chargingStation
.getOcppStrictCompliance() === true &&
353 chargingStation
.inPendingState() === true &&
354 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
355 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
358 ErrorType
.SECURITY_ERROR
,
359 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
363 )} while the charging station is in pending state on the central server`,
369 chargingStation
.isRegistered() === true ||
370 (chargingStation
.getOcppStrictCompliance() === false &&
371 chargingStation
.inUnknownState() === true)
374 this.incomingRequestHandlers
.has(commandName
) === true &&
375 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
378 this.validatePayload(chargingStation
, commandName
, commandPayload
);
379 // Call the method to build the response
380 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
387 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
388 Handle incoming request error:`,
396 ErrorType
.NOT_IMPLEMENTED
,
397 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
408 ErrorType
.SECURITY_ERROR
,
409 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
413 )} while the charging station is not registered on the central server.`,
418 // Send the built response
419 await chargingStation
.ocppRequestService
.sendResponse(
427 private validatePayload(
428 chargingStation
: ChargingStation
,
429 commandName
: OCPP16IncomingRequestCommand
,
430 commandPayload
: JsonType
,
432 if (this.jsonSchemas
.has(commandName
) === true) {
433 return this.validateIncomingRequestPayload(
436 this.jsonSchemas
.get(commandName
)!,
441 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
442 for command '${commandName}' PDU validation`,
447 // Simulate charging station restart
448 private handleRequestReset(
449 chargingStation
: ChargingStation
,
450 commandPayload
: ResetRequest
,
452 const { type } = commandPayload
;
453 this.runInAsyncScope(
454 chargingStation
.reset
.bind(chargingStation
) as (
455 this: ChargingStation
,
459 `${type}Reset` as OCPP16StopTransactionReason
,
460 ).catch(Constants
.EMPTY_FUNCTION
);
462 `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be
463 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime!)}`,
465 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
468 private async handleRequestUnlockConnector(
469 chargingStation
: ChargingStation
,
470 commandPayload
: UnlockConnectorRequest
,
471 ): Promise
<UnlockConnectorResponse
> {
472 const { connectorId
} = commandPayload
;
473 if (chargingStation
.hasConnector(connectorId
) === false) {
475 `${chargingStation.logPrefix()} Trying to unlock a non existing
476 connector id ${connectorId.toString()}`,
478 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
480 if (connectorId
=== 0) {
482 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`,
484 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
486 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
487 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
489 OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
491 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
492 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
494 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
496 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
499 OCPP16ChargePointStatus
.Available
,
501 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
504 private handleRequestGetConfiguration(
505 chargingStation
: ChargingStation
,
506 commandPayload
: GetConfigurationRequest
,
507 ): GetConfigurationResponse
{
508 const { key
} = commandPayload
;
509 const configurationKey
: OCPPConfigurationKey
[] = [];
510 const unknownKey
: string[] = [];
511 if (isUndefined(key
) === true) {
512 for (const configuration
of chargingStation
.ocppConfiguration
!.configurationKey
!) {
513 if (isUndefined(configuration
.visible
) === true) {
514 configuration
.visible
= true;
516 if (configuration
.visible
=== false) {
519 configurationKey
.push({
520 key
: configuration
.key
,
521 readonly: configuration
.readonly,
522 value
: configuration
.value
,
525 } else if (isNotEmptyArray(key
) === true) {
526 for (const k
of key
!) {
527 const keyFound
= getConfigurationKey(chargingStation
, k
, true);
529 if (isUndefined(keyFound
.visible
) === true) {
530 keyFound
.visible
= true;
532 if (keyFound
.visible
=== false) {
535 configurationKey
.push({
537 readonly: keyFound
.readonly,
538 value
: keyFound
.value
,
551 private handleRequestChangeConfiguration(
552 chargingStation
: ChargingStation
,
553 commandPayload
: ChangeConfigurationRequest
,
554 ): ChangeConfigurationResponse
{
555 const { key
, value
} = commandPayload
;
556 const keyToChange
= getConfigurationKey(chargingStation
, key
, true);
557 if (keyToChange
?.readonly === true) {
558 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
559 } else if (keyToChange
?.readonly === false) {
560 let valueChanged
= false;
561 if (keyToChange
.value
!== value
) {
562 setConfigurationKeyValue(chargingStation
, key
, value
, true);
565 let triggerHeartbeatRestart
= false;
567 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
568 OCPP16StandardParametersKey
.HeartBeatInterval
&&
571 setConfigurationKeyValue(
573 OCPP16StandardParametersKey
.HeartbeatInterval
,
576 triggerHeartbeatRestart
= true;
579 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
580 OCPP16StandardParametersKey
.HeartbeatInterval
&&
583 setConfigurationKeyValue(
585 OCPP16StandardParametersKey
.HeartBeatInterval
,
588 triggerHeartbeatRestart
= true;
590 if (triggerHeartbeatRestart
) {
591 chargingStation
.restartHeartbeat();
594 (keyToChange
.key
as OCPP16StandardParametersKey
) ===
595 OCPP16StandardParametersKey
.WebSocketPingInterval
&&
598 chargingStation
.restartWebSocketPing();
600 if (keyToChange
.reboot
) {
601 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
603 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
605 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
608 private handleRequestSetChargingProfile(
609 chargingStation
: ChargingStation
,
610 commandPayload
: SetChargingProfileRequest
,
611 ): SetChargingProfileResponse
{
613 OCPP16ServiceUtils
.checkFeatureProfile(
615 OCPP16SupportedFeatureProfiles
.SmartCharging
,
616 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
619 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
621 const { connectorId
, csChargingProfiles
} = commandPayload
;
622 if (chargingStation
.hasConnector(connectorId
) === false) {
624 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
625 non existing connector id ${connectorId}`,
627 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
630 csChargingProfiles
.chargingProfilePurpose
===
631 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
634 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
637 csChargingProfiles
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
638 (connectorId
=== 0 ||
639 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false)
642 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
643 on connector ${connectorId} without a started transaction`,
645 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
647 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, csChargingProfiles
);
649 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,
652 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
655 private handleRequestGetCompositeSchedule(
656 chargingStation
: ChargingStation
,
657 commandPayload
: OCPP16GetCompositeScheduleRequest
,
658 ): OCPP16GetCompositeScheduleResponse
{
660 OCPP16ServiceUtils
.checkFeatureProfile(
662 OCPP16SupportedFeatureProfiles
.SmartCharging
,
663 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
666 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
668 const { connectorId
, duration
, chargingRateUnit
} = commandPayload
;
669 if (chargingStation
.hasConnector(connectorId
) === false) {
671 `${chargingStation.logPrefix()} Trying to get composite schedule to a
672 non existing connector id ${connectorId}`,
674 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
676 if (connectorId
=== 0) {
678 `${chargingStation.logPrefix()} Get composite schedule on connector id ${connectorId} is not yet supported`,
680 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
682 if (chargingRateUnit
) {
684 `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported`,
686 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
688 const connectorStatus
= chargingStation
.getConnectorStatus(connectorId
)!;
691 connectorStatus
?.chargingProfiles
&&
692 isEmptyArray(chargingStation
.getConnectorStatus(0)?.chargingProfiles
),
695 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
697 const currentDate
= new Date();
698 const interval
: Interval
= {
700 end
: addSeconds(currentDate
, duration
),
702 const chargingProfiles
: OCPP16ChargingProfile
[] = [];
703 for (const chargingProfile
of cloneObject
<OCPP16ChargingProfile
[]>(
704 (connectorStatus
?.chargingProfiles
?? []).concat(
705 chargingStation
.getConnectorStatus(0)?.chargingProfiles
?? [],
707 ).sort((a
, b
) => b
.stackLevel
- a
.stackLevel
)) {
709 connectorStatus
?.transactionStarted
&&
710 isNullOrUndefined(chargingProfile
.chargingSchedule
?.startSchedule
)
713 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
714 chargingProfile.chargingProfileId
715 } has no startSchedule defined. Trying to set it to the connector current transaction start date`,
717 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
718 chargingProfile
.chargingSchedule
.startSchedule
= connectorStatus
?.transactionStart
;
720 if (!isDate(chargingProfile
.chargingSchedule
?.startSchedule
)) {
722 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
723 chargingProfile.chargingProfileId
724 } startSchedule property is not a Date object. Trying to convert it to a Date object`,
726 chargingProfile
.chargingSchedule
.startSchedule
= convertToDate(
727 chargingProfile
.chargingSchedule
?.startSchedule
,
731 !prepareChargingProfileKind(
734 interval
.start
as Date,
735 chargingStation
.logPrefix(),
741 !canProceedChargingProfile(
743 interval
.start
as Date,
744 chargingStation
.logPrefix(),
749 // Add active charging profiles into chargingProfiles array
751 isValidTime(chargingProfile
.chargingSchedule
?.startSchedule
) &&
752 isWithinInterval(chargingProfile
.chargingSchedule
.startSchedule
!, interval
)
754 chargingProfiles
.push(chargingProfile
);
757 const compositeSchedule
: OCPP16ChargingSchedule
= {
759 chargingProfiles
.map(
760 (chargingProfile
) => chargingProfile
.chargingSchedule
.startSchedule
?? maxTime
,
764 ...chargingProfiles
.map(
765 (chargingProfile
) => chargingProfile
.chargingSchedule
.duration
?? -Infinity,
768 chargingRateUnit
: chargingProfiles
.every(
770 chargingProfile
.chargingSchedule
.chargingRateUnit
=== ChargingRateUnitType
.AMPERE
,
772 ? ChargingRateUnitType
.AMPERE
773 : chargingProfiles
.every(
775 chargingProfile
.chargingSchedule
.chargingRateUnit
=== ChargingRateUnitType
.WATT
,
777 ? ChargingRateUnitType
.WATT
778 : ChargingRateUnitType
.AMPERE
,
779 // FIXME: remove overlapping charging schedule periods
780 chargingSchedulePeriod
: chargingProfiles
781 .map((chargingProfile
) => chargingProfile
.chargingSchedule
.chargingSchedulePeriod
)
783 (accumulator
, value
) =>
784 accumulator
.concat(value
).sort((a
, b
) => a
.startPeriod
- b
.startPeriod
),
787 minChargeRate
: Math.min(
788 ...chargingProfiles
.map(
789 (chargingProfile
) => chargingProfile
.chargingSchedule
.minChargeRate
?? Infinity,
794 status: GenericStatus
.Accepted
,
795 scheduleStart
: compositeSchedule
.startSchedule
!,
797 chargingSchedule
: compositeSchedule
,
801 private handleRequestClearChargingProfile(
802 chargingStation
: ChargingStation
,
803 commandPayload
: ClearChargingProfileRequest
,
804 ): ClearChargingProfileResponse
{
806 OCPP16ServiceUtils
.checkFeatureProfile(
808 OCPP16SupportedFeatureProfiles
.SmartCharging
,
809 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
812 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
814 const { connectorId
} = commandPayload
;
815 if (chargingStation
.hasConnector(connectorId
!) === false) {
817 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
818 a non existing connector id ${connectorId}`,
820 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
823 !isNullOrUndefined(connectorId
) &&
824 isNotEmptyArray(chargingStation
.getConnectorStatus(connectorId
!)?.chargingProfiles
)
826 chargingStation
.getConnectorStatus(connectorId
!)!.chargingProfiles
= [];
828 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}`,
830 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
832 if (isNullOrUndefined(connectorId
)) {
833 let clearedCP
= false;
834 if (chargingStation
.hasEvses
) {
835 for (const evseStatus
of chargingStation
.evses
.values()) {
836 for (const connectorStatus
of evseStatus
.connectors
.values()) {
837 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
840 connectorStatus
.chargingProfiles
,
845 for (const id
of chargingStation
.connectors
.keys()) {
846 clearedCP
= OCPP16ServiceUtils
.clearChargingProfiles(
849 chargingStation
.getConnectorStatus(id
)?.chargingProfiles
,
854 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
857 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
860 private async handleRequestChangeAvailability(
861 chargingStation
: ChargingStation
,
862 commandPayload
: OCPP16ChangeAvailabilityRequest
,
863 ): Promise
<OCPP16ChangeAvailabilityResponse
> {
864 const { connectorId
, type } = commandPayload
;
865 if (chargingStation
.hasConnector(connectorId
) === false) {
867 `${chargingStation.logPrefix()} Trying to change the availability of a
868 non existing connector id ${connectorId.toString()}`,
870 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
872 const chargePointStatus
: OCPP16ChargePointStatus
=
873 type === OCPP16AvailabilityType
.Operative
874 ? OCPP16ChargePointStatus
.Available
875 : OCPP16ChargePointStatus
.Unavailable
;
876 if (connectorId
=== 0) {
877 let response
: OCPP16ChangeAvailabilityResponse
;
878 if (chargingStation
.hasEvses
) {
879 for (const evseStatus
of chargingStation
.evses
.values()) {
880 response
= await OCPP16ServiceUtils
.changeAvailability(
882 [...evseStatus
.connectors
.keys()],
888 response
= await OCPP16ServiceUtils
.changeAvailability(
890 [...chargingStation
.connectors
.keys()],
898 (chargingStation
.isChargingStationAvailable() === true ||
899 (chargingStation
.isChargingStationAvailable() === false &&
900 type === OCPP16AvailabilityType
.Inoperative
))
902 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
903 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type;
904 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
906 chargingStation
.getConnectorStatus(connectorId
)!.availability
= type;
907 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
912 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
914 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
917 private async handleRequestRemoteStartTransaction(
918 chargingStation
: ChargingStation
,
919 commandPayload
: RemoteStartTransactionRequest
,
920 ): Promise
<GenericResponse
> {
921 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
922 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
923 return this.notifyRemoteStartTransactionRejected(
925 transactionConnectorId
,
930 !chargingStation
.isChargingStationAvailable() ||
931 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
933 return this.notifyRemoteStartTransactionRejected(
935 transactionConnectorId
,
939 const remoteStartTransactionLogMsg
= `
940 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
941 chargingStation.stationInfo.chargingStationId
942 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
943 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
945 transactionConnectorId
,
946 OCPP16ChargePointStatus
.Preparing
,
948 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
)!;
949 // Authorization check required
951 chargingStation
.getAuthorizeRemoteTxRequests() === true &&
952 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
954 // Authorization successful, start transaction
956 this.setRemoteStartTransactionChargingProfile(
958 transactionConnectorId
,
962 connectorStatus
.transactionRemoteStarted
= true;
965 await chargingStation
.ocppRequestService
.requestHandler
<
966 OCPP16StartTransactionRequest
,
967 OCPP16StartTransactionResponse
968 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
969 connectorId
: transactionConnectorId
,
972 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
974 logger
.debug(remoteStartTransactionLogMsg
);
975 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
977 return this.notifyRemoteStartTransactionRejected(
979 transactionConnectorId
,
983 return this.notifyRemoteStartTransactionRejected(
985 transactionConnectorId
,
989 // No authorization check required, start transaction
991 this.setRemoteStartTransactionChargingProfile(
993 transactionConnectorId
,
997 connectorStatus
.transactionRemoteStarted
= true;
1000 await chargingStation
.ocppRequestService
.requestHandler
<
1001 OCPP16StartTransactionRequest
,
1002 OCPP16StartTransactionResponse
1003 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
1004 connectorId
: transactionConnectorId
,
1007 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
1009 logger
.debug(remoteStartTransactionLogMsg
);
1010 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1012 return this.notifyRemoteStartTransactionRejected(
1014 transactionConnectorId
,
1018 return this.notifyRemoteStartTransactionRejected(
1020 transactionConnectorId
,
1025 private async notifyRemoteStartTransactionRejected(
1026 chargingStation
: ChargingStation
,
1027 connectorId
: number,
1029 ): Promise
<GenericResponse
> {
1031 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
1033 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1036 OCPP16ChargePointStatus
.Available
,
1040 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
1041 ${connectorId.toString()}, idTag '${idTag}', availability '${chargingStation.getConnectorStatus(
1043 )?.availability}', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`,
1045 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1048 private setRemoteStartTransactionChargingProfile(
1049 chargingStation
: ChargingStation
,
1050 connectorId
: number,
1051 chargingProfile
: OCPP16ChargingProfile
,
1055 chargingProfile
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
1057 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, chargingProfile
);
1059 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
1060 on connector id ${connectorId}: %j`,
1066 chargingProfile
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
1069 `${chargingStation.logPrefix()} Not allowed to set ${
1070 chargingProfile.chargingProfilePurpose
1071 } charging profile(s) at remote start transaction`,
1078 private async handleRequestRemoteStopTransaction(
1079 chargingStation
: ChargingStation
,
1080 commandPayload
: RemoteStopTransactionRequest
,
1081 ): Promise
<GenericResponse
> {
1082 const { transactionId
} = commandPayload
;
1083 if (chargingStation
.hasEvses
) {
1084 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1086 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1087 if (connectorStatus
.transactionId
=== transactionId
) {
1088 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1094 for (const connectorId
of chargingStation
.connectors
.keys()) {
1097 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1099 return OCPP16ServiceUtils
.remoteStopTransaction(chargingStation
, connectorId
);
1104 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id
1105 ${transactionId.toString()}`,
1107 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1110 private handleRequestUpdateFirmware(
1111 chargingStation
: ChargingStation
,
1112 commandPayload
: OCPP16UpdateFirmwareRequest
,
1113 ): OCPP16UpdateFirmwareResponse
{
1115 OCPP16ServiceUtils
.checkFeatureProfile(
1117 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1118 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
1122 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1123 Cannot simulate firmware update: feature profile not supported`,
1125 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1127 let { retrieveDate
} = commandPayload
;
1129 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1130 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1133 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1134 Cannot simulate firmware update: firmware update is already in progress`,
1136 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1138 retrieveDate
= convertToDate(retrieveDate
)!;
1139 const now
= Date.now();
1140 if (retrieveDate
?.getTime() <= now
) {
1141 this.runInAsyncScope(
1142 this.updateFirmwareSimulation
.bind(this) as (
1143 this: OCPP16IncomingRequestService
,
1148 ).catch(Constants
.EMPTY_FUNCTION
);
1152 this.runInAsyncScope(
1153 this.updateFirmwareSimulation
.bind(this) as (
1154 this: OCPP16IncomingRequestService
,
1159 ).catch(Constants
.EMPTY_FUNCTION
);
1161 retrieveDate
?.getTime() - now
,
1164 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1167 private async updateFirmwareSimulation(
1168 chargingStation
: ChargingStation
,
1172 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1175 if (chargingStation
.hasEvses
) {
1176 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1178 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1179 if (connectorStatus
?.transactionStarted
=== false) {
1180 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1183 OCPP16ChargePointStatus
.Unavailable
,
1190 for (const connectorId
of chargingStation
.connectors
.keys()) {
1193 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1195 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1198 OCPP16ChargePointStatus
.Unavailable
,
1203 await chargingStation
.ocppRequestService
.requestHandler
<
1204 OCPP16FirmwareStatusNotificationRequest
,
1205 OCPP16FirmwareStatusNotificationResponse
1206 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1207 status: OCPP16FirmwareStatus
.Downloading
,
1209 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1211 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1212 OCPP16FirmwareStatus
.DownloadFailed
1214 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1215 await chargingStation
.ocppRequestService
.requestHandler
<
1216 OCPP16FirmwareStatusNotificationRequest
,
1217 OCPP16FirmwareStatusNotificationResponse
1218 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1219 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1221 chargingStation
.stationInfo
.firmwareStatus
=
1222 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1225 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1226 await chargingStation
.ocppRequestService
.requestHandler
<
1227 OCPP16FirmwareStatusNotificationRequest
,
1228 OCPP16FirmwareStatusNotificationResponse
1229 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1230 status: OCPP16FirmwareStatus
.Downloaded
,
1232 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1233 let wasTransactionsStarted
= false;
1234 let transactionsStarted
: boolean;
1236 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1237 if (runningTransactions
> 0) {
1238 const waitTime
= secondsToMilliseconds(15);
1240 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1241 ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
1243 )} before continuing firmware update simulation`,
1245 await sleep(waitTime
);
1246 transactionsStarted
= true;
1247 wasTransactionsStarted
= true;
1249 if (chargingStation
.hasEvses
) {
1250 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1252 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1253 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1254 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1257 OCPP16ChargePointStatus
.Unavailable
,
1264 for (const connectorId
of chargingStation
.connectors
.keys()) {
1267 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1268 OCPP16ChargePointStatus
.Unavailable
1270 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1273 OCPP16ChargePointStatus
.Unavailable
,
1278 transactionsStarted
= false;
1280 } while (transactionsStarted
);
1281 !wasTransactionsStarted
&&
1282 (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
))));
1283 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1286 await chargingStation
.ocppRequestService
.requestHandler
<
1287 OCPP16FirmwareStatusNotificationRequest
,
1288 OCPP16FirmwareStatusNotificationResponse
1289 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1290 status: OCPP16FirmwareStatus
.Installing
,
1292 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1294 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1295 OCPP16FirmwareStatus
.InstallationFailed
1297 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1298 await chargingStation
.ocppRequestService
.requestHandler
<
1299 OCPP16FirmwareStatusNotificationRequest
,
1300 OCPP16FirmwareStatusNotificationResponse
1301 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1302 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1304 chargingStation
.stationInfo
.firmwareStatus
=
1305 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1308 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1309 await sleep(secondsToMilliseconds(getRandomInteger(maxDelay
, minDelay
)));
1310 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1314 private async handleRequestGetDiagnostics(
1315 chargingStation
: ChargingStation
,
1316 commandPayload
: GetDiagnosticsRequest
,
1317 ): Promise
<GetDiagnosticsResponse
> {
1319 OCPP16ServiceUtils
.checkFeatureProfile(
1321 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1322 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1326 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1327 Cannot get diagnostics: feature profile not supported`,
1329 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1331 const { location
} = commandPayload
;
1332 const uri
= new URL(location
);
1333 if (uri
.protocol
.startsWith('ftp:')) {
1334 let ftpClient
: Client
| undefined;
1336 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1337 .filter((file
) => file
.endsWith('.log'))
1338 .map((file
) => join('./', file
));
1339 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1340 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1341 ftpClient
= new Client();
1342 const accessResponse
= await ftpClient
.access({
1344 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1345 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1346 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1348 let uploadResponse
: FTPResponse
| undefined;
1349 if (accessResponse
.code
=== 220) {
1350 ftpClient
.trackProgress((info
) => {
1352 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
1354 } bytes transferred from diagnostics archive ${info.name}`,
1356 chargingStation
.ocppRequestService
1358 OCPP16DiagnosticsStatusNotificationRequest
,
1359 OCPP16DiagnosticsStatusNotificationResponse
1360 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1361 status: OCPP16DiagnosticsStatus
.Uploading
,
1365 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1366 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1371 uploadResponse
= await ftpClient
.uploadFrom(
1372 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1373 `${uri.pathname}${diagnosticsArchive}`,
1375 if (uploadResponse
.code
=== 226) {
1376 await chargingStation
.ocppRequestService
.requestHandler
<
1377 OCPP16DiagnosticsStatusNotificationRequest
,
1378 OCPP16DiagnosticsStatusNotificationResponse
1379 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1380 status: OCPP16DiagnosticsStatus
.Uploaded
,
1385 return { fileName
: diagnosticsArchive
};
1387 throw new OCPPError(
1388 ErrorType
.GENERIC_ERROR
,
1389 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1390 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1392 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1395 throw new OCPPError(
1396 ErrorType
.GENERIC_ERROR
,
1397 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1398 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1400 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1403 await chargingStation
.ocppRequestService
.requestHandler
<
1404 OCPP16DiagnosticsStatusNotificationRequest
,
1405 OCPP16DiagnosticsStatusNotificationResponse
1406 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1407 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1412 return this.handleIncomingRequestError
<GetDiagnosticsResponse
>(
1414 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1416 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
},
1421 `${chargingStation.logPrefix()} Unsupported protocol ${
1423 } to transfer the diagnostic logs archive`,
1425 await chargingStation
.ocppRequestService
.requestHandler
<
1426 OCPP16DiagnosticsStatusNotificationRequest
,
1427 OCPP16DiagnosticsStatusNotificationResponse
1428 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1429 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1431 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1435 private handleRequestTriggerMessage(
1436 chargingStation
: ChargingStation
,
1437 commandPayload
: OCPP16TriggerMessageRequest
,
1438 ): OCPP16TriggerMessageResponse
{
1439 const { requestedMessage
, connectorId
} = commandPayload
;
1441 !OCPP16ServiceUtils
.checkFeatureProfile(
1443 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1444 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1446 !OCPP16ServiceUtils
.isMessageTriggerSupported(chargingStation
, requestedMessage
)
1448 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1451 !OCPP16ServiceUtils
.isConnectorIdValid(
1453 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1457 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1460 switch (requestedMessage
) {
1461 case OCPP16MessageTrigger
.BootNotification
:
1463 chargingStation
.ocppRequestService
1464 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1466 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1467 chargingStation
.bootNotificationRequest
,
1468 { skipBufferingOnError
: true, triggerMessage
: true },
1470 .then((response
) => {
1471 chargingStation
.bootNotificationResponse
= response
;
1473 .catch(Constants
.EMPTY_FUNCTION
);
1474 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1475 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1476 case OCPP16MessageTrigger
.Heartbeat
:
1478 chargingStation
.ocppRequestService
1479 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1481 OCPP16RequestCommand
.HEARTBEAT
,
1484 triggerMessage
: true,
1487 .catch(Constants
.EMPTY_FUNCTION
);
1488 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1489 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1490 case OCPP16MessageTrigger
.StatusNotification
:
1492 if (!isNullOrUndefined(connectorId
)) {
1493 chargingStation
.ocppRequestService
1494 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1496 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1499 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1500 status: chargingStation
.getConnectorStatus(connectorId
!)?.status,
1503 triggerMessage
: true,
1506 .catch(Constants
.EMPTY_FUNCTION
);
1508 // eslint-disable-next-line no-lonely-if
1509 if (chargingStation
.hasEvses
) {
1510 for (const evseStatus
of chargingStation
.evses
.values()) {
1511 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
1512 chargingStation
.ocppRequestService
1514 OCPP16StatusNotificationRequest
,
1515 OCPP16StatusNotificationResponse
1518 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1521 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1522 status: connectorStatus
.status,
1525 triggerMessage
: true,
1528 .catch(Constants
.EMPTY_FUNCTION
);
1532 for (const id
of chargingStation
.connectors
.keys()) {
1533 chargingStation
.ocppRequestService
1535 OCPP16StatusNotificationRequest
,
1536 OCPP16StatusNotificationResponse
1539 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1542 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1543 status: chargingStation
.getConnectorStatus(id
)?.status,
1546 triggerMessage
: true,
1549 .catch(Constants
.EMPTY_FUNCTION
);
1553 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1554 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1556 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1559 return this.handleIncomingRequestError
<OCPP16TriggerMessageResponse
>(
1561 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1563 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
},
1568 private handleRequestDataTransfer(
1569 chargingStation
: ChargingStation
,
1570 commandPayload
: OCPP16DataTransferRequest
,
1571 ): OCPP16DataTransferResponse
{
1572 const { vendorId
} = commandPayload
;
1574 if (Object.values(OCPP16DataTransferVendorId
).includes(vendorId
)) {
1575 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1577 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1579 return this.handleIncomingRequestError
<OCPP16DataTransferResponse
>(
1581 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1583 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
},
1588 private async handleRequestReserveNow(
1589 chargingStation
: ChargingStation
,
1590 commandPayload
: OCPP16ReserveNowRequest
,
1591 ): Promise
<OCPP16ReserveNowResponse
> {
1593 !OCPP16ServiceUtils
.checkFeatureProfile(
1595 OCPP16SupportedFeatureProfiles
.Reservation
,
1596 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1599 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1601 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1602 let response
: OCPP16ReserveNowResponse
;
1604 if (connectorId
> 0 && !chargingStation
.isConnectorAvailable(connectorId
)) {
1605 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1607 if (connectorId
=== 0 && !chargingStation
.getReserveConnectorZeroSupported()) {
1608 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1610 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1611 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1613 await removeExpiredReservations(chargingStation
);
1614 switch (chargingStation
.getConnectorStatus(connectorId
)!.status) {
1615 case OCPP16ChargePointStatus
.Faulted
:
1616 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1618 case OCPP16ChargePointStatus
.Preparing
:
1619 case OCPP16ChargePointStatus
.Charging
:
1620 case OCPP16ChargePointStatus
.SuspendedEV
:
1621 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1622 case OCPP16ChargePointStatus
.Finishing
:
1623 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1625 case OCPP16ChargePointStatus
.Unavailable
:
1626 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1628 case OCPP16ChargePointStatus
.Reserved
:
1629 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1630 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1633 // eslint-disable-next-line no-fallthrough
1635 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1636 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1639 await chargingStation
.addReservation({
1640 id
: commandPayload
.reservationId
,
1643 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1648 chargingStation
.getConnectorStatus(connectorId
)!.status = OCPP16ChargePointStatus
.Available
;
1649 return this.handleIncomingRequestError
<OCPP16ReserveNowResponse
>(
1651 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1653 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
},
1658 private async handleRequestCancelReservation(
1659 chargingStation
: ChargingStation
,
1660 commandPayload
: OCPP16CancelReservationRequest
,
1661 ): Promise
<GenericResponse
> {
1663 !OCPP16ServiceUtils
.checkFeatureProfile(
1665 OCPP16SupportedFeatureProfiles
.Reservation
,
1666 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1669 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1672 const { reservationId
} = commandPayload
;
1673 const reservation
= chargingStation
.getReservationBy('reservationId', reservationId
);
1674 if (isUndefined(reservation
)) {
1676 `${chargingStation.logPrefix()} Reservation with id ${reservationId}
1677 does not exist on charging station`,
1679 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1681 await chargingStation
.removeReservation(
1683 ReservationTerminationReason
.RESERVATION_CANCELED
,
1685 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1687 return this.handleIncomingRequestError
<GenericResponse
>(
1689 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1691 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
},