mac
2024-10-24 73a49ddd0516e5c9a4b697c593d62c74e420403b
HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/device/controller/HDLPMDeviceController.java
New file
@@ -0,0 +1,733 @@
package com.hdl.linkpm.sdk.device.controller;
import android.text.TextUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hdl.hdlhttp.HxHttp;
import com.hdl.linkpm.sdk.core.api.HDLCloudHomeApi;
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.device.bean.CategoryInfo;
import com.hdl.linkpm.sdk.device.bean.DeviceOidInfoBean;
import com.hdl.linkpm.sdk.device.bean.FunctionActionBean;
import com.hdl.linkpm.sdk.device.bean.FunctionInfoBaseBean;
import com.hdl.linkpm.sdk.device.bean.FunctionInfoListBean;
import com.hdl.linkpm.sdk.device.bean.GatewayCloudBean;
import com.hdl.linkpm.sdk.device.bean.ProductInfo;
import com.hdl.linkpm.sdk.home.bean.QuestionListInfo;
import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils;
import com.hdl.linkpm.sdk.utils.HDLGsonUtils;
import java.util.List;
import io.reactivex.rxjava3.disposables.Disposable;
/**
 * Created by jlchen on 12/3/21.
 * 设备相关的网络请求
 */
public class HDLPMDeviceController {
    /**
     * instance
     */
    private volatile static HDLPMDeviceController instance;
    /**
     * getInstance
     *
     * @return HDLDeviceController
     */
    public static synchronized HDLPMDeviceController getInstance() {
        if (instance == null) {
            synchronized (HDLPMDeviceController.class) {
                if (instance == null) {
                    instance = new HDLPMDeviceController();
                }
            }
        }
        return instance;
    }
    /***************4.设备、功能增删改查管理***************/
    /**
     * 设备基本信息列表(oid)全量添加
     *
     * @param homeId        住宅id
     * @param oidDeviceList 最新的设备Oid列表
     * @param callBack
     * @return
     */
    public Disposable fullAddDeviceOidList(String homeId, List<DeviceOidInfoBean> oidDeviceList, IDefaultCallBack callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.add("devices", HDLGsonUtils.toJsonArray(oidDeviceList));
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_OID_LIST_FULL_ADD);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 设备基本信息列表(oid)获取
     *
     * @param homeId   住宅id
     * @param callBack
     * @return
     */
    public Disposable getDeviceOidList(String homeId, IResponseCallBack<List<DeviceOidInfoBean>> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_OID_LIST_GET);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<DeviceOidInfoBean>>() {
                    @Override
                    public void onResponse(List<DeviceOidInfoBean> response) {
                        if (callBack != null) {
                            callBack.onSuccess(response);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 设备基本信息(oid)删除
     * 删除设备或者从网关等oid
     *
     * @param homeId   住宅id
     * @param oid      设备的oid
     * @param callBack
     * @return
     */
    public Disposable removeDeviceOid(String homeId, String oid, IDefaultCallBack callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("oid", oid);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_OID_REMOVE);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 设备(功能)全量更新
     *
     * @param homeId       住宅id
     * @param gatewayId    网关Id
     * @param functionList 最新的功能列表
     * @param callBack
     * @return
     */
    public Disposable fullAddFunctionList(String homeId, String gatewayId, List<FunctionInfoBaseBean> functionList, IDefaultCallBack callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("gatewayId", gatewayId);
        json.add("devices", HDLGsonUtils.toJsonArray(functionList));
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_FUNCTION_LIST_FULL_ADD);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取产品分类树
     *
     * @param callBack
     * @return
     */
    public Disposable getCategoryList(IResponseCallBack<List<CategoryInfo>> callBack) {
        JsonObject json = new JsonObject();
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_CATEGORY_LIST_ALLTREE);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<CategoryInfo>>() {
                    @Override
                    public void onResponse(List<CategoryInfo> 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 callBack
     * @return
     */
    public Disposable getGatewayList(String homeId, IResponseCallBack<List<GatewayCloudBean>> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_GATEWAY_LIST);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<GatewayCloudBean>>() {
                    @Override
                    public void onResponse(List<GatewayCloudBean> 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 callBack
     * @return
     */
    public Disposable getCategoryListByName(String productName, IResponseCallBack<List<ProductInfo>> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("productName", productName);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_CATEGORY_DEVICE_LIST);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<ProductInfo>>() {
                    @Override
                    public void onResponse(List<ProductInfo> 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 callBack
     * @return
     */
    public Disposable getCategoryListByCode(String categorySecondCode, IResponseCallBack<List<ProductInfo>> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("categorySecondCode", categorySecondCode);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_CATEGORY_DEVICE_LIST);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<List<ProductInfo>>() {
                    @Override
                    public void onResponse(List<ProductInfo> 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 网关Id
     * @param callBack
     * @return
     */
    public Disposable getFunctionList(String homeId, String gatewayId, IResponseCallBack<FunctionInfoListBean> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("gatewayId", gatewayId);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_FUNCTION_LIST_GET);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<FunctionInfoListBean>() {
                    @Override
                    public void onResponse(FunctionInfoListBean 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 网关Id
     * @param devices   云端设备ID集合
     * @param callBack
     * @return
     */
    public Disposable batchDeleteFunctionList(String homeId, String gatewayId, List<String> devices, IDefaultCallBack callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("gatewayId", gatewayId);
        json.add("devices", HDLGsonUtils.toJsonArray(devices));
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_FUNCTION_BATCH_DELETE);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 控制设备
     *
     * @param homeId    住宅id
     * @param gatewayId 网关Id
     * @param actions   动作
     * @param callBack
     * @return
     */
    public Disposable controlFunction(String homeId, String gatewayId, List<FunctionActionBean> actions, IDefaultCallBack callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("gatewayId", gatewayId);
        json.add("actions", HDLGsonUtils.toJsonArray(actions));
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_FUNCTION_CONTROL);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (callBack != null) {
                            callBack.onSuccess();
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取常见问题列表
     *
     * @param languageType 0:中文 1:英文,可用值:CHINESE,ENGLISH
     * @param pageNo       当前页
     * @param pageSize     页面大小
     * @param question     问题
     * @param callBack
     * @return
     */
    public Disposable getQuestionList(String languageType, String question, String pageNo, String pageSize, IResponseCallBack<QuestionListInfo> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("zoneType", languageType);
        json.addProperty("question", question);
        json.addProperty("pageNo", pageNo);
        json.addProperty("pageSize", pageSize);
        json.addProperty("softwareType", "DEBUGGING_TREASURE");
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_QUESTION_GET_LIST);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<QuestionListInfo>() {
                    @Override
                    public void onResponse(QuestionListInfo 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 name     设备名称
     * @param apk      产品spk
     * @param oid      设备oid
     * @param mac      设备mac
     * @param sid      设备sid
     * @param uidList     房间uid列表
     * @param callBack 回调
     * @return -
     */
    public Disposable bindMillimeterDevice(String homeId, String name, String apk, String oid, String mac, String sid, List<String> uidList, IResponseCallBack<String> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("name", name);
        json.addProperty("spk", apk);
        json.addProperty("oid", oid);
        json.addProperty("mac", mac);
        json.addProperty("sid", sid);
        if(uidList!=null) {
            json.add("uids", HDLGsonUtils.toJsonArray(uidList));
        }
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_INDEPENDENT_REGISTER);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String str) {
                        if (callBack != null) {
                            callBack.onSuccess(str);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 解绑毫米波设备
     *
     * @param homeId   住宅Id
     * @param spk      产品spk
     * @param mac      设备mac
     * @param callBack 回调
     * @return -
     */
    public Disposable unBindMillimeterDevice(String homeId, String spk, String mac, IResponseCallBack<String> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("spk", spk);
        json.addProperty("mac", mac);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_INDEPENDENT_UNBIND);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String str) {
                        if (callBack != null) {
                            callBack.onSuccess(str);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取link功能列表
     *
     * @param homeId   住宅Id
     * @param spk      spk(指定获取单个功能)
     * @param callBack 回调
     * @return -
     */
    public Disposable getDeviceList(String homeId, String spk, IResponseCallBack<FunctionInfoListBean> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        if(!TextUtils.isEmpty(spk)) {
            json.addProperty("spk", spk);
        }
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_FUNCTION_LIST_GET);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<FunctionInfoListBean>() {
                    @Override
                    public void onResponse(FunctionInfoListBean functionInfoListBean) {
                        if (callBack != null) {
                            callBack.onSuccess(functionInfoListBean);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 获取毫米波(获取设备远程通讯信息)
     *
     * @param homeId   住宅Id
     * @param spk      spk
     * @param mac      设备mac
     * @param callBack 回调
     * @return -
     */
    public Disposable getDeviceRemoteInfo(String homeId, String spk,String mac, IResponseCallBack<String> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("spk", spk);
        json.addProperty("mac", mac);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_REMOTEINFO_GET);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String str) {
                        if (callBack != null) {
                            callBack.onSuccess(str);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 设备(功能)修改备注
     *
     * @param homeId    住宅id
     * @param deviceId 设备Id
     * @param name 设备名称
     * @return  callBack
     */
    public Disposable rename(String homeId, long deviceId,String name, IResponseCallBack<String> callBack) {
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("deviceId", deviceId);
        json.addProperty("name", name);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_RENAME);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String string) {
                        if (callBack != null) {
                            callBack.onSuccess(string);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
    /**
     * 设备(功能)绑定房间
     *
     * @param homeId    住宅id
     * @param deviceIds 设备id列表
     * @param roomIds 房间id列表
     * @return  callBack
     */
    public Disposable bindRoom(String homeId, List<String> deviceIds,List<String> roomIds, IResponseCallBack<String> callBack) {
        JsonObject json = new JsonObject();
        JsonArray deviceIdsArray=new JsonArray();
        JsonArray roomIdsArray=new JsonArray();
        for (int i = 0; i < deviceIds.size(); i++) {
            deviceIdsArray.add(deviceIdsArray.get(i));
        }
        for (int i = 0; i < roomIds.size(); i++) {
            roomIdsArray.add(roomIdsArray.get(i));
        }
        json.addProperty("homeId", homeId);
        json.add("deviceIds", deviceIdsArray);
        json.add("roomIds", roomIdsArray);
        String requestUrl = HDLCloudHomeApi.getRequestUrl(HDLCloudHomeApi.POST_DEVICE_BINDROOM);
        return HxHttp.builder()
                .url(requestUrl)
                .raw(json.toString())
                .build()
                .post()
                .subscribeWith(new HDLResponse<String>() {
                    @Override
                    public void onResponse(String string) {
                        if (callBack != null) {
                            callBack.onSuccess(string);
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                        HDLExceptionSubmitUtils.submit(requestUrl,json,e);
                        if (callBack != null) {
                            callBack.onFailure(e);
                        }
                    }
                });
    }
}