DataUtil.java 10 KB

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