wjc
2025-04-11 1a08f52ec36e3b71204a23a8813fe50f1748b277
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -3,14 +3,13 @@
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
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;
@@ -22,6 +21,7 @@
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;
@@ -34,12 +34,12 @@
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.hdl.sdk.link.gateway.type.GatewayMasterType;
import com.huawei.hms.support.api.entity.core.JosBaseReq;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
@@ -292,7 +292,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) {
@@ -347,7 +348,7 @@
                        }
                        newOidList.addAll(oidBeanList);
                        if (atomicInteger.get() == list.size()) {
                            if (newOidList.size() == 0) {
                            if (newOidList.isEmpty()) {
                                return;
                            }
//                        //增量添加oid
@@ -382,7 +383,7 @@
                    public void onError(HDLLinkException e) {
                        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;
                            }
@@ -625,6 +626,10 @@
     * @param cloudCallBeak 回调update
     */
    public void fullUpdateOid(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) {
            HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true);
            return;
        }
        if (oidList == null || oidList.size() == 0) {
            HdlLogLogic.print("oid列表为空,无法全量更新oid到云端---", true);
            return;
@@ -680,6 +685,10 @@
     * @param cloudCallBeak 回调update
     */
    public void updateOidAdd(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) {
            HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true);
            return;
        }
        if (oidList == null || oidList.size() == 0) {
            HdlLogLogic.print("oid列表为空,无法增量添加oid到云端---", true);
            return;
@@ -690,6 +699,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());
@@ -912,7 +925,7 @@
     * @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;
        JsonObject json = new JsonObject();
        json.addProperty("device_mac", mac);
@@ -928,11 +941,11 @@
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<BaseLocalResponse<GatewayBean>>() {
                Type typeOfT = new TypeToken<BaseLocalResponse<InverterBean>>() {
                }.getType();
                BaseLocalResponse<GatewayBean> baseLocalResponse = gson.fromJson(json, typeOfT);
                BaseLocalResponse<InverterBean> baseLocalResponse = gson.fromJson(json, typeOfT);
                if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) {
                    linkCallBack.onSuccess(new GatewayBean());
                    linkCallBack.onSuccess(new InverterBean());
                } else {
                    linkCallBack.onSuccess(baseLocalResponse.getObjects());
                }
@@ -1061,36 +1074,107 @@
     * @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) {
                        List<String> removeSidList = new ArrayList<>();
                        for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                            GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                            if (!querySidInverter(list, gatewayBean.getSid())) {
                                //本地有,云端没有,删除本地;
                                removeSidList.add(gatewayBean.getSid());
                    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);
                                }
                                List<String> removeSidList = new ArrayList<>();
                                for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                                    GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                                    if (querySidInverter(inverterDeviceList, gatewayBean.getSid())) {
                                        //本地有,云端没有,删除本地;
                                        removeSidList.add(gatewayBean.getSid());
                                    }
                                }
                                for (int i = 0; i < removeSidList.size(); i++) {
                                    //删除本地的逆变器
                                    removeLocalInverter(removeSidList.get(i));
                                }
                                //情况1:有局域网,有外网;
                                setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak);
                            }
                        }
                        for (int i = 0; i < removeSidList.size(); i++) {
                            //删除本地的逆变器
                            removeLocalInverter(removeSidList.get(i));
                        }
                        //情况1:有局域网,有外网;
                        setDeviceRemoteInfo(list, homeId, cloudCallBeak);
                            @Override
                            public void onFailure(HDLException e) {
                                //向云端获取负载中心设备列表失败后,仍然处理逆变器设备列表
                                List<String> removeSidList = new ArrayList<>();
                                for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                                    GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                                    if (querySidInverter(inverterDeviceList, gatewayBean.getSid())) {
                                        //本地有,云端没有,删除本地;
                                        removeSidList.add(gatewayBean.getSid());
                                    }
                                }
                                for (int i = 0; i < removeSidList.size(); i++) {
                                    //删除本地的逆变器
                                    removeLocalInverter(removeSidList.get(i));
                                }
                                //情况1:有局域网,有外网;
                                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(getCurrentHomeGatewayList(homeId));
                                    }
                                    return;
                                }
                                List<String> removeSidList = new ArrayList<>();
                                for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                                    GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                                    if (!isLoadCenterSpk(gatewayBean.getSpk())) {
                                        //不是负载中心的spk不做处理;
                                        continue;
                                    }
                                    if (querySidInverter(loadCentreDeviceList, gatewayBean.getSid())) {
                                        //本地有,云端没有,删除本地;
                                        removeSidList.add(gatewayBean.getSid());
                                    }
                                }
                                for (int i = 0; i < removeSidList.size(); i++) {
                                    //删除本地的逆变器
                                    removeLocalInverter(removeSidList.get(i));
                                }
                                //情况1:有局域网,有外网;
                                setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                //向云端获取负载中心设备列表失败后,默认返回本地逆变器列表;
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                }
                            }
                        });
//                        if (cloudCallBeak != null) {
//                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
//                        }
                    }
                });
            }
@@ -1098,19 +1182,63 @@
            @Override
            public void onError(HDLLinkException e) {
                //外网只有1种情况(本地搜索逆变器列表失败了)
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                //向云端获取逆变器列表
                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) {
                                //外网进来先【默认】清空本地逆变器列表
                                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                                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) {
                                //外网进来先【默认】清空本地逆变器列表
                                //向云端获取负载中心列表失败,直接返回逆变器列表;
                                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                                setDeviceRemoteInfo(cloudInverterList, homeId, cloudCallBeak);
                            }
                        });
//                        //外网进来先【默认】清空本地逆变器列表
//                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
//                        setDeviceRemoteInfo(list, 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) {
                                //外网进来先【默认】清空本地逆变器列表
                                HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                                //逆变器列表和负载中心列表
                                setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onFailure(e);
                                }
                            }
                        });
//                        if (cloudCallBeak != null) {
//                            cloudCallBeak.onFailure(e);
//                        }
                    }
                });
            }
@@ -1168,19 +1296,21 @@
    }
    /**
     * 通过sid查询逆变器
     * 通过sid查询逆变器是否存绑定在云端
     *
     * @param list 云端上逆变器列表
     * @return 存在为true, 否则为false
     * @return true不存在,否则存在
     */
    private boolean querySidInverter(List<CloudInverterDeviceBean> list, String sid) {
        int exists_value = -1;
        for (int i = 0; i < list.size(); i++) {
            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
            if (sid.equals(cloudInverterDeviceBean.getSid())) {
                return true;
                exists_value = 1;
                break;
            }
        }
        return false;
        return exists_value == -1;
    }
    /**
@@ -1232,6 +1362,16 @@
        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);
    }
    /**
@@ -1428,11 +1568,11 @@
     * @param homeId    电站id
     * @param arrayList 设备id数组
     */
    public void setDeviceStartup(String homeId, String arrayList, CloudCallBeak<Boolean> cloudCallBeak) {
    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.addProperty("deviceIds", arrayList);
        json.add("deviceIds", arrayList);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
@@ -1448,6 +1588,122 @@
            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);
                }
            }
        });
@@ -1489,6 +1745,9 @@
        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.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号
@@ -1515,7 +1774,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;
    }
@@ -1531,5 +1792,16 @@
        return false;
    }
    public final String DEVICE_SPK = "energy.hdl_inverter";
    /**
     * 逆变器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";
}