wjc
2025-05-19 1cfe43744e4424bc098ca5c51e12c0e5b93ae124
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -8,6 +8,8 @@
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.InverterBean;
import com.hdl.photovoltaic.bean.LocalResponse;
import com.hdl.photovoltaic.bean.PageNumberObject;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
@@ -19,8 +21,10 @@
import com.hdl.photovoltaic.listener.LinkCallBack;
import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.DeviceInfoBean;
import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.NotRunBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
@@ -34,7 +38,10 @@
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;
import java.util.concurrent.atomic.AtomicInteger;
@@ -83,23 +90,95 @@
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
     *
     * @param homeId 电站ID
     */
    public List<GatewayBean> getCurrentHomeGatewayList(String homeId) {
        List<GatewayBean> newList = new ArrayList<>();
        List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) {
                    continue;
                }
                //用homeId筛选当前住宅的逆变器列表
                if (gatewayBean.getHomeId().equals(homeId)) {
                    newList.add(gatewayBean);
        try {
            List<GatewayBean> newList = new ArrayList<>();
            //云端有设备列表,本地没有,即HDLLinkLocalGateway.getInstance().getGatewayList()没有,里面会调用refreshGatewayCacheData()方法虚拟一个逆变器实体添加本地列表里面;
            List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
            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;
                    }
                    //用homeId筛选当前住宅的逆变器列表
                    if (gatewayBean.getHomeId().equals(homeId)) {
                        newList.add(gatewayBean);
                    }
                }
            }
            return newList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
        return newList;
    }
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
     * 可以设置是否整合本地逆变器列表数据
     *
     * @param homeId                  电站ID
     * @param cloudInverterDeviceList 云端上的逆变器列表(并网,离网,负载中心)
     * @param isIntegrateLocalData    是否强制整合本地逆变器数据(false:不需要(以云端列表为主(逆变器,负载中心)多少个显示多少个),true:需要(有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地列表为主,搜索多少个设备就显示多少个))
     */
    public List<GatewayBean> IntegrateCloudAndLocalData(String homeId, List<CloudInverterDeviceBean> cloudInverterDeviceList, boolean isIntegrateLocalData) {
        try {
            List<GatewayBean> newList = new ArrayList<>();
            //云端有设备列表,本地没有,即HDLLinkLocalGateway.getInstance().getGatewayList()没有,里面会调用refreshGatewayCacheData()方法虚拟一个逆变器实体添加本地列表里面;
            List<GatewayBean> list = getCurrentHomeGatewayList(homeId);
            if (list != null && list.size() > 0) {
                newList.addAll(list);
            }
            //有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地newList列表为主,搜索多少个设备就显示多少个
            if (cloudInverterDeviceList != null && cloudInverterDeviceList.size() > 0) {
                List<GatewayBean> checkNewList = new ArrayList<>();
                for (int i = 0; i < cloudInverterDeviceList.size(); i++) {
                    CloudInverterDeviceBean cloudInverterDevice = cloudInverterDeviceList.get(i);
                    for (GatewayBean gatewayBean : newList) {
                        if (cloudInverterDevice.getOsn().equals(gatewayBean.getDevice_mac())) {
                            checkNewList.add(gatewayBean);
                            break;
                        }
                    }
                }
                newList.clear();
                newList.addAll(checkNewList);
            }
//            //模拟设备列表数据
//            List<GatewayBean> mockData = getGatewayDeviceListMockData(3);
//            if (mockData.size() > 0) {
//                for (GatewayBean mockDatum : mockData) {
//                    newList.add(0, mockDatum);
//                }
//            }
            //胡彬要求的 2025年05月19日13:28:47 即使逆变器删除了云端上成功,本地逆变器初始化不成功(断开)依然不显示;
            if (!isIntegrateLocalData) {
                //强制以云端为主,云端有就显示逆变器列表,没有就不显示
                if (cloudInverterDeviceList == null || cloudInverterDeviceList.size() == 0) {
                    newList.clear();
                }
            }
            return newList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
    /**
@@ -157,11 +236,12 @@
            list.remove(index);
        }
    }
    /**
     * 移除缓存列表里面逆变器
     *
     * @param homeId 电站id
     * @param deviceId    逆变器deviceId
     * @param homeId   电站id
     * @param deviceId 逆变器deviceId
     */
    public void removeLocalCacheMemoryGatewayToDeviceId(String homeId, String deviceId) {
        if (TextUtils.isEmpty(deviceId)) {
@@ -286,7 +366,8 @@
     * @param whetherToAdd true表示用增量,false表示用全量
     */
    public void uploadOidDataToCloud(String homeId, List<GatewayBean> list, boolean whetherToAdd, CloudCallBeak<Boolean> callBeak) {
        if (list == null || list.size() == 0) {
        if (list == null || list.isEmpty()) {
            return;
        }
        if (whetherToAdd) {
@@ -319,6 +400,7 @@
                    @Override
                    public void onError(HDLLinkException e) {
                        HdlLogLogic.print("获取oid列表失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                    }
                });
                SystemClock.sleep(200);
@@ -326,10 +408,13 @@
        } else {
            //只有进入详情界面
            List<OidBean> newOidList = new ArrayList<>();
            //表示中间任何一个获取oid失败。都要改变用增量上传oid列表
            AtomicBoolean isIncrementalOidAddition = new AtomicBoolean(false);
            AtomicInteger atomicInteger = new AtomicInteger(0);
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (gatewayBean == null || TextUtils.isEmpty(gatewayBean.getDevice_mac())) {
//                    isIncrementalOidAddition.set(true);
                    continue;
                }
                getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack<List<OidBean>>() {
@@ -337,46 +422,50 @@
                    public void onSuccess(List<OidBean> oidBeanList) {
                        atomicInteger.set(atomicInteger.get() + 1);
                        if (oidBeanList == null) {
//                            isIncrementalOidAddition.set(true);
                            return;
                        }
                        newOidList.addAll(oidBeanList);
                        if (atomicInteger.get() == list.size()) {
                            if (newOidList.size() == 0) {
                            if (newOidList.isEmpty()) {
                                return;
                            }
//                        //增量添加oid
//                        updateOidAdd(homeId, oidList, new CloudCallBeak<Boolean>() {
//                            @Override
//                            public void onSuccess(Boolean obj) {
//                                HdlLogLogic.print("上传oid列表到云端成功---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac(), true);
//                            }
//
//                            @Override
//                            public void onFailure(HDLException e) {
//                                HdlLogLogic.print("上传oid列表到云端失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
//                            }
//                        });
                            if (isIncrementalOidAddition.get()) {
                                //增量添加oid
                                updateOidAdd(homeId, newOidList, new CloudCallBeak<Boolean>() {
                                    @Override
                                    public void onSuccess(Boolean obj) {
                                        HdlLogLogic.print("上传oid列表到云端成功(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---\r\n数据---" + new Gson().toJson(oidBeanList), true);
                                    }
                            //全部获取后再上传
                            fullUpdateOid(homeId, newOidList, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                    HdlLogLogic.print("上传oid列表到云端成功(全量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---\r\n数据---" + new Gson().toJson(newOidList), true);
                                }
                                    @Override
                                    public void onFailure(HDLException e) {
                                        HdlLogLogic.print("上传oid列表到云端失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                    }
                                });
                            } else {
                                //全部获取后再上传
                                fullUpdateOid(homeId, newOidList, new CloudCallBeak<Boolean>() {
                                    @Override
                                    public void onSuccess(Boolean obj) {
                                        HdlLogLogic.print("上传oid列表到云端成功(全量)---住宅id:" + homeId + "---\r\n数据---" + new Gson().toJson(newOidList), true);
                                    }
                                @Override
                                public void onFailure(HDLException e) {
                                    HdlLogLogic.print("上传oid列表到云端失败(全量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                }
                            });
                                    @Override
                                    public void onFailure(HDLException e) {
                                        HdlLogLogic.print("上传oid列表到云端失败(全量)---住宅id:" + homeId + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                    }
                                });
                            }
                        }
                    }
                    @Override
                    public void onError(HDLLinkException e) {
                        isIncrementalOidAddition.set(true);
                        atomicInteger.set(atomicInteger.get() + 1);
                        if (atomicInteger.get() == list.size()) {
                            if (newOidList.size() == 0) {
                            if (newOidList.isEmpty()) {
                                HdlLogLogic.print("获取逆变器oid列表失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                return;
                            }
@@ -427,8 +516,16 @@
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId);
                    List<GatewayBean> list = new ArrayList<>();
                    GatewayBean gatewayBean = new GatewayBean();
                    gatewayBean.setHomeId(homeId);
                    gatewayBean.setDevice_mac(mac);
                    gatewayBean.setSpk(spk);
                    gatewayBean.setSid(sid);
                    gatewayBean.setOid(oid);
                    gatewayBean.setDevice_name(name);
                    list.add(gatewayBean);
                    //添加设备成功后,上传oid列表到云端
                    uploadOidDataToCloud(homeId, list, true, null);
                }
            }
@@ -493,22 +590,7 @@
            @Override
            public void onFailure(HDLException e) {
                //添加逆变器失败后初始化逆变器
                HdlLogLogic.print("添加逆变器失败后开始初始化逆变器-->", true);
                for (int i = 0; i < list.size(); i++) {
                    GatewayBean gatewayBean = list.get(i);
                    HdlDeviceLogic.getInstance().initializeInverter(gatewayBean.getDevice_mac(), new LinkCallBack<Boolean>() {
                        @Override
                        public void onSuccess(Boolean obj) {
                            HdlLogLogic.print("初始化逆变器成功-->mac:" + gatewayBean.getDevice_mac(), true);
                        }
                        @Override
                        public void onError(HDLLinkException e) {
                            HdlLogLogic.print("初始化逆变器失败-->mac:" + gatewayBean.getDevice_mac(), true);
                        }
                    });
                }
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
@@ -634,7 +716,11 @@
     * @param cloudCallBeak 回调update
     */
    public void fullUpdateOid(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (oidList == null || oidList.size() == 0) {
        if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) {
            HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true);
            return;
        }
        if (oidList == null || oidList.isEmpty()) {
            HdlLogLogic.print("oid列表为空,无法全量更新oid到云端---", true);
            return;
        }
@@ -689,7 +775,11 @@
     * @param cloudCallBeak 回调update
     */
    public void updateOidAdd(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (oidList == null || oidList.size() == 0) {
        if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) {
            HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true);
            return;
        }
        if (oidList == null || oidList.isEmpty()) {
            HdlLogLogic.print("oid列表为空,无法增量添加oid到云端---", true);
            return;
        }
@@ -699,6 +789,10 @@
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < oidList.size(); i++) {
            OidBean oidBean = oidList.get(i);
            if (TextUtils.isEmpty(oidBean.getDevice_model())) {
                //为空过滤掉;
                continue;
            }
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("protocolType", oidBean.getProtocolType());
            jsonObject.addProperty("deviceType", oidBean.getDeviceType());
@@ -721,7 +815,7 @@
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
                HdlLogLogic.print("增量添加oid到云端成功---");
                HdlLogLogic.print("增量添加oid到云端成功---", true);
            }
            @Override
@@ -921,22 +1015,31 @@
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayInfo(String mac, LinkCallBack<GatewayBean> linkCallBack) {
    public void getGatewayInfo(String mac, LinkCallBack<InverterBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_INFO;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
        JsonObject json = new JsonObject();
        json.addProperty("device_mac", mac);
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(null);
                    }
                if (linkCallBack == null) {
                    return;
                }
                Gson gson = new Gson();
                GatewayBean gatewayBean = gson.fromJson(json, GatewayBean.class);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(gatewayBean);
                if (TextUtils.isEmpty(json)) {
                    linkCallBack.onSuccess(null);
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<BaseLocalResponse<InverterBean>>() {
                }.getType();
                BaseLocalResponse<InverterBean> baseLocalResponse = gson.fromJson(json, typeOfT);
                if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) {
                    linkCallBack.onSuccess(new InverterBean());
                } else {
                    linkCallBack.onSuccess(baseLocalResponse.getObjects());
                }
            }
            @Override
@@ -1054,51 +1157,127 @@
    }
    /**
     * 获取逆变器列表(整合云端和本地列表)
     * 注意:有外网,以云端设备为准,本地存在,云端没有则删除;内网,以本地为主,搜索多少个设备就显示多少个;
     * 获取逆变器列表,整合云端逆变器列表和本地逆变器列表(设备类型有:并网逆变器,离线逆变器,负载中心设备);
     * 注意:有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地为主,搜索多少个设备就显示多少个
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 返回逆变器列表
     */
    public void getCurrentHomeLocalAndCloudGatewayList(String homeId, CloudCallBeak<List<GatewayBean>> cloudCallBeak) {
        HdlDeviceLogic.getInstance().searchCurrentHomeGateway(homeId, new GatewayCallBack() {
        searchCurrentHomeGateway(homeId, new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                //局域网有2种情况(1:有局域网,有外网;2:有局域网,没有外网);
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                //向云端获取逆变器列表
                getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        ///情况1:有局域网,有外网;
                        setDeviceRemoteInfo(list, homeId, cloudCallBeak);
                    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) {
                        ///情况2:有局域网,没有外网;
                        //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表;
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                        }
                        //向云端获取负载中心列表
                        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) {
                //外网只有1种情况(本地搜索逆变器列表失败了)
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
//                //外网进来先【默认】清空本地逆变器列表
//                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                //向云端获取逆变器列表
                getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        //外网进来先【默认】清空本地逆变器列表
                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                        setDeviceRemoteInfo(list, homeId, cloudCallBeak);
                    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) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(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<>());
                                }
                            }
                        });
                    }
                });
            }
@@ -1116,9 +1295,8 @@
        if (list == null || list.size() == 0) {
            //云端没有绑定逆变器,默认返回本地逆变器列表;
            if (cloudCallBeak != null) {
                cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list,false));
            }
            return;
        }
        AtomicInteger atomicInteger = new AtomicInteger(0);
@@ -1131,10 +1309,10 @@
                    atomicInteger.set(atomicInteger.get() + 1);
                    //更新逆变器本地缓存信息
                    refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo, homeId);
                    //到最后一条,才有回调
                    //到最后一条,才处理回调
                    if (atomicInteger.get() == list.size()) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                            cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list,false));
                        }
                    }
                }
@@ -1142,13 +1320,16 @@
                @Override
                public void onFailure(HDLException e) {
                    atomicInteger.set(atomicInteger.get() + 1);
                    //当最后一条请求失败了,才有回调
                    //注意:这个逆变器只能本地控制(因为拿不到mqtt通讯秘钥以及GatewayID,无法远程控制)
                    //更新逆变器本地缓存信息
                    refreshGatewayCacheData(false, cloudInverterDeviceBean, null, homeId);
                    //到最后一条,才处理回调
                    if (atomicInteger.get() == list.size()) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                            cloudCallBeak.onSuccess(IntegrateCloudAndLocalData(homeId, list,false));
                        }
                    }
                    HdlLogLogic.print("获取设备远程通讯信息失败---msg---" + e.getMsg() + "(" + e.getCode() + ")");
                    HdlLogLogic.print("获取设备远程通讯信息失败(mac:" + cloudInverterDeviceBean.getOsn() + ")------" + e.getMsg() + "(" + e.getCode() + ")", true);
                }
            });
        }
@@ -1156,19 +1337,21 @@
    }
    /**
     * 通过sid查询逆变器
     * 通过mac查询逆变器是否存绑定在云端
     *
     * @param list 云端上逆变器列表
     * @return 存在为true, 否则为false
     * @return true不存在,否则存在
     */
    private boolean querySidInverter(List<CloudInverterDeviceBean> list, String sid) {
    private boolean queryMacInverter(List<CloudInverterDeviceBean> list, String mac) {
        int exists_value = -1;
        for (int i = 0; i < list.size(); i++) {
            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
            if (sid.equals(cloudInverterDeviceBean.getSid())) {
                return true;
            if (mac.equals(cloudInverterDeviceBean.getOsn())) {
                exists_value = 1;
                break;
            }
        }
        return false;
        return exists_value == -1;
    }
    /**
@@ -1213,13 +1396,23 @@
    /**
     * 删除本地缓存逆变器
     *
     * @param sid 设备sid
     * @param mac 设备mac
     */
    private void removeLocalInverter(String sid) {
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
    private void removeLocalInverter(String mac) {
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
        if (gatewayBean != null) {
            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
        }
    }
    /**
     * 判断spk是否是负载中心
     *
     * @param spk spk
     * @return 真=true,否则返回false
     */
    private boolean isLoadCenterSpk(String spk) {
        return Objects.equals(spk, LC_SPK);
    }
    /**
@@ -1375,6 +1568,189 @@
    /**
     * 获取未运行逆变器列表
     *
     * @param homeId 电站id
     */
    public void getListNotRun(String homeId, CloudCallBeak<List<NotRunBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_listNotRun;
        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(new ArrayList<>());
                    }
                }
                Type type = new TypeToken<List<NotRunBean>>() {
                }.getType();
                List<NotRunBean> list = new Gson().fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 逆变器开机
     *
     * @param homeId    电站id
     * @param arrayList 设备id数组
     */
    public void setDeviceStartup(String homeId, JsonArray arrayList, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_startup;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.add("deviceIds", arrayList);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取设备详情
     *
     * @param homeId    电站id
     * @param arrayList 设备id数组
     */
    public void getDeviceInfo(String homeId, JsonArray arrayList, CloudCallBeak<List<DeviceInfoBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_info;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.add("deviceIds", arrayList);
        // 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(new ArrayList<>());
                    }
                    return;
                }
                Gson gson = new Gson();
                List<DeviceInfoBean> list = gson.fromJson(jsonStr, new TypeToken<List<DeviceInfoBean>>() {
                }.getType());
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取电站下负载控制中心列表
     *
     * @param homeId 电站id
     */
    public void getLoadCentreDeviceList(String homeId, CloudCallBeak<List<CloudInverterDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_loadCentreDevices;
        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(new ArrayList<>());
                    }
                }
                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);
                }
            }
        });
    }
    /**
     * 逆变器密码登录
     *
     * @param mac      逆变器mac
     * @param password 逆变器密码
     */
    public void getPasswordVerification(String mac, String password, LinkCallBack<LocalResponse> callBeak) {
        String requestUrl = TopicApi.DELETING_GATEWAY_password_verifiy;
        JsonObject json = new JsonObject();
        json.addProperty("password", password);
        json.addProperty("mac", mac);
        TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    Gson gson = new Gson();
                    LocalResponse localResponse = gson.fromJson(msg, LocalResponse.class);
                    if (localResponse == null) {
                        localResponse = new LocalResponse();
                    }
                    if (callBeak != null) {
                        callBeak.onSuccess(localResponse);
                    }
                } catch (Exception e) {
                    if (callBeak != null) {
                        callBeak.onSuccess(new LocalResponse());
                    }
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (callBeak != null) {
                    callBeak.onError(e);
                }
            }
        });
    }
    /**
     * 刷新逆变器缓存信息(包括设置mqtt秘钥)
     * 注: //目的是为了获取拿到网关ID,mqtt通讯秘钥等信息,缓存本地逆变器列表里面,发送数据数据时自动去缓存列表里面去查找;
     * 1:本地存在,更新信息即可;
@@ -1400,8 +1776,9 @@
            HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
        }
        gatewayBean.setHomeId(homeId);//住宅id
        gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态
        gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态(1 : 连接中,2 : 故障, 3 : 运行,4 : 离线)
        gatewayBean.setDeviceId(cloudInverterDeviceBean.getDeviceId());//设置link设备id
        gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());//设置mac
        gatewayBean.setOid(cloudInverterDeviceBean.getOid());//设备oid
@@ -1409,13 +1786,18 @@
        gatewayBean.setDevice_name(cloudInverterDeviceBean.getName());//设备名称
        gatewayBean.setGatewayType(cloudInverterDeviceBean.getSpk());//设置spk
        gatewayBean.setLocalEncrypt(true);
        //离网逆变器
        gatewayBean.setOgMaster(cloudInverterDeviceBean.isOgMaster());
        //逆变器
        gatewayBean.setMaster(GatewayMasterType.MasterTrue);//默认都是主逆变器(以后支持从逆变器要更改)
        gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态
        gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态(逆变器设备)
        gatewayBean.setWorkStatusDesc(cloudInverterDeviceBean.getWorkStatusDesc());//工作状态(负载中心设备)
        gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号
        gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//设备类型(产品二级分类名称
        gatewayBean.setDeviceType(cloudInverterDeviceBean.getDeviceType());//设备类型(INV:逆变器;BMS:BMS控制盒;BATTERY:电池单元)
        gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号,逆变器地址
        gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率
        gatewayBean.setOutputActivePower(cloudInverterDeviceBean.getOutputActivePower());//逆变器输出有功功率
        gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量
        if (!TextUtils.isEmpty(cloudInverterDeviceBean.getOmodel())) {
            //可能设备没有上报给云端,建议绑定逆变器时候追加一个参数;
@@ -1434,7 +1816,9 @@
     */
    public List<String> getGatewaySpk() {
        List<String> spks = new ArrayList<>();
        spks.add(DEVICE_SPK);
        spks.add(INVERTER_DEVICE_SPK);
        spks.add(LC_SPK);
        spks.add(OFF_INVERTER_OG_SPK);
        return spks;
    }
@@ -1450,5 +1834,48 @@
        return false;
    }
    public final String DEVICE_SPK = "energy.hdl_inverter";
    /**
     * 模拟逆变器设备列表
     *
     * @param sum 模拟多少个
     * @return 返回逆变器列表
     */
    public List<GatewayBean> getGatewayDeviceListMockData(int sum) {
        try {
            List<GatewayBean> list = new ArrayList<>();
            for (int i = 0; i < sum; i++) {
                String spk = getGatewaySpk().get(i % getGatewaySpk().size());
                GatewayBean gatewayBean = new GatewayBean();
                gatewayBean.setHomeId("100000000" + i);
                gatewayBean.setDevice_mac("200000000" + i);
                gatewayBean.setSpk(spk);
                gatewayBean.setGateway_type(spk);
                gatewayBean.setMaster("true");
                gatewayBean.setSid("300000000" + i);
                gatewayBean.setOid("400000000" + i);
                gatewayBean.setGatewayId("500000000" + i);
                gatewayBean.setDevice_name("模拟-" + i + "(" + spk + ")");
                gatewayBean.setDeviceStatus(5);//默认都是离线¬
                list.add(gatewayBean);
            }
            return list;
        } catch (Exception ignored) {
            return new ArrayList<>();
        }
    }
    /**
     * 逆变器spk
     */
    public final String INVERTER_DEVICE_SPK = "energy.hdl_inverter";
    /**
     * 负载中心spk
     */
    public final String LC_SPK = "energy.hdl_lc";
    /**
     * 离线逆变器spk
     */
    public final String OFF_INVERTER_OG_SPK = "energy.inverter_og";
}