DataUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 java.io.*;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.Map;
  12. import java.util.Properties;
  13. public class DataUtil {
  14. public static String lastUploadPressRivetMessage = "";
  15. public static Boolean synrTcp(NettyClient nettyClient,String mes_gw){
  16. try{
  17. //TCP杩炴帴鍚庯紝鐩存帴鍏堝彂鍚屾鎶ユ枃
  18. String start = "aaaabbbbbABW";
  19. String msgType = "SYNR";
  20. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  21. String da = "DA" + DateLocalUtils.getCurrentDate();
  22. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  23. String synr_str = start + msgType + gw + da + zt;
  24. //鎻掑叆鎶ユ枃鏈湴鏁版嵁
  25. String gy = "";
  26. String sn = "";
  27. JdbcUtils.insertData(gw, gy, synr_str, msgType, sn);
  28. nettyClient.run(synr_str);
  29. return true;
  30. }catch (Exception e){
  31. return false;
  32. }
  33. }
  34. public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
  35. try{
  36. //TCP杩炴帴鍚庯紝鐩存帴鍏堝彂鍚屾鎶ユ枃
  37. String start = "aaaabbbbbABW";
  38. String msgType = "AXTW";
  39. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  40. String da = "DA" + DateLocalUtils.getCurrentDate();
  41. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  42. String axtw_str = start + msgType + gw + da + zt;
  43. //鎻掑叆鎶ユ枃鏈湴鏁版嵁
  44. String gy = "";
  45. String sn = "";
  46. JdbcUtils.insertData(gw, gy, axtw_str, msgType, sn);
  47. nettyClient.future.channel().writeAndFlush(axtw_str);
  48. return true;
  49. }catch (Exception e){
  50. return false;
  51. }
  52. }
  53. public static Boolean checkQuality(NettyClient nettyClient, String sn, String user){
  54. try{
  55. String msgType = "AQDW";
  56. String gy = "100000";
  57. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
  58. return aqdw_str;
  59. }catch (Exception e){
  60. return false;
  61. }
  62. }
  63. public static Boolean startWork(NettyClient nettyClient,String sn,String user){
  64. try{
  65. String msgType = "MKSW";
  66. String gy = "";
  67. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
  68. return aqdw_str;
  69. }catch (Exception e){
  70. return false;
  71. }
  72. }
  73. public static Boolean bindUser(NettyClient nettyClient,String sn,String user){
  74. try{
  75. String msgType = "MBDW";
  76. String gy = "400001";
  77. String param = rightPad(user, 36);
  78. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"01",param);
  79. return aqdw_str;
  80. }catch (Exception e){
  81. return false;
  82. }
  83. }
  84. public static Boolean bindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  85. try{
  86. String msgType = "MBDW";
  87. String param = rightPad(wsn, 36);
  88. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  89. return aqdw_str;
  90. }catch (Exception e){
  91. return false;
  92. }
  93. }
  94. public static Boolean unBindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  95. try{
  96. String msgType = "MJBW";
  97. String param = rightPad(wsn, 36);
  98. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  99. return aqdw_str;
  100. }catch (Exception e){
  101. return false;
  102. }
  103. }
  104. public static Boolean sendQuality(NettyClient nettyClient,String sn,String ret,String user){
  105. try{
  106. String msgType = "MQDW";
  107. String gy = "100000";
  108. String param = getPressParams();
  109. String paramNums = String.format("%02d", MesClient.pressParamTitle.length);
  110. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,paramNums,param);
  111. return aqdw_str;
  112. }catch (Exception e){
  113. return false;
  114. }
  115. }
  116. public static String getPressParams(){
  117. StringBuilder params = new StringBuilder();
  118. for(int i=0; i<MesClient.pressParamTitle.length; i++){
  119. String title = MesClient.pressParamTitle[i] == null ? "" : MesClient.pressParamTitle[i];
  120. String value = MesClient.pressParamValue[i] == null ? "" : MesClient.pressParamValue[i];
  121. params.append(rightPad(title, 20));
  122. params.append(rightPad(value, 20));
  123. }
  124. return params.toString();
  125. }
  126. public static Boolean uploadPressRivetData(String sn, Map<Integer, Map<String, String>> pressRivetMap){
  127. try{
  128. String enconding = "UTF-8";
  129. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  130. Properties pro = new Properties();
  131. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  132. pro.load(br);
  133. String mesServerIp = pro.getProperty("mes.server_ip");
  134. String oprno = pro.getProperty("mes.gw").trim();
  135. String lineSn = pro.getProperty("mes.line_sn").trim();
  136. String url = pro.getProperty("mes.press_rivet_upload_url");
  137. if(url == null || url.trim().isEmpty()){
  138. url = "http://"+mesServerIp+":8980/js/a/mes/mesProductRecord/yamao";
  139. }
  140. JSONObject json = new JSONObject();
  141. json.put("sn", sn == null ? "" : sn);
  142. json.put("oprno", oprno);
  143. json.put("lineSn", lineSn);
  144. json.put("datas", pressRivetMap.values());
  145. String params = json.toJSONString();
  146. System.out.println("json="+params);
  147. String result = doPostJson(url,params);
  148. System.out.println("result="+result);
  149. if(result == null || result.trim().isEmpty()){
  150. lastUploadPressRivetMessage = "上传失败,服务器无响应";
  151. return false;
  152. }
  153. if(result.trim().equalsIgnoreCase("false")){
  154. lastUploadPressRivetMessage = "上传失败";
  155. return false;
  156. }
  157. JSONObject resultObj = JSONObject.parseObject(result);
  158. lastUploadPressRivetMessage = resultObj.getString("Message");
  159. String uploadResult = resultObj.getString("Result");
  160. if((lastUploadPressRivetMessage == null || lastUploadPressRivetMessage.trim().isEmpty()) && !"SUCCESS".equalsIgnoreCase(uploadResult)){
  161. lastUploadPressRivetMessage = "上传失败";
  162. }
  163. return "SUCCESS".equalsIgnoreCase(uploadResult);
  164. }catch (Exception e){
  165. e.printStackTrace();
  166. lastUploadPressRivetMessage = "上传异常:" + e.getMessage();
  167. return false;
  168. }
  169. }
  170. public static Boolean sendQualityNew(NettyClient nettyClient,String sn,String ret,String user){
  171. try{
  172. String msgType = "MQDW";
  173. String craft = "100000";
  174. String params = "";
  175. String enconding = "UTF-8";
  176. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  177. Properties pro = new Properties();
  178. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  179. pro.load(br);
  180. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  181. String start = "aaaabbbbbABW";
  182. String gy = "GY" + rightPad(craft, 6);
  183. String reslx = "LX" + rightPad("", 2);
  184. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  185. String rs = "RS"+ rightPad(ret, 2);
  186. String da = "DA" + DateLocalUtils.getCurrentDate();
  187. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  188. String yh = "YH" + rightPad(user, 20);
  189. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + "00" + params;
  190. System.out.println("message="+aqdw_str);
  191. JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
  192. return true;
  193. }catch (Exception e){
  194. return false;
  195. }
  196. }
  197. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
  198. try{
  199. String enconding = "UTF-8";
  200. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  201. Properties pro = new Properties();
  202. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  203. pro.load(br);
  204. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  205. String start = "aaaabbbbbABW";
  206. String gy = "GY" + rightPad(craft, 6);
  207. String reslx = "LX" + rightPad(lx, 2);
  208. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  209. String rs = "RS"+ rightPad(result, 2);
  210. String da = "DA" + DateLocalUtils.getCurrentDate();
  211. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  212. String yh = "YH" + rightPad(user, 20);
  213. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  214. System.out.println("message="+aqdw_str);
  215. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  216. nettyClient.future.channel().writeAndFlush(aqdw_str);
  217. return true;
  218. }catch (Exception e){
  219. return false;
  220. }
  221. }
  222. public static String rightPad(final String str, final int size) {
  223. if (str == null) {
  224. return null;
  225. }
  226. String strret = str;
  227. if(str.length() > size){
  228. strret = str.substring(0,size);
  229. }
  230. return String.format("%-"+size+"s", strret);
  231. }
  232. public static JSONObject getBindMaterail() {
  233. try{
  234. String enconding = "UTF-8";
  235. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  236. Properties pro = new Properties();
  237. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  238. pro.load(br);
  239. String mes_server_ip = pro.getProperty("mes.server_ip");
  240. String oprno = pro.getProperty("mes.gw").trim();
  241. String lineSn = pro.getProperty("mes.line_sn").trim();
  242. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  243. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  244. System.out.println("params="+params);
  245. String result = doPost(url,params);
  246. System.out.println("result="+result);
  247. if(result.equalsIgnoreCase("false")) {
  248. return null;
  249. }else {
  250. return JSONObject.parseObject(result);
  251. }
  252. }catch (Exception e){
  253. return null;
  254. }
  255. }
  256. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  257. try{
  258. String enconding = "UTF-8";
  259. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  260. Properties pro = new Properties();
  261. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  262. pro.load(br);
  263. String mes_server_ip = pro.getProperty("mes.server_ip");
  264. String oprno = pro.getProperty("mes.gw").trim();
  265. String lineSn = pro.getProperty("mes.line_sn").trim();
  266. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  267. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  268. System.out.println("params="+params);
  269. String result = doPost(url,params);
  270. System.out.println("result="+result);
  271. if(result.equalsIgnoreCase("false")) {
  272. return null;
  273. }else {
  274. return JSONObject.parseObject(result);
  275. }
  276. }catch (Exception e){
  277. return null;
  278. }
  279. }
  280. public static String doPost(String httpUrl, String param) {
  281. HttpURLConnection connection = null;
  282. InputStream is = null;
  283. OutputStream os = null;
  284. BufferedReader br = null;
  285. String result = null;
  286. try {
  287. URL url = new URL(httpUrl);
  288. connection = (HttpURLConnection) url.openConnection();
  289. connection.setRequestMethod("POST");
  290. connection.setConnectTimeout(5000);
  291. connection.setReadTimeout(10000);
  292. connection.setDoOutput(true);
  293. connection.setDoInput(true);
  294. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  295. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  296. os = connection.getOutputStream();
  297. os.write(param.getBytes());
  298. if (connection.getResponseCode() == 200) {
  299. is = connection.getInputStream();
  300. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  301. StringBuffer sbf = new StringBuffer();
  302. String temp = null;
  303. while ((temp = br.readLine()) != null) {
  304. sbf.append(temp);
  305. sbf.append("\r\n");
  306. }
  307. result = sbf.toString();
  308. }
  309. } catch (MalformedURLException e) {
  310. e.printStackTrace();
  311. } catch (IOException e) {
  312. e.printStackTrace();
  313. } finally {
  314. if (null != br) {
  315. try {
  316. br.close();
  317. } catch (IOException e) {
  318. e.printStackTrace();
  319. }
  320. }
  321. if (null != os) {
  322. try {
  323. os.close();
  324. } catch (IOException e) {
  325. e.printStackTrace();
  326. }
  327. }
  328. if (null != is) {
  329. try {
  330. is.close();
  331. } catch (IOException e) {
  332. e.printStackTrace();
  333. }
  334. }
  335. if (connection != null) {
  336. connection.disconnect();
  337. }
  338. }
  339. return result;
  340. }
  341. public static String doPostJson(String httpUrl, String json) {
  342. HttpURLConnection connection = null;
  343. InputStream is = null;
  344. OutputStream os = null;
  345. BufferedReader br = null;
  346. String result = null;
  347. try {
  348. URL url = new URL(httpUrl);
  349. connection = (HttpURLConnection) url.openConnection();
  350. connection.setRequestMethod("POST");
  351. connection.setConnectTimeout(5000);
  352. connection.setReadTimeout(10000);
  353. connection.setDoOutput(true);
  354. connection.setDoInput(true);
  355. connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  356. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  357. os = connection.getOutputStream();
  358. os.write(json.getBytes(StandardCharsets.UTF_8));
  359. if (connection.getResponseCode() == 200) {
  360. is = connection.getInputStream();
  361. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  362. StringBuffer sbf = new StringBuffer();
  363. String temp = null;
  364. while ((temp = br.readLine()) != null) {
  365. sbf.append(temp);
  366. sbf.append("\r\n");
  367. }
  368. result = sbf.toString();
  369. }
  370. } catch (MalformedURLException e) {
  371. e.printStackTrace();
  372. } catch (IOException e) {
  373. e.printStackTrace();
  374. } finally {
  375. if (null != br) {
  376. try {
  377. br.close();
  378. } catch (IOException e) {
  379. e.printStackTrace();
  380. }
  381. }
  382. if (null != os) {
  383. try {
  384. os.close();
  385. } catch (IOException e) {
  386. e.printStackTrace();
  387. }
  388. }
  389. if (null != is) {
  390. try {
  391. is.close();
  392. } catch (IOException e) {
  393. e.printStackTrace();
  394. }
  395. }
  396. if (connection != null) {
  397. connection.disconnect();
  398. }
  399. }
  400. return result;
  401. }
  402. public static JSONObject upAlarm(String sn,String ucode) {
  403. try{
  404. String enconding = "UTF-8";
  405. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  406. Properties pro = new Properties();
  407. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  408. pro.load(br);
  409. String mes_server_ip = pro.getProperty("mes.server_ip");
  410. String oprno = pro.getProperty("mes.gw").trim();
  411. String lineSn = pro.getProperty("mes.line_sn").trim();
  412. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessAlarm/add";
  413. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+ucode;
  414. System.out.println("params="+params);
  415. String result = doPost(url,params);
  416. System.out.println("result="+result);
  417. if(result.equalsIgnoreCase("false")) {
  418. return null;
  419. }else {
  420. return JSONObject.parseObject(result);
  421. }
  422. }catch (Exception e){
  423. return null;
  424. }
  425. }
  426. }