package org.jeecg.modules.watch; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; import org.eclipse.milo.opcua.sdk.client.OpcUaClient; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.common.entity.LogTopic; import org.jeecg.modules.common.enums.EDeviceInformationType; import org.jeecg.modules.dataRepository.entity.PointData; import org.jeecg.modules.device.entity.DeviceGatherService; import org.jeecg.modules.device.entity.DeviceInformation; import org.jeecg.modules.device.service.IDeviceGatherServiceService; import org.jeecg.modules.device.service.IDeviceInformationService; import org.jeecg.modules.deviceConn.opc.utils.OpcUaServerUtils; import org.jeecg.modules.deviceLog.service.IDeviceLogService; import org.jeecg.modules.devicePoint.entity.DevicePoint; import org.jeecg.modules.devicePoint.service.IDevicePointService; import org.jeecg.modules.events.entity.LeanEventsHost; import org.jeecg.modules.events.service.ILeanEventsHostService; import org.jeecg.modules.gatherData.entity.FpgGatherData; import org.jeecg.modules.gatherData.service.IFpgGatherDataService; import org.jeecg.modules.leanEventWarn.service.ILeanEventWarnInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.Arrays; import java.util.Date; import java.util.List; @Slf4j @EnableAsync @Component //@EnableScheduling public class OpcGatherWatch { @Autowired IDeviceInformationService deviceInformationService; @Autowired IDeviceGatherServiceService deviceGatherServiceService; @Autowired IDevicePointService devicePointService; @Autowired IFpgGatherDataService fpgGatherDataService; @Autowired IDeviceLogService deviceLogService; @Autowired MongoTemplate mongoTemplate; @Autowired RedisTemplate redisTemplate; @Autowired ILeanEventsHostService leanEventsHostService; @Autowired ILeanEventWarnInfoService leanEventWarnInfoService; // 采集频率设置值(数据库字典匹配,否则不执行查询不到设备) @Scheduled(fixedDelay = 500) public void t1(){ opc("500"); } // 0.5ms @Scheduled(fixedDelay = 1000) public void t2(){ opc("1000"); } // 1ms @Scheduled(fixedDelay = 2000) public void t3(){ opc("2000"); } @Scheduled(fixedDelay = 5000) public void t4(){ opc("5000"); } @Scheduled(fixedDelay = 10000) public void t5(){ opc("10000"); } @Scheduled(fixedDelay = 30000) public void t6(){ opc("30000"); } @Scheduled(fixedDelay = 60000) public void t7(){ opc("60000"); } //opc数据采集 public void opc(String freq){ // 获取所有运行中设备(峰平谷) LambdaQueryWrapper opcquery = new LambdaQueryWrapper().eq(DeviceGatherService::getStatus, "0").eq(DeviceGatherService::getFreq, freq); List opclist = deviceGatherServiceService.list(opcquery); //获取当前时间 Date curentDate = new Date(); //遍历设备集合 opclist.forEach(opcConn -> { //获取设备下所有点位 List opcPoints = devicePointService.list( new LambdaQueryWrapper().eq(DevicePoint::getDeviceId,opcConn.getId())); OpcUaClient opcUaClient = null; String readText = ""; try { //与设备建立连接 opcUaClient = OpcUaServerUtils.connectOpcUaServer(opcConn); //遍历设备下所有点位 for (DevicePoint opcPoint : opcPoints) { try { //读取点位数据 String nodeValue = OpcUaServerUtils.readNodeValue(opcUaClient, opcPoint.getNameindex(), opcPoint.getItemid()); if (oConvertUtils.isEmpty(nodeValue)) { nodeValue = ""; } //处理boolean类型数据 true为1 false为0 readText = nodeValue.equals("true")?"1":nodeValue.equals("false")?"0":nodeValue; //将点位数据存入点位信息对象中 opcPoint.setTestResult(nodeValue); //设置点位状态为正常 opcPoint.setGatherStatus("0"); //如果点位状态异常则恢复为正常 if(opcPoint.getGatherStatus().equals(1)){ opcPoint.setGatherStatus("0"); deviceLogService.save(LogTopic.OPC.READ_RECOVER,opcConn.getDeviceTitle(),opcConn.getDeviceIp(),opcPoint.getPointName()); } } catch (Exception e) { //打印点位异常日志 log.error(e.getMessage()); deviceLogService.save(LogTopic.OPC.READ_ERROR,opcConn.getDeviceTitle(),opcConn.getDeviceIp(),opcPoint.getPointName()); //设置点位状态为异常 opcPoint.setGatherStatus("1"); } finally { //将点位数据存入mongo中 mongoTemplate.insert(new PointData(opcPoint, freq, readText, curentDate), opcPoint.getId()); //更新数据库点位信息 devicePointService.updateById(opcPoint); // 存入采集设备 FpgGatherData fpgGatherData = new FpgGatherData(); fpgGatherData.setDeviceInformationId(opcConn.getId()); // 设备ID // 根据设备ID 查询设备信息,获取到设备所属的区域ID DeviceInformation deviceInformation = deviceInformationService.getById(opcPoint.getDeviceId()); fpgGatherData.setDeviceRegionId(deviceInformation.getDeviceRegionId()); // 区域id fpgGatherData.setDevicePointId(opcPoint.getId()); // 采集点ID fpgGatherData.setVoltage(new BigDecimal(10)); // 电压 fpgGatherData.setActivePower(new BigDecimal(10)); // 有功功率 fpgGatherData.setRunCurrent(new BigDecimal(10)); // 运行电流 fpgGatherData.setCreateTime(curentDate); // 创建时间 fpgGatherDataService.save(fpgGatherData); } } } catch (Exception e) { log.error(e.getMessage()); deviceLogService.save(LogTopic.OPC.CONNECTE_ERROR,opcConn.getDeviceTitle(),opcConn.getDeviceIp()); } finally { //更新数据库设备信息 deviceGatherServiceService.updateById(opcConn); //关闭连接 if(opcUaClient != null) { opcUaClient.disconnect(); } } }); } }