mac
2024-05-06 a4e8dee87338beef6f45f053d8fa9c36dc74ed09
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -9,6 +9,7 @@
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.PageNumberObject;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
@@ -19,8 +20,11 @@
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.DeviceBean;
import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.HouseInfoBean;
import com.hdl.photovoltaic.ui.bean.MessageBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.config.TopicConstant;
@@ -67,6 +71,27 @@
        return sHdlDeviceLogic;
    }
    public static final String kWp = "kWp";
    public static final String kWh = "kW.h";
    public static final String kW = "kW";
    private List<DeviceBean> mListDevice = new ArrayList<>();
    public void clearDeviceList() {
        if (mListDevice == null) {
            return;
        }
        if (mListDevice.size() > 0) {
            mListDevice.clear();
        }
    }
    public List<DeviceBean> getDeviceList() {
        if (mListDevice == null) {
            return new ArrayList<>();
        }
        return mListDevice;
    }
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
@@ -89,36 +114,221 @@
        return newList;
    }
    /**
     * 添加逆变器到缓存列表里面去
     *
     * @param homeId         电站id
     * @param newGatewayBean 逆变器对象
     */
    public void addGatewayToLocalCacheMemory(String homeId, GatewayBean newGatewayBean) {
        if (newGatewayBean == null || TextUtils.isEmpty(homeId)) {
            return;
        }
        List<GatewayBean> list = getCurrentHomeGatewayList(homeId);
        if (list == null || list.size() == 0) {
            return;
        }
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean = list.get(i);
            if (gatewayBean.getDevice_mac().equals(newGatewayBean.getDevice_mac())) {
                index = i;
                break;
            }
        }
        if (index > 0) {
            list.remove(index);
            list.add(index, newGatewayBean);
        }
    }
    /**
     * 移除缓存列表里面逆变器
     *
     * @param homeId 电站id
     * @param mac    逆变器mac
     */
    public void removeLocalCacheMemoryGateway(String homeId, String mac) {
        if (TextUtils.isEmpty(mac)) {
            return;
        }
        List<GatewayBean> list = getCurrentHomeGatewayList(homeId);
        if (list == null || list.size() == 0) {
            return;
        }
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean = list.get(i);
            if (gatewayBean.getDevice_mac().equals(mac)) {
                index = i;
                break;
            }
        }
        if (index > 0) {
            list.remove(index);
        }
    }
    /**
     * 获取设备列表(安装商)
     *
     * @param searchTxt 搜索内容
     * @param pageNo    页码
     * @param pageSize  页数
     */
    public void getPowerStationDeviceList(String searchTxt, long pageNo, long pageSize, CloudCallBeak<PageNumberObject<DeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_deviceList;
        JsonObject json = new JsonObject();
        if (!TextUtils.isEmpty(searchTxt)) {
            json.addProperty("searchTxt", searchTxt);
        }
        json.addProperty("pageNo", pageNo);//页码
        json.addProperty("pageSize", pageSize);//页数
//        json.addProperty("zoneType", zoneType);//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(new PageNumberObject<>());
                    }
                }
                Gson gson = new Gson();
                Type type = new TypeToken<PageNumberObject<DeviceBean>>() {
                }.getType();
                PageNumberObject<DeviceBean> pageNumberObject = gson.fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(pageNumberObject);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 添加【设备列表】到本地缓存
     *
     * @param list -设备列表
     */
    public void setListDevice(List<DeviceBean> list) {
        try {
            if (list == null || list.size() == 0) {
                return;
            }
            if (this.mListDevice.size() == 0) {
                this.mListDevice.addAll(list);
                return;
            }
            for (int i = 0; i < list.size(); i++) {
                this.setSingleDevice(list.get(i));
            }
        } catch (Exception ignored) {
        }
    }
    /**
     * 添加设备到列表里面
     *
     * @param deviceBean -设备对象
     */
    public void setSingleDevice(DeviceBean deviceBean) {
        try {
            if (deviceBean == null) {
                return;
            }
            boolean if_boolean = false;
            for (int i = 0; i < mListDevice.size(); i++) {
                if (mListDevice.get(i).getOsn().equals(deviceBean.getOsn())) {
                    //存在替换
                    mListDevice.remove(i);
                    mListDevice.add(i, deviceBean);
                    if_boolean = true;
                    break;
                }
            }
            if (!if_boolean) {
                //没有添加
                this.mListDevice.add(deviceBean);
            }
        } catch (Exception e) {
            String mes = e.getMessage();
            HdlLogLogic.print("--->" + mes);
        }
    }
    /**
     * 逆变器上传数据到云端(包括:sid,oid)
     *
     * @param homeId 住宅id
     * @param list   逆变器列表
     * @param homeId       住宅id
     * @param list         逆变器列表
     * @param whetherToAdd true表示用增量,false表示用全量
     */
    public void uploadDataToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> callBeak) {
    public void uploadOidDataToCloud(String homeId, List<GatewayBean> list, boolean whetherToAdd, 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++) {
            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) {
                    atomicInteger.set(atomicInteger.get() + 1);
                    if (oidBeanList == null) {
                        return;
                    }
                    oidList.addAll(oidBeanList);
                    if (atomicInteger.get() == list.size()) {
                        if (oidList.size() == 0) {
        if (whetherToAdd) {
            for (int i = 0; i < list.size(); i++) {
                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) {
                            HdlLogLogic.print("上传oid列表到云端成功--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "--->数据是null", true);
                            return;
                        }
                        //增量添加oid
                        updateOidAdd(homeId, oidBeanList, new CloudCallBeak<Boolean>() {
                            @Override
                            public void onSuccess(Boolean obj) {
                                HdlLogLogic.print("上传oid列表到云端成功(增量)--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "--->\r\n数据--->" + new Gson().toJson(oidBeanList), true);
                            }
                            @Override
                            public void onFailure(HDLException e) {
                                HdlLogLogic.print("上传oid列表到云端失败(增量)--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                            }
                        });
                    }
                    @Override
                    public void onError(HDLLinkException e) {
                    }
                });
                SystemClock.sleep(200);
            }
        } else {
            //只有进入详情界面
            List<OidBean> newOidList = new ArrayList<>();
            AtomicInteger atomicInteger = new AtomicInteger(0);
            for (int i = 0; i < list.size(); i++) {
                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) {
                        atomicInteger.set(atomicInteger.get() + 1);
                        if (oidBeanList == null) {
                            return;
                        }
                        newOidList.addAll(oidBeanList);
                        if (atomicInteger.get() == list.size()) {
                            if (newOidList.size() == 0) {
                                return;
                            }
//                        //增量添加oid
//                        updateOidAdd(homeId, oidList, new CloudCallBeak<Boolean>() {
//                            @Override
@@ -132,46 +342,47 @@
//                            }
//                        });
                        //全部获取后再上传
                        fullUpdateOid(homeId, oidList, new CloudCallBeak<Boolean>() {
                            @Override
                            public void onSuccess(Boolean obj) {
                                HdlLogLogic.print("上传oid列表到云端成功--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac(), true);
                            }
                            //全部获取后再上传
                            fullUpdateOid(homeId, newOidList, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                    HdlLogLogic.print("上传oid列表到云端成功(全量)--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "--->\r\n数据--->" + new Gson().toJson(newOidList), true);
                                }
                            @Override
                            public void onFailure(HDLException e) {
                                HdlLogLogic.print("上传oid列表到云端失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                            }
                        });
                    }
                }
                @Override
                public void onError(HDLLinkException e) {
                    atomicInteger.set(atomicInteger.get() + 1);
                    if (oidList.size() == 0) {
                        HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                        return;
                    }
                    //增量添加oid
                    updateOidAdd(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);
                                }
                            });
                        }
                    }
                        @Override
                        public void onFailure(HDLException e) {
                            HdlLogLogic.print("上传oid列表到云端失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                    @Override
                    public void onError(HDLLinkException e) {
                        atomicInteger.set(atomicInteger.get() + 1);
                        if (atomicInteger.get() == list.size()) {
                            if (newOidList.size() == 0) {
                                HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                return;
                            }
                            //增量添加oid
                            updateOidAdd(homeId, newOidList, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                    HdlLogLogic.print("上传oid列表到云端成功(增量)--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "--->\r\n数据--->" + new Gson().toJson(newOidList), true);
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                    HdlLogLogic.print("上传oid列表到云端失败(增量)--->住宅id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                                }
                            });
                        }
                    });
                }
            });
            SystemClock.sleep(200);
                    }
                });
                SystemClock.sleep(200);
            }
        }
    }
    /**
@@ -203,7 +414,7 @@
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId);
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null);
                    uploadOidDataToCloud(UserConfigManage.getInstance().getHomeId(), list, true, null);
                }
            }
@@ -246,8 +457,8 @@
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端(去掉,原因是我上传成功后,网关再次全量上传oid列表,会把之前oid列表覆盖掉)
                    uploadDataToCloud(homeId, list, null);
                    //临时的逻辑,上传oid列表到云端
                    uploadOidDataToCloud(homeId, list, true, null);
                }
            }
@@ -1182,6 +1393,7 @@
        gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率
        gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量
        if (!TextUtils.isEmpty(cloudInverterDeviceBean.getOmodel())) {
            //可能设备没有上报给云端,建议绑定逆变器时候追加一个参数;
            gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号
        }
        gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk