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 DiagnosticsStatusNotificationResponse
,
25 GetConfigurationResponse
,
26 GetDiagnosticsResponse
,
27 OCPP16BootNotificationResponse
,
28 OCPP16HeartbeatResponse
,
29 OCPP16StatusNotificationResponse
,
30 OCPP16TriggerMessageResponse
,
31 SetChargingProfileResponse
,
32 UnlockConnectorResponse
,
33 } from
'../../../types/ocpp/1.6/Responses';
35 ChargingProfilePurposeType
,
36 OCPP16ChargingProfile
,
37 } from
'../../../types/ocpp/1.6/ChargingProfile';
38 import { Client
, FTPResponse
} from
'basic-ftp';
40 OCPP16AuthorizationStatus
,
41 OCPP16AuthorizeResponse
,
42 OCPP16StartTransactionResponse
,
43 OCPP16StopTransactionReason
,
44 OCPP16StopTransactionResponse
,
45 } from
'../../../types/ocpp/1.6/Transaction';
47 import type ChargingStation from
'../../ChargingStation';
48 import Constants from
'../../../utils/Constants';
49 import { DefaultResponse
} from
'../../../types/ocpp/Responses';
50 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
51 import { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
52 import { JsonType
} from
'../../../types/JsonType';
53 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
54 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
55 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
56 import { OCPP16MeterValuesResponse
} from
'../../../types/ocpp/1.6/MeterValues';
57 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
58 import { OCPP16StandardParametersKey
} from
'../../../types/ocpp/1.6/Configuration';
59 import { OCPPConfigurationKey
} from
'../../../types/ocpp/Configuration';
60 import OCPPError from
'../../../exception/OCPPError';
61 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
62 import { URL
} from
'url';
63 import Utils from
'../../../utils/Utils';
65 import logger from
'../../../utils/Logger';
66 import path from
'path';
67 import tar from
'tar';
69 const moduleName
= 'OCPP16IncomingRequestService';
71 export default class OCPP16IncomingRequestService
extends OCPPIncomingRequestService
{
72 private incomingRequestHandlers
: Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>;
74 public constructor(chargingStation
: ChargingStation
) {
75 if (new.target
?.name
=== moduleName
) {
76 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
78 super(chargingStation
);
79 this.incomingRequestHandlers
= new Map
<OCPP16IncomingRequestCommand
, IncomingRequestHandler
>([
80 [OCPP16IncomingRequestCommand
.RESET
, this.handleRequestReset
.bind(this)],
81 [OCPP16IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
82 [OCPP16IncomingRequestCommand
.UNLOCK_CONNECTOR
, this.handleRequestUnlockConnector
.bind(this)],
84 OCPP16IncomingRequestCommand
.GET_CONFIGURATION
,
85 this.handleRequestGetConfiguration
.bind(this),
88 OCPP16IncomingRequestCommand
.CHANGE_CONFIGURATION
,
89 this.handleRequestChangeConfiguration
.bind(this),
92 OCPP16IncomingRequestCommand
.SET_CHARGING_PROFILE
,
93 this.handleRequestSetChargingProfile
.bind(this),
96 OCPP16IncomingRequestCommand
.CLEAR_CHARGING_PROFILE
,
97 this.handleRequestClearChargingProfile
.bind(this),
100 OCPP16IncomingRequestCommand
.CHANGE_AVAILABILITY
,
101 this.handleRequestChangeAvailability
.bind(this),
104 OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
,
105 this.handleRequestRemoteStartTransaction
.bind(this),
108 OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
,
109 this.handleRequestRemoteStopTransaction
.bind(this),
111 [OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
, this.handleRequestGetDiagnostics
.bind(this)],
112 [OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
, this.handleRequestTriggerMessage
.bind(this)],
116 public async handleRequest(
118 commandName
: OCPP16IncomingRequestCommand
,
119 commandPayload
: JsonType
121 let result
: JsonType
;
123 this.chargingStation
.getOcppStrictCompliance() &&
124 this.chargingStation
.isInPendingState() &&
125 (commandName
=== OCPP16IncomingRequestCommand
.REMOTE_START_TRANSACTION
||
126 commandName
=== OCPP16IncomingRequestCommand
.REMOTE_STOP_TRANSACTION
)
129 ErrorType
.SECURITY_ERROR
,
130 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
134 )} while the charging station is in pending state on the central server`,
139 this.chargingStation
.isRegistered() ||
140 (!this.chargingStation
.getOcppStrictCompliance() && this.chargingStation
.isInUnknownState())
142 if (this.incomingRequestHandlers
.has(commandName
)) {
144 // Call the method to build the result
145 result
= await this.incomingRequestHandlers
.get(commandName
)(commandPayload
);
148 logger
.error(this.chargingStation
.logPrefix() + ' Handle request error: %j', error
);
154 ErrorType
.NOT_IMPLEMENTED
,
155 `${commandName} is not implemented to handle request payload ${JSON.stringify(
165 ErrorType
.SECURITY_ERROR
,
166 `${commandName} cannot be issued to handle request payload ${JSON.stringify(
170 )} while the charging station is not registered on the central server.`,
174 // Send the built result
175 await this.chargingStation
.ocppRequestService
.sendResult(messageId
, result
, commandName
);
178 // Simulate charging station restart
179 private handleRequestReset(commandPayload
: ResetRequest
): DefaultResponse
{
180 // eslint-disable-next-line @typescript-eslint/no-misused-promises
181 setImmediate(async (): Promise
<void> => {
182 await this.chargingStation
.stop(
183 (commandPayload
.type + 'Reset') as OCPP16StopTransactionReason
185 await Utils
.sleep(this.chargingStation
.stationInfo
.resetTime
);
186 this.chargingStation
.start();
189 `${this.chargingStation.logPrefix()} ${
191 } reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(
192 this.chargingStation.stationInfo.resetTime
195 return Constants
.OCPP_RESPONSE_ACCEPTED
;
198 private handleRequestClearCache(): DefaultResponse
{
199 return Constants
.OCPP_RESPONSE_ACCEPTED
;
202 private async handleRequestUnlockConnector(
203 commandPayload
: UnlockConnectorRequest
204 ): Promise
<UnlockConnectorResponse
> {
205 const connectorId
= commandPayload
.connectorId
;
206 if (connectorId
=== 0) {
208 this.chargingStation
.logPrefix() + ' Trying to unlock connector ' + connectorId
.toString()
210 return Constants
.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED
;
212 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
213 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
).transactionId
;
215 this.chargingStation
.getBeginEndMeterValues() &&
216 this.chargingStation
.getOcppStrictCompliance() &&
217 !this.chargingStation
.getOutOfOrderEndMeterValues()
219 // FIXME: Implement OCPP version agnostic helpers
220 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
221 this.chargingStation
,
223 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
)
225 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16MeterValuesResponse
>(
226 OCPP16RequestCommand
.METER_VALUES
,
230 meterValue
: transactionEndMeterValue
,
235 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StopTransactionResponse
>(
236 OCPP16RequestCommand
.STOP_TRANSACTION
,
240 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
241 idTag
: this.chargingStation
.getTransactionIdTag(transactionId
),
242 reason
: OCPP16StopTransactionReason
.UNLOCK_COMMAND
,
245 if (stopResponse
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
246 return Constants
.OCPP_RESPONSE_UNLOCKED
;
248 return Constants
.OCPP_RESPONSE_UNLOCK_FAILED
;
250 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
251 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
254 status: OCPP16ChargePointStatus
.AVAILABLE
,
255 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
258 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
259 return Constants
.OCPP_RESPONSE_UNLOCKED
;
262 private handleRequestGetConfiguration(
263 commandPayload
: GetConfigurationRequest
264 ): GetConfigurationResponse
{
265 const configurationKey
: OCPPConfigurationKey
[] = [];
266 const unknownKey
: string[] = [];
267 if (Utils
.isEmptyArray(commandPayload
.key
)) {
268 for (const configuration
of this.chargingStation
.configuration
.configurationKey
) {
269 if (Utils
.isUndefined(configuration
.visible
)) {
270 configuration
.visible
= true;
272 if (!configuration
.visible
) {
275 configurationKey
.push({
276 key
: configuration
.key
,
277 readonly: configuration
.readonly,
278 value
: configuration
.value
,
282 for (const key
of commandPayload
.key
) {
283 const keyFound
= this.chargingStation
.getConfigurationKey(key
);
285 if (Utils
.isUndefined(keyFound
.visible
)) {
286 keyFound
.visible
= true;
288 if (!keyFound
.visible
) {
291 configurationKey
.push({
293 readonly: keyFound
.readonly,
294 value
: keyFound
.value
,
297 unknownKey
.push(key
);
307 private handleRequestChangeConfiguration(
308 commandPayload
: ChangeConfigurationRequest
309 ): ChangeConfigurationResponse
{
310 // JSON request fields type sanity check
311 if (!Utils
.isString(commandPayload
.key
)) {
313 `${this.chargingStation.logPrefix()} ${
314 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
315 } request key field is not a string:`,
319 if (!Utils
.isString(commandPayload
.value
)) {
321 `${this.chargingStation.logPrefix()} ${
322 OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION
323 } request value field is not a string:`,
327 const keyToChange
= this.chargingStation
.getConfigurationKey(commandPayload
.key
, true);
329 return Constants
.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED
;
330 } else if (keyToChange
&& keyToChange
.readonly) {
331 return Constants
.OCPP_CONFIGURATION_RESPONSE_REJECTED
;
332 } else if (keyToChange
&& !keyToChange
.readonly) {
333 let valueChanged
= false;
334 if (keyToChange
.value
!== commandPayload
.value
) {
335 this.chargingStation
.setConfigurationKeyValue(
337 commandPayload
.value
,
342 let triggerHeartbeatRestart
= false;
343 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartBeatInterval
&& valueChanged
) {
344 this.chargingStation
.setConfigurationKeyValue(
345 OCPP16StandardParametersKey
.HeartbeatInterval
,
348 triggerHeartbeatRestart
= true;
350 if (keyToChange
.key
=== OCPP16StandardParametersKey
.HeartbeatInterval
&& valueChanged
) {
351 this.chargingStation
.setConfigurationKeyValue(
352 OCPP16StandardParametersKey
.HeartBeatInterval
,
355 triggerHeartbeatRestart
= true;
357 if (triggerHeartbeatRestart
) {
358 this.chargingStation
.restartHeartbeat();
360 if (keyToChange
.key
=== OCPP16StandardParametersKey
.WebSocketPingInterval
&& valueChanged
) {
361 this.chargingStation
.restartWebSocketPing();
363 if (keyToChange
.reboot
) {
364 return Constants
.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED
;
366 return Constants
.OCPP_CONFIGURATION_RESPONSE_ACCEPTED
;
370 private handleRequestSetChargingProfile(
371 commandPayload
: SetChargingProfileRequest
372 ): SetChargingProfileResponse
{
373 if (!this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)) {
375 `${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
376 commandPayload.connectorId
379 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
382 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
383 ChargingProfilePurposeType
.CHARGE_POINT_MAX_PROFILE
&&
384 commandPayload
.connectorId
!== 0
386 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
389 commandPayload
.csChargingProfiles
.chargingProfilePurpose
===
390 ChargingProfilePurposeType
.TX_PROFILE
&&
391 (commandPayload
.connectorId
=== 0 ||
392 !this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
)?.transactionStarted
)
394 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED
;
396 this.chargingStation
.setChargingProfile(
397 commandPayload
.connectorId
,
398 commandPayload
.csChargingProfiles
401 `${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`,
402 this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
).chargingProfiles
404 return Constants
.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
407 private handleRequestClearChargingProfile(
408 commandPayload
: ClearChargingProfileRequest
409 ): ClearChargingProfileResponse
{
410 const connectorStatus
= this.chargingStation
.getConnectorStatus(commandPayload
.connectorId
);
411 if (!connectorStatus
) {
413 `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
414 commandPayload.connectorId
417 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
419 if (commandPayload
.connectorId
&& !Utils
.isEmptyArray(connectorStatus
.chargingProfiles
)) {
420 connectorStatus
.chargingProfiles
= [];
422 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
423 connectorStatus
.chargingProfiles
425 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
427 if (!commandPayload
.connectorId
) {
428 let clearedCP
= false;
429 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
431 !Utils
.isEmptyArray(this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
)
434 .getConnectorStatus(connectorId
)
435 .chargingProfiles
?.forEach((chargingProfile
: OCPP16ChargingProfile
, index
: number) => {
436 let clearCurrentCP
= false;
437 if (chargingProfile
.chargingProfileId
=== commandPayload
.id
) {
438 clearCurrentCP
= true;
441 !commandPayload
.chargingProfilePurpose
&&
442 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
444 clearCurrentCP
= true;
447 !chargingProfile
.stackLevel
&&
448 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
450 clearCurrentCP
= true;
453 chargingProfile
.stackLevel
=== commandPayload
.stackLevel
&&
454 chargingProfile
.chargingProfilePurpose
=== commandPayload
.chargingProfilePurpose
456 clearCurrentCP
= true;
458 if (clearCurrentCP
) {
459 connectorStatus
.chargingProfiles
[index
] = {} as OCPP16ChargingProfile
;
461 `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
462 connectorStatus
.chargingProfiles
470 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED
;
473 return Constants
.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN
;
476 private async handleRequestChangeAvailability(
477 commandPayload
: ChangeAvailabilityRequest
478 ): Promise
<ChangeAvailabilityResponse
> {
479 const connectorId
: number = commandPayload
.connectorId
;
480 if (!this.chargingStation
.getConnectorStatus(connectorId
)) {
482 `${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
484 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
486 const chargePointStatus
: OCPP16ChargePointStatus
=
487 commandPayload
.type === OCPP16AvailabilityType
.OPERATIVE
488 ? OCPP16ChargePointStatus
.AVAILABLE
489 : OCPP16ChargePointStatus
.UNAVAILABLE
;
490 if (connectorId
=== 0) {
491 let response
: ChangeAvailabilityResponse
= Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
492 for (const id
of this.chargingStation
.connectors
.keys()) {
493 if (this.chargingStation
.getConnectorStatus(id
)?.transactionStarted
) {
494 response
= Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
496 this.chargingStation
.getConnectorStatus(id
).availability
= commandPayload
.type;
497 if (response
=== Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
) {
498 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
499 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
502 status: chargePointStatus
,
503 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
506 this.chargingStation
.getConnectorStatus(id
).status = chargePointStatus
;
512 (this.chargingStation
.getConnectorStatus(0).availability
===
513 OCPP16AvailabilityType
.OPERATIVE
||
514 (this.chargingStation
.getConnectorStatus(0).availability
===
515 OCPP16AvailabilityType
.INOPERATIVE
&&
516 commandPayload
.type === OCPP16AvailabilityType
.INOPERATIVE
))
518 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
519 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
520 return Constants
.OCPP_AVAILABILITY_RESPONSE_SCHEDULED
;
522 this.chargingStation
.getConnectorStatus(connectorId
).availability
= commandPayload
.type;
523 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
524 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
525 { connectorId
, status: chargePointStatus
, errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
}
527 this.chargingStation
.getConnectorStatus(connectorId
).status = chargePointStatus
;
528 return Constants
.OCPP_AVAILABILITY_RESPONSE_ACCEPTED
;
530 return Constants
.OCPP_AVAILABILITY_RESPONSE_REJECTED
;
533 private async handleRequestRemoteStartTransaction(
534 commandPayload
: RemoteStartTransactionRequest
535 ): Promise
<DefaultResponse
> {
536 const transactionConnectorId
= commandPayload
.connectorId
;
537 const connectorStatus
= this.chargingStation
.getConnectorStatus(transactionConnectorId
);
538 if (transactionConnectorId
) {
539 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
540 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
542 connectorId
: transactionConnectorId
,
543 status: OCPP16ChargePointStatus
.PREPARING
,
544 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
547 connectorStatus
.status = OCPP16ChargePointStatus
.PREPARING
;
548 if (this.chargingStation
.isChargingStationAvailable() && connectorStatus
) {
549 // Check if authorized
550 if (this.chargingStation
.getAuthorizeRemoteTxRequests()) {
551 let authorized
= false;
553 this.chargingStation
.getLocalAuthListEnabled() &&
554 this.chargingStation
.hasAuthorizedTags() &&
555 this.chargingStation
.authorizedTags
.find((value
) => value
=== commandPayload
.idTag
)
557 connectorStatus
.localAuthorizeIdTag
= commandPayload
.idTag
;
558 connectorStatus
.idTagLocalAuthorized
= true;
560 } else if (this.chargingStation
.getMayAuthorizeAtRemoteStart()) {
561 connectorStatus
.authorizeIdTag
= commandPayload
.idTag
;
562 const authorizeResponse
: OCPP16AuthorizeResponse
=
563 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16AuthorizeResponse
>(
564 OCPP16RequestCommand
.AUTHORIZE
,
566 idTag
: commandPayload
.idTag
,
569 if (authorizeResponse
?.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
574 `${this.chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
578 // Authorization successful, start transaction
580 this.setRemoteStartTransactionChargingProfile(
581 transactionConnectorId
,
582 commandPayload
.chargingProfile
585 connectorStatus
.transactionRemoteStarted
= true;
588 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StartTransactionResponse
>(
589 OCPP16RequestCommand
.START_TRANSACTION
,
591 connectorId
: transactionConnectorId
,
592 idTag
: commandPayload
.idTag
,
595 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
598 this.chargingStation
.logPrefix() +
599 ' Transaction remotely STARTED on ' +
600 this.chargingStation
.stationInfo
.chargingStationId
+
602 transactionConnectorId
.toString() +
606 return Constants
.OCPP_RESPONSE_ACCEPTED
;
608 return this.notifyRemoteStartTransactionRejected(
609 transactionConnectorId
,
613 return this.notifyRemoteStartTransactionRejected(
614 transactionConnectorId
,
618 return this.notifyRemoteStartTransactionRejected(
619 transactionConnectorId
,
623 // No authorization check required, start transaction
625 this.setRemoteStartTransactionChargingProfile(
626 transactionConnectorId
,
627 commandPayload
.chargingProfile
630 connectorStatus
.transactionRemoteStarted
= true;
633 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StartTransactionResponse
>(
634 OCPP16RequestCommand
.START_TRANSACTION
,
636 connectorId
: transactionConnectorId
,
637 idTag
: commandPayload
.idTag
,
640 ).idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
643 this.chargingStation
.logPrefix() +
644 ' Transaction remotely STARTED on ' +
645 this.chargingStation
.stationInfo
.chargingStationId
+
647 transactionConnectorId
.toString() +
651 return Constants
.OCPP_RESPONSE_ACCEPTED
;
653 return this.notifyRemoteStartTransactionRejected(
654 transactionConnectorId
,
658 return this.notifyRemoteStartTransactionRejected(
659 transactionConnectorId
,
663 return this.notifyRemoteStartTransactionRejected(
664 transactionConnectorId
,
668 return this.notifyRemoteStartTransactionRejected(transactionConnectorId
, commandPayload
.idTag
);
671 private async notifyRemoteStartTransactionRejected(
674 ): Promise
<DefaultResponse
> {
676 this.chargingStation
.getConnectorStatus(connectorId
).status !==
677 OCPP16ChargePointStatus
.AVAILABLE
679 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
680 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
683 status: OCPP16ChargePointStatus
.AVAILABLE
,
684 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
687 this.chargingStation
.getConnectorStatus(connectorId
).status =
688 OCPP16ChargePointStatus
.AVAILABLE
;
691 this.chargingStation
.logPrefix() +
692 ' Remote starting transaction REJECTED on connector Id ' +
693 connectorId
.toString() +
697 this.chargingStation
.getConnectorStatus(connectorId
).availability
+
699 this.chargingStation
.getConnectorStatus(connectorId
).status
701 return Constants
.OCPP_RESPONSE_REJECTED
;
704 private setRemoteStartTransactionChargingProfile(
706 cp
: OCPP16ChargingProfile
708 if (cp
&& cp
.chargingProfilePurpose
=== ChargingProfilePurposeType
.TX_PROFILE
) {
709 this.chargingStation
.setChargingProfile(connectorId
, cp
);
711 `${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`,
712 this.chargingStation
.getConnectorStatus(connectorId
).chargingProfiles
715 } else if (cp
&& cp
.chargingProfilePurpose
!== ChargingProfilePurposeType
.TX_PROFILE
) {
717 `${this.chargingStation.logPrefix()} Not allowed to set ${
718 cp.chargingProfilePurpose
719 } charging profile(s) at remote start transaction`
727 private async handleRequestRemoteStopTransaction(
728 commandPayload
: RemoteStopTransactionRequest
729 ): Promise
<DefaultResponse
> {
730 const transactionId
= commandPayload
.transactionId
;
731 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
734 this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
736 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StatusNotificationResponse
>(
737 OCPP16RequestCommand
.STATUS_NOTIFICATION
,
740 status: OCPP16ChargePointStatus
.FINISHING
,
741 errorCode
: OCPP16ChargePointErrorCode
.NO_ERROR
,
744 this.chargingStation
.getConnectorStatus(connectorId
).status =
745 OCPP16ChargePointStatus
.FINISHING
;
747 this.chargingStation
.getBeginEndMeterValues() &&
748 this.chargingStation
.getOcppStrictCompliance() &&
749 !this.chargingStation
.getOutOfOrderEndMeterValues()
751 // FIXME: Implement OCPP version agnostic helpers
752 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
753 this.chargingStation
,
755 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
)
757 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16MeterValuesResponse
>(
758 OCPP16RequestCommand
.METER_VALUES
,
762 meterValue
: transactionEndMeterValue
,
766 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<OCPP16StopTransactionResponse
>(
767 OCPP16RequestCommand
.STOP_TRANSACTION
,
771 this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
772 idTag
: this.chargingStation
.getTransactionIdTag(transactionId
),
775 return Constants
.OCPP_RESPONSE_ACCEPTED
;
779 this.chargingStation
.logPrefix() +
780 ' Trying to remote stop a non existing transaction ' +
781 transactionId
.toString()
783 return Constants
.OCPP_RESPONSE_REJECTED
;
786 private async handleRequestGetDiagnostics(
787 commandPayload
: GetDiagnosticsRequest
788 ): Promise
<GetDiagnosticsResponse
> {
790 this.chargingStation
.logPrefix() +
792 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
+
793 ' request received: %j',
796 const uri
= new URL(commandPayload
.location
);
797 if (uri
.protocol
.startsWith('ftp:')) {
798 let ftpClient
: Client
;
801 .readdirSync(path
.resolve(__dirname
, '../../../../'))
802 .filter((file
) => file
.endsWith('.log'))
803 .map((file
) => path
.join('./', file
));
804 const diagnosticsArchive
=
805 this.chargingStation
.stationInfo
.chargingStationId
+ '_logs.tar.gz';
806 tar
.create({ gzip
: true }, logFiles
).pipe(fs
.createWriteStream(diagnosticsArchive
));
807 ftpClient
= new Client();
808 const accessResponse
= await ftpClient
.access({
810 ...(!Utils
.isEmptyString(uri
.port
) && { port
: Utils
.convertToInt(uri
.port
) }),
811 ...(!Utils
.isEmptyString(uri
.username
) && { user
: uri
.username
}),
812 ...(!Utils
.isEmptyString(uri
.password
) && { password
: uri
.password
}),
814 let uploadResponse
: FTPResponse
;
815 if (accessResponse
.code
=== 220) {
816 // eslint-disable-next-line @typescript-eslint/no-misused-promises
817 ftpClient
.trackProgress(async (info
) => {
819 `${this.chargingStation.logPrefix()} ${
821 } bytes transferred from diagnostics archive ${info.name}`
823 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<DiagnosticsStatusNotificationResponse
>(
824 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
826 status: OCPP16DiagnosticsStatus
.Uploading
,
830 uploadResponse
= await ftpClient
.uploadFrom(
831 path
.join(path
.resolve(__dirname
, '../../../../'), diagnosticsArchive
),
832 uri
.pathname
+ diagnosticsArchive
834 if (uploadResponse
.code
=== 226) {
835 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<DiagnosticsStatusNotificationResponse
>(
836 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
838 status: OCPP16DiagnosticsStatus
.Uploaded
,
844 return { fileName
: diagnosticsArchive
};
847 ErrorType
.GENERIC_ERROR
,
848 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
849 uploadResponse?.code && '|' + uploadResponse?.code.toString()
851 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
855 ErrorType
.GENERIC_ERROR
,
856 `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${
857 uploadResponse?.code && '|' + uploadResponse?.code.toString()
859 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
862 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<DiagnosticsStatusNotificationResponse
>(
863 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
865 status: OCPP16DiagnosticsStatus
.UploadFailed
,
871 return this.handleIncomingRequestError(
872 OCPP16IncomingRequestCommand
.GET_DIAGNOSTICS
,
874 { errorResponse
: Constants
.OCPP_RESPONSE_EMPTY
}
879 `${this.chargingStation.logPrefix()} Unsupported protocol ${
881 } to transfer the diagnostic logs archive`
883 await this.chargingStation
.ocppRequestService
.sendMessageHandler
<DiagnosticsStatusNotificationResponse
>(
884 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
886 status: OCPP16DiagnosticsStatus
.UploadFailed
,
889 return Constants
.OCPP_RESPONSE_EMPTY
;
893 private handleRequestTriggerMessage(
894 commandPayload
: OCPP16TriggerMessageRequest
895 ): OCPP16TriggerMessageResponse
{
897 switch (commandPayload
.requestedMessage
) {
898 case MessageTrigger
.BootNotification
:
900 this.chargingStation
.ocppRequestService
901 .sendMessageHandler
<OCPP16BootNotificationResponse
>(
902 OCPP16RequestCommand
.BOOT_NOTIFICATION
,
905 this.chargingStation
.getBootNotificationRequest().chargePointModel
,
907 this.chargingStation
.getBootNotificationRequest().chargePointVendor
,
908 chargeBoxSerialNumber
:
909 this.chargingStation
.getBootNotificationRequest().chargeBoxSerialNumber
,
911 this.chargingStation
.getBootNotificationRequest().firmwareVersion
,
912 chargePointSerialNumber
:
913 this.chargingStation
.getBootNotificationRequest().chargePointSerialNumber
,
914 iccid
: this.chargingStation
.getBootNotificationRequest().iccid
,
915 imsi
: this.chargingStation
.getBootNotificationRequest().imsi
,
917 this.chargingStation
.getBootNotificationRequest().meterSerialNumber
,
918 meterType
: this.chargingStation
.getBootNotificationRequest().meterType
,
920 { skipBufferingOnError
: true, triggerMessage
: true }
923 this.chargingStation
.bootNotificationResponse
= value
;
926 /* This is intentional */
928 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
929 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
930 case MessageTrigger
.Heartbeat
:
932 this.chargingStation
.ocppRequestService
933 .sendMessageHandler
<OCPP16HeartbeatResponse
>(OCPP16RequestCommand
.HEARTBEAT
, null, {
934 triggerMessage
: true,
937 /* This is intentional */
939 }, Constants
.OCPP_TRIGGER_MESSAGE_DELAY
);
940 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
;
942 return Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
;
945 return this.handleIncomingRequestError(
946 OCPP16IncomingRequestCommand
.TRIGGER_MESSAGE
,
948 { errorResponse
: Constants
.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
}