DataUtil.java 11 KB

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