mac
2024-04-02 05581cb7328000de65c5e8c1ef12b366b44b11b1
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -1,13 +1,14 @@
package com.hdl.photovoltaic.other;
import android.os.SystemClock;
import android.text.TextUtils;
import com.facebook.imagepipeline.image.OriginalEncodedImageInfo;
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.linkpm.sdk.home.type.GatewayType;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
@@ -22,12 +23,17 @@
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.config.TopicConstant;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.core.bean.LinkRequest;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.core.connect.HDLConnectHelper;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.hdl.sdk.link.gateway.type.GatewayMasterType;
@@ -36,8 +42,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import io.dcloud.common.adapter.util.DeviceInfo;
/**
@@ -63,6 +67,7 @@
        return sHdlDeviceLogic;
    }
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
     */
@@ -84,64 +89,88 @@
        return newList;
    }
    /**
     * 获取当前住宅的【主】逆变器
     */
    public GatewayBean getCurrentHomeMainGateway(String homeId) {
        return queryCurrentHomeMainGateway(this.getCurrentHomeGatewayList(homeId), homeId);
    }
    /**
     * 查询当前住宅的【主】逆变器
     */
    public GatewayBean queryCurrentHomeMainGateway(List<GatewayBean> list, String homeId) {
        if (list == null || list.size() == 0) {
            return null;
        }
        GatewayBean findGatewayBean = null;
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean = list.get(i);
            if (gatewayBean.getMaster().equals(GatewayMasterType.MasterTrue) && gatewayBean.getHomeId().equals(homeId)) {
                //找到返回
                findGatewayBean = gatewayBean;
                break;
            }
        }
        return findGatewayBean;
    }
    /**
     * 逆变器上传数据到云端(包括:sid,oid)
     *
     * @param homeId 住宅id
     * @param mac    设备mac
     * @param list   逆变器列表
     */
    public void uploadDataToCloud(String homeId, String mac, CloudCallBeak<Boolean> callBeak) {
        getInverterOidList(mac, new LinkCallBack<List<OidBean>>() {
            @Override
            public void onSuccess(List<OidBean> oidBeanList) {
                if (oidBeanList == null) {
                    return;
    public void uploadDataToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> callBeak) {
        if (list == null || list.size() == 0) {
            return;
        }
        List<OidBean> oidList = new ArrayList<>();
        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())) {
                continue;
            }
            getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack<List<OidBean>>() {
                @Override
                public void onSuccess(List<OidBean> oidBeanList) {
                    atomicInteger.set(atomicInteger.get() + 1);
                    if (oidBeanList == null) {
                        return;
                    }
                    oidList.addAll(oidBeanList);
                    if (atomicInteger.get() == list.size()) {
                        if (oidList.size() == 0) {
                            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);
//                            }
//                        });
                        //全部获取后再上传
                        fullUpdateOid(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);
                            }
                        });
                    }
                }
                fullUpdateOid(homeId, oidBeanList, new CloudCallBeak<Boolean>() {
                    @Override
                    public void onSuccess(Boolean obj) {
                        HdlLogLogic.print("上传oid列表到云端成功--->住宅id:" + homeId + "--->mac:" + mac, true);
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HdlLogLogic.print("上传oid列表到云端失败--->住宅id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                @Override
                public void onError(HDLLinkException e) {
                    atomicInteger.set(atomicInteger.get() + 1);
                    if (oidList.size() == 0) {
                        HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                        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 onError(HDLLinkException e) {
                HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
                        @Override
                        public void onFailure(HDLException e) {
                            HdlLogLogic.print("上传oid列表到云端失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                        }
                    });
                }
            });
            SystemClock.sleep(200);
        }
    }
@@ -173,7 +202,8 @@
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null);
                    List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId);
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null);
                }
            }
@@ -189,23 +219,23 @@
    /**
     * 添加多个逆变器到云端上
     *
     * @param mac           -
     * @param list          逆变器列表
     * @param cloudCallBeak -
     */
    public void addAllInverterDeviceToCloud(String homeId, String mac, List<GatewayBean> list, CloudCallBeak<Boolean> cloudCallBeak) {
    public void addAllInverterDeviceToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Add_All;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        JsonArray ary = new JsonArray();
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean=list.get(i);
            GatewayBean gatewayBean = list.get(i);
            JsonObject inverterInfo = new JsonObject();
            inverterInfo.addProperty("mac",gatewayBean.getDevice_mac());
            inverterInfo.addProperty("spk",gatewayBean.getGatewayType());
            inverterInfo.addProperty("sid",gatewayBean.getSid());
            inverterInfo.addProperty("oid",gatewayBean.getOid());
            inverterInfo.addProperty("name",gatewayBean.getDevice_name());
            inverterInfo.addProperty("mac", gatewayBean.getDevice_mac());
            inverterInfo.addProperty("spk", gatewayBean.getGatewayType());
            inverterInfo.addProperty("sid", gatewayBean.getSid());
            inverterInfo.addProperty("oid", gatewayBean.getOid());
            inverterInfo.addProperty("name", gatewayBean.getDevice_name());
            ary.add(inverterInfo);
        }
        json.add("devices", ary);
@@ -216,8 +246,8 @@
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null);
                    //临时的逻辑,上传oid列表到云端(去掉,原因是我上传成功后,网关再次全量上传oid列表,会把之前oid列表覆盖掉)
                    uploadDataToCloud(homeId, list, null);
                }
            }
@@ -626,6 +656,68 @@
    }
    /**
     * 初始化逆变器
     *
     * @param mac 网关mac
     */
    public void initializeGateway(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.GATEWAY_INITIALIZE_REMOTE;
        JsonObject jObject = new JsonObject();
        jObject.addProperty("device_mac", mac);
        jObject.addProperty("reset_factory", "true");//为false或无此字段,则为"清除数据"
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jObject, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
                HdlLogLogic.print("初始化逆变器成功-->mac:" + mac, true);
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
                HdlLogLogic.print("初始化逆变器失败-->mac:" + mac + "--->" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
    }
    /**
     * 删除oid
     *
     * @param mac 网关mac
     * @param oid 设备oid
     */
    public void deleteOid(String mac, String oid, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.DELETING_GATEWAY_SLAVE;
        JsonArray jsonArray = new JsonArray();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("oid", oid);//设备oid
        jsonArray.add(jsonObject);
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jsonArray, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
    /**
     * 获取逆变器oid列表
     *
     * @param mac          网关mac
@@ -705,15 +797,11 @@
                                    if (atomicInteger.get() == list.size()) {
                                        //收集删除逆变器sid
                                        List<String> removeSidList = new ArrayList<>();
                                        for (int i = 0; i < list.size(); i++) {
                                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
                                            for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                                                GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                                                if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) {
                                                    //本地有,云端没有,删除本地;
                                                    removeSidList.add(gatewayBean.getSid());
                                                }
                                        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());
                                            }
                                        }
                                        for (int i = 0; i < removeSidList.size(); i++) {
@@ -733,15 +821,11 @@
                                    if (atomicInteger.get() == list.size()) {
                                        //收集删除逆变器sid
                                        List<String> removeSidList = new ArrayList<>();
                                        for (int i = 0; i < list.size(); i++) {
                                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
                                            for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) {
                                                GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j);
                                                if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) {
                                                    //本地有,云端没有,删除本地;
                                                    removeSidList.add(gatewayBean.getSid());
                                                }
                                        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());
                                            }
                                        }
                                        for (int i = 0; i < removeSidList.size(); i++) {
@@ -834,6 +918,22 @@
                });
            }
        });
    }
    /**
     * 通过sid查询逆变器
     *
     * @param list 云端上逆变器列表
     * @return 存在为true, 否则为false
     */
    private boolean querySidInverter(List<CloudInverterDeviceBean> list, String sid) {
        for (int i = 0; i < list.size(); i++) {
            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
            if (sid.equals(cloudInverterDeviceBean.getSid())) {
                return true;
            }
        }
        return false;
    }
    /**
@@ -941,8 +1041,9 @@
     * @param gatewayCallBack -
     */
    public void searchAllNetworkGateway(GatewayCallBack gatewayCallBack) {
        List<String> spks = this.getGatewaySpk();
        //网关搜索
        HDLLinkLocalSdk.getInstance().refreshGateway(gatewayCallBack, this.getGatewaySpk());
        HDLLinkLocalGateway.getInstance().refreshGatewayBySpk(spks, true, gatewayCallBack);
    }
    /**
@@ -1080,7 +1181,9 @@
        gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号,逆变器地址
        gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率
        gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量
        gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号
        if (!TextUtils.isEmpty(cloudInverterDeviceBean.getOmodel())) {
            gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号
        }
        gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk