You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
12 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.spring.modules.cdc.task;
  2. import HslCommunication.Core.Types.OperateResult;
  3. import HslCommunication.Profinet.Melsec.MelsecMcNet;
  4. import HslCommunication.Profinet.Siemens.SiemensPLCS;
  5. import HslCommunication.Profinet.Siemens.SiemensS7Net;
  6. import com.spring.modules.cdc.entity.Collect;
  7. import com.spring.modules.cdc.entity.CollectDetail;
  8. import com.spring.modules.cdc.entity.CollectRecord;
  9. import com.spring.modules.cdc.entity.Device;
  10. import com.spring.modules.cdc.service.*;
  11. import com.spring.modules.cdc.utils.CollectUtils;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.scheduling.annotation.EnableScheduling;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Component;
  19. import java.io.UnsupportedEncodingException;
  20. import java.math.BigDecimal;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.stream.Collectors;
  26. /**
  27. * 数据采集定时任务
  28. */
  29. @Component
  30. @Configuration
  31. @EnableScheduling
  32. @Slf4j
  33. public class GatherDataTask {
  34. @Value("${collect.data.line1}")
  35. private String line1IP;
  36. @Value("${collect.data.line2}")
  37. private String line2IP;
  38. @Value("${collect.data.line3}")
  39. private String line3IP;
  40. @Value("${collect.data.line1Value}")
  41. private int line1Value;
  42. @Value("${collect.data.line2Value}")
  43. private int line2Value;
  44. @Value("${collect.data.line3Value}")
  45. private int line3Value;
  46. @Value("${task.flag}")
  47. private boolean flag;
  48. private static final String BASE_NO = "last_send_time";
  49. @Autowired
  50. private ICDCBaseDataService cdcBaseDataService;
  51. @Autowired
  52. private ICollectService collectService;
  53. @Autowired
  54. private IDeviceService deviceService;
  55. @Autowired
  56. private ICollectRecordService collectRecordService;
  57. @Autowired
  58. private ICollectDetailService collectDetailService;
  59. @Autowired
  60. private IMailConfigService mailConfigService;
  61. @Autowired
  62. private IMobileService mobileService;
  63. @Scheduled(cron = "${task.data.initGather}")
  64. public void execute() {
  65. if (!flag){
  66. return;
  67. }
  68. // TODO: 这里可以写数据采集的逻辑
  69. log.info("数据采集接口");
  70. // 获取三种类型设备
  71. List<Device> list = deviceService.lambdaQuery().eq(Device::getActive, "Y").list();
  72. // 读取信息 每条线调用方式不同
  73. // 1、涂布设备-大线
  74. // 2、涂布设备-小线
  75. // 3、配妆间设备
  76. Map<String, List<Device>> map = list.stream().collect(Collectors.groupingBy(Device::getDeviceAddress));
  77. for (String ip : map.keySet()) {
  78. if (ip.equals(line1IP)){
  79. List<Device> deviceList = map.get(ip);
  80. SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200,ip);
  81. OperateResult connect = siemensS7Net.ConnectServer();
  82. if (connect.IsSuccess) {
  83. log.info("IP:{} 连接成功!!!",ip);
  84. for (Device device : deviceList) {
  85. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  86. int i = CollectUtils.readXMZValue(siemensS7Net, device.getDeviceIp());
  87. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i);
  88. CollectRecord collectRecord = getCollectRecord(device, c1, i);
  89. collectRecordService.insertCollectRecord(collectRecord);
  90. collectRecordService.updateCollectCurrentValue(collectRecord);
  91. }
  92. }else {
  93. log.info("IP:{} 连接失败",ip);
  94. for (Device device : deviceList) {
  95. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  96. // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress());
  97. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line1Value);
  98. CollectRecord collectRecord = getCollectRecord(device, c1, line1Value);
  99. collectRecordService.insertCollectRecord(collectRecord);
  100. collectRecordService.updateCollectCurrentValue(collectRecord);
  101. }
  102. }
  103. }else if (ip.equals(line2IP)){
  104. List<Device> deviceList = map.get(ip);
  105. MelsecMcNet melsecMcNet = new MelsecMcNet(ip, 1010);
  106. OperateResult connect = melsecMcNet.ConnectServer();
  107. if (connect.IsSuccess) {
  108. log.info("IP:{} 连接成功!!!",ip);
  109. for (Device device : deviceList) {
  110. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  111. int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceIp());
  112. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i);
  113. CollectRecord collectRecord = getCollectRecord(device, c1, i);
  114. collectRecordService.insertCollectRecord(collectRecord);
  115. collectRecordService.updateCollectCurrentValue(collectRecord);
  116. }
  117. }else {
  118. log.info("IP:{} 连接失败",ip);
  119. for (Device device : deviceList) {
  120. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  121. // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress());
  122. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line2Value);
  123. CollectRecord collectRecord = getCollectRecord(device, c1, line2Value);
  124. collectRecordService.insertCollectRecord(collectRecord);
  125. collectRecordService.updateCollectCurrentValue(collectRecord);
  126. }
  127. }
  128. }else if (ip.equals(line3IP)){
  129. List<Device> deviceList = map.get(ip);
  130. MelsecMcNet melsecMcNet = new MelsecMcNet(ip, 1010);
  131. OperateResult connect = melsecMcNet.ConnectServer();
  132. if (connect.IsSuccess) {
  133. log.info("IP:{} 连接成功!!!",ip);
  134. for (Device device : deviceList) {
  135. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  136. int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceIp());
  137. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),i);
  138. CollectRecord collectRecord = getCollectRecord(device, c1, i);
  139. collectRecordService.insertCollectRecord(collectRecord);
  140. collectRecordService.updateCollectCurrentValue(collectRecord);
  141. }
  142. }else {
  143. log.info("IP:{} 连接失败",ip);
  144. for (Device device : deviceList) {
  145. List<Collect> c1 = collectService.lambdaQuery().in(Collect::getCollectDesc, device.getDeviceType()).eq(Collect::getActive, "Y").list();
  146. // int i = CollectUtils.readSLValue(melsecMcNet, device.getDeviceAddress());
  147. log.info("设备:{} 读取值:{}",device.getDeviceDesc(),line3Value);
  148. CollectRecord collectRecord = getCollectRecord(device, c1, line3Value);
  149. collectRecordService.insertCollectRecord(collectRecord);
  150. collectRecordService.updateCollectCurrentValue(collectRecord);
  151. }
  152. }
  153. }else {
  154. log.info("不存在IP");
  155. }
  156. }
  157. // 存储采集的数据
  158. // 采集数据与设置数据对比
  159. // Date lastSendTime = lineCollectList1.get(0).getLastSendTime();
  160. // if (Objects.nonNull(lastSendTime)){
  161. // log.info("最终时间:{}", lastSendTime.toLocaleString());
  162. // // 校验短时间内是否发送给 邮件/短信
  163. // int time = cdcBaseDataService.getLongCdcBaseData(BASE_NO);
  164. // lastSendTime.setTime(lastSendTime.getTime() + (long) time * 60 * 1000);
  165. // // 最后一次发送时间 与 当前时间比较 是否大于当前时间
  166. // if (new Date().before(lastSendTime)){
  167. // // 记录信息 距离最后一次发送时间 在规定范围内,不发送短信
  168. // return;
  169. // }
  170. // }
  171. //
  172. // // 达到预警情况下,发送预警邮件,短信等
  173. // List<CollectDetail> detailList = collectDetailService
  174. // .lambdaQuery()
  175. // .eq(CollectDetail::getCollectId, lineCollectList1.get(0).getCollectId())
  176. // .eq(CollectDetail::getSite, lineCollectList1.get(0).getSite())
  177. // .list();
  178. // log.info("设置的 电话/邮箱 详情:{}", detailList);
  179. // // 处理结果集
  180. // Map<String, List<CollectDetail>> map = detailList.stream().collect(Collectors.groupingBy(CollectDetail::getType));
  181. // // 发送预警邮件,短信等
  182. // for (String s : map.keySet()) {
  183. // if ("mail".equals(s)){
  184. // // 发送邮件
  185. // List<String> mailList = map.get(s).stream().map(CollectDetail::getItemValue).collect(Collectors.toList());
  186. // log.info("发送邮件:{}", mailList);
  187. // if (mailList.isEmpty()){
  188. // continue;
  189. // }
  190. //// try {
  191. //// mailConfigService.send(mailList.toArray(new String[0]), "数据采集预警", "数据采集预警",1);
  192. //// } catch (MessagingException | UnsupportedEncodingException e) {
  193. //// log.error("发送邮件失败:{}", e.getMessage());
  194. //// throw new RuntimeException(e);
  195. //// }
  196. // }
  197. // if ("phone".equals(s)){
  198. // // 发送短信
  199. //// List<String> phoneList = map.get(s).stream().map(CollectDetail::getItemValue).collect(Collectors.toList());
  200. //// log.info("发送短信:{}", phoneList);
  201. //// if (phoneList.isEmpty()){
  202. //// continue;
  203. //// }
  204. //// String[] array = phoneList.toArray(new String[0]);
  205. //// mobileService.send(String.join(",", array), "【数据采集预警】数据采集预警");
  206. // }
  207. // }
  208. }
  209. private static CollectRecord getCollectRecord(Device device, List<Collect> c1, int i) {
  210. CollectRecord collectRecord = new CollectRecord();
  211. collectRecord.setDeviceNo(device.getDeviceNo());
  212. collectRecord.setCollectId(c1.get(0).getCollectId());
  213. collectRecord.setItemValue(new BigDecimal(i));
  214. collectRecord.setSite(device.getSite());
  215. if (device.getDeviceType().contains("浓度")){
  216. if (new BigDecimal(i).compareTo(c1.get(0).getMinValue()) < 0){
  217. collectRecord.setWarnFlag("Y");
  218. }
  219. if (c1.get(0).getMaxValue().compareTo(new BigDecimal(i)) < 0){
  220. collectRecord.setWarnFlag("Y");
  221. }
  222. }else {
  223. if (c1.get(0).getMaxValue().compareTo(new BigDecimal(i)) < 0){
  224. collectRecord.setWarnFlag("Y");
  225. }
  226. }
  227. return collectRecord;
  228. }
  229. }