刘卫锦
2023-08-14 bd80ca98ffb4f483ca2eba47051281fa69e790ac
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -1,14 +1,20 @@
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.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.bean.LocalResponse;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.TcpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.internet.api.TopicApi;
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean;
@@ -17,9 +23,11 @@
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;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import java.lang.reflect.Type;
@@ -52,20 +60,25 @@
    }
    /**
     * 添加逆变器
     * 添加逆变器到云端上
     *
     * @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() {
@@ -86,12 +99,12 @@
    }
    /**
     * 获取逆变器列表
     * 获取云端逆变器列表
     *
     * @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);
@@ -100,20 +113,28 @@
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @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);
                try {
                    if (httpResponsePack != null && httpResponsePack.getData() != null) {
                        Gson gson = new Gson();
                        String json = gson.toJson(httpResponsePack.getData());
                        Type type = new TypeToken<List<CloudInverterDeviceBean>>() {
                        }.getType();
                        List<CloudInverterDeviceBean> list = new Gson().fromJson(json, type);
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(list);
                        }
                    }
                } catch (Exception e) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(loginUserRegionBean);
                        cloudCallBeak.onFailure(e);
                    }
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(Exception e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -190,9 +211,9 @@
     * 全量更新OID
     *
     * @param oidList       oid列表
     * @param cloudCallBeak 回调
     * @param cloudCallBeak 回调update
     */
    public void fullRenewalOid(List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
    public void fullUpdateOid(List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("operationSource", "PROGRAM_ENERGY");//
@@ -237,6 +258,117 @@
    /**
     * 设置网关远程参数
     *
     * @param mac           设备mac
     * @param cloudCallBeak 回调update
     */
    public void steGatewayParam(String mac, CloudCallBeak<Boolean> cloudCallBeak) {
        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, 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 (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 编辑网关参数
     *
     * @param mac           设备mac
     * @param cloudCallBeak 回调update
     */
    public void editGatewayParam(String mac, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", "true");
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取网关oid列表
     *
     * @param mac           网关mac
     * @param cloudCallBeak 回调
     */
    public void getGatewayOidList(String mac, CloudCallBeak<List<OidBean>> cloudCallBeak) {
        String requestUrl = TopicApi.GET_GATEWAY_OID_LIST;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                try {
                    if (!TextUtils.isEmpty(json)) {
                        Gson gson = new Gson();
                        LocalResponse localResponse = gson.fromJson(json, LocalResponse.class);
                        String objects = gson.toJson(localResponse.getObjects());
                        Type typeOfT = new TypeToken<List<OidBean>>() {
                        }.getType();
                        List<OidBean> list = gson.fromJson(objects, typeOfT);
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(list);
                        }
                    } else {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(new ArrayList<>());
                        }
                    }
                } catch (Exception e) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onFailure(e);
                    }
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 搜索网关列表
     *
     * @param gatewayCallBack -