hxb
2024-10-24 ce88de4891b87c3b7b2750575e15d6e48d518852
HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/workbench/databackup/controller/DataBackupController.java
New file
@@ -0,0 +1,377 @@
package com.hdl.linkpm.sdk.workbench.databackup.controller;
import android.os.Environment;
import com.google.gson.JsonObject;
import com.hdl.hdlhttp.HxHttp;
import com.hdl.hdlhttp.callback.HxException;
import com.hdl.linkpm.sdk.HDLLinkPMSdk;
import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi;
import com.hdl.linkpm.sdk.core.bean.PageInfoListBean;
import com.hdl.linkpm.sdk.core.callback.IDefaultCallBack;
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.utils.HDLExceptionSubmitUtils;
import com.hdl.linkpm.sdk.utils.HDLFileUtils;
import com.hdl.linkpm.sdk.utils.HDLGsonUtils;
import com.hdl.linkpm.sdk.workbench.databackup.bean.BackupListBean;
import com.hdl.linkpm.sdk.workbench.databackup.bean.GatewayBackupBean;
import java.io.File;
import java.util.List;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.subscribers.DisposableSubscriber;
import okhttp3.ResponseBody;
/**
 * Created by hxb on 2022/2/23.
 */
public class DataBackupController {
    /**
     * instance
     */
    private volatile static DataBackupController instance;
    /**
     * getInstance
     *
     * @return DataBackupController
     */
    public static synchronized DataBackupController getInstance() {
        if (instance == null) {
            synchronized (DataBackupController.class) {
                if (instance == null) {
                    instance = new DataBackupController();
                }
            }
        }
        return instance;
    }
    /**
     * 上传备份数据
     *
     * @param homeId   住宅ID
     * @param data     上传的数据内容
     * @param callBack
     * @return
     */
    public Disposable dataBackupUpload(String homeId, byte[] data, IDefaultCallBack callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_BACKUP_UPLOAD + "?houseId=" + homeId;
        String path = HDLLinkPMSdk.getContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath() + "/Temp/";
        String fileName = System.currentTimeMillis() + "";
        HDLFileUtils.byteToFile(data, path, fileName);
        File file = new File(path + fileName);
        return HxHttp.builder()
                .file(file, "file")
                .url(requestUrl)
                .build()
                .upload()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String code) {
                        HDLFileUtils.deleteFile(file);
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLFileUtils.deleteFile(file);
                        HDLExceptionSubmitUtils.submit(requestUrl, homeId, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 删除备份数据
     *
     * @param homeId   住宅ID
     * @param callBack
     * @return
     */
    public Disposable databackDel(String homeId, IDefaultCallBack callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_BACKUP_DEL;
        JsonObject json = new JsonObject();
        json.add("houseIds", HDLGsonUtils.toJsonArray(new String[]{homeId}));
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String url) {
                        if (null != callBack) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取数据最新下载路径
     *
     * @param homeId   住宅ID
     * @param callBack
     * @return
     */
    public Disposable databackDownload(String homeId,String path, IDefaultCallBack callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_BACKUP_DOWNLOAD_URL;
        JsonObject json = new JsonObject();
        json.addProperty("houseId", homeId);
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String url) {
                        HxHttp.builder()
                                .url(url)
                                .build()
                                .download()
                                .subscribe(new DisposableSubscriber<ResponseBody>() {
                                    @Override
                                    public void onNext(ResponseBody responseBody) {
                                        if (HDLFileUtils.writeFile(path, responseBody.byteStream())) {
                                            if (null != callBack) {
                                                callBack.onSuccess();
                                            }
                                        } else {
                                            if (callBack != null) {
                                                callBack.onFailure(new HDLException(HxException.ErrorCode.UNKNOWN, "Download Fail"));
                                            }
                                        }
                                    }
                                    @Override
                                    public void onError(Throwable t) {
                                        if (callBack != null) {
                                            callBack.onFailure(new HDLException(HxException.ErrorCode.UNKNOWN, t.getMessage()));
                                        }
                                    }
                                    @Override
                                    public void onComplete() {
                                    }
                                });
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取备份列表
     *
     * @param userId   用户ID
     * @param pageNo 开始索引
     * @param pageSize 页面大小
     * @param callBack
     * @return
     */
    public Disposable databackList(String userId,int pageNo, int pageSize, IResponseCallBack<PageInfoListBean<BackupListBean>> callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_BACKUP_LIST;
        JsonObject json = new JsonObject();
        json.addProperty("pageNo", pageNo);
        json.addProperty("pageSize", pageSize);
        json.addProperty("debugUserId", userId);
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<PageInfoListBean<BackupListBean>>() {
                    @Override
                    public void onResponse(PageInfoListBean<BackupListBean> response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取网关最新的三份备份数据
     *
     * @param homeId   住宅Id
     * @param gatewayId 云端上网关gatewayId
     * @param callBack
     * @return
     */
    public Disposable getGatewayBackupDataList(String homeId, String gatewayId, IResponseCallBack<List<GatewayBackupBean>> callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_GATEWAY_BACKUP_LIST;
        JsonObject json = new JsonObject();
        json.addProperty("spaceCode", homeId);
        json.addProperty("gatewayId", gatewayId);
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<GatewayBackupBean>>() {
                    @Override
                    public void onResponse(List<GatewayBackupBean> response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     *  获取调试完成的备份文件回复
     *
     * @param homeId   住宅Id
     * @param gatewayId 云端上网关gatewayId
     * @param callBack
     * @return
     */
    public Disposable getDebugGatewayBackupDataList(String homeId, String gatewayId, IResponseCallBack<GatewayBackupBean> callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_GATEWAY_BACKUP_DEBUGCOMPLETEBACKUPFILE;
        JsonObject json = new JsonObject();
        json.addProperty("spaceCode", homeId);
        json.addProperty("gatewayId", gatewayId);
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<GatewayBackupBean>() {
                    @Override
                    public void onResponse(GatewayBackupBean response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 调试宝选择网关恢复的备份文件
     *
     * @param homeId   住宅Id
     * @param gatewayId 云端上网关gatewayId
     * @param recordId 备份记录id
     * @param callBack 回调
     * @return Disposable
     */
    public Disposable getGatewayBackupRecover(String homeId, String gatewayId,long recordId, IResponseCallBack  callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_GATEWAY_BACKUP_RECOVER;
        JsonObject json = new JsonObject();
        json.addProperty("spaceCode", homeId);
        json.addProperty("gatewayId", gatewayId);
        json.addProperty("recordId", recordId);
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 网关替换
     *
     * @param homeId   住宅Id
     * @param gatewayId 云端上网关gatewayId
     * @param newMac newMac   新网关mac
     * @param callBack 回调
     * @return Disposable
     */
    public Disposable getGatewayReplace(String homeId, String gatewayId,String newMac, IResponseCallBack  callBack) {
        String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + HDLCloudUserApi.POST_GATEWAY_REPLACE;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("gatewayId", gatewayId);//旧网关id
        json.addProperty("newMac", newMac);//newMac   新网关mac
        return HxHttp.builder()
                .raw(json.toString())
                .url(requestUrl)
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl, json, e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
}