Add error log when Json client cannot be retrieved
MODIFY
src/server/ocpp/json/JsonRestWSConnection.ts
src/server/ocpp/json/JsonRestWSConnection.ts
@@ -66,9 +66,7 @@ export default class JsonRestWSConnection extends WSConnection {
public async handleRequest(messageId: string, commandName: ServerAction, commandPayload: any): Promise<void> {
// Get the Charging Station
const chargingStation = await ChargingStationStorage.getChargingStation(this.getTenantID(), this.getChargingStationID());
- // Found?
if (!chargingStation) {
- // Error
throw new BackendError({
source: this.getChargingStationID(),
module: MODULE_NAME,
@@ -78,7 +76,7 @@ export default class JsonRestWSConnection extends WSConnection {
});
}
// Get the client from JSON Server
- const chargingStationClient: ChargingStationClient = global.centralSystemJsonServer.getChargingStationClient(this.getTenantID(), chargingStation.id);
+ const chargingStationClient: ChargingStationClient = global.centralSystemJsonServer.getChargingStationClient(this.getTenantID(), this.getChargingStationID());
if (!chargingStationClient) {
throw new BackendError({
source: this.getChargingStationID(),
@@ -89,13 +87,11 @@ export default class JsonRestWSConnection extends WSConnection {
});
}
// Call the client
- let result;
- // Build the method
const actionMethod = commandName[0].toLowerCase() + commandName.substring(1);
// Call
if (typeof chargingStationClient[actionMethod] === 'function') {
// Call the method
- result = await chargingStationClient[actionMethod](commandPayload);
+ const result = await chargingStationClient[actionMethod](commandPayload);
// Send Response
await this.sendMessage(messageId, result, MessageType.CALL_RESULT_MESSAGE, commandName);
} else {

MODIFY
src/server/ocpp/json/JsonWSConnection.ts
src/server/ocpp/json/JsonWSConnection.ts
@@ -150,6 +150,13 @@ export default class JsonWSConnection extends WSConnection {
if (this.isWSConnectionOpen()) {
return this.chargingStationClient;
}
+ Logging.logError({
+ tenantID: this.getTenantID(),
+ source: this.getChargingStationID() ? this.getChargingStationID() : '',
+ module: MODULE_NAME, method: 'getChargingStationClient',
+ action: ServerAction.WS_CONNECTION,
+ message: `Cannot retrieve JSon Client from WS Connection with status '${this.getConnectionStatusString()}'`,
+ });
return null;
}

MODIFY
src/server/ocpp/json/WSConnection.ts
src/server/ocpp/json/WSConnection.ts
import { MessageType, OcppErrorType } from '../../../types/WebSocket';
-import WebSocket, { CloseEvent, ErrorEvent, MessageEvent, OPEN } from 'ws';
+import WebSocket, { CLOSED, CLOSING, CONNECTING, CloseEvent, ErrorEvent, MessageEvent, OPEN } from 'ws';
import BackendError from '../../../exception/BackendError';
import ChargingStationStorage from '../../../storage/mongodb/ChargingStationStorage';
@@ -374,6 +374,25 @@ export default abstract class WSConnection {
return this.wsConnection.readyState === OPEN;
}
+ public getConnectionStatus(): number {
+ return this.wsConnection.readyState;
+ }
+
+ public getConnectionStatusString(): string {
+ switch (this.getConnectionStatus()) {
+ case OPEN:
+ return 'Open';
+ case CONNECTING:
+ return 'Connecting';
+ case CLOSING:
+ return 'Closing';
+ case CLOSED:
+ return 'Closed';
+ default:
+ return `Unknown code '${this.wsConnection.readyState}'`;
+ }
+ }
+
public abstract handleRequest(messageId: string, commandName: ServerAction, commandPayload: any): Promise<void>;
public abstract onError(errorEvent: ErrorEvent): void;

Add error log when Json client cannot be retrieved