liurunyu
2024-08-15 f7e731bdc2fce4445c0d22993134c6c2b07d207b
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
package com.ruoyi.web.controller.system;
 
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.netty.ChannelHandlerContextCache;
import com.ruoyi.common.utils.netty.ConcurrentHashMapBean;
import com.ruoyi.common.utils.netty.NettyTool;
import com.ruoyi.common.utils.netty.connectToClient;
import com.ruoyi.system.domain.dto.NettyCommandDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 命令 执行
 *
 * @author ruoyi
 * @date 2022-10-11
 */
@Slf4j
@Api("远程升级")
@RestController
@RequestMapping("/system/command")
public class NettyCommandController extends BaseController
{
 
    @Autowired
    private RedisCache redisCache;
 
    /**
     * 查询升级内容详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:content:list')")
    @PostMapping("perform")
    public AjaxResult list(@RequestBody  NettyCommandDTO nettyCommandDTO)
    {
        //解析文件
        Integer type=nettyCommandDTO.getType();
        switch (type){
            case 15:
                if (nettyCommandDTO.getUpgrade()==1){
                    for (String stcd:nettyCommandDTO.getStcdList()){
                        stcd=NettyTool.stcdHex2bcd(stcd);
                        String updatefileStrCRC = ChannelHandlerContextCache.UpgradeFileString;
                        //高位在前
                        updatefileStrCRC=updatefileStrCRC.substring(0,updatefileStrCRC.length()-1);
                        String RS232number2f = NettyTool.getCRChigh(updatefileStrCRC);
                        log.info("---------------CRC16校验----------"+RS232number2f+"--------------");
                        //块数量 两个字节向前补位
                        String Numberofblocks = NettyTool.addZeroForNum(NettyTool.Numberofblocks(ChannelHandlerContextCache.cache).toString(), 4);
                        String crc = NettyTool.getCRChigh("AA000E0000000001" + Numberofblocks + RS232number2f);
                        log.info("数据包:【" + "AA000E0000000001" + Numberofblocks + RS232number2f + crc + "】" + " 块数量:【" + Numberofblocks + "】" + " 文件校验码:【" + RS232number2f + "】" + " CRC16校验:【" + crc + "】");
                        String data  = "AA000E0000000001" + Numberofblocks + RS232number2f + crc;
                        connectToClient.write2Client(data, ConcurrentHashMapBean.channelMap.get(stcd),"下载文件信息01");
                    }
                    //发送升级文件信息
                    return AjaxResult.success("执行成功");
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }
 
        return AjaxResult.success("执行成功");
    }
 
 
    @PreAuthorize("@ss.hasPermi('system:content:list')")
    @PostMapping("/importData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile", dataTypeClass = MultipartFile.class)
    })
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String fileName= file.getOriginalFilename();
        //判定是否重复数据
//        if (redisCache.getCacheList("fileName").contains(fileName)){
//            return AjaxResult.success("升级文件重复请重新升级");
//        }
        //解析数据
         String data1 =NettyTool.readCityFile(file);
         List<String> kuai1List=new ArrayList<>();
         //
        List<Integer> indexs=NettyTool.getIndexs(data1,"@");
        for (Integer index:indexs){
            String  kuai = data1.substring(index,data1.length());
            String[] strings = kuai.split(" ");
            kuai1List.add(strings[0].substring(0,strings[0].length()-2));
        }
 
        String UpgradeFileString = NettyTool.readCityFile(file).replace(" ", "");
        //读取文件信息 储存到 redis中
        log.info("---------------------读取文件成功,字符串解析成功-------------------------------");
        //开始分包数 将@后的存到map 里面 将其分包
//        ChannelHandlerContextCache.fileStr = NettyTool.xiabiao(UpgradeFileString);
        Map<String, String>  fileStr_new = NettyTool.xiabiao_new(UpgradeFileString, kuai1List);
        ChannelHandlerContextCache.fileStr = fileStr_new;
//        ChannelHandlerContextCache.cache=NettyTool.cachepage(ChannelHandlerContextCache.fileStr);
        Map<String, List<String>> cache_new = NettyTool.cachepage(fileStr_new);
        ChannelHandlerContextCache.cache = cache_new;
        //将字数不够的补齐
        for (List<String> value :  ChannelHandlerContextCache.cache.values()) {
            String a = NettyTool.addstrfFForNum(value.get(value.size() - 1), 1024);
            value.set(value.size() - 1, a);
        }
 
        ChannelHandlerContextCache.keys=ChannelHandlerContextCache.cache.keySet().toArray();
        //计算包数 开始逐步发送
        int pagenumber=NettyTool.Calculatepagenumber(ChannelHandlerContextCache.cache);
        /*log.info("-----------------包数一共为 "+pagenumber+"--------------------------------");*/
        /*   开始原始文件 进行校验  替换 @为 40000*/
//        String updatefileStr = UpgradeFileString.replaceAll("@", "4000");
        String updatefileStr_new=NettyTool.replace(UpgradeFileString,kuai1List);
        ChannelHandlerContextCache.UpgradeFileString=updatefileStr_new;
        String updatefileStrCRC = updatefileStr_new.replaceAll("q","");
        //高位在前
        String RS232number2f = NettyTool.getCRChigh(updatefileStrCRC);
        log.info("---------------CRC16校验----------"+RS232number2f+"--------------");
        //块数量 两个字节向前补位
        String Numberofblocks = NettyTool.addZeroForNum(NettyTool.Numberofblocks(ChannelHandlerContextCache.cache).toString(), 4);
        String crc = NettyTool.getCRChigh("AA000E0000000001" + Numberofblocks + RS232number2f);
        log.info("数据包:【" + "AA000E0000000001" + Numberofblocks + RS232number2f + crc + "】" + " 块数量:【" + Numberofblocks + "】" + " 文件校验码:【" + RS232number2f + "】" + " CRC16校验:【" + crc + "】");
        String data  = "AA000E0000000001" + Numberofblocks + RS232number2f + crc;
//        connectToClient.write2Client(data, ConcurrentHashMapBean.channelMap.get("166400"),"下载文件信息01");
         //发送升级文件信息
        List<String> fileNameList=redisCache.getCacheList(fileName);
        fileNameList.add(fileName);
        redisCache.setCacheList("fileName",fileNameList);
        //ChannelHandlerContextCache.fileStr
        redisCache.setCacheMap(fileName+"fileStr",ChannelHandlerContextCache.fileStr);
        //ChannelHandlerContextCache.cache
        redisCache.setCacheMap(fileName+"cache",ChannelHandlerContextCache.cache);
        //ChannelHandlerContextCache.keys
        redisCache.setCacheObject(fileName+"keys",ChannelHandlerContextCache.keys);
        //ChannelHandlerContextCache.UpgradeFileString
        redisCache.setCacheObject(fileName+"UpgradeFileString",ChannelHandlerContextCache.UpgradeFileString);
        return AjaxResult.success("执行成功");
    }
 
 
 
 
 
}