OpcGatherWatch.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package org.jeecg.modules.watch;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
  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.opc.utils.OpcUaServerUtils;
  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.List;
  20. @Slf4j
  21. @EnableAsync
  22. @Component
  23. //@EnableScheduling
  24. public class OpcGatherWatch {
  25. @Autowired
  26. IDeviceInformationService deviceInformationService;
  27. @Autowired
  28. IDevicePointService devicePointService;
  29. @Autowired
  30. MongoTemplate mongoTemplate;
  31. // 采集频率设置值(数据库字典匹配,否则不执行查询不到设备)
  32. @Scheduled(fixedDelay = 500)
  33. public void t1(){
  34. opc("500");
  35. } // 0.5ms
  36. @Scheduled(fixedDelay = 1000)
  37. public void t2(){
  38. opc("1000");
  39. } // 1ms
  40. @Scheduled(fixedDelay = 2000)
  41. public void t3(){
  42. opc("2000");
  43. }
  44. @Scheduled(fixedDelay = 5000)
  45. public void t4(){
  46. opc("5000");
  47. }
  48. @Scheduled(fixedDelay = 10000)
  49. public void t5(){
  50. opc("10000");
  51. }
  52. @Scheduled(fixedDelay = 30000)
  53. public void t6(){
  54. opc("30000");
  55. }
  56. @Scheduled(fixedDelay = 60000)
  57. public void t7(){
  58. opc("60000");
  59. }
  60. public void opc(String freq){
  61. //opc数据采集
  62. LambdaQueryWrapper<DeviceInformation> opcquery = new LambdaQueryWrapper<DeviceInformation>().eq(DeviceInformation::getStatus, "0").eq(DeviceInformation::getFreq, freq);
  63. List<DeviceInformation> opclist = deviceInformationService.list(opcquery);
  64. Date curentDate = new Date();
  65. opclist.forEach(opcConn -> {
  66. List<DevicePoint> opcPoints = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId,opcConn.getId()));
  67. OpcUaClient opcUaClient = null;
  68. String readText = "";
  69. try {
  70. opcUaClient = OpcUaServerUtils.connectOpcUaServer(opcConn);
  71. //主动获取
  72. for (DevicePoint opcPoint : opcPoints) {
  73. try {
  74. String nodeValue = OpcUaServerUtils.readNodeValue(opcUaClient, opcPoint.getNameindex(), opcPoint.getItemid());
  75. if (oConvertUtils.isEmpty(nodeValue)) {
  76. nodeValue = "";
  77. }
  78. readText = nodeValue.equals("true")?"1":nodeValue.equals("false")?"0":nodeValue;
  79. opcPoint.setTestResult(nodeValue);
  80. opcConn.setStatus("0");
  81. if(opcPoint.getGatherStatus().equals(1)){
  82. opcPoint.setGatherStatus("0");
  83. }
  84. } catch (Exception e) {
  85. log.error(e.getMessage());
  86. opcPoint.setGatherStatus("1");
  87. } finally {
  88. mongoTemplate.insert(new PointData(opcPoint, freq, readText, curentDate), opcPoint.getId());
  89. devicePointService.updateById(opcPoint);
  90. }
  91. }
  92. } catch (Exception e) {
  93. log.error(e.getMessage());
  94. } finally {
  95. deviceInformationService.updateById(opcConn);
  96. if(opcUaClient != null)opcUaClient.disconnect();
  97. }
  98. });
  99. }
  100. }