DataUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.util.Properties;
  11. public class DataUtil {
  12. // 删除当前工位后台工件码
  13. public static JSONObject delCurSn(String oprno, String serverIp, String lineSn) {
  14. try {
  15. String url = "http://" + serverIp + ":8980/js/a/mes/mesProductRecord/cancelCurSn";
  16. String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn + "&__sid=" + MesClient.sessionid;
  17. String result = doPost(url, params);
  18. if (result == null || result.equalsIgnoreCase("false")) {
  19. return null;
  20. }
  21. return JSONObject.parseObject(result);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. return null;
  25. }
  26. }
  27. // 从后台获取当前工位工件码
  28. public static JSONObject getCurSn(String oprno, String serverIp, String lineSn) {
  29. try {
  30. String url = "http://" + serverIp + ":8980/js/a/mes/mesProductRecord/getCurSn";
  31. String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn + "&__sid=" + MesClient.sessionid;
  32. String result = doPost(url, params);
  33. if (result == null || result.equalsIgnoreCase("false")) {
  34. return null;
  35. }
  36. return JSONObject.parseObject(result);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. return null;
  40. }
  41. }
  42. public static Boolean synrTcp(NettyClient nettyClient,String mes_gw){
  43. try{
  44. //TCP连接后,直接先发同步报文
  45. String start = "aaaabbbbbABW";
  46. String msgType = "SYNR";
  47. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  48. String da = "DA" + DateLocalUtils.getCurrentDate();
  49. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  50. String synr_str = start + msgType + gw + da + zt;
  51. //插入报文本地数据
  52. String gy = "";
  53. String sn = "";
  54. JdbcUtils.insertData(gw, gy, synr_str, msgType, sn);
  55. nettyClient.run(synr_str);
  56. return true;
  57. }catch (Exception e){
  58. return false;
  59. }
  60. }
  61. public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
  62. try{
  63. //TCP连接后,直接先发同步报文
  64. String start = "aaaabbbbbABW";
  65. String msgType = "AXTW";
  66. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  67. String da = "DA" + DateLocalUtils.getCurrentDate();
  68. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  69. String axtw_str = start + msgType + gw + da + zt;
  70. //插入报文本地数据
  71. String gy = "";
  72. String sn = "";
  73. JdbcUtils.insertData(gw, gy, axtw_str, msgType, sn);
  74. nettyClient.future.channel().writeAndFlush(axtw_str);
  75. return true;
  76. }catch (Exception e){
  77. return false;
  78. }
  79. }
  80. public static JSONObject checkQuality(String sn, String user) {
  81. try {
  82. String url = "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProductRecord/pccheck";
  83. String params = "__ajax=json&oprno=" + MesClient.mes_gw.trim()
  84. + "&lineSn=" + MesClient.mes_line_sn.trim()
  85. + "&sn=" + sn.trim()
  86. + "&ucode=" + user.trim()
  87. + "&__sid=" + MesClient.sessionid;
  88. System.out.println("pccheck params=" + params);
  89. String result = doPost(url, params);
  90. System.out.println("pccheck result=" + result);
  91. if (result == null || result.equalsIgnoreCase("false")) {
  92. return null;
  93. }
  94. JdbcUtils.insertData(MesClient.mes_gw, "100000", params, "AQDW", sn.trim());
  95. return JSONObject.parseObject(result);
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. return null;
  99. }
  100. }
  101. public static JSONObject sendQuality(String sn, String ret, String user) {
  102. try {
  103. String url = "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProductRecord/pcresult";
  104. String params = "__ajax=json&oprno=" + MesClient.mes_gw.trim()
  105. + "&lineSn=" + MesClient.mes_line_sn.trim()
  106. + "&sn=" + sn.trim()
  107. + "&result=" + ret.trim()
  108. + "&ucode=" + user.trim()
  109. + "&__sid=" + MesClient.sessionid;
  110. System.out.println("pcresult params=" + params);
  111. String result = doPost(url, params);
  112. System.out.println("pcresult result=" + result);
  113. if (result == null || result.equalsIgnoreCase("false")) {
  114. return null;
  115. }
  116. JdbcUtils.insertData(MesClient.mes_gw, "100000", params, "MQDW", sn.trim());
  117. return JSONObject.parseObject(result);
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return null;
  121. }
  122. }
  123. public static Boolean bindUser(NettyClient nettyClient,String sn,String user){
  124. try{
  125. String msgType = "MBDW";
  126. String gy = "400001";
  127. String param = rightPad(user, 36);
  128. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"01",param);
  129. return aqdw_str;
  130. }catch (Exception e){
  131. return false;
  132. }
  133. }
  134. public static Boolean bindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  135. try{
  136. String msgType = "MBDW";
  137. String param = rightPad(wsn, 36);
  138. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  139. return aqdw_str;
  140. }catch (Exception e){
  141. return false;
  142. }
  143. }
  144. public static Boolean unBindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  145. try{
  146. String msgType = "MJBW";
  147. String param = rightPad(wsn, 36);
  148. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
  149. return aqdw_str;
  150. }catch (Exception e){
  151. return false;
  152. }
  153. }
  154. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
  155. try{
  156. String enconding = "UTF-8";
  157. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  158. Properties pro = new Properties();
  159. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  160. pro.load(br);
  161. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  162. String start = "aaaabbbbbABW";
  163. String gy = "GY" + rightPad(craft, 6);
  164. String reslx = "LX" + rightPad(lx, 2);
  165. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  166. String rs = "RS"+ rightPad(result, 2);
  167. String da = "DA" + DateLocalUtils.getCurrentDate();
  168. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  169. String yh = "YH" + rightPad(user, 20);
  170. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  171. System.out.println("message="+aqdw_str);
  172. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  173. nettyClient.future.channel().writeAndFlush(aqdw_str);
  174. return true;
  175. }catch (Exception e){
  176. return false;
  177. }
  178. }
  179. public static String rightPad(final String str, final int size) {
  180. if (str == null) {
  181. return null;
  182. }
  183. String strret = str;
  184. if(str.length() > size){
  185. strret = str.substring(0,size);
  186. }
  187. return String.format("%-"+size+"s", strret);
  188. }
  189. public static JSONObject getBindMaterail() {
  190. try{
  191. String enconding = "UTF-8";
  192. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  193. Properties pro = new Properties();
  194. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  195. pro.load(br);
  196. String mes_server_ip = pro.getProperty("mes.server_ip");
  197. String oprno = pro.getProperty("mes.gw").trim();
  198. String lineSn = pro.getProperty("mes.line_sn").trim();
  199. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  200. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  201. System.out.println("params="+params);
  202. String result = doPost(url,params);
  203. System.out.println("result="+result);
  204. if(result.equalsIgnoreCase("false")) {
  205. return null;
  206. }else {
  207. return JSONObject.parseObject(result);
  208. }
  209. }catch (Exception e){
  210. return null;
  211. }
  212. }
  213. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  214. try{
  215. String enconding = "UTF-8";
  216. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  217. Properties pro = new Properties();
  218. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  219. pro.load(br);
  220. String mes_server_ip = pro.getProperty("mes.server_ip");
  221. String oprno = pro.getProperty("mes.gw").trim();
  222. String lineSn = pro.getProperty("mes.line_sn").trim();
  223. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  224. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  225. System.out.println("params="+params);
  226. String result = doPost(url,params);
  227. System.out.println("result="+result);
  228. if(result.equalsIgnoreCase("false")) {
  229. return null;
  230. }else {
  231. return JSONObject.parseObject(result);
  232. }
  233. }catch (Exception e){
  234. return null;
  235. }
  236. }
  237. public static JSONObject upParams(String upparams) {
  238. try{
  239. String mes_server_ip = MesClient.mes_server_ip;
  240. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductProd/batchsave";
  241. String params = "__ajax=json&oprno="+MesClient.mes_gw+"&lineSn="+MesClient.mes_line_sn+"&params="+upparams;
  242. System.out.println("params="+params);
  243. String result = doPost(url,params);
  244. System.out.println("result="+result);
  245. if(result.equalsIgnoreCase("false")) {
  246. return null;
  247. }else {
  248. return JSONObject.parseObject(result);
  249. }
  250. }catch (Exception e){
  251. return null;
  252. }
  253. }
  254. public static String doPost(String httpUrl, String param) {
  255. HttpURLConnection connection = null;
  256. InputStream is = null;
  257. OutputStream os = null;
  258. BufferedReader br = null;
  259. String result = null;
  260. try {
  261. URL url = new URL(httpUrl);
  262. connection = (HttpURLConnection) url.openConnection();
  263. connection.setRequestMethod("POST");
  264. connection.setConnectTimeout(15000);
  265. connection.setReadTimeout(60000);
  266. connection.setDoOutput(true);
  267. connection.setDoInput(true);
  268. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  269. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  270. os = connection.getOutputStream();
  271. os.write(param.getBytes());
  272. if (connection.getResponseCode() == 200) {
  273. is = connection.getInputStream();
  274. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  275. StringBuffer sbf = new StringBuffer();
  276. String temp = null;
  277. while ((temp = br.readLine()) != null) {
  278. sbf.append(temp);
  279. sbf.append("\r\n");
  280. }
  281. result = sbf.toString();
  282. }
  283. } catch (MalformedURLException e) {
  284. e.printStackTrace();
  285. } catch (IOException e) {
  286. e.printStackTrace();
  287. } finally {
  288. if (null != br) {
  289. try {
  290. br.close();
  291. } catch (IOException e) {
  292. e.printStackTrace();
  293. }
  294. }
  295. if (null != os) {
  296. try {
  297. os.close();
  298. } catch (IOException e) {
  299. e.printStackTrace();
  300. }
  301. }
  302. if (null != is) {
  303. try {
  304. is.close();
  305. } catch (IOException e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. connection.disconnect();
  310. }
  311. return result;
  312. }
  313. }