Handle K8s Json multi instance
MODIFY
src/client/ocpp/ChargingStationClientFactory.ts
src/client/ocpp/ChargingStationClientFactory.ts
-import ChargingStation from '../../types/ChargingStation';
-import global from '../../types/GlobalType';
-import { OCPPProtocol } from '../../types/ocpp/OCPPServer';
import Tenant, { TenantComponents } from '../../types/Tenant';
-import Utils from '../../utils/Utils';
-import OCPIClientFactory from '../ocpi/OCPIClientFactory';
+
+import ChargingStation from '../../types/ChargingStation';
import ChargingStationClient from './ChargingStationClient';
import JsonRestChargingStationClient from './json/JsonRestChargingStationClient';
+import OCPIClientFactory from '../ocpi/OCPIClientFactory';
+import { OCPPProtocol } from '../../types/ocpp/OCPPServer';
import SoapChargingStationClient from './soap/SoapChargingStationClient';
-
-
-const MODULE_NAME = 'ChargingStationClientFactory';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
export default class ChargingStationClientFactory {
public static async getChargingStationClient(tenant: Tenant, chargingStation: ChargingStation): Promise<ChargingStationClient> {
@@ -20,7 +18,7 @@ export default class ChargingStationClientFactory {
// JSON
case OCPPProtocol.JSON:
// Json Server
- if (global.centralSystemJsonServer) {
+ if (global.centralSystemJsonServer?.hasChargingStationConnected(tenant, chargingStation)) {
// Get the local WS Connection Client
chargingClient = await global.centralSystemJsonServer.getChargingStationClient(tenant, chargingStation);
} else {

MODIFY
src/client/ocpp/json/JsonRestChargingStationClient.ts
src/client/ocpp/json/JsonRestChargingStationClient.ts
@@ -24,8 +24,16 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
super();
this.tenantID = tenantID;
// Get URL
- let chargingStationURL = chargingStation.chargingStationURL;
- if (!chargingStationURL) {
+ let jsonServerURL: string;
+ // Check K8s
+ if (process.env.POD_NAME && chargingStation.cloudHostIP) {
+ // Use K8s IP, always in wss
+ jsonServerURL = `wss://${chargingStation.cloudHostIP}`;
+ console.log('jsonServerURL K8s', jsonServerURL);
+ } else {
+ jsonServerURL = chargingStation.chargingStationURL;
+ console.log('jsonServerURL AWS', jsonServerURL);
+ if (!jsonServerURL) {
throw new BackendError({
...LoggingHelper.getChargingStationProperties(chargingStation),
module: MODULE_NAME, method: 'constructor',
@@ -33,11 +41,12 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
detailedMessages: { chargingStation }
});
}
+ }
// Check URL: remove starting and trailing '/'
- if (chargingStationURL.endsWith('/')) {
- chargingStationURL = chargingStationURL.substring(0, chargingStationURL.length - 1);
+ if (jsonServerURL.endsWith('/')) {
+ jsonServerURL = jsonServerURL.substring(0, jsonServerURL.length - 1);
}
- this.serverURL = `${chargingStationURL}/REST/${tenantID}/${chargingStation.tokenID}/${chargingStation.id}`;
+ this.serverURL = `${jsonServerURL}/REST/${tenantID}/${chargingStation.tokenID}/${chargingStation.id}`;
this.chargingStation = chargingStation;
this.requests = {};
}

MODIFY
src/server/ocpp/json/JsonCentralSystemServer.ts
src/server/ocpp/json/JsonCentralSystemServer.ts
@@ -125,6 +125,10 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
return jsonWebSocket.getChargingStationClient();
}
+ public hasChargingStationConnected(tenant: Tenant, chargingStation: ChargingStation): boolean {
+ return this.jsonWSConnections.has(`${tenant.id}~${chargingStation.id}`);
+ }
+
private async onUpgrade(res: uWS.HttpResponse, req: uWS.HttpRequest, context: uWS.us_socket_context_t) {
// Check for WS connection over HTTP
try {

Handle K8s Json multi instance