From 4e59e31d1eff03798eabae43e3062be9fe61218c Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期四, 29 六月 2023 17:55:34 +0800
Subject: [PATCH] 2023年06月29日17:55:11

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java |  205 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 173 insertions(+), 32 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
index 245bdbc..5914e25 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -1,13 +1,35 @@
 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.bean.ModBusBean;
+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.common.exception.HDLLinkCode;
+import com.hdl.sdk.link.common.exception.HDLLinkException;
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
+import com.hdl.sdk.link.core.callback.GatewayCallBack;
+import com.hdl.sdk.link.core.callback.ModbusCallBack;
+import com.hdl.sdk.link.core.connect.HDLModBusConnect;
 
+import org.greenrobot.eventbus.EventBus;
 import org.json.JSONObject;
 
+
+import java.util.List;
 
 import io.dcloud.feature.unimp.DCUniMPJSCallback;
 
@@ -34,6 +56,8 @@
         return sHdlUniLogic;
     }
 
+    //region ******銆愬師鐢熴�戝拰銆愬皬绋嬪簭銆戦�氳鐨勬柟娉� onOtherUniMPEventReceive();openUniMP();sendUni();******
+
     /**
      * 鍘熺敓鎺ユ敹鍒皍ni鍙戞潵鐨勬暟鎹�
      *
@@ -44,18 +68,83 @@
      */
     @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)) {
+            String type = getKeyValue("type", data);
+            if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) {
+                //浣忓畢妯″潡
+                switch (type) {
+                    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);
+                    uniCallbackData(null, callback);
+                }
+            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
+                //璁惧妯″潡
+                switch (type) {
+                    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) {
+                                uniCallbackData(gatewayBeanList, callback);
+                            }
+
+                            @Override
+                            public void onError(HDLLinkException e) {
+                                //鍙戦�佸け璐�
+                                uniCallbackData(null, e.getCode(), "", callback);
+                            }
+                        });
+                    }
+                    break;
+                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
+                        sendModBus(data, callback);
+                    }
+                }
+
+            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) {
+                WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance());
+                //wifi妯″潡
+                switch (type) {
+                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
+                        //鑾峰彇wifi鍒楄〃
+                        if (callback != null) {
+                            uniCallbackData(wifiUtils.getScanResult(), callback);
+                        }
+                    }
+                    break;
+                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
+                        //褰撳墠wifi璇︽儏
+                        if (callback != null) {
+                            uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), callback);
+                        }
+                    }
+                    break;
+                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
+                        //wifi杩炴帴
+                    }
+                    break;
+
                 }
             }
             HdlLogLogic.print("uni===鍘熺敓鎺ユ敹uni鍙戞潵鐨勬暟鎹�", event + "\r\n" + data);
@@ -63,6 +152,38 @@
             HdlLogLogic.print("uni===鍘熺敓鎺ユ敹uni鍙戞潵鐨勬暟鎹�", e.getMessage());
         }
 
+    }
+
+    /**
+     * 鍙戦�乵odbus鍗忚鏁版嵁
+     *
+     * @param data     modbus鏁版嵁
+     * @param callback 鍥炶皟
+     */
+    void sendModBus(Object data, DCUniMPJSCallback callback) {
+        String tempData = getKeyValue("data", data);
+        if (tempData == null) {
+            LogUtils.i("data鍐呭涓虹┖");
+            return;
+        }
+        ModBusBean modBusBean = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(tempData), ModBusBean.class);
+        if (modBusBean.getOid() == null || modBusBean.getData() == null) {
+            LogUtils.i("鍐呭涓虹┖,oid=" + modBusBean.getOid() + " data=" + modBusBean.getData());
+            return;
+        }
+
+        //鍙戦�乵odbus鍗忚
+        HDLModBusConnect.getInstance().Send(modBusBean.getOid(), modBusBean.getData(), new ModbusCallBack() {
+            @Override
+            public void onSuccess(byte[] data) {
+                uniCallbackData(data, callback);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
+            }
+        });
     }
 
     /**
@@ -82,15 +203,12 @@
      * 鍘熺敓銆愪富鍔ㄣ�戝悜灏忕▼搴忓彂閫侀�氱煡浜嬩欢
      * 娉ㄦ剰锛氶渶瑕佹彁鍓嶅皬绋嬪簭鍦ㄨ繍琛屾墠鍙垚鍔�
      *
-     * @param topic 涓婚
-     * @param body  闄勪欢鏁版嵁(娌℃湁鏁版嵁濉玭ull)
+     * @param topic            涓婚澶х被(濡�:UNI_EVENT_REPLY_HOME_MODEL)
+     * @param callBackBaseBean 鈥斺��
      */
-    public void sendUni(String topic, String body) {
+    public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
         try {
-            HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
-            callBackBaseBean.setTopic(topic);
-            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());
         }
@@ -102,6 +220,10 @@
     public void checkRemoveOtherUniMPEventCallBack() {
         HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
     }
+    //endregion
+
+
+    //region ******涓�鑸柟娉�******
 
     /**
      * 缁勮uni鍙戦�佹暟鎹牸寮�
@@ -138,14 +260,14 @@
      * @param msg      缁撴灉鎻忚堪鐨勪俊鎭�
      * @param callback 鍥炶皟
      */
-    private void uniCallbackData(Object obj, String code, String msg, DCUniMPJSCallback callback) {
+    private void uniCallbackData(Object obj, int code, String msg, DCUniMPJSCallback callback) {
         HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
         try {
             uniCallBackBaseBean.setCode(code);
             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());
@@ -154,20 +276,8 @@
 
     }
 
-
-    /**
-     * 鑾峰彇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;
+    private void uniCallbackData(Object obj, DCUniMPJSCallback callback) {
+        uniCallbackData(obj, HDLLinkCode.HDL_SUCCESS.getCode(), HDLLinkCode.HDL_SUCCESS.getMsg(), callback);
     }
 
     /**
@@ -177,11 +287,42 @@
      */
     private JSONObject getJSONObject(Object obj) {
         try {
+            if (obj == null) {
+                return new JSONObject();
+            }
+            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
+
 }
 

--
Gitblit v1.8.0