DataUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 sendQualityOld(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 sendQuality(NettyClient nettyClient,String sn,String ret,String user){
  113. try{
  114. String msgType = "MQDW";
  115. String craft = "100000";
  116. String params = rightPad("Y"+MesClient.programNo, 12);
  117. String enconding = "UTF-8";
  118. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  119. Properties pro = new Properties();
  120. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  121. pro.load(br);
  122. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  123. String start = "aaaabbbbbABW";
  124. String gy = "GY" + rightPad(craft, 6);
  125. String reslx = "LX" + rightPad("", 2);
  126. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  127. String rs = "RS"+ rightPad(ret, 2);
  128. String da = "DA" + DateLocalUtils.getCurrentDate();
  129. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  130. String yh = "YH" + rightPad(user, 20);
  131. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + "01" + params;
  132. System.out.println("message="+aqdw_str);
  133. if(!sn.isEmpty()){
  134. // JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
  135. nettyClient.future.channel().writeAndFlush(aqdw_str);
  136. }
  137. return true;
  138. }catch (Exception e){
  139. return false;
  140. }
  141. }
  142. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
  143. try{
  144. String enconding = "UTF-8";
  145. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  146. Properties pro = new Properties();
  147. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  148. pro.load(br);
  149. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  150. String start = "aaaabbbbbABW";
  151. String gy = "GY" + rightPad(craft, 6);
  152. String reslx = "LX" + rightPad(lx, 2);
  153. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  154. String rs = "RS"+ rightPad(result, 2);
  155. String da = "DA" + DateLocalUtils.getCurrentDate();
  156. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  157. String yh = "YH" + rightPad(user, 20);
  158. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  159. System.out.println("message="+aqdw_str);
  160. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  161. nettyClient.future.channel().writeAndFlush(aqdw_str);
  162. return true;
  163. }catch (Exception e){
  164. return false;
  165. }
  166. }
  167. public static String rightPad(final String str, final int size) {
  168. if (str == null) {
  169. return null;
  170. }
  171. String strret = str;
  172. if(str.length() > size){
  173. strret = str.substring(0,size);
  174. }
  175. return String.format("%-"+size+"s", strret);
  176. }
  177. public static JSONObject getBindMaterail() {
  178. try{
  179. String enconding = "UTF-8";
  180. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  181. Properties pro = new Properties();
  182. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  183. pro.load(br);
  184. String mes_server_ip = pro.getProperty("mes.server_ip");
  185. String oprno = pro.getProperty("mes.gw").trim();
  186. String lineSn = pro.getProperty("mes.line_sn").trim();
  187. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  188. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  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 JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  202. try{
  203. String enconding = "UTF-8";
  204. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  205. Properties pro = new Properties();
  206. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  207. pro.load(br);
  208. String mes_server_ip = pro.getProperty("mes.server_ip");
  209. String oprno = pro.getProperty("mes.gw").trim();
  210. String lineSn = pro.getProperty("mes.line_sn").trim();
  211. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  212. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  213. System.out.println("params="+params);
  214. String result = doPost(url,params);
  215. System.out.println("result="+result);
  216. if(result.equalsIgnoreCase("false")) {
  217. return null;
  218. }else {
  219. return JSONObject.parseObject(result);
  220. }
  221. }catch (Exception e){
  222. return null;
  223. }
  224. }
  225. public static String doPost(String httpUrl, String param) {
  226. HttpURLConnection connection = null;
  227. InputStream is = null;
  228. OutputStream os = null;
  229. BufferedReader br = null;
  230. String result = null;
  231. try {
  232. URL url = new URL(httpUrl);
  233. connection = (HttpURLConnection) url.openConnection();
  234. connection.setRequestMethod("POST");
  235. connection.setConnectTimeout(15000);
  236. connection.setReadTimeout(60000);
  237. connection.setDoOutput(true);
  238. connection.setDoInput(true);
  239. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  240. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  241. os = connection.getOutputStream();
  242. os.write(param.getBytes());
  243. if (connection.getResponseCode() == 200) {
  244. is = connection.getInputStream();
  245. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  246. StringBuffer sbf = new StringBuffer();
  247. String temp = null;
  248. while ((temp = br.readLine()) != null) {
  249. sbf.append(temp);
  250. sbf.append("\r\n");
  251. }
  252. result = sbf.toString();
  253. }
  254. } catch (MalformedURLException e) {
  255. e.printStackTrace();
  256. } catch (IOException e) {
  257. e.printStackTrace();
  258. } finally {
  259. if (null != br) {
  260. try {
  261. br.close();
  262. } catch (IOException e) {
  263. e.printStackTrace();
  264. }
  265. }
  266. if (null != os) {
  267. try {
  268. os.close();
  269. } catch (IOException e) {
  270. e.printStackTrace();
  271. }
  272. }
  273. if (null != is) {
  274. try {
  275. is.close();
  276. } catch (IOException e) {
  277. e.printStackTrace();
  278. }
  279. }
  280. connection.disconnect();
  281. }
  282. return result;
  283. }
  284. }