1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 import path from
'path';
5 import { URL
, fileURLToPath
} from
'url';
7 import type { JSONSchemaType
} from
'ajv';
8 import { Client
, FTPResponse
} from
'basic-ftp';
11 import OCPPError from
'../../../exception/OCPPError';
12 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
13 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
14 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
16 ChargingProfilePurposeType
,
17 OCPP16ChargingProfile
,
18 } from
'../../../types/ocpp/1.6/ChargingProfile';
20 OCPP16StandardParametersKey
,
21 OCPP16SupportedFeatureProfiles
,
22 } from
'../../../types/ocpp/1.6/Configuration';
23 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
25 ChangeAvailabilityRequest
,
26 ChangeConfigurationRequest
,
27 ClearChargingProfileRequest
,
28 DiagnosticsStatusNotificationRequest
,
29 GetConfigurationRequest
,
30 GetDiagnosticsRequest
,
32 OCPP16AvailabilityType
,
33 OCPP16BootNotificationRequest
,
34 OCPP16ClearCacheRequest
,
35 OCPP16HeartbeatRequest
,
36 OCPP16IncomingRequestCommand
,
38 OCPP16StatusNotificationRequest
,
39 OCPP16TriggerMessageRequest
,
40 RemoteStartTransactionRequest
,
41 RemoteStopTransactionRequest
,
43 SetChargingProfileRequest
,
44 UnlockConnectorRequest
,
45 } from
'../../../types/ocpp/1.6/Requests';
47 ChangeAvailabilityResponse
,
48 ChangeConfigurationResponse
,
49 ClearChargingProfileResponse
,
50 DiagnosticsStatusNotificationResponse
,
51 GetConfigurationResponse
,
52 GetDiagnosticsResponse
,
53 OCPP16BootNotificationResponse
,
54 OCPP16HeartbeatResponse
,
55 OCPP16StatusNotificationResponse
,
56 OCPP16TriggerMessageResponse
,
57 SetChargingProfileResponse
,
58 UnlockConnectorResponse
,
59 } from
'../../../types/ocpp/1.6/Responses';
61 OCPP16AuthorizationStatus
,
62 OCPP16AuthorizeRequest
,
63 OCPP16AuthorizeResponse
,
64 OCPP16StartTransactionRequest
,
65 OCPP16StartTransactionResponse
,
66 OCPP16StopTransactionReason
,
67 } from
'../../../types/ocpp/1.6/Transaction';
68 import type { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
69 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
70 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
71 import type { DefaultResponse
} from
'../../../types/ocpp/Responses';
72 import { StopTransactionReason
} from
'../../../types/ocpp/Transaction';
73 import Constants from
'../../../utils/Constants';
74 import logger from
'../../../utils/Logger';
75 import Utils from
'../../../utils/Utils';
76 import type ChargingStation from
'../../ChargingStation';
77 import { ChargingStationConfigurationUtils
} from
'../../ChargingStationConfigurationUtils';
78 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
79 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
80 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
82 const moduleName
= 'OCPP16IncomingRequestService';
84 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
85 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
86 private jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
88 public constructor() {
89 if (new.target
?.name
=== moduleName
) {
90 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
93 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
94 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
95 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
96 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
98 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
99 this.handleRequestGetConfiguration
.bind(this),
102 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
103 this.handleRequestChangeConfiguration
.bind(this),
106 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
107 this.handleRequestSetChargingProfile
.bind(this),
110 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
111 this.handleRequestClearChargingProfile
.bind(this),
114 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
115 this.handleRequestChangeAvailability
.bind(this),
118 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
119 this.handleRequestRemoteStartTransaction
.bind(this),
122 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
123 this.handleRequestRemoteStopTransaction
.bind(this),
125 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
126 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
128 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
130 OCPP16IncomingRequestCommand
.RESET
,
134 path
.dirname(fileURLToPath(import.meta
.url
)),
135 '../../../assets/json-schemas/ocpp/1.6/Reset.json'
139 ) as JSONSchemaType
<ResetRequest
>,
142 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
146 path
.dirname(fileURLToPath(import.meta
.url
)),
147 '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
151 ) as JSONSchemaType
<OCPP16ClearCacheRequest
>,
154 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
158 path
.dirname(fileURLToPath(import.meta
.url
)),
159 '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
163 ) as JSONSchemaType
<UnlockConnectorRequest
>,
166 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
170 path
.dirname(fileURLToPath(import.meta
.url
)),
171 '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
175 ) as JSONSchemaType
<GetConfigurationRequest
>,
178 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
182 path
.dirname(fileURLToPath(import.meta
.url
)),
183 '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
187 ) as JSONSchemaType
<ChangeConfigurationRequest
>,
190 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
194 path
.dirname(fileURLToPath(import.meta
.url
)),
195 '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
199 ) as JSONSchemaType
<GetDiagnosticsRequest
>,
202 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
206 path
.dirname(fileURLToPath(import.meta
.url
)),
207 '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
211 ) as JSONSchemaType
<SetChargingProfileRequest
>,
214 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
218 path
.dirname(fileURLToPath(import.meta
.url
)),
219 '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
223 ) as JSONSchemaType
<ClearChargingProfileRequest
>,
226 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
230 path
.dirname(fileURLToPath(import.meta
.url
)),
231 '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
235 ) as JSONSchemaType
<ChangeAvailabilityRequest
>,
238 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
242 path
.dirname(fileURLToPath(import.meta
.url
)),
243 '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
247 ) as JSONSchemaType
<RemoteStartTransactionRequest
>,
250 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
254 path
.dirname(fileURLToPath(import.meta
.url
)),
255 '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
259 ) as JSONSchemaType
<RemoteStopTransactionRequest
>,
262 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
266 path
.dirname(fileURLToPath(import.meta
.url
)),
267 '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
271 ) as JSONSchemaType
<OCPP16TriggerMessageRequest
>,
274 this.validatePayload
.bind(this);
277 public async incomingRequestHandler(
278 chargingStation
: ChargingStation
,
280 commandName
: OCPP16IncomingRequestCommand
,
281 commandPayload
: JsonType
283 let response
: JsonType
;
285 chargingStation
.getOcppStrictCompliance() &&
286 chargingStation
.isInPendingState() &&
287 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
288 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
291 ErrorType
.SECURITY_ERROR
,
292 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
296 )} while the charging station is in pending state on the central server`,
302 chargingStation
.isRegistered() ||
303 (!chargingStation
.getOcppStrictCompliance() && chargingStation
.isInUnknownState())
306 this.incomingRequestHandlers
.has(commandName
) &&
307 ChargingStationUtils
.isIncomingRequestCommandSupported(commandName
, chargingStation
)
310 this.validatePayload(chargingStation
, commandName
, commandPayload
);
311 // Call the method to build the response
312 response
= await this.incomingRequestHandlers
.get(commandName
)(
319 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
327 ErrorType
.NOT_IMPLEMENTED
,
328 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
339 ErrorType
.SECURITY_ERROR
,
340 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
344 )} while the charging station is not registered on the central server.`,
349 // Send the built response
350 await chargingStation
.ocppRequestService
.sendResponse(
358 private validatePayload(
359 chargingStation
: ChargingStation
,
360 commandName
: OCPP16IncomingRequestCommand
,
361 commandPayload
: JsonType
363 if (this.jsonSchemas
.has(commandName
)) {
364 return this.validateIncomingRequestPayload(
367 this.jsonSchemas
.get(commandName
),
372 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
377 // Simulate charging station restart
378 private handleRequestReset(
379 chargingStation
: ChargingStation
,
380 commandPayload
: ResetRequest
382 // eslint-disable-next-line @typescript-eslint/no-misused-promises
383 setImmediate(async (): Promise
<void> => {
384 if (chargingStation
.getNumberOfRunningTransactions() > 0) {
385 await chargingStation
.stopRunningTransactions(
386 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
389 await chargingStation
.reset();
392 `${chargingStation.logPrefix()} ${
394 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
395 chargingStation.stationInfo.resetTime
398 return Constants
.OCPP_RESPONSE_ACCEPTED
;
401 private handleRequestClearCache(): DefaultResponse
{
402 return Constants
.OCPP_RESPONSE_ACCEPTED
;
405 private async handleRequestUnlockConnector(
406 chargingStation
: ChargingStation
,
407 commandPayload
: UnlockConnectorRequest
408 ): Promise
<UnlockConnectorResponse
> {
409 const connectorId
= commandPayload
.connectorId
;
410 if (connectorId
=== 0) {
412 chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
414 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
416 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
417 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
419 OCPP16StopTransactionReason
.UNLOCK_COMMAND
421 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
422 return Constants
.OCPP_RESPONSE_UNLOCKED
;
424 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
426 await chargingStation
.ocppRequestService
.requestHandler
<
427 OCPP16StatusNotificationRequest
,
428 OCPP16StatusNotificationResponse
429 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
431 status: OCPP16ChargePointStatus
.AVAILABLE
,
432 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
434 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
435 return Constants
.OCPP_RESPONSE_UNLOCKED
;
438 private handleRequestGetConfiguration(
439 chargingStation
: ChargingStation
,
440 commandPayload
: GetConfigurationRequest
441 ): GetConfigurationResponse
{
442 const configurationKey
: OCPPConfigurationKey
[] = [];
443 const unknownKey
: string[] = [];
444 if (Utils
.isEmptyArray(commandPayload
.key
)) {
445 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
446 if (Utils
.isUndefined(configuration
.visible
)) {
447 configuration
.visible
= true;
449 if (!configuration
.visible
) {
452 configurationKey
.push({
453 key
: configuration
.key
,
454 readonly: configuration
.readonly,
455 value
: configuration
.value
,
459 for (const key
of commandPayload
.key
) {
460 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
465 if (Utils
.isUndefined(keyFound
.visible
)) {
466 keyFound
.visible
= true;
468 if (!keyFound
.visible
) {
471 configurationKey
.push({
473 readonly: keyFound
.readonly,
474 value
: keyFound
.value
,
477 unknownKey
.push(key
);
487 private handleRequestChangeConfiguration(
488 chargingStation
: ChargingStation
,
489 commandPayload
: ChangeConfigurationRequest
490 ): ChangeConfigurationResponse
{
491 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
497 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
498 } else if (keyToChange
&& keyToChange
.readonly) {
499 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
500 } else if (keyToChange
&& !keyToChange
.readonly) {
501 let valueChanged
= false;
502 if (keyToChange
.value
!== commandPayload
.value
) {
503 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
506 commandPayload
.value
,
511 let triggerHeartbeatRestart
= false;
512 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
513 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
515 OCPP16StandardParametersKey
.HeartbeatInterval
,
518 triggerHeartbeatRestart
= true;
520 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
521 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
523 OCPP16StandardParametersKey
.HeartBeatInterval
,
526 triggerHeartbeatRestart
= true;
528 if (triggerHeartbeatRestart
) {
529 chargingStation
.restartHeartbeat();
531 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
532 chargingStation
.restartWebSocketPing();
534 if (keyToChange
.reboot
) {
535 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
537 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
541 private handleRequestSetChargingProfile(
542 chargingStation
: ChargingStation
,
543 commandPayload
: SetChargingProfileRequest
544 ): SetChargingProfileResponse
{
546 !OCPP16ServiceUtils
.checkFeatureProfile(
548 OCPP16SupportedFeatureProfiles
.SmartCharging
,
549 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
552 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
554 if (!chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
556 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
557 commandPayload.connectorId
560 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
563 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
564 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
565 commandPayload
.connectorId
!== 0
567 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
570 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
571 ChargingProfilePurposeType
.TX_PROFILE
&&
572 (commandPayload
.connectorId
=== 0 ||
573 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
576 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
578 chargingStation
.setChargingProfile(
579 commandPayload
.connectorId
,
580 commandPayload
.csChargingProfiles
583 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
584 commandPayload.connectorId
585 }, dump their stack: %j`,
586 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
588 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
591 private handleRequestClearChargingProfile(
592 chargingStation
: ChargingStation
,
593 commandPayload
: ClearChargingProfileRequest
594 ): ClearChargingProfileResponse
{
596 !OCPP16ServiceUtils
.checkFeatureProfile(
598 OCPP16SupportedFeatureProfiles
.SmartCharging
,
599 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
602 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
604 const connectorStatus
= chargingStation
.getConnectorStatus(commandPayload
.connectorId
);
605 if (!connectorStatus
) {
607 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
608 commandPayload.connectorId
611 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
613 if (commandPayload
.connectorId
&& !Utils
.isEmptyArray(connectorStatus
.chargingProfiles
)) {
614 connectorStatus
.chargingProfiles
= [];
616 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
617 commandPayload.connectorId
618 }, dump their stack: %j`,
619 connectorStatus
.chargingProfiles
621 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
623 if (!commandPayload
.connectorId
) {
624 let clearedCP
= false;
625 for (const connectorId
of chargingStation
.connectors
.keys()) {
626 if (!Utils
.isEmptyArray(chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)) {
628 .getConnectorStatus(connectorId
)
629 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
630 let clearCurrentCP
= false;
631 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
632 clearCurrentCP
= true;
635 !commandPayload
.chargingProfilePurpose
&&
636 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
638 clearCurrentCP
= true;
641 !chargingProfile
.stackLevel
&&
642 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
644 clearCurrentCP
= true;
647 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
648 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
650 clearCurrentCP
= true;
652 if (clearCurrentCP
) {
653 connectorStatus
.chargingProfiles
.splice(index
, 1);
655 `${chargingStation.logPrefix()} Matching charging profile(s) cleared on connector id ${
656 commandPayload.connectorId
657 }, dump their stack: %j`,
658 connectorStatus
.chargingProfiles
666 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
669 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
672 private async handleRequestChangeAvailability(
673 chargingStation
: ChargingStation
,
674 commandPayload
: ChangeAvailabilityRequest
675 ): Promise
<ChangeAvailabilityResponse
> {
676 const connectorId
: number = commandPayload
.connectorId
;
677 if (!chargingStation
.getConnectorStatus(connectorId
)) {
679 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
681 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
683 const chargePointStatus
: OCPP16ChargePointStatus
=
684 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
685 ? OCPP16ChargePointStatus
.AVAILABLE
686 : OCPP16ChargePointStatus
.UNAVAILABLE
;
687 if (connectorId
=== 0) {
688 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
689 for (const id
of chargingStation
.connectors
.keys()) {
690 if (chargingStation
.getConnectorStatus(id
)?.transactionStarted
=== true) {
691 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
693 chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
694 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
695 await chargingStation
.ocppRequestService
.requestHandler
<
696 OCPP16StatusNotificationRequest
,
697 OCPP16StatusNotificationResponse
698 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
700 status: chargePointStatus
,
701 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
703 chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
709 (chargingStation
.getConnectorStatus(0).availability
=== OCPP16AvailabilityType
.OPERATIVE
||
710 (chargingStation
.getConnectorStatus(0).availability
===
711 OCPP16AvailabilityType
.INOPERATIVE
&&
712 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
714 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
715 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
716 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
718 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
719 await chargingStation
.ocppRequestService
.requestHandler
<
720 OCPP16StatusNotificationRequest
,
721 OCPP16StatusNotificationResponse
722 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
724 status: chargePointStatus
,
725 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
727 chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
728 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
730 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
733 private async handleRequestRemoteStartTransaction(
734 chargingStation
: ChargingStation
,
735 commandPayload
: RemoteStartTransactionRequest
736 ): Promise
<DefaultResponse
> {
737 const transactionConnectorId
= commandPayload
.connectorId
;
738 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
739 if (transactionConnectorId
) {
740 const remoteStartTransactionLogMsg
=
741 chargingStation
.logPrefix() +
742 ' Transaction remotely STARTED on ' +
743 chargingStation
.stationInfo
.chargingStationId
+
745 transactionConnectorId
.toString() +
747 commandPayload
.idTag
+
749 await chargingStation
.ocppRequestService
.requestHandler
<
750 OCPP16StatusNotificationRequest
,
751 OCPP16StatusNotificationResponse
752 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
753 connectorId
: transactionConnectorId
,
754 status: OCPP16ChargePointStatus
.PREPARING
,
755 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
757 connectorStatus
.status = OCPP16ChargePointStatus
.PREPARING
;
758 if (chargingStation
.isChargingStationAvailable() && connectorStatus
) {
759 // Check if authorized
760 if (chargingStation
.getAuthorizeRemoteTxRequests()) {
761 let authorized
= false;
763 chargingStation
.getLocalAuthListEnabled() &&
764 chargingStation
.hasAuthorizedTags() &&
765 chargingStation
.authorizedTagsCache
767 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
769 .find((value
) => value
=== commandPayload
.idTag
)
771 connectorStatus
.localAuthorizeIdTag
= commandPayload
.idTag
;
772 connectorStatus
.idTagLocalAuthorized
= true;
774 } else if (chargingStation
.getMustAuthorizeAtRemoteStart()) {
775 connectorStatus
.authorizeIdTag
= commandPayload
.idTag
;
776 const authorizeResponse
: OCPP16AuthorizeResponse
=
777 await chargingStation
.ocppRequestService
.requestHandler
<
778 OCPP16AuthorizeRequest
,
779 OCPP16AuthorizeResponse
780 >(chargingStation
, OCPP16RequestCommand
.AUTHORIZE
, {
781 idTag
: commandPayload
.idTag
,
783 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
788 `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
792 // Authorization successful, start transaction
794 this.setRemoteStartTransactionChargingProfile(
796 transactionConnectorId
,
797 commandPayload
.chargingProfile
800 connectorStatus
.transactionRemoteStarted
= true;
803 await chargingStation
.ocppRequestService
.requestHandler
<
804 OCPP16StartTransactionRequest
,
805 OCPP16StartTransactionResponse
806 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
807 connectorId
: transactionConnectorId
,
808 idTag
: commandPayload
.idTag
,
810 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
812 logger
.debug(remoteStartTransactionLogMsg
);
813 return Constants
.OCPP_RESPONSE_ACCEPTED
;
815 return this.notifyRemoteStartTransactionRejected(
817 transactionConnectorId
,
821 return this.notifyRemoteStartTransactionRejected(
823 transactionConnectorId
,
827 return this.notifyRemoteStartTransactionRejected(
829 transactionConnectorId
,
833 // No authorization check required, start transaction
835 this.setRemoteStartTransactionChargingProfile(
837 transactionConnectorId
,
838 commandPayload
.chargingProfile
841 connectorStatus
.transactionRemoteStarted
= true;
844 await chargingStation
.ocppRequestService
.requestHandler
<
845 OCPP16StartTransactionRequest
,
846 OCPP16StartTransactionResponse
847 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
848 connectorId
: transactionConnectorId
,
849 idTag
: commandPayload
.idTag
,
851 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
853 logger
.debug(remoteStartTransactionLogMsg
);
854 return Constants
.OCPP_RESPONSE_ACCEPTED
;
856 return this.notifyRemoteStartTransactionRejected(
858 transactionConnectorId
,
862 return this.notifyRemoteStartTransactionRejected(
864 transactionConnectorId
,
868 return this.notifyRemoteStartTransactionRejected(
870 transactionConnectorId
,
874 return this.notifyRemoteStartTransactionRejected(
876 transactionConnectorId
,
881 private async notifyRemoteStartTransactionRejected(
882 chargingStation
: ChargingStation
,
885 ): Promise
<DefaultResponse
> {
887 chargingStation
.getConnectorStatus(connectorId
).status !== OCPP16ChargePointStatus
.AVAILABLE
889 await chargingStation
.ocppRequestService
.requestHandler
<
890 OCPP16StatusNotificationRequest
,
891 OCPP16StatusNotificationResponse
892 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
894 status: OCPP16ChargePointStatus
.AVAILABLE
,
895 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
897 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
900 chargingStation
.logPrefix() +
901 ' Remote starting transaction REJECTED on connector Id ' +
902 connectorId
.toString() +
905 "', availability '" +
906 chargingStation
.getConnectorStatus(connectorId
).availability
+
908 chargingStation
.getConnectorStatus(connectorId
).status +
911 return Constants
.OCPP_RESPONSE_REJECTED
;
914 private setRemoteStartTransactionChargingProfile(
915 chargingStation
: ChargingStation
,
917 cp
: OCPP16ChargingProfile
919 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
920 chargingStation
.setChargingProfile(connectorId
, cp
);
922 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`,
923 chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
926 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
928 `${chargingStation.logPrefix()} Not allowed to set ${
929 cp.chargingProfilePurpose
930 } charging profile(s) at remote start transaction`
938 private async handleRequestRemoteStopTransaction(
939 chargingStation
: ChargingStation
,
940 commandPayload
: RemoteStopTransactionRequest
941 ): Promise
<DefaultResponse
> {
942 const transactionId
= commandPayload
.transactionId
;
943 for (const connectorId
of chargingStation
.connectors
.keys()) {
946 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
948 await chargingStation
.ocppRequestService
.requestHandler
<
949 OCPP16StatusNotificationRequest
,
950 OCPP16StatusNotificationResponse
951 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
953 status: OCPP16ChargePointStatus
.FINISHING
,
954 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
956 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.FINISHING
;
957 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
959 StopTransactionReason
.REMOTE
961 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
962 return Constants
.OCPP_RESPONSE_ACCEPTED
;
964 return Constants
.OCPP_RESPONSE_REJECTED
;
968 chargingStation
.logPrefix() +
969 ' Trying to remote stop a non existing transaction ' +
970 transactionId
.toString()
972 return Constants
.OCPP_RESPONSE_REJECTED
;
975 private async handleRequestGetDiagnostics(
976 chargingStation
: ChargingStation
,
977 commandPayload
: GetDiagnosticsRequest
978 ): Promise
<GetDiagnosticsResponse
> {
980 !OCPP16ServiceUtils
.checkFeatureProfile(
982 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
983 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
986 return Constants
.OCPP_RESPONSE_EMPTY
;
989 chargingStation
.logPrefix() +
991 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
992 ' request received: %j',
995 const uri
= new URL(commandPayload
.location
);
996 if (uri
.protocol
.startsWith('ftp:')) {
997 let ftpClient
: Client
;
1000 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'))
1001 .filter((file
) => file
.endsWith('.log'))
1002 .map((file
) => path
.join('./', file
));
1003 const diagnosticsArchive
= chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
1004 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
1005 ftpClient
= new Client();
1006 const accessResponse
= await ftpClient
.access({
1008 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1009 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
1010 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
1012 let uploadResponse
: FTPResponse
;
1013 if (accessResponse
.code
=== 220) {
1014 // eslint-disable-next-line @typescript-eslint/no-misused-promises
1015 ftpClient
.trackProgress(async (info
) => {
1017 `${chargingStation.logPrefix()} ${
1019 } bytes transferred from diagnostics archive ${info.name}`
1021 await chargingStation
.ocppRequestService
.requestHandler
<
1022 DiagnosticsStatusNotificationRequest
,
1023 DiagnosticsStatusNotificationResponse
1024 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1025 status: OCPP16DiagnosticsStatus
.Uploading
,
1028 uploadResponse
= await ftpClient
.uploadFrom(
1030 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'),
1033 uri
.pathname
+ diagnosticsArchive
1035 if (uploadResponse
.code
=== 226) {
1036 await chargingStation
.ocppRequestService
.requestHandler
<
1037 DiagnosticsStatusNotificationRequest
,
1038 DiagnosticsStatusNotificationResponse
1039 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1040 status: OCPP16DiagnosticsStatus
.Uploaded
,
1045 return { fileName
: diagnosticsArchive
};
1047 throw new OCPPError(
1048 ErrorType
.GENERIC_ERROR
,
1049 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1050 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1052 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1055 throw new OCPPError(
1056 ErrorType
.GENERIC_ERROR
,
1057 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1058 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1060 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1063 await chargingStation
.ocppRequestService
.requestHandler
<
1064 DiagnosticsStatusNotificationRequest
,
1065 DiagnosticsStatusNotificationResponse
1066 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1067 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1072 return this.handleIncomingRequestError(
1074 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1076 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
1081 `${chargingStation.logPrefix()} Unsupported protocol ${
1083 } to transfer the diagnostic logs archive`
1085 await chargingStation
.ocppRequestService
.requestHandler
<
1086 DiagnosticsStatusNotificationRequest
,
1087 DiagnosticsStatusNotificationResponse
1088 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1089 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1091 return Constants
.OCPP_RESPONSE_EMPTY
;
1095 private handleRequestTriggerMessage(
1096 chargingStation
: ChargingStation
,
1097 commandPayload
: OCPP16TriggerMessageRequest
1098 ): OCPP16TriggerMessageResponse
{
1100 !OCPP16ServiceUtils
.checkFeatureProfile(
1102 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1103 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1106 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1108 // TODO: factor out the check on connector id
1109 if (commandPayload
?.connectorId
< 0) {
1111 `${chargingStation.logPrefix()} ${
1112 OCPP16IncomingRequestCommand.TRIGGER_MESSAGE
1113 } incoming request received with invalid connectorId ${commandPayload.connectorId}`
1115 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1118 switch (commandPayload
.requestedMessage
) {
1119 case MessageTrigger
.BootNotification
:
1121 chargingStation
.ocppRequestService
1122 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1124 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1126 chargePointModel
: chargingStation
.bootNotificationRequest
.chargePointModel
,
1127 chargePointVendor
: chargingStation
.bootNotificationRequest
.chargePointVendor
,
1128 chargeBoxSerialNumber
:
1129 chargingStation
.bootNotificationRequest
.chargeBoxSerialNumber
,
1130 firmwareVersion
: chargingStation
.bootNotificationRequest
.firmwareVersion
,
1131 chargePointSerialNumber
:
1132 chargingStation
.bootNotificationRequest
.chargePointSerialNumber
,
1133 iccid
: chargingStation
.bootNotificationRequest
.iccid
,
1134 imsi
: chargingStation
.bootNotificationRequest
.imsi
,
1135 meterSerialNumber
: chargingStation
.bootNotificationRequest
.meterSerialNumber
,
1136 meterType
: chargingStation
.bootNotificationRequest
.meterType
,
1138 { skipBufferingOnError
: true, triggerMessage
: true }
1141 chargingStation
.bootNotificationResponse
= value
;
1144 /* This is intentional */
1146 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1147 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1148 case MessageTrigger
.Heartbeat
:
1150 chargingStation
.ocppRequestService
1151 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1153 OCPP16RequestCommand
.HEARTBEAT
,
1156 triggerMessage
: true,
1160 /* This is intentional */
1162 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1163 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1164 case MessageTrigger
.StatusNotification
:
1166 if (commandPayload
?.connectorId
) {
1167 chargingStation
.ocppRequestService
1168 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1170 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1172 connectorId
: commandPayload
.connectorId
,
1173 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1174 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
).status,
1177 triggerMessage
: true,
1181 /* This is intentional */
1184 for (const connectorId
of chargingStation
.connectors
.keys()) {
1185 chargingStation
.ocppRequestService
1187 OCPP16StatusNotificationRequest
,
1188 OCPP16StatusNotificationResponse
1191 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1194 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1195 status: chargingStation
.getConnectorStatus(connectorId
).status,
1198 triggerMessage
: true,
1202 /* This is intentional */
1206 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1207 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1209 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1212 return this.handleIncomingRequestError(
1214 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1216 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}