Commit | Line | Data |
---|---|---|
c0560973 | 1 | import { AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../../types/ocpp/Transaction'; |
47e22477 | 2 | import { DiagnosticsStatus, IncomingRequestCommand, RequestCommand } from '../../types/ocpp/Requests'; |
c0560973 | 3 | |
efa43e52 | 4 | import { BootNotificationResponse } from '../../types/ocpp/Responses'; |
c0560973 JB |
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'; | |
fd0c36fa | 11 | import { MeterValue } from '../../types/ocpp/MeterValues'; |
14763b46 | 12 | import OCPPError from './OCPPError'; |
c0560973 | 13 | import OCPPResponseService from './OCPPResponseService'; |
a6b3c6c3 | 14 | import PerformanceStatistics from '../../performance/PerformanceStatistics'; |
c0560973 JB |
15 | import logger from '../../utils/Logger'; |
16 | ||
17 | export default abstract class OCPPRequestService { | |
18 | public chargingStation: ChargingStation; | |
19 | protected ocppResponseService: OCPPResponseService; | |
20 | ||
21 | constructor(chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) { | |
22 | this.chargingStation = chargingStation; | |
23 | this.ocppResponseService = ocppResponseService; | |
24 | } | |
25 | ||
a6b3c6c3 | 26 | public async sendMessage(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, |
9292e32a | 27 | skipBufferingOnError = false): Promise<unknown> { |
c0560973 JB |
28 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
29 | const self = this; | |
c0560973 | 30 | // Send a message through wsConnection |
e7accadb JB |
31 | return new Promise((resolve, reject) => { |
32 | const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback); | |
a6b3c6c3 JB |
33 | if (this.chargingStation.getEnableStatistics()) { |
34 | this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); | |
35 | } | |
36 | // Check if wsConnection opened | |
37 | if (this.chargingStation.isWebSocketConnectionOpened()) { | |
c0560973 | 38 | // Yes: Send Message |
aef1b33a | 39 | const beginId = PerformanceStatistics.beginMeasure(commandName); |
c0560973 | 40 | this.chargingStation.wsConnection.send(messageToSend); |
aef1b33a | 41 | PerformanceStatistics.endMeasure(commandName, beginId); |
9292e32a | 42 | } else if (!skipBufferingOnError) { |
3ba2381e | 43 | // Buffer it |
77f00f84 | 44 | this.chargingStation.addToMessageQueue(messageToSend); |
32b02249 JB |
45 | // Reject it but keep the request in the cache |
46 | reject(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams?.details ?? {})); | |
c0560973 JB |
47 | } |
48 | // Response? | |
49 | if (messageType === MessageType.CALL_RESULT_MESSAGE) { | |
50 | // Yes: send Ok | |
e7accadb | 51 | resolve(commandName); |
32b02249 | 52 | } else { |
c0560973 | 53 | // Send timeout |
32b02249 | 54 | setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_SOCKET_TIMEOUT); |
c0560973 JB |
55 | } |
56 | ||
3340259a JB |
57 | /** |
58 | * Function that will receive the request's response | |
59 | * | |
81797102 JB |
60 | * @param payload |
61 | * @param requestPayload | |
3340259a | 62 | */ |
c0560973 | 63 | async function responseCallback(payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> { |
52748e2c | 64 | if (self.chargingStation.getEnableStatistics()) { |
aef1b33a | 65 | self.chargingStation.performanceStatistics.addRequestStatistic(commandName, MessageType.CALL_RESULT_MESSAGE); |
c0560973 JB |
66 | } |
67 | // Send the response | |
68 | await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload); | |
32b02249 | 69 | self.chargingStation.requests.delete(messageId); |
c0560973 JB |
70 | resolve(payload); |
71 | } | |
72 | ||
3340259a | 73 | /** |
32b02249 | 74 | * Function that will receive the request's error response |
3340259a | 75 | * |
81797102 | 76 | * @param error |
3340259a | 77 | */ |
c0560973 | 78 | function rejectCallback(error: OCPPError): void { |
52748e2c | 79 | if (self.chargingStation.getEnableStatistics()) { |
aef1b33a | 80 | self.chargingStation.performanceStatistics.addRequestStatistic(commandName, MessageType.CALL_ERROR_MESSAGE); |
c0560973 | 81 | } |
32b02249 JB |
82 | logger.debug(`${self.chargingStation.logPrefix()} Error %j occurred when calling command %s with parameters %j`, error, commandName, commandParams); |
83 | self.chargingStation.requests.delete(messageId); | |
c0560973 JB |
84 | reject(error); |
85 | } | |
86 | }); | |
87 | } | |
88 | ||
c7f95d16 | 89 | protected handleRequestError(commandName: RequestCommand, error: Error): void { |
47e22477 | 90 | logger.error(this.chargingStation.logPrefix() + ' Request command ' + commandName + ' error: %j', error); |
c0560973 JB |
91 | throw error; |
92 | } | |
93 | ||
e7accadb JB |
94 | private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, |
95 | responseCallback: (payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>) => Promise<void>, rejectCallback: (error: OCPPError) => void): string { | |
96 | let messageToSend: string; | |
97 | // Type of message | |
98 | switch (messageType) { | |
99 | // Request | |
100 | case MessageType.CALL_MESSAGE: | |
101 | // Build request | |
32b02249 | 102 | this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandName, commandParams as Record<string, unknown>]); |
e7accadb JB |
103 | messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); |
104 | break; | |
105 | // Response | |
106 | case MessageType.CALL_RESULT_MESSAGE: | |
107 | // Build response | |
108 | messageToSend = JSON.stringify([messageType, messageId, commandParams]); | |
109 | break; | |
110 | // Error Message | |
111 | case MessageType.CALL_ERROR_MESSAGE: | |
112 | // Build Error Message | |
113 | messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]); | |
114 | break; | |
115 | } | |
116 | return messageToSend; | |
117 | } | |
118 | ||
c0560973 JB |
119 | public abstract sendHeartbeat(): Promise<void>; |
120 | public abstract sendBootNotification(chargePointModel: string, chargePointVendor: string, chargeBoxSerialNumber?: string, firmwareVersion?: string, chargePointSerialNumber?: string, iccid?: string, imsi?: string, meterSerialNumber?: string, meterType?: string): Promise<BootNotificationResponse>; | |
121 | public abstract sendStatusNotification(connectorId: number, status: ChargePointStatus, errorCode?: ChargePointErrorCode): Promise<void>; | |
163547b1 | 122 | public abstract sendAuthorize(connectorId: number, idTag?: string): Promise<AuthorizeResponse>; |
c0560973 JB |
123 | public abstract sendStartTransaction(connectorId: number, idTag?: string): Promise<StartTransactionResponse>; |
124 | public abstract sendStopTransaction(transactionId: number, meterStop: number, idTag?: string, reason?: StopTransactionReason): Promise<StopTransactionResponse>; | |
aef1b33a | 125 | public abstract sendMeterValues(connectorId: number, transactionId: number, interval: number): Promise<void>; |
fd0c36fa JB |
126 | public abstract sendTransactionBeginMeterValues(connectorId: number, transactionId: number, beginMeterValue: MeterValue): Promise<void>; |
127 | public abstract sendTransactionEndMeterValues(connectorId: number, transactionId: number, endMeterValue: MeterValue): Promise<void>; | |
47e22477 | 128 | public abstract sendDiagnosticsStatusNotification(diagnosticsStatus: DiagnosticsStatus): Promise<void>; |
c0560973 | 129 | public abstract sendError(messageId: string, error: OCPPError, commandName: RequestCommand | IncomingRequestCommand): Promise<unknown>; |
c0560973 | 130 | } |