我不明白 Java 版 OCA-OCPP 库中的服务器和客户端如何与 EV 充电器配合使用。我不明白必须使用哪个端口来请求 WebSocket 以及如何配置电动汽车充电器 (Mennekes)。
我想要一个向客户端发送请求的服务器,然后客户端向电动汽车充电器发送请求。我不希望客户端必须管理两个单独的连接,一个用于服务器,另一个用于电动汽车充电器。
public class OCPPClient {
private IClientAPI client;
private ClientCoreProfile core;
private JSONConfiguration configuration;
private WssSocket wssSocket;
public void connect(String ipChargingPoint, String port) throws Exception {
// The core profile is mandatory
core = new ClientCoreProfile(new ClientCoreEventHandler() {})
configuration = JSONConfiguration.get();
configuration.setParameter(JSONConfiguration.USERNAME_PARAMETER, "operator");
configuration.setParameter(JSONConfiguration.PASSWORD_PARAMETER, "CX1zRpdK");
configuration.setParameter(JSONConfiguration.TCP_NO_DELAY_PARAMETER, false);
configuration.setParameter(JSONConfiguration.PROXY_PARAMETER, null);
configuration.setParameter(JSONConfiguration.PING_INTERVAL_PARAMETER, 300);
configuration.setParameter(JSONConfiguration.WEBSOCKET_WORKER_COUNT, 4);
configuration.setParameter(JSONConfiguration.CONNECT_TIMEOUT_IN_MS_PARAMETER, 10000);
wssSocket = new WssSocket();
wssSocket.uri(new URI("ws://" + ipChargingPoint + ":" + port));
client = new JSONClient(core, "137640200473", wssSocket, configuration);
client.connect("ws://" + ipChargingPoint + ":" + port, null);
}
public void sendBootNotification() throws Exception {
// Use the feature profile to help create event
Request request = core.createBootNotificationRequest("some vendor", "some model");
// Client returns a promise which will be filled once it receives a confirmation.
client.send(request).whenComplete((s, ex) -> System.out.println(s));
}
public IClientAPI getClient() {
return client;
}
public void disconnect() {
client.disconnect();
}
}
@Data
public class OCPPServer {
private JSONServer server;
private ServerCoreProfile core;
private Map<String, OCPPClient> clients = new HashMap<>();
private JSONConfiguration configuration;
public void started() throws Exception
{
if (server != null)
return;
configuration = JSONConfiguration.get();
configuration.setParameter(JSONConfiguration.USERNAME_PARAMETER, "operator");
configuration.setParameter(JSONConfiguration.PASSWORD_PARAMETER, "CX1zRpdK");
configuration.setParameter(JSONConfiguration.TCP_NO_DELAY_PARAMETER, true);
configuration.setParameter(JSONConfiguration.PROXY_PARAMETER, 3000);
configuration.setParameter(JSONConfiguration.PING_INTERVAL_PARAMETER, 300);
configuration.setParameter(JSONConfiguration.WEBSOCKET_WORKER_COUNT, 4);
configuration.setParameter(JSONConfiguration.CONNECT_TIMEOUT_IN_MS_PARAMETER, 10000);
// The core profile is mandatory
core = new ServerCoreProfile(new ServerCoreEventHandler() {});
server = new JSONServer(core,configuration);
server.open("172.28.2.83", 3000, new ServerEvents() {})
System.out.println("Server started and listening on 172.28.2.83:3000");
}
public void sendClearCacheRequestToClient(String idStation) throws Exception {
// Récupérer le client correspondant à l'ID de la station
OCPPClient client = clients.get(idStation);
if (client != null) {
// Utiliser le profil de fonctionnalité pour aider à créer l'événement
ClearCacheRequest request = core.createClearCacheRequest();
// Le client renvoie une promesse qui sera remplie une fois qu'il recevra une confirmation.
client.getClient().send(request).whenComplete((confirmation, ex) -> {
if (ex != null) {
System.out.println("Failed to send ClearCacheRequest to client: " + ex.getMessage());
} else {
//Le serveur a reçu une confirmation de la station de recharge
System.out.println("ClearCacheRequest sent to client: " + confirmation);
}
});
} else {
System.out.println("No client found for station ID: " + idStation);
}
}
public void stop(){
server.close();
}
public int getActiveSessions() {
return clients.size();
}
public void createSessions(String idStation, String ipStation, String port ) {
OCPPClient client = new OCPPClient();
if(server == null){
try {
started();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
client.connect(ipStation, port); // Assurez-vous que l'adresse IP de la station est correcte
System.out.println("Connected to ws://" + ipStation + " on port " + port);
clients.put(idStation, client);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public OCPPClient getClient(String uuid) {
return clients.get(uuid);
}
}
class OCPPConnectionTest {
@Test
void testClientServerConnection() throws Exception {
OCPPServer server = new OCPPServer();
// Start the server
server.started();
assertEquals(0, server.getActiveSessions());
// Connect the client and send boot notification
server.createSessions("1", "172.28.2.57", "3000");
server.getClient("1").sendBootNotification();
// Verify that the server has an active session
assertEquals(1, server.getActiveSessions());
}
}