mac
2023-11-16 f10bd384ab2c5e459b9bf20766970615e429b4ec
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -5,12 +5,14 @@
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.linkpm.sdk.device.bean.DeviceOidInfoBean;
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.GatewayDriverBean;
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.listener.LinkCallBack;
@@ -21,11 +23,11 @@
import com.hdl.photovoltaic.utils.WifiUtils;
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;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
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;
@@ -190,10 +192,50 @@
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL.equals(event)) {
                //OTA升级模块
                switch (type) {
                    //向云端获取oid列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOUD_OID_LIST: {
                        this.uniGetCloudOidList(data, callback);
                    }
                    break;
                    //当前设备固件列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LIST: {
                        this.uniGetCurrentDeviceFirmwares(data, callback);
                    }
                    break;
                    //设备新固件列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_NEW_LIST: {
                        this.uniGetNewDeviceFirmwares(data, callback);
                    }
                    break;
                    //设备固件升级
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_UPGRADE: {
                        this.uniUpgradeDeviceFirmware(data, callback);
                    }
                    break;
                    //当前设备驱动列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LIST: {
                        this.uniGetCurrentGatewayDrivers(data, callback);
                    }
                    break;
                    //设备新驱动列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_NEW: {
                        this.uniGetNewGatewayDrivers(data, callback);
                    }
                    break;
                    //设备驱动升级
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_UPGRADE: {
                        this.uniUpgradeGatewayDriver(data, callback);
                    }
                    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);
        }
    }
@@ -206,10 +248,9 @@
     * @param jsonObject 附件数据(没有数据填null)
     */
    public void openUniMP(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJsonDate(jsonObject, true);
        HdlLogLogic.print("uni===组装uni发送数据格式", json.toString());
        JSONObject json = this.createdJSONObject(jsonObject, true);
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
        HdlLogLogic.print("uni===组装uni发送数据格式===" + json, false);
    }
    /**
@@ -223,7 +264,7 @@
        try {
            HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean));
        } catch (Exception e) {
            HdlLogLogic.print("uni===原生主动向小程序发送通知事件", e.getMessage());
            HdlLogLogic.print("uni===原生主动向小程序发送通知事件===" + e.getMessage(), false);
        }
    }
@@ -235,7 +276,143 @@
    }
    //endregion
    //region ******uni逻辑方法******
    //region ******uni接口方法******
    /**
     * 向云端获取逆变器oid列表
     * 前提条件:要上传逆变器oid列表给云端
     *
     * @param callback -
     */
    private void uniGetCloudOidList(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().getCloudOidList(new CloudCallBeak<List<DeviceOidInfoBean>>() {
            @Override
            public void onSuccess(List<DeviceOidInfoBean> obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端获取【当前设备固件】列表
     * 前提条件:设备自动上报oid信息给云端
     */
    private void uniGetCurrentDeviceFirmwares(Object data, DCUniMPJSCallback callback) {
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));
        HdlOtaLogic.getInstance().getCurrentDeviceFirmwares(deviceOidId, new CloudCallBeak<List<DeviceFirmwareBean>>() {
            @Override
            public void onSuccess(List<DeviceFirmwareBean> obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端获取【设备新固件】列表
     * 前提条件:要通过平台软件上传新固件
     */
    private void uniGetNewDeviceFirmwares(Object data, DCUniMPJSCallback callback) {
        String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//硬件型号
        String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//系统镜像id
        HdlOtaLogic.getInstance().getNewDeviceFirmwares(hardwareModel, osImageId, new CloudCallBeak<List<CloudDeviceFirmwaresBean>>() {
            @Override
            public void onSuccess(List<CloudDeviceFirmwaresBean> obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端发起【设备固件】升级OTA指令
     */
    private void uniUpgradeDeviceFirmware(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>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端获取【当前设备驱动】列表
     * 前提条件:设备自动上报oid信息给云端
     */
    private void uniGetCurrentGatewayDrivers(Object data, DCUniMPJSCallback callback) {
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));
        HdlOtaLogic.getInstance().getCurrentGatewayDrivers(deviceOid, new CloudCallBeak<List<GatewayDriverBean>>() {
            @Override
            public void onSuccess(List<GatewayDriverBean> obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端获取【设备新驱动】列表
     * 前提条件:要通过平台软件上传新驱动
     */
    private void uniGetNewGatewayDrivers(Object data, DCUniMPJSCallback callback) {
        String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//驱动编号或驱动名称
        String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//驱动类型id
        HdlOtaLogic.getInstance().getNewGatewayDrivers(driveCode, osImageId, new CloudCallBeak<CloudGatewayDriversBean>() {
            @Override
            public void onSuccess(CloudGatewayDriversBean obj) {
                uniCallbackData(obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端发起【设备驱动】升级OTA指令
     */
    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));//网关设备oid
        String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id
        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);
            }
        });
    }
    /**
     * 逆变器清空住宅id
@@ -253,7 +430,7 @@
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMessage(), callback);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
@@ -357,34 +534,29 @@
                            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);
                                    }
                        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.getMessage(), 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.getMessage(), callback);
                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                    }
                });
            }
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMessage(), callback);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
@@ -405,7 +577,7 @@
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMessage(), callback);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
@@ -426,7 +598,7 @@
            @Override
            public void onError(HDLLinkException e) {
                //发送失败
                uniCallbackData(null, e.getCode(), e.getMessage(), callback);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
@@ -454,7 +626,7 @@
            @Override
            public void onFailure(HDLException e) {
                if (callback != null) {
                    uniCallbackData(null, e.getCode(), e.getMessage(), callback);
                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                }
            }
        });
@@ -464,14 +636,22 @@
    /**
     * 发送modbus协议数据
     * 透传协议
     * 下发主题:/user/${gw_id}/custom/native/${driver}/down;
     * 逆变器回复主题:/user/${gw_id}/custom/native/${driver}/down_reply;
     * Modbus ECU协议:事件ID(2个byte)->协议[固定:0,0](2个byte)->长度(2byte)->标识符[oid的addresses值](4个byte)->功能码(1个byte)->负载数据(N个byte);
     * 长度(2个byte)=标识符(4个byte)+功能码(1个byte)+负载数据(N个byte);
     * 负载数据=寄存器地址(2个byte)+寄存器长度(2个byte)+内容长度(1个byte)+内容数据(N个byte)【注意:单个写入寄存器-->去掉<寄存器长度>和<内容长度>】;
     * 寄存器长度=(内容数据/2);
     * 例子:new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01};
     *
     * @param data     modbus数据
     * @param callback 回调
     */
    void sendModBus(Object data, DCUniMPJSCallback callback) {
    private void sendModBus(Object data, DCUniMPJSCallback callback) {
        String tempData = getKeyValue("data", data);
        if (TextUtils.isEmpty(tempData)) {
            HdlLogLogic.print("data内容为空");
            HdlLogLogic.print("data内容为空", false);
            return;
        }
        ModBusBean modBusBean = new Gson().fromJson(tempData, ModBusBean.class);
@@ -482,7 +662,7 @@
//            modBusBean.setOid("0101050217BBC400");
//        }
        if (modBusBean.getMac() == null || modBusBean.getData() == null) {
            HdlLogLogic.print("内容为空,oid=" + modBusBean.getMac() + " data=" + Arrays.toString(modBusBean.getData()));
            HdlLogLogic.print("内容为空===oid=" + modBusBean.getMac() + " data=" + Arrays.toString(modBusBean.getData()), false);
            return;
        }
@@ -509,11 +689,11 @@
    /**
     * 组装uni发送数据格式
     *
     * @param data                   -附件数据(没有数据填null)
     * @param data                   附加数据(没有数据填null)
     * @param isTokenAndRefreshToken (true=底层默认添加token和refreshToken;false=不加)
     * @return JSONObject
     * @return JSONObject            uni方法名里面参数需要的JSONObject对象
     */
    private JSONObject createdJsonDate(JSONObject data, boolean isTokenAndRefreshToken) {
    private JSONObject createdJSONObject(JSONObject data, boolean isTokenAndRefreshToken) {
        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
        try {
            if (data == null) {
@@ -526,7 +706,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();
    }
@@ -553,7 +733,7 @@
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
            HdlLogLogic.print("uni===组装uni发送数据格式===" + e.getMessage(), false);
        }
@@ -607,6 +787,7 @@
        } catch (Exception e) {
            return "";
        }
    }
    //endregion