wjc
2023-07-12 e604e1797744977f599dad9f543db3e7477fe115
app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -1,18 +1,28 @@
package com.hdl.photovoltaic.other;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.HouseInfoBean;
import com.hdl.photovoltaic.ui.bean.HouseIdBean;
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.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@@ -122,12 +132,12 @@
                            }
                            @Override
                            public void onFailure(Exception exception) {
                            public void onFailure(HDLException e) {
                                atomicInteger.set(atomicInteger.get() + 1);
                                if (atomicInteger.get() == houseIdList.size()) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onFailure(exception);
                                        cloudCallBeak.onFailure(e);
                                    }
                                }
                            }
@@ -142,9 +152,9 @@
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -154,8 +164,11 @@
    /**
     * 获取住宅(电站)ID列表
     *
     * @param key      发电功率排序(powerSort);今日发电量排序(todayElectricitySort);创建时间排序(createTimeSort);
     * @param keyValue (descending:降序ascending:升序),
     * @param key      发电功率排序(powerSort);
     *                 今日发电量排序(todayElectricitySort);
     *                 创建时间排序(createTimeSort);
     * @param keyValue (descending:降序
     *                 ascending:升序),
     */
    public void getResidenceIdList(String key, String keyValue, CloudCallBeak<List<HouseIdBean>> cloudCallBeak) {
@@ -164,72 +177,66 @@
        if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(keyValue)) {
            json.addProperty(key, keyValue);//发电功率排序(descending:降序ascending:升序)
        }
//        json.addProperty("powerSort", "descending");//发电功率排序(descending:降序ascending:升序)
//        json.addProperty("todayElectricitySort", "descending");//今日发电量排序
//        json.addProperty("createTimeSort", "descending");//创建时间排序
//        json.addProperty("zoneType", "password");//区域
        json.addProperty("pageNo", 1);//页码
        json.addProperty("pageSize", pageSize);//页数
        List<HouseIdBean> list = new ArrayList<>();
        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 jsonStr = gson.toJson(httpResponsePack.getData());
                    HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
                    list.addAll(houseInfoBeanClass.getList());
                    //总共有多少页
                    long totalPage = houseInfoBeanClass.totalPage;
                    if (totalPage < 2) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(list);
                        }
                        return;
                    }
                    //记录请求条数
                    AtomicInteger atomicInteger = new AtomicInteger(0);
                    //从第二页获取数据
                    for (int i = 2; i <= totalPage; i++) {
                        json.addProperty("pageNo", i);//更新页码
                        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
                            @Override
                            public void onSuccess(HttpResponsePack httpResponsePack) {
                                atomicInteger.set(atomicInteger.get() + 1);
                                Gson gson = new Gson();
                                String jsonStr = gson.toJson(httpResponsePack.getData());
                                HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
                                list.addAll(houseInfoBeanClass.getList());
                                if (atomicInteger.get() == totalPage - 1) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(list);
                                    }
                                }
                            }
                            @Override
                            public void onFailure(Exception exception) {
                                atomicInteger.set(atomicInteger.get() + 1);
                                if (atomicInteger.get() == totalPage - 1) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(list);
                                    }
                                }
                            }
                        });
                    }
                } else {
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                }
                Gson gson = new Gson();
                HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
                list.addAll(houseInfoBeanClass.getList());
                //总共有多少页
                long totalPage = houseInfoBeanClass.totalPage;
                if (totalPage < 2) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                    return;
                }
                //记录请求条数
                AtomicInteger atomicInteger = new AtomicInteger(0);
                //从第二页获取数据
                for (int i = 2; i <= totalPage; i++) {
                    json.addProperty("pageNo", i);//更新页码
                    HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
                        @Override
                        public void onSuccess(String jsonStr) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            Gson gson = new Gson();
                            HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
                            list.addAll(houseInfoBeanClass.getList());
                            if (atomicInteger.get() == totalPage - 1) {
                                //最后一条退出
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(list);
                                }
                            }
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            if (atomicInteger.get() == totalPage - 1) {
                                //最后一条退出
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(list);
                                }
                            }
                        }
                    });
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
@@ -250,24 +257,25 @@
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);//电站id
        //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 jsonStr = gson.toJson(httpResponsePack.getData());
                    HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(houseInfoBean);
                        cloudCallBeak.onSuccess(null);
                    }
                }
                Gson gson = new Gson();
                HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(houseInfoBean);
                }
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -307,9 +315,9 @@
        json.addProperty("electrovalence", houseInfoBean.getElectrovalence());
        json.addProperty("totalCost", houseInfoBean.getTotalCost());
        json.addProperty("zoneType", houseInfoBean.getZoneType());
        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);
                }
@@ -317,9 +325,9 @@
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -336,9 +344,9 @@
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);//电站id
        //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);
                }
@@ -346,9 +354,9 @@
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -386,9 +394,9 @@
        json.addProperty("electrovalence", houseInfoBean.getElectrovalence());
        json.addProperty("totalCost", houseInfoBean.getTotalCost());
        json.addProperty("zoneType", houseInfoBean.getZoneType());
        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);
                }
@@ -396,9 +404,9 @@
            }
            @Override
            public void onFailure(Exception exception) {
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                    cloudCallBeak.onFailure(e);
                }
            }
        });
@@ -406,6 +414,37 @@
    }
    /**
     * 获取住宅图片
     */
    public void getResidenceImage(String imageUrl, CloudCallBeak<Bitmap> cloudCallBeak) {
        String requestUrl = imageUrl;
        JsonObject json = new JsonObject();
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(null);
                    }
                    return;
                }
                Bitmap bitmap = BitmapFactory.decodeByteArray(jsonStr.getBytes(), 0, jsonStr.getBytes().length);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(bitmap);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 添加【住宅详情】到本地缓存
@@ -471,20 +510,43 @@
    /**
     * 切换住宅
     *
     * @param homeId 住宅id
     * @param houseIdBean 住宅Id对象
     */
    public Boolean switchHouse(String homeId) {
    public void switchHouse(HouseIdBean houseIdBean) {
        String oidHomeId = UserConfigManage.getInstance().getHomeId();
        UserConfigManage.getInstance().setHomeId(homeId);
//        if (houseIdBean.getHomeId().equals(oidHomeId)) {
//            return;
//        }
        //删除旧的住宅文件夹
        HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
        //重新设置住宅id
        UserConfigManage.getInstance().setHomeId(houseIdBean.getHomeId());
        //重新设置本地通讯秘钥
        UserConfigManage.getInstance().setLocalSecret(houseIdBean.getLocalSecret());
        //重新创建住宅文件夹
        HdlFileLogic.getInstance().createDirectory();
        HdlThreadLogic.runThread(new Runnable() {
        //配置本地通信的信息
        initLocalLinkSdk();
        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
            @Override
            public void run() {
                //搜索一下住宅设备
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
            }
        }, null, null);
        return true;
            @Override
            public void onError(HDLLinkException e) {
            }
        });
    }
    /**
     * 配置本地通信的信息(tcp通信用到)
     */
    private void initLocalLinkSdk() {
        HDLLinkConfig.getInstance().setHomeId(UserConfigManage.getInstance().getHomeId());
        HDLLinkConfig.getInstance().setLocalSecret(UserConfigManage.getInstance().getLocalSecret());
    }