From 48bd94f4e362c8a604017cbaf4c12e08be10c816 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期五, 10 五月 2024 17:25:37 +0800
Subject: [PATCH] 2024年05月10日17:25:30

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java |  237 +++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 165 insertions(+), 72 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 467ca14..77eda60 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -10,13 +10,11 @@
 import android.location.Location;
 import android.location.LocationManager;
 import android.os.IBinder;
+import android.provider.Settings;
 import android.text.TextUtils;
-import android.view.View;
-import android.widget.TextView;
 
 import androidx.core.app.ActivityCompat;
 
-import com.alibaba.fastjson.JSON;
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
@@ -45,6 +43,7 @@
 import com.hdl.photovoltaic.ui.bean.Geolocation;
 import com.hdl.photovoltaic.ui.bean.LinkBean;
 import com.hdl.photovoltaic.ui.bean.OidBean;
+import com.hdl.photovoltaic.ui.bean.UnCountBean;
 import com.hdl.photovoltaic.ui.me.AsRegardsActivity;
 import com.hdl.photovoltaic.ui.me.PersonalDataActivity;
 import com.hdl.photovoltaic.ui.me.SetActivity;
@@ -54,7 +53,6 @@
 import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
 import com.hdl.photovoltaic.utils.NetworkUtils;
 import com.hdl.photovoltaic.utils.WifiUtils;
-import com.hdl.photovoltaic.widget.ConfirmationCancelDialog;
 import com.hdl.sdk.link.common.exception.HDLLinkCode;
 import com.hdl.sdk.link.common.exception.HDLLinkException;
 import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
@@ -72,11 +70,14 @@
 
 
 import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.lang.reflect.Type;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.net.InetSocketAddress;
+import java.net.Socket;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -283,18 +284,28 @@
                     }
                     break;
                     //褰撳墠wifi璇︽儏
-                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
+                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_PHONE_INFO: {
                         if (callback != null) {
                             uniCallbackData(mode_type, wifiUtils.getCurrentConnectWifiInfo(), callback);
                         }
                     }
                     break;
-                    //wifi杩炴帴
-                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
+                    //閰嶇疆WiFi淇℃伅缁欓�嗗彉鍣�
+                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_SET_DEVICE: {
+                        String mac = getKeyValue("mac", getKeyValue("data", data));//
+                        String wif_type = getKeyValue("wif_type", getKeyValue("data", data));//1锛氱儹鐐�,2锛氭棤绾縲ifi,3锛氫互澶綉
+                        String ssid = getKeyValue("ssid", getKeyValue("data", data));//
+                        String password = getKeyValue("password", getKeyValue("data", data));//
+                        startTcpThreadSendAccountAndPassword(mode_type, ssid, password, callback);
 
                     }
                     break;
-
+                    //鎵撳紑鎵嬫満Wifi璁剧疆鐣岄潰
+                    case HDLUniMP.UNI_EVENT_REPLY_OPEN_WIFI_SETTINGS: {
+                        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
+                        HDLApp.getInstance().startActivity(intent);
+                    }
+                    break;
                 }
             } else if (HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL.equals(topic)) {
                 //OTA鍗囩骇妯″潡
@@ -376,49 +387,6 @@
                     break;
 
                 }
-            } else if (HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL.equals(topic)) {
-                //鍏跺畠妯″潡
-                switch (type) {
-                    //鑾峰彇娓╁害鍗曚綅
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNIT: {
-                        uniCallbackData(mode_type, UserConfigManage.getInstance().getTemperature_unit(), callback);
-                    }
-                    break;
-                    //鑾峰彇褰撳墠app璇█
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APP_LANGUAGE: {
-                        uniCallbackData(mode_type, UserConfigManage.getInstance().getCurrentAppLanguage(), callback);
-                    }
-                    break;
-                    //鑾峰彇浣嶇疆淇℃伅
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_LOCATION_INFO: {
-                        this.uniGetLocationInfo(mode_type, data, callback);
-                    }
-                    break;
-                    //搴旂敤鍐茬獊
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APPLICATION_CONFLICT: {
-                        if (callback != null) {
-                            uniCallbackData(type, HDLUdpConnect.getInstance().isBindSuccess(), callback);
-                        }
-                    }
-                    break;
-                    //涓撻棬鏀堕泦uni鏃ュ織
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNI_LOG: {
-                        String customizeContentFormat = getKeyValue("customizeContentFormat", getKeyValue("data", data));
-                        HdlLogLogic.print("uni--->log--->" + customizeContentFormat, true);
-
-                    }
-                    break;
-                    //uni涓婚〉鐐瑰嚮杩斿洖閫氱煡
-                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK: {
-                        //EventBus浜嬩欢鍒嗗彂
-                        BaseEventBus baseEventBus = new BaseEventBus();
-                        baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL);
-                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK);
-                        EventBus.getDefault().post(baseEventBus);
-
-                    }
-                    break;
-                }
             } else if (HDLUniMP.UNI_EVENT_REPLY_MINE_MODEL.equals(topic)) {
                 //鎴戠殑妯″潡
                 switch (type) {
@@ -464,6 +432,54 @@
                     break;
 
 
+                }
+            } else if (HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL.equals(topic)) {
+                //鍏跺畠妯″潡
+                switch (type) {
+                    //鑾峰彇娓╁害鍗曚綅
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNIT: {
+                        uniCallbackData(mode_type, UserConfigManage.getInstance().getTemperature_unit(), callback);
+                    }
+                    break;
+                    //鑾峰彇褰撳墠app璇█
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APP_LANGUAGE: {
+                        uniCallbackData(mode_type, UserConfigManage.getInstance().getCurrentAppLanguage(), callback);
+                    }
+                    break;
+                    //鑾峰彇浣嶇疆淇℃伅
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_LOCATION_INFO: {
+                        this.uniGetLocationInfo(mode_type, data, callback);
+                    }
+                    break;
+                    //搴旂敤鍐茬獊
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APPLICATION_CONFLICT: {
+                        if (callback != null) {
+                            uniCallbackData(type, HDLUdpConnect.getInstance().isBindSuccess(), callback);
+                        }
+                    }
+                    break;
+                    //涓撻棬鏀堕泦uni鏃ュ織
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNI_LOG: {
+                        String customizeContentFormat = getKeyValue("customizeContentFormat", getKeyValue("data", data));
+                        HdlLogLogic.print("uni--->log--->" + customizeContentFormat, true);
+
+                    }
+                    break;
+                    //uni涓婚〉鐐瑰嚮杩斿洖閫氱煡
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK: {
+                        //EventBus浜嬩欢鍒嗗彂
+                        BaseEventBus baseEventBus = new BaseEventBus();
+                        baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL);
+                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK);
+                        EventBus.getDefault().post(baseEventBus);
+
+                    }
+                    break;
+                    //uni閫氱煡鍘熺敓閫�鍑虹櫥褰�
+                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_LOGOUT: {
+                        HDLLinkPMUser.getInstance().logout(0);
+                    }
+                    break;
                 }
             }
 
@@ -640,10 +656,9 @@
                     String[] ary = path.split("/");
                     String[] driverInfoAry = ary[ary.length - 1].split("_");
                     DeviceFirmwareBean deviceFirmwareBean = new DeviceFirmwareBean();
-                    deviceFirmwareBean.setOid(driverInfoAry[0]);
-                    deviceFirmwareBean.setImageId(driverInfoAry[1]);
-                    deviceFirmwareBean.setVersion(driverInfoAry[2].replace(".zip", ""));
-                    deviceFirmwareBean.setLocalUrl(HdlFileLogic.getInstance().getFirmwarePathFileName(deviceFirmwareBean.getOid(), osImageId, deviceFirmwareBean.getVersion()));
+                    deviceFirmwareBean.setImageId(driverInfoAry[0]);
+                    deviceFirmwareBean.setVersion(driverInfoAry[1].replace(".zip", ""));
+                    deviceFirmwareBean.setLocalUrl(HdlFileLogic.getInstance().getFirmwarePathFileName(osImageId, deviceFirmwareBean.getVersion()));
                     list.add(deviceFirmwareBean);
                 }
             }
@@ -671,7 +686,7 @@
                 HdlThreadLogic.runSubThread(new Runnable() {
                     @Override
                     public void run() {
-                        String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version);
+                        String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(imageId, version);
                         //涓嶅湪瀛愮嚎绋嬭娴佷細鍗℃涓荤嚎绋�
                         boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(firmwarePathFileName, responseBody, md5, HdlOtaLogic.firmware_type);
                         if (isBoolean) {
@@ -681,9 +696,9 @@
                         }
 
                         if (isBoolean) {
-                            uniCallbackData(type, null, 0, "鍐欏叆鏂伴┍鍔ㄦ枃浠跺埌鍐呭瓨鎴愬姛", callback);
+                            uniCallbackData(type, null, 0, HDLApp.getInstance().getString(R.string.writing_firmware_upgrade_memory_succeeded), callback);
                         } else {
-                            uniCallbackData(type, null, -2, "涓嬭浇鍥轰欢鍗囩骇鏂囦欢澶辫触", callback);
+                            uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.download_firmware_upgrade_file_failed), callback);
                         }
                     }
                 });
@@ -725,10 +740,10 @@
                         @Override
                         public void onServiceConnected(ComponentName name, IBinder service) {
                             //鏈湴鍗囩骇鍥轰欢鏂囦欢璺緞
-                            String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version);
+                            String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(imageId, version);
                             byte[] data = HdlFileLogic.getInstance().readFileByte(firmwarePathFileName);
                             if (data == null || data.length == 0) {
-                                uniCallbackData(type, null, -2, "鏈湴鎵句笉鍒板崌绾у浐浠舵枃浠�,璇蜂笅杞藉ソ鍥轰欢鏂囦欢,鍐嶉噸鏂板崌绾�.", callback);
+                                uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.found_locally_firmware_upgrade_file), callback);
                                 return;
                             }
                             String md5 = HDLMD5Utils.encodeMD5(data);//缃戝叧鍥轰欢闇�瑕�
@@ -743,14 +758,14 @@
 
                                 @Override
                                 public void onError(HDLLinkException e) {
-                                    uniCallbackData(type, null, -2, "閫氱煡缁欑綉鍏冲崌绾у浐浠舵枃浠跺湴鍧�澶辫触,鏃犳硶鍗囩骇,", callback);
+                                    uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.notify_gateway_firmware_upgrade_failed), callback);
                                 }
                             });
                         }
 
                         @Override
                         public void onServiceDisconnected(ComponentName name) {
-                            uniCallbackData(type, null, -2, "鏈湴鏈嶅姟鏈夊紓甯稿け璐�,鏃犳硶鍗囩骇,", callback);
+                            uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.local_service_fails), callback);
                         }
                     });
                 } else {
@@ -934,9 +949,9 @@
                             HdlFileLogic.getInstance().deleteFile(drivePathFileName);//涓嬭浇澶辫触锛屽垹闄や笉瀹屾暣椹卞姩鏂囦欢;
                         }
                         if (isBoolean) {
-                            uniCallbackData(type, null, 0, "鍐欏叆鏂伴┍鍔ㄦ枃浠跺埌鍐呭瓨鎴愬姛", callback);
+                            uniCallbackData(type, null, 0, HDLApp.getInstance().getString(R.string.writing_drive_memory_succeeded), callback);
                         } else {
-                            uniCallbackData(type, null, -2, "涓嬭浇椹卞姩鍗囩骇鏂囦欢澶辫触", callback);
+                            uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.download_drive_file_failed), callback);
                         }
                     }
                 });
@@ -980,7 +995,7 @@
                             String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version);
                             byte[] data = HdlFileLogic.getInstance().readFileByte(drivePathFileName);
                             if (data == null || data.length == 0) {
-                                uniCallbackData(type, null, -2, "鏈湴鎵句笉鍒板崌绾ч┍鍔ㄦ枃浠�,璇蜂笅杞藉ソ椹卞姩鏂囦欢,鍐嶉噸鏂板崌绾�.", callback);
+                                uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.found_locally_drive_file), callback);
                                 return;
                             }
                             String md5 = HDLMD5Utils.encodeMD5(data);//缃戝叧椹卞姩闇�瑕�
@@ -995,14 +1010,14 @@
 
                                 @Override
                                 public void onError(HDLLinkException e) {
-                                    uniCallbackData(type, null, -2, "閫氱煡缁欑綉鍏冲崌绾ч┍鍔ㄦ枃浠跺湴鍧�澶辫触,鏃犳硶鍗囩骇,", callback);
+                                    uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.notify_gateway_drive_failed), callback);
                                 }
                             });
                         }
 
                         @Override
                         public void onServiceDisconnected(ComponentName name) {
-                            uniCallbackData(type, null, -2, "鏈湴鏈嶅姟鏈夊紓甯稿け璐�,鏃犳硶鍗囩骇,", callback);
+                            uniCallbackData(type, null, -2, HDLApp.getInstance().getString(R.string.local_service_fails), callback);
                         }
                     });
                 } else {
@@ -1089,7 +1104,7 @@
      */
     private void uniCancelDownloadingUpgradeFile(String type, Object data, DCUniMPJSCallback callback) {
         HdlOtaLogic.getInstance().stopDownloadUpgradeFile();
-        uniCallbackData(type, null, 0, "鍙栨秷涓嬭浇鎴愬姛.", callback);
+        uniCallbackData(type, null, 0, HDLApp.getInstance().getString(R.string.cancel_download_successfully), callback);
     }
 
     /**
@@ -1251,9 +1266,9 @@
      */
     private void uniGetMessageNumber(String type, Object data, DCUniMPJSCallback callback) {
         String homeId = getKeyValue("homeId", getKeyValue("data", data));
-        HdlMessageLogic.getInstance().getMessageUntreatedCount(homeId, new CloudCallBeak<String>() {
+        HdlMessageLogic.getInstance().getMessageUntreatedCount(homeId, new CloudCallBeak<UnCountBean>() {
             @Override
-            public void onSuccess(String data) {
+            public void onSuccess(UnCountBean data) {
                 uniCallbackData(type, data, callback);
             }
 
@@ -1630,6 +1645,84 @@
 
 
     /**
+     * 鍙戦�佽处鍙峰拰瀵嗙爜缁欓�嗗彉鍣�
+     *
+     * @param ssid     璐﹀彿
+     * @param password 瀵嗙爜
+     * @param callback uni鍥炶皟
+     */
+    public void startTcpThreadSendAccountAndPassword(String type, String ssid, String password, DCUniMPJSCallback callback) {
+        new Thread(
+                () -> {
+                    Socket socket = null;
+                    OutputStream outputStreamTcp = null;
+                    InputStream inputStreamTcp = null;
+                    try {
+                        socket = new Socket();
+                        socket.setTcpNoDelay(true);
+                        socket.setSoTimeout(10 * 1000);//10绉掕秴鏃�
+                        socket.connect(new InetSocketAddress("192.168.8.1", 8586));
+                        socket.setKeepAlive(true);
+//                        logMessage("tcp杩炴帴鐘舵�佹垚鍔�", 0, "");
+                        outputStreamTcp = socket.getOutputStream();
+                        JsonObject jsonObject = new JsonObject();
+                        jsonObject.addProperty("id", "102030");
+                        jsonObject.addProperty("ssid", ssid);
+                        jsonObject.addProperty("password", password);
+                        jsonObject.addProperty("server_addr", HDLLinkPMUser.getInstance().getHomeRegionUrl());
+                        jsonObject.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
+                        String sendData = "Topic:/user/id/custom/wifi/set\r\n";
+                        sendData += "Length:" + jsonObject.toString().getBytes().length + "\r\n\r\n";
+                        sendData += jsonObject.toString();
+                        outputStreamTcp.write(sendData.getBytes());
+                        outputStreamTcp.flush();
+                        Thread.sleep(200);
+                        inputStreamTcp = socket.getInputStream();
+                        byte[] bytes = new byte[1204 * 2];
+                        int len = inputStreamTcp.read(bytes);
+                        if (len != -1) {
+                            String str = new String(bytes, 0, bytes.length);
+                            String[] strings = str.split("\r\n\r\n");
+                            String[] topicAndLength = strings[0].split("\r\n");
+                            String topic = topicAndLength[0];
+                            JSONObject json = new JSONObject(strings[1]);
+//                            logMessage("tcp鍥炲鏁版嵁", 0, strings[0] + "\r\n" + json.toString());
+                            if (topic.endsWith("wifi/set_reply") || topic.endsWith("wifi/result/notify")) {
+                                if (callback != null) {
+                                    uniCallbackData(type, json, callback);
+                                }
+                                if (json.has("mac")) { //鍥炲鎴愬姛鍐嶆嬁璁惧mac
+//                                    this.device_mac = json.getString("mac");
+//                                    this.isSucceedTcp = true;//琛ㄧず鎴愬姛
+                                }
+                            }
+                        }
+
+                    } catch (Exception ignored) {
+                    } finally {
+                        try {
+                            if (outputStreamTcp != null) {
+                                outputStreamTcp.close();
+                                outputStreamTcp = null;
+                            }
+                            if (inputStreamTcp != null) {
+                                inputStreamTcp.close();
+                                inputStreamTcp = null;
+                            }
+                            if (socket != null) {
+                                socket.close();
+                                socket = null;
+                            }
+
+                        } catch (Exception ignored) {
+                        }
+                    }
+                }
+        ).start();
+    }
+
+
+    /**
      * 鍙戦�乵odbus鍗忚鏁版嵁
      * 閫忎紶鍗忚
      * 涓嬪彂涓婚锛�/user/${gw_id}/custom/native/${driver}/down;
@@ -1732,7 +1825,7 @@
         HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
         try {
             uniCallBackBaseBean.setCode(code);
-            uniCallBackBaseBean.setMes(msg);
+            uniCallBackBaseBean.setMsg(msg);
             uniCallBackBaseBean.setData(obj);
             if (callback != null) {
                 callback.invoke(getJSONObject(uniCallBackBaseBean));

--
Gitblit v1.8.0