1 import { AuthorizeResponse
, StartTransactionResponse
, StopTransactionReason
, StopTransactionResponse
} from
'../../types/ocpp/Transaction';
2 import { DiagnosticsStatus
, IncomingRequestCommand
, RequestCommand
} from
'../../types/ocpp/Requests';
4 import { BootNotificationResponse
} from
'../../types/ocpp/Responses';
5 import { ChargePointErrorCode
} from
'../../types/ocpp/ChargePointErrorCode';
6 import { ChargePointStatus
} from
'../../types/ocpp/ChargePointStatus';
7 import ChargingStation from
'../ChargingStation';
8 import Constants from
'../../utils/Constants';
9 import { ErrorType
} from
'../../types/ocpp/ErrorType';
10 import { MessageType
} from
'../../types/ocpp/MessageType';
11 import { MeterValue
} from
'../../types/ocpp/MeterValues';
12 import OCPPError from
'./OCPPError';
13 import OCPPResponseService from
'./OCPPResponseService';
14 import PerformanceStatistics from
'../../performance/PerformanceStatistics';
15 import logger from
'../../utils/Logger';
17 export default abstract class OCPPRequestService
{
18 public chargingStation
: ChargingStation
;
19 protected ocppResponseService
: OCPPResponseService
;
21 constructor(chargingStation
: ChargingStation
, ocppResponseService
: OCPPResponseService
) {
22 this.chargingStation
= chargingStation
;
23 this.ocppResponseService
= ocppResponseService
;
26 public async sendMessage(messageId
: string, commandParams
: any, messageType
: MessageType
, commandName
: RequestCommand
| IncomingRequestCommand
,
27 skipBufferingOnError
= false): Promise
<unknown
> {
28 // eslint-disable-next-line @typescript-eslint/no-this-alias
30 // Send a message through wsConnection
31 return new Promise((resolve
, reject
) => {
32 const messageToSend
= this.buildMessageToSend(messageId
, commandParams
, messageType
, commandName
, responseCallback
, rejectCallback
);
33 if (this.chargingStation
.getEnableStatistics()) {
34 this.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
36 // Check if wsConnection opened
37 if (this.chargingStation
.isWebSocketConnectionOpened()) {
39 const beginId
= PerformanceStatistics
.beginMeasure(commandName
);
40 this.chargingStation
.wsConnection
.send(messageToSend
);
41 PerformanceStatistics
.endMeasure(commandName
, beginId
);
42 } else if (!skipBufferingOnError
) {
44 this.chargingStation
.addToMessageQueue(messageToSend
);
46 return rejectCallback(new OCPPError(commandParams
?.code
?? ErrorType
.GENERIC_ERROR
, commandParams
?.message
?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams
?.details
?? {}));
49 if (messageType
=== MessageType
.CALL_RESULT_MESSAGE
) {
52 } else if (messageType
=== MessageType
.CALL_ERROR_MESSAGE
) {
54 setTimeout(() => rejectCallback(new OCPPError(commandParams
?.code
?? ErrorType
.GENERIC_ERROR
, commandParams
?.message
?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams
?.details
?? {})), Constants
.OCPP_ERROR_TIMEOUT
);
58 * Function that will receive the request's response
61 * @param requestPayload
63 async function responseCallback(payload
: Record
<string, unknown
> | string, requestPayload
: Record
<string, unknown
>): Promise
<void> {
64 if (self.chargingStation
.getEnableStatistics()) {
65 self.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, MessageType
.CALL_RESULT_MESSAGE
);
68 await self.ocppResponseService
.handleResponse(commandName
as RequestCommand
, payload
, requestPayload
);
73 * Function that will receive the request's rejection
77 function rejectCallback(error
: OCPPError
): void {
78 if (self.chargingStation
.getEnableStatistics()) {
79 self.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, MessageType
.CALL_ERROR_MESSAGE
);
81 logger
.debug(`${self.chargingStation.logPrefix()} Error: %j occurred when calling command %s with parameters: %j`, error
, commandName
, commandParams
);
83 // eslint-disable-next-line no-empty-function
84 self.chargingStation
.requests
.set(messageId
, [() => { }, () => { }, {}]);
91 protected handleRequestError(commandName
: RequestCommand
, error
: Error): void {
92 logger
.error(this.chargingStation
.logPrefix() + ' Request command ' + commandName
+ ' error: %j', error
);
96 private buildMessageToSend(messageId
: string, commandParams
: any, messageType
: MessageType
, commandName
: RequestCommand
| IncomingRequestCommand
,
97 responseCallback
: (payload
: Record
<string, unknown
> | string, requestPayload
: Record
<string, unknown
>) => Promise
<void>, rejectCallback
: (error
: OCPPError
) => void): string {
98 let messageToSend
: string;
100 switch (messageType
) {
102 case MessageType
.CALL_MESSAGE
:
104 this.chargingStation
.requests
.set(messageId
, [responseCallback
, rejectCallback
, commandParams
as Record
<string, unknown
>]);
105 messageToSend
= JSON
.stringify([messageType
, messageId
, commandName
, commandParams
]);
108 case MessageType
.CALL_RESULT_MESSAGE
:
110 messageToSend
= JSON
.stringify([messageType
, messageId
, commandParams
]);
113 case MessageType
.CALL_ERROR_MESSAGE
:
114 // Build Error Message
115 messageToSend
= JSON
.stringify([messageType
, messageId
, commandParams
?.code
?? ErrorType
.GENERIC_ERROR
, commandParams
?.message
?? '', commandParams
?.details
?? {}]);
118 return messageToSend
;
121 public abstract sendHeartbeat(): Promise
<void>;
122 public abstract sendBootNotification(chargePointModel
: string, chargePointVendor
: string, chargeBoxSerialNumber
?: string, firmwareVersion
?: string, chargePointSerialNumber
?: string, iccid
?: string, imsi
?: string, meterSerialNumber
?: string, meterType
?: string): Promise
<BootNotificationResponse
>;
123 public abstract sendStatusNotification(connectorId
: number, status: ChargePointStatus
, errorCode
?: ChargePointErrorCode
): Promise
<void>;
124 public abstract sendAuthorize(connectorId
: number, idTag
?: string): Promise
<AuthorizeResponse
>;
125 public abstract sendStartTransaction(connectorId
: number, idTag
?: string): Promise
<StartTransactionResponse
>;
126 public abstract sendStopTransaction(transactionId
: number, meterStop
: number, idTag
?: string, reason
?: StopTransactionReason
): Promise
<StopTransactionResponse
>;
127 public abstract sendMeterValues(connectorId
: number, transactionId
: number, interval
: number): Promise
<void>;
128 public abstract sendTransactionBeginMeterValues(connectorId
: number, transactionId
: number, beginMeterValue
: MeterValue
): Promise
<void>;
129 public abstract sendTransactionEndMeterValues(connectorId
: number, transactionId
: number, endMeterValue
: MeterValue
): Promise
<void>;
130 public abstract sendDiagnosticsStatusNotification(diagnosticsStatus
: DiagnosticsStatus
): Promise
<void>;
131 public abstract sendError(messageId
: string, error
: OCPPError
, commandName
: RequestCommand
| IncomingRequestCommand
): Promise
<unknown
>;