modBusGatherWatch.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package org.jeecg.modules.watch;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.jeecg.common.util.oConvertUtils;
  5. import org.jeecg.modules.dataRepository.entity.PointData;
  6. import org.jeecg.modules.device.entity.DeviceInformation;
  7. import org.jeecg.modules.device.service.IDeviceInformationService;
  8. import org.jeecg.modules.deviceConn.modbus.service.ModbusTcp;
  9. import org.jeecg.modules.devicePoint.entity.DevicePoint;
  10. import org.jeecg.modules.devicePoint.service.IDevicePointService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.mongodb.core.MongoTemplate;
  13. import org.springframework.scheduling.annotation.EnableAsync;
  14. import org.springframework.scheduling.annotation.EnableScheduling;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import org.springframework.stereotype.Component;
  17. import java.util.Date;
  18. import java.util.List;
  19. @Slf4j
  20. @EnableAsync
  21. @Component
  22. //@EnableScheduling
  23. public class modBusGatherWatch {
  24. @Autowired
  25. IDeviceInformationService deviceInformationService;
  26. @Autowired
  27. IDevicePointService devicePointService;
  28. @Autowired
  29. MongoTemplate mongoTemplate;
  30. @Scheduled(fixedDelay = 500)
  31. public void t1(){
  32. modbus("500");
  33. }
  34. @Scheduled(fixedDelay = 1000)
  35. public void t2(){
  36. modbus("1000");
  37. }
  38. @Scheduled(fixedDelay = 2000)
  39. public void t3(){
  40. modbus("2000");
  41. }
  42. @Scheduled(fixedDelay = 5000)
  43. public void t4(){
  44. modbus("5000");
  45. }
  46. @Scheduled(fixedDelay = 10000)
  47. public void t5(){
  48. modbus("10000");
  49. }
  50. @Scheduled(fixedDelay = 30000)
  51. public void t6(){
  52. modbus("30000");
  53. }
  54. @Scheduled(fixedDelay = 60000)
  55. public void t7(){
  56. modbus("60000");
  57. }
  58. //modbus数据采集
  59. public void modbus(String freq){
  60. //获取所有运行中设备
  61. LambdaQueryWrapper<DeviceInformation> modquery = new LambdaQueryWrapper<DeviceInformation>().eq(DeviceInformation::getStatus, "0").eq(DeviceInformation::getFreq, freq);
  62. List<DeviceInformation> modBusList = deviceInformationService.list(modquery);
  63. //获取当前时间
  64. Date curentDate = new Date();
  65. //遍历设备集合
  66. modBusList.forEach(modBus -> {
  67. //与设备建立连接
  68. ModbusTcp plc = new ModbusTcp(modBus.getModbusDeviceNum(), modBus.getDeviceIp(), Integer.valueOf(modBus.getDevicePort()));
  69. //未处理数据
  70. String read = "";
  71. //处理过的数据
  72. String readText = "";
  73. try {
  74. //获取该设备下的所有设备点位
  75. List<DevicePoint> modBusPoints = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId,modBus.getId()));
  76. //遍历设备点位集合
  77. for (DevicePoint modBusPoint : modBusPoints) {
  78. try {
  79. //读取设备点位数据
  80. read = "" + plc.readInt16(Integer.valueOf(modBusPoint.getPointAddr()));
  81. if (oConvertUtils.isEmpty(read)) {
  82. read = "";
  83. }
  84. //处理boolean类型数据 true为1 false为0
  85. readText = read.equals("true")?"1":read.equals("false")?"0":read;
  86. //将点位数据存入点位信息对象中
  87. modBusPoint.setTestResult(read);
  88. //如果点位状态异常则恢复为正常
  89. if(modBusPoint.getGatherStatus().equals(1)){
  90. modBusPoint.setGatherStatus("0");
  91. }
  92. } catch (Exception e) {
  93. //打印点位异常日志
  94. log.error(e.getMessage());
  95. //设置点位状态为异常
  96. modBusPoint.setGatherStatus("1");
  97. }finally {
  98. //将点位数据存入mongo中
  99. mongoTemplate.insert(new PointData(modBusPoint, freq, readText , curentDate), modBusPoint.getId());
  100. //更新数据库点位信息
  101. devicePointService.updateById(modBusPoint);
  102. readText = "";
  103. }
  104. }
  105. } catch (Exception e) {
  106. log.error(e.getMessage());
  107. }finally {
  108. //关闭连接
  109. plc.close();
  110. //更新数据库设备信息
  111. deviceInformationService.updateById(modBus);
  112. }
  113. });
  114. }
  115. }