2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-05-18 661570d3cef48b7df121f649fe00743770a9f16d
fix
已修改2个文件
159 ■■■■■ 文件已修改
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java 111 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RemoteUpdateService.java 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java
@@ -2,12 +2,14 @@
import com.sandu.ximon.admin.dto.RemoteFileDto;
import com.sandu.ximon.admin.utils.HexUtils;
import com.sandu.ximon.admin.utils.StringUtil;
import org.apache.catalina.LifecycleState;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -17,45 +19,74 @@
 * @date 2022/5/5 14:26
 */
public class FileProcessingUtils {
    private static final int readSize = 512;
//    public static void main(String[] args) {
//        try {
//            RemoteFileDto remoteFileDto = read("C:\\Users\\Administrator\\Desktop\\test\\H01_v3.0.12.min.dhcp.bin");
//            System.out.println(remoteFileDto.getListSize());
//            System.out.println(remoteFileDto.getList().size());
//            System.out.println(Arrays.toString(remoteFileDto.getList().get(0)));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    public static RemoteFileDto read(String filePath) throws IOException {
        List<String> data = new ArrayList<>();
        int dataSize = 0;
        StringBuffer fileHexStr=new StringBuffer();
        File file = new File(filePath);
        InputStream in = new FileInputStream(file);
        // 一次性取多少个字节
        byte[] bytes = new byte[512];
        // 读取到的字节数组长度,为-1时表示没有数据
        int length = 0;
        // 循环取数据
        while ((length = in.read(bytes)) != -1) {
            String temp=HexUtils.encodeHexStr(Arrays.copyOf(bytes, length));
            data.add(temp);
            fileHexStr.append(temp);
            dataSize += length;
    public static RemoteFileDto read(String filePath) {
        //文件地址为空
        if (filePath == null) {
            return null;
        }
        List<String> data = new ArrayList<>();
        // 取数据长度
        int dataSize = 0;
        StringBuffer fileHexStr = new StringBuffer();
        try {
            // 创建URL
            URL url = new URL(filePath);
            // 试图连接并取得返回状态码
            URLConnection urlconn = url.openConnection();
            urlconn.connect();
            HttpURLConnection httpconn = (HttpURLConnection) urlconn;
            int HttpResult = httpconn.getResponseCode();
            if (HttpResult != HttpURLConnection.HTTP_OK) {
                System.out.print("无法连接到");
                return null;
            } else {
                InputStream in = urlconn.getInputStream();
        in.close();
                // 一次性取多少个字节
                byte[] bytes = new byte[readSize];
                // 读取到的字节数组长度,为-1时表示没有数据
                int length = 0;
                // 循环取数据
                while ((length = in.read(bytes)) != -1) {
                    String temp = HexUtils.encodeHexStr(Arrays.copyOf(bytes, length));
                    if (!StringUtil.strIsNullOrEmpty(temp)) {
                        fileHexStr.append(temp);
                        dataSize += length;
                    }
                }
        RemoteFileDto remoteFileDto = new RemoteFileDto();
        remoteFileDto.setList(data);
        remoteFileDto.setListSize(dataSize);
        remoteFileDto.setFileHexStr(fileHexStr.toString());
        return remoteFileDto;
                in.close();
                //按每帧数据大小进行切割截取数据
                String tempHexStr = fileHexStr.toString();
                int c = readSize * 2;
                int cl = -1;
                while (!StringUtil.strIsNullOrEmpty(tempHexStr)) {
                    if (tempHexStr.length() > c) {
                        cl = c;
                    } else {
                        cl = tempHexStr.length();
                    }
                    data.add(tempHexStr.substring(0, cl));
                    tempHexStr = tempHexStr.substring(cl, tempHexStr.length());
                }
                RemoteFileDto remoteFileDto = new RemoteFileDto();
                remoteFileDto.setList(data);
                remoteFileDto.setListSize(dataSize);
                remoteFileDto.setFileHexStr(fileHexStr.toString());
                return remoteFileDto;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
        RemoteFileDto t = read("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/20220507175550f1c437-e2f6-413f-9a78-c1cfea3d2a82.bin");
        System.out.println(t.getListSize());
        System.out.println(t.getList().size());
    }
}
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RemoteUpdateService.java
@@ -120,31 +120,30 @@
     * @param filePath 文件路径地址
     */
    public RemoteFileDto UpdateFileInfo(String orderType, String mac, String filePath) {
        try {
            RemoteFileDto remoteFileDto = FileProcessingUtils.read(filePath);
            RemoteSendFileReqInnerFrame remoteSendFileReqInnerFrame
                    = new RemoteSendFileReqInnerFrame(remoteFileDto.getListSize(), remoteFileDto.getList().size(), 512, remoteFileDto.getFileHexStr());
            A5Frame a5Frame = new A5Frame(orderType, remoteSendFileReqInnerFrame);
            System.out.println(a5Frame + "      a5Frame");
            CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
            StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级文件信息", a5Frame, commonFrame);
            System.out.println(commonFrame + "            -----commonFrame");
            RemoteUpdateFileInfoReportInnerFrame remoteUpdateFileInfoReportInnerFrame
                    = new RemoteUpdateFileInfoReportInnerFrame().transformFrame(commonFrame.getPayload());
            //确认OK
            if (remoteUpdateFileInfoReportInnerFrame != null && remoteUpdateFileInfoReportInnerFrame.isFlag()) {
                return remoteFileDto;
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        RemoteFileDto remoteFileDto = FileProcessingUtils.read(filePath);
        if (remoteFileDto == null) {
            throw new BusinessException("文件不存在");
        }
        return null;
        RemoteSendFileReqInnerFrame remoteSendFileReqInnerFrame
                = new RemoteSendFileReqInnerFrame(remoteFileDto.getListSize(), remoteFileDto.getList().size(), 512, remoteFileDto.getFileHexStr());
        A5Frame a5Frame = new A5Frame(orderType, remoteSendFileReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级文件信息", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
        RemoteUpdateFileInfoReportInnerFrame remoteUpdateFileInfoReportInnerFrame
                = new RemoteUpdateFileInfoReportInnerFrame().transformFrame(commonFrame.getPayload());
        //确认OK
        if (remoteUpdateFileInfoReportInnerFrame != null && remoteUpdateFileInfoReportInnerFrame.isFlag()) {
            return remoteFileDto;
        } else {
            return null;
        }
    }
    /**
@@ -160,12 +159,13 @@
        CommonFrame commonFrame = null;
        try {
            commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
            StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级包数据", a5Frame, commonFrame);
            System.out.println(commonFrame + "            -----commonFrame");
        } catch (Exception e) {
            System.out.println("发送数据超时或失败");
        }
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级包数据", a5Frame, commonFrame);
        //FE  B2  0004  0056  01  00  AE2C4474
    }