Back to the org code
MODIFY
src/server/ocpp/json/JsonOCPPServer.ts
src/server/ocpp/json/JsonOCPPServer.ts
@@ -2,11 +2,7 @@ import * as uWS from 'uWebSockets.js';
import { App, HttpRequest, HttpResponse, WebSocket, us_socket_context_t } from 'uWebSockets.js';
import { ServerAction, ServerType, WSServerProtocol } from '../../../types/Server';
-import {
- WebSocketAction,
- WebSocketCloseEventStatusCode,
- WebSocketPingResult
-} from '../../../types/WebSocket';
+import { WebSocketAction, WebSocketCloseEventStatusCode, WebSocketPingResult } from '../../../types/WebSocket';
import CentralSystemConfiguration from '../../../types/configuration/CentralSystemConfiguration';
import ChargingStation from '../../../types/ChargingStation';
@@ -32,7 +28,7 @@ const MODULE_NAME = 'JsonOCPPServer';
export default class JsonOCPPServer extends OCPPServer {
private waitingWSMessages = 0;
private runningWSMessages = 0;
- private runningWSRequestsMessages: Map<string, WebSocketAction> = new Map();
+ private runningWSRequestsMessages: Record<string, boolean> = {};
private jsonWSConnections: Map<string, JsonWSConnection> = new Map();
private jsonRestWSConnections: Map<string, JsonRestWSConnection> = new Map();
@@ -75,7 +71,7 @@ export default class JsonOCPPServer extends OCPPServer {
// Remove connection
void this.removeWSWrapper(WebSocketAction.CLOSE, ServerAction.WS_SERVER_CONNECTION_CLOSE, wsWrapper).then(async () => {
await this.logWSConnectionClosed(wsWrapper, ServerAction.WS_SERVER_CONNECTION_CLOSE, code,
- `${WebSocketAction.CLOSE} > WS Connection ID '${wsWrapper.guid}' closed by charging station with code '${code}', reason: ${reason || 'No reason given'}'`);
+ `${WebSocketAction.CLOSE} > WS Connection ID '${wsWrapper.guid}' closed by charging station with code '${code}', reason: '${!Utils.isNullOrEmptyString(reason) ? reason : 'No reason given'}'`);
});
},
ping: (ws: WebSocket, message: ArrayBuffer) => {
@@ -145,8 +141,7 @@ export default class JsonOCPPServer extends OCPPServer {
tenantID: Constants.DEFAULT_TENANT_ID,
chargingStationID: chargingStation.id,
module: MODULE_NAME, method: 'getChargingStationClient',
- action: ServerAction.WS_SERVER_CONNECTION,
- message: `${message} - tenant: ${tenant.subdomain}`
+ action: ServerAction.WS_SERVER_CONNECTION, message
});
return;
}
@@ -343,7 +338,8 @@ export default class JsonOCPPServer extends OCPPServer {
}
// Wait for Init (avoid WS connection with same URL), ocppMessageType only provided when a WS Message is received
await this.waitForWSLockToRelease(wsAction, action, wsWrapper);
- this.runningWSRequestsMessages.set(wsWrapper.url, wsAction) ;
+ // Lock
+ this.runningWSRequestsMessages[wsWrapper.url] = true;
}
private releaseLockForWSMessageRequest(wsWrapper: WSWrapper, ocppMessageType?: OCPPMessageType): void {
@@ -352,7 +348,7 @@ export default class JsonOCPPServer extends OCPPServer {
return;
}
// Unlock
- this.runningWSRequestsMessages.delete(wsWrapper.url);
+ delete this.runningWSRequestsMessages[wsWrapper.url];
}
private async onMessage(ws: uWS.WebSocket, message: string, isBinary: boolean): Promise<void> {
@@ -480,7 +476,7 @@ export default class JsonOCPPServer extends OCPPServer {
private async waitForWSLockToRelease(wsAction: WebSocketAction, action: ServerAction, wsWrapper: WSWrapper): Promise<boolean> {
// Wait for init to handle multiple same WS Connection
- if (this.runningWSRequestsMessages.has(wsWrapper.url)) {
+ if (this.runningWSRequestsMessages[wsWrapper.url]) {
const maxNumberOfTrials = 10;
let numberOfTrials = 0;
const timeStart = Date.now();
@@ -492,14 +488,13 @@ export default class JsonOCPPServer extends OCPPServer {
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
this.waitingWSMessages++;
- try {
// eslint-disable-next-line no-constant-condition
while (true) {
// Wait
await Utils.sleep(Constants.WS_LOCK_TIME_OUT_MILLIS);
numberOfTrials++;
// Message has been processed
- if (!this.runningWSRequestsMessages.has(wsWrapper.url)) {
+ if (!this.runningWSRequestsMessages[wsWrapper.url]) {
await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT_ID,
chargingStationID: wsWrapper.chargingStationID,
@@ -507,6 +502,8 @@ export default class JsonOCPPServer extends OCPPServer {
message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' - Lock has been acquired successfully after ${numberOfTrials} trial(s) and ${Utils.computeTimeDurationSecs(timeStart)} secs`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
+ // Free the lock
+ this.waitingWSMessages--;
break;
}
// Handle remaining trial
@@ -516,20 +513,14 @@ export default class JsonOCPPServer extends OCPPServer {
tenantID: Constants.DEFAULT_TENANT_ID,
chargingStationID: wsWrapper.chargingStationID,
action, module: MODULE_NAME, method: 'waitForWSLockToRelease',
- message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' - Cannot acquire the lock after ${numberOfTrials} trial(s) action : ${this.runningWSRequestsMessages.get(wsWrapper.url)} and ${Utils.computeTimeDurationSecs(timeStart)} secs - Lock will be forced to be released `,
+ message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' - Cannot acquire the lock after ${numberOfTrials} trial(s) and ${Utils.computeTimeDurationSecs(timeStart)} secs - Lock will be forced to be released`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
- if (this.runningWSRequestsMessages.get(wsWrapper.url) === WebSocketAction.OPEN) {
- await this.closeWebSocket(wsAction, action, wsWrapper,
- WebSocketCloseEventStatusCode.CLOSE_TRY_AGAIN_LATER, `${wsAction} > WS Connection ID '${wsWrapper.guid}' has been closed after it has been timed out because onOpen is not finished`);
- throw new Error('CLOSE_TRY_AGAIN_LATER');
- }
+ // Free the lock
+ this.waitingWSMessages--;
break;
}
}
- } finally {
- this.waitingWSMessages--;
- }
}
return true;
}
@@ -694,31 +685,22 @@ export default class JsonOCPPServer extends OCPPServer {
sizeOfCurrentRequestsBytes += sizeof(currentOcppRequests);
numberOfCurrentRequests += Object.keys(currentOcppRequests).length;
}
-
// Log Stats on number of WS Connections
await Logging.logDebug({
tenantID: Constants.DEFAULT_TENANT_ID,
action: ServerAction.WS_SERVER_CONNECTION, module: MODULE_NAME, method: 'monitorWSConnections',
- message: `${this.jsonWSConnections.size} WS connections, ${this.jsonRestWSConnections.size} REST connections, ${this.runningWSMessages} Messages, ${this.runningWSRequestsMessages.size} Requests, ${this.waitingWSMessages} queued WS Message(s)`,
+ message: `${this.jsonWSConnections.size} WS connections, ${this.jsonRestWSConnections.size} REST connections, ${this.runningWSMessages} Messages, ${Object.keys(this.runningWSRequestsMessages).length} Requests, ${this.waitingWSMessages} queued WS Message(s)`,
detailedMessages: [
`${numberOfCurrentRequests} JSON WS Requests cached`,
`${sizeOfCurrentRequestsBytes / 1000} kB used in JSON WS cache`
]
});
- if (global.monitoringServer) {
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_RUNNING_REQUEST_RESPONSE).set(this.runningWSMessages);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_OCPP_CONNECTIONS_COUNT).set(this.jsonWSConnections.size);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_REST_CONNECTIONS_COUNT).set(this.jsonRestWSConnections.size);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_CURRRENT_REQUEST).set(numberOfCurrentRequests);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_RUNNING_REQUEST).set(Object.keys(this.runningWSRequestsMessages).length);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_QUEUED_REQUEST).set(this.waitingWSMessages);
- }
if (this.isDebug()) {
Logging.logConsoleDebug('=====================================');
Logging.logConsoleDebug(`** ${this.jsonWSConnections.size} JSON Connection(s)`);
Logging.logConsoleDebug(`** ${numberOfCurrentRequests} JSON WS Requests in cache with a size of ${sizeOfCurrentRequestsBytes / 1000} kB`);
Logging.logConsoleDebug(`** ${this.jsonRestWSConnections.size} REST Connection(s)`);
- Logging.logConsoleDebug(`** ${this.runningWSRequestsMessages.size} running WS Requests`);
+ Logging.logConsoleDebug(`** ${Object.keys(this.runningWSRequestsMessages).length} running WS Requests`);
Logging.logConsoleDebug(`** ${this.runningWSMessages} running WS Messages (Requests + Responses)`);
Logging.logConsoleDebug(`** ${this.waitingWSMessages} queued WS Message(s)`);
Logging.logConsoleDebug('=====================================');

Back to the org code