1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 ChangeAvailabilityRequest
,
5 ChangeConfigurationRequest
,
6 ClearChargingProfileRequest
,
7 GetConfigurationRequest
,
10 OCPP16AvailabilityType
,
11 OCPP16IncomingRequestCommand
,
13 OCPP16TriggerMessageRequest
,
14 RemoteStartTransactionRequest
,
15 RemoteStopTransactionRequest
,
17 SetChargingProfileRequest
,
18 UnlockConnectorRequest
,
19 } from
'../../../types/ocpp/1.6/Requests';
21 ChangeAvailabilityResponse
,
22 ChangeConfigurationResponse
,
23 ClearChargingProfileResponse
,
24 GetConfigurationResponse
,
25 GetDiagnosticsResponse
,
26 OCPP16TriggerMessageResponse
,
27 SetChargingProfileResponse
,
28 UnlockConnectorResponse
,
29 } from
'../../../types/ocpp/1.6/Responses';
31 ChargingProfilePurposeType
,
32 OCPP16ChargingProfile
,
33 } from
'../../../types/ocpp/1.6/ChargingProfile';
34 import { Client
, FTPResponse
} from
'basic-ftp';
36 OCPP16AuthorizationStatus
,
37 OCPP16StopTransactionReason
,
38 } from
'../../../types/ocpp/1.6/Transaction';
40 import type ChargingStation from
'../../ChargingStation';
41 import Constants from
'../../../utils/Constants';
42 import { DefaultResponse
} from
'../../../types/ocpp/Responses';
43 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
44 import { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
45 import { JsonType
} from
'../../../types/JsonType';
46 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
47 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
48 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
49 import { OCPP16StandardParametersKey
} from
'../../../types/ocpp/1.6/Configuration';
50 import { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
51 import OCPPError from
'../../../exception/OCPPError';
52 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
53 import { URL
} from
'url';
54 import Utils from
'../../../utils/Utils';
56 import logger from
'../../../utils/Logger';
57 import path from
'path';
58 import tar from
'tar';
60 const moduleName
= 'OCPP16IncomingRequestService';
62 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
63 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
65 public constructor(chargingStation
: ChargingStation
) {
66 if (new.target
?.name
=== moduleName
) {
67 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
69 super(chargingStation
);
70 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
71 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
72 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
73 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
75 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
76 this.handleRequestGetConfiguration
.bind(this),
79 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
80 this.handleRequestChangeConfiguration
.bind(this),
83 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
84 this.handleRequestSetChargingProfile
.bind(this),
87 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
88 this.handleRequestClearChargingProfile
.bind(this),
91 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
92 this.handleRequestChangeAvailability
.bind(this),
95 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
96 this.handleRequestRemoteStartTransaction
.bind(this),
99 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
100 this.handleRequestRemoteStopTransaction
.bind(this),
102 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
103 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
107 public async handleRequest(
109 commandName
: OCPP16IncomingRequestCommand
,
110 commandPayload
: JsonType
112 let result
: JsonType
;
114 this.chargingStation
.getOcppStrictCompliance() &&
115 this.chargingStation
.isInPendingState() &&
116 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
117 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
120 ErrorType
.SECURITY_ERROR
,
121 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
125 )} while the charging station is in pending state on the central server`,
130 this.chargingStation
.isRegistered() ||
131 (!this.chargingStation
.getOcppStrictCompliance() && this.chargingStation
.isInUnknownState())
133 if (this.incomingRequestHandlers
.has(commandName
)) {
135 // Call the method to build the result
136 result
= await this.incomingRequestHandlers
.get(commandName
)(commandPayload
);
139 logger
.error(this.chargingStation
.logPrefix() + ' Handle request error: %j', error
);
145 ErrorType
.NOT_IMPLEMENTED
,
146 `${commandName} is not implemented to handle request payload ${JSON.stringify(
156 ErrorType
.SECURITY_ERROR
,
157 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
161 )} while the charging station is not registered on the central server.`,
165 // Send the built result
166 await this.chargingStation
.ocppRequestService
.sendResult(messageId
, result
, commandName
);
169 // Simulate charging station restart
170 private handleRequestReset(commandPayload
: ResetRequest
): DefaultResponse
{
171 // eslint-disable-next-line @typescript-eslint/no-misused-promises
172 setImmediate(async (): Promise
<void> => {
173 await this.chargingStation
.stop(
174 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
176 await Utils
.sleep(this.chargingStation
.stationInfo
.resetTime
);
177 this.chargingStation
.start();
180 `${this.chargingStation.logPrefix()} ${
182 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
183 this.chargingStation.stationInfo.resetTime
186 return Constants
.OCPP_RESPONSE_ACCEPTED
;
189 private handleRequestClearCache(): DefaultResponse
{
190 return Constants
.OCPP_RESPONSE_ACCEPTED
;
193 private async handleRequestUnlockConnector(
194 commandPayload
: UnlockConnectorRequest
195 ): Promise
<UnlockConnectorResponse
> {
196 const connectorId
= commandPayload
.connectorId
;
197 if (connectorId
=== 0) {
199 this.chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
201 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
203 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
204 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
).transactionId
;
205 const stopResponse
= await this.chargingStation
.ocppRequestService
.sendStopTransaction(
207 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
208 this.chargingStation
.getTransactionIdTag(transactionId
),
209 OCPP16StopTransactionReason
.UNLOCK_COMMAND
211 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
212 return Constants
.OCPP_RESPONSE_UNLOCKED
;
214 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
216 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
217 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
220 status: OCPP16ChargePointStatus
.AVAILABLE
,
221 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
224 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
225 return Constants
.OCPP_RESPONSE_UNLOCKED
;
228 private handleRequestGetConfiguration(
229 commandPayload
: GetConfigurationRequest
230 ): GetConfigurationResponse
{
231 const configurationKey
: OCPPConfigurationKey
[] = [];
232 const unknownKey
: string[] = [];
233 if (Utils
.isEmptyArray(commandPayload
.key
)) {
234 for (const configuration
of this.chargingStation
.configuration
.configurationKey
) {
235 if (Utils
.isUndefined(configuration
.visible
)) {
236 configuration
.visible
= true;
238 if (!configuration
.visible
) {
241 configurationKey
.push({
242 key
: configuration
.key
,
243 readonly: configuration
.readonly,
244 value
: configuration
.value
,
248 for (const key
of commandPayload
.key
) {
249 const keyFound
= this.chargingStation
.getConfigurationKey(key
);
251 if (Utils
.isUndefined(keyFound
.visible
)) {
252 keyFound
.visible
= true;
254 if (!keyFound
.visible
) {
257 configurationKey
.push({
259 readonly: keyFound
.readonly,
260 value
: keyFound
.value
,
263 unknownKey
.push(key
);
273 private handleRequestChangeConfiguration(
274 commandPayload
: ChangeConfigurationRequest
275 ): ChangeConfigurationResponse
{
276 // JSON request fields type sanity check
277 if (!Utils
.isString(commandPayload
.key
)) {
279 `${this.chargingStation.logPrefix()} ${
280 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
281 } request key field is not a string:`,
285 if (!Utils
.isString(commandPayload
.value
)) {
287 `${this.chargingStation.logPrefix()} ${
288 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
289 } request value field is not a string:`,
293 const keyToChange
= this.chargingStation
.getConfigurationKey(commandPayload
.key
, true);
295 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
296 } else if (keyToChange
&& keyToChange
.readonly) {
297 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
298 } else if (keyToChange
&& !keyToChange
.readonly) {
299 const keyIndex
= this.chargingStation
.configuration
.configurationKey
.indexOf(keyToChange
);
300 let valueChanged
= false;
302 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
!== commandPayload
.value
304 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
= commandPayload
.value
;
307 let triggerHeartbeatRestart
= false;
308 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
309 this.chargingStation
.setConfigurationKeyValue(
310 OCPP16StandardParametersKey
.HeartbeatInterval
,
313 triggerHeartbeatRestart
= true;
315 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
316 this.chargingStation
.setConfigurationKeyValue(
317 OCPP16StandardParametersKey
.HeartBeatInterval
,
320 triggerHeartbeatRestart
= true;
322 if (triggerHeartbeatRestart
) {
323 this.chargingStation
.restartHeartbeat();
325 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
326 this.chargingStation
.restartWebSocketPing();
328 if (keyToChange
.reboot
) {
329 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
331 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
335 private handleRequestSetChargingProfile(
336 commandPayload
: SetChargingProfileRequest
337 ): SetChargingProfileResponse
{
338 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
340 `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
341 commandPayload.connectorId
344 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
347 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
348 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
349 commandPayload
.connectorId
!== 0
351 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
354 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
355 ChargingProfilePurposeType
.TX_PROFILE
&&
356 (commandPayload
.connectorId
=== 0 ||
357 !this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
)
359 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
361 this.chargingStation
.setChargingProfile(
362 commandPayload
.connectorId
,
363 commandPayload
.csChargingProfiles
366 `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`,
367 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
369 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
372 private handleRequestClearChargingProfile(
373 commandPayload
: ClearChargingProfileRequest
374 ): ClearChargingProfileResponse
{
375 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
377 `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
378 commandPayload.connectorId
381 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
384 commandPayload
.connectorId
&&
386 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
389 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
391 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
392 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
394 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
396 if (!commandPayload
.connectorId
) {
397 let clearedCP
= false;
398 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
400 !Utils
.isEmptyArray(this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)
403 .getConnectorStatus(connectorId
)
404 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
405 let clearCurrentCP
= false;
406 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
407 clearCurrentCP
= true;
410 !commandPayload
.chargingProfilePurpose
&&
411 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
413 clearCurrentCP
= true;
416 !chargingProfile
.stackLevel
&&
417 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
419 clearCurrentCP
= true;
422 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
423 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
425 clearCurrentCP
= true;
427 if (clearCurrentCP
) {
428 this.chargingStation
.getConnectorStatus(
429 commandPayload
.connectorId
430 ).chargingProfiles
[index
] = {} as OCPP16ChargingProfile
;
432 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
433 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
442 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
445 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
448 private async handleRequestChangeAvailability(
449 commandPayload
: ChangeAvailabilityRequest
450 ): Promise
<ChangeAvailabilityResponse
> {
451 const connectorId
: number = commandPayload
.connectorId
;
452 if (!this.chargingStation
.getConnectorStatus(connectorId
)) {
454 `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
456 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
458 const chargePointStatus
: OCPP16ChargePointStatus
=
459 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
460 ? OCPP16ChargePointStatus
.AVAILABLE
461 : OCPP16ChargePointStatus
.UNAVAILABLE
;
462 if (connectorId
=== 0) {
463 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
464 for (const id
of this.chargingStation
.connectors
.keys()) {
465 if (this.chargingStation
.getConnectorStatus(id
)?.transactionStarted
) {
466 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
468 this.chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
469 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
470 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
471 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
474 status: chargePointStatus
,
475 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
478 this.chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
484 (this.chargingStation
.getConnectorStatus(0).availability
===
485 OCPP16AvailabilityType
.OPERATIVE
||
486 (this.chargingStation
.getConnectorStatus(0).availability
===
487 OCPP16AvailabilityType
.INOPERATIVE
&&
488 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
490 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
491 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
492 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
494 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
495 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
496 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
497 { connectorId
, status: chargePointStatus
, errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
}
499 this.chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
500 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
502 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
505 private async handleRequestRemoteStartTransaction(
506 commandPayload
: RemoteStartTransactionRequest
507 ): Promise
<DefaultResponse
> {
508 const transactionConnectorId
: number = commandPayload
.connectorId
;
509 if (transactionConnectorId
) {
510 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
511 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
513 connectorId
: transactionConnectorId
,
514 status: OCPP16ChargePointStatus
.PREPARING
,
515 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
518 this.chargingStation
.getConnectorStatus(transactionConnectorId
).status =
519 OCPP16ChargePointStatus
.PREPARING
;
521 this.chargingStation
.isChargingStationAvailable() &&
522 this.chargingStation
.isConnectorAvailable(transactionConnectorId
)
524 // Check if authorized
525 if (this.chargingStation
.getAuthorizeRemoteTxRequests()) {
526 let authorized
= false;
528 this.chargingStation
.getLocalAuthListEnabled() &&
529 this.chargingStation
.hasAuthorizedTags() &&
530 this.chargingStation
.authorizedTags
.find((value
) => value
=== commandPayload
.idTag
)
532 this.chargingStation
.getConnectorStatus(transactionConnectorId
).localAuthorizeIdTag
=
533 commandPayload
.idTag
;
534 this.chargingStation
.getConnectorStatus(transactionConnectorId
).idTagLocalAuthorized
=
537 } else if (this.chargingStation
.getMayAuthorizeAtRemoteStart()) {
538 const authorizeResponse
= await this.chargingStation
.ocppRequestService
.sendAuthorize(
539 transactionConnectorId
,
542 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
547 `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
551 // Authorization successful, start transaction
553 this.setRemoteStartTransactionChargingProfile(
554 transactionConnectorId
,
555 commandPayload
.chargingProfile
558 this.chargingStation
.getConnectorStatus(
559 transactionConnectorId
560 ).transactionRemoteStarted
= true;
563 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
564 transactionConnectorId
,
567 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
570 this.chargingStation
.logPrefix() +
571 ' Transaction remotely STARTED on ' +
572 this.chargingStation
.stationInfo
.chargingStationId
+
574 transactionConnectorId
.toString() +
578 return Constants
.OCPP_RESPONSE_ACCEPTED
;
580 return this.notifyRemoteStartTransactionRejected(
581 transactionConnectorId
,
585 return this.notifyRemoteStartTransactionRejected(
586 transactionConnectorId
,
590 return this.notifyRemoteStartTransactionRejected(
591 transactionConnectorId
,
595 // No authorization check required, start transaction
597 this.setRemoteStartTransactionChargingProfile(
598 transactionConnectorId
,
599 commandPayload
.chargingProfile
602 this.chargingStation
.getConnectorStatus(transactionConnectorId
).transactionRemoteStarted
=
606 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
607 transactionConnectorId
,
610 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
613 this.chargingStation
.logPrefix() +
614 ' Transaction remotely STARTED on ' +
615 this.chargingStation
.stationInfo
.chargingStationId
+
617 transactionConnectorId
.toString() +
621 return Constants
.OCPP_RESPONSE_ACCEPTED
;
623 return this.notifyRemoteStartTransactionRejected(
624 transactionConnectorId
,
628 return this.notifyRemoteStartTransactionRejected(
629 transactionConnectorId
,
633 return this.notifyRemoteStartTransactionRejected(
634 transactionConnectorId
,
638 return this.notifyRemoteStartTransactionRejected(transactionConnectorId
, commandPayload
.idTag
);
641 private async notifyRemoteStartTransactionRejected(
644 ): Promise
<DefaultResponse
> {
646 this.chargingStation
.getConnectorStatus(connectorId
).status !==
647 OCPP16ChargePointStatus
.AVAILABLE
649 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
650 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
653 status: OCPP16ChargePointStatus
.AVAILABLE
,
654 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
657 this.chargingStation
.getConnectorStatus(connectorId
).status =
658 OCPP16ChargePointStatus
.AVAILABLE
;
661 this.chargingStation
.logPrefix() +
662 ' Remote starting transaction REJECTED on connector Id ' +
663 connectorId
.toString() +
667 this.chargingStation
.getConnectorStatus(connectorId
).availability
+
669 this.chargingStation
.getConnectorStatus(connectorId
).status
671 return Constants
.OCPP_RESPONSE_REJECTED
;
674 private setRemoteStartTransactionChargingProfile(
676 cp
: OCPP16ChargingProfile
678 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
679 this.chargingStation
.setChargingProfile(connectorId
, cp
);
681 `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`,
682 this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
685 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
687 `${this.chargingStation.logPrefix()} Not allowed to set ${
688 cp.chargingProfilePurpose
689 } charging profile(s) at remote start transaction`
697 private async handleRequestRemoteStopTransaction(
698 commandPayload
: RemoteStopTransactionRequest
699 ): Promise
<DefaultResponse
> {
700 const transactionId
= commandPayload
.transactionId
;
701 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
704 this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
706 await this.chargingStation
.ocppRequestService
.sendMessageHandler(
707 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
710 status: OCPP16ChargePointStatus
.FINISHING
,
711 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
714 this.chargingStation
.getConnectorStatus(connectorId
).status =
715 OCPP16ChargePointStatus
.FINISHING
;
716 await this.chargingStation
.ocppRequestService
.sendStopTransaction(
718 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
719 this.chargingStation
.getTransactionIdTag(transactionId
)
721 return Constants
.OCPP_RESPONSE_ACCEPTED
;
725 this.chargingStation
.logPrefix() +
726 ' Trying to remote stop a non existing transaction ' +
727 transactionId
.toString()
729 return Constants
.OCPP_RESPONSE_REJECTED
;
732 private async handleRequestGetDiagnostics(
733 commandPayload
: GetDiagnosticsRequest
734 ): Promise
<GetDiagnosticsResponse
> {
736 this.chargingStation
.logPrefix() +
738 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
739 ' request received: %j',
742 const uri
= new URL(commandPayload
.location
);
743 if (uri
.protocol
.startsWith('ftp:')) {
744 let ftpClient
: Client
;
747 .readdirSync(path
.resolve(__dirname
, '../../../../'))
748 .filter((file
) => file
.endsWith('.log'))
749 .map((file
) => path
.join('./', file
));
750 const diagnosticsArchive
=
751 this.chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
752 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
753 ftpClient
= new Client();
754 const accessResponse
= await ftpClient
.access({
756 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
757 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
758 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
760 let uploadResponse
: FTPResponse
;
761 if (accessResponse
.code
=== 220) {
762 // eslint-disable-next-line @typescript-eslint/no-misused-promises
763 ftpClient
.trackProgress(async (info
) => {
765 `${this.chargingStation.logPrefix()} ${
767 } bytes transferred from diagnostics archive ${info.name}`
769 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
770 OCPP16DiagnosticsStatus
.Uploading
773 uploadResponse
= await ftpClient
.uploadFrom(
774 path
.join(path
.resolve(__dirname
, '../../../../'), diagnosticsArchive
),
775 uri
.pathname
+ diagnosticsArchive
777 if (uploadResponse
.code
=== 226) {
778 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
779 OCPP16DiagnosticsStatus
.Uploaded
784 return { fileName
: diagnosticsArchive
};
787 ErrorType
.GENERIC_ERROR
,
788 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
789 uploadResponse?.code && '|' + uploadResponse?.code.toString()
791 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
795 ErrorType
.GENERIC_ERROR
,
796 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
797 uploadResponse?.code && '|' + uploadResponse?.code.toString()
799 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
802 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
803 OCPP16DiagnosticsStatus
.UploadFailed
808 return this.handleIncomingRequestError(
809 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
811 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
816 `${this.chargingStation.logPrefix()} Unsupported protocol ${
818 } to transfer the diagnostic logs archive`
820 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
821 OCPP16DiagnosticsStatus
.UploadFailed
823 return Constants
.OCPP_RESPONSE_EMPTY
;
827 private handleRequestTriggerMessage(
828 commandPayload
: OCPP16TriggerMessageRequest
829 ): OCPP16TriggerMessageResponse
{
831 switch (commandPayload
.requestedMessage
) {
832 case MessageTrigger
.BootNotification
:
834 this.chargingStation
.ocppRequestService
836 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
839 this.chargingStation
.getBootNotificationRequest().chargePointModel
,
841 this.chargingStation
.getBootNotificationRequest().chargePointVendor
,
842 chargeBoxSerialNumber
:
843 this.chargingStation
.getBootNotificationRequest().chargeBoxSerialNumber
,
845 this.chargingStation
.getBootNotificationRequest().firmwareVersion
,
846 chargePointSerialNumber
:
847 this.chargingStation
.getBootNotificationRequest().chargePointSerialNumber
,
848 iccid
: this.chargingStation
.getBootNotificationRequest().iccid
,
849 imsi
: this.chargingStation
.getBootNotificationRequest().imsi
,
851 this.chargingStation
.getBootNotificationRequest().meterSerialNumber
,
852 meterType
: this.chargingStation
.getBootNotificationRequest().meterType
,
854 { skipBufferingOnError
: true, triggerMessage
: true }
857 /* This is intentional */
859 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
860 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
861 case MessageTrigger
.Heartbeat
:
863 this.chargingStation
.ocppRequestService
864 .sendMessageHandler(OCPP16RequestCommand
.HEARTBEAT
, null, { triggerMessage
: true })
866 /* This is intentional */
868 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
869 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
871 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
874 return this.handleIncomingRequestError(
875 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
877 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}