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 { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
47 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
48 import { OCPP16StandardParametersKey
} from
'../../../types/ocpp/1.6/Configuration';
49 import { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
50 import OCPPError from
'../../../exception/OCPPError';
51 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
52 import { URL
} from
'url';
53 import Utils from
'../../../utils/Utils';
55 import logger from
'../../../utils/Logger';
56 import path from
'path';
57 import tar from
'tar';
59 const moduleName
= 'OCPP16IncomingRequestService';
61 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
62 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
64 public constructor(chargingStation
: ChargingStation
) {
65 if (new.target
?.name
=== moduleName
) {
66 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
68 super(chargingStation
);
69 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
70 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
71 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
72 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
74 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
75 this.handleRequestGetConfiguration
.bind(this),
78 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
79 this.handleRequestChangeConfiguration
.bind(this),
82 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
83 this.handleRequestSetChargingProfile
.bind(this),
86 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
87 this.handleRequestClearChargingProfile
.bind(this),
90 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
91 this.handleRequestChangeAvailability
.bind(this),
94 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
95 this.handleRequestRemoteStartTransaction
.bind(this),
98 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
99 this.handleRequestRemoteStopTransaction
.bind(this),
101 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
102 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
106 public async handleRequest(
108 commandName
: OCPP16IncomingRequestCommand
,
109 commandPayload
: JsonType
111 let result
: JsonType
;
113 this.chargingStation
.getOcppStrictCompliance() &&
114 this.chargingStation
.isInPendingState() &&
115 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
116 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
119 ErrorType
.SECURITY_ERROR
,
120 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
124 )} while the charging station is in pending state on the central server`,
129 this.chargingStation
.isRegistered() ||
130 (!this.chargingStation
.getOcppStrictCompliance() && this.chargingStation
.isInUnknownState())
132 if (this.incomingRequestHandlers
.has(commandName
)) {
134 // Call the method to build the result
135 result
= await this.incomingRequestHandlers
.get(commandName
)(commandPayload
);
138 logger
.error(this.chargingStation
.logPrefix() + ' Handle request error: %j', error
);
144 ErrorType
.NOT_IMPLEMENTED
,
145 `${commandName} is not implemented to handle request payload ${JSON.stringify(
155 ErrorType
.SECURITY_ERROR
,
156 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
160 )} while the charging station is not registered on the central server.`,
164 // Send the built result
165 await this.chargingStation
.ocppRequestService
.sendResult(messageId
, result
, commandName
);
168 // Simulate charging station restart
169 private handleRequestReset(commandPayload
: ResetRequest
): DefaultResponse
{
170 // eslint-disable-next-line @typescript-eslint/no-misused-promises
171 setImmediate(async (): Promise
<void> => {
172 await this.chargingStation
.stop(
173 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
175 await Utils
.sleep(this.chargingStation
.stationInfo
.resetTime
);
176 this.chargingStation
.start();
179 `${this.chargingStation.logPrefix()} ${
181 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
182 this.chargingStation.stationInfo.resetTime
185 return Constants
.OCPP_RESPONSE_ACCEPTED
;
188 private handleRequestClearCache(): DefaultResponse
{
189 return Constants
.OCPP_RESPONSE_ACCEPTED
;
192 private async handleRequestUnlockConnector(
193 commandPayload
: UnlockConnectorRequest
194 ): Promise
<UnlockConnectorResponse
> {
195 const connectorId
= commandPayload
.connectorId
;
196 if (connectorId
=== 0) {
198 this.chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
200 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
202 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
203 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
).transactionId
;
204 const stopResponse
= await this.chargingStation
.ocppRequestService
.sendStopTransaction(
206 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
207 this.chargingStation
.getTransactionIdTag(transactionId
),
208 OCPP16StopTransactionReason
.UNLOCK_COMMAND
210 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
211 return Constants
.OCPP_RESPONSE_UNLOCKED
;
213 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
215 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
217 OCPP16ChargePointStatus
.AVAILABLE
219 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
220 return Constants
.OCPP_RESPONSE_UNLOCKED
;
223 private handleRequestGetConfiguration(
224 commandPayload
: GetConfigurationRequest
225 ): GetConfigurationResponse
{
226 const configurationKey
: OCPPConfigurationKey
[] = [];
227 const unknownKey
: string[] = [];
228 if (Utils
.isEmptyArray(commandPayload
.key
)) {
229 for (const configuration
of this.chargingStation
.configuration
.configurationKey
) {
230 if (Utils
.isUndefined(configuration
.visible
)) {
231 configuration
.visible
= true;
233 if (!configuration
.visible
) {
236 configurationKey
.push({
237 key
: configuration
.key
,
238 readonly: configuration
.readonly,
239 value
: configuration
.value
,
243 for (const key
of commandPayload
.key
) {
244 const keyFound
= this.chargingStation
.getConfigurationKey(key
);
246 if (Utils
.isUndefined(keyFound
.visible
)) {
247 keyFound
.visible
= true;
249 if (!keyFound
.visible
) {
252 configurationKey
.push({
254 readonly: keyFound
.readonly,
255 value
: keyFound
.value
,
258 unknownKey
.push(key
);
268 private handleRequestChangeConfiguration(
269 commandPayload
: ChangeConfigurationRequest
270 ): ChangeConfigurationResponse
{
271 // JSON request fields type sanity check
272 if (!Utils
.isString(commandPayload
.key
)) {
274 `${this.chargingStation.logPrefix()} ${
275 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
276 } request key field is not a string:`,
280 if (!Utils
.isString(commandPayload
.value
)) {
282 `${this.chargingStation.logPrefix()} ${
283 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
284 } request value field is not a string:`,
288 const keyToChange
= this.chargingStation
.getConfigurationKey(commandPayload
.key
, true);
290 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
291 } else if (keyToChange
&& keyToChange
.readonly) {
292 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
293 } else if (keyToChange
&& !keyToChange
.readonly) {
294 const keyIndex
= this.chargingStation
.configuration
.configurationKey
.indexOf(keyToChange
);
295 let valueChanged
= false;
297 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
!== commandPayload
.value
299 this.chargingStation
.configuration
.configurationKey
[keyIndex
].value
= commandPayload
.value
;
302 let triggerHeartbeatRestart
= false;
303 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
304 this.chargingStation
.setConfigurationKeyValue(
305 OCPP16StandardParametersKey
.HeartbeatInterval
,
308 triggerHeartbeatRestart
= true;
310 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
311 this.chargingStation
.setConfigurationKeyValue(
312 OCPP16StandardParametersKey
.HeartBeatInterval
,
315 triggerHeartbeatRestart
= true;
317 if (triggerHeartbeatRestart
) {
318 this.chargingStation
.restartHeartbeat();
320 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
321 this.chargingStation
.restartWebSocketPing();
323 if (keyToChange
.reboot
) {
324 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
326 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
330 private handleRequestSetChargingProfile(
331 commandPayload
: SetChargingProfileRequest
332 ): SetChargingProfileResponse
{
333 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
335 `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
336 commandPayload.connectorId
339 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
342 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
343 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
344 commandPayload
.connectorId
!== 0
346 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
349 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
350 ChargingProfilePurposeType
.TX_PROFILE
&&
351 (commandPayload
.connectorId
=== 0 ||
352 !this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
)
354 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
356 this.chargingStation
.setChargingProfile(
357 commandPayload
.connectorId
,
358 commandPayload
.csChargingProfiles
361 `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`,
362 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
364 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
367 private handleRequestClearChargingProfile(
368 commandPayload
: ClearChargingProfileRequest
369 ): ClearChargingProfileResponse
{
370 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
372 `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
373 commandPayload.connectorId
376 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
379 commandPayload
.connectorId
&&
381 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
384 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
= [];
386 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
387 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
389 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
391 if (!commandPayload
.connectorId
) {
392 let clearedCP
= false;
393 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
395 !Utils
.isEmptyArray(this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)
398 .getConnectorStatus(connectorId
)
399 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
400 let clearCurrentCP
= false;
401 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
402 clearCurrentCP
= true;
405 !commandPayload
.chargingProfilePurpose
&&
406 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
408 clearCurrentCP
= true;
411 !chargingProfile
.stackLevel
&&
412 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
414 clearCurrentCP
= true;
417 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
418 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
420 clearCurrentCP
= true;
422 if (clearCurrentCP
) {
423 this.chargingStation
.getConnectorStatus(
424 commandPayload
.connectorId
425 ).chargingProfiles
[index
] = {} as OCPP16ChargingProfile
;
427 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
428 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)
437 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
440 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
443 private async handleRequestChangeAvailability(
444 commandPayload
: ChangeAvailabilityRequest
445 ): Promise
<ChangeAvailabilityResponse
> {
446 const connectorId
: number = commandPayload
.connectorId
;
447 if (!this.chargingStation
.getConnectorStatus(connectorId
)) {
449 `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
451 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
453 const chargePointStatus
: OCPP16ChargePointStatus
=
454 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
455 ? OCPP16ChargePointStatus
.AVAILABLE
456 : OCPP16ChargePointStatus
.UNAVAILABLE
;
457 if (connectorId
=== 0) {
458 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
459 for (const id
of this.chargingStation
.connectors
.keys()) {
460 if (this.chargingStation
.getConnectorStatus(id
)?.transactionStarted
) {
461 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
463 this.chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
464 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
465 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
469 this.chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
475 (this.chargingStation
.getConnectorStatus(0).availability
===
476 OCPP16AvailabilityType
.OPERATIVE
||
477 (this.chargingStation
.getConnectorStatus(0).availability
===
478 OCPP16AvailabilityType
.INOPERATIVE
&&
479 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
481 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
482 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
483 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
485 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
486 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
490 this.chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
491 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
493 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
496 private async handleRequestRemoteStartTransaction(
497 commandPayload
: RemoteStartTransactionRequest
498 ): Promise
<DefaultResponse
> {
499 const transactionConnectorId
: number = commandPayload
.connectorId
;
500 if (transactionConnectorId
) {
501 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
502 transactionConnectorId
,
503 OCPP16ChargePointStatus
.PREPARING
505 this.chargingStation
.getConnectorStatus(transactionConnectorId
).status =
506 OCPP16ChargePointStatus
.PREPARING
;
508 this.chargingStation
.isChargingStationAvailable() &&
509 this.chargingStation
.isConnectorAvailable(transactionConnectorId
)
511 // Check if authorized
512 if (this.chargingStation
.getAuthorizeRemoteTxRequests()) {
513 let authorized
= false;
515 this.chargingStation
.getLocalAuthListEnabled() &&
516 this.chargingStation
.hasAuthorizedTags() &&
517 this.chargingStation
.authorizedTags
.find((value
) => value
=== commandPayload
.idTag
)
519 this.chargingStation
.getConnectorStatus(transactionConnectorId
).localAuthorizeIdTag
=
520 commandPayload
.idTag
;
521 this.chargingStation
.getConnectorStatus(transactionConnectorId
).idTagLocalAuthorized
=
524 } else if (this.chargingStation
.getMayAuthorizeAtRemoteStart()) {
525 const authorizeResponse
= await this.chargingStation
.ocppRequestService
.sendAuthorize(
526 transactionConnectorId
,
529 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
534 `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
538 // Authorization successful, start transaction
540 this.setRemoteStartTransactionChargingProfile(
541 transactionConnectorId
,
542 commandPayload
.chargingProfile
545 this.chargingStation
.getConnectorStatus(
546 transactionConnectorId
547 ).transactionRemoteStarted
= true;
550 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
551 transactionConnectorId
,
554 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
557 this.chargingStation
.logPrefix() +
558 ' Transaction remotely STARTED on ' +
559 this.chargingStation
.stationInfo
.chargingStationId
+
561 transactionConnectorId
.toString() +
565 return Constants
.OCPP_RESPONSE_ACCEPTED
;
567 return this.notifyRemoteStartTransactionRejected(
568 transactionConnectorId
,
572 return this.notifyRemoteStartTransactionRejected(
573 transactionConnectorId
,
577 return this.notifyRemoteStartTransactionRejected(
578 transactionConnectorId
,
582 // No authorization check required, start transaction
584 this.setRemoteStartTransactionChargingProfile(
585 transactionConnectorId
,
586 commandPayload
.chargingProfile
589 this.chargingStation
.getConnectorStatus(transactionConnectorId
).transactionRemoteStarted
=
593 await this.chargingStation
.ocppRequestService
.sendStartTransaction(
594 transactionConnectorId
,
597 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
600 this.chargingStation
.logPrefix() +
601 ' Transaction remotely STARTED on ' +
602 this.chargingStation
.stationInfo
.chargingStationId
+
604 transactionConnectorId
.toString() +
608 return Constants
.OCPP_RESPONSE_ACCEPTED
;
610 return this.notifyRemoteStartTransactionRejected(
611 transactionConnectorId
,
615 return this.notifyRemoteStartTransactionRejected(
616 transactionConnectorId
,
620 return this.notifyRemoteStartTransactionRejected(
621 transactionConnectorId
,
625 return this.notifyRemoteStartTransactionRejected(transactionConnectorId
, commandPayload
.idTag
);
628 private async notifyRemoteStartTransactionRejected(
631 ): Promise
<DefaultResponse
> {
633 this.chargingStation
.getConnectorStatus(connectorId
).status !==
634 OCPP16ChargePointStatus
.AVAILABLE
636 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
638 OCPP16ChargePointStatus
.AVAILABLE
640 this.chargingStation
.getConnectorStatus(connectorId
).status =
641 OCPP16ChargePointStatus
.AVAILABLE
;
644 this.chargingStation
.logPrefix() +
645 ' Remote starting transaction REJECTED on connector Id ' +
646 connectorId
.toString() +
650 this.chargingStation
.getConnectorStatus(connectorId
).availability
+
652 this.chargingStation
.getConnectorStatus(connectorId
).status
654 return Constants
.OCPP_RESPONSE_REJECTED
;
657 private setRemoteStartTransactionChargingProfile(
659 cp
: OCPP16ChargingProfile
661 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
662 this.chargingStation
.setChargingProfile(connectorId
, cp
);
664 `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`,
665 this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
668 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
670 `${this.chargingStation.logPrefix()} Not allowed to set ${
671 cp.chargingProfilePurpose
672 } charging profile(s) at remote start transaction`
680 private async handleRequestRemoteStopTransaction(
681 commandPayload
: RemoteStopTransactionRequest
682 ): Promise
<DefaultResponse
> {
683 const transactionId
= commandPayload
.transactionId
;
684 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
687 this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
689 await this.chargingStation
.ocppRequestService
.sendStatusNotification(
691 OCPP16ChargePointStatus
.FINISHING
693 this.chargingStation
.getConnectorStatus(connectorId
).status =
694 OCPP16ChargePointStatus
.FINISHING
;
695 await this.chargingStation
.ocppRequestService
.sendStopTransaction(
697 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
698 this.chargingStation
.getTransactionIdTag(transactionId
)
700 return Constants
.OCPP_RESPONSE_ACCEPTED
;
704 this.chargingStation
.logPrefix() +
705 ' Trying to remote stop a non existing transaction ' +
706 transactionId
.toString()
708 return Constants
.OCPP_RESPONSE_REJECTED
;
711 private async handleRequestGetDiagnostics(
712 commandPayload
: GetDiagnosticsRequest
713 ): Promise
<GetDiagnosticsResponse
> {
715 this.chargingStation
.logPrefix() +
717 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
718 ' request received: %j',
721 const uri
= new URL(commandPayload
.location
);
722 if (uri
.protocol
.startsWith('ftp:')) {
723 let ftpClient
: Client
;
726 .readdirSync(path
.resolve(__dirname
, '../../../../'))
727 .filter((file
) => file
.endsWith('.log'))
728 .map((file
) => path
.join('./', file
));
729 const diagnosticsArchive
=
730 this.chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
731 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
732 ftpClient
= new Client();
733 const accessResponse
= await ftpClient
.access({
735 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
736 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
737 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
739 let uploadResponse
: FTPResponse
;
740 if (accessResponse
.code
=== 220) {
741 // eslint-disable-next-line @typescript-eslint/no-misused-promises
742 ftpClient
.trackProgress(async (info
) => {
744 `${this.chargingStation.logPrefix()} ${
746 } bytes transferred from diagnostics archive ${info.name}`
748 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
749 OCPP16DiagnosticsStatus
.Uploading
752 uploadResponse
= await ftpClient
.uploadFrom(
753 path
.join(path
.resolve(__dirname
, '../../../../'), diagnosticsArchive
),
754 uri
.pathname
+ diagnosticsArchive
756 if (uploadResponse
.code
=== 226) {
757 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
758 OCPP16DiagnosticsStatus
.Uploaded
763 return { fileName
: diagnosticsArchive
};
766 ErrorType
.GENERIC_ERROR
,
767 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
768 uploadResponse?.code && '|' + uploadResponse?.code.toString()
770 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
774 ErrorType
.GENERIC_ERROR
,
775 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
776 uploadResponse?.code && '|' + uploadResponse?.code.toString()
778 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
781 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
782 OCPP16DiagnosticsStatus
.UploadFailed
787 return this.handleIncomingRequestError(
788 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
790 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
795 `${this.chargingStation.logPrefix()} Unsupported protocol ${
797 } to transfer the diagnostic logs archive`
799 await this.chargingStation
.ocppRequestService
.sendDiagnosticsStatusNotification(
800 OCPP16DiagnosticsStatus
.UploadFailed
802 return Constants
.OCPP_RESPONSE_EMPTY
;
806 private handleRequestTriggerMessage(
807 commandPayload
: OCPP16TriggerMessageRequest
808 ): OCPP16TriggerMessageResponse
{
810 switch (commandPayload
.requestedMessage
) {
811 case MessageTrigger
.BootNotification
:
813 this.chargingStation
.ocppRequestService
815 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
818 this.chargingStation
.getBootNotificationRequest().chargePointModel
,
820 this.chargingStation
.getBootNotificationRequest().chargePointVendor
,
821 chargeBoxSerialNumber
:
822 this.chargingStation
.getBootNotificationRequest().chargeBoxSerialNumber
,
824 this.chargingStation
.getBootNotificationRequest().firmwareVersion
,
825 chargePointSerialNumber
:
826 this.chargingStation
.getBootNotificationRequest().chargePointSerialNumber
,
827 iccid
: this.chargingStation
.getBootNotificationRequest().iccid
,
828 imsi
: this.chargingStation
.getBootNotificationRequest().imsi
,
830 this.chargingStation
.getBootNotificationRequest().meterSerialNumber
,
831 meterType
: this.chargingStation
.getBootNotificationRequest().meterType
,
833 { skipBufferingOnError
: true, triggerMessage
: true }
836 /* This is intentional */
838 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
839 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
840 case MessageTrigger
.Heartbeat
:
842 this.chargingStation
.ocppRequestService
843 .sendMessageHandler(OCPP16RequestCommand
.HEARTBEAT
, null, { triggerMessage
: true })
845 /* This is intentional */
847 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
848 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
850 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
853 return this.handleIncomingRequestError(
854 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
856 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}