DataUtil.java 12 KB

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