mac
2023-11-03 9e875b253959eff8f6af567308ad804fe69d0a62
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -2,27 +2,37 @@
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
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.listener.LinkCallBack;
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.WifiUtils;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.core.callback.ModbusCallBack;
import com.hdl.sdk.link.core.connect.HDLModBusConnect;
import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import io.dcloud.feature.unimp.DCUniMPJSCallback;
@@ -50,6 +60,8 @@
        return sHdlUniLogic;
    }
    //region ******【原生】和【小程序】通讯的3个方法名 onOtherUniMPEventReceive();openUniMP();sendUni();******
    /**
     * 原生接收到uni发来的数据
     *
@@ -64,108 +76,128 @@
            if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
                return;
            }
            org.json.JSONObject jsonObject = getJSONObject(data);
            String type_value = "";
            String oid="";
            if (jsonObject.has("type")) {
                type_value = jsonObject.getString("type");
            }
            if (jsonObject.has("oid")) {
                oid = jsonObject.getString("oid");
            }
            String type = getKeyValue("type", data);
            if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) {
                //住宅模块
                switch (type_value) {
                switch (type) {
                    //创建电站
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
                        //创建
                        HdlResidenceLogic.getInstance().getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
                            @Override
                            public void onSuccess(List<HouseIdBean> list) {
                                HdlResidenceLogic.getInstance().setHouseIdList(list);
                            }
                            @Override
                            public void onFailure(Exception exception) {
                            }
                        });
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION);
                        baseEventBus.setData(getKeyValue("data", data));
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                    //读取详情
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
                        //详情
                    //关闭详情页
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE: {
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                        baseEventBus.setData(getKeyValue("data", data));
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                }
                if (callback != null) {
                    uniCallbackData(null, "0", "", callback);
                    uniCallbackData(null, callback);
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
                //设备模块
                switch (type_value) {
                switch (type) {
                    //添加逆变器到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        //添加
                        this.uniAddInverterDeviceToCloud(data, callback);
                    }
                    break;
                    //删除云端逆变器
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_DEL: {
                        this.uniDelInverterDevice(data, callback);
                    }
                    break;
                    //局域网搜索逆变器列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_NET_LIST: {
                        this.uniSearchGateway(callback);
                    }
                    break;
                    //获取逆变器列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: {
                        //网关搜索
                        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
                            @Override
                            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                                HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                                callBackBaseBean.setTopic(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL);
                                callBackBaseBean.setType(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_LIST);
                                callBackBaseBean.setData(gatewayBeanList);
                                sendUni(callBackBaseBean);
                            }
                            @Override
                            public void onError(HDLLinkException e) {
                                //发送失败
                            }
                        });
                        this.uniGetCurrentHomeLocalAndCloudGatewayList(callback);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND:{
                        //发送modbus协议
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        sendModBus(data, callback);
                    }
                    break;
                    //获取oid列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_OID: {
                        this.uniGetInverterOidList(data, callback);
                    }
                    break;
                    //逆变器时间读取
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME: {
                        this.uniGetInverterTime(data, callback);
                    }
                    break;
                    //逆变器时间编辑
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME_EDIT: {
                        this.uniEditInverterTime(data, callback);
                    }
                    break;
                    //逆变器上传数据到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_UPLOADING_DATA: {
                        this.uniUploadDataToCloud(data, callback);
                    }
                    break;
                    //逆变器清空住宅id
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_CLEAR_DATA: {
                        this.uniClearInverterHomeId(data, callback);
                    }
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) {
                WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance());
                //wifi模块
                switch (type_value) {
                switch (type) {
                    //获取wifi列表
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
                        //获取wifi列表
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getScanResult(), "0", "", callback);
                            uniCallbackData(wifiUtils.getScanResult(), callback);
                        }
                    }
                    break;
                    //当前wifi详情
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
                        //当前wifi详情
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), "0", "", callback);
                            uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), callback);
                        }
                    }
                    break;
                    //wifi连接
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
                        //wifi连接
                    }
                    break;
                }
            }
            HdlLogLogic.print("uni===原生接收uni发来的数据", event + "\r\n" + data);
            HdlLogLogic.print("uni===原生接收uni发来的数据===" + event + "\r\n" + data, false);
        } catch (Exception e) {
            HdlLogLogic.print("uni===原生接收uni发来的数据", e.getMessage());
            HdlLogLogic.print("uni===原生接收uni发来的数据===" + e.getMessage(), false);
        }
    }
    /**
     * 原生打开uni指定页面
@@ -176,19 +208,22 @@
    public void openUniMP(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJsonDate(jsonObject, true);
        HdlLogLogic.print("uni===组装uni发送数据格式", json.toString());
        HdlLogLogic.print("uni===组装uni发送数据格式===" + json, false);
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
    }
    /**
     * 原生【主动】向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param topic            主题大类(如:UNI_EVENT_REPLY_HOME_MODEL)
     * @param callBackBaseBean ——
     */
    public void sendUni(HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
    public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
        try {
            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());
            HdlLogLogic.print("uni===原生主动向小程序发送通知事件===" + e.getMessage(), false);
        }
    }
@@ -198,6 +233,278 @@
    public void checkRemoveOtherUniMPEventCallBack() {
        HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
    }
    //endregion
    //region ******uni逻辑方法******
    /**
     * 逆变器清空住宅id
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniClearInverterHomeId(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().clearInverterHomeId(mac, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(null, callback);
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 逆变器【上传数据】到云端
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniUploadDataToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().uploadDataToCloud(mac, null);
    }
    /**
     * 编辑逆变器时间
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniEditInverterTime(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        String date = getKeyValue("date", getKeyValue("data", data));
        String time = getKeyValue("time", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().editGatewayTime(mac, date, time, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(true, callback);
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 获取逆变器时间
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniGetInverterTime(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().getGatewayTime(mac, new LinkCallBack<DeviceTimeBean>() {
            @Override
            public void onSuccess(DeviceTimeBean deviceTimeBean) {
                if (callback != null) {
                    uniCallbackData(deviceTimeBean, callback);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 获取oid列表
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniGetInverterOidList(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().getInverterOidList(mac, new LinkCallBack<List<OidBean>>() {
            @Override
            public void onSuccess(List<OidBean> list) {
                if (callback != null) {
                    uniCallbackData(list, callback);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 逆变器添加到云端上
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniAddInverterDeviceToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().setGatewayRemoteParam(mac, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                HdlDeviceLogic.getInstance().editGatewayParam(mac, new LinkCallBack<Boolean>() {
                    @Override
                    public void onSuccess(Boolean obj) {
                        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
                        if (gatewayBean == null) {
                            uniCallbackData(null, -100, "本地找不到网关", callback);
                            return;
                        }
                        HdlDeviceLogic.getInstance().addInverterDeviceToCloud(mac,
                                gatewayBean.getGatewayType(),
                                gatewayBean.getSid(),
                                gatewayBean.getOid(),
                                gatewayBean.getDevice_name(),
                                new CloudCallBeak<Boolean>() {
                                    @Override
                                    public void onSuccess(Boolean obj) {
                                        uniCallbackData(null, callback);
                                    }
                                    @Override
                                    public void onFailure(HDLException e) {
                                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                                    }
                                });
                    }
                    @Override
                    public void onError(HDLLinkException e) {
                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                    }
                });
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 删除云端上逆变器
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniDelInverterDevice(Object data, DCUniMPJSCallback callback) {
        String deviceId = getKeyValue("deviceId", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().delInverterDevice(deviceId, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(null, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 局域网搜索逆变器列表
     *
     * @param callback uni回调
     */
    private void uniSearchGateway(DCUniMPJSCallback callback) {
        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(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 获取逆变器列表
     *
     * @param callback uni回调
     */
    private void uniGetCurrentHomeLocalAndCloudGatewayList(DCUniMPJSCallback callback) {
        HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(new CloudCallBeak<List<GatewayBean>>() {
            @Override
            public void onSuccess(List<GatewayBean> list) {
                if (callback != null) {
                    uniCallbackData(list, callback);
                    //EventBus事件分发
                    BaseEventBus baseEventBus = new BaseEventBus();
                    baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST);
                    baseEventBus.setData(list);
                    EventBus.getDefault().post(baseEventBus);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (callback != null) {
                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                }
            }
        });
    }
    /**
     * 发送modbus协议数据
     *
     * @param data     modbus数据
     * @param callback 回调
     */
    void sendModBus(Object data, DCUniMPJSCallback callback) {
        String tempData = getKeyValue("data", data);
        if (TextUtils.isEmpty(tempData)) {
            HdlLogLogic.print("data内容为空", false);
            return;
        }
        ModBusBean modBusBean = new Gson().fromJson(tempData, ModBusBean.class);
//        ModBusBean modBusBean = new ModBusBean();
//        modBusBean.setOid("0101050219D44A00");
//        modBusBean.setData(new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01});
//        if (TextUtils.isEmpty(modBusBean.getOid())) {
//            modBusBean.setOid("0101050217BBC400");
//        }
        if (modBusBean.getMac() == null || modBusBean.getData() == null) {
            HdlLogLogic.print("内容为空===oid=" + modBusBean.getMac() + " data=" + Arrays.toString(modBusBean.getData()), false);
            return;
        }
        //发送modbus协议
        HDLModBusConnect.getInstance().Send(modBusBean.getMac(), modBusBean.getData(), new ModbusCallBack() {
            @Override
            public void onSuccess(int[] data) {
                Log.d("data", Arrays.toString(data));
                uniCallbackData(data, callback);
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    //endregion
    //region ******一般方法******
    /**
     * 组装uni发送数据格式
@@ -219,7 +526,7 @@
            uniCallBackBaseBean.setData(data);
            return getJSONObject(uniCallBackBaseBean);
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
            HdlLogLogic.print("uni===组装uni发送数据格式===" + e.getMessage(), false);
        }
        return new JSONObject();
    }
@@ -234,7 +541,7 @@
     * @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);
@@ -242,28 +549,18 @@
            uniCallBackBaseBean.setData(obj);
            if (callback != null) {
                callback.invoke(getJSONObject(uniCallBackBaseBean));
//                callback.invoke(uniCallBackBaseBean);
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
            HdlLogLogic.print("uni===组装uni发送数据格式===" + e.getMessage(), false);
        }
    }
    /**
     * 获取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);
    }
    /**
@@ -279,12 +576,39 @@
            if (TextUtils.isEmpty(obj.toString())) {
                return new JSONObject();
            }
            if (obj.toString().startsWith("{") && obj.toString().endsWith("}")) {
                return new JSONObject(obj.toString());
            }
            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
}