package com.spring.modules.cdc.task; import HslCommunication.Core.Types.OperateResult; import HslCommunication.Profinet.Melsec.MelsecMcNet; import HslCommunication.Profinet.Siemens.SiemensPLCS; import HslCommunication.Profinet.Siemens.SiemensS7Net; import com.spring.modules.cdc.entity.Collect; import com.spring.modules.cdc.entity.CollectDetail; import com.spring.modules.cdc.entity.CollectRecord; import com.spring.modules.cdc.entity.Device; import com.spring.modules.cdc.service.*; import com.spring.modules.cdc.utils.CollectUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** * 数据采集定时任务 */ @Component @Configuration @EnableScheduling @Slf4j public class GatherDataTask { @Value("${collect.data.line1}") private String line1IP; @Value("${collect.data.line2}") private String line2IP; @Value("${collect.data.line3}") private String line3IP; @Value("${collect.data.line1Value}") private int line1Value; @Value("${collect.data.line2Value}") private int line2Value; @Value("${collect.data.line3Value}") private int line3Value; @Value("${task.flag}") private boolean flag; private static final String BASE_NO = "last_send_time"; @Autowired private ICDCBaseDataService cdcBaseDataService; @Autowired private ICollectService collectService; @Autowired private IDeviceService deviceService; @Autowired private ICollectRecordService collectRecordService; @Autowired private ICollectDetailService collectDetailService; @Autowired private IMailConfigService mailConfigService; @Autowired private IMobileService mobileService; @Scheduled(cron = "${task.data.initGather}") public void execute() { if (!flag){ return; } // TODO: 这里可以写数据采集的逻辑 log.info("数据采集接口"); // 获取三种类型设备 List list = deviceService.lambdaQuery().eq(Device::getActive, "Y").list(); // 读取信息 每条线调用方式不同 // 1、涂布设备-大线 // 2、涂布设备-小线 // 3、配妆间设备 Map> map = list.stream().collect(Collectors.groupingBy(Device::getDeviceAddress)); for (String ip : map.keySet()) { if (ip.equals(line1IP)){ List deviceList = map.get(ip); SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200,ip); OperateResult connect = siemensS7Net.ConnectServer(); if (connect.IsSuccess) { log.info("IP:{} 连接成功!!!",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); int i = CollectUtils.readXMZValue(siemensS7Net, device.getDeviceIp()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i); CollectRecord collectRecord = getCollectRecord(device, c1, i); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } }else { log.info("IP:{} 连接失败",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line1Value); CollectRecord collectRecord = getCollectRecord(device, c1, line1Value); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } } }else if (ip.equals(line2IP)){ List deviceList = map.get(ip); MelsecMcNet melsecMcNet = new MelsecMcNet(ip, 1010); OperateResult connect = melsecMcNet.ConnectServer(); if (connect.IsSuccess) { log.info("IP:{} 连接成功!!!",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceIp()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i); CollectRecord collectRecord = getCollectRecord(device, c1, i); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } }else { log.info("IP:{} 连接失败",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line2Value); CollectRecord collectRecord = getCollectRecord(device, c1, line2Value); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } } }else if (ip.equals(line3IP)){ List deviceList = map.get(ip); MelsecMcNet melsecMcNet = new MelsecMcNet(ip, 1010); OperateResult connect = melsecMcNet.ConnectServer(); if (connect.IsSuccess) { log.info("IP:{} 连接成功!!!",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceIp()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i); CollectRecord collectRecord = getCollectRecord(device, c1, i); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } }else { log.info("IP:{} 连接失败",ip); for (Device device : deviceList) { List c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list(); // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress()); log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line3Value); CollectRecord collectRecord = getCollectRecord(device, c1, line3Value); collectRecordService.insertCollectRecord(collectRecord); collectRecordService.updateCollectCurrentValue(collectRecord); } } }else { log.info("不存在IP"); } } // 存储采集的数据 // 采集数据与设置数据对比 // Date lastSendTime = lineCollectList1.get(0).getLastSendTime(); // if (Objects.nonNull(lastSendTime)){ // log.info("最终时间:{}", lastSendTime.toLocaleString()); // // 校验短时间内是否发送给 邮件/短信 // int time = cdcBaseDataService.getLongCdcBaseData(BASE_NO); // lastSendTime.setTime(lastSendTime.getTime() + (long) time * 60 * 1000); // // 最后一次发送时间 与 当前时间比较 是否大于当前时间 // if (new Date().before(lastSendTime)){ // // 记录信息 距离最后一次发送时间 在规定范围内,不发送短信 // return; // } // } // // // 达到预警情况下,发送预警邮件,短信等 // List detailList = collectDetailService // .lambdaQuery() // .eq(CollectDetail::getCollectId, lineCollectList1.get(0).getCollectId()) // .eq(CollectDetail::getSite, lineCollectList1.get(0).getSite()) // .list(); // log.info("设置的 电话/邮箱 详情:{}", detailList); // // 处理结果集 // Map> map = detailList.stream().collect(Collectors.groupingBy(CollectDetail::getType)); // // 发送预警邮件,短信等 // for (String s : map.keySet()) { // if ("mail".equals(s)){ // // 发送邮件 // List mailList = map.get(s).stream().map(CollectDetail::getItemValue).collect(Collectors.toList()); // log.info("发送邮件:{}", mailList); // if (mailList.isEmpty()){ // continue; // } //// try { //// mailConfigService.send(mailList.toArray(new String[0]), "数据采集预警", "数据采集预警",1); //// } catch (MessagingException | UnsupportedEncodingException e) { //// log.error("发送邮件失败:{}", e.getMessage()); //// throw new RuntimeException(e); //// } // } // if ("phone".equals(s)){ // // 发送短信 //// List phoneList = map.get(s).stream().map(CollectDetail::getItemValue).collect(Collectors.toList()); //// log.info("发送短信:{}", phoneList); //// if (phoneList.isEmpty()){ //// continue; //// } //// String[] array = phoneList.toArray(new String[0]); //// mobileService.send(String.join(",", array), "【数据采集预警】数据采集预警"); // } // } } private static CollectRecord getCollectRecord(Device device, List c1, int i) { CollectRecord collectRecord = new CollectRecord(); collectRecord.setDeviceNo(device.getDeviceNo()); collectRecord.setCollectId(c1.get(0).getCollectId()); collectRecord.setItemValue(new BigDecimal(i)); collectRecord.setSite(device.getSite()); if (device.getDeviceType().contains("浓度")){ if (new BigDecimal(i).compareTo(c1.get(0).getMinValue()) < 0){ collectRecord.setWarnFlag("Y"); } if (c1.get(0).getMaxValue().compareTo(new BigDecimal(i)) < 0){ collectRecord.setWarnFlag("Y"); } }else { if (c1.get(0).getMaxValue().compareTo(new BigDecimal(i)) < 0){ collectRecord.setWarnFlag("Y"); } } return collectRecord; } }