MyReadConfigFile.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.jiayue.ipfcst.client.protocol2file;
  2. import com.syjy.DataExchangeException;
  3. import com.syjy.FileUtils;
  4. import com.syjy.tunnelinfo.BaseTunnelInfo;
  5. import com.syjy.tunnelinfo.DataPoint;
  6. import com.syjy.tunnelinfo.gathertunnelinfo.*;
  7. import jxl.Cell;
  8. import jxl.Sheet;
  9. import jxl.Workbook;
  10. import jxl.WorkbookSettings;
  11. import jxl.read.biff.BiffException;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import wei.yigulu.modbus.domain.datatype.ModbusDataTypeEnum;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * 读取配置文件
  23. *
  24. * @author: xiuwei
  25. * @version:
  26. */
  27. @Slf4j
  28. public class MyReadConfigFile {
  29. public static List<BaseTunnelInfo> readConfigFile() throws IOException, BiffException {
  30. List<BaseTunnelInfo> tunnelInfos = new ArrayList<>();
  31. File configFile = getConfigFile();
  32. if (configFile == null) {
  33. return tunnelInfos;
  34. }
  35. InputStream is = new FileInputStream(configFile);
  36. WorkbookSettings ws = new WorkbookSettings();
  37. ws.setCellValidationDisabled(true);
  38. Workbook wb = Workbook.getWorkbook(is, ws);
  39. Sheet[] sheets = wb.getSheets();
  40. String cellValue;
  41. BaseTunnelInfo tunnelInfo;
  42. List<DataPoint> dataPoints;
  43. MyDataPoint dataPoint;
  44. ModbusDataTypeEnum dataType;
  45. int sheetI = 1;
  46. for (Sheet sheet : sheets) {
  47. if ("attribute".equals(sheet.getName())) {
  48. continue;
  49. }
  50. try {
  51. cellValue = getCellContext(sheet.getCell(1, 0));
  52. switch (cellValue) {
  53. case "104M":
  54. tunnelInfo = new Gather104TcpTunnelInfo();
  55. break;
  56. case "ModbusTCPM":
  57. tunnelInfo = new GatherModbusTcpTunnelInfo();
  58. break;
  59. case "ModbusTCPM_锦州阳光":
  60. tunnelInfo = new GatherModbusRtuByTcpServerTunnelInfo();
  61. break;
  62. case "ModbusRTUM":
  63. tunnelInfo = new GatherModbusRtuTunnelInfo();
  64. break;
  65. case "CDTM":
  66. tunnelInfo = new GatherCdtRtuTunnelInfo();
  67. break;
  68. case "GenFile":
  69. tunnelInfo = new GenerateFileTunnelInfo();
  70. break;
  71. case "Calculator":
  72. tunnelInfo = new CalculatorTunnelInfo();
  73. break;
  74. case "气象站":
  75. continue;
  76. default:
  77. throw new DataExchangeException(4002, "通道类型异常");
  78. }
  79. tunnelInfo.setId(sheetI + "");
  80. tunnelInfo.setTunnelName(sheet.getName() + sheetI);
  81. tunnelInfo.setByRow(sheet.getRow(2));
  82. tunnelInfo.setRefreshInterval(Integer.parseInt(getCellContext(sheet.getCell(3, 0))));
  83. dataPoints = new ArrayList<>();
  84. for (int i = 4; i < sheet.getRows(); i++) {
  85. dataPoint = new MyDataPoint();
  86. try {
  87. dataPoint.setId(Integer.parseInt(getCellContext(sheet.getCell(0, i))));
  88. } catch (DataExchangeException e) {
  89. continue;
  90. }
  91. if ("Calculator".equals(getCellContext(sheet.getCell(1, 0)))) {
  92. dataPoint.setFormula(getCellContext(sheet.getCell(1, i)));
  93. } else {
  94. dataPoint.setProtocolPoint(Integer.parseInt(getCellContext(sheet.getCell(1, i))));
  95. }
  96. cellValue = getCellContext(sheet.getCell(2, i));
  97. if ("遥信".equals(cellValue)) {
  98. dataType = ModbusDataTypeEnum.A16;
  99. } else if ("遥测".equals(cellValue)) {
  100. dataType = ModbusDataTypeEnum.ABCD;
  101. } else {
  102. dataType = ModbusDataTypeEnum.valueOf(cellValue);
  103. }
  104. dataPoint.setDataType(dataType);
  105. dataPoint.setMag(Double.parseDouble(getCellContext(sheet.getCell(3, i))));
  106. if (sheet.getRow(i).length > 5) {
  107. if ("测风塔".equals(sheet.getCell(4, i).getContents())) {
  108. dataPoint.setEquipmentType(EquipmentType.WIND_TOWER);
  109. dataPoint.setEquipmentAttr(sheet.getCell(5, i).getContents());
  110. } else if ("气象站".equals(sheet.getCell(4, i).getContents())) {
  111. dataPoint.setEquipmentType(EquipmentType.WEATHER_STATION);
  112. dataPoint.setEquipmentAttr(sheet.getCell(5, i).getContents());
  113. }
  114. }
  115. if (sheet.getRow(i).length > 6 && sheet.getCell(6, i) != null && !"".equals(sheet.getCell(6, i).getContents())) {
  116. dataPoint.setSlaveId(Integer.parseInt(getCellContext(sheet.getCell(6, i))));
  117. }
  118. dataPoints.add(dataPoint);
  119. }
  120. tunnelInfo.setDataPoints(dataPoints);
  121. tunnelInfos.add(tunnelInfo);
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. sheetI++;
  126. }
  127. return tunnelInfos;
  128. }
  129. public static String getCellContext(Cell cell) throws DataExchangeException {
  130. if (cell == null) {
  131. throw new DataExchangeException(4001, "配置文件缺少配置内容," + cell.getRow() + "行" + cell.getColumn() + "列");
  132. }
  133. String val = cell.getContents();
  134. if (StringUtils.isNoneEmpty(val)) {
  135. return val;
  136. } else {
  137. throw new DataExchangeException(4001, "配置文件缺少配置内容," + cell.getRow() + "行" + cell.getColumn() + "列");
  138. }
  139. }
  140. public static File getConfigFile() {
  141. // configFile1 是jar包同级目录
  142. File configFile1 = new File(FileUtils.getResourcesPath(MyReadConfigFile.class));
  143. //configFile2 是jar包的父级目录的同级目录
  144. File configFile2 = null;
  145. if (configFile1 != null) {
  146. configFile2 = configFile1.getParentFile();
  147. }
  148. File[] files = new File[0];
  149. if (configFile2 != null) {
  150. files = configFile2.listFiles();
  151. }
  152. if (files != null) {
  153. for (File f : files) {
  154. if (f.getName().contains("pointconfig.xls")) {
  155. log.info("在jar包上层目录发现点表配置文件");
  156. return f;
  157. }
  158. }
  159. }
  160. if (configFile1 != null) {
  161. files = configFile1.listFiles();
  162. }
  163. if (files != null) {
  164. for (File f : files) {
  165. if (f.getName().contains("pointconfig.xls")) {
  166. log.info("在jar包同级目录发现点表配置文件");
  167. return f;
  168. }
  169. }
  170. }
  171. return null;
  172. }
  173. }