mac
2023-12-05 b2d2e6dcf6ec38a2f24721f2bc925d634a6eb78d
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();******
@@ -134,6 +151,7 @@
                        this.uniGetCurrentHomeLocalAndCloudGatewayList(callback);
                    }
                    break;
                    //modBus协议专用
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        sendModBus(data, callback);
                    }
@@ -210,6 +228,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,12 +253,51 @@
                        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;
                    //打开服务
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_OPEN_SERVICE: {
                        this.uniOpenService(data, callback);
                    }
                    break;
                    //关闭服务
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOSE_SERVICE: {
                        this.uniCloseService(data, callback);
                    }
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL.equals(event)) {
                //其它模块
                switch (type) {
                    //获取温度单位
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNIT: {
                        uniCallbackData(UserConfigManage.getInstance().getTemperature_unit(), callback);
                    }
                    break;
                    //获取当前app语言
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APP_LANGUAGE: {
                        uniCallbackData(UserConfigManage.getInstance().getCurrentAppLanguage(), callback);
                    }
                    break;
                }
            }
            HdlLogLogic.print("uni===原生接收uni发来的数据===" + event + "\r\n" + data, false);
@@ -242,7 +309,7 @@
    /**
     * 原生打开uni指定页面
     * 原生【打开】uni指定页面
     *
     * @param path       打开路径
     * @param jsonObject 附件数据(没有数据填null)
@@ -258,7 +325,7 @@
     * 注意:需要提前小程序在运行才可成功
     *
     * @param topic            主题大类(如:UNI_EVENT_REPLY_HOME_MODEL)
     * @param callBackBaseBean ——
     * @param callBackBaseBean 自定义实体类
     */
    public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
        try {
@@ -303,7 +370,7 @@
     * 前提条件:设备自动上报oid信息给云端
     */
    private void uniGetCurrentDeviceFirmwares(Object data, DCUniMPJSCallback callback) {
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id
        HdlOtaLogic.getInstance().getCurrentDeviceFirmwares(deviceOidId, new CloudCallBeak<List<DeviceFirmwareBean>>() {
            @Override
            public void onSuccess(List<DeviceFirmwareBean> obj) {
@@ -338,22 +405,151 @@
    }
    /**
     * 向云端发起【设备固件】升级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>() {
    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.setOid(driverInfoAry[0]);
                    deviceFirmwareBean.setVersion(driverInfoAry[1].replace(".zip", ""));
                    deviceFirmwareBean.setLocalUrl(HdlFileLogic.getInstance().getFirmwarePathFileName(deviceFirmwareBean.getOid(), deviceFirmwareBean.getVersion()));
                    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 oid = getKeyValue("oid", getKeyValue("data", data));//固件的oid
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id
        String version = getKeyValue("version", getKeyValue("data", data));//固件版本
        String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地固件文件路径
        String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址
        String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5
        HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(url, new CloudCallBeak<ResponseBody>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(obj, callback);
            public void onSuccess(ResponseBody responseBody) {
                String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, version);
                boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(firmwarePathFileName, responseBody, md5, HdlOtaLogic.firmware_type);
                if (isBoolean) {
                    HdlFileLogic.getInstance().deleteFile(localUrl);//下载成功,删除旧固件文件;
                } else {
                    HdlFileLogic.getInstance().deleteFile(firmwarePathFileName);//下载失败,删除不完整固件文件;
                }
                HdlLogLogic.print("写入新固件文件到内存成功.", false);
            }
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("下载固件文件到内存失败.", false);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 向云端发起【设备固件】升级OTA指令
     */
    private void uniUpgradeDeviceFirmware(Object data, DCUniMPJSCallback callback) {
        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket)
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id
        String oid = getKeyValue("oid", getKeyValue("data", data));//升级固件的oid
        String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//固件版本id
        String version = getKeyValue("version", getKeyValue("data", data));//固件版本
        String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//云端上文件MD5
        //升级本地优先->云端升级
        boolean isLocalConnect = HdlDeviceLogic.getInstance().isLocalConnect(deviceMac);
        if (isLocalConnect) {
            //本地
            //1,建立本地服务;
            //2,告诉网关手机ip和端口;
            //实例化 获取ip 地址
            HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    //本地升级固件文件路径
                    String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, version);
                    String data = HdlFileLogic.getInstance().readFile(firmwarePathFileName);
                    String md5 = HDLMD5Utils.encodeMD5(data);//网关固件需要
                    //升级固件文件地址
                    String upgradeFileLocalPathUrl = AppManagerUtils.getAppManager().getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + firmwarePathFileName;
                    //通知给网关升级固件文件地址等信息
                    HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, oid, hardwareModel, 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().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 +592,210 @@
    }
    /**
     * 向云端发起【设备驱动】升级OTA指令
     * 获取【设备本地驱动】列表
     */
    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));//网关设备oid
    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));//云端上文件MD5
        HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(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);
            }
        });
    }
    /**
     * 向云端发起【设备驱动】升级OTA指令
     */
    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket)
        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,告诉网关手机ip和端口;
            //实例化 获取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();
    }
    /**
     * 开启服务
     */
    private void uniOpenService(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                HdlOtaLogic.getInstance().mServiceStart = false;
            }
        });
    }
    /**
     * 关闭服务
     */
    private void uniCloseService(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().unLocalService();
    }
    /**
     * @param deviceStatus 逆变器连接云端状态值( 1:连接中,2:故障,3:运行,4:离线)
     * @return 返回文本
     */
    public String getDeviceStatusString(int deviceStatus) {
        switch (deviceStatus) {
            case 1: {
                return HDLApp.getInstance().getString(R.string.my_power_station_connecting);
            }
            case 2: {
                return HDLApp.getInstance().getString(R.string.my_power_station_malfunction);
            }
            case 4: {
                return HDLApp.getInstance().getString(R.string.my_power_station_off_line);
            }
            default:
                return "";
        }
    }
    /**
     * 逆变器清空住宅id
     *
     * @param data     uni数据