mac
2024-05-08 b75795836a851a13e54cc4efb597262fb448217c
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;
@@ -54,7 +52,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 +69,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 +283,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:无线wifi,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 +386,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 +431,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 +655,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 +685,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 +695,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 +739,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 +757,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 +948,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 +994,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 +1009,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 +1103,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);
    }
    /**
@@ -1409,7 +1423,7 @@
//        com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray(data);
        try {
            String devices = getKeyValue("devices", getKeyValue("data", data));
            String homeId = UserConfigManage.getInstance().getHomeId(); //getKeyValue("homeId", getKeyValue("data", data));
            String homeId = UserConfigManage.getInstance().getHomeId(); //绑定成功之后通知
            Gson gson = new Gson();
            Type typeOfT = new TypeToken<List<GatewayBean>>() {
            }.getType();
@@ -1630,6 +1644,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();
    }
    /**
     * 发送modbus协议数据
     * 透传协议
     * 下发主题:/user/${gw_id}/custom/native/${driver}/down;
@@ -1732,15 +1824,15 @@
        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));
//                callback.invoke(uniCallBackBaseBean);
                HdlLogLogic.print("uni--->接收--->" + type + "--->" + new Gson().toJson(uniCallBackBaseBean), true);
                HdlLogLogic.print("uni--->回复--->" + type + "--->" + new Gson().toJson(uniCallBackBaseBean), true);
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni--->接收--->" + type + "--->" + e.getMessage(), true);
            HdlLogLogic.print("uni--->回复--->" + type + "--->" + e.getMessage(), true);
        }