s7gatherWatch.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package org.jeecg.modules.watch;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import io.netty.util.internal.StringUtil;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.jeecg.common.util.oConvertUtils;
  6. import org.jeecg.modules.dataRepository.entity.PointData;
  7. import org.jeecg.modules.device.entity.DeviceInformation;
  8. import org.jeecg.modules.device.service.IDeviceInformationService;
  9. import org.jeecg.modules.deviceConn.s7.service.S7PLC;
  10. import org.jeecg.modules.devicePoint.entity.DevicePoint;
  11. import org.jeecg.modules.devicePoint.service.IDevicePointService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.mongodb.core.MongoTemplate;
  14. import org.springframework.scheduling.annotation.EnableAsync;
  15. import org.springframework.scheduling.annotation.EnableScheduling;
  16. import org.springframework.scheduling.annotation.Scheduled;
  17. import org.springframework.stereotype.Component;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. @EnableScheduling
  22. @EnableAsync
  23. @Component
  24. @Slf4j
  25. public class s7gatherWatch {
  26. @Autowired
  27. IDeviceInformationService deviceInformationService;
  28. @Autowired
  29. IDevicePointService devicePointService;
  30. @Autowired
  31. MongoTemplate mongoTemplate;
  32. HashMap<String, S7PLC> s7PLCmap = new HashMap<>();
  33. @Scheduled(fixedDelay = 500)
  34. public void t1(){
  35. s7("500");
  36. }
  37. @Scheduled(fixedDelay = 1000)
  38. public void t2(){
  39. s7("1000");
  40. }
  41. @Scheduled(fixedDelay = 2000)
  42. public void t3(){
  43. s7("2000");
  44. }
  45. @Scheduled(fixedDelay = 5000)
  46. public void t4(){
  47. s7("5000");
  48. }
  49. @Scheduled(fixedDelay = 10000)
  50. public void t5(){
  51. s7("10000");
  52. }
  53. @Scheduled(fixedDelay = 30000)
  54. public void t6(){
  55. s7("30000");
  56. }
  57. @Scheduled(fixedDelay = 60000)
  58. public void t7(){
  59. s7("60000");
  60. }
  61. //s7数据采集
  62. public void s7(String freq){
  63. //获取当前时间
  64. Date curentDate = new Date();
  65. //获取所有运行中设备
  66. LambdaQueryWrapper<DeviceInformation> s7Query = new LambdaQueryWrapper<DeviceInformation>().ne(DeviceInformation::getStatus, "1").eq(DeviceInformation::getFreq, freq);
  67. List<DeviceInformation> s7List = deviceInformationService.list(s7Query);
  68. //遍历设备集合
  69. s7List.forEach(s7 -> {
  70. List<DevicePoint> s7Infos = null;
  71. //未处理数据
  72. String read = "";
  73. //处理过的数据
  74. String readText = "";
  75. //从连接池中拿取连接
  76. S7PLC s7PLC = s7PLCmap.get(s7.getId());
  77. //连接对象为空则创建
  78. if(oConvertUtils.isEmpty(s7PLC)){
  79. //与设备建立连接
  80. s7PLC = new S7PLC(s7.getS7Model(), s7.getDeviceIp(), Integer.valueOf(s7.getDevicePort()), s7.getS7Rack(), Integer.valueOf(s7.getS7Slot()), Integer.valueOf(s7.getS7LongestWidth()));
  81. //将连接放入连接池中
  82. s7PLCmap.put(s7.getId(),s7PLC);
  83. }
  84. try {
  85. //获取设备下所有点位
  86. s7Infos = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId,s7.getId()));
  87. //遍历点位集合
  88. for (DevicePoint s7Info : s7Infos) {
  89. try {
  90. //读取点位数据
  91. String pointAddr = s7Info.getPointAddr()+"";
  92. if (s7Info.getDateType().equals("STRING")){
  93. read = "" + s7PLC.readString(pointAddr);
  94. readText = read;
  95. }else if (s7Info.getDateType().equals("REAL")){
  96. read = "" + s7PLC.readFloat32(pointAddr);
  97. readText = read;
  98. }else if (s7Info.getDateType().equals("DINT")){
  99. read = "" + s7PLC.readInt32(pointAddr);
  100. readText = read;
  101. }else if (s7Info.getDateType().equals("INT")){
  102. read = "" + s7PLC.readInt16(pointAddr);
  103. readText = read;
  104. }else if (s7Info.getDateType().equals("DWORD")){
  105. read = "" + s7PLC.readInt32(pointAddr);
  106. readText = read;
  107. }else if (s7Info.getDateType().equals("WORD")){
  108. read = "" + s7PLC.readInt16(pointAddr);
  109. readText = read;
  110. }else if (s7Info.getDateType().equals("BOOL")){
  111. read = "" + s7PLC.readBoolean(pointAddr);
  112. if(!StringUtil.isNullOrEmpty(read))readText = read.equals("true")?"1":"0";
  113. }
  114. if (oConvertUtils.isEmpty(read)) {
  115. read = "";
  116. }
  117. //将点位数据存入点位信息对象中
  118. s7Info.setTestResult(read);
  119. //如果点位状态异常则恢复为正常
  120. if(s7Info.getGatherStatus().equals("1")){
  121. s7Info.setGatherStatus("0");
  122. }
  123. } catch (Exception e) {
  124. //打印点位异常日志
  125. log.error(e.getMessage());
  126. //设置点位状态为异常
  127. s7Info.setGatherStatus("1");
  128. }finally {
  129. //将点位数据存入mongo中
  130. mongoTemplate.insert(new PointData(s7Info, freq, readText, curentDate), s7Info.getId());
  131. //更新数据库点位信息
  132. devicePointService.updateById(s7Info);
  133. readText = "";
  134. }
  135. }
  136. } catch (Exception e) {
  137. }
  138. });
  139. }
  140. //关闭连接并从连接池中移除
  141. public void closeS7(String id){
  142. S7PLC s7PLC = s7PLCmap.get(id);
  143. if(!oConvertUtils.isEmpty(s7PLC)){
  144. s7PLC.close();
  145. s7PLCmap.remove(id);
  146. }
  147. }
  148. }