From 4cb222a3126fca75a9034a8dcde5e657e49952ae Mon Sep 17 00:00:00 2001 From: zelian_wu Date: Fri, 14 Jun 2024 11:43:57 +0800 Subject: [PATCH] 2024-06-14 --- pom.xml | 5 + .../controller/CollectRecordController.java | 3 +- .../cdc/mapper/CollectRecordMapper.java | 2 + .../cdc/service/ICollectRecordService.java | 2 + .../impl/CollectRecordServiceImpl.java | 5 + .../modules/cdc/task/GatherDataTask.java | 243 +++++++++++++----- .../modules/cdc/utils/CollectUtils.java | 33 +++ src/main/resources/application-dev.yml | 15 +- .../mapper/cdc/collectRecordMapper.xml | 23 +- 9 files changed, 261 insertions(+), 70 deletions(-) create mode 100644 src/main/java/com/spring/modules/cdc/utils/CollectUtils.java diff --git a/pom.xml b/pom.xml index 0b0fb9da..548f2f98 100644 --- a/pom.xml +++ b/pom.xml @@ -322,6 +322,11 @@ org.springframework.boot spring-boot-starter-mail + + com.github.dathlin + HslCommunication + 3.7.0 + diff --git a/src/main/java/com/spring/modules/cdc/controller/CollectRecordController.java b/src/main/java/com/spring/modules/cdc/controller/CollectRecordController.java index 3e13ebb6..ca78690d 100644 --- a/src/main/java/com/spring/modules/cdc/controller/CollectRecordController.java +++ b/src/main/java/com/spring/modules/cdc/controller/CollectRecordController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Date; import java.util.List; @RestController @@ -29,7 +30,7 @@ public class CollectRecordController { @PostMapping("/list/item") public R listItem(@RequestBody CollectRecord collectRecord){ List list = collectRecordService.selectCollectRecordListByItemNo(collectRecord); - return R.ok().put("rows", list); + return R.ok().put("rows", list).put("time",new Date().getTime()); } @PostMapping("/save") diff --git a/src/main/java/com/spring/modules/cdc/mapper/CollectRecordMapper.java b/src/main/java/com/spring/modules/cdc/mapper/CollectRecordMapper.java index 09354749..504707a7 100644 --- a/src/main/java/com/spring/modules/cdc/mapper/CollectRecordMapper.java +++ b/src/main/java/com/spring/modules/cdc/mapper/CollectRecordMapper.java @@ -12,4 +12,6 @@ public interface CollectRecordMapper extends BaseMapper { List selectCollectRecordList(CollectRecord collectRecord); List selectCollectRecordListByItemNo(CollectRecord collectRecord); + + void updateCollectCurrentValue(CollectRecord collectRecord); } diff --git a/src/main/java/com/spring/modules/cdc/service/ICollectRecordService.java b/src/main/java/com/spring/modules/cdc/service/ICollectRecordService.java index 23e37207..9c093058 100644 --- a/src/main/java/com/spring/modules/cdc/service/ICollectRecordService.java +++ b/src/main/java/com/spring/modules/cdc/service/ICollectRecordService.java @@ -15,4 +15,6 @@ public interface ICollectRecordService extends IService { void updateCollectRecord(CollectRecord collectRecord); void deleteCollectRecord(CollectRecord collectRecord); + + void updateCollectCurrentValue(CollectRecord collectRecord); } diff --git a/src/main/java/com/spring/modules/cdc/service/impl/CollectRecordServiceImpl.java b/src/main/java/com/spring/modules/cdc/service/impl/CollectRecordServiceImpl.java index 9c39e09e..70dd3632 100644 --- a/src/main/java/com/spring/modules/cdc/service/impl/CollectRecordServiceImpl.java +++ b/src/main/java/com/spring/modules/cdc/service/impl/CollectRecordServiceImpl.java @@ -56,6 +56,11 @@ public class CollectRecordServiceImpl extends ServiceImpl lineDeviceList1 = deviceService.lambdaQuery().like(Device::getDeviceType, Line1).eq(Device::getActive, "Y").list(); - List lineDeviceList2 = deviceService.lambdaQuery().like(Device::getDeviceType, Line2).eq(Device::getActive, "Y").list(); - List lineDeviceList3 = deviceService.lambdaQuery().like(Device::getDeviceType, Line3).eq(Device::getActive, "Y").list(); - - // 获取设备数值 报警信息 - List lineCollectList1 = collectService.lambdaQuery().like(Collect::getCollectDesc, Line1).eq(Collect::getActive, "Y").list(); - List lineCollectGCList1 = collectService.lambdaQuery().like(Collect::getCollectDesc, LineGC1).eq(Collect::getActive, "Y").list(); - List lineCollectList2 = collectService.lambdaQuery().like(Collect::getCollectDesc, Line2).eq(Collect::getActive, "Y").list(); - List lineCollectGCList2 = collectService.lambdaQuery().like(Collect::getCollectDesc, LineGC2).eq(Collect::getActive, "Y").list(); - List lineCollectList3 = collectService.lambdaQuery().like(Collect::getCollectDesc, Line3).eq(Collect::getActive, "Y").list(); - + 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.getDeviceAddress()); + 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.getDeviceAddress()); + 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.getDeviceAddress()); + 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()){ +// 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; // } -// String[] array = phoneList.toArray(new String[0]); -// mobileService.send(String.join(",", array), "【数据采集预警】数据采集预警"); +//// 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; } } diff --git a/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java b/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java new file mode 100644 index 00000000..bf8adbb0 --- /dev/null +++ b/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java @@ -0,0 +1,33 @@ +package com.spring.modules.cdc.utils; + + +import HslCommunication.Core.Types.OperateResultExOne; +import HslCommunication.Profinet.Melsec.MelsecMcNet; +import HslCommunication.Profinet.Siemens.SiemensS7Net; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class CollectUtils { + + public static int readSLValue(MelsecMcNet melsecMcNet, String address) { + OperateResultExOne readResult = melsecMcNet.ReadInt16(address); + if (readResult.IsSuccess) { + int value = readResult.Content; + return value; + } else { + System.out.println("读取失败: " + readResult.ToMessageShowString()); + return -1; + } + } + + public static int readXMZValue(SiemensS7Net siemensS7Net, String address) { + OperateResultExOne readResult = siemensS7Net.ReadInt16(address); + if (readResult.IsSuccess) { + int value = readResult.Content; + return value; + } else { + System.out.println("读取失败: " + readResult.ToMessageShowString()); + return -1; + } + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 88ad7864..1de368cc 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -75,7 +75,18 @@ ccl-sms: # 定时任务 task: data: - initGather: 0 * * * * ? # 每分钟执行 + initGather: 0/10 * * * * ? # 每分钟执行 +# initGather: * * * 31 2 ? # 每分钟执行 + flag: false # 单点登录 -login-token-url: 'http://192.168.1.160:8881/#/login-token' \ No newline at end of file +login-token-url: 'http://192.168.1.160:8881/#/login-token' + +collect: + data: + line1: 192.168.3.2 + line2: 192.168.3.39 + line3: 1 + line1Value: 0 + line2Value: 0 + line3Value: 0 \ No newline at end of file diff --git a/src/main/resources/mapper/cdc/collectRecordMapper.xml b/src/main/resources/mapper/cdc/collectRecordMapper.xml index 9e6c53d2..b560a3ac 100644 --- a/src/main/resources/mapper/cdc/collectRecordMapper.xml +++ b/src/main/resources/mapper/cdc/collectRecordMapper.xml @@ -24,6 +24,22 @@ left join cdc_device_info di on cr.device_no = di.device_no and di.site = cr.site left join cdc_collect_data cd on cr.collect_id = cd.collect_id and cr.site = cd.site + + select + cr.device_no, + di.device_desc, + di.device_type, + cr.collect_id, + cd.collect_desc, + cr.item_value, + cr.create_time, + cr.site, + cd.max_value, + cd.min_value + from cdc_collect_current_value cr + left join cdc_device_info di on cr.device_no = di.device_no and di.site = cr.site + left join cdc_collect_data cd on cr.collect_id = cd.collect_id and cr.site = cd.site + @@ -52,9 +68,8 @@ + + + update cdc_collect_current_value set item_value = #{itemValue}, create_time = #{createTime} where device_no = #{deviceNo} and collect_id = #{collectId} and site = #{site} + \ No newline at end of file