From 5e080d063f213f2b2c2ed34e1d54b9fd7df1c6b0 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期四, 23 十一月 2023 19:28:47 +0800
Subject: [PATCH] 2023年11月23日19:28:37

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java |  313 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 303 insertions(+), 10 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 ff4307a..36f0772 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -1,6 +1,11 @@
 package com.hdl.photovoltaic.other;
 
 
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -10,16 +15,23 @@
 import com.hdl.linkpm.sdk.ota.bean.CloudDeviceFirmwaresBean;
 import com.hdl.linkpm.sdk.ota.bean.CloudGatewayDriversBean;
 import com.hdl.linkpm.sdk.ota.bean.DeviceFirmwareBean;
+import com.hdl.linkpm.sdk.ota.bean.DownloadUrlBean;
 import com.hdl.linkpm.sdk.ota.bean.GatewayDriverBean;
+import com.hdl.linkpm.sdk.utils.HDLMD5Utils;
 import com.hdl.photovoltaic.HDLApp;
+import com.hdl.photovoltaic.R;
 import com.hdl.photovoltaic.bean.ModBusBean;
 import com.hdl.photovoltaic.config.UserConfigManage;
+import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer;
+import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService;
 import com.hdl.photovoltaic.listener.CloudCallBeak;
 import com.hdl.photovoltaic.listener.LinkCallBack;
+import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
 import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
 import com.hdl.photovoltaic.ui.bean.OidBean;
 import com.hdl.photovoltaic.uni.HDLUniMP;
 import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
+import com.hdl.photovoltaic.utils.AppManagerUtils;
 import com.hdl.photovoltaic.utils.WifiUtils;
 import com.hdl.sdk.link.common.exception.HDLLinkCode;
 import com.hdl.sdk.link.common.exception.HDLLinkException;
@@ -34,10 +46,14 @@
 import org.json.JSONObject;
 
 
+import java.io.File;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 
 import io.dcloud.feature.unimp.DCUniMPJSCallback;
+import okhttp3.ResponseBody;
 
 /**
  * uni灏忕▼搴忕殑閫昏緫
@@ -61,6 +77,7 @@
         }
         return sHdlUniLogic;
     }
+
 
     //region ******銆愬師鐢熴�戝拰銆愬皬绋嬪簭銆戦�氳鐨�3涓柟娉曞悕 onOtherUniMPEventReceive();openUniMP();sendUni();******
 
@@ -210,6 +227,16 @@
                         this.uniGetNewDeviceFirmwares(data, callback);
                     }
                     break;
+                    //璁惧鏈湴鍥轰欢鍒楄〃
+                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LOCAL: {
+                        this.uniGetLocalFirmwares(data, callback);
+                    }
+                    break;
+                    //璁惧鍥轰欢涓嬭浇
+                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_DOWNLOAD: {
+                        this.uniDeviceFirmwareDownload(data, callback);
+                    }
+                    break;
                     //璁惧鍥轰欢鍗囩骇
                     case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_UPGRADE: {
                         this.uniUpgradeDeviceFirmware(data, callback);
@@ -225,9 +252,24 @@
                         this.uniGetNewGatewayDrivers(data, callback);
                     }
                     break;
+                    //璁惧鏈湴椹卞姩鍒楄〃
+                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LOCAL: {
+                        this.uniGetLocalDrivers(data, callback);
+                    }
+                    break;
+                    //璁惧椹卞姩涓嬭浇
+                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_DOWNLOAD: {
+                        this.uniGatewayDriverDownload(data, callback);
+                    }
+                    break;
                     //璁惧椹卞姩鍗囩骇
                     case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_UPGRADE: {
                         this.uniUpgradeGatewayDriver(data, callback);
+                    }
+                    break;
+                    //璁惧鍙栨秷涓嬭浇鍗囩骇鏂囦欢
+                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_CANCEL_DOWNLOAD: {
+                        this.uniCancelDownloadingUpgradeFile(data, callback);
                     }
                     break;
 
@@ -258,7 +300,7 @@
      * 娉ㄦ剰锛氶渶瑕佹彁鍓嶅皬绋嬪簭鍦ㄨ繍琛屾墠鍙垚鍔�
      *
      * @param topic            涓婚澶х被(濡�:UNI_EVENT_REPLY_HOME_MODEL)
-     * @param callBackBaseBean 鈥斺��
+     * @param callBackBaseBean 鑷畾涔夊疄浣撶被
      */
     public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
         try {
@@ -338,14 +380,43 @@
     }
 
     /**
-     * 鍚戜簯绔彂璧枫�愯澶囧浐浠躲�戝崌绾TA鎸囦护
+     * 鑾峰彇銆愯澶囨湰鍦板浐浠躲�戝垪琛�
      */
-    private void uniUpgradeDeviceFirmware(Object data, DCUniMPJSCallback callback) {
+    private void uniGetLocalFirmwares(Object data, DCUniMPJSCallback callback) {
+        try {
+            String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//纭欢鍨嬪彿
+            String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//绯荤粺闀滃儚id
+            String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//绯荤粺闀滃儚id
+            List<DeviceFirmwareBean> list = new ArrayList<>();
+            String driverFileList = HdlFileLogic.getInstance().getFirmwareRootPath();
+            File file = new File(driverFileList);
+            if (file.list() != null) {
+                for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) {
+                    String path = Objects.requireNonNull(file.list())[i];
+                    String[] ary = path.split("/");
+                    String[] driverInfoAry = ary[ary.length - 1].split("_");
+                    DeviceFirmwareBean deviceFirmwareBean = new DeviceFirmwareBean();
+                    deviceFirmwareBean.setVersion(driverInfoAry[0]);
+                    deviceFirmwareBean.setImageId(driverInfoAry[1].replace(".zip", ""));
+                    list.add(deviceFirmwareBean);
+                }
+            }
+            if (callback != null) {
+                this.uniCallbackData(list, callback);
+            }
+        } catch (Exception ignored) {
+        }
+    }
+
+    /**
+     * 鍚戜簯绔彂璧枫�愯澶囧浐浠躲�戜笅杞芥寚浠�
+     */
+    private void uniDeviceFirmwareDownload(Object data, DCUniMPJSCallback callback) {
         String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//璁惧id
         String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//鍥轰欢鐗堟湰id
-        HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak<Boolean>() {
+        HdlOtaLogic.getInstance().getDeviceFirmwareDownloadUrl(firmwareVersionId, new CloudCallBeak<DownloadUrlBean>() {
             @Override
-            public void onSuccess(Boolean obj) {
+            public void onSuccess(DownloadUrlBean obj) {
                 uniCallbackData(obj, callback);
             }
 
@@ -354,6 +425,63 @@
                 uniCallbackData(null, e.getCode(), e.getMsg(), callback);
             }
         });
+    }
+
+    /**
+     * 鍚戜簯绔彂璧枫�愯澶囧浐浠躲�戝崌绾TA鎸囦护
+     */
+    private void uniUpgradeDeviceFirmware(Object data, DCUniMPJSCallback callback) {
+
+        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//缃戝叧璁惧mac锛堟煡鎵緎ocket锛�
+        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//璁惧id
+        String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//鍥轰欢鐗堟湰id
+
+
+        //鍗囩骇鏈湴浼樺厛->浜戠鍗囩骇
+        boolean isLocalConnect = HdlDeviceLogic.getInstance().isLocalConnect(deviceMac);
+        if (isLocalConnect) {
+            //鏈湴
+            //1,寤虹珛鏈湴鏈嶅姟锛�
+            // 2锛屽憡璇夌綉鍏虫墜鏈篿p鍜岀鍙o紱
+
+        } else {
+            //杩滅▼鍗囩骇闇�瑕佹娴嬪湪閫嗗彉鍣ㄦ湁娌℃湁杩炴帴涓婁簯
+            HdlDeviceLogic.getInstance().checkInverterConnectedCloud(deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
+                @Override
+                public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
+                    //1:寰呮満,2:杩炴帴涓�,3:鏁呴殰,4:杩愯,5:绂荤嚎,6:閫嗗彉鍣ㄨ繛涓嶄笂浜�(鑷畾涔�)
+                    if (cloudInverterDeviceBean == null) {
+                        uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
+                        return;
+                    }
+                    if (cloudInverterDeviceBean.getDeviceStatus() != 4) {
+                        uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), getDeviceStatusString(cloudInverterDeviceBean.getDeviceStatus()), callback);
+                        return;
+                    }
+                    //4:杩愯
+                    HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak<Boolean>() {
+                        @Override
+                        public void onSuccess(Boolean obj) {
+                            uniCallbackData(obj, callback);
+                        }
+
+                        @Override
+                        public void onFailure(HDLException e) {
+                            uniCallbackData(null, e.getCode(), e.getMsg(), callback);
+                        }
+                    });
+
+                }
+
+                @Override
+                public void onFailure(HDLException e) {
+                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
+                }
+            });
+
+        }
+
+
     }
 
     /**
@@ -396,25 +524,190 @@
     }
 
     /**
-     * 鍚戜簯绔彂璧枫�愯澶囬┍鍔ㄣ�戝崌绾TA鎸囦护
+     * 鑾峰彇銆愯澶囨湰鍦伴┍鍔ㄣ�戝垪琛�
      */
-    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
+    private void uniGetLocalDrivers(Object data, DCUniMPJSCallback callback) {
+        try {
+            String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//椹卞姩缂栧彿鎴栭┍鍔ㄥ悕绉�
+            String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//椹卞姩绫诲瀷id
+            String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//椹卞姩绫诲瀷id
+            List<GatewayDriverBean> list = new ArrayList<>();
+            String driverFileList = HdlFileLogic.getInstance().getDriveRootPath();
+            File file = new File(driverFileList);
+            if (file.list() != null) {
+                for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) {
+                    String fileName = Objects.requireNonNull(file.list())[i];
+                    if (!fileName.contains(driveCode)) {
+                        continue;
+                    }
+                    String[] driverInfoAry = fileName.split("_");
+                    GatewayDriverBean gatewayDriverBean = new GatewayDriverBean();
+                    gatewayDriverBean.setDriverCode(driverInfoAry[0]);
+                    gatewayDriverBean.setVersion(driverInfoAry[1].replace(".zip", ""));
+                    gatewayDriverBean.setLocalUrl(HdlFileLogic.getInstance().getDrivePathFileName(gatewayDriverBean.getDriverCode(), gatewayDriverBean.getVersion()));
+                    list.add(gatewayDriverBean);
+                }
+            }
+            if (callback != null) {
+                this.uniCallbackData(list, callback);
+            }
+        } catch (Exception exception) {
+            this.uniCallbackData(new ArrayList<>(), callback);
+        }
+
+    }
+
+    /**
+     * 鍚戜簯绔彂璧枫�愯澶囬┍鍔ㄤ笅杞姐�戞寚浠�
+     */
+    private void uniGatewayDriverDownload(Object data, DCUniMPJSCallback callback) {
         String deviceOid = getKeyValue("oid", getKeyValue("data", data));//缃戝叧璁惧oid
+        String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//椹卞姩缂栫爜
+        String imageId = getKeyValue("imageId", getKeyValue("data", data));//椹卞姩闀滃儚id
+        String version = getKeyValue("version", getKeyValue("data", data));//椹卞姩鐗堟湰
         String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//椹卞姩鐗堟湰id
-        HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak<Boolean>() {
+        String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//鍌ㄥ瓨鍦ㄦ湰鍦伴┍鍔ㄦ枃浠惰矾寰�
+        String url = getKeyValue("url", getKeyValue("data", data));//浜戠涓婃枃浠跺湴鍧�
+        String md5 = getKeyValue("md5", getKeyValue("data", data));//浜戠涓婃枃浠禡D5
+        HdlOtaLogic.getInstance().getDeviceDriverDownloadFile(url, new CloudCallBeak<ResponseBody>() {
             @Override
-            public void onSuccess(Boolean obj) {
-                uniCallbackData(obj, callback);
+            public void onSuccess(ResponseBody responseBody) {
+                String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version);
+                boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(drivePathFileName, responseBody, md5, HdlOtaLogic.driver_type);
+                if (isBoolean) {
+                    HdlFileLogic.getInstance().deleteFile(localUrl);//涓嬭浇鎴愬姛,鍒犻櫎鏃ф枃浠�;
+                } else {
+                    HdlFileLogic.getInstance().deleteFile(drivePathFileName);//涓嬭浇澶辫触鍒犻櫎涓嶅畬鎴愭暟鎹�;
+                }
+                HdlLogLogic.print("鍐欏叆鏂伴┍鍔ㄦ枃浠跺埌鍐呭瓨鎴愬姛.", false);
             }
 
             @Override
             public void onFailure(HDLException e) {
+                HdlLogLogic.print("涓嬭浇椹卞姩鏂囦欢鍒板唴瀛樺け璐�.", false);
                 uniCallbackData(null, e.getCode(), e.getMsg(), callback);
             }
         });
     }
 
     /**
+     * 鍚戜簯绔彂璧枫�愯澶囬┍鍔ㄣ�戝崌绾TA鎸囦护
+     */
+    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
+
+        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//缃戝叧璁惧mac锛堟煡鎵緎ocket锛�
+        String deviceOid = getKeyValue("oid", getKeyValue("data", data));//缃戝叧璁惧oid
+        String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//椹卞姩鐗堟湰id
+        String module = getKeyValue("module", getKeyValue("data", data));//妯″潡淇℃伅
+        String version = getKeyValue("version", getKeyValue("data", data));//椹卞姩鐗堟湰鍙�
+        String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//椹卞姩缂栫爜
+        //鍗囩骇鏈湴浼樺厛->浜戠鍗囩骇
+        boolean isLocalConnect = HdlDeviceLogic.getInstance().isLocalConnect(deviceMac);
+        if (isLocalConnect) {
+            //鏈湴
+            //1,寤虹珛鏈湴鏈嶅姟锛�
+            // 2锛屽憡璇夌綉鍏虫墜鏈篿p鍜岀鍙o紱
+            //瀹炰緥鍖� 鑾峰彇ip 鍦板潃
+            HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
+                @Override
+                public void onServiceConnected(ComponentName name, IBinder service) {
+                    //鏈湴鍗囩骇椹卞姩鏂囦欢璺緞
+                    String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version);
+                    String data = HdlFileLogic.getInstance().readFile(drivePathFileName);
+                    String md5 = HDLMD5Utils.encodeMD5(data);//缃戝叧椹卞姩闇�瑕�
+                    //鍗囩骇椹卞姩鏂囦欢鍦板潃
+                    String upgradeFileLocalPathUrl = AppManagerUtils.getAppManager().getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + drivePathFileName;
+                    //閫氱煡缁欑綉鍏冲崌绾ч┍鍔ㄦ枃浠跺湴鍧�绛変俊鎭�
+                    HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, deviceOid, module, version, data.length() + "", upgradeFileLocalPathUrl, md5, new LinkCallBack<String>() {
+                        @Override
+                        public void onSuccess(String obj) {
+                            HdlLogLogic.print("閫氱煡缁欑綉鍏冲崌绾ч┍鍔ㄦ枃浠跺湴鍧�鎴愬姛.", false);
+                        }
+
+                        @Override
+                        public void onError(HDLLinkException e) {
+                            HdlLogLogic.print("閫氱煡缁欑綉鍏冲崌绾ч┍鍔ㄦ枃浠跺湴鍧�澶辫触,鏃犳硶鍗囩骇.", false);
+                            uniCallbackData(null, -2, "閫氱煡缁欑綉鍏冲崌绾ч┍鍔ㄦ枃浠跺湴鍧�澶辫触,鏃犳硶鍗囩骇,", callback);
+                        }
+                    });
+                }
+
+                @Override
+                public void onServiceDisconnected(ComponentName name) {
+                    uniCallbackData(null, -2, "鏈湴鏈嶅姟鏈夊紓甯稿け璐�,鏃犳硶鍗囩骇,", callback);
+                }
+            });
+        } else {
+            //杩滅▼鍗囩骇闇�瑕併�愭娴嬨�戝湪閫嗗彉鍣ㄦ湁娌℃湁杩炴帴涓婁簯
+            HdlDeviceLogic.getInstance().checkInverterConnectedCloud(deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
+                @Override
+                public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
+                    //1锛氳繛鎺ヤ腑,2锛氭晠闅�,3锛氳繍琛�,4锛氱绾�,6:閫嗗彉鍣ㄨ繛涓嶄笂浜�(鑷畾涔�)
+                    if (cloudInverterDeviceBean == null) {
+                        uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback);
+                        return;
+                    }
+                    if (cloudInverterDeviceBean.getDeviceStatus() != 3) {
+                        uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
+                        return;
+                    }
+                    //4:杩愯
+                    HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak<Boolean>() {
+                        @Override
+                        public void onSuccess(Boolean obj) {
+                            uniCallbackData(obj, callback);
+                        }
+
+                        @Override
+                        public void onFailure(HDLException e) {
+                            uniCallbackData(null, e.getCode(), e.getMsg(), callback);
+                        }
+                    });
+
+                }
+
+                @Override
+                public void onFailure(HDLException e) {
+                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
+                }
+            });
+
+        }
+    }
+
+    /**
+     * 鍙栨秷涓嬭浇鍗囩骇鏂囦欢
+     */
+    private void uniCancelDownloadingUpgradeFile(Object data, DCUniMPJSCallback callback) {
+        HdlOtaLogic.getInstance().setStopDriversDownload();
+    }
+
+
+    /**
+     * @param deviceStatus 閫嗗彉鍣ㄨ繛鎺ヤ簯绔姸鎬佸�硷紙 1:寰呮満,2:杩炴帴涓�,3:鏁呴殰,4:杩愯,5:绂荤嚎锛�
+     * @return 杩斿洖鏂囨湰
+     */
+    public String getDeviceStatusString(int deviceStatus) {
+        switch (deviceStatus) {
+            case 1: {
+                return HDLApp.getInstance().getString(R.string.my_power_station_await);
+            }
+            case 2: {
+                return HDLApp.getInstance().getString(R.string.my_power_station_connecting);
+            }
+            case 3: {
+                return HDLApp.getInstance().getString(R.string.my_power_station_malfunction);
+            }
+            case 5: {
+                return HDLApp.getInstance().getString(R.string.my_power_station_off_line);
+            }
+            default:
+                return "";
+        }
+
+    }
+
+    /**
      * 閫嗗彉鍣ㄦ竻绌轰綇瀹卛d
      *
      * @param data     uni鏁版嵁

--
Gitblit v1.8.0