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
: (value
?: any | PromiseLike
<any>) => void, reject
: (reason
?: any) => void) => {
32 let messageToSend
: string;
34 switch (messageType
) {
36 case MessageType
.CALL_MESSAGE
:
38 this.chargingStation
.requests
[messageId
] = [responseCallback
, rejectCallback
, commandParams
as Record
<string, unknown
>];
39 messageToSend
= JSON
.stringify([messageType
, messageId
, commandName
, commandParams
]);
42 case MessageType
.CALL_RESULT_MESSAGE
:
44 messageToSend
= JSON
.stringify([messageType
, messageId
, commandParams
]);
47 case MessageType
.CALL_ERROR_MESSAGE
:
48 // Build Error Message
49 messageToSend
= JSON
.stringify([messageType
, messageId
, commandParams
?.code
?? ErrorType
.GENERIC_ERROR
, commandParams
?.message
?? '', commandParams
?.details
?? {}]);
52 if (this.chargingStation
.getEnableStatistics()) {
53 this.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
55 // Check if wsConnection opened
56 if (this.chargingStation
.isWebSocketConnectionOpened()) {
58 const beginId
= PerformanceStatistics
.beginMeasure(commandName
);
59 this.chargingStation
.wsConnection
.send(messageToSend
);
60 PerformanceStatistics
.endMeasure(commandName
, beginId
);
61 } else if (!skipBufferingOnError
) {
63 this.chargingStation
.addToMessageQueue(messageToSend
);
65 return rejectCallback(new OCPPError(commandParams
?.code
?? ErrorType
.GENERIC_ERROR
, commandParams
?.message
?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams
.details
?? {}));
68 if (messageType
=== MessageType
.CALL_RESULT_MESSAGE
) {
71 } else if (messageType
=== MessageType
.CALL_ERROR_MESSAGE
) {
73 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
);
77 * Function that will receive the request's response
80 * @param requestPayload
82 async function responseCallback(payload
: Record
<string, unknown
> | string, requestPayload
: Record
<string, unknown
>): Promise
<void> {
83 if (self.chargingStation
.getEnableStatistics()) {
84 self.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, MessageType
.CALL_RESULT_MESSAGE
);
87 await self.ocppResponseService
.handleResponse(commandName
as RequestCommand
, payload
, requestPayload
);
92 * Function that will receive the request's rejection
96 function rejectCallback(error
: OCPPError
): void {
97 if (self.chargingStation
.getEnableStatistics()) {
98 self.chargingStation
.performanceStatistics
.addRequestStatistic(commandName
, MessageType
.CALL_ERROR_MESSAGE
);
100 logger
.debug(`${self.chargingStation.logPrefix()} Error: %j occurred when calling command %s with parameters: %j`, error
, commandName
, commandParams
);
102 // eslint-disable-next-line no-empty-function
103 self.chargingStation
.requests
[messageId
] = [() => { }, () => { }, {}];
110 protected handleRequestError(commandName
: RequestCommand
, error
: Error): void {
111 logger
.error(this.chargingStation
.logPrefix() + ' Request command ' + commandName
+ ' error: %j', error
);
115 public abstract sendHeartbeat(): Promise
<void>;
116 public abstract sendBootNotification(chargePointModel
: string, chargePointVendor
: string, chargeBoxSerialNumber
?: string, firmwareVersion
?: string, chargePointSerialNumber
?: string, iccid
?: string, imsi
?: string, meterSerialNumber
?: string, meterType
?: string): Promise
<BootNotificationResponse
>;
117 public abstract sendStatusNotification(connectorId
: number, status: ChargePointStatus
, errorCode
?: ChargePointErrorCode
): Promise
<void>;
118 public abstract sendAuthorize(connectorId
: number, idTag
?: string): Promise
<AuthorizeResponse
>;
119 public abstract sendStartTransaction(connectorId
: number, idTag
?: string): Promise
<StartTransactionResponse
>;
120 public abstract sendStopTransaction(transactionId
: number, meterStop
: number, idTag
?: string, reason
?: StopTransactionReason
): Promise
<StopTransactionResponse
>;
121 public abstract sendMeterValues(connectorId
: number, transactionId
: number, interval
: number): Promise
<void>;
122 public abstract sendTransactionBeginMeterValues(connectorId
: number, transactionId
: number, beginMeterValue
: MeterValue
): Promise
<void>;
123 public abstract sendTransactionEndMeterValues(connectorId
: number, transactionId
: number, endMeterValue
: MeterValue
): Promise
<void>;
124 public abstract sendDiagnosticsStatusNotification(diagnosticsStatus
: DiagnosticsStatus
): Promise
<void>;
125 public abstract sendError(messageId
: string, error
: OCPPError
, commandName
: RequestCommand
| IncomingRequestCommand
): Promise
<unknown
>;