123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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<DeviceGatherService> opcquery = new LambdaQueryWrapper<DeviceGatherService>().eq(DeviceGatherService::getStatus, "0").eq(DeviceGatherService::getFreq, freq);
- List<DeviceGatherService> opclist = deviceGatherServiceService.list(opcquery);
- //获取当前时间
- Date curentDate = new Date();
- //遍历设备集合
- opclist.forEach(opcConn -> {
- //获取设备下所有点位
- List<DevicePoint> opcPoints = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().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();
- }
- }
- });
- }
- }
|