DataUtil.java 15 KB

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