wjc
2023-06-29 4a209c4c6ef8b4402114ca26b45dd4bbe869190b
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -1,28 +1,30 @@
package com.hdl.photovoltaic.other;
import android.content.Intent;
import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.bean.BaseEventBus;
import com.hdl.photovoltaic.config.ConstantManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.ui.bean.HouseIdBean;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
import com.hdl.photovoltaic.uni.UniToAndroidBean;
import com.hdl.photovoltaic.utils.WifiUtils;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
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.core.connect.HDLModBusConnect;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import io.dcloud.feature.unimp.DCUniMPJSCallback;
@@ -50,6 +52,8 @@
        return sHdlUniLogic;
    }
    //region ******【原生】和【小程序】通讯的方法 onOtherUniMPEventReceive();openUniMP();sendUni();******
    /**
     * 原生接收到uni发来的数据
     *
@@ -64,33 +68,16 @@
            if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
                return;
            }
            org.json.JSONObject jsonObject = getJSONObject(data);
            String type_value = "";
            String oid="";
            if (jsonObject.has("type")) {
                type_value = jsonObject.getString("type");
            }
            if (jsonObject.has("oid")) {
                oid = jsonObject.getString("oid");
            }
            String type = getKeyValue("type", data);
            if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) {
                //住宅模块
                switch (type_value) {
                switch (type) {
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
                        //创建
                        HdlResidenceLogic.getInstance().getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
                            @Override
                            public void onSuccess(List<HouseIdBean> list) {
                                HdlResidenceLogic.getInstance().setHouseIdList(list);
                            }
                            @Override
                            public void onFailure(Exception exception) {
                            }
                        });
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setType(ConstantManage.EVENTBUS_POST_HOME_CREATED);
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
@@ -104,7 +91,7 @@
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
                //设备模块
                switch (type_value) {
                switch (type) {
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        //添加
                    }
@@ -114,31 +101,35 @@
                        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
                            @Override
                            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                                HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                                callBackBaseBean.setTopic(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL);
                                callBackBaseBean.setType(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_LIST);
                                callBackBaseBean.setData(gatewayBeanList);
                                sendUni(callBackBaseBean);
//                                HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
//                                callBackBaseBean.setType(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_LIST);
//                                callBackBaseBean.setData(gatewayBeanList);
//                                uniCallbackData(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, "", "", callBackBaseBean);
                            }
                            @Override
                            public void onError(HDLLinkException e) {
                                //发送失败
                                HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                                callBackBaseBean.setType(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_LIST);
                                callBackBaseBean.setCode(e.getCode() + "");
                                callBackBaseBean.setCode(e.getMsg());
                                sendUni(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, callBackBaseBean);
                            }
                        });
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND:{
                        //发送modbus协议
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        //发送modbus协议
//                        HDLModBusConnect.getInstance().Send(uniToAndroidBean.getGatewayOid(), null, );
                    }
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) {
                WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance());
                //wifi模块
                switch (type_value) {
                switch (type) {
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
                        //获取wifi列表
                        if (callback != null) {
@@ -183,10 +174,13 @@
    /**
     * 原生【主动】向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param topic            主题大类(如:UNI_EVENT_REPLY_HOME_MODEL)
     * @param callBackBaseBean ——
     */
    public void sendUni(HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
    public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
        try {
            HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, HDLUniMP.UNI_APP_ID, getJSONObject(callBackBaseBean));
            HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean));
        } catch (Exception e) {
            HdlLogLogic.print("uni===原生主动向小程序发送通知事件", e.getMessage());
        }
@@ -198,6 +192,10 @@
    public void checkRemoveOtherUniMPEventCallBack() {
        HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
    }
    //endregion
    //region ******一般方法******
    /**
     * 组装uni发送数据格式
@@ -250,22 +248,6 @@
    }
    /**
     * 获取uni发送对象
     *
     * @param obj  附加数据
     * @param code 状态码
     * @param msg  结果描述的信息
     */
    private HDLUniMP.UniCallBackBaseBean getUniCallBackBaseBean(Object obj, String code, String msg) {
        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
        uniCallBackBaseBean.setCode(code);
        uniCallBackBaseBean.setMes(msg);
        uniCallBackBaseBean.setData(obj);
        return uniCallBackBaseBean;
    }
    /**
     * 当前对象转 JSONObject
     *
@@ -279,12 +261,36 @@
            if (TextUtils.isEmpty(obj.toString())) {
                return new JSONObject();
            }
            if (obj instanceof JSONObject) {
                return (JSONObject) obj;
            }
            String json = new Gson().toJson(obj);
            return new JSONObject(json);
        } catch (Exception e) {
            return new JSONObject();
        }
    }
    /**
     * 获取 KeyValue
     *
     * @param key -
     * @param obj -
     * @return value
     */
    private String getKeyValue(String key, Object obj) {
        try {
            JSONObject jsonObject = this.getJSONObject(obj);
            if (jsonObject.has(key)) {
                return jsonObject.getString(key);
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }
    //endregion
}