ProductTjScheduling.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package com.jeesite.modules.scheduling;
  2. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  3. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  4. import com.jeesite.common.config.Global;
  5. import com.jeesite.common.lang.ObjectUtils;
  6. import com.jeesite.common.lang.StringUtils;
  7. import com.jeesite.modules.mes.entity.MesLine;
  8. import com.jeesite.modules.mes.entity.MesProduct;
  9. import com.jeesite.modules.mes.entity.MesProductRecord;
  10. import com.jeesite.modules.mes.entity.MesProductTj;
  11. import com.jeesite.modules.mes.resp.BindMaterialResp;
  12. import com.jeesite.modules.mes.resp.ParamsResp;
  13. import com.jeesite.modules.mes.service.MesLineService;
  14. import com.jeesite.modules.mes.service.MesProductRecordService;
  15. import com.jeesite.modules.mes.service.MesProductService;
  16. import com.jeesite.modules.mes.service.MesProductTjService;
  17. import com.jeesite.modules.mes.util.CommonUitl;
  18. import com.jeesite.modules.mes.web.MesProductRecordController;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.context.annotation.Configuration;
  23. import org.springframework.scheduling.annotation.EnableScheduling;
  24. import org.springframework.scheduling.annotation.Scheduled;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.concurrent.atomic.AtomicBoolean;
  28. /**
  29. * 涂胶 S7 采集与质量握手(OP220,固定工位号)。
  30. * <pre>
  31. * DB1300 点位:
  32. * 0.0 框架码 String[30]
  33. * 64.0 扫码反馈信号
  34. * 64.2 质量校验通过反馈(校验通过 MES 写 true,工位完成后 MES 写 false)
  35. * 64.4 涂胶数据采集触发
  36. * 64.5 整体完成信号(true 时提交工位 OK 并回查确认落库,成功后将 64.2 置 false)
  37. * 66.0 涂胶序号 Word
  38. * 68.0 涂胶速度 Real
  39. * 72.0 涂胶长度 Word
  40. *
  41. * 配置(application.yml):
  42. * mes.tj.enabled 是否启用(默认 false)
  43. * mes.tj.plc.ip PLC IP,默认 10.0.40.100
  44. * mes.tj.lineSn 产线编号,默认 XT
  45. * </pre>
  46. */
  47. @Configuration
  48. @EnableScheduling
  49. public class ProductTjScheduling {
  50. private static final Logger logger = LoggerFactory.getLogger(ProductTjScheduling.class);
  51. private static final String ADDR_SN = "DB1300.0.0";
  52. private static final int SN_LENGTH = 30;
  53. private static final String ADDR_SCAN_FEEDBACK = "DB1300.64.0";
  54. private static final String ADDR_QUALITY_PASS = "DB1300.64.2";
  55. private static final String ADDR_GLUE_COLLECT = "DB1300.64.4";
  56. private static final String ADDR_COMPLETE = "DB1300.64.5";
  57. private static final String ADDR_GLUE_SEQ = "DB1300.66.0";
  58. private static final String ADDR_GLUE_SPEED = "DB1300.68.0";
  59. private static final String ADDR_GLUE_LENGTH = "DB1300.72.0";
  60. private static final String DEFAULT_PLC_IP = "10.0.40.100";
  61. private static final String OPRNO = "OP220";
  62. private static final String DEFAULT_LINE_SN = "XT";
  63. @Autowired
  64. private MesProductRecordController mesProductRecordController;
  65. @Autowired
  66. private MesProductService mesProductService;
  67. @Autowired
  68. private MesLineService mesLineService;
  69. @Autowired
  70. private MesProductTjService mesProductTjService;
  71. @Autowired
  72. private MesProductRecordService mesProductRecordService;
  73. private final AtomicBoolean running = new AtomicBoolean(false);
  74. private volatile String lastKnownSn;
  75. private volatile String lastCompletedSn;
  76. @Scheduled(fixedRate = 100)
  77. public void collect() {
  78. String enabled = Global.getConfig("mes.tj.enabled");
  79. if (StringUtils.isBlank(enabled) || !"true".equalsIgnoreCase(enabled.trim())) {
  80. return;
  81. }
  82. runOnce();
  83. }
  84. /**
  85. * 直接执行一轮(供测试调用)
  86. */
  87. public void runOnce() {
  88. if (!running.compareAndSet(false, true)) {
  89. return;
  90. }
  91. String ip = getPlcIp();
  92. S7PLC s7PLC = null;
  93. try {
  94. s7PLC = new S7PLC(EPlcType.S1200, ip);
  95. handleScanFeedback(s7PLC);
  96. handleGlueCollect(s7PLC);
  97. handleComplete(s7PLC);
  98. } catch (Exception e) {
  99. logger.error("涂胶S7任务失败[{}]:{}", ip, e.getMessage());
  100. } finally {
  101. if (s7PLC != null) {
  102. try {
  103. s7PLC.close();
  104. } catch (Exception ignore) {
  105. }
  106. }
  107. running.set(false);
  108. }
  109. }
  110. /**
  111. * 扫码反馈为 true:读框架码 → 质量校验 → 通过则写 DB1300.64.2=true
  112. */
  113. private void handleScanFeedback(S7PLC s7PLC) {
  114. if (!s7PLC.readBoolean(ADDR_SCAN_FEEDBACK)) {
  115. lastCompletedSn = null;
  116. return;
  117. }
  118. String sn = readWorkpieceCode(s7PLC);
  119. if (StringUtils.isBlank(sn)) {
  120. return;
  121. }
  122. lastKnownSn = sn;
  123. boolean pass = doQualityCheck(sn);
  124. if (pass) {
  125. s7PLC.writeBoolean(ADDR_QUALITY_PASS, true);
  126. logger.info("涂胶质量校验通过 sn={},已写 {}=true", sn, ADDR_QUALITY_PASS);
  127. } else {
  128. logger.warn("涂胶质量校验未通过 sn={}", sn);
  129. }
  130. }
  131. /**
  132. * 涂胶采集触发为 true:读涂胶参数 → 落库 mes_product_tj → 清触发位
  133. */
  134. private void handleGlueCollect(S7PLC s7PLC) {
  135. if (!s7PLC.readBoolean(ADDR_GLUE_COLLECT)) {
  136. return;
  137. }
  138. String sn = readWorkpieceCode(s7PLC);
  139. if (StringUtils.isBlank(sn)) {
  140. sn = lastKnownSn;
  141. }
  142. if (StringUtils.isBlank(sn)) {
  143. logger.error("涂胶数据采集失败:无工件码,保持 {}=true 待重试", ADDR_GLUE_COLLECT);
  144. return;
  145. }
  146. lastKnownSn = sn;
  147. int glueSeq = s7PLC.readUInt16(ADDR_GLUE_SEQ);
  148. float glueSpeed = s7PLC.readFloat32(ADDR_GLUE_SPEED);
  149. int glueLength = s7PLC.readUInt16(ADDR_GLUE_LENGTH);
  150. logger.info("涂胶数据采集 sn={} 序号={} 速度={} 长度={}", sn, glueSeq, glueSpeed, glueLength);
  151. if (!saveTjData(sn, glueSeq, glueSpeed, glueLength)) {
  152. logger.error("涂胶数据保存失败,保持 {}=true 待重试 sn={}", ADDR_GLUE_COLLECT, sn);
  153. return;
  154. }
  155. s7PLC.writeBoolean(ADDR_GLUE_COLLECT, false);
  156. logger.info("涂胶数据采集完成 sn={} 序号={},已将 {} 置 false", sn, glueSeq, ADDR_GLUE_COLLECT);
  157. }
  158. /**
  159. * DB1300.64.5 整体完成信号为 true:提交工位 OK → 成功后将 DB1300.64.2 置 false
  160. */
  161. private void handleComplete(S7PLC s7PLC) {
  162. if (!s7PLC.readBoolean(ADDR_COMPLETE)) {
  163. return;
  164. }
  165. String sn = readWorkpieceCode(s7PLC);
  166. if (StringUtils.isBlank(sn)) {
  167. sn = lastKnownSn;
  168. }
  169. if (StringUtils.isBlank(sn)) {
  170. logger.error("涂胶工位完成提交失败:无工件码,保持 {}=true 待重试", ADDR_COMPLETE);
  171. return;
  172. }
  173. if (sn.equals(lastCompletedSn)) {
  174. return;
  175. }
  176. if (!submitStationResult(sn, "OK")) {
  177. logger.error("涂胶工位结果提交失败,保持信号待重试 sn={}", sn);
  178. return;
  179. }
  180. s7PLC.writeBoolean(ADDR_QUALITY_PASS, false);
  181. lastCompletedSn = sn;
  182. logger.info("涂胶工位完成 sn={},已提交 OK 并将 {} 置 false", sn, ADDR_QUALITY_PASS);
  183. }
  184. private boolean saveTjData(String sn, int glueSeq, float glueSpeed, int glueLength) {
  185. try {
  186. MesProductTj entity = new MesProductTj();
  187. entity.setSn(sn);
  188. entity.setOprno(OPRNO);
  189. entity.setTjData(String.valueOf(glueSeq));
  190. entity.setTjSpeed(fmt(glueSpeed));
  191. entity.setTjLength(String.valueOf(glueLength));
  192. mesProductTjService.save(entity);
  193. return true;
  194. } catch (Exception e) {
  195. logger.error("涂胶数据保存异常 sn={}:{}", sn, e.getMessage());
  196. return false;
  197. }
  198. }
  199. /**
  200. * 直接提交工位结果(供测试 / 手工触发),不读写 PLC 信号。
  201. * 逻辑与 64.5 完成时的 submitStationResult 相同(含回查与强制落库)。
  202. *
  203. * @param sn 工件码
  204. * @param result 结果,通常为 OK
  205. * @return true=craft=100000 已确认为目标结果
  206. */
  207. public boolean submitStationResultNow(String sn, String result) {
  208. if (StringUtils.isBlank(sn)) {
  209. logger.error("涂胶工位结果提交跳过:工件码为空");
  210. return false;
  211. }
  212. return submitStationResult(sn.trim(), StringUtils.isBlank(result) ? "OK" : result.trim());
  213. }
  214. /**
  215. * 提交工位结果为 OK,并回查 craft=100000 确认已落库。
  216. * updateRecordResult 在单部件工位或缺少总结果行时可能静默跳过,此时改走 updateRecord 强制更新。
  217. */
  218. private boolean submitStationResult(String sn, String result) {
  219. String oldOprno = OPRNO + "A";
  220. String lineSn = getLineSn(sn);
  221. if (StringUtils.isBlank(lineSn)) {
  222. logger.warn("涂胶工位结果提交失败:无法解析产线编号 sn={}", sn);
  223. return false;
  224. }
  225. try {
  226. List<ParamsResp> params = new ArrayList<>();
  227. List<BindMaterialResp> bmlists = new ArrayList<>();
  228. mesProductRecordService.updateRecordResult(sn, oldOprno, "100000", "", result,
  229. "", params, lineSn, true, bmlists, null);
  230. if (isStationMainResult(sn, lineSn, result)) {
  231. logger.info("涂胶工位结果已提交 sn={} oprno={} result={}", sn, oldOprno, result);
  232. return true;
  233. }
  234. logger.warn("updateRecordResult 未真正更新 craft=100000,改为 updateRecord 强制提交 sn={} oprno={}",
  235. sn, oldOprno);
  236. mesProductRecordService.updateRecord(sn, oldOprno, "100000", "", result, "", lineSn);
  237. if (!isStationMainResult(sn, lineSn, result)) {
  238. logger.error("涂胶工位结果强制提交后仍未落库 sn={} oprno={} lineSn={}", sn, oldOprno, lineSn);
  239. return false;
  240. }
  241. logger.info("涂胶工位结果已强制提交 sn={} oprno={} result={}", sn, oldOprno, result);
  242. return true;
  243. } catch (Exception e) {
  244. logger.error("涂胶工位结果提交异常 sn={}:{}", sn, e.getMessage());
  245. return false;
  246. }
  247. }
  248. /** 回查工位总结果行 craft=100000 是否已为指定结果 */
  249. private boolean isStationMainResult(String sn, String lineSn, String expectContent) {
  250. String fOprno = CommonUitl.formatOprno(OPRNO + "A");
  251. MesProductRecord query = new MesProductRecord();
  252. query.setSn(sn);
  253. query.setOprno(fOprno);
  254. query.setCraft("100000");
  255. query.setLineSn(lineSn);
  256. List<MesProductRecord> list = mesProductRecordService.findList(query);
  257. if (list == null || list.isEmpty()) {
  258. return false;
  259. }
  260. return expectContent.equals(list.get(0).getContent());
  261. }
  262. private boolean doQualityCheck(String sn) {
  263. String oldOprno = OPRNO + "A";
  264. String lineSn = getLineSn(sn);
  265. if (StringUtils.isBlank(lineSn)) {
  266. logger.warn("涂胶质量校验失败:无法解析产线编号 sn={}", sn);
  267. return false;
  268. }
  269. try {
  270. String ret = mesProductRecordController.checkQualityCommon(oldOprno, sn, lineSn, "");
  271. if (StringUtils.isBlank(ret) || ret.length() < 5) {
  272. return false;
  273. }
  274. return "UD".equals(ret.substring(3, 5));
  275. } catch (Exception e) {
  276. logger.error("涂胶质量校验异常 sn={}:{}", sn, e.getMessage());
  277. return false;
  278. }
  279. }
  280. private String getLineSn(String sn) {
  281. String lineSn = Global.getConfig("mes.tj.lineSn");
  282. if (StringUtils.isNotBlank(lineSn)) {
  283. return lineSn.trim();
  284. }
  285. MesProduct product = mesProductService.findBySn(sn);
  286. if (ObjectUtils.isEmpty(product) || StringUtils.isBlank(product.getCate())) {
  287. return DEFAULT_LINE_SN;
  288. }
  289. MesLine query = new MesLine();
  290. query.setProductCate(product.getCate());
  291. query.setStatus("0");
  292. MesLine line = mesLineService.findInfo(query);
  293. if (ObjectUtils.isEmpty(line) || StringUtils.isBlank(line.getSn())) {
  294. return DEFAULT_LINE_SN;
  295. }
  296. return line.getSn();
  297. }
  298. private String readWorkpieceCode(S7PLC s7PLC) {
  299. String raw = s7PLC.readString(ADDR_SN, SN_LENGTH);
  300. if (StringUtils.isBlank(raw)) {
  301. return null;
  302. }
  303. StringBuilder sb = new StringBuilder(raw.length());
  304. for (int i = 0; i < raw.length(); i++) {
  305. char c = raw.charAt(i);
  306. if (c == 0 || c == ' ') {
  307. continue;
  308. }
  309. if (c >= 32 && c < 127) {
  310. sb.append(c);
  311. }
  312. }
  313. return sb.toString().trim();
  314. }
  315. private String getPlcIp() {
  316. String ip = Global.getConfig("mes.tj.plc.ip");
  317. return StringUtils.isBlank(ip) ? DEFAULT_PLC_IP : ip.trim();
  318. }
  319. private static String fmt(float val) {
  320. return String.format("%.3f", val);
  321. }
  322. }