DataUtil.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.mes.util.Base64Utils;
  5. import com.mes.util.JdbcUtils;
  6. import com.mes.util.QmPlcPoint;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.*;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLEncoder;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. public class DataUtil {
  18. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  19. public static JSONObject checkQuality(String sn, String user){
  20. return qmCheck(sn, user);
  21. }
  22. public static JSONObject qmCheck(String sn, String user){
  23. try{
  24. String mes_server_ip = MesClient.mes_server_ip;
  25. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  26. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  27. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmcheck";
  28. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&workNum="+user;
  29. log.info("params="+params);
  30. String result = doPost(url,params);
  31. log.info("result="+result);
  32. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  33. return null;
  34. }else {
  35. JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn);
  36. return JSONObject.parseObject(result);
  37. }
  38. }catch (Exception e){
  39. log.info("e="+e.getMessage());
  40. return null;
  41. }
  42. }
  43. public static JSONObject sendQmResult(String sn, String user, QmPlcPoint.QmTestData testData){
  44. return sendQmResult(sn, user, testData, null);
  45. }
  46. public static JSONObject sendQmResult(String sn, String user, QmPlcPoint.QmTestData testData, String ngLeakPoints){
  47. try{
  48. String mes_server_ip = MesClient.mes_server_ip;
  49. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  50. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  51. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmresult";
  52. String testTime = QmPlcPoint.formatTestTime(testData.date, testData.time);
  53. String title = testData.name == null ? "" : testData.name;
  54. String cxm = testData.name == null ? "" : testData.name;
  55. String result = testData.result == null ? "NG" : testData.result;
  56. String testPressure = testData.testPressure == null ? "" : testData.testPressure.trim();
  57. String leakVal = testData.leakVal == null ? "" : testData.leakVal.trim();
  58. String remark = QmPlcPoint.buildRemark(testData);
  59. String testPressureBase64 = Base64Utils.getBase64(testPressure);
  60. String leakValBase64 = Base64Utils.getBase64(leakVal);
  61. String testTimeBase64 = Base64Utils.getBase64(testTime);
  62. String titleBase64 = Base64Utils.getBase64(title);
  63. log.info("qmresult upload leakVal='{}' base64='{}', testPressure='{}' base64='{}'",
  64. leakVal, leakValBase64, testPressure, testPressureBase64);
  65. StringBuilder params = new StringBuilder();
  66. params.append("__ajax=json");
  67. appendUrlParam(params, "oprno", oprno);
  68. appendUrlParam(params, "lineSn", lineSn);
  69. appendUrlParam(params, "sn", sn);
  70. appendUrlParam(params, "workNum", user);
  71. appendUrlParam(params, "result", result);
  72. appendUrlParam(params, "cxm", cxm);
  73. appendUrlParam(params, "cq", testData.pressurizingTime);
  74. appendUrlParam(params, "by", testData.equalizingTime);
  75. appendUrlParam(params, "cs", testData.testingTime);
  76. appendUrlParam(params, "testPressure", testPressureBase64);
  77. appendUrlParam(params, "testPressureUnit", testData.testPressureUnit);
  78. appendUrlParam(params, "leakVal", leakValBase64);
  79. appendUrlParam(params, "leakValUnit", testData.leakValUnit);
  80. appendUrlParam(params, "testTime", testTimeBase64);
  81. appendUrlParam(params, "title", titleBase64);
  82. appendUrlParam(params, "remark", remark);
  83. appendUrlParam(params, "deviceType", "S7-200SMART");
  84. appendUrlParam(params, "ngLeakPoints", ngLeakPoints);
  85. log.info("qmresult params="+params);
  86. String response = doPost(url, params.toString());
  87. log.info("qmresult result="+response);
  88. if(response == null || response.trim().isEmpty() || response.equalsIgnoreCase("false")) {
  89. return null;
  90. }
  91. JdbcUtils.insertData(oprno, "100000", params.toString(), "MQDW", sn);
  92. return JSONObject.parseObject(response);
  93. }catch (Exception e){
  94. log.info("sendQmResult e="+e.getMessage());
  95. return null;
  96. }
  97. }
  98. private static void appendUrlParam(StringBuilder params, String name, String value) {
  99. try {
  100. params.append("&").append(name).append("=");
  101. if (value != null && !value.isEmpty()) {
  102. params.append(URLEncoder.encode(value, StandardCharsets.UTF_8.name()));
  103. }
  104. } catch (UnsupportedEncodingException e) {
  105. params.append("&").append(name).append("=");
  106. if (value != null) {
  107. params.append(value);
  108. }
  109. }
  110. }
  111. public static JSONObject bindWarehouse(String sn,String materielSn,String user,String craft ){
  112. String ret = "OK";
  113. try{
  114. String mes_server_ip = MesClient.mes_server_ip;
  115. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  116. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  117. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcbindRecord";
  118. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user + "&craft="+craft+"&materielSn="+materielSn;
  119. log.info("params="+params);
  120. String result = doPost(url,params);
  121. log.info("result="+result);
  122. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  123. return null;
  124. }else {
  125. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  126. return JSONObject.parseObject(result);
  127. }
  128. }catch (Exception e){
  129. log.info("e="+e.getMessage());
  130. return null;
  131. }
  132. }
  133. public static JSONObject sendQuality(String sn,String ret,String user){
  134. try{
  135. String mes_server_ip = MesClient.mes_server_ip;
  136. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  137. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  138. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult";
  139. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user;
  140. log.info("params="+params);
  141. String result = doPost(url,params);
  142. log.info("result="+result);
  143. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  144. return null;
  145. }else {
  146. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  147. return JSONObject.parseObject(result);
  148. }
  149. }catch (Exception e){
  150. log.info("e="+e.getMessage());
  151. return null;
  152. }
  153. }
  154. public static String rightPad(final String str, final int size) {
  155. if (str == null) {
  156. return null;
  157. }
  158. String strret = str;
  159. if(str.length() > size){
  160. strret = str.substring(0,size);
  161. }
  162. return String.format("%-"+size+"s", strret);
  163. }
  164. public static JSONObject getBindMaterail() {
  165. try{
  166. String mes_server_ip = MesClient.mes_server_ip;
  167. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  168. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  169. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  170. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  171. System.out.println("params="+params);
  172. String result = doPost(url,params);
  173. System.out.println("result="+result);
  174. if(result.equalsIgnoreCase("false")) {
  175. return null;
  176. }else {
  177. return JSONObject.parseObject(result);
  178. }
  179. }catch (Exception e){
  180. return null;
  181. }
  182. }
  183. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  184. try{
  185. String mes_server_ip = MesClient.mes_server_ip;
  186. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  187. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  188. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  189. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  190. System.out.println("params="+params);
  191. String result = doPost(url,params);
  192. System.out.println("result="+result);
  193. if(result.equalsIgnoreCase("false")) {
  194. return null;
  195. }else {
  196. return JSONObject.parseObject(result);
  197. }
  198. }catch (Exception e){
  199. return null;
  200. }
  201. }
  202. public static String doPost(String httpUrl, String param) {
  203. httpUrl = appendSid(httpUrl);
  204. HttpURLConnection connection = null;
  205. InputStream is = null;
  206. OutputStream os = null;
  207. BufferedReader br = null;
  208. String result = null;
  209. try {
  210. URL url = new URL(httpUrl);
  211. connection = (HttpURLConnection) url.openConnection();
  212. connection.setRequestMethod("POST");
  213. connection.setConnectTimeout(15000);
  214. connection.setReadTimeout(60000000);
  215. connection.setDoOutput(true);
  216. connection.setDoInput(true);
  217. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  218. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  219. os = connection.getOutputStream();
  220. os.write(param.getBytes(StandardCharsets.UTF_8));
  221. int code = connection.getResponseCode();
  222. log.info("HTTP POST " + httpUrl + " code=" + code);
  223. if (code == 200) {
  224. is = connection.getInputStream();
  225. } else {
  226. is = connection.getErrorStream();
  227. }
  228. if (is != null) {
  229. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  230. StringBuffer sbf = new StringBuffer();
  231. String temp = null;
  232. while ((temp = br.readLine()) != null) {
  233. sbf.append(temp);
  234. sbf.append("\r\n");
  235. }
  236. result = sbf.toString();
  237. }
  238. } catch (MalformedURLException e) {
  239. e.printStackTrace();
  240. } catch (IOException e) {
  241. e.printStackTrace();
  242. } finally {
  243. if (null != br) {
  244. try {
  245. br.close();
  246. } catch (IOException e) {
  247. e.printStackTrace();
  248. }
  249. }
  250. if (null != os) {
  251. try {
  252. os.close();
  253. } catch (IOException e) {
  254. e.printStackTrace();
  255. }
  256. }
  257. if (null != is) {
  258. try {
  259. is.close();
  260. } catch (IOException e) {
  261. e.printStackTrace();
  262. }
  263. }
  264. if (connection != null) {
  265. connection.disconnect();
  266. }
  267. }
  268. return result;
  269. }
  270. /**
  271. * 查询工作记录
  272. * @param oprno 工位号(空则查所有工位)
  273. * @param sn 工件码(可选,用于搜索)
  274. * @param pageNo 页码
  275. * @param pageSize 每页条数
  276. * @return 工作记录响应
  277. */
  278. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  279. WorkRecordResp resp = new WorkRecordResp();
  280. try {
  281. String mes_server_ip = MesClient.mes_server_ip;
  282. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  283. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData";
  284. StringBuilder params = new StringBuilder();
  285. params.append("__ajax=json");
  286. params.append("&lineSn=").append(lineSn);
  287. params.append("&pageNo=").append(pageNo);
  288. params.append("&pageSize=").append(pageSize);
  289. System.out.println("oprno="+oprno);
  290. //todo
  291. if (oprno != null && !oprno.isEmpty()) {
  292. params.append("&oprno=").append(oprno);
  293. }
  294. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  295. if (sn != null && !sn.isEmpty()) {
  296. params.append("&sn=").append(sn);
  297. }
  298. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  299. String result = doPost(url, params.toString());
  300. log.info("查询工作记录结果: result=" + result);
  301. if (result == null || result.trim().isEmpty()) {
  302. resp.setResult(false);
  303. resp.setMessage("请求返回空结果");
  304. return resp;
  305. }
  306. JSONObject jsonObj = JSONObject.parseObject(result);
  307. if (jsonObj == null) {
  308. resp.setResult(false);
  309. resp.setMessage("解析响应失败");
  310. return resp;
  311. }
  312. resp.setResult(true);
  313. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  314. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  315. resp.setCount(jsonObj.getLongValue("count"));
  316. List<WorkRecordData> list = new ArrayList<>();
  317. JSONArray listArray = jsonObj.getJSONArray("list");
  318. if (listArray != null) {
  319. for (int i = 0; i < listArray.size(); i++) {
  320. JSONObject item = listArray.getJSONObject(i);
  321. WorkRecordData data = new WorkRecordData();
  322. data.setId(item.getString("id"));
  323. data.setSn(item.getString("sn"));
  324. data.setOprno(item.getString("oprno"));
  325. data.setUpdateBy(item.getString("updateBy"));
  326. data.setUpdateDate(item.getString("updateDate"));
  327. data.setContent(item.getString("content"));
  328. list.add(data);
  329. }
  330. }
  331. resp.setList(list);
  332. } catch (Exception e) {
  333. log.error("查询工作记录异常: " + e.getMessage());
  334. e.printStackTrace();
  335. resp.setResult(false);
  336. resp.setMessage("查询异常: " + e.getMessage());
  337. }
  338. return resp;
  339. }
  340. private static String appendSid(String url) {
  341. if (url == null || url.contains("__sid=") || url.contains("/login")) {
  342. return url;
  343. }
  344. try {
  345. String sid = MesClient.sessionid;
  346. if (sid == null || sid.length() == 0) {
  347. return url;
  348. }
  349. return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
  350. } catch (Exception e) {
  351. return url;
  352. }
  353. }
  354. }