You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Spring Boot中OCPP架构管理及充电桩交互配置咨询

问题描述

我不清楚Open Charge Alliance(OCA)推出的Java版OCA-OCPP库中,服务端与客户端如何和Mennekes电动汽车充电桩交互;也不知道WebSocket请求该使用哪个端口,以及如何配置Mennekes充电桩。

我希望实现这样的架构:服务端向客户端发送请求,客户端向充电桩发送请求,不想让客户端维护分别指向服务端和充电桩的两个独立连接。

相关代码示例

OCPPClient 客户端实现类

public class OCPPClient {
    private IClientAPI client;
    private ClientCoreProfile core;
    private JSONConfiguration configuration;
    private WssSocket wssSocket;

    public void connect(String ipChargingPoint, String port) throws Exception {

        // 核心配置文件是必填的
        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 {

        // 使用功能配置文件辅助创建请求
        Request request = core.createBootNotificationRequest("some vendor", "some model");

        // 客户端会返回一个Promise,收到确认后会被填充
        client.send(request).whenComplete((s, ex) -> System.out.println(s));
    }

    public IClientAPI getClient() {
        return client;
    }

    public void disconnect() {
        client.disconnect();
    }
}

OCPPServer 服务端实现类

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

        // 核心配置文件是必填的
        core = new ServerCoreProfile(new ServerCoreEventHandler() {});

        server = new JSONServer(core, configuration);
        server.open("172.28.2.83", 3000, new ServerEvents() {});
        System.out.println("服务端已启动,监听172.28.2.83:3000");
    }

    public void sendClearCacheRequestToClient(String idStation) throws Exception {
        // 获取对应充电桩ID的客户端
        OCPPClient client = clients.get(idStation);

        if (client != null) {
            // 使用功能配置文件辅助创建请求
            ClearCacheRequest request = core.createClearCacheRequest();

            // 客户端会返回一个Promise,收到确认后会被填充
            client.getClient().send(request).whenComplete((confirmation, ex) -> {
                if (ex != null) {
                    System.out.println("向客户端发送ClearCacheRequest失败: " + ex.getMessage());
                } else {
                    // 服务端已收到充电桩的确认信息
                    System.out.println("已向客户端发送ClearCacheRequest: " + confirmation);
                }
            });
        } else {
            System.out.println("未找到对应充电桩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); // 确保充电桩的IP地址正确
            System.out.println("已连接到ws://" + ipStation + ":" + port);
            clients.put(idStation, client);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public OCPPClient getClient(String uuid) {
        return clients.get(uuid);
    }
}

OCPPConnectionTest 连接测试类

class OCPPConnectionTest {

    @Test
    void testClientServerConnection() throws Exception {
        OCPPServer server = new OCPPServer();

        // 启动服务端
        server.started();
        assertEquals(0, server.getActiveSessions());

        // 连接客户端并发送启动通知
        server.createSessions("1", "172.28.2.57", "3000");
        server.getClient("1").sendBootNotification();

        // 验证服务端存在一个活跃会话
        assertEquals(1, server.getActiveSessions());
    }
}

内容的提问来源于stack exchange,提问作者Romain Geurts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 06:05:56