package org.jeecg.modules.watch; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; 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.modbus.service.ModbusTcp; 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 modBusGatherWatch { @Autowired IDeviceInformationService deviceInformationService; @Autowired IDevicePointService devicePointService; @Autowired MongoTemplate mongoTemplate; @Scheduled(fixedDelay = 500) public void t1(){ modbus("500"); } @Scheduled(fixedDelay = 1000) public void t2(){ modbus("1000"); } @Scheduled(fixedDelay = 2000) public void t3(){ modbus("2000"); } @Scheduled(fixedDelay = 5000) public void t4(){ modbus("5000"); } @Scheduled(fixedDelay = 10000) public void t5(){ modbus("10000"); } @Scheduled(fixedDelay = 30000) public void t6(){ modbus("30000"); } @Scheduled(fixedDelay = 60000) public void t7(){ modbus("60000"); } //modbus数据采集 public void modbus(String freq){ //获取所有运行中设备 LambdaQueryWrapper modquery = new LambdaQueryWrapper().eq(DeviceInformation::getStatus, "0").eq(DeviceInformation::getFreq, freq); List modBusList = deviceInformationService.list(modquery); //获取当前时间 Date curentDate = new Date(); //遍历设备集合 modBusList.forEach(modBus -> { //与设备建立连接 ModbusTcp plc = new ModbusTcp(modBus.getModbusDeviceNum(), modBus.getDeviceIp(), Integer.valueOf(modBus.getDevicePort())); //未处理数据 String read = ""; //处理过的数据 String readText = ""; try { //获取该设备下的所有设备点位 List modBusPoints = devicePointService.list( new LambdaQueryWrapper().eq(DevicePoint::getDeviceId,modBus.getId())); //遍历设备点位集合 for (DevicePoint modBusPoint : modBusPoints) { try { //读取设备点位数据 read = "" + plc.readInt16(Integer.valueOf(modBusPoint.getPointAddr())); if (oConvertUtils.isEmpty(read)) { read = ""; } //处理boolean类型数据 true为1 false为0 readText = read.equals("true")?"1":read.equals("false")?"0":read; //将点位数据存入点位信息对象中 modBusPoint.setTestResult(read); //如果点位状态异常则恢复为正常 if(modBusPoint.getGatherStatus().equals(1)){ modBusPoint.setGatherStatus("0"); } } catch (Exception e) { //打印点位异常日志 log.error(e.getMessage()); //设置点位状态为异常 modBusPoint.setGatherStatus("1"); }finally { //将点位数据存入mongo中 mongoTemplate.insert(new PointData(modBusPoint, freq, readText , curentDate), modBusPoint.getId()); //更新数据库点位信息 devicePointService.updateById(modBusPoint); readText = ""; } } } catch (Exception e) { log.error(e.getMessage()); }finally { //关闭连接 plc.close(); //更新数据库设备信息 deviceInformationService.updateById(modBus); } }); } }