原创

如何在 java spring boot 应用程序上管理 ocpp 架构

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I don't understand how the server and client from the OCA-OCPP library for Java work with the EV Charger. I don't understand which port I have to use for requesting WebSocket and how to configure the EV Charger (Mennekes).

I want to have a server that sends requests to the client, and the client sends requests to the EV charger. I don't want to have a client that has to manage two separate connections, one for the server and the other for the EV charger.

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());

    }


}
正文到此结束
热门推荐
本文目录