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 kuai1List=new ArrayList<>(); // List 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 fileStr_new = NettyTool.xiabiao_new(UpgradeFileString, kuai1List); ChannelHandlerContextCache.fileStr = fileStr_new; // ChannelHandlerContextCache.cache=NettyTool.cachepage(ChannelHandlerContextCache.fileStr); Map> cache_new = NettyTool.cachepage(fileStr_new); ChannelHandlerContextCache.cache = cache_new; //将字数不够的补齐 for (List 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 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("执行成功"); } }