wjc
2026-03-24 c4ae4589c6c001329ebb731589b209e8ddcbf7ca
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -13,6 +13,8 @@
import com.hdl.photovoltaic.bean.PageNumberObject;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.enums.DeviceType;
import com.hdl.photovoltaic.enums.PowerStationType;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.TcpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
@@ -38,7 +40,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -102,13 +103,16 @@
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    GatewayBean gatewayBean = list.get(i);
                    if (!getGatewaySpk().contains(gatewayBean.getGatewayType())) {
                        continue;
                    }
                    if (TextUtils.isEmpty(gatewayBean.getDevice_mac())
                            || TextUtils.isEmpty(gatewayBean.getSid())
                            || TextUtils.isEmpty(gatewayBean.getOid())) {
                        continue;
                    if (gatewayBean.isActivate()) {
                        //已激活(getGatewayType,getDevice_mac,getSid,getOid)才有值
                        if (!getGatewaySpk().contains(gatewayBean.getGatewayType())) {
                            continue;
                        }
                        if (TextUtils.isEmpty(gatewayBean.getDevice_mac())
                                || TextUtils.isEmpty(gatewayBean.getSid())
                                || TextUtils.isEmpty(gatewayBean.getOid())) {
                            continue;
                        }
                    }
                    //用homeId筛选当前住宅的逆变器列表
                    if (gatewayBean.getHomeId().equals(homeId)) {
@@ -137,9 +141,9 @@
        try {
            List<GatewayBean> newList = new ArrayList<>();
            //云端有设备列表,本地没有,即HDLLinkLocalGateway.getInstance().getGatewayList()没有,里面会调用refreshGatewayCacheData()方法虚拟一个逆变器实体添加本地列表里面;
            List<GatewayBean> list = getCurrentHomeGatewayList(homeId);
            if (list != null && list.size() > 0) {
                newList.addAll(list);
            List<GatewayBean> localList = getCurrentHomeGatewayList(homeId);
            if (localList != null && localList.size() > 0) {
                newList.addAll(localList);
            }
            //有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地newList列表为主,搜索多少个设备就显示多少个
@@ -149,6 +153,7 @@
                    CloudInverterDeviceBean cloudInverterDevice = cloudInverterDeviceList.get(i);
                    for (GatewayBean gatewayBean : newList) {
                        if (cloudInverterDevice.getOsn().equals(gatewayBean.getDevice_mac())) {
                            //获取更全逆变器详情字段
                            checkNewList.add(gatewayBean);
                            break;
                        }
@@ -246,7 +251,7 @@
        if (TextUtils.isEmpty(deviceId)) {
            return;
        }
        List<GatewayBean> list = getCurrentHomeGatewayList(homeId);
        List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
        if (list == null || list.size() == 0) {
            return;
        }
@@ -258,9 +263,10 @@
                break;
            }
        }
        if (index > 0) {
        if (index > -1) {
            list.remove(index);
        }
    }
    /**
@@ -607,6 +613,43 @@
     */
    public void getCloudInverterDeviceList(String homeId, CloudCallBeak<List<CloudInverterDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(null);
                    }
                }
                Type type = new TypeToken<List<CloudInverterDeviceBean>>() {
                }.getType();
                List<CloudInverterDeviceBean> list = new Gson().fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取云端BMS设备列表
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 回调
     */
    public void getCloudBmsDeviceList(String homeId, CloudCallBeak<List<CloudInverterDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_bmsDevices;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        // json.addProperty("zoneType", "password");//区域
@@ -1184,131 +1227,208 @@
    }
    /**
     * 获取逆变器列表,整合云端逆变器列表和本地逆变器列表(设备类型有:并网逆变器,离线逆变器,负载中心设备);
     * 注意:有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地为主,搜索多少个设备就显示多少个
     * 设备清空运行数据
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 返回逆变器列表
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getCurrentHomeLocalAndCloudGatewayList(String homeId, CloudCallBeak<List<GatewayBean>> cloudCallBeak) {
        searchCurrentHomeGateway(homeId, new GatewayCallBack() {
    public void DeviceClearData(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.DEVICE_RUN_DATA_CLEAR;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                //向云端获取逆变器列表
                getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> inverterDeviceList) {
                        //向云端获取负载中心列表
                        getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                            @Override
                            public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                if (loadCentreDeviceList != null) {
                                    //合并负载中心设备列表,然后在一起做处理
                                    inverterDeviceList.addAll(loadCentreDeviceList);
                                }
                                //设置设备的远程信息
                                setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                //向云端获取负载中心设备列表失败后,仍然处理逆变器设备列表
                                //设置设备的远程信息
                                setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                            }
                        });
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表;
                        //向云端获取负载中心列表
                        getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                            @Override
                            public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                if (loadCentreDeviceList == null) {
                                    if (cloudCallBeak != null) {
                                        //没有数据,默认返回本地逆变器列表;
                                        cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, null, false));
                                    }
                                    return;
                                }
                                //设置设备的远程信息
                                setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                //向云端获取负载中心设备列表失败后,默认返回本地逆变器列表;
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, null, false));
                                }
                            }
                        });
                    }
                });
            public void onSuccess(String json) {
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
//                //外网进来先【默认】清空本地逆变器列表
//                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                //向云端获取逆变器列表
                getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> cloudInverterList) {
                        //再向云端获取负载中心列表
                        getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                            @Override
                            public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                List<CloudInverterDeviceBean> newList = new ArrayList<>();
                                if (cloudInverterList != null) {
                                    //添加逆变器设备列表
                                    newList.addAll(cloudInverterList);
                                }
                                if (loadCentreDeviceList != null) {
                                    //添加负载中心设备列表
                                    newList.addAll(loadCentreDeviceList);
                                }
                                //整合设备列表后,再一起设置设备的远程信息
                                setDeviceRemoteInfo(newList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                //设置设备的远程信息
                                setDeviceRemoteInfo(cloudInverterList, homeId, cloudCallBeak);
                            }
                        });
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        //向云端获取逆变器列表失败
                        //再向云端获取负载中心列表
                        getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                            @Override
                            public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                //设置设备的远程信息
                                setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                //都是获取失败的话,直接返回空列表;
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(new ArrayList<>());
                                }
                            }
                        });
                    }
                });
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
    /**
     * 获取逆变器列表,整合云端逆变器列表和本地逆变器列表(设备类型有:并网逆变器,离线逆变器,负载中心设备);
     * 注意:有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地为主,搜索多少个设备就显示多少个
     *
     * @param homeId           住宅id
     * @param cloudCallBeak    返回逆变器列表
     * @param powerStationType 电站类型(逆变器,BMS电站)
     */
    public void getCurrentHomeLocalAndCloudGatewayList(String homeId, String powerStationType, CloudCallBeak<List<GatewayBean>> cloudCallBeak) {
//        HDLLinkLocalGateway.getInstance().getGatewayList().clear();//先清空之前逆变器列表
        if (powerStationType.equals(PowerStationType.BMS)) {
            searchCurrentHomeGateway(homeId, new GatewayCallBack() {
                @Override
                public void onSuccess(List<GatewayBean> gatewayBeanList) {
                    //向云端获取逆变器列表
                    getCloudBmsDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                        @Override
                        public void onSuccess(List<CloudInverterDeviceBean> inverterDeviceList) {
                            //设置设备的远程信息
                            setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表;
                            //向云端获取负载中心设备列表失败后,默认返回本地逆变器列表;
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, null, false));
                            }
                        }
                    });
                }
                @Override
                public void onError(HDLLinkException e) {
//                //外网进来先【默认】清空本地逆变器列表
//                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                    //向云端获取逆变器列表
                    getCloudBmsDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                        @Override
                        public void onSuccess(List<CloudInverterDeviceBean> cloudInverterList) {
                            //整合设备列表后,再一起设置设备的远程信息
                            setDeviceRemoteInfo(cloudInverterList, homeId, cloudCallBeak);
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            //向云端获取逆变器列表失败
                            //都是获取失败的话,直接返回空列表;
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(new ArrayList<>());
                            }
                        }
                    });
                }
            });
        } else {
            searchCurrentHomeGateway(homeId, new GatewayCallBack() {
                @Override
                public void onSuccess(List<GatewayBean> gatewayBeanList) {
                    //向云端获取逆变器列表
                    getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                        @Override
                        public void onSuccess(List<CloudInverterDeviceBean> inverterDeviceList) {
                            //向云端获取负载中心列表
                            getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                                @Override
                                public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                    if (loadCentreDeviceList != null) {
                                        //合并负载中心设备列表,然后在一起做处理
                                        inverterDeviceList.addAll(loadCentreDeviceList);
                                    }
                                    //设置设备的远程信息
                                    setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                    //向云端获取负载中心设备列表失败后,仍然处理逆变器设备列表
                                    //设置设备的远程信息
                                    setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                                }
                            });
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表;
                            //向云端获取负载中心列表
                            getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                                @Override
                                public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                    if (loadCentreDeviceList == null) {
                                        if (cloudCallBeak != null) {
                                            //没有数据,默认返回本地逆变器列表;
                                            cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, null, false));
                                        }
                                        return;
                                    }
                                    //设置设备的远程信息
                                    setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                    //向云端获取负载中心设备列表失败后,默认返回本地逆变器列表;
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, null, false));
                                    }
                                }
                            });
                        }
                    });
                }
                @Override
                public void onError(HDLLinkException e) {
//                //外网进来先【默认】清空本地逆变器列表
//                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                    //向云端获取逆变器列表
                    getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                        @Override
                        public void onSuccess(List<CloudInverterDeviceBean> cloudInverterList) {
                            //再向云端获取负载中心列表
                            getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                                @Override
                                public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                    List<CloudInverterDeviceBean> newList = new ArrayList<>();
                                    if (cloudInverterList != null) {
                                        //添加逆变器设备列表
                                        newList.addAll(cloudInverterList);
                                    }
                                    if (loadCentreDeviceList != null) {
                                        //添加负载中心设备列表
                                        newList.addAll(loadCentreDeviceList);
                                    }
                                    //整合设备列表后,再一起设置设备的远程信息
                                    setDeviceRemoteInfo(newList, homeId, cloudCallBeak);
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                    //设置设备的远程信息
                                    setDeviceRemoteInfo(cloudInverterList, homeId, cloudCallBeak);
                                }
                            });
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            //向云端获取逆变器列表失败
                            //再向云端获取负载中心列表
                            getLoadCentreDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                                @Override
                                public void onSuccess(List<CloudInverterDeviceBean> loadCentreDeviceList) {
                                    //设置设备的远程信息
                                    setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                    //都是获取失败的话,直接返回空列表;
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(new ArrayList<>());
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }
    }
    /**
@@ -1329,19 +1449,38 @@
        AtomicInteger atomicInteger = new AtomicInteger(0);
        for (int i = 0; i < list.size(); i++) {
            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
            if (cloudInverterDeviceBean == null || !cloudInverterDeviceBean.isActivate()) {
                //未激活设备
                atomicInteger.set(atomicInteger.get() + 1);
                //注意:这个逆变器只能本地控制(因为拿不到mqtt通讯秘钥以及GatewayID,无法远程控制)
                //更新逆变器本地缓存信息
                refreshGatewayCacheData(false, cloudInverterDeviceBean, null, homeId);
                //到最后一条,才处理回调
                if (atomicInteger.get() == list.size()) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list, false));
            if (cloudInverterDeviceBean == null) {
                if (cloudInverterDeviceBean.getDeviceType().equals(DeviceType.bms)) {
                    //未激活设备
                    atomicInteger.set(atomicInteger.get() + 1);
                    //注意:这个逆变器只能本地控制(因为拿不到mqtt通讯秘钥以及GatewayID,无法远程控制)
                    //更新逆变器本地缓存信息
                    refreshGatewayCacheData(false, cloudInverterDeviceBean, null, homeId);
                    //到最后一条,才处理回调
                    if (atomicInteger.get() == list.size()) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list, false));
                        }
                    }
                    HdlLogLogic.print("设备未激活(mac:" + cloudInverterDeviceBean.getOsn() + "))", true);
                    continue;
                } else {
                    if (!cloudInverterDeviceBean.isActivate()) {
                        //未激活设备
                        atomicInteger.set(atomicInteger.get() + 1);
                        //注意:这个逆变器只能本地控制(因为拿不到mqtt通讯秘钥以及GatewayID,无法远程控制)
                        //更新逆变器本地缓存信息
                        refreshGatewayCacheData(false, cloudInverterDeviceBean, null, homeId);
                        //到最后一条,才处理回调
                        if (atomicInteger.get() == list.size()) {
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list, false));
                            }
                        }
                        HdlLogLogic.print("设备未激活(mac:" + cloudInverterDeviceBean.getOsn() + "))", true);
                        continue;
                    }
                }
                HdlLogLogic.print("设备未激活(mac:" + cloudInverterDeviceBean.getOsn() + "))", true);
            }
            //获取远程设备通讯信息(拿到网关ID,mqtt通讯秘钥)
            getDeviceRemoteInfo(homeId, cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak<DeviceRemoteInfo>() {
@@ -1846,8 +1985,13 @@
        }
        gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk
        gatewayBean.setActivate(cloudInverterDeviceBean.isActivate());//是否激活设备(true:激活;false:未激活)
        gatewayBean.setPopUpgrade(cloudInverterDeviceBean.isPopUpgrade());//改设备是否有弹窗升级
        gatewayBean.setSubPopUpgrade(cloudInverterDeviceBean.isSubPopUpgrade());//下挂设备是否有弹窗升级
        if (cloudInverterDeviceBean.getDeviceType().equals(DeviceType.bms)) {
            gatewayBean.setDeviceOidId(cloudInverterDeviceBean.getDeviceOidId());
            gatewayBean.setFwVersion(cloudInverterDeviceBean.getFwVersion());
        }
    }
@@ -1861,6 +2005,7 @@
        spks.add(INVERTER_DEVICE_SPK);
        spks.add(LC_SPK);
        spks.add(OFF_INVERTER_OG_SPK);
        spks.add(HDL_BMS_SPK);
        return spks;
    }
@@ -1920,4 +2065,8 @@
     * 离线逆变器spk
     */
    public final String OFF_INVERTER_OG_SPK = "energy.inverter_og";
    /**
     * BMS设备spk
     */
    public final String HDL_BMS_SPK = "energy.hdl_bms";
}