123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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.dataRepository.entity.PointData;
- import org.jeecg.modules.device.entity.DeviceInformation;
- import org.jeecg.modules.device.service.IDeviceInformationService;
- import org.jeecg.modules.deviceConn.opc.utils.OpcUaServerUtils;
- import org.jeecg.modules.devicePoint.entity.DevicePoint;
- import org.jeecg.modules.devicePoint.service.IDevicePointService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import org.springframework.scheduling.annotation.EnableAsync;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- import java.util.List;
- @Slf4j
- @EnableAsync
- @Component
- //@EnableScheduling
- public class OpcGatherWatch {
- @Autowired
- IDeviceInformationService deviceInformationService;
- @Autowired
- IDevicePointService devicePointService;
- @Autowired
- MongoTemplate mongoTemplate;
- // 采集频率设置值(数据库字典匹配,否则不执行查询不到设备)
- @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");
- }
- public void opc(String freq){
- //opc数据采集
- LambdaQueryWrapper<DeviceInformation> opcquery = new LambdaQueryWrapper<DeviceInformation>().eq(DeviceInformation::getStatus, "0").eq(DeviceInformation::getFreq, freq);
- List<DeviceInformation> opclist = deviceInformationService.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 = "";
- }
- readText = nodeValue.equals("true")?"1":nodeValue.equals("false")?"0":nodeValue;
- opcPoint.setTestResult(nodeValue);
- opcConn.setStatus("0");
- if(opcPoint.getGatherStatus().equals(1)){
- opcPoint.setGatherStatus("0");
- }
- } catch (Exception e) {
- log.error(e.getMessage());
- opcPoint.setGatherStatus("1");
- } finally {
- mongoTemplate.insert(new PointData(opcPoint, freq, readText, curentDate), opcPoint.getId());
- devicePointService.updateById(opcPoint);
- }
- }
- } catch (Exception e) {
- log.error(e.getMessage());
- } finally {
- deviceInformationService.updateById(opcConn);
- if(opcUaClient != null)opcUaClient.disconnect();
- }
- });
- }
- }
|