OpcGatherWatch.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.common.entity.LogTopic;
  7. import org.jeecg.modules.common.enums.EDeviceInformationType;
  8. import org.jeecg.modules.dataRepository.entity.PointData;
  9. import org.jeecg.modules.device.entity.DeviceGatherService;
  10. import org.jeecg.modules.device.entity.DeviceInformation;
  11. import org.jeecg.modules.device.service.IDeviceGatherServiceService;
  12. import org.jeecg.modules.device.service.IDeviceInformationService;
  13. import org.jeecg.modules.deviceConn.opc.utils.OpcUaServerUtils;
  14. import org.jeecg.modules.deviceLog.service.IDeviceLogService;
  15. import org.jeecg.modules.devicePoint.entity.DevicePoint;
  16. import org.jeecg.modules.devicePoint.service.IDevicePointService;
  17. import org.jeecg.modules.events.entity.LeanEventsHost;
  18. import org.jeecg.modules.events.service.ILeanEventsHostService;
  19. import org.jeecg.modules.gatherData.entity.FpgGatherData;
  20. import org.jeecg.modules.gatherData.service.IFpgGatherDataService;
  21. import org.jeecg.modules.leanEventWarn.service.ILeanEventWarnInfoService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.data.mongodb.core.MongoTemplate;
  24. import org.springframework.data.redis.core.RedisTemplate;
  25. import org.springframework.scheduling.annotation.EnableAsync;
  26. import org.springframework.scheduling.annotation.Scheduled;
  27. import org.springframework.stereotype.Component;
  28. import java.math.BigDecimal;
  29. import java.util.Arrays;
  30. import java.util.Date;
  31. import java.util.List;
  32. @Slf4j
  33. @EnableAsync
  34. @Component
  35. //@EnableScheduling
  36. public class OpcGatherWatch {
  37. @Autowired
  38. IDeviceInformationService deviceInformationService;
  39. @Autowired
  40. IDeviceGatherServiceService deviceGatherServiceService;
  41. @Autowired
  42. IDevicePointService devicePointService;
  43. @Autowired
  44. IFpgGatherDataService fpgGatherDataService;
  45. @Autowired
  46. IDeviceLogService deviceLogService;
  47. @Autowired
  48. MongoTemplate mongoTemplate;
  49. @Autowired
  50. RedisTemplate redisTemplate;
  51. @Autowired
  52. ILeanEventsHostService leanEventsHostService;
  53. @Autowired
  54. ILeanEventWarnInfoService leanEventWarnInfoService;
  55. // 采集频率设置值(数据库字典匹配,否则不执行查询不到设备)
  56. @Scheduled(fixedDelay = 500)
  57. public void t1(){
  58. opc("500");
  59. } // 0.5ms
  60. @Scheduled(fixedDelay = 1000)
  61. public void t2(){
  62. opc("1000");
  63. } // 1ms
  64. @Scheduled(fixedDelay = 2000)
  65. public void t3(){
  66. opc("2000");
  67. }
  68. @Scheduled(fixedDelay = 5000)
  69. public void t4(){
  70. opc("5000");
  71. }
  72. @Scheduled(fixedDelay = 10000)
  73. public void t5(){
  74. opc("10000");
  75. }
  76. @Scheduled(fixedDelay = 30000)
  77. public void t6(){
  78. opc("30000");
  79. }
  80. @Scheduled(fixedDelay = 60000)
  81. public void t7(){
  82. opc("60000");
  83. }
  84. //opc数据采集
  85. public void opc(String freq){
  86. // 获取所有运行中设备(峰平谷)
  87. LambdaQueryWrapper<DeviceGatherService> opcquery = new LambdaQueryWrapper<DeviceGatherService>().eq(DeviceGatherService::getStatus, "0").eq(DeviceGatherService::getFreq, freq);
  88. List<DeviceGatherService> opclist = deviceGatherServiceService.list(opcquery);
  89. //获取当前时间
  90. Date curentDate = new Date();
  91. //遍历设备集合
  92. opclist.forEach(opcConn -> {
  93. //获取设备下所有点位
  94. List<DevicePoint> opcPoints = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId,opcConn.getId()));
  95. OpcUaClient opcUaClient = null;
  96. String readText = "";
  97. try {
  98. //与设备建立连接
  99. opcUaClient = OpcUaServerUtils.connectOpcUaServer(opcConn);
  100. //遍历设备下所有点位
  101. for (DevicePoint opcPoint : opcPoints) {
  102. try {
  103. //读取点位数据
  104. String nodeValue = OpcUaServerUtils.readNodeValue(opcUaClient, opcPoint.getNameindex(), opcPoint.getItemid());
  105. if (oConvertUtils.isEmpty(nodeValue)) {
  106. nodeValue = "";
  107. }
  108. //处理boolean类型数据 true为1 false为0
  109. readText = nodeValue.equals("true")?"1":nodeValue.equals("false")?"0":nodeValue;
  110. //将点位数据存入点位信息对象中
  111. opcPoint.setTestResult(nodeValue);
  112. //设置点位状态为正常
  113. opcPoint.setGatherStatus("0");
  114. //如果点位状态异常则恢复为正常
  115. if(opcPoint.getGatherStatus().equals(1)){
  116. opcPoint.setGatherStatus("0");
  117. deviceLogService.save(LogTopic.OPC.READ_RECOVER,opcConn.getDeviceTitle(),opcConn.getDeviceIp(),opcPoint.getPointName());
  118. }
  119. } catch (Exception e) {
  120. //打印点位异常日志
  121. log.error(e.getMessage());
  122. deviceLogService.save(LogTopic.OPC.READ_ERROR,opcConn.getDeviceTitle(),opcConn.getDeviceIp(),opcPoint.getPointName());
  123. //设置点位状态为异常
  124. opcPoint.setGatherStatus("1");
  125. } finally {
  126. //将点位数据存入mongo中
  127. mongoTemplate.insert(new PointData(opcPoint, freq, readText, curentDate), opcPoint.getId());
  128. //更新数据库点位信息
  129. devicePointService.updateById(opcPoint);
  130. // 存入采集设备
  131. FpgGatherData fpgGatherData = new FpgGatherData();
  132. fpgGatherData.setDeviceInformationId(opcConn.getId()); // 设备ID
  133. // 根据设备ID 查询设备信息,获取到设备所属的区域ID
  134. DeviceInformation deviceInformation = deviceInformationService.getById(opcPoint.getDeviceId());
  135. fpgGatherData.setDeviceRegionId(deviceInformation.getDeviceRegionId()); // 区域id
  136. fpgGatherData.setDevicePointId(opcPoint.getId()); // 采集点ID
  137. fpgGatherData.setVoltage(new BigDecimal(10)); // 电压
  138. fpgGatherData.setActivePower(new BigDecimal(10)); // 有功功率
  139. fpgGatherData.setRunCurrent(new BigDecimal(10)); // 运行电流
  140. fpgGatherData.setCreateTime(curentDate); // 创建时间
  141. fpgGatherDataService.save(fpgGatherData);
  142. }
  143. }
  144. } catch (Exception e) {
  145. log.error(e.getMessage());
  146. deviceLogService.save(LogTopic.OPC.CONNECTE_ERROR,opcConn.getDeviceTitle(),opcConn.getDeviceIp());
  147. } finally {
  148. //更新数据库设备信息
  149. deviceGatherServiceService.updateById(opcConn);
  150. //关闭连接
  151. if(opcUaClient != null) {
  152. opcUaClient.disconnect();
  153. }
  154. }
  155. });
  156. }
  157. }