2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-06-02 93b045e2f659a8dbd424bd8c9beb6525e7a80c05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.sandu.ximon.admin.service;
 
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.dto.RemoteFileDto;
import com.sandu.ximon.admin.dto.RemoteUpdateTypeDto;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.*;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.*;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.RemoteUpdateTypeEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.manager.iot.rrpc.util.FileProcessingUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.dao.domain.RemoteUpdateFile;
import com.sandu.ximon.dao.mapper.RemoteUpdateFileMapper;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
 
/**
 * @author ZZQ
 * @date 2022/4/12 11:09
 */
@Service
public class RemoteUpdateService extends BaseServiceImpl<RemoteUpdateFileMapper, RemoteUpdateFile> {
    /**
     * 添加文件更新内容
     *
     * @param filename
     * @param fileType
     * @param softwareVersion
     * @param hardwareVersion
     * @param aliAddress
     */
    public boolean addRemoteUpdateFile(String filename, String fileType, String softwareVersion
            , String hardwareVersion, String aliAddress, String fileLength) {
        RemoteUpdateFile remoteUpdateFile = new RemoteUpdateFile();
        remoteUpdateFile.setAliAddress(aliAddress);
        remoteUpdateFile.setFileType(fileType);
        remoteUpdateFile.setSoftwareVersion(new BigDecimal(softwareVersion).setScale(2, BigDecimal.ROUND_DOWN));
        remoteUpdateFile.setHardwareVersion(new BigDecimal(hardwareVersion).setScale(2, BigDecimal.ROUND_DOWN));
        remoteUpdateFile.setFilename(filename);
        remoteUpdateFile.setFileLength(fileLength);
        return save(remoteUpdateFile);
    }
 
    /**
     * 固件升级文件
     *
     * @return
     */
    public List<RemoteUpdateFile> getRemoteFileList() {
        return list();
    }
 
 
    /**
     * 远程升级类型
     */
    public List<RemoteUpdateTypeDto> getRemoteUpdateTypeList() {
        return RemoteUpdateTypeEnum.getAllType();
    }
 
 
    /**
     * 启动远程升级命令
     *
     * @param mac             设备MAC地址
     * @param hardwareVersion 硬件版本
     * @param softwareVersion 软件版本
     * @param updateFlag      升级标志
     */
    public boolean StartRemoteUpdate(String orderType, String mac, String hardwareVersion, String softwareVersion, String updateFlag) {
 
        RemoteStartUpdateReqInnerFrame remoteStartUpdateReqInnerFrame = new RemoteStartUpdateReqInnerFrame(hardwareVersion, softwareVersion, updateFlag);
        A5Frame a5Frame = new A5Frame(orderType, remoteStartUpdateReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-启动远程升级", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
 
        RemoteStartUpdateReportInnerFrame remoteStartUpdateReportInnerFrame
                = new RemoteStartUpdateReportInnerFrame().transformFrame(commonFrame.getPayload());
        boolean flag;
        if (remoteStartUpdateReportInnerFrame.isValidate()) {
            switch (remoteStartUpdateReportInnerFrame.getOrderFlag()) {
                case "0":
                    if ("1".equals(remoteStartUpdateReportInnerFrame.getUpdateFlag())) {
                        System.out.println("开始升级相关步骤");
                        //服务器下发升级文件信息
                        flag = true;
                    } else {
                        System.out.println("不进行升级");
                        flag = false;
                    }
                    break;
                case "1":
                    System.out.println("终端不在空闲状态");
                    flag = false;
                    break;
                case "8":
                    System.out.println("指令有误");
                    flag = false;
                    break;
                default:
                    flag = false;
            }
        } else {
            throw new BusinessException("数据校验错误,请重新请求");
        }
        return flag;
    }
 
    /**
     * 服务器下发升级文件信息
     *
     * @param filePath 文件路径地址
     */
    public RemoteFileDto UpdateFileInfo(String orderType, String mac, String filePath) {
 
        RemoteFileDto remoteFileDto = FileProcessingUtils.read(filePath);
        if (remoteFileDto == null) {
            throw new BusinessException("文件不存在");
        }
 
        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;
        }
    }
 
    /**
     * 升级包数据
     *
     * @param volumeOrder 卷序
     * @param datas       升级包分卷数据
     */
    public void UpdateData(String orderType, String mac, int volumeOrder, String datas) {
        RemoteUpdateDataReqInnerFrame remoteUpdateDataReqInnerFrame = new RemoteUpdateDataReqInnerFrame(volumeOrder, datas);
        A5Frame a5Frame = new A5Frame(orderType, remoteUpdateDataReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = null;
        try {
            commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
 
            System.out.println(commonFrame + "            -----commonFrame");
        } catch (Exception e) {
            System.out.println("发送数据超时或失败");
        }
 
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级包数据", a5Frame, commonFrame);
        //FE  B2  0004  0056  01  00  AE2C4474
    }
 
    /**
     * 服务器下发查询漏包帧
     */
    public boolean SearchLoseData(String orderType, String mac, RemoteFileDto remoteFileDto, int resendTime) {
        RemoteSearchLoseDataReqInnerFrame remoteSearchLoseDataReqInnerFrame = new RemoteSearchLoseDataReqInnerFrame();
        A5Frame a5Frame = new A5Frame(orderType, remoteSearchLoseDataReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-查询漏包帧", a5Frame, commonFrame);
 
        System.out.println(commonFrame + "            -----commonFrame");
 
        RemoteSearchLoseDataReportInnerFrame remoteSearchLoseDataReportInnerFrame = new RemoteSearchLoseDataReportInnerFrame().transformFrame(commonFrame.getPayload());
 
        //校验不通过,有漏包
        if (!remoteSearchLoseDataReportInnerFrame.isCheckState()) {
            if (resendTime == 3) {
                throw new BusinessException("重发丢帧数据超次数,更新失败!");
            }
            for (Integer integer : remoteSearchLoseDataReportInnerFrame.getList()) {
                UpdateData(orderType, mac, integer, remoteFileDto.getList().get(integer));
            }
            SearchLoseData(orderType, mac, remoteFileDto, resendTime + 1);
        }
        return true;
    }
 
    /**
     * 服务器下发结束升级帧
     */
    public boolean FinishUpdate(String orderType, String mac) {
        RemoteFinishUpdateReqInnerFrame remoteFinishUpdateReqInnerFrame = new RemoteFinishUpdateReqInnerFrame();
        A5Frame a5Frame = new A5Frame(orderType, remoteFinishUpdateReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级结束", a5Frame, commonFrame);
 
        System.out.println(commonFrame + "            -----commonFrame");
 
        RemoteFinishUpdateReportInnerFrame remoteFinishUpdateReportInnerFrame = new RemoteFinishUpdateReportInnerFrame().transformFrame(commonFrame.getPayload());
 
        //返回升级结果
        return remoteFinishUpdateReportInnerFrame.isResponseState();
    }
 
    /**
     * 服务器下发升级结果查询
     */
    public String SearchUpdateResult(String orderType, String mac) {
        RemoteSearchUpdateResultReqInnerFrame remoteSearchUpdateResultReqInnerFrame = new RemoteSearchUpdateResultReqInnerFrame();
        A5Frame a5Frame = new A5Frame(orderType, remoteSearchUpdateResultReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-升级结果查询", a5Frame, commonFrame);
 
        System.out.println(commonFrame + "            -----commonFrame");
 
        RemoteSearchUpdateResultReportInnerFrame innerFrame = new RemoteSearchUpdateResultReportInnerFrame().transformFrame(commonFrame.getPayload());
        String result = "硬件版本:" + innerFrame.getHardwareVersion() + "  软件件版本:" + innerFrame.getSoftwareVersion();
        return result;
    }
 
    /**
     * 服务器下发紧急中断升级控制帧
     */
    public boolean stopUpdate(String orderType, String mac) {
        RemoteStopUpdateReqInnerFrame remoteStopUpdateReqInnerFrame = new RemoteStopUpdateReqInnerFrame();
        A5Frame a5Frame = new A5Frame(orderType, remoteStopUpdateReqInnerFrame);
        System.out.println(a5Frame + "      a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(mac, "固件升级-紧急中断", a5Frame, commonFrame);
 
        System.out.println(commonFrame + "            -----commonFrame");
        RemoteStopUpdateReportInnerFrame innerFrame = new RemoteStopUpdateReportInnerFrame().transformFrame(commonFrame.getPayload());
 
        return innerFrame.isResponseState();
    }
 
}