Added enum for WS actions
Fixed wrong used actions
MODIFY
src/server/ocpp/json/JsonCentralSystemServer.ts
src/server/ocpp/json/JsonCentralSystemServer.ts
@@ -2,7 +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 { WebSocketCloseEventStatusCode, WebSocketPingResult } from '../../../types/WebSocket';
+import { WebSocketAction, WebSocketCloseEventStatusCode, WebSocketPingResult } from '../../../types/WebSocket';
import CentralSystemConfiguration from '../../../types/configuration/CentralSystemConfiguration';
import CentralSystemServer from '../CentralSystemServer';
@@ -67,10 +67,10 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
const wsWrapper = ws.wsWrapper as WSWrapper;
// Close
wsWrapper.closed = true;
- // Remove connection
- await this.removeWSWrapper('OnClose', ServerAction.WS_SERVER_CONNECTION_CLOSE, wsWrapper);
await this.logWSConnectionClosed(wsWrapper, ServerAction.WS_SERVER_CONNECTION_CLOSE, code,
- `OnClose > WS Connection ID '${wsWrapper.guid}' closed by charging station with code '${code}', reason: '${!Utils.isNullOrEmptyString(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'}'`);
+ // Remove connection
+ await this.removeWSWrapper(WebSocketAction.CLOSE, ServerAction.WS_SERVER_CONNECTION_CLOSE, wsWrapper);
},
ping: async (ws: WebSocket, message: ArrayBuffer) => {
// Convert
@@ -165,7 +165,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'onUpgrade',
action: ServerAction.WS_SERVER_CONNECTION,
- message: `OnUpgrade > WS Connection with URL '${url}' is invalid: No 'OCPP16' or 'REST' in path`
+ message: `${WebSocketAction.UPGRADE} > WS Connection with URL '${url}' is invalid: No 'OCPP16' or 'REST' in path`
});
res.close();
return;
@@ -177,7 +177,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'onUpgrade',
action: ServerAction.WS_SERVER_CONNECTION,
- message: `OnUpgrade > WS Connection with URL '${url}' is invalid, expected protocol 'ocpp1.6' but got '${protocol}'`,
+ message: `${WebSocketAction.UPGRADE} > WS Connection with URL '${url}' is invalid, expected protocol 'ocpp1.6' but got '${protocol}'`,
detailedMessages: { protocol }
});
res.close();
@@ -188,7 +188,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'onUpgrade',
action: ServerAction.WS_SERVER_CONNECTION,
- message: `OnUpgrade > WS Connection with URL '${url}' is invalid, expected protocol 'rest' but got '${protocol}'`,
+ message: `${WebSocketAction.UPGRADE} > WS Connection with URL '${url}' is invalid, expected protocol 'rest' but got '${protocol}'`,
detailedMessages: { protocol }
});
res.close();
@@ -202,7 +202,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
context
);
} catch (error) {
- const message = `OnUpgrade > New WS Connection with URL '${url}' failed with error: ${error.message as string}`;
+ const message = `${WebSocketAction.UPGRADE} > New WS Connection with URL '${url}' failed with error: ${error.message as string}`;
res.writeStatus('500');
res.end(message);
this.isDebug() && Logging.logConsoleDebug(message);
@@ -241,8 +241,8 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
await Logging.logException(error, ServerAction.WS_SERVER_CONNECTION_OPEN, MODULE_NAME, 'onOpen', wsWrapper.tenantID);
}
// Close WS
- await this.closeWebSocket('OnOpen', ServerAction.WS_SERVER_CONNECTION_OPEN, wsWrapper, WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
- `OnOpen > WS Connection ID '${wsWrapper.guid}' has been rejected and closed by server due to an exception: ${error.message as string}`);
+ await this.closeWebSocket(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, wsWrapper, WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
+ `${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been rejected and closed by server due to an exception: ${error.message as string}`);
} finally {
this.runningWSMessages--;
this.releaseLockForWSMessageRequest(wsWrapper);
@@ -264,7 +264,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
await Logging.logDebug({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.WS_SERVER_CONNECTION_OPEN, module: MODULE_NAME, method: 'checkAndStoreWSOpenedConnection',
- message: `OnOpen > WS Connection ID '${wsWrapper.guid}' is being checked ('${wsWrapper.url}')`,
+ message: `${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' is being checked ('${wsWrapper.url}')`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Initialize (check of Tenant, Token, Charging Station -> Can take time)
@@ -282,8 +282,8 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
// Check already existing WS Connection
await this.checkAndCloseIdenticalOpenedWSConnection(wsWrapper, wsConnection);
// Keep WS connection in cache
- await this.setWSConnection(ServerAction.WS_SERVER_CONNECTION_OPEN, wsConnection, wsWrapper);
- const message = `OnOpen > WS Connection ID '${wsWrapper.guid}' has been accepted in ${Utils.computeTimeDurationSecs(timeStart)} secs`;
+ await this.setWSConnection(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, wsConnection, wsWrapper);
+ const message = `${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been accepted in ${Utils.computeTimeDurationSecs(timeStart)} secs`;
await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
@@ -297,7 +297,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
});
} else {
await this.logWSConnectionClosed(wsWrapper, ServerAction.WS_SERVER_CONNECTION_OPEN, WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
- `OnOpen > WS Connection ID '${wsWrapper.guid}' has been closed during initialization in ${Utils.computeTimeDurationSecs(timeStart)} secs ('${wsWrapper.url}')`);
+ `${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been closed during initialization in ${Utils.computeTimeDurationSecs(timeStart)} secs ('${wsWrapper.url}')`);
}
}
@@ -318,11 +318,11 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_CONNECTION, module: MODULE_NAME, method: 'checkAndCloseIdenticalOpenedWSConnection',
- message: `OnOpen > Existing WS Connection ID '${existingWSWrapper.guid}' will be closed and replaced by new incoming one with ID '${wsWrapper.guid}'`,
+ message: `${WebSocketAction.OPEN} > Existing WS Connection ID '${existingWSWrapper.guid}' will be closed and replaced by new incoming one with ID '${wsWrapper.guid}'`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
- await this.closeWebSocket('OnOpen', ServerAction.WS_SERVER_CONNECTION_OPEN, existingWSConnection.getWS(), WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
- `OnOpen > Existing WS Connection ID '${existingWSWrapper.guid}' has been closed successfully by the server`);
+ await this.closeWebSocket(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, existingWSConnection.getWS(), WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
+ `${WebSocketAction.OPEN} > Existing WS Connection ID '${existingWSWrapper.guid}' has been closed successfully by the server`);
}
}
}
@@ -376,7 +376,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
this.releaseLockForWSMessageRequest(wsWrapper, ocppMessageType);
}
} catch (error) {
- const logMessage = `OnMessage > WS Connection ID '${wsWrapper.guid}' got error while processing WS Message: ${error.message as string}`;
+ const logMessage = `${WebSocketAction.MESSAGE} > WS Connection ID '${wsWrapper.guid}' got error while processing WS Message: ${error.message as string}`;
await Logging.logError({
...LoggingHelper.getWSWrapperProperties(wsWrapper),
action: ServerAction.WS_SERVER_MESSAGE,
@@ -407,11 +407,11 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_MESSAGE,
module: MODULE_NAME, method: 'checkWSConnectionFromOnMessage',
- message: `OnMessage > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an unreferenced WS Connection, it will be then added in the WS cache`,
+ message: `${WebSocketAction.MESSAGE} > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an unreferenced WS Connection, it will be then added in the WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Add WS connection from OnMessage in cache
- await this.setWSConnection(ServerAction.WS_SERVER_MESSAGE, wsConnection, wsWrapper);
+ await this.setWSConnection(WebSocketAction.MESSAGE, ServerAction.WS_SERVER_MESSAGE, wsConnection, wsWrapper);
return;
}
// Should have the same GUID
@@ -422,7 +422,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_MESSAGE,
module: MODULE_NAME, method: 'checkWSConnectionFromOnMessage',
- message: `OnMessage > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an already referenced WS Connection ID '${wsExistingWrapper.guid}' in WS cache, ping will be performed...`,
+ message: `${WebSocketAction.MESSAGE} > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an already referenced WS Connection ID '${wsExistingWrapper.guid}' in WS cache, ping will be performed...`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper), wsExistingWrapper: this.getWSWrapperData(wsExistingWrapper) }
});
// Ping
@@ -433,24 +433,24 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_MESSAGE,
module: MODULE_NAME, method: 'checkWSConnectionFromOnMessage',
- message: `OnMessage > Existing WS Connection ID '${wsExistingWrapper.guid}' ping succeded meaning multiple WS connections are opened by the same charging station, existing one will be closed and replaced by new one with ID '${wsWrapper.guid}'`,
+ message: `${WebSocketAction.MESSAGE} > Existing WS Connection ID '${wsExistingWrapper.guid}' ping succeded meaning multiple WS connections are opened by the same charging station, existing one will be closed and replaced by new one with ID '${wsWrapper.guid}'`,
detailedMessages: { wsExistingWrapper: this.getWSWrapperData(wsExistingWrapper), wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Close WS
- await this.closeWebSocket('OnMessage', ServerAction.WS_SERVER_MESSAGE, wsExistingWrapper,
- WebSocketCloseEventStatusCode.CLOSE_ABNORMAL, `OnMessage > Existing WS Connection ID '${wsExistingWrapper.guid}' has been closed successfully by server (duplicate WS Connection)`);
+ await this.closeWebSocket(WebSocketAction.MESSAGE, ServerAction.WS_SERVER_MESSAGE, wsExistingWrapper,
+ WebSocketCloseEventStatusCode.CLOSE_ABNORMAL, `${WebSocketAction.MESSAGE} > Existing WS Connection ID '${wsExistingWrapper.guid}' has been closed successfully by server (duplicate WS Connection)`);
} else {
await Logging.logWarning({
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_MESSAGE,
module: MODULE_NAME, method: 'checkWSConnectionFromOnMessage',
- message: `OnMessage > Existing WS Connection ID '${wsExistingWrapper.guid}' ping failed, new WS Connection ID '${wsWrapper.guid}' will be then added in the WS cache`,
+ message: `${WebSocketAction.MESSAGE} > Existing WS Connection ID '${wsExistingWrapper.guid}' ping failed, new WS Connection ID '${wsWrapper.guid}' will be then added in the WS cache`,
detailedMessages: { wsExistingWrapper: this.getWSWrapperData(wsExistingWrapper), wsWrapper: this.getWSWrapperData(wsWrapper) }
});
}
// Keep WS connection in cache
- await this.setWSConnection(ServerAction.WS_SERVER_MESSAGE, wsConnection, wsWrapper);
+ await this.setWSConnection(WebSocketAction.MESSAGE, ServerAction.WS_SERVER_MESSAGE, wsConnection, wsWrapper);
}
}
@@ -540,17 +540,17 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_CONNECTION_PING, module: MODULE_NAME, method: 'pingWebSocket',
- message: `Ping > Failed to ping the WS Connection ID '${wsWrapper.guid}' after ${wsWrapper.nbrPingFailed} trial(s), will be removed from WS cache`,
+ message: `${WebSocketAction.PING} > Failed to ping the WS Connection ID '${wsWrapper.guid}' after ${wsWrapper.nbrPingFailed} trial(s), will be removed from WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper), error: error.stack }
});
- await this.closeWebSocket('Ping', ServerAction.WS_SERVER_CONNECTION_PING, wsWrapper,
- WebSocketCloseEventStatusCode.CLOSE_ABNORMAL, `Ping > WS Connection ID '${wsWrapper.guid}' has been closed by server after ${wsWrapper.nbrPingFailed} failed ping`);
+ await this.closeWebSocket(WebSocketAction.PING, ServerAction.WS_SERVER_CONNECTION_PING, wsWrapper,
+ WebSocketCloseEventStatusCode.CLOSE_ABNORMAL, `${WebSocketAction.PING} > WS Connection ID '${wsWrapper.guid}' has been closed by server after ${wsWrapper.nbrPingFailed} failed ping`);
} else {
await Logging.logWarning({
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_CONNECTION_PING, module: MODULE_NAME, method: 'pingWebSocket',
- message: `Ping > Failed to ping the WS Connection ID '${wsWrapper.guid}' after ${wsWrapper.nbrPingFailed} trial(s) (${Constants.WS_MAX_NBR_OF_FAILED_PINGS - wsWrapper.nbrPingFailed} remaining)`,
+ message: `${WebSocketAction.PING} > Failed to ping the WS Connection ID '${wsWrapper.guid}' after ${wsWrapper.nbrPingFailed} trial(s) (${Constants.WS_MAX_NBR_OF_FAILED_PINGS - wsWrapper.nbrPingFailed} remaining)`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper), error: error.stack }
});
}
@@ -562,7 +562,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
}
}
- private async closeWebSocket(wsAction: string, action: ServerAction, wsWrapper: WSWrapper, code: WebSocketCloseEventStatusCode, message: string): Promise<void> {
+ private async closeWebSocket(wsAction: WebSocketAction, action: ServerAction, wsWrapper: WSWrapper, code: WebSocketCloseEventStatusCode, message: string): Promise<void> {
// Close WS
if (!wsWrapper.closed) {
try {
@@ -583,7 +583,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
await this.removeWSWrapper(wsAction, action, wsWrapper);
}
- private async setWSConnection(action: ServerAction, wsConnection: WSConnection, wsWrapper: WSWrapper) {
+ private async setWSConnection(wsAction: WebSocketAction, action: ServerAction, wsConnection: WSConnection, wsWrapper: WSWrapper) {
// Reference a Json WebSocket connection object
if (wsWrapper.protocol === WSServerProtocol.OCPP16) {
this.jsonWSConnections.set(wsConnection.getID(), wsConnection as JsonWSConnection);
@@ -591,7 +591,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action, module: MODULE_NAME, method: 'setWSConnection',
- message: `OnMessage > WS Connection ID '${wsWrapper.guid}' has been added in the WS cache`,
+ message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' has been added in the WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
}
@@ -601,7 +601,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action, module: MODULE_NAME, method: 'setWSConnection',
- message: `OnMessage > WS Connection ID '${wsWrapper.guid}' has been added in the WS cache`,
+ message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' has been added in the WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
}
@@ -617,7 +617,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
}
}
- private async removeWSWrapper(wsAction: string, action: ServerAction, wsWrapper: WSWrapper): Promise<void> {
+ private async removeWSWrapper(wsAction: WebSocketAction, action: ServerAction, wsWrapper: WSWrapper): Promise<void> {
if (wsWrapper.protocol === WSServerProtocol.OCPP16) {
await this.removeWSConnection(
wsAction, action, wsWrapper.wsConnection, this.jsonWSConnections);
@@ -628,7 +628,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
}
}
- private async removeWSConnection(wsAction: string, action: ServerAction, wsConnection: WSConnection, wsConnections: Map<string, WSConnection>): Promise<void> {
+ private async removeWSConnection(wsAction: WebSocketAction, action: ServerAction, wsConnection: WSConnection, wsConnections: Map<string, WSConnection>): Promise<void> {
if (wsConnection) {
const wsWrapper = wsConnection.getWS();
const existingWsConnection = wsConnections.get(wsConnection.getID());
@@ -642,7 +642,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
tenantID: Constants.DEFAULT_TENANT,
chargingStationID: wsWrapper.chargingStationID,
action, module: MODULE_NAME, method: 'setWSConnection',
- message: `OnMessage > WS Connection ID '${wsWrapper.guid}' has been removed from the WS cache`,
+ message: `${wsAction} > WS Connection ID '${wsWrapper.guid}' has been removed from the WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
} else {
@@ -742,15 +742,6 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
});
}
}
- } else {
- const message = `No ${type} WS connection to ping`;
- this.isDebug() && Logging.logConsoleDebug(message);
- await Logging.logDebug({
- tenantID: Constants.DEFAULT_TENANT,
- module: MODULE_NAME, method: 'checkAndCleanupWebSockets',
- action: ServerAction.WS_SERVER_CONNECTION_PING,
- message
- });
}
}

MODIFY
src/types/WebSocket.ts
src/types/WebSocket.ts
@@ -26,6 +26,15 @@ export interface WebSocketPingResult {
errorMessage?: string;
}
+export enum WebSocketAction {
+ UPGRADE = 'Upgrade',
+ OPEN = 'Open',
+ CLOSE = 'Close',
+ PING = 'Ping',
+ PONG = 'Pong',
+ MESSAGE = 'Message',
+}
+
export enum WebSocketCloseEventStatusCode {
CLOSE_NORMAL = 1000,
CLOSE_GOING_AWAY = 1001,

Added enum for WS actions
Fixed wrong used actions