mac
2024-03-20 5ca2b601848c62607d88fee5afdae6af80f6eea7
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -2,6 +2,7 @@
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;
@@ -21,12 +22,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;
@@ -60,6 +66,7 @@
        return sHdlDeviceLogic;
    }
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
     */
@@ -81,64 +88,55 @@
        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++) {
            atomicInteger.set(atomicInteger.get() + 1);
            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) {
                    if (oidBeanList == null) {
                        return;
                    }
                    oidList.addAll(oidBeanList);
                    if (atomicInteger.get() == list.size()) {
                        //全部获取后再上传
                        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) {
                    HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                }
            });
        }
            @Override
            public void onError(HDLLinkException e) {
                HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
    }
@@ -170,7 +168,52 @@
                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);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 添加多个逆变器到云端上
     *
     * @param list          逆变器列表
     * @param 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);
            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());
            ary.add(inverterInfo);
        }
        json.add("devices", ary);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null);
                }
            }
@@ -442,7 +485,7 @@
     * 网关时间读取
     *
     * @param mac          设备mac
     * @param linkCallBack 回调update
     * @param linkCallBack 回调updatetime
     */
    public void getGatewayTime(String mac, LinkCallBack<DeviceTimeBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_TIME;
@@ -525,7 +568,7 @@
    public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", GatewayMasterType.MasterTrue);
        json.addProperty("master", GatewayMasterType.MasterFalse);
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
@@ -577,6 +620,68 @@
        });
    }
    /**
     * 初始化逆变器
     *
     * @param mac 网关mac
     */
    public void initializeGateway(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.GATEWAY_INITIALIZE_REMOTE;
        JsonObject sendJsonObj = new JsonObject();
        JsonObject jObject = new JsonObject();
        jObject.addProperty("device_mac", mac);
        jObject.addProperty("reset_factory", "true");//为false或无此字段,则为"清除数据"
        sendJsonObj.add("objects", jObject);
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, sendJsonObj, "", 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
     * @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列表
@@ -770,7 +875,7 @@
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                        }
                                    }
                                    HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")");
                                    HdlLogLogic.print("获取设备远程通讯信息失败--->msg--->" + e.getMsg() + "(" + e.getCode() + ")");
                                }
                            });
                        }
@@ -850,6 +955,7 @@
        JsonObject json = new JsonObject();
        json.addProperty("homeId", "");
        json.addProperty("server_addr", "");
        json.addProperty("local_secret", "");
        TcpClient.getInstance().sendDataToLinkGateway(mac, true, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
@@ -892,9 +998,10 @@
     *
     * @param gatewayCallBack -
     */
    public void searchAllGateway(GatewayCallBack gatewayCallBack) {
    public void searchAllNetworkGateway(GatewayCallBack gatewayCallBack) {
        List<String> spks = this.getGatewaySpk();
        //网关搜索
        HDLLinkLocalSdk.getInstance().refreshGateway(gatewayCallBack, this.getGatewaySpk());
        HDLLinkLocalGateway.getInstance().refreshGatewayBySpk(spks, true, gatewayCallBack);
    }
    /**