Channel.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //package com.jiayue.biz.conf;
  2. //
  3. //import cn.hutool.core.date.DateTime;
  4. //import cn.hutool.core.thread.ThreadFactoryBuilder;
  5. //import com.jiayue.biz.domain.PointAttribute;
  6. //import com.jiayue.biz.domain.TunnelInfo;
  7. //import com.jiayue.biz.domain.WindTowerInfo;
  8. //import com.jiayue.biz.service.PointAttributeService;
  9. //import com.jiayue.biz.service.TunnelInfoService;
  10. //import com.jiayue.biz.service.WindTowerInfoService;
  11. //import com.jiayue.biz.service.impl.PointAttributeServiceImpl;
  12. //import com.jiayue.biz.service.impl.WindTowerDataParentTableServiceImpl;
  13. //import com.jiayue.biz.util.CalculationUtil;
  14. //import com.jiayue.common.annotation.Log;
  15. //import com.jiayue.common.utils.DateUtil;
  16. //import io.netty.channel.ChannelFuture;
  17. //import lombok.AllArgsConstructor;
  18. //import lombok.extern.slf4j.Slf4j;
  19. //import org.springframework.beans.factory.annotation.Autowired;
  20. //import org.springframework.context.annotation.Bean;
  21. //import org.springframework.context.annotation.Configuration;
  22. //import org.springframework.scheduling.annotation.Scheduled;
  23. //import wei.yigulu.modbus.domain.FunctionCode;
  24. //import wei.yigulu.modbus.domain.Obj4RequestRegister;
  25. //import wei.yigulu.modbus.domain.datatype.IModbusDataType;
  26. //import wei.yigulu.modbus.domain.datatype.ModbusDataTypeEnum;
  27. //import wei.yigulu.modbus.domain.datatype.NumericModbusData;
  28. //import wei.yigulu.modbus.exceptiom.ModbusException;
  29. //import wei.yigulu.modbus.netty.ModbusTcpMasterBuilder;
  30. //import wei.yigulu.modbus.utils.ModbusRequestDataUtils;
  31. //
  32. //import java.math.BigDecimal;
  33. //import java.util.*;
  34. //import java.util.concurrent.ExecutorService;
  35. //import java.util.concurrent.SynchronousQueue;
  36. //import java.util.concurrent.ThreadPoolExecutor;
  37. //import java.util.concurrent.TimeUnit;
  38. //import java.util.stream.Collectors;
  39. //
  40. ////@Configuration
  41. //@AllArgsConstructor
  42. //@Slf4j
  43. //public class Channel {
  44. // private final
  45. // TunnelInfoService tunnelInfoService;
  46. //
  47. // private final
  48. // PointAttributeService pointAttributeService;
  49. //
  50. // private final
  51. // WindTowerDataParentTableServiceImpl windTowerDataParentTableService;
  52. // private final
  53. // WindTowerInfoService windTowerInfoService;
  54. //
  55. //
  56. // private final ExecutorService calculatorThread = new ThreadPoolExecutor(15, 1000,
  57. // 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactoryBuilder().setNamePrefix("MyRecurringTaskThread-").build());
  58. //
  59. //
  60. // private HashMap<String, ModbusTcpMasterBuilder> masterMap;
  61. //
  62. //
  63. //// @Bean
  64. // public void timingTunnel() {
  65. // List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
  66. // tunnelInfoList.clear();
  67. // TunnelInfo tunnelInfo1 = new TunnelInfo();
  68. // TunnelInfo tunnelInfo2 = new TunnelInfo();
  69. // tunnelInfo1.setDataFormat("CDAB");
  70. //// tunnelInfo1.setIp("192.168.10.75");
  71. // tunnelInfo1.setIp("192.168.1.206");
  72. // tunnelInfo1.setPort(502);
  73. // tunnelInfo1.setStationId("七台河3005");
  74. // tunnelInfo1.setEquipmentNo("3005");
  75. //// tunnelInfo1.setStationId("虎林15244");
  76. //// tunnelInfo1.setEquipmentNo("15244");
  77. //
  78. // tunnelInfo2.setDataFormat("CDAB");
  79. // tunnelInfo2.setIp("192.168.10.75");
  80. // tunnelInfo2.setPort(502);
  81. // tunnelInfo2.setStationId("虎林15244");
  82. // tunnelInfo2.setEquipmentNo("15244");
  83. // tunnelInfoList.add(tunnelInfo1);
  84. // tunnelInfoList.add(tunnelInfo2);
  85. // for (TunnelInfo tunnelInfo : tunnelInfoList) {
  86. // ModbusTcpMasterBuilder master = start(tunnelInfo);
  87. // masterMap.put(tunnelInfo.getStationId(), master);
  88. //
  89. // }
  90. // }
  91. //
  92. // //创建线程
  93. // public ModbusTcpMasterBuilder start(TunnelInfo tunnelInfo) {
  94. // //创建通道后 对通道map 进行 采集数据
  95. // //获取场站id 形成map
  96. // ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
  97. // master.getConfigInfoMap().put("stationId", tunnelInfo.getStationId());
  98. // //create 会阻塞线程
  99. // calculatorThread.execute(master::create);
  100. // master.isConnected();
  101. // return master;
  102. // }
  103. //
  104. // //关闭通道移除连接
  105. // public void stop(String stationId) {
  106. // if (masterMap.get(stationId).isConnected()) {
  107. // masterMap.get(stationId).stop();
  108. // masterMap.remove(stationId);
  109. // }
  110. // }
  111. //
  112. //// @Scheduled(cron = "0 0/5 * * * ? ")
  113. // public void tunnel() {
  114. // List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
  115. // List<PointAttribute> attributeList = pointAttributeService.list();
  116. // String time = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm");
  117. // for (Map.Entry<String, ModbusTcpMasterBuilder> masterBuilderEntry : masterMap.entrySet()) {
  118. // //获取通道map中的key
  119. // String stationId = masterBuilderEntry.getKey();
  120. // //风速层高
  121. // HashSet<Integer> wsHeightSet = new HashSet<>();
  122. // //风向层高
  123. // HashSet<Integer> wdHeightSet = new HashSet<>();
  124. // //根据通道key获取ip端口号等数据
  125. // List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getStationId().equals(stationId)).collect(Collectors.toList());
  126. // TunnelInfo tunnelInfo;
  127. // if (!tunnelist.isEmpty()) {
  128. // tunnelInfo = tunnelist.get(0);
  129. // } else {
  130. // //如果此通道不存在 关闭通道
  131. // this.stop(stationId);
  132. // continue;
  133. // }
  134. // //根据通道key获取点表数据
  135. // List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getStationId().equals(stationId)).collect(Collectors.toList());
  136. // //拿到所有点位
  137. // List<Integer> points = pointAttributeList.stream().map(PointAttribute::getPoint).collect(Collectors.toList());
  138. // Map<Integer, ModbusDataTypeEnum> pointTypeMap = new HashMap<>();
  139. // for (Integer point : points) {
  140. // //点位以及点位类型存入map
  141. // pointTypeMap.put(point, ModbusDataTypeEnum.valueOf(tunnelInfo.getDataFormat()));
  142. // }
  143. //
  144. // //获取连接
  145. // ModbusTcpMasterBuilder master = masterBuilderEntry.getValue();
  146. // //获取点位信息map
  147. // Map<String, Integer> stringStringBuilderMap = savePoints(pointAttributeList, wsHeightSet, wdHeightSet);
  148. // WindTowerInfo windTowerInfo = new WindTowerInfo();
  149. // //判断是否存在此塔
  150. // boolean emptyOfEntity = windTowerInfoService.lambdaQuery().eq(WindTowerInfo::getEquipmentNo, tunnelInfo.getEquipmentNo()).list().isEmpty();
  151. // if (emptyOfEntity) {
  152. // StringBuilder wsHeightStr = new StringBuilder();
  153. // for (Integer height : wsHeightSet) {
  154. // wsHeightStr = wsHeightStr.append(height + ",");
  155. // }
  156. // String wsHeight = null;
  157. // if (wsHeightStr.length() > 0) {
  158. // wsHeight = wsHeightStr.substring(0, wsHeightStr.length() - 1);
  159. // }
  160. //
  161. // StringBuilder wdHeightStr = new StringBuilder();
  162. // for (Integer height : wsHeightSet) {
  163. // wdHeightStr = wdHeightStr.append(height + ",");
  164. // }
  165. // String wdHeight = null;
  166. // if (wdHeightStr.length() > 0) {
  167. // wdHeight = wdHeightStr.substring(0, wdHeightStr.length() - 1);
  168. // }
  169. // //存储测风塔信息
  170. // windTowerInfo.setEquipmentNo(tunnelist.get(0).getEquipmentNo());
  171. // windTowerInfo.setName(tunnelist.get(0).getStationId());
  172. // windTowerInfo.setHeights(wsHeight);
  173. // windTowerInfo.setWdHeights(wdHeight);
  174. // windTowerInfo.setType("2");
  175. // windTowerInfoService.save(windTowerInfo);
  176. // }
  177. // // 例如: key:wsAve val:数据
  178. // HashMap<String, BigDecimal> pointMap = new HashMap<>();
  179. // //分解成Obj4RequestCoil
  180. // List<Obj4RequestRegister> obj4RequestRegisters = null;
  181. // try {
  182. // obj4RequestRegisters = ModbusRequestDataUtils.splitModbusRequest(pointTypeMap, 1, FunctionCode.READ_HOLDING_REGISTERS);
  183. // for (Obj4RequestRegister obj4RequestRegister : obj4RequestRegisters) {
  184. // Map<Integer, IModbusDataType> registerData = ModbusRequestDataUtils.getRegisterData(master, obj4RequestRegister);
  185. // if(!registerData.isEmpty()){
  186. // for (Map.Entry<Integer, IModbusDataType> typeEntry : registerData.entrySet()) {
  187. // for (Map.Entry<String, Integer> entry : stringStringBuilderMap.entrySet()) {
  188. // if (typeEntry.getKey().equals(entry.getValue())) {
  189. // log.info("点位:{}-----数值:{}",typeEntry.getKey(),((NumericModbusData) typeEntry.getValue()).getValue());
  190. // //点位一致 置换value
  191. // pointMap.put(entry.getKey(), ((NumericModbusData) typeEntry.getValue()).getValue());
  192. // }
  193. // }
  194. // }
  195. // }
  196. //
  197. // }
  198. // } catch (ModbusException e) {
  199. // log.info(e.getMsg());
  200. // throw new RuntimeException(e);
  201. // }
  202. // wsHeightSet.addAll(wdHeightSet);
  203. // windTowerDataParentTableService.saveDataForTunnel(pointMap, wsHeightSet,tunnelInfo.getEquipmentNo(),time);
  204. // }
  205. // }
  206. //
  207. //
  208. // //获取点表所对应数据
  209. // public Map<String, Integer> savePoints(List<PointAttribute> channelPoints, HashSet<Integer> wsHeightSet, HashSet<Integer> wdHeightSet) {
  210. // //如果数据库没有此测风塔 读取点表信息增加塔表
  211. // HashMap<String, Integer> dataMap = new HashMap<>();
  212. // for (PointAttribute channelPoint : channelPoints) {
  213. // if(channelPoint.getUnit() == null){
  214. // channelPoint.setUnit("");
  215. // }
  216. // if ((channelPoint.getUnit().equals("y") || channelPoint.getUnit().equals("Y")) && channelPoint.getMeaning().contains("年")) {
  217. // dataMap.put("yyyy", channelPoint.getPoint());
  218. // } else if ((channelPoint.getUnit().equals("y") || channelPoint.getMeaning().equals("Y")) && channelPoint.getMeaning().contains("月")) {
  219. // dataMap.put("MM", channelPoint.getPoint());
  220. // } else if (channelPoint.getUnit().equals("d") && channelPoint.getMeaning().contains("日")) {
  221. // dataMap.put("dd", channelPoint.getPoint());
  222. // } else if (channelPoint.getUnit().equals("h") && channelPoint.getMeaning().contains("时")) {
  223. // dataMap.put("HH", channelPoint.getPoint());
  224. // } else if (channelPoint.getUnit().equals("m") && channelPoint.getMeaning().contains("分")) {
  225. // dataMap.put("mm", channelPoint.getPoint());
  226. // } else if (channelPoint.getMeaning().contains("纬")) {
  227. // dataMap.put("latitude", channelPoint.getPoint());
  228. // } else if (channelPoint.getMeaning().contains("经")) {
  229. // dataMap.put("longitude", channelPoint.getPoint());
  230. // } else if (channelPoint.getMeaning().contains("海拔")) {
  231. // dataMap.put("elevation", channelPoint.getPoint());
  232. // } else if (channelPoint.getMeaning().contains("电压")) {
  233. // String status = status(channelPoint.getMeaning());
  234. // dataMap.put("v" + status, channelPoint.getPoint());
  235. // } else {
  236. // String dataType = dataType(channelPoint.getMeaning(), wsHeightSet, wdHeightSet);
  237. // if (!dataType.equals("")) {
  238. // dataMap.put(dataType, channelPoint.getPoint());
  239. // }
  240. // }
  241. //
  242. // }
  243. // return dataMap;
  244. // }
  245. //
  246. //
  247. // public String dataType(String str, HashSet<Integer> wsHeightSet, HashSet<Integer> wdHeightSet) {
  248. // String typeStr = "";
  249. // int index;
  250. // if (!str.contains("米") && !str.contains("m")) {
  251. // return "";
  252. // }
  253. // if (str.contains("米")) {
  254. // index = str.indexOf("米");
  255. // } else {
  256. // index = str.indexOf("m");
  257. // }
  258. // //截取风速
  259. // String dataStr = str.substring(0, index);
  260. // Integer height = Integer.valueOf(CalculationUtil.getNumberFromString(dataStr));
  261. // String dataType = "";
  262. // typeStr = status(str);
  263. //
  264. // if (str.contains("风速") && !str.contains("风向")) {
  265. // dataType = "ws";
  266. // wsHeightSet.add(height);
  267. // } else if (str.contains("风向")) {
  268. // dataType = "wd";
  269. // wdHeightSet.add(height);
  270. // } else if (str.contains("湿度")) {
  271. // dataType = "rh";
  272. // return dataType + typeStr;
  273. // } else if (str.contains("温度")) {
  274. // dataType = "t";
  275. // return dataType + typeStr;
  276. // } else if (str.contains("气压") || str.contains("压力")) {
  277. // dataType = "pa";
  278. // return dataType + typeStr;
  279. // }
  280. // return dataType + typeStr + height;
  281. // }
  282. //
  283. // public String status(String str) {
  284. // String typeStr = "";
  285. // if (str.contains("平均值") && !str.contains("G")) {
  286. // typeStr = "Ave";
  287. // } else if (str.contains("方差") || str.contains("标准偏差") || str.contains("标准差")) {
  288. // typeStr = "Sta";
  289. // } else if (str.contains("最大值") && !str.contains("极大")) {
  290. // typeStr = "Max";
  291. // } else if (str.contains("最小值")) {
  292. // typeStr = "Min";
  293. // } else if (str.contains("瞬时")) {
  294. // typeStr = "Inst";
  295. // } else if (str.contains("极大")) {
  296. // typeStr = "Great";
  297. // } else if (str.contains("Gust") || str.contains("GUST")) {
  298. // typeStr = "Gust";
  299. // } else if (str.contains("风速") && str.contains("风向")) {
  300. // typeStr = "OnWs";
  301. // } else if (str.contains("实时")) {
  302. // typeStr = "Now";
  303. // }
  304. // return typeStr;
  305. // }
  306. //
  307. //
  308. //}