wjc
2023-06-28 14de918a79943e4961b09fa01ed320c6cad41f2e
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -1,11 +1,25 @@
package com.hdl.photovoltaic.other;
import android.text.TextUtils;
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.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
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 org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
@@ -44,18 +58,97 @@
     */
    @Override
    public void onOtherUniMPEventReceive(String appId, String event, Object data, DCUniMPJSCallback callback) {
        if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
            return;
        }
        try {
            org.json.JSONObject jsonObject = null;
            if (data != null) {
                jsonObject = new org.json.JSONObject(data.toString());
            if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
                return;
            }
            //住宅模块
            if (HDLUniMP.UNI_EVENT_REPLY_HOME_LIST.equals(event)) {
            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");
            }
            if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) {
                //住宅模块
                switch (type_value) {
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
                        //创建
                        //eventbus通知
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setType(ConstantManage.EVENTBUS_POST_HOME_CREATED);
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
                        //详情
                    }
                    break;
                }
                if (callback != null) {
                    uniCallbackData(null, "0", "", callback);
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
                //设备模块
                switch (type_value) {
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        //添加
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: {
                        //网关搜索
                        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);
                            }
                            @Override
                            public void onError(HDLLinkException e) {
                                //发送失败
                            }
                        });
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND:{
                        //发送modbus协议
                    }
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) {
                WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance());
                //wifi模块
                switch (type_value) {
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
                        //获取wifi列表
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getScanResult(), "0", "", callback);
                        }
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
                        //当前wifi详情
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), "0", "", callback);
                        }
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
                        //wifi连接
                    }
                    break;
                }
            }
            HdlLogLogic.print("uni===原生接收uni发来的数据", event + "\r\n" + data);
@@ -68,7 +161,7 @@
    /**
     * 原生打开uni指定页面
     *
     * @param path       打开路径
     * @param path       打开全路径
     * @param jsonObject 附件数据(没有数据填null)
     */
    public void openUniMP(String path, JSONObject jsonObject) {
@@ -82,15 +175,16 @@
     * 原生【主动】向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param topic 主题
     * @param topic 主题大类
     * @param type  功能类
     * @param body  附件数据(没有数据填null)
     */
    public void sendUni(String topic, String body) {
    public void sendUni(String topic, String type, String body) {
        try {
            HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
            callBackBaseBean.setTopic(topic);
            callBackBaseBean.setType(type);
            callBackBaseBean.setData(body);
            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());
        }
@@ -145,29 +239,13 @@
            uniCallBackBaseBean.setMes(msg);
            uniCallBackBaseBean.setData(obj);
            if (callback != null) {
                callback.invoke(uniCallBackBaseBean);
                callback.invoke(getJSONObject(uniCallBackBaseBean));
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
        }
    }
    /**
     * 获取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;
    }
    /**
@@ -177,7 +255,14 @@
     */
    private JSONObject getJSONObject(Object obj) {
        try {
            if (obj == null) {
                return new JSONObject();
            }
            if (TextUtils.isEmpty(obj.toString())) {
                return new JSONObject();
            }
            String json = new Gson().toJson(obj);
            return new JSONObject(json);
        } catch (Exception e) {
            return new JSONObject();