mac
2024-05-15 eb8097cb8517726094115410777083d5f63701f0
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -34,6 +34,7 @@
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer;
import com.hdl.photovoltaic.internet.TcpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.internet.api.TopicApi;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.listener.LinkCallBack;
@@ -64,6 +65,7 @@
import com.hdl.sdk.link.core.connect.HDLModBusConnect;
import com.hdl.sdk.link.core.connect.HDLUdpConnect;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.huawei.hms.api.Api;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
@@ -90,6 +92,8 @@
 */
public class HdlUniLogic implements HDLUniMPSDKManager.IOnOtherUniMPEventCallBack {
    private static volatile HdlUniLogic sHdlUniLogic;
    public final static String separator="---";
    /**
     * 获取当前对象
@@ -127,8 +131,8 @@
            }
            String type = getKeyValue("type", data);//小类
            String logTag = getKeyValue("logTag", data);//标记uni指令
            String mode_type = "大类->" + topic + "--->" + "小类->" + type;//用来打印的
            HdlLogLogic.print(logTag + ":" + "uni--->发送--->" + mode_type + "\r\n" + data, true);
            String mode_type = "大类-" + topic + "---" + "小类-" + type;//用来打印的
            HdlLogLogic.print(logTag + ":" + "uni---发送---" + mode_type + "\r\n" + data, true);
            if (HDLUniMP.UNI_EVENT_REPLY_USER_MODEL.equals(topic)) {
                //用户模块
                switch (type) {
@@ -280,37 +284,57 @@
                WifiUtils wifiUtils = WifiUtils.getInstance();
                //wifi模块
                switch (type) {
                    //获取wifi列表
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
                    //获取手机wifi列表
                    case HDLUniMP.UNI_EVENT_REPLY_PHONE_WIFI_LIST: {
                        if (callback != null) {
                            uniCallbackData(mode_type, wifiUtils.getScanResult(), callback);
                        }
                    }
                    break;
                    //当前wifi详情
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_PHONE_INFO: {
                    //获取手机当前wifi详情
                    case HDLUniMP.UNI_EVENT_REPLY_PHONE_WIFI_INFO: {
                        if (callback != null) {
                            uniCallbackData(mode_type, wifiUtils.getCurrentConnectWifiInfo(), callback);
                        }
                    }
                    break;
                    //配置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: {
                    case HDLUniMP.UNI_EVENT_REPLY_PHONE_OPEN_WIFI_SETTINGS: {
                        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        HDLApp.getInstance().startActivity(intent);
                    }
                    //逆变器WiFi信息配置
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_WIFI_SET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_WIFI_EDIT, callback);
                    }
                    break;
                    //逆变器WiFi信息获取
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_WIFI_GET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_WIFI_GET, callback);
                    }
                    break;
                    //逆变器热点配置
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_HOTSPOT_SET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_HOTSPOT_EDIT, callback);
                    }
                    break;
                    //逆变器热点获取
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_HOTSPOT_GET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_HOTSPOT_GET, callback);
                    }
                    break;
                    //逆变器以太网配置
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ETHERNET_SET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_ETHERNET_EDIT, callback);
                    }
                    break;
                    //逆变器以太网获取
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ETHERNET_GET: {
                        uniGetAndSetNetwork(mode_type, data, TopicApi.DELETING_GATEWAY_ETHERNET_GET, callback);
                    }
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL.equals(topic)) {
                //OTA升级模块
@@ -467,7 +491,7 @@
                    //专门收集uni日志
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNI_LOG: {
                        String customizeContentFormat = getKeyValue("customizeContentFormat", getKeyValue("data", data));
                        HdlLogLogic.print("uni--->log--->" + customizeContentFormat, true);
                        HdlLogLogic.print("uni---log---" + customizeContentFormat, true);
                    }
                    break;
@@ -490,7 +514,7 @@
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni--->发送--->" + e.getMessage(), true);
            HdlLogLogic.print("uni---发送---" + e.getMessage(), true);
        }
    }
@@ -505,7 +529,7 @@
    public void openUniMP(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJSONObject(jsonObject, true);
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
        HdlLogLogic.print("uni--->组装uni发送数据格式--->" + json, false);
        HdlLogLogic.print("uni---组装uni发送数据格式---" + json, false);
    }
    /**
@@ -519,7 +543,7 @@
        try {
            HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean));
        } catch (Exception e) {
            HdlLogLogic.print("uni--->原生主动向小程序发送通知事件--->" + e.getMessage(), false);
            HdlLogLogic.print("uni---原生主动向小程序发送通知事件---" + e.getMessage(), false);
        }
    }
@@ -532,6 +556,41 @@
    //endregion
    //region ******uni接口方法******
    /**
     * 获取或者配置逆变器网络(包括逆变器热点,wifi,以太网)信息
     */
    private void uniGetAndSetNetwork(String type, Object data, String TopicApi, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        JsonObject jObject = null;
        String attribute_data = getKeyValue("attribute_data", getKeyValue("data", data));
        if (!TextUtils.isEmpty(attribute_data)) {
            try {
                jObject = new Gson().fromJson(attribute_data, JsonObject.class);
            } catch (Exception ignored) {
                jObject = new JsonObject();
            }
        }
        //解密负载数据(写密钥给网关一定是明文,因为那时网关还没有密钥)
        TcpClient.getInstance().sendDataToLinkGateway(mac, false, TopicApi, jObject, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (callback != null) {
                    uniCallbackData(type, msg, callback);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                if (callback != null) {
                    uniCallbackData(type, null, e.getCode(), e.getMsg(), callback);
                }
            }
        });
    }
    /**
     * 获取位置信息,包括经纬度
@@ -737,7 +796,7 @@
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
                    HdlLogLogic.print("本地升级--->", true);
                    HdlLogLogic.print("本地升级---", true);
                    //本地
                    //1,建立本地服务;
                    //2,告诉网关手机ip和端口;
@@ -775,7 +834,7 @@
                        }
                    });
                } else {
                    HdlLogLogic.print("在线升级--->", true);
                    HdlLogLogic.print("在线升级---", true);
                    //远程升级需要检测在逆变器有没有连接上云
                    HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                        @Override
@@ -814,7 +873,7 @@
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("在线升级--->", true);
                HdlLogLogic.print("在线升级---", true);
                //远程升级需要检测在逆变器有没有连接上云
                HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                    @Override
@@ -989,7 +1048,7 @@
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
                    HdlLogLogic.print("本地升级--->", true);
                    HdlLogLogic.print("本地升级---", true);
                    //本地
                    //1,建立本地服务;
                    // 2,告诉网关手机ip和端口;
@@ -1027,7 +1086,7 @@
                        }
                    });
                } else {
                    HdlLogLogic.print("在线升级--->", true);
                    HdlLogLogic.print("在线升级---", true);
                    //远程升级需要【检测】在逆变器有没有连接上云
                    HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                        @Override
@@ -1066,7 +1125,7 @@
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("在线升级--->", true);
                HdlLogLogic.print("在线升级---", true);
                //远程升级需要【检测】在逆变器有没有连接上云
                HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                    @Override
@@ -1768,7 +1827,7 @@
        String tempData = getKeyValue("data", data);
        if (TextUtils.isEmpty(tempData)) {
            uniCallbackData(type, null, -2, "data内容为空", callback);
            //HdlLogLogic.print("data内容为空--->", false);
            //HdlLogLogic.print("data内容为空---", false);
            return;
        }
        ModBusBean modBusBean = new Gson().fromJson(tempData, ModBusBean.class);
@@ -1780,24 +1839,24 @@
//        }
        if (modBusBean.getMac() == null || modBusBean.getData() == null) {
            uniCallbackData(type, null, -2, "发送失败,(mac或者oid)为空", callback);
            //HdlLogLogic.print("uni--->发送--->ModBus--->发送失败,(mac或者oid)为空." + Arrays.toString(modBusBean.getData()), true);
            //HdlLogLogic.print("uni---发送---ModBus---发送失败,(mac或者oid)为空." + Arrays.toString(modBusBean.getData()), true);
            return;
        }
        //HdlLogLogic.print("uni--->发送--->ModBus--->" + Arrays.toString(modBusBean.getData()), true);
        //HdlLogLogic.print("uni---发送---ModBus---" + Arrays.toString(modBusBean.getData()), true);
        //发送modbus协议
        HDLModBusConnect.getInstance().Send(modBusBean.getMac(), modBusBean.getData(), new ModbusCallBack() {
            @Override
            public void onSuccess(int[] data) {
                uniCallbackData(type, data, callback);
//                HdlLogLogic.print("uni--->接收--->ModBus--->" + Arrays.toString(data), true);
//                HdlLogLogic.print("uni---接收---ModBus---" + Arrays.toString(data), true);
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(type, null, e.getCode(), e.getMsg(), callback);
//                HdlLogLogic.print("uni--->接收--->ModBus--->" + e.getMsg() + "(" + e.getCode() + ")", true);
//                HdlLogLogic.print("uni---接收---ModBus---" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
    }
@@ -1833,7 +1892,7 @@
            uniCallBackBaseBean.setData(data);
            return getJSONObject(uniCallBackBaseBean);
        } catch (Exception e) {
            HdlLogLogic.print("uni--->组装发送给uni数据格式有异常--->" + e.getMessage(), false);
            HdlLogLogic.print("uni---组装发送给uni数据格式有异常---" + e.getMessage(), false);
        }
        return new JSONObject();
    }
@@ -1858,10 +1917,10 @@
            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);
        }