mac
2023-09-23 b8209d15d915f72d9abe3a68b76e1f4cfd21eac3
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -1,10 +1,13 @@
package com.hdl.photovoltaic.other;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlhttp.HxHttp;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
@@ -14,12 +17,14 @@
import com.hdl.photovoltaic.internet.api.TopicApi;
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
import com.hdl.photovoltaic.listener.CloudCallBeak;
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.InverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
@@ -33,7 +38,7 @@
import io.reactivex.rxjava3.disposables.Disposable;
/**
 * 设备逻辑的界面
 * 设备逻辑
 */
public class HdlDeviceLogic {
    private static volatile HdlDeviceLogic sHdlDeviceLogic;
@@ -56,72 +61,81 @@
    }
    /**
     * 添加逆变器
     * 添加逆变器到云端上
     *
     * @param inverterDeviceBean 逆变器对象
     * @param cloudCallBeak      回调
     * @param mac           -
     * @param spk           -
     * @param sid           -
     * @param oid           -
     * @param name          -
     * @param cloudCallBeak
     */
    public void addInverterDevice(InverterDeviceBean inverterDeviceBean, CloudCallBeak<Boolean> cloudCallBeak) {
    public void addInverterDeviceToCloud(String mac, String spk, String sid, String oid, String name, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Add;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("mac", inverterDeviceBean.getDevice_mac());
        json.addProperty("spk", inverterDeviceBean.getGateway_type());
        json.addProperty("sid", inverterDeviceBean.getSid());
        json.addProperty("oid", inverterDeviceBean.getOid());
        json.addProperty("name", inverterDeviceBean.getDevice_name());
        json.addProperty("mac", mac);
        json.addProperty("spk", spk);
        json.addProperty("sid", sid);
        json.addProperty("oid", oid);
        json.addProperty("name", name);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取逆变器列表
     * 获取云端逆变器列表
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 回调
     */
    public void getInverterDeviceList(String homeId, CloudCallBeak<CloudInverterDeviceBean> cloudCallBeak) {
    public void getCloudInverterDeviceList(String homeId, CloudCallBeak<List<CloudInverterDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (httpResponsePack != null && httpResponsePack.getData() != null) {
                    Gson gson = new Gson();
                    String json = gson.toJson(httpResponsePack.getData());
                    CloudInverterDeviceBean loginUserRegionBean = new Gson().fromJson(json, CloudInverterDeviceBean.class);
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(loginUserRegionBean);
                        cloudCallBeak.onSuccess(null);
                    }
                }
                Type type = new TypeToken<List<CloudInverterDeviceBean>>() {
                }.getType();
                List<CloudInverterDeviceBean> list = new Gson().fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 删除逆变器
@@ -136,18 +150,18 @@
        json.addProperty("deviceId", deviceId);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -166,25 +180,28 @@
        json.addProperty("parentOid", parentOid);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (httpResponsePack != null && httpResponsePack.getData() != null) {
                    Gson gson = new Gson();
                    String json = gson.toJson(httpResponsePack.getData());
                    Type typeOfT = new TypeToken<List<CloudInverterChildDeviceBean>>() {
                    }.getType();
                    List<CloudInverterChildDeviceBean> list = gson.fromJson(json, typeOfT);
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                        cloudCallBeak.onSuccess(null);
                    }
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<List<CloudInverterChildDeviceBean>>() {
                }.getType();
                List<CloudInverterChildDeviceBean> list = gson.fromJson(jsonStr, typeOfT);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -219,9 +236,9 @@
        json.add("devices", jsonArray);
        // json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
@@ -230,9 +247,9 @@
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -243,24 +260,36 @@
    /**
     * 设置网关远程参数
     *
     * @param mac           设备mac
     * @param cloudCallBeak 回调update
     * @param mac          设备mac
     * @param linkCallBack 回调update
     */
    public void steGatewayParam(String mac, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = TopicApi.GATEWAY_SEND_REMOTE_EDIT;
    public void steGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("server_addr", AppConfigManage.getUserRegionUrl());
        json.addProperty("local_secret", UserConfigManage.getInstance().getLocalSecret());
        TcpClient.getInstance().sendDataToLinkGateway(mac, true, requestUrl, json, "", new HDLLinkCallBack() {
        //解密负载数据(因为写密钥给网关一定明文,因为那时网关还没有密钥)
        TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
                if (gatewayBean != null) {
                    if (!TextUtils.isEmpty(UserConfigManage.getInstance().getLocalSecret())) {
                        gatewayBean.setLocalEncrypt(true);
                    }
                }
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
@@ -268,22 +297,26 @@
    /**
     * 编辑网关参数
     *
     * @param mac           设备mac
     * @param cloudCallBeak 回调update
     * @param mac          设备mac
     * @param linkCallBack 回调update
     */
    public void steEditGatewayParam(String mac, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = TopicApi.GATEWAY_SEND_EDIT;
    public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", "true");
        TcpClient.getInstance().sendDataToLinkGateway(mac, true, requestUrl, json, "", new HDLLinkCallBack() {
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
@@ -291,6 +324,186 @@
    /**
     * 获取网关详情信息
     *
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayInfo(String mac, LinkCallBack<GatewayBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_INFO;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(null);
                    }
                    return;
                }
                Gson gson = new Gson();
                GatewayBean gatewayBean = gson.fromJson(json, GatewayBean.class);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(gatewayBean);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
    /**
     * 获取网关oid列表
     *
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayOidList(String mac, LinkCallBack<List<OidBean>> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_OID_LIST;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(null);
                    }
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<List<OidBean>>() {
                }.getType();
                List<OidBean> list = gson.fromJson(json, typeOfT);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(list);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
    /**
     * 获取本地设备列表
     * 注意:有外网以云端设备为准,本地存在,云端没有则删除;内网以本地为主,搜索多少个设备就显示多少个;
     *
     * @param cloudCallBeak -
     */
    public void getLocalGatewayList(CloudCallBeak<Boolean> cloudCallBeak) {
        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        if (list == null || list.size() == 0) {
//                            //云端没有绑定逆变器,以本地为主;
//                            HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                            }
                            return;
                        }
                        //收集删除逆变器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 i = 0; i < removeSidList.size(); i++) {
                            //获取本地的毫米波
                            removeInverter(removeSidList.get(i));
                        }
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(true);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(e);
                        }
                    }
                });
            }
            @Override
            public void onError(HDLLinkException e) {
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        if (list == null || list.size() == 0) {
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                            }
                            return;
                        }
                        //清楚本地网关列表
                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                        for (int i = 0; i < list.size(); i++) {
                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
                            GatewayBean gatewayBean = new GatewayBean();
                            gatewayBean.setOid(cloudInverterDeviceBean.getOid());
                            gatewayBean.setSid(cloudInverterDeviceBean.getSid());
                            gatewayBean.setGatewayId(cloudInverterDeviceBean.getGatewayId());
                            gatewayBean.setOnline(cloudInverterDeviceBean.isOnline());
                            gatewayBean.setDevice_name(cloudInverterDeviceBean.getGatewayName());
                            gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId());
                            gatewayBean.setLocalEncrypt(true);
                            gatewayBean.setMaster("true");
                            //添加网关
                            HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);
                        }
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(true);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(e);
                        }
                    }
                });
            }
        });
    }
    /**
     * 删除本地逆变器
     *
     * @param sid 设备sid
     */
    private void removeInverter(String sid) {
        //获取本地的毫米波
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
        if (gatewayBean != null) {
            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
        }
    }
    /**
     * 搜索网关列表
     *
     * @param gatewayCallBack -