wjc
2023-06-27 da8bb15e0d0293d373071b44b89f2b953ecc0830
app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -1,5 +1,9 @@
package com.hdl.photovoltaic.other;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.photovoltaic.bean.HttpResponsePack;
@@ -11,13 +15,19 @@
import com.hdl.photovoltaic.ui.bean.HouseInfoBean;
import com.hdl.photovoltaic.ui.bean.HouseListBean;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 住宅逻辑界面
 */
public class HdlResidenceLogic {
    public static final int pageSize = 50;
    private static volatile HdlResidenceLogic sHdlResidenceLogic;
@@ -59,24 +69,93 @@
    //住宅详情列表
    private List<HouseInfoBean> houseInfoList = new ArrayList<>();
    public HouseListBean getHouseByHouseId(String homeId) {
        return new HouseListBean();
    /**
     * 获取住宅列表
     *
     * @param cloudCallBeak -
     */
    public void getHouseList(CloudCallBeak<Boolean> cloudCallBeak) {
        //获取住宅列表
        getResidenceList("", "", new CloudCallBeak<List<HouseListBean>>() {
            @Override
            public void onSuccess(List<HouseListBean> houseListBeanList) {
                if (houseListBeanList != null && houseListBeanList.size() > 0) {
                    houseList.clear();
                    houseList.addAll(houseListBeanList);
                    //标记最后一条
                    AtomicInteger atomicInteger = new AtomicInteger(0);
                    for (int i = 0; i < houseList.size(); i++) {
                        HouseListBean houseListBean = houseList.get(i);
                        atomicInteger.set(i);
                        //获取住宅详情列表
                        getResidenceInfo(houseListBean.getHomeId(), new CloudCallBeak<HouseInfoBean>() {
                            @Override
                            public void onSuccess(HouseInfoBean houseInfoBean) {
                                if (houseInfoBean != null) {
                                    houseInfoBean.setHomeImage(houseListBean.getHomeImage());//电站图片
                                    houseInfoBean.setInstalledCapacity(houseListBean.getInstalledCapacity());//装机容量
                                    houseInfoBean.setPower(houseListBean.getPower());//发电功率
                                    houseInfoBean.setTodayElectricity(houseListBean.getTodayElectricity());//今天发电量
                                    houseInfoBean.setPowerStationStatus(houseListBean.getPowerStationStatus());//电站状态(电站状态   1:正常(运行),2:离线,3:连接中,4:故障)
                                    //添加到本地缓存
                                    addHouseToLocalMemory(houseInfoBean);
                                }
                                if (atomicInteger.get() == houseList.size() - 1) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(true);
                                    }
    }
                            }
                            @Override
                            public void onFailure(Exception exception) {
                                if (atomicInteger.get() == houseList.size() - 1) {
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onFailure(exception);
                                    }
                                }
                            }
                        });
                    }
                } else {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(true);
                    }
                }
            }
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
    /**
     * 获取住宅(电站)列表
     *
     * @param pageNo   当前页数
     * @param pageSize 一页大小
     * @param pageNo   当前页数 开始默认1
     * @param pageSize 一页大小 开始默认50
     * @param key      发电功率排序(powerSort);今日发电量排序(todayElectricitySort);创建时间排序(createTimeSort);
     * @param keyValue (descending:降序ascending:升序),
     */
    public void getResidenceList(int pageNo, int pageSize, CloudCallBeak<Boolean> cloudCallBeak) {
    public void getResidenceList(int pageNo, int pageSize, String key, String keyValue, CloudCallBeak<Boolean> cloudCallBeak) {
        if (pageNo == 1) {
            //第一次进来清空列表
            houseList.clear();
        }
        String requestUrl = HttpApi.POST_PowerStation_List;
        JsonObject json = new JsonObject();
        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");//创建时间排序
@@ -91,6 +170,7 @@
                    Gson gson = new Gson();
                    String jsonStr = gson.toJson(httpResponsePack.getData());
                    HouseInfoBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseInfoBeanClass.class);
                    //添加【住宅】到本地缓存
                    houseList.addAll(houseInfoBeanClass.getList());
                    //总共有多少页
                    long totalPage = houseInfoBeanClass.totalPage;
@@ -100,7 +180,14 @@
                        }
                        return;
                    }
                    getResidenceList(pageNo + 1, 100, cloudCallBeak);
                    if (pageNo > totalPage) {
                        //当前请求的页数大于总页数,云端出错了立即返回去;
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(true);
                        }
                        return;
                    }
                    getResidenceList(pageNo + 1, pageSize, key, keyValue, cloudCallBeak);
                }
            }
@@ -115,6 +202,87 @@
        });
    }
    public void getResidenceList(String key, String keyValue, CloudCallBeak<List<HouseListBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_List;
        JsonObject json = new JsonObject();
        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<HouseListBean> list = new ArrayList<>();
        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 jsonStr = gson.toJson(httpResponsePack.getData());
                    HouseInfoBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseInfoBeanClass.class);
                    list.addAll(houseInfoBeanClass.getList());
                    //总共有多少页
                    long totalPage = houseInfoBeanClass.totalPage;
                    if (totalPage < 2) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(list);
                        }
                        return;
                    }
                    //标记最后一条
                    AtomicBoolean isLast = new AtomicBoolean(false);
                    //从第二页获取数据
                    for (int i = 2; i <= totalPage; i++) {
                        if (totalPage == i) {
                            isLast.set(true);
                        }
                        json.addProperty("pageNo", i);//更新页码
                        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
                            @Override
                            public void onSuccess(HttpResponsePack httpResponsePack) {
                                Gson gson = new Gson();
                                String jsonStr = gson.toJson(httpResponsePack.getData());
                                HouseInfoBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseInfoBeanClass.class);
                                list.addAll(houseInfoBeanClass.getList());
                                if (isLast.get()) {
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(list);
                                    }
                                }
                            }
                            @Override
                            public void onFailure(Exception exception) {
                                if (isLast.get()) {
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(list);
                                    }
                                }
                            }
                        });
                    }
                } else {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                }
            }
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
    /**
     * 获取住宅(电站)详情
@@ -134,6 +302,7 @@
                    Gson gson = new Gson();
                    String jsonStr = gson.toJson(httpResponsePack.getData());
                    HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(houseInfoBean);
                    }
@@ -153,7 +322,7 @@
    /**
     * 编辑住宅(电站)
     * 编辑住宅(电站),
     *
     * @param houseInfoBean -
     * @param cloudCallBeak -
@@ -232,12 +401,12 @@
    }
    /**
     * 添加住宅(电站)
     * 添加住宅(电站)到云端
     *
     * @param houseInfoBean -
     * @param cloudCallBeak -
     */
    public void addResidence(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
    public void addHouseToCloud(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_Create;
        JsonObject json = new JsonObject();
        JsonObject location = new JsonObject();
@@ -282,6 +451,87 @@
    }
    /**
     * 切换住宅
     *
     * @param homeId 住宅id
     */
    public Boolean switchHouse(String homeId) {
        UserConfigManage.getInstance().setHomeId(homeId);
        HdlThreadLogic.runThread(new Runnable() {
            @Override
            public void run() {
                //搜索一下住宅设备
            }
        }, null, null);
        //关闭进度条
        return true;
    }
    /**
     * 添加【住宅详情】到本地缓存
     *
     * @param houseInfoBean 住宅详情
     */
    public void addHouseToLocalMemory(HouseInfoBean houseInfoBean) {
        if (houseInfoBean == null) {
            return;
        }
        int index = isExistHouse(houseInfoBean.getHomeId());
        if (index > -1) {
            houseInfoList.remove(index);
            houseInfoList.add(index, houseInfoBean);
        } else {
            houseInfoList.add(houseInfoBean);
        }
    }
    /**
     * 查询本地缓存住宅列表是否存在住宅
     *
     * @param homeId 住宅id
     * @return 列表索引
     */
    public int isExistHouse(String homeId) {
        int index = -1;
        for (int i = 0; i < houseInfoList.size(); i++) {
            if (houseInfoList.get(i).getHomeId().equals(homeId)) {
                index = i;
                break;
            }
        }
        return index;
    }
    /**
     * 获取住宅详情
     *
     * @param homeId 住宅id
     * @return 住宅找不到返回null
     */
    public HouseInfoBean getHouse(String homeId) {
        int position = isExistHouse(homeId);
        if (position > -1) {
            return houseInfoList.get(position);
        }
        return null;
    }
    /**
     * 删除住宅
     *
     * @param homeId 住宅id
     */
    public void delHouse(String homeId) {
        int position = isExistHouse(homeId);
        if (position > -1) {
            houseInfoList.remove(position);
        }
    }
    static class HouseInfoBeanClass {