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 { create
} from
'tar';
11 import { OCPP16Constants
} from
'./OCPP16Constants';
12 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
15 ChargingStationConfigurationUtils
,
17 } from
'../../../charging-station';
18 import { OCPPError
} from
'../../../exception';
20 type ChangeAvailabilityRequest
,
21 type ChangeAvailabilityResponse
,
22 type ChangeConfigurationRequest
,
23 type ChangeConfigurationResponse
,
24 type ClearChargingProfileRequest
,
25 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 OCPP16ChargePointErrorCode
,
43 OCPP16ChargePointStatus
,
44 type OCPP16ChargingProfile
,
45 OCPP16ChargingProfilePurposeType
,
46 type OCPP16ChargingSchedule
,
47 type OCPP16ClearCacheRequest
,
48 type OCPP16DataTransferRequest
,
49 type OCPP16DataTransferResponse
,
50 OCPP16DataTransferVendorId
,
51 OCPP16DiagnosticsStatus
,
52 type OCPP16DiagnosticsStatusNotificationRequest
,
53 type OCPP16DiagnosticsStatusNotificationResponse
,
55 type OCPP16FirmwareStatusNotificationRequest
,
56 type OCPP16FirmwareStatusNotificationResponse
,
57 type OCPP16GetCompositeScheduleRequest
,
58 type OCPP16GetCompositeScheduleResponse
,
59 type OCPP16HeartbeatRequest
,
60 type OCPP16HeartbeatResponse
,
61 OCPP16IncomingRequestCommand
,
64 type OCPP16ReserveNowRequest
,
65 type OCPP16ReserveNowResponse
,
66 OCPP16StandardParametersKey
,
67 type OCPP16StartTransactionRequest
,
68 type OCPP16StartTransactionResponse
,
69 type OCPP16StatusNotificationRequest
,
70 type OCPP16StatusNotificationResponse
,
71 OCPP16StopTransactionReason
,
72 OCPP16SupportedFeatureProfiles
,
73 type OCPP16TriggerMessageRequest
,
74 type OCPP16TriggerMessageResponse
,
75 type OCPP16UpdateFirmwareRequest
,
76 type OCPP16UpdateFirmwareResponse
,
77 type OCPPConfigurationKey
,
79 type RemoteStartTransactionRequest
,
80 type RemoteStopTransactionRequest
,
82 ReservationTerminationReason
,
84 type SetChargingProfileRequest
,
85 type SetChargingProfileResponse
,
86 type StartTransactionRequest
,
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
>([
118 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
119 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
120 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
122 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
123 this.handleRequestGetConfiguration
.bind(this),
126 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
127 this.handleRequestChangeConfiguration
.bind(this),
130 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
131 this.handleRequestGetCompositeSchedule
.bind(this),
134 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
135 this.handleRequestSetChargingProfile
.bind(this),
138 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
139 this.handleRequestClearChargingProfile
.bind(this),
142 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
143 this.handleRequestChangeAvailability
.bind(this),
146 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
147 this.handleRequestRemoteStartTransaction
.bind(this),
150 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
151 this.handleRequestRemoteStopTransaction
.bind(this),
153 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
154 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
155 [OCPP16IncomingRequestCommand
.DATA_TRANSFER
, this.handleRequestDataTransfer
.bind(this)],
156 [OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
, this.handleRequestUpdateFirmware
.bind(this)],
157 [OCPP16IncomingRequestCommand
.RESERVE_NOW
, this.handleRequestReserveNow
.bind(this)],
159 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
160 this.handleRequestCancelReservation
.bind(this),
163 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
165 OCPP16IncomingRequestCommand
.RESET
,
166 OCPP16ServiceUtils
.parseJsonSchemaFile
<ResetRequest
>(
167 'assets/json-schemas/ocpp/1.6/Reset.json',
173 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
174 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ClearCacheRequest
>(
175 'assets/json-schemas/ocpp/1.6/ClearCache.json',
181 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
182 OCPP16ServiceUtils
.parseJsonSchemaFile
<UnlockConnectorRequest
>(
183 'assets/json-schemas/ocpp/1.6/UnlockConnector.json',
189 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
190 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetConfigurationRequest
>(
191 'assets/json-schemas/ocpp/1.6/GetConfiguration.json',
197 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
198 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeConfigurationRequest
>(
199 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json',
205 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
206 OCPP16ServiceUtils
.parseJsonSchemaFile
<GetDiagnosticsRequest
>(
207 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json',
213 OCPP16IncomingRequestCommand
.GET_COMPOSITE_SCHEDULE
,
214 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16GetCompositeScheduleRequest
>(
215 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json',
221 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
222 OCPP16ServiceUtils
.parseJsonSchemaFile
<SetChargingProfileRequest
>(
223 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json',
229 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
230 OCPP16ServiceUtils
.parseJsonSchemaFile
<ClearChargingProfileRequest
>(
231 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json',
237 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
238 OCPP16ServiceUtils
.parseJsonSchemaFile
<ChangeAvailabilityRequest
>(
239 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
245 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
246 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStartTransactionRequest
>(
247 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json',
253 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
254 OCPP16ServiceUtils
.parseJsonSchemaFile
<RemoteStopTransactionRequest
>(
255 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json',
261 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
262 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16TriggerMessageRequest
>(
263 'assets/json-schemas/ocpp/1.6/TriggerMessage.json',
269 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
270 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16DataTransferRequest
>(
271 'assets/json-schemas/ocpp/1.6/DataTransfer.json',
277 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
,
278 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16UpdateFirmwareRequest
>(
279 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json',
285 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
286 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16ReserveNowRequest
>(
287 'assets/json-schemas/ocpp/1.6/ReserveNow.json',
293 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
294 OCPP16ServiceUtils
.parseJsonSchemaFile
<OCPP16CancelReservationRequest
>(
295 'assets/json-schemas/ocpp/1.6/CancelReservation.json',
301 this.validatePayload
= this.validatePayload
.bind(this) as (
302 chargingStation
: ChargingStation
,
303 commandName
: OCPP16IncomingRequestCommand
,
304 commandPayload
: JsonType
308 public async incomingRequestHandler(
309 chargingStation
: ChargingStation
,
311 commandName
: OCPP16IncomingRequestCommand
,
312 commandPayload
: JsonType
314 let response
: JsonType
;
316 chargingStation
.getOcppStrictCompliance() === true &&
317 chargingStation
.inPendingState() === true &&
318 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
319 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
322 ErrorType
.SECURITY_ERROR
,
323 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
327 )} while the charging station is in pending state on the central server`,
333 chargingStation
.isRegistered() === true ||
334 (chargingStation
.getOcppStrictCompliance() === false &&
335 chargingStation
.inUnknownState() === true)
338 this.incomingRequestHandlers
.has(commandName
) === true &&
339 OCPP16ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
342 this.validatePayload(chargingStation
, commandName
, commandPayload
);
343 // Call the method to build the response
344 response
= await this.incomingRequestHandlers
.get(commandName
)(
351 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler:
352 Handle incoming request error:`,
360 ErrorType
.NOT_IMPLEMENTED
,
361 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
372 ErrorType
.SECURITY_ERROR
,
373 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
377 )} while the charging station is not registered on the central server.`,
382 // Send the built response
383 await chargingStation
.ocppRequestService
.sendResponse(
391 private validatePayload(
392 chargingStation
: ChargingStation
,
393 commandName
: OCPP16IncomingRequestCommand
,
394 commandPayload
: JsonType
396 if (this.jsonSchemas
.has(commandName
) === true) {
397 return this.validateIncomingRequestPayload(
400 this.jsonSchemas
.get(commandName
),
405 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found
406 for command '${commandName}' PDU validation`
411 // Simulate charging station restart
412 private handleRequestReset(
413 chargingStation
: ChargingStation
,
414 commandPayload
: ResetRequest
416 this.runInAsyncScope(
417 chargingStation
.reset
.bind(chargingStation
) as (
418 this: ChargingStation
,
422 `${commandPayload.type}Reset` as OCPP16StopTransactionReason
423 ).catch(Constants
.EMPTY_FUNCTION
);
425 `${chargingStation.logPrefix()} ${
427 } reset command received, simulating it. The station will be
428 back online in ${formatDurationMilliSeconds(chargingStation.stationInfo.resetTime)}`
430 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
433 private async handleRequestUnlockConnector(
434 chargingStation
: ChargingStation
,
435 commandPayload
: UnlockConnectorRequest
436 ): Promise
<UnlockConnectorResponse
> {
437 const connectorId
= commandPayload
.connectorId
;
438 if (chargingStation
.hasConnector(connectorId
) === false) {
440 `${chargingStation.logPrefix()} Trying to unlock a non existing
441 connector id ${connectorId.toString()}`
443 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
445 if (connectorId
=== 0) {
447 `${chargingStation.logPrefix()} Trying to unlock connector id ${connectorId.toString()}`
449 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
451 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
452 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
454 OCPP16StopTransactionReason
.UNLOCK_COMMAND
456 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
457 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
459 return OCPP16Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
461 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
464 OCPP16ChargePointStatus
.Available
466 return OCPP16Constants
.OCPP_RESPONSE_UNLOCKED
;
469 private handleRequestGetConfiguration(
470 chargingStation
: ChargingStation
,
471 commandPayload
: GetConfigurationRequest
472 ): GetConfigurationResponse
{
473 const configurationKey
: OCPPConfigurationKey
[] = [];
474 const unknownKey
: string[] = [];
475 if (isUndefined(commandPayload
.key
) === true) {
476 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
477 if (isUndefined(configuration
.visible
) === true) {
478 configuration
.visible
= true;
480 if (configuration
.visible
=== false) {
483 configurationKey
.push({
484 key
: configuration
.key
,
485 readonly: configuration
.readonly,
486 value
: configuration
.value
,
489 } else if (isNotEmptyArray(commandPayload
.key
) === true) {
490 for (const key
of commandPayload
.key
) {
491 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
497 if (isUndefined(keyFound
.visible
) === true) {
498 keyFound
.visible
= true;
500 if (keyFound
.visible
=== false) {
503 configurationKey
.push({
505 readonly: keyFound
.readonly,
506 value
: keyFound
.value
,
509 unknownKey
.push(key
);
519 private handleRequestChangeConfiguration(
520 chargingStation
: ChargingStation
,
521 commandPayload
: ChangeConfigurationRequest
522 ): ChangeConfigurationResponse
{
523 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
529 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
530 } else if (keyToChange
?.readonly === true) {
531 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
532 } else if (keyToChange
?.readonly === false) {
533 let valueChanged
= false;
534 if (keyToChange
.value
!== commandPayload
.value
) {
535 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
538 commandPayload
.value
,
543 let triggerHeartbeatRestart
= false;
544 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
545 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
547 OCPP16StandardParametersKey
.HeartbeatInterval
,
550 triggerHeartbeatRestart
= true;
552 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
553 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
555 OCPP16StandardParametersKey
.HeartBeatInterval
,
558 triggerHeartbeatRestart
= true;
560 if (triggerHeartbeatRestart
) {
561 chargingStation
.restartHeartbeat();
563 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
564 chargingStation
.restartWebSocketPing();
566 if (keyToChange
.reboot
) {
567 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
569 return OCPP16Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
573 private handleRequestSetChargingProfile(
574 chargingStation
: ChargingStation
,
575 commandPayload
: SetChargingProfileRequest
576 ): SetChargingProfileResponse
{
578 OCPP16ServiceUtils
.checkFeatureProfile(
580 OCPP16SupportedFeatureProfiles
.SmartCharging
,
581 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
584 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
586 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
588 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a
589 non existing connector id ${commandPayload.connectorId}`
591 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
594 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
595 OCPP16ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
596 commandPayload
.connectorId
!== 0
598 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
601 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
602 OCPP16ChargingProfilePurposeType
.TX_PROFILE
&&
603 (commandPayload
.connectorId
=== 0 ||
604 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
608 `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
609 on connector ${commandPayload.connectorId} without a started transaction`
611 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
613 OCPP16ServiceUtils
.setChargingProfile(
615 commandPayload
.connectorId
,
616 commandPayload
.csChargingProfiles
619 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
620 commandPayload.connectorId
622 commandPayload
.csChargingProfiles
624 return OCPP16Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
627 private handleRequestGetCompositeSchedule(
628 chargingStation
: ChargingStation
,
629 commandPayload
: OCPP16GetCompositeScheduleRequest
630 ): OCPP16GetCompositeScheduleResponse
{
632 OCPP16ServiceUtils
.checkFeatureProfile(
634 OCPP16SupportedFeatureProfiles
.SmartCharging
,
635 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
638 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
640 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
642 `${chargingStation.logPrefix()} Trying to get composite schedule to a
643 non existing connector id ${commandPayload.connectorId}`
645 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
648 isEmptyArray(chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
)
650 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
652 const startDate
= new Date();
653 const endDate
= new Date(startDate
.getTime() + commandPayload
.duration
* 1000);
654 let compositeSchedule
: OCPP16ChargingSchedule
;
655 for (const chargingProfile
of chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
657 // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
659 chargingProfile
.chargingSchedule
?.startSchedule
>= startDate
&&
660 chargingProfile
.chargingSchedule
?.startSchedule
<= endDate
662 compositeSchedule
= chargingProfile
.chargingSchedule
;
667 status: GenericStatus
.Accepted
,
668 scheduleStart
: compositeSchedule
?.startSchedule
,
669 connectorId
: commandPayload
.connectorId
,
670 chargingSchedule
: compositeSchedule
,
674 private handleRequestClearChargingProfile(
675 chargingStation
: ChargingStation
,
676 commandPayload
: ClearChargingProfileRequest
677 ): ClearChargingProfileResponse
{
679 OCPP16ServiceUtils
.checkFeatureProfile(
681 OCPP16SupportedFeatureProfiles
.SmartCharging
,
682 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
685 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
687 if (chargingStation
.hasConnector(commandPayload
.connectorId
) === false) {
689 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to
690 a non existing connector id ${commandPayload.connectorId}`
692 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
695 !isNullOrUndefined(commandPayload
.connectorId
) &&
697 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.chargingProfiles
700 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
702 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
703 commandPayload.connectorId
706 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
708 if (isNullOrUndefined(commandPayload
.connectorId
)) {
709 let clearedCP
= false;
710 const clearChargingProfiles
= (connectorStatus
: ConnectorStatus
) => {
711 if (isNotEmptyArray(connectorStatus
?.chargingProfiles
)) {
712 connectorStatus
?.chargingProfiles
?.forEach(
713 (chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
714 let clearCurrentCP
= false;
715 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
716 clearCurrentCP
= true;
719 !commandPayload
.chargingProfilePurpose
&&
720 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
722 clearCurrentCP
= true;
725 !chargingProfile
.stackLevel
&&
726 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
728 clearCurrentCP
= true;
731 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
732 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
734 clearCurrentCP
= true;
736 if (clearCurrentCP
) {
737 connectorStatus
?.chargingProfiles
?.splice(index
, 1);
739 `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`,
748 if (chargingStation
.hasEvses
) {
749 for (const evseStatus
of chargingStation
.evses
.values()) {
750 for (const connectorStatus
of evseStatus
.connectors
.values()) {
751 clearChargingProfiles(connectorStatus
);
755 for (const connectorId
of chargingStation
.connectors
.keys()) {
756 clearChargingProfiles(chargingStation
.getConnectorStatus(connectorId
));
760 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
763 return OCPP16Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
766 private async handleRequestChangeAvailability(
767 chargingStation
: ChargingStation
,
768 commandPayload
: ChangeAvailabilityRequest
769 ): Promise
<ChangeAvailabilityResponse
> {
770 const connectorId
: number = commandPayload
.connectorId
;
771 if (chargingStation
.hasConnector(connectorId
) === false) {
773 `${chargingStation.logPrefix()} Trying to change the availability of a
774 non existing connector id ${connectorId.toString()}`
776 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
778 const chargePointStatus
: OCPP16ChargePointStatus
=
779 commandPayload
.type === OCPP16AvailabilityType
.Operative
780 ? OCPP16ChargePointStatus
.Available
781 : OCPP16ChargePointStatus
.Unavailable
;
782 if (connectorId
=== 0) {
783 let response
: ChangeAvailabilityResponse
=
784 OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
785 const changeAvailability
= async (id
: number, connectorStatus
: ConnectorStatus
) => {
786 if (connectorStatus
?.transactionStarted
=== true) {
787 response
= OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
789 connectorStatus
.availability
= commandPayload
.type;
790 if (response
=== OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
791 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
798 if (chargingStation
.hasEvses
) {
799 for (const evseStatus
of chargingStation
.evses
.values()) {
800 for (const [id
, connectorStatus
] of evseStatus
.connectors
) {
801 await changeAvailability(id
, connectorStatus
);
805 for (const id
of chargingStation
.connectors
.keys()) {
806 await changeAvailability(id
, chargingStation
.getConnectorStatus(id
));
812 (chargingStation
.isChargingStationAvailable() === true ||
813 (chargingStation
.isChargingStationAvailable() === false &&
814 commandPayload
.type === OCPP16AvailabilityType
.Inoperative
))
816 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
817 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
818 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
820 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
821 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
826 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
828 return OCPP16Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
831 private async handleRequestRemoteStartTransaction(
832 chargingStation
: ChargingStation
,
833 commandPayload
: RemoteStartTransactionRequest
834 ): Promise
<GenericResponse
> {
835 const { connectorId
: transactionConnectorId
, idTag
, chargingProfile
} = commandPayload
;
837 chargingStation
.getConnectorStatus(transactionConnectorId
).status ===
838 OCPP16ChargePointStatus
.Reserved
;
839 const reservedOnConnectorZero
=
840 chargingStation
.getConnectorStatus(0).status === OCPP16ChargePointStatus
.Reserved
;
843 !chargingStation
.validateIncomingRequestWithReservation(transactionConnectorId
, idTag
)) ||
844 (reservedOnConnectorZero
&& !chargingStation
.validateIncomingRequestWithReservation(0, idTag
))
846 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
848 if (chargingStation
.hasConnector(transactionConnectorId
) === false) {
849 return this.notifyRemoteStartTransactionRejected(
851 transactionConnectorId
,
856 !chargingStation
.isChargingStationAvailable() ||
857 !chargingStation
.isConnectorAvailable(transactionConnectorId
)
859 return this.notifyRemoteStartTransactionRejected(
861 transactionConnectorId
,
865 const remoteStartTransactionLogMsg
= `
866 ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
867 chargingStation.stationInfo.chargingStationId
868 }#${transactionConnectorId.toString()} for idTag '${idTag}'`;
869 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
871 transactionConnectorId
,
872 OCPP16ChargePointStatus
.Preparing
874 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
875 // Check if authorized
877 chargingStation
.getAuthorizeRemoteTxRequests() &&
878 (await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, transactionConnectorId
, idTag
))
880 // Authorization successful, start transaction
882 this.setRemoteStartTransactionChargingProfile(
884 transactionConnectorId
,
888 connectorStatus
.transactionRemoteStarted
= true;
889 const startTransactionPayload
: Partial
<StartTransactionRequest
> = {
890 connectorId
: transactionConnectorId
,
893 if (reserved
|| reservedOnConnectorZero
) {
894 const reservation
= chargingStation
.getReservationBy(
895 ReservationFilterKey
.CONNECTOR_ID
,
896 reservedOnConnectorZero
? 0 : transactionConnectorId
898 startTransactionPayload
.reservationId
= reservation
.id
;
899 await chargingStation
.removeReservation(
901 ReservationTerminationReason
.TRANSACTION_STARTED
906 await chargingStation
.ocppRequestService
.requestHandler
<
907 OCPP16StartTransactionRequest
,
908 OCPP16StartTransactionResponse
909 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, startTransactionPayload
)
910 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
912 logger
.debug(remoteStartTransactionLogMsg
);
913 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
915 return this.notifyRemoteStartTransactionRejected(
917 transactionConnectorId
,
921 return this.notifyRemoteStartTransactionRejected(
923 transactionConnectorId
,
927 // No authorization check required, start transaction
929 this.setRemoteStartTransactionChargingProfile(
931 transactionConnectorId
,
935 connectorStatus
.transactionRemoteStarted
= true;
938 await chargingStation
.ocppRequestService
.requestHandler
<
939 OCPP16StartTransactionRequest
,
940 OCPP16StartTransactionResponse
941 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
942 connectorId
: transactionConnectorId
,
945 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
947 logger
.debug(remoteStartTransactionLogMsg
);
948 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
950 return this.notifyRemoteStartTransactionRejected(
952 transactionConnectorId
,
956 return this.notifyRemoteStartTransactionRejected(
958 transactionConnectorId
,
963 private async notifyRemoteStartTransactionRejected(
964 chargingStation
: ChargingStation
,
967 ): Promise
<GenericResponse
> {
969 chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.Available
971 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
974 OCPP16ChargePointStatus
.Available
978 `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id
979 ${connectorId.toString()}, idTag '${idTag}', availability '${
980 chargingStation.getConnectorStatus(connectorId)?.availability
981 }', status '${chargingStation.getConnectorStatus(connectorId)?.status}'`
983 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
986 private setRemoteStartTransactionChargingProfile(
987 chargingStation
: ChargingStation
,
989 cp
: OCPP16ChargingProfile
991 if (cp
&& cp
.chargingProfilePurpose
=== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
992 OCPP16ServiceUtils
.setChargingProfile(chargingStation
, connectorId
, cp
);
994 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
995 on connector id ${connectorId}: %j`,
999 } else if (cp
&& cp
.chargingProfilePurpose
!== OCPP16ChargingProfilePurposeType
.TX_PROFILE
) {
1001 `${chargingStation.logPrefix()} Not allowed to set ${
1002 cp.chargingProfilePurpose
1003 } charging profile(s) at remote start transaction`
1011 private async handleRequestRemoteStopTransaction(
1012 chargingStation
: ChargingStation
,
1013 commandPayload
: RemoteStopTransactionRequest
1014 ): Promise
<GenericResponse
> {
1015 const transactionId
= commandPayload
.transactionId
;
1016 const remoteStopTransaction
= async (connectorId
: number): Promise
<GenericResponse
> => {
1017 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1020 OCPP16ChargePointStatus
.Finishing
1022 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
1024 OCPP16StopTransactionReason
.REMOTE
1026 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
1027 return OCPP16Constants
.OCPP_RESPONSE_ACCEPTED
;
1029 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1031 if (chargingStation
.hasEvses
) {
1032 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1034 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1035 if (connectorStatus
.transactionId
=== transactionId
) {
1036 return remoteStopTransaction(connectorId
);
1042 for (const connectorId
of chargingStation
.connectors
.keys()) {
1045 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
1047 return remoteStopTransaction(connectorId
);
1052 `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
1053 ${transactionId.toString()}`
1055 return OCPP16Constants
.OCPP_RESPONSE_REJECTED
;
1058 private handleRequestUpdateFirmware(
1059 chargingStation
: ChargingStation
,
1060 commandPayload
: OCPP16UpdateFirmwareRequest
1061 ): OCPP16UpdateFirmwareResponse
{
1063 OCPP16ServiceUtils
.checkFeatureProfile(
1065 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1066 OCPP16IncomingRequestCommand
.UPDATE_FIRMWARE
1070 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1071 Cannot simulate firmware update: feature profile not supported`
1073 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1076 !isNullOrUndefined(chargingStation
.stationInfo
.firmwareStatus
) &&
1077 chargingStation
.stationInfo
.firmwareStatus
!== OCPP16FirmwareStatus
.Installed
1080 `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware:
1081 Cannot simulate firmware update: firmware update is already in progress`
1083 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1085 const retrieveDate
= convertToDate(commandPayload
.retrieveDate
);
1086 const now
= Date.now();
1087 if (retrieveDate
?.getTime() <= now
) {
1088 this.runInAsyncScope(
1089 this.updateFirmwareSimulation
.bind(this) as (
1090 this: OCPP16IncomingRequestService
,
1095 ).catch(Constants
.EMPTY_FUNCTION
);
1098 this.runInAsyncScope(
1099 this.updateFirmwareSimulation
.bind(this) as (
1100 this: OCPP16IncomingRequestService
,
1105 ).catch(Constants
.EMPTY_FUNCTION
);
1106 }, retrieveDate
?.getTime() - now
);
1108 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1111 private async updateFirmwareSimulation(
1112 chargingStation
: ChargingStation
,
1116 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1119 if (chargingStation
.hasEvses
) {
1120 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1122 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1123 if (connectorStatus
?.transactionStarted
=== false) {
1124 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1127 OCPP16ChargePointStatus
.Unavailable
1134 for (const connectorId
of chargingStation
.connectors
.keys()) {
1137 chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== false
1139 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1142 OCPP16ChargePointStatus
.Unavailable
1147 await chargingStation
.ocppRequestService
.requestHandler
<
1148 OCPP16FirmwareStatusNotificationRequest
,
1149 OCPP16FirmwareStatusNotificationResponse
1150 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1151 status: OCPP16FirmwareStatus
.Downloading
,
1153 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloading
;
1155 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1156 OCPP16FirmwareStatus
.DownloadFailed
1158 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1159 await chargingStation
.ocppRequestService
.requestHandler
<
1160 OCPP16FirmwareStatusNotificationRequest
,
1161 OCPP16FirmwareStatusNotificationResponse
1162 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1163 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1165 chargingStation
.stationInfo
.firmwareStatus
=
1166 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1169 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1170 await chargingStation
.ocppRequestService
.requestHandler
<
1171 OCPP16FirmwareStatusNotificationRequest
,
1172 OCPP16FirmwareStatusNotificationResponse
1173 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1174 status: OCPP16FirmwareStatus
.Downloaded
,
1176 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Downloaded
;
1177 let wasTransactionsStarted
= false;
1178 let transactionsStarted
: boolean;
1180 const runningTransactions
= chargingStation
.getNumberOfRunningTransactions();
1181 if (runningTransactions
> 0) {
1182 const waitTime
= 15 * 1000;
1184 `${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
1185 ${runningTransactions} transaction(s) in progress, waiting ${
1187 } seconds before continuing firmware update simulation`
1189 await sleep(waitTime
);
1190 transactionsStarted
= true;
1191 wasTransactionsStarted
= true;
1193 if (chargingStation
.hasEvses
) {
1194 for (const [evseId
, evseStatus
] of chargingStation
.evses
) {
1196 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1197 if (connectorStatus
?.status !== OCPP16ChargePointStatus
.Unavailable
) {
1198 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1201 OCPP16ChargePointStatus
.Unavailable
1208 for (const connectorId
of chargingStation
.connectors
.keys()) {
1211 chargingStation
.getConnectorStatus(connectorId
)?.status !==
1212 OCPP16ChargePointStatus
.Unavailable
1214 await OCPP16ServiceUtils
.sendAndSetConnectorStatus(
1217 OCPP16ChargePointStatus
.Unavailable
1222 transactionsStarted
= false;
1224 } while (transactionsStarted
);
1225 !wasTransactionsStarted
&& (await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000));
1226 if (checkChargingStation(chargingStation
, chargingStation
.logPrefix()) === false) {
1229 await chargingStation
.ocppRequestService
.requestHandler
<
1230 OCPP16FirmwareStatusNotificationRequest
,
1231 OCPP16FirmwareStatusNotificationResponse
1232 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1233 status: OCPP16FirmwareStatus
.Installing
,
1235 chargingStation
.stationInfo
.firmwareStatus
= OCPP16FirmwareStatus
.Installing
;
1237 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
===
1238 OCPP16FirmwareStatus
.InstallationFailed
1240 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1241 await chargingStation
.ocppRequestService
.requestHandler
<
1242 OCPP16FirmwareStatusNotificationRequest
,
1243 OCPP16FirmwareStatusNotificationResponse
1244 >(chargingStation
, OCPP16RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1245 status: chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
,
1247 chargingStation
.stationInfo
.firmwareStatus
=
1248 chargingStation
.stationInfo
?.firmwareUpgrade
?.failureStatus
;
1251 if (chargingStation
.stationInfo
?.firmwareUpgrade
?.reset
=== true) {
1252 await sleep(getRandomInteger(maxDelay
, minDelay
) * 1000);
1253 await chargingStation
.reset(OCPP16StopTransactionReason
.REBOOT
);
1257 private async handleRequestGetDiagnostics(
1258 chargingStation
: ChargingStation
,
1259 commandPayload
: GetDiagnosticsRequest
1260 ): Promise
<GetDiagnosticsResponse
> {
1262 OCPP16ServiceUtils
.checkFeatureProfile(
1264 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
1265 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1269 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1270 Cannot get diagnostics: feature profile not supported`
1272 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1274 const uri
= new URL(commandPayload
.location
);
1275 if (uri
.protocol
.startsWith('ftp:')) {
1276 let ftpClient
: Client
;
1278 const logFiles
= readdirSync(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'))
1279 .filter((file
) => file
.endsWith('.log'))
1280 .map((file
) => join('./', file
));
1281 const diagnosticsArchive
= `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
1282 create({ gzip
: true }, logFiles
).pipe(createWriteStream(diagnosticsArchive
));
1283 ftpClient
= new Client();
1284 const accessResponse
= await ftpClient
.access({
1286 ...(isNotEmptyString(uri
.port
) && { port
: convertToInt(uri
.port
) }),
1287 ...(isNotEmptyString(uri
.username
) && { user
: uri
.username
}),
1288 ...(isNotEmptyString(uri
.password
) && { password
: uri
.password
}),
1290 let uploadResponse
: FTPResponse
;
1291 if (accessResponse
.code
=== 220) {
1292 ftpClient
.trackProgress((info
) => {
1294 `${chargingStation.logPrefix()} ${
1296 } bytes transferred from diagnostics archive ${info.name}`
1298 chargingStation
.ocppRequestService
1300 OCPP16DiagnosticsStatusNotificationRequest
,
1301 OCPP16DiagnosticsStatusNotificationResponse
1302 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1303 status: OCPP16DiagnosticsStatus
.Uploading
,
1307 `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics:
1308 Error while sending '${OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION}'`,
1313 uploadResponse
= await ftpClient
.uploadFrom(
1314 join(resolve(dirname(fileURLToPath(import.meta
.url
)), '../'), diagnosticsArchive
),
1315 `${uri.pathname}${diagnosticsArchive}`
1317 if (uploadResponse
.code
=== 226) {
1318 await chargingStation
.ocppRequestService
.requestHandler
<
1319 OCPP16DiagnosticsStatusNotificationRequest
,
1320 OCPP16DiagnosticsStatusNotificationResponse
1321 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1322 status: OCPP16DiagnosticsStatus
.Uploaded
,
1327 return { fileName
: diagnosticsArchive
};
1329 throw new OCPPError(
1330 ErrorType
.GENERIC_ERROR
,
1331 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1332 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1334 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1337 throw new OCPPError(
1338 ErrorType
.GENERIC_ERROR
,
1339 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1340 uploadResponse?.code && `|${uploadResponse?.code.toString()}
`
1342 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1345 await chargingStation
.ocppRequestService
.requestHandler
<
1346 OCPP16DiagnosticsStatusNotificationRequest
,
1347 OCPP16DiagnosticsStatusNotificationResponse
1348 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1349 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1354 return this.handleIncomingRequestError(
1356 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1358 { errorResponse
: OCPP16Constants
.OCPP_RESPONSE_EMPTY
}
1363 `${chargingStation.logPrefix()} Unsupported protocol ${
1365 } to transfer the diagnostic logs archive`
1367 await chargingStation
.ocppRequestService
.requestHandler
<
1368 OCPP16DiagnosticsStatusNotificationRequest
,
1369 OCPP16DiagnosticsStatusNotificationResponse
1370 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1371 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1373 return OCPP16Constants
.OCPP_RESPONSE_EMPTY
;
1377 private handleRequestTriggerMessage(
1378 chargingStation
: ChargingStation
,
1379 commandPayload
: OCPP16TriggerMessageRequest
1380 ): OCPP16TriggerMessageResponse
{
1382 !OCPP16ServiceUtils
.checkFeatureProfile(
1384 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1385 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1387 !OCPP16ServiceUtils
.isMessageTriggerSupported(
1389 commandPayload
.requestedMessage
1392 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1395 !OCPP16ServiceUtils
.isConnectorIdValid(
1397 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1398 commandPayload
.connectorId
1401 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1404 switch (commandPayload
.requestedMessage
) {
1405 case OCPP16MessageTrigger
.BootNotification
:
1407 chargingStation
.ocppRequestService
1408 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1410 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1411 chargingStation
.bootNotificationRequest
,
1412 { skipBufferingOnError
: true, triggerMessage
: true }
1414 .then((response
) => {
1415 chargingStation
.bootNotificationResponse
= response
;
1417 .catch(Constants
.EMPTY_FUNCTION
);
1418 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1419 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1420 case OCPP16MessageTrigger
.Heartbeat
:
1422 chargingStation
.ocppRequestService
1423 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1425 OCPP16RequestCommand
.HEARTBEAT
,
1428 triggerMessage
: true,
1431 .catch(Constants
.EMPTY_FUNCTION
);
1432 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1433 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1434 case OCPP16MessageTrigger
.StatusNotification
:
1436 if (!isNullOrUndefined(commandPayload
?.connectorId
)) {
1437 chargingStation
.ocppRequestService
1438 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1440 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1442 connectorId
: commandPayload
.connectorId
,
1443 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1444 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.status,
1447 triggerMessage
: true,
1450 .catch(Constants
.EMPTY_FUNCTION
);
1452 // eslint-disable-next-line no-lonely-if
1453 if (chargingStation
.hasEvses
) {
1454 for (const evseStatus
of chargingStation
.evses
.values()) {
1455 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1456 chargingStation
.ocppRequestService
1458 OCPP16StatusNotificationRequest
,
1459 OCPP16StatusNotificationResponse
1462 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1465 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1466 status: connectorStatus
.status,
1469 triggerMessage
: true,
1472 .catch(Constants
.EMPTY_FUNCTION
);
1476 for (const connectorId
of chargingStation
.connectors
.keys()) {
1477 chargingStation
.ocppRequestService
1479 OCPP16StatusNotificationRequest
,
1480 OCPP16StatusNotificationResponse
1483 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1486 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1487 status: chargingStation
.getConnectorStatus(connectorId
)?.status,
1490 triggerMessage
: true,
1493 .catch(Constants
.EMPTY_FUNCTION
);
1497 }, OCPP16Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1498 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1500 return OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1503 return this.handleIncomingRequestError(
1505 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1507 { errorResponse
: OCPP16Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}
1512 private handleRequestDataTransfer(
1513 chargingStation
: ChargingStation
,
1514 commandPayload
: OCPP16DataTransferRequest
1515 ): OCPP16DataTransferResponse
{
1517 if (Object.values(OCPP16DataTransferVendorId
).includes(commandPayload
.vendorId
)) {
1518 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_ACCEPTED
;
1520 return OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
;
1522 return this.handleIncomingRequestError(
1524 OCPP16IncomingRequestCommand
.DATA_TRANSFER
,
1526 { errorResponse
: OCPP16Constants
.OCPP_DATA_TRANSFER_RESPONSE_REJECTED
}
1531 private async handleRequestReserveNow(
1532 chargingStation
: ChargingStation
,
1533 commandPayload
: OCPP16ReserveNowRequest
1534 ): Promise
<OCPP16ReserveNowResponse
> {
1536 !OCPP16ServiceUtils
.checkFeatureProfile(
1538 OCPP16SupportedFeatureProfiles
.Reservation
,
1539 OCPP16IncomingRequestCommand
.RESERVE_NOW
1542 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1544 const { reservationId
, idTag
, connectorId
} = commandPayload
;
1545 let response
: OCPP16ReserveNowResponse
;
1547 if (!chargingStation
.isConnectorAvailable(connectorId
) && connectorId
> 0) {
1548 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1550 if (connectorId
=== 0 && !chargingStation
.getReservationOnConnectorId0Enabled()) {
1551 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1553 if (!(await OCPP16ServiceUtils
.isIdTagAuthorized(chargingStation
, connectorId
, idTag
))) {
1554 return OCPP16Constants
.OCPP_RESERVATION_RESPONSE_REJECTED
;
1556 switch (chargingStation
.getConnectorStatus(connectorId
).status) {
1557 case OCPP16ChargePointStatus
.Faulted
:
1558 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
;
1560 case OCPP16ChargePointStatus
.Preparing
:
1561 case OCPP16ChargePointStatus
.Charging
:
1562 case OCPP16ChargePointStatus
.SuspendedEV
:
1563 case OCPP16ChargePointStatus
.SuspendedEVSE
:
1564 case OCPP16ChargePointStatus
.Finishing
:
1565 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1567 case OCPP16ChargePointStatus
.Unavailable
:
1568 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_UNAVAILABLE
;
1570 case OCPP16ChargePointStatus
.Reserved
:
1571 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
, connectorId
)) {
1572 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1575 // eslint-disable-next-line no-fallthrough
1577 if (!chargingStation
.isConnectorReservable(reservationId
, idTag
)) {
1578 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_OCCUPIED
;
1581 await chargingStation
.addReservation({
1582 id
: commandPayload
.reservationId
,
1585 response
= OCPP16Constants
.OCPP_RESERVATION_RESPONSE_ACCEPTED
;
1590 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.Available
;
1591 return this.handleIncomingRequestError(
1593 OCPP16IncomingRequestCommand
.RESERVE_NOW
,
1595 { errorResponse
: OCPP16Constants
.OCPP_RESERVATION_RESPONSE_FAULTED
}
1600 private async handleRequestCancelReservation(
1601 chargingStation
: ChargingStation
,
1602 commandPayload
: OCPP16CancelReservationRequest
1603 ): Promise
<GenericResponse
> {
1605 !OCPP16ServiceUtils
.checkFeatureProfile(
1607 OCPP16SupportedFeatureProfiles
.Reservation
,
1608 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
1611 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1614 const { reservationId
} = commandPayload
;
1615 const [exists
, reservation
] = chargingStation
.doesReservationExists({ id
: reservationId
});
1618 `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
1619 does not exist on charging station`
1621 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
;
1623 await chargingStation
.removeReservation(
1625 ReservationTerminationReason
.RESERVATION_CANCELED
1627 return OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
;
1629 return this.handleIncomingRequestError(
1631 OCPP16IncomingRequestCommand
.CANCEL_RESERVATION
,
1633 { errorResponse
: OCPP16Constants
.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED
}