package com.hdl.linkpm.sdk.template;/* *create by wxr *date 2022/1/19 */ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.hdl.hdlhttp.HxHttp; import com.hdl.linkpm.sdk.HDLLinkPMSdk; import com.hdl.linkpm.sdk.core.callback.IDefaultCallBack; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader; import com.hdl.linkpm.sdk.core.response.HDLResponse; import com.hdl.linkpm.sdk.template.bean.CloudTemplateDevice; import com.hdl.linkpm.sdk.template.bean.CloudTemplateSpatialInfo; import com.hdl.linkpm.sdk.template.bean.TemplateDeviceBean; import com.hdl.linkpm.sdk.template.bean.TemplateFunctionBean; import com.hdl.linkpm.sdk.template.bean.TemplateGatewayBean; import com.hdl.linkpm.sdk.template.bean.TemplateListResponseBean; import com.hdl.linkpm.sdk.template.bean.TemplateLogicBean; import com.hdl.linkpm.sdk.template.bean.TemplateRoomPackBean; import com.hdl.linkpm.sdk.template.bean.TemplateSceneBean; import com.hdl.linkpm.sdk.template.bean.TemplateSecurityBean; import com.hdl.linkpm.sdk.template.callback.ICreateTemplateCallBack; import com.hdl.linkpm.sdk.template.callback.IGetTemplateDeviceListCallBack; import com.hdl.linkpm.sdk.template.callback.IGetTemplateGatewayListCallBack; import com.hdl.linkpm.sdk.template.callback.IGetTemplateSpatialInfoCallBack; import com.hdl.linkpm.sdk.template.callback.ITemplateFunctionsCallBack; import com.hdl.linkpm.sdk.template.callback.ITemplateListCallBack; import com.hdl.linkpm.sdk.template.callback.ITemplateLogicCallBack; import com.hdl.linkpm.sdk.template.callback.ITemplateScenesCallBack; import com.hdl.linkpm.sdk.template.callback.ITemplateSecurityCallBack; import com.hdl.linkpm.sdk.template.controller.HDLTemplateController; import com.hdl.linkpm.sdk.utils.HDLFileUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.functions.Consumer; import okhttp3.ResponseBody; public class HDLLinkPMTemplate { /** * instance */ private volatile static HDLLinkPMTemplate instance; /** * getInstance * * @return HDLPMTemplateController */ public static synchronized HDLLinkPMTemplate getInstance() { if (instance == null) { synchronized (HDLLinkPMTemplate.class) { if (instance == null) { instance = new HDLLinkPMTemplate(); } } } return instance; } /** * 创建模板 * @param templateName 模板名称 * @param communityCode 社区编码 * @param houseLayoutId 户型id (要调用户型列表的接口) * @param templateDesc 模板描述 * @param callBack 回调 */ public void CreateTemplate(String templateName,String communityCode,String houseLayoutId,String templateDesc,ICreateTemplateCallBack callBack) { HDLTemplateController.getInstance().CreateTemplate(templateName, communityCode, houseLayoutId, templateDesc, new ICreateTemplateCallBack() { @Override public void onSuccess(String templateId) { callBack.onSuccess(templateId); } @Override public void onFailure(HDLException error) { callBack.onFailure(error); } }); } /** * 添加设备(全量) * @param templateExtendsId * @param beans * @param callBack */ public void UpdateTemplateDevices(String templateExtendsId,List beans,IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdateTemplateDevices(templateExtendsId, beans, new IDefaultCallBack() { @Override public void onSuccess() { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 上传模板zb数据文件文件 * * @param templateExtendsId 模板ID * @param file 上传的文件 * @param callBack * @return */ public Disposable TemplateZbFileUpload(String templateExtendsId, File file, IDefaultCallBack callBack) { String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + "/iot-cloud/debug/backup/file/zigbeeDataUpload?templateExtendsId=" + templateExtendsId; return HxHttp.builder() .file(file, "file") .url(requestUrl) .build() .upload() .subscribeWith(new HDLResponse() { @Override public void onResponse(String code) { // FileUtils.deleteFile(file);//不用删掉本地文件 2022-09-28 17:24:06 if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException e) { HDLFileUtils.deleteFile(file); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 下载模板zb数据文件文件 */ public static boolean saveTempalteZbFile(ResponseBody zipData,File file) { // 如果存在就先删除数据,数据会追加 if(file.exists()){ file.delete(); file.deleteOnExit(); } InputStream is = null; byte[] buf = new byte[4096]; int len = 0; FileOutputStream fos = null; try { is = zipData.byteStream(); long total = zipData.contentLength(); fos = new FileOutputStream(file); long sum = 0; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); sum += len; } fos.flush(); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (is != null) is.close(); } catch (IOException e) { } try { if (fos != null) fos.close(); } catch (IOException e) { } } } /** * 获取模板zb数据文件Url * @return */ public Disposable getTempalteZbFileUrl(String templateExtendsId,File file, IDefaultCallBack callBack) { String requestUrl = HDLLinkPMSdk.getUserRegionUrl() + "/iot-cloud/debug/backup/file/zigbeeDataNewestUrl"; JsonObject json = new JsonObject(); json.addProperty("templateExtendsId", templateExtendsId); return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String url) { if (url == null) { callBack.onSuccess(); } else { if (callBack != null) { downloadTempalteZbFile(url, file, new IDefaultCallBack() { @Override public void onSuccess() { callBack.onSuccess(); } @Override public void onFailure(HDLException error) { callBack.onFailure(error); } }); } } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 下载模板zb数据文件文件 * @param callBack * @return */ public Disposable downloadTempalteZbFile(String url,File file, IDefaultCallBack callBack) { return HxHttp.builder() .url(url) .headers(HDLSmartHeader.IGNORE_SIGN_HEADER,1) .build() .download() .subscribe(new Consumer() { @Override public void accept(ResponseBody responseBody) throws Exception { //网络请求成功,读取文件保存到sd卡 // callBack.onSuccess(responseBody); //保存文件 // File file = new File(getUserFilesPath() + "/" + templateBean.getTemplateExtendsId()); boolean saveResult = saveTempalteZbFile(responseBody,file); if(saveResult) { callBack.onSuccess(); }else { callBack.onFailure(new HDLException(0,"")); } } }, new Consumer() { @Override public void accept(Throwable throwable) throws Exception { /** * 网络请求失败 * 具体可以自己实现 */ callBack.onFailure(new HDLException(404,"")); } }); } /** * 获取模板列表 * @param callBack */ public void GetTemplateList(int pageNo, int pageSize, ITemplateListCallBack callBack) { HDLTemplateController.getInstance().GetTemplateList(pageNo,pageSize,new ITemplateListCallBack() { @Override public void onSuccess(TemplateListResponseBean templateListResponseBeans) { callBack.onSuccess(templateListResponseBeans); } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 删除模板 * @param templateExtendsId 模板ID * @param callBack */ public void DeleteTemplate(String templateExtendsId,IDefaultCallBack callBack){ HDLTemplateController.getInstance().DeleteTemplate(templateExtendsId, new IDefaultCallBack() { @Override public void onSuccess() { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 更新网关数据(全量) */ public void UpdateTemplateGateway(String templateExtendsId , List gatewayBeans, IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdateTemplateGateway(templateExtendsId, gatewayBeans, new IDefaultCallBack() { @Override public void onSuccess() { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 更新房间(楼层)信息(全量) * @param templateExtendsId * @param rooms * @param callBack * @return */ public void UpdateTemplateRoom(String templateExtendsId ,List rooms, IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdateTemplateRoom(templateExtendsId, rooms, new IDefaultCallBack() { @Override public void onSuccess() { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 添加功能(全量) * @param templateExtendsId 模板名称 * @param functionInfoList 功能列表 * @param callBack 回调 */ public void UpdateTemplateFunctions(String templateExtendsId, List functionInfoList, IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdateTemplateFunctions(templateExtendsId, functionInfoList, new IDefaultCallBack() { @Override public void onSuccess() { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 更新模板场景(全量) * @param templateExtendsId * @param sceneBeans * @param callBack */ public void UpdataTemplateScenes(String templateExtendsId, List sceneBeans, IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdataTemplateScenes(templateExtendsId, sceneBeans, new IDefaultCallBack() { @Override public void onSuccess() { if(callBack != null){ callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if(callBack != null){ callBack.onFailure(error); } } }); } /** * 更新模板自动化 (全量) * @param templateExtendsId * @param logicBeansJson * @param callBack */ public void UpdataTemplateLogic(String templateExtendsId, JsonElement logicBeansJson,// List logicBeans, IDefaultCallBack callBack){ HDLTemplateController.getInstance().UpdataTemplateLogic(templateExtendsId, logicBeansJson, new IDefaultCallBack() { @Override public void onSuccess() { if(callBack != null){ callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if(callBack != null){ callBack.onFailure(error); } } }); } /** * 更新模板安防 (全量) * @param templateExtendsId * @param securityInfos * @param callBack */ public void UpdataTemplateSecurity(String templateExtendsId, JsonElement securityInfos ,IDefaultCallBack callBack ){ HDLTemplateController.getInstance().UpdataTemplateSecurity(templateExtendsId, securityInfos, new IDefaultCallBack() { @Override public void onSuccess() { if(callBack != null){ callBack.onSuccess(); } } @Override public void onFailure(HDLException error) { if(callBack != null){ callBack.onFailure(error); } } }); } /** * 获取区域信息 * @param templateExtendsId * @param callBack */ public void GetTemplateAreaSpatialInfo(String templateExtendsId ,IGetTemplateSpatialInfoCallBack callBack){ HDLTemplateController.getInstance().GetTemplateAreaSpatialInfo(templateExtendsId, new IGetTemplateSpatialInfoCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 获取网关列表 * @param id * @param callBack */ public void GetTemplateGatewayList(String id, IGetTemplateGatewayListCallBack callBack){ HDLTemplateController.getInstance().GetTemplateGatewayList(id, new IGetTemplateGatewayListCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 获取设备信息 * @param templateId * @param callBack */ public void GetTemplateDeviceList(String templateId,IGetTemplateDeviceListCallBack callBack){ HDLTemplateController.getInstance().GetTemplateDeviceList(templateId, new IGetTemplateDeviceListCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 获取功能列表 * @param templateExtendsId * @param callBack */ public void GetTemplateFunctions(String templateExtendsId , ITemplateFunctionsCallBack callBack){ HDLTemplateController.getInstance().GetTemplateFunctions(templateExtendsId, new ITemplateFunctionsCallBack() { @Override public void onSuccess(List templateFunctionBean) { if (callBack != null) { callBack.onSuccess(templateFunctionBean); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 获取场景列表 * @param templateExtendsId * @param callBack */ public void GetTemplateScenes(String templateExtendsId , ITemplateScenesCallBack callBack){ HDLTemplateController.getInstance().GetTemplateScenes(templateExtendsId, new ITemplateScenesCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } }); } /** * 获取安防列表 * @param templateExtendsId * @param callBack */ public void GetTemplateSecurity(String templateExtendsId , ITemplateSecurityCallBack callBack){ HDLTemplateController.getInstance().GetTemplateSecurity(templateExtendsId, new ITemplateSecurityCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } } ); } /** * 获取逻辑列表 * @param templateExtendsId * @param callBack */ public void GetTemplateLogic(String templateExtendsId ,ITemplateLogicCallBack callBack){ HDLTemplateController.getInstance().GetTemplateLogic(templateExtendsId, new ITemplateLogicCallBack() { @Override public void onSuccess(List data) { if (callBack != null) { callBack.onSuccess(data); } } @Override public void onFailure(HDLException error) { if (callBack != null) { callBack.onFailure(error); } } } ); } }