mac
2023-10-13 c13cb54d0b9f9b2c73f213b6cdb163f462fd64c7
app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -6,47 +6,33 @@
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.api.HDLCloudHomeApi;
import com.hdl.linkpm.sdk.core.callback.IResponseCallBack;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.core.response.HDLResponse;
import com.hdl.linkpm.sdk.device.bean.FunctionInfoBean;
import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils;
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.listener.LinkCallBack;
import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo;
import com.hdl.photovoltaic.ui.bean.InverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
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.bean.request.BaseLocalRequest;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.hdl.sdk.link.gateway.type.GatewayMasterType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import io.reactivex.rxjava3.disposables.Disposable;
/**
@@ -54,27 +40,6 @@
 */
public class HdlDeviceLogic {
    private static volatile HdlDeviceLogic sHdlDeviceLogic;
    /**
     * 获取当前住宅的逆变器列表
     */
    public List<GatewayBean> getGatewayList() {
        List<GatewayBean> newList = new ArrayList<>();
        List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) {
                    continue;
                }
                if (gatewayBean.getMaster().equals("true")
                        && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) {
                    newList.add(gatewayBean);
                }
            }
        }
        return newList;
    }
    /**
     * 获取当前对象
@@ -91,6 +56,59 @@
        }
        return sHdlDeviceLogic;
    }
    /**
     * 获取当前住宅的逆变器列表
     */
    public List<GatewayBean> getCurrentHomeGatewayList() {
        List<GatewayBean> newList = new ArrayList<>();
        List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) {
                    continue;
                }
                if (gatewayBean.getMaster().equals(GatewayMasterType.MasterTrue) && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) {
                    newList.add(gatewayBean);
                }
            }
        }
        return newList;
    }
    /**
     * 逆变器上传数据到云端(包括:sid,oid)
     *
     * @param mac 设备mac
     */
    public void uploadDataToCloud(String mac, CloudCallBeak<Boolean> callBeak) {
        getInverterOidList(mac, new LinkCallBack<List<OidBean>>() {
            @Override
            public void onSuccess(List<OidBean> obj) {
                if (obj == null) {
                    return;
                }
                fullUpdateOid(obj, new CloudCallBeak<Boolean>() {
                    @Override
                    public void onSuccess(Boolean obj) {
                    }
                    @Override
                    public void onFailure(HDLException e) {
                    }
                });
            }
            @Override
            public void onError(HDLLinkException e) {
            }
        });
    }
    /**
@@ -121,30 +139,7 @@
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                    //临时的逻辑,上传oid列表到云端
                    getGatewayOidList(mac, new LinkCallBack<List<OidBean>>() {
                        @Override
                        public void onSuccess(List<OidBean> obj) {
                            if (obj == null) {
                                return;
                            }
                            fullUpdateOid(obj, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                }
                                @Override
                                public void onFailure(HDLException e) {
                                }
                            });
                        }
                        @Override
                        public void onError(HDLLinkException e) {
                        }
                    });
                    uploadDataToCloud(mac, null);
                }
            }
@@ -196,7 +191,7 @@
    /**
     * 删除逆变器
     * 删除云端上逆变器
     *
     * @param deviceId      设备id
     * @param cloudCallBeak 回调
@@ -358,13 +353,25 @@
     * @param mac          设备mac
     * @param linkCallBack 回调update
     */
    public void getGatewayTime(String mac, LinkCallBack<Boolean> linkCallBack) {
    public void getGatewayTime(String mac, LinkCallBack<DeviceTimeBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_TIME;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(new DeviceTimeBean());
                    }
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<BaseLocalResponse<DeviceTimeBean>>() {
                }.getType();
                BaseLocalResponse<DeviceTimeBean> baseLocalResponse = gson.fromJson(json, typeOfT);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                    linkCallBack.onSuccess(baseLocalResponse.getObjects());
                }
            }
@@ -382,17 +389,16 @@
     * 网关时间修改
     *
     * @param mac          设备mac
     * @param date         日期
     * @param time         时间
     * @param linkCallBack 回调update
     */
    public void editGatewayTime(String mac, JsonObject jsonObject, LinkCallBack<Boolean> linkCallBack) {
    public void editGatewayTime(String mac, String date, String time, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_TIME_EDIT;
//        JsonObject json = new JsonObject();
//        json.addProperty("master", "true");
//        "objects": {
//            "date": "2020-08-15",
//                    "time": "17:25:20"
//        }
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jsonObject, "", new HDLLinkCallBack() {
        JsonObject json = new JsonObject();
        json.addProperty("date", date);// "2020-08-15"
        json.addProperty("time", time);//"17:25:20"
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (linkCallBack != null) {
@@ -419,7 +425,7 @@
    public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", "true");
        json.addProperty("master", GatewayMasterType.MasterTrue);
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
@@ -473,12 +479,12 @@
    }
    /**
     * 获取网关oid列表
     * 获取逆变器oid列表
     *
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayOidList(String mac, LinkCallBack<List<OidBean>> linkCallBack) {
    public void getInverterOidList(String mac, LinkCallBack<List<OidBean>> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_OID_LIST;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
@@ -515,15 +521,16 @@
    }
    /**
     * 获取本地逆变器列表
     * 获取逆变器列表
     * 注意:有外网以云端设备为准,本地存在,云端没有则删除;内网以本地为主,搜索多少个设备就显示多少个;
     *
     * @param cloudCallBeak -
     * @param cloudCallBeak 返回逆变器列表
     */
    public void getLocalGatewayList(CloudCallBeak<Boolean> cloudCallBeak) {
    public void getCurrentHomeLocalAndCloudGatewayList(CloudCallBeak<List<GatewayBean>> 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) {
@@ -531,7 +538,7 @@
//                            //云端没有绑定逆变器,以本地为主;
//                            HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                                cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                            }
                            return;
                        }
@@ -560,10 +567,10 @@
                                        }
                                        for (int i = 0; i < removeSidList.size(); i++) {
                                            //删除本地的逆变器
                                            removeInverter(removeSidList.get(i));
                                            removeLocalInverter(removeSidList.get(i));
                                        }
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(true);
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                                        }
                                    }
                                }
@@ -588,10 +595,10 @@
                                        }
                                        for (int i = 0; i < removeSidList.size(); i++) {
                                            //删除本地的逆变器
                                            removeInverter(removeSidList.get(i));
                                            removeLocalInverter(removeSidList.get(i));
                                        }
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(true);
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                                        }
                                    }
                                    HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")");
@@ -613,15 +620,15 @@
            @Override
            public void onError(HDLLinkException e) {
                //外网情况
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        //清楚本地网关列表
                        //云端没有,清空本地逆变器列表
                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                        if (list == null || list.size() == 0) {
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                                cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                            }
                            return;
                        }
@@ -636,7 +643,7 @@
                                    refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo);
                                    if (atomicInteger.get() == list.size()) {
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(true);
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                                        }
                                    }
                                }
@@ -646,7 +653,7 @@
                                    atomicInteger.set(atomicInteger.get() + 1);
                                    if (atomicInteger.get() == list.size()) {
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(true);
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList());
                                        }
                                    }
                                    HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")");
@@ -678,7 +685,7 @@
     * @return -
     */
    public void getDeviceRemoteInfo(String homeId, String spk, String mac, CloudCallBeak<DeviceRemoteInfo> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_remoteInfo;
        String requestUrl = HttpApi.POST_Device_RemoteInfo;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("spk", spk);
@@ -709,11 +716,11 @@
    }
    /**
     * 删除本地逆变器
     * 删除本地缓存逆变器
     *
     * @param sid 设备sid
     */
    private void removeInverter(String sid) {
    private void removeLocalInverter(String sid) {
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
        if (gatewayBean != null) {
            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
@@ -721,7 +728,41 @@
    }
    /**
     * 搜索网关列表
     * 清空逆变器住宅id
     *
     * @param mac mac
     */
    public void clearInverterHomeId(String mac, LinkCallBack<Boolean> callBeak) {
        String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", "");
        json.addProperty("server_addr", "");
        TcpClient.getInstance().sendDataToLinkGateway(mac, true, 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 (callBeak != null) {
                    callBeak.onSuccess(true);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (callBeak != null) {
                    callBeak.onError(e);
                }
            }
        });
    }
    /**
     * 搜索逆变器列表
     *
     * @param gatewayCallBack -
     */
@@ -730,6 +771,7 @@
        //网关搜索
        HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack);
    }
    /**
     * 刷新逆变器缓存信息(包括设置mqtt秘钥)
@@ -752,6 +794,10 @@
        if (deviceRemoteInfo != null) {
            gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥
            gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
            //用之前的库,底层mqtt订阅,加解密会用到该参数;
            HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库
            HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
        }
        gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId());//住宅id
        gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态
@@ -762,13 +808,16 @@
        gatewayBean.setDevice_name(cloudInverterDeviceBean.getName());//设备名称
        gatewayBean.setGatewayType(cloudInverterDeviceBean.getSpk());//设置spk
        gatewayBean.setLocalEncrypt(true);
        gatewayBean.setMaster("true");
        gatewayBean.setMaster(GatewayMasterType.MasterTrue);
        gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态
        gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号
        gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//设备类型(产品二级分类名称
        gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号
        gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率
        gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量
        gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号
        gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk
    }