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 Constants from
'../../../utils/Constants';
73 import logger from
'../../../utils/Logger';
74 import Utils from
'../../../utils/Utils';
75 import type ChargingStation from
'../../ChargingStation';
76 import { ChargingStationConfigurationUtils
} from
'../../ChargingStationConfigurationUtils';
77 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
78 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
79 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
81 const moduleName
= 'OCPP16IncomingRequestService';
83 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
84 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
85 private jsonSchemas
: Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
87 public constructor() {
88 if (new.target
?.name
=== moduleName
) {
89 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
92 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
93 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
94 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
95 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
97 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
98 this.handleRequestGetConfiguration
.bind(this),
101 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
102 this.handleRequestChangeConfiguration
.bind(this),
105 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
106 this.handleRequestSetChargingProfile
.bind(this),
109 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
110 this.handleRequestClearChargingProfile
.bind(this),
113 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
114 this.handleRequestChangeAvailability
.bind(this),
117 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
118 this.handleRequestRemoteStartTransaction
.bind(this),
121 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
122 this.handleRequestRemoteStopTransaction
.bind(this),
124 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
125 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
127 this.jsonSchemas
= new Map
<OCPP16IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
129 OCPP16IncomingRequestCommand
.RESET
,
133 path
.dirname(fileURLToPath(import.meta
.url
)),
134 '../../../assets/json-schemas/ocpp/1.6/Reset.json'
138 ) as JSONSchemaType
<ResetRequest
>,
141 OCPP16IncomingRequestCommand
.CLEAR_CACHE
,
145 path
.dirname(fileURLToPath(import.meta
.url
)),
146 '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
150 ) as JSONSchemaType
<OCPP16ClearCacheRequest
>,
153 OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
,
157 path
.dirname(fileURLToPath(import.meta
.url
)),
158 '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
162 ) as JSONSchemaType
<UnlockConnectorRequest
>,
165 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
169 path
.dirname(fileURLToPath(import.meta
.url
)),
170 '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
174 ) as JSONSchemaType
<GetConfigurationRequest
>,
177 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
181 path
.dirname(fileURLToPath(import.meta
.url
)),
182 '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
186 ) as JSONSchemaType
<ChangeConfigurationRequest
>,
189 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
193 path
.dirname(fileURLToPath(import.meta
.url
)),
194 '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
198 ) as JSONSchemaType
<GetDiagnosticsRequest
>,
201 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
205 path
.dirname(fileURLToPath(import.meta
.url
)),
206 '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
210 ) as JSONSchemaType
<SetChargingProfileRequest
>,
213 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
217 path
.dirname(fileURLToPath(import.meta
.url
)),
218 '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
222 ) as JSONSchemaType
<ClearChargingProfileRequest
>,
225 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
229 path
.dirname(fileURLToPath(import.meta
.url
)),
230 '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
234 ) as JSONSchemaType
<ChangeAvailabilityRequest
>,
237 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
241 path
.dirname(fileURLToPath(import.meta
.url
)),
242 '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
246 ) as JSONSchemaType
<RemoteStartTransactionRequest
>,
249 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
253 path
.dirname(fileURLToPath(import.meta
.url
)),
254 '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
258 ) as JSONSchemaType
<RemoteStopTransactionRequest
>,
261 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
265 path
.dirname(fileURLToPath(import.meta
.url
)),
266 '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
270 ) as JSONSchemaType
<OCPP16TriggerMessageRequest
>,
273 this.validatePayload
.bind(this);
276 public async incomingRequestHandler(
277 chargingStation
: ChargingStation
,
279 commandName
: OCPP16IncomingRequestCommand
,
280 commandPayload
: JsonType
282 let response
: JsonType
;
284 chargingStation
.getOcppStrictCompliance() &&
285 chargingStation
.isInPendingState() &&
286 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
287 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
290 ErrorType
.SECURITY_ERROR
,
291 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
295 )} while the charging station is in pending state on the central server`,
301 chargingStation
.isRegistered() ||
302 (!chargingStation
.getOcppStrictCompliance() && chargingStation
.isInUnknownState())
305 this.incomingRequestHandlers
.has(commandName
) &&
306 ChargingStationUtils
.isIncomingRequestCommandSupported(commandName
, chargingStation
)
309 this.validatePayload(chargingStation
, commandName
, commandPayload
);
310 // Call the method to build the response
311 response
= await this.incomingRequestHandlers
.get(commandName
)(
318 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
326 ErrorType
.NOT_IMPLEMENTED
,
327 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
338 ErrorType
.SECURITY_ERROR
,
339 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
343 )} while the charging station is not registered on the central server.`,
348 // Send the built response
349 await chargingStation
.ocppRequestService
.sendResponse(
357 private validatePayload(
358 chargingStation
: ChargingStation
,
359 commandName
: OCPP16IncomingRequestCommand
,
360 commandPayload
: JsonType
362 if (this.jsonSchemas
.has(commandName
)) {
363 return this.validateIncomingRequestPayload(
366 this.jsonSchemas
.get(commandName
),
371 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
376 // Simulate charging station restart
377 private handleRequestReset(
378 chargingStation
: ChargingStation
,
379 commandPayload
: ResetRequest
381 // eslint-disable-next-line @typescript-eslint/no-misused-promises
382 setImmediate(async (): Promise
<void> => {
383 await chargingStation
.reset((commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
);
386 `${chargingStation.logPrefix()} ${
388 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
389 chargingStation.stationInfo.resetTime
392 return Constants
.OCPP_RESPONSE_ACCEPTED
;
395 private handleRequestClearCache(): DefaultResponse
{
396 return Constants
.OCPP_RESPONSE_ACCEPTED
;
399 private async handleRequestUnlockConnector(
400 chargingStation
: ChargingStation
,
401 commandPayload
: UnlockConnectorRequest
402 ): Promise
<UnlockConnectorResponse
> {
403 const connectorId
= commandPayload
.connectorId
;
404 if (connectorId
=== 0) {
406 chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
408 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
410 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
411 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
413 OCPP16StopTransactionReason
.UNLOCK_COMMAND
415 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
416 return Constants
.OCPP_RESPONSE_UNLOCKED
;
418 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
420 await chargingStation
.ocppRequestService
.requestHandler
<
421 OCPP16StatusNotificationRequest
,
422 OCPP16StatusNotificationResponse
423 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
425 status: OCPP16ChargePointStatus
.AVAILABLE
,
426 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
428 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
429 return Constants
.OCPP_RESPONSE_UNLOCKED
;
432 private handleRequestGetConfiguration(
433 chargingStation
: ChargingStation
,
434 commandPayload
: GetConfigurationRequest
435 ): GetConfigurationResponse
{
436 const configurationKey
: OCPPConfigurationKey
[] = [];
437 const unknownKey
: string[] = [];
438 if (Utils
.isEmptyArray(commandPayload
.key
) === true) {
439 for (const configuration
of chargingStation
.ocppConfiguration
.configurationKey
) {
440 if (Utils
.isUndefined(configuration
.visible
) === true) {
441 configuration
.visible
= true;
443 if (configuration
.visible
=== false) {
446 configurationKey
.push({
447 key
: configuration
.key
,
448 readonly: configuration
.readonly,
449 value
: configuration
.value
,
453 for (const key
of commandPayload
.key
) {
454 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
459 if (Utils
.isUndefined(keyFound
.visible
) === true) {
460 keyFound
.visible
= true;
462 if (keyFound
.visible
=== false) {
465 configurationKey
.push({
467 readonly: keyFound
.readonly,
468 value
: keyFound
.value
,
471 unknownKey
.push(key
);
481 private handleRequestChangeConfiguration(
482 chargingStation
: ChargingStation
,
483 commandPayload
: ChangeConfigurationRequest
484 ): ChangeConfigurationResponse
{
485 const keyToChange
= ChargingStationConfigurationUtils
.getConfigurationKey(
491 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
492 } else if (keyToChange
&& keyToChange
.readonly) {
493 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
494 } else if (keyToChange
&& !keyToChange
.readonly) {
495 let valueChanged
= false;
496 if (keyToChange
.value
!== commandPayload
.value
) {
497 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
500 commandPayload
.value
,
505 let triggerHeartbeatRestart
= false;
506 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
507 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
509 OCPP16StandardParametersKey
.HeartbeatInterval
,
512 triggerHeartbeatRestart
= true;
514 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
515 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
517 OCPP16StandardParametersKey
.HeartBeatInterval
,
520 triggerHeartbeatRestart
= true;
522 if (triggerHeartbeatRestart
) {
523 chargingStation
.restartHeartbeat();
525 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
526 chargingStation
.restartWebSocketPing();
528 if (keyToChange
.reboot
) {
529 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
531 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
535 private handleRequestSetChargingProfile(
536 chargingStation
: ChargingStation
,
537 commandPayload
: SetChargingProfileRequest
538 ): SetChargingProfileResponse
{
540 !OCPP16ServiceUtils
.checkFeatureProfile(
542 OCPP16SupportedFeatureProfiles
.SmartCharging
,
543 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
546 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED
;
548 if (!chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
550 `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
551 commandPayload.connectorId
554 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
557 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
558 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
559 commandPayload
.connectorId
!== 0
561 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
564 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
565 ChargingProfilePurposeType
.TX_PROFILE
&&
566 (commandPayload
.connectorId
=== 0 ||
567 chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
===
570 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
572 chargingStation
.setChargingProfile(
573 commandPayload
.connectorId
,
574 commandPayload
.csChargingProfiles
577 `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${
578 commandPayload.connectorId
579 }, dump their stack: %j`,
580 chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
582 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
585 private handleRequestClearChargingProfile(
586 chargingStation
: ChargingStation
,
587 commandPayload
: ClearChargingProfileRequest
588 ): ClearChargingProfileResponse
{
590 !OCPP16ServiceUtils
.checkFeatureProfile(
592 OCPP16SupportedFeatureProfiles
.SmartCharging
,
593 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
596 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
598 const connectorStatus
= chargingStation
.getConnectorStatus(commandPayload
.connectorId
);
599 if (!connectorStatus
) {
601 `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
602 commandPayload.connectorId
605 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
607 if (commandPayload
.connectorId
&& !Utils
.isEmptyArray(connectorStatus
.chargingProfiles
)) {
608 connectorStatus
.chargingProfiles
= [];
610 `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
611 commandPayload.connectorId
612 }, dump their stack: %j`,
613 connectorStatus
.chargingProfiles
615 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
617 if (!commandPayload
.connectorId
) {
618 let clearedCP
= false;
619 for (const connectorId
of chargingStation
.connectors
.keys()) {
620 if (!Utils
.isEmptyArray(chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)) {
622 .getConnectorStatus(connectorId
)
623 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
624 let clearCurrentCP
= false;
625 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
626 clearCurrentCP
= true;
629 !commandPayload
.chargingProfilePurpose
&&
630 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
632 clearCurrentCP
= true;
635 !chargingProfile
.stackLevel
&&
636 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
638 clearCurrentCP
= true;
641 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
642 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
644 clearCurrentCP
= true;
646 if (clearCurrentCP
) {
647 connectorStatus
.chargingProfiles
.splice(index
, 1);
649 `${chargingStation.logPrefix()} Matching charging profile(s) cleared on connector id ${
650 commandPayload.connectorId
651 }, dump their stack: %j`,
652 connectorStatus
.chargingProfiles
660 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
663 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
666 private async handleRequestChangeAvailability(
667 chargingStation
: ChargingStation
,
668 commandPayload
: ChangeAvailabilityRequest
669 ): Promise
<ChangeAvailabilityResponse
> {
670 const connectorId
: number = commandPayload
.connectorId
;
671 if (!chargingStation
.getConnectorStatus(connectorId
)) {
673 `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
675 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
677 const chargePointStatus
: OCPP16ChargePointStatus
=
678 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
679 ? OCPP16ChargePointStatus
.AVAILABLE
680 : OCPP16ChargePointStatus
.UNAVAILABLE
;
681 if (connectorId
=== 0) {
682 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
683 for (const id
of chargingStation
.connectors
.keys()) {
684 if (chargingStation
.getConnectorStatus(id
)?.transactionStarted
=== true) {
685 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
687 chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
688 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
689 await chargingStation
.ocppRequestService
.requestHandler
<
690 OCPP16StatusNotificationRequest
,
691 OCPP16StatusNotificationResponse
692 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
694 status: chargePointStatus
,
695 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
697 chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
703 (chargingStation
.getConnectorStatus(0).availability
=== OCPP16AvailabilityType
.OPERATIVE
||
704 (chargingStation
.getConnectorStatus(0).availability
===
705 OCPP16AvailabilityType
.INOPERATIVE
&&
706 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
708 if (chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
709 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
710 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
712 chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
713 await chargingStation
.ocppRequestService
.requestHandler
<
714 OCPP16StatusNotificationRequest
,
715 OCPP16StatusNotificationResponse
716 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
718 status: chargePointStatus
,
719 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
721 chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
722 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
724 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
727 private async handleRequestRemoteStartTransaction(
728 chargingStation
: ChargingStation
,
729 commandPayload
: RemoteStartTransactionRequest
730 ): Promise
<DefaultResponse
> {
731 const transactionConnectorId
= commandPayload
.connectorId
;
732 const connectorStatus
= chargingStation
.getConnectorStatus(transactionConnectorId
);
733 if (transactionConnectorId
) {
734 const remoteStartTransactionLogMsg
=
735 chargingStation
.logPrefix() +
736 ' Transaction remotely STARTED on ' +
737 chargingStation
.stationInfo
.chargingStationId
+
739 transactionConnectorId
.toString() +
741 commandPayload
.idTag
+
743 await chargingStation
.ocppRequestService
.requestHandler
<
744 OCPP16StatusNotificationRequest
,
745 OCPP16StatusNotificationResponse
746 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
747 connectorId
: transactionConnectorId
,
748 status: OCPP16ChargePointStatus
.PREPARING
,
749 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
751 connectorStatus
.status = OCPP16ChargePointStatus
.PREPARING
;
752 if (chargingStation
.isChargingStationAvailable() && connectorStatus
) {
753 // Check if authorized
754 if (chargingStation
.getAuthorizeRemoteTxRequests()) {
755 let authorized
= false;
757 chargingStation
.getLocalAuthListEnabled() &&
758 chargingStation
.hasAuthorizedTags() &&
759 chargingStation
.authorizedTagsCache
761 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
763 .find((value
) => value
=== commandPayload
.idTag
)
765 connectorStatus
.localAuthorizeIdTag
= commandPayload
.idTag
;
766 connectorStatus
.idTagLocalAuthorized
= true;
768 } else if (chargingStation
.getMustAuthorizeAtRemoteStart()) {
769 connectorStatus
.authorizeIdTag
= commandPayload
.idTag
;
770 const authorizeResponse
: OCPP16AuthorizeResponse
=
771 await chargingStation
.ocppRequestService
.requestHandler
<
772 OCPP16AuthorizeRequest
,
773 OCPP16AuthorizeResponse
774 >(chargingStation
, OCPP16RequestCommand
.AUTHORIZE
, {
775 idTag
: commandPayload
.idTag
,
777 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
782 `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
786 // Authorization successful, start transaction
788 this.setRemoteStartTransactionChargingProfile(
790 transactionConnectorId
,
791 commandPayload
.chargingProfile
794 connectorStatus
.transactionRemoteStarted
= true;
797 await chargingStation
.ocppRequestService
.requestHandler
<
798 OCPP16StartTransactionRequest
,
799 OCPP16StartTransactionResponse
800 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
801 connectorId
: transactionConnectorId
,
802 idTag
: commandPayload
.idTag
,
804 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
806 logger
.debug(remoteStartTransactionLogMsg
);
807 return Constants
.OCPP_RESPONSE_ACCEPTED
;
809 return this.notifyRemoteStartTransactionRejected(
811 transactionConnectorId
,
815 return this.notifyRemoteStartTransactionRejected(
817 transactionConnectorId
,
821 return this.notifyRemoteStartTransactionRejected(
823 transactionConnectorId
,
827 // No authorization check required, start transaction
829 this.setRemoteStartTransactionChargingProfile(
831 transactionConnectorId
,
832 commandPayload
.chargingProfile
835 connectorStatus
.transactionRemoteStarted
= true;
838 await chargingStation
.ocppRequestService
.requestHandler
<
839 OCPP16StartTransactionRequest
,
840 OCPP16StartTransactionResponse
841 >(chargingStation
, OCPP16RequestCommand
.START_TRANSACTION
, {
842 connectorId
: transactionConnectorId
,
843 idTag
: commandPayload
.idTag
,
845 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
847 logger
.debug(remoteStartTransactionLogMsg
);
848 return Constants
.OCPP_RESPONSE_ACCEPTED
;
850 return this.notifyRemoteStartTransactionRejected(
852 transactionConnectorId
,
856 return this.notifyRemoteStartTransactionRejected(
858 transactionConnectorId
,
862 return this.notifyRemoteStartTransactionRejected(
864 transactionConnectorId
,
868 return this.notifyRemoteStartTransactionRejected(
870 transactionConnectorId
,
875 private async notifyRemoteStartTransactionRejected(
876 chargingStation
: ChargingStation
,
879 ): Promise
<DefaultResponse
> {
881 chargingStation
.getConnectorStatus(connectorId
).status !== OCPP16ChargePointStatus
.AVAILABLE
883 await chargingStation
.ocppRequestService
.requestHandler
<
884 OCPP16StatusNotificationRequest
,
885 OCPP16StatusNotificationResponse
886 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
888 status: OCPP16ChargePointStatus
.AVAILABLE
,
889 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
891 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
894 chargingStation
.logPrefix() +
895 ' Remote starting transaction REJECTED on connector Id ' +
896 connectorId
.toString() +
899 "', availability '" +
900 chargingStation
.getConnectorStatus(connectorId
).availability
+
902 chargingStation
.getConnectorStatus(connectorId
).status +
905 return Constants
.OCPP_RESPONSE_REJECTED
;
908 private setRemoteStartTransactionChargingProfile(
909 chargingStation
: ChargingStation
,
911 cp
: OCPP16ChargingProfile
913 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
914 chargingStation
.setChargingProfile(connectorId
, cp
);
916 `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`,
917 chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
920 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
922 `${chargingStation.logPrefix()} Not allowed to set ${
923 cp.chargingProfilePurpose
924 } charging profile(s) at remote start transaction`
932 private async handleRequestRemoteStopTransaction(
933 chargingStation
: ChargingStation
,
934 commandPayload
: RemoteStopTransactionRequest
935 ): Promise
<DefaultResponse
> {
936 const transactionId
= commandPayload
.transactionId
;
937 for (const connectorId
of chargingStation
.connectors
.keys()) {
940 chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
942 await chargingStation
.ocppRequestService
.requestHandler
<
943 OCPP16StatusNotificationRequest
,
944 OCPP16StatusNotificationResponse
945 >(chargingStation
, OCPP16RequestCommand
.STATUS_NOTIFICATION
, {
947 status: OCPP16ChargePointStatus
.FINISHING
,
948 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
950 chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.FINISHING
;
951 const stopResponse
= await chargingStation
.stopTransactionOnConnector(
953 OCPP16StopTransactionReason
.REMOTE
955 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
956 return Constants
.OCPP_RESPONSE_ACCEPTED
;
958 return Constants
.OCPP_RESPONSE_REJECTED
;
962 chargingStation
.logPrefix() +
963 ' Trying to remote stop a non existing transaction ' +
964 transactionId
.toString()
966 return Constants
.OCPP_RESPONSE_REJECTED
;
969 private async handleRequestGetDiagnostics(
970 chargingStation
: ChargingStation
,
971 commandPayload
: GetDiagnosticsRequest
972 ): Promise
<GetDiagnosticsResponse
> {
974 !OCPP16ServiceUtils
.checkFeatureProfile(
976 OCPP16SupportedFeatureProfiles
.FirmwareManagement
,
977 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
980 return Constants
.OCPP_RESPONSE_EMPTY
;
983 chargingStation
.logPrefix() +
985 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
986 ' request received: %j',
989 const uri
= new URL(commandPayload
.location
);
990 if (uri
.protocol
.startsWith('ftp:')) {
991 let ftpClient
: Client
;
994 .readdirSync(path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'))
995 .filter((file
) => file
.endsWith('.log'))
996 .map((file
) => path
.join('./', file
));
997 const diagnosticsArchive
= chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
998 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
999 ftpClient
= new Client();
1000 const accessResponse
= await ftpClient
.access({
1002 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
1003 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
1004 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
1006 let uploadResponse
: FTPResponse
;
1007 if (accessResponse
.code
=== 220) {
1008 // eslint-disable-next-line @typescript-eslint/no-misused-promises
1009 ftpClient
.trackProgress(async (info
) => {
1011 `${chargingStation.logPrefix()} ${
1013 } bytes transferred from diagnostics archive ${info.name}`
1015 await chargingStation
.ocppRequestService
.requestHandler
<
1016 DiagnosticsStatusNotificationRequest
,
1017 DiagnosticsStatusNotificationResponse
1018 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1019 status: OCPP16DiagnosticsStatus
.Uploading
,
1022 uploadResponse
= await ftpClient
.uploadFrom(
1024 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../../../../'),
1027 uri
.pathname
+ diagnosticsArchive
1029 if (uploadResponse
.code
=== 226) {
1030 await chargingStation
.ocppRequestService
.requestHandler
<
1031 DiagnosticsStatusNotificationRequest
,
1032 DiagnosticsStatusNotificationResponse
1033 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1034 status: OCPP16DiagnosticsStatus
.Uploaded
,
1039 return { fileName
: diagnosticsArchive
};
1041 throw new OCPPError(
1042 ErrorType
.GENERIC_ERROR
,
1043 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1044 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1046 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1049 throw new OCPPError(
1050 ErrorType
.GENERIC_ERROR
,
1051 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
1052 uploadResponse?.code && '|' + uploadResponse?.code.toString()
1054 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
1057 await chargingStation
.ocppRequestService
.requestHandler
<
1058 DiagnosticsStatusNotificationRequest
,
1059 DiagnosticsStatusNotificationResponse
1060 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1061 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1066 return this.handleIncomingRequestError(
1068 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
1070 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
1075 `${chargingStation.logPrefix()} Unsupported protocol ${
1077 } to transfer the diagnostic logs archive`
1079 await chargingStation
.ocppRequestService
.requestHandler
<
1080 DiagnosticsStatusNotificationRequest
,
1081 DiagnosticsStatusNotificationResponse
1082 >(chargingStation
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
, {
1083 status: OCPP16DiagnosticsStatus
.UploadFailed
,
1085 return Constants
.OCPP_RESPONSE_EMPTY
;
1089 private handleRequestTriggerMessage(
1090 chargingStation
: ChargingStation
,
1091 commandPayload
: OCPP16TriggerMessageRequest
1092 ): OCPP16TriggerMessageResponse
{
1094 !OCPP16ServiceUtils
.checkFeatureProfile(
1096 OCPP16SupportedFeatureProfiles
.RemoteTrigger
,
1097 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
1100 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1102 // TODO: factor out the check on connector id
1103 if (commandPayload
?.connectorId
< 0) {
1105 `${chargingStation.logPrefix()} ${
1106 OCPP16IncomingRequestCommand.TRIGGER_MESSAGE
1107 } incoming request received with invalid connectorId ${commandPayload.connectorId}`
1109 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
;
1112 switch (commandPayload
.requestedMessage
) {
1113 case MessageTrigger
.BootNotification
:
1115 chargingStation
.ocppRequestService
1116 .requestHandler
<OCPP16BootNotificationRequest
, OCPP16BootNotificationResponse
>(
1118 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
1120 chargePointModel
: chargingStation
.bootNotificationRequest
.chargePointModel
,
1121 chargePointVendor
: chargingStation
.bootNotificationRequest
.chargePointVendor
,
1122 chargeBoxSerialNumber
:
1123 chargingStation
.bootNotificationRequest
.chargeBoxSerialNumber
,
1124 firmwareVersion
: chargingStation
.bootNotificationRequest
.firmwareVersion
,
1125 chargePointSerialNumber
:
1126 chargingStation
.bootNotificationRequest
.chargePointSerialNumber
,
1127 iccid
: chargingStation
.bootNotificationRequest
.iccid
,
1128 imsi
: chargingStation
.bootNotificationRequest
.imsi
,
1129 meterSerialNumber
: chargingStation
.bootNotificationRequest
.meterSerialNumber
,
1130 meterType
: chargingStation
.bootNotificationRequest
.meterType
,
1132 { skipBufferingOnError
: true, triggerMessage
: true }
1135 chargingStation
.bootNotificationResponse
= value
;
1138 /* This is intentional */
1140 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1141 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1142 case MessageTrigger
.Heartbeat
:
1144 chargingStation
.ocppRequestService
1145 .requestHandler
<OCPP16HeartbeatRequest
, OCPP16HeartbeatResponse
>(
1147 OCPP16RequestCommand
.HEARTBEAT
,
1150 triggerMessage
: true,
1154 /* This is intentional */
1156 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1157 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1158 case MessageTrigger
.StatusNotification
:
1160 if (commandPayload
?.connectorId
) {
1161 chargingStation
.ocppRequestService
1162 .requestHandler
<OCPP16StatusNotificationRequest
, OCPP16StatusNotificationResponse
>(
1164 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1166 connectorId
: commandPayload
.connectorId
,
1167 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1168 status: chargingStation
.getConnectorStatus(commandPayload
.connectorId
).status,
1171 triggerMessage
: true,
1175 /* This is intentional */
1178 for (const connectorId
of chargingStation
.connectors
.keys()) {
1179 chargingStation
.ocppRequestService
1181 OCPP16StatusNotificationRequest
,
1182 OCPP16StatusNotificationResponse
1185 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
1188 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
1189 status: chargingStation
.getConnectorStatus(connectorId
).status,
1192 triggerMessage
: true,
1196 /* This is intentional */
1200 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
1201 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
1203 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
1206 return this.handleIncomingRequestError(
1208 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
1210 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}