DataUtil.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.netty.NettyClient;
  4. import com.mes.util.DateLocalUtils;
  5. import com.mes.util.JdbcUtils;
  6. import com.mes.util.TestParam;
  7. import com.mes.util.Base64Utils;
  8. import java.util.Base64;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import java.io.*;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16. import java.util.Properties;
  17. public class DataUtil {
  18. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  19. public static Boolean synrTcp(NettyClient nettyClient,String mes_gw){
  20. try{
  21. //TCP连接后,直接先发同步报文
  22. String start = "aaaabbbbbABW";
  23. String msgType = "SYNR";
  24. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  25. String da = "DA" + DateLocalUtils.getCurrentDate();
  26. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  27. String synr_str = start + msgType + gw + da + zt;
  28. //插入报文本地数据
  29. String gy = "";
  30. String sn = "";
  31. JdbcUtils.insertData(gw, gy, synr_str, msgType, sn);
  32. nettyClient.run(synr_str);
  33. return true;
  34. }catch (Exception e){
  35. return false;
  36. }
  37. }
  38. public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
  39. try{
  40. //TCP连接后,直接先发同步报文
  41. String start = "aaaabbbbbABW";
  42. String msgType = "AXTW";
  43. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  44. String da = "DA" + DateLocalUtils.getCurrentDate();
  45. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  46. String axtw_str = start + msgType + gw + da + zt;
  47. //插入报文本地数据
  48. String gy = "";
  49. String sn = "";
  50. JdbcUtils.insertData(gw, gy, axtw_str, msgType, sn);
  51. nettyClient.future.channel().writeAndFlush(axtw_str);
  52. return true;
  53. }catch (Exception e){
  54. return false;
  55. }
  56. }
  57. public static Boolean checkQuality(NettyClient nettyClient, String sn, String user){
  58. try{
  59. String msgType = "AQDW";
  60. String gy = "100000";
  61. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
  62. return aqdw_str;
  63. }catch (Exception e){
  64. return false;
  65. }
  66. }
  67. public static Boolean startWork(NettyClient nettyClient,String sn,String user){
  68. try{
  69. String msgType = "MKSW";
  70. String gy = "";
  71. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
  72. return aqdw_str;
  73. }catch (Exception e){
  74. return false;
  75. }
  76. }
  77. public static Boolean bindUser(NettyClient nettyClient,String sn,String user){
  78. try{
  79. String msgType = "MBDW";
  80. String gy = "400001";
  81. String param = rightPad(user, 36);
  82. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"01",param);
  83. return aqdw_str;
  84. }catch (Exception e){
  85. return false;
  86. }
  87. }
  88. public static Boolean bindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  89. try{
  90. String msgType = "MBDW";
  91. String param = rightPad(wsn, 36);
  92. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  93. return aqdw_str;
  94. }catch (Exception e){
  95. return false;
  96. }
  97. }
  98. public static Boolean unBindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  99. try{
  100. String msgType = "MJBW";
  101. String param = rightPad(wsn, 36);
  102. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  103. return aqdw_str;
  104. }catch (Exception e){
  105. return false;
  106. }
  107. }
  108. public static Boolean sendQuality(String sn,String result,String user){
  109. try{
  110. String msgType = "MQDW";
  111. String craft = "100000";
  112. String lx = "";
  113. String paramNums = "00";
  114. String params = "";
  115. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  116. Properties pro = new Properties();
  117. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  118. pro.load(br);
  119. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  120. String start = "aaaabbbbbABW";
  121. String gy = "GY" + rightPad(craft, 6);
  122. String reslx = "LX" + rightPad(lx, 2);
  123. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  124. String rs = "RS"+ rightPad(result, 2);
  125. String da = "DA" + DateLocalUtils.getCurrentDate();
  126. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  127. String yh = "YH" + rightPad(user, 20);
  128. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  129. log.info("message="+aqdw_str);
  130. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  131. JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
  132. return true;
  133. }catch (Exception e){
  134. return false;
  135. }
  136. }
  137. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
  138. try{
  139. String enconding = "UTF-8";
  140. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  141. Properties pro = new Properties();
  142. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  143. pro.load(br);
  144. String gw = "GW"+rightPad(MesClient.configParam.getOprno(), 6);
  145. String start = "aaaabbbbbABW";
  146. String gy = "GY" + rightPad(craft, 6);
  147. String reslx = "LX" + rightPad(lx, 2);
  148. String id = MesClient.configParam.getLineSn() + rightPad(sn, 36);
  149. String rs = "RS"+ rightPad(result, 2);
  150. String da = "DA" + DateLocalUtils.getCurrentDate();
  151. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  152. String yh = "YH" + rightPad(user, 20);
  153. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  154. log.info("message="+aqdw_str);
  155. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  156. nettyClient.future.channel().writeAndFlush(aqdw_str);
  157. return true;
  158. }catch (Exception e){
  159. return false;
  160. }
  161. }
  162. public static String rightPad(final String str, final int size) {
  163. if (str == null) {
  164. return null;
  165. }
  166. String strret = str;
  167. if(str.length() > size){
  168. strret = str.substring(0,size);
  169. }
  170. return String.format("%-"+size+"s", strret);
  171. }
  172. public static JSONObject getBindMaterail() {
  173. try{
  174. String enconding = "UTF-8";
  175. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  176. Properties pro = new Properties();
  177. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  178. pro.load(br);
  179. String mes_server_ip = pro.getProperty("mes.server_ip");
  180. String oprno = pro.getProperty("mes.gw").trim();
  181. String lineSn = pro.getProperty("mes.line_sn").trim();
  182. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  183. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  184. log.info("params="+params);
  185. String result = doPost(url,params);
  186. log.info("result="+result);
  187. if(result.equalsIgnoreCase("false")) {
  188. return null;
  189. }else {
  190. return JSONObject.parseObject(result);
  191. }
  192. }catch (Exception e){
  193. return null;
  194. }
  195. }
  196. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  197. try{
  198. String enconding = "UTF-8";
  199. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  200. Properties pro = new Properties();
  201. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  202. pro.load(br);
  203. String mes_server_ip = pro.getProperty("mes.server_ip");
  204. String oprno = pro.getProperty("mes.gw").trim();
  205. String lineSn = pro.getProperty("mes.line_sn").trim();
  206. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  207. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  208. log.info("params="+params);
  209. String result = doPost(url,params);
  210. log.info("result="+result);
  211. if(result.equalsIgnoreCase("false")) {
  212. return null;
  213. }else {
  214. return JSONObject.parseObject(result);
  215. }
  216. }catch (Exception e){
  217. return null;
  218. }
  219. }
  220. // 上传气密结果
  221. public static JSONObject qmResultData(TestParam testParam) {
  222. try{
  223. String enconding = "UTF-8";
  224. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  225. Properties pro = new Properties();
  226. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  227. pro.load(br);
  228. String mes_server_ip = pro.getProperty("mes.server_ip");
  229. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmresult";
  230. // 根据服务器端代码,对特定参数进行Base64编码
  231. String testPressureBase64 = Base64Utils.getBase64(testParam.getParam1() != null ? testParam.getParam1() : "");
  232. String leakValBase64 = Base64Utils.getBase64(testParam.getParam3() != null ? testParam.getParam3() : "");
  233. String testTimeBase64 = Base64Utils.getBase64(testParam.getCreateTime() != null ? testParam.getCreateTime() : "");
  234. String titleBase64 = Base64Utils.getBase64(testParam.getOprnoTitle() != null ? testParam.getOprnoTitle() : "");
  235. // 注意:sn(序列号)和workNum(测试人员)不应该Base64编码
  236. log.info("服务器端需要Base64编码的参数: testPressure='{}'->'{}', leakVal='{}'->'{}', testTime='{}'->'{}', title='{}'->'{}'"
  237. , testParam.getParam1(), testPressureBase64, testParam.getParam3(), leakValBase64
  238. , testParam.getCreateTime(), testTimeBase64, testParam.getOprnoTitle(), titleBase64);
  239. String params = "__ajax=json&oprno="+URLEncoder.encode(testParam.getOprno() != null ? testParam.getOprno() : "", "UTF-8")
  240. +"&lineSn="+URLEncoder.encode(testParam.getLineSn() != null ? testParam.getLineSn() : "", "UTF-8")
  241. +"&sn="+URLEncoder.encode(testParam.getSn() != null ? testParam.getSn() : "", "UTF-8")
  242. +"&workNum="+URLEncoder.encode(testParam.getUcode() != null ? testParam.getUcode() : "", "UTF-8")
  243. +"&cxm="+URLEncoder.encode(testParam.getParam5() != null ? testParam.getParam5() : "", "UTF-8")
  244. +"&result="+URLEncoder.encode(testParam.getResult() != null ? testParam.getResult() : "", "UTF-8")
  245. +"&testPressure="+URLEncoder.encode(testPressureBase64, "UTF-8")
  246. +"&testPressureUnit="+URLEncoder.encode(testParam.getParam2() != null ? testParam.getParam2() : "", "UTF-8")
  247. +"&leakVal="+URLEncoder.encode(leakValBase64, "UTF-8")
  248. +"&leakValUnit="+URLEncoder.encode(testParam.getParam4() != null ? testParam.getParam4() : "", "UTF-8")
  249. +"&testTime="+URLEncoder.encode(testTimeBase64, "UTF-8")
  250. +"&title="+URLEncoder.encode(titleBase64, "UTF-8")
  251. +"&remark="+URLEncoder.encode(testParam.getRemark() != null ? testParam.getRemark() : "", "UTF-8")
  252. +"&deviceType="+URLEncoder.encode(testParam.getDeviceType() != null ? testParam.getDeviceType() : "", "UTF-8")
  253. // cq=充气时长 by=稳压时长 cs=测试时长(秒);testTime 仍为完成时刻
  254. +"&cq="+URLEncoder.encode(testParam.getFillDuration() != null ? testParam.getFillDuration() : "", "UTF-8")
  255. +"&by="+URLEncoder.encode(testParam.getStabilizeTime() != null ? testParam.getStabilizeTime() : "", "UTF-8")
  256. +"&cs="+URLEncoder.encode(testParam.getTestDuration() != null ? testParam.getTestDuration() : "", "UTF-8")
  257. +"&leakRate="+URLEncoder.encode("", "UTF-8")
  258. +"&leakRateUnit="+URLEncoder.encode("", "UTF-8")
  259. +"&type="+URLEncoder.encode("", "UTF-8");
  260. // 添加sessionid
  261. // if (MesClient.sessionid != null && !MesClient.sessionid.isEmpty()) {
  262. // params += "&__sid=" + MesClient.sessionid;
  263. // }
  264. log.info("提交时长字段 cq(充气)={}, by(稳压)={}, cs(测试)={}",
  265. testParam.getFillDuration(), testParam.getStabilizeTime(), testParam.getTestDuration());
  266. log.info("params="+params);
  267. String result = doPost(url,params);
  268. log.info("result="+result);
  269. // 检查result是否为null或空
  270. if(result == null || result.trim().isEmpty()) {
  271. log.error("HTTP请求返回结果为空,请检查服务器连接和参数格式");
  272. log.info("压力值: '{}', 压力单位: '{}', 泄漏值: '{}', 泄漏单位: '{}'",
  273. testParam.getParam1(), testParam.getParam2(), testParam.getParam3(), testParam.getParam4());
  274. return null;
  275. }
  276. if(result.equalsIgnoreCase("false")) {
  277. return null;
  278. }else {
  279. try {
  280. return JSONObject.parseObject(result);
  281. } catch (Exception e) {
  282. log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result);
  283. return null;
  284. }
  285. }
  286. }catch (Exception e){
  287. log.error("qmResultData方法执行异常: {}", e.getMessage());
  288. e.printStackTrace();
  289. return null;
  290. }
  291. }
  292. /**
  293. * HTTP 同步质量校验(PLC 进站用)
  294. */
  295. public static JSONObject checkQualityHttp(String sn) {
  296. try {
  297. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  298. Properties pro = new Properties();
  299. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  300. pro.load(br);
  301. br.close();
  302. String mes_server_ip = pro.getProperty("mes.server_ip");
  303. String oprno = MesClient.mes_gw != null && !MesClient.mes_gw.isEmpty()
  304. ? MesClient.mes_gw.trim()
  305. : pro.getProperty("mes.gw").trim();
  306. String lineSn = MesClient.mes_line_sn != null && !MesClient.mes_line_sn.isEmpty()
  307. ? MesClient.mes_line_sn.trim()
  308. : pro.getProperty("mes.line_sn").trim();
  309. String userCode = MesClient.user_menu != null ? MesClient.user_menu.getText() : "";
  310. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/checkQuality";
  311. String params = "__ajax=json&sn=" + URLEncoder.encode(sn.trim(), "UTF-8")
  312. + "&oprno=" + URLEncoder.encode(oprno, "UTF-8")
  313. + "&lineSn=" + URLEncoder.encode(lineSn, "UTF-8")
  314. + "&userCode=" + URLEncoder.encode(userCode, "UTF-8");
  315. log.info("checkQualityHttp params={}", params);
  316. String result = doPost(url, params);
  317. log.info("checkQualityHttp result={}", result);
  318. if (result == null || result.equalsIgnoreCase("false")) {
  319. return null;
  320. }
  321. return JSONObject.parseObject(result);
  322. } catch (Exception e) {
  323. log.error("checkQualityHttp异常: {}", e.getMessage());
  324. e.printStackTrace();
  325. return null;
  326. }
  327. }
  328. public static String doPost(String httpUrl, String param) {
  329. HttpURLConnection connection = null;
  330. InputStream is = null;
  331. OutputStream os = null;
  332. BufferedReader br = null;
  333. String result = null;
  334. try {
  335. URL url = new URL(httpUrl);
  336. connection = (HttpURLConnection) url.openConnection();
  337. connection.setRequestMethod("POST");
  338. connection.setConnectTimeout(15000);
  339. connection.setReadTimeout(60000);
  340. connection.setDoOutput(true);
  341. connection.setDoInput(true);
  342. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  343. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  344. os = connection.getOutputStream();
  345. os.write(param.getBytes());
  346. int responseCode = connection.getResponseCode();
  347. log.info("HTTP响应码: " + responseCode);
  348. if (responseCode == 200) {
  349. is = connection.getInputStream();
  350. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  351. StringBuffer sbf = new StringBuffer();
  352. String temp = null;
  353. while ((temp = br.readLine()) != null) {
  354. sbf.append(temp);
  355. sbf.append("\r\n");
  356. }
  357. result = sbf.toString();
  358. } else {
  359. log.error("HTTP请求失败,响应码: " + responseCode);
  360. // 尝试读取错误响应内容以获取更多信息
  361. try {
  362. is = connection.getErrorStream();
  363. if (is != null) {
  364. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  365. StringBuffer errorBuffer = new StringBuffer();
  366. String temp = null;
  367. while ((temp = br.readLine()) != null) {
  368. errorBuffer.append(temp);
  369. errorBuffer.append("\r\n");
  370. }
  371. log.error("服务器错误响应内容: " + errorBuffer.toString());
  372. }
  373. } catch (Exception errorReadException) {
  374. log.error("读取错误响应失败: " + errorReadException.getMessage());
  375. }
  376. }
  377. } catch (MalformedURLException e) {
  378. e.printStackTrace();
  379. } catch (IOException e) {
  380. e.printStackTrace();
  381. } finally {
  382. if (null != br) {
  383. try {
  384. br.close();
  385. } catch (IOException e) {
  386. e.printStackTrace();
  387. }
  388. }
  389. if (null != os) {
  390. try {
  391. os.close();
  392. } catch (IOException e) {
  393. e.printStackTrace();
  394. }
  395. }
  396. if (null != is) {
  397. try {
  398. is.close();
  399. } catch (IOException e) {
  400. e.printStackTrace();
  401. }
  402. }
  403. connection.disconnect();
  404. }
  405. return result;
  406. }
  407. public static String getBase64Encode(String str) {
  408. if (str == null || "".equals(str)) {
  409. return "";
  410. }
  411. try {
  412. // 简单清理:去除多余空格,保持数据格式规范
  413. str = str.trim().replaceAll("\\s+", "");
  414. // 使用最简单的标准Base64编码
  415. return Base64.getEncoder().encodeToString(str.getBytes("UTF-8"));
  416. } catch (UnsupportedEncodingException e) {
  417. e.printStackTrace();
  418. return "";
  419. }
  420. }
  421. }