DataUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 sendCreate(NettyClient nettyClient,String sn,String ret,String user,String wsn){
  113. try{
  114. String msgType = "MCJW";
  115. String gy = "100000";
  116. String param = rightPad("Y"+wsn, 36);
  117. // String param = "";
  118. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,"01",param);
  119. return aqdw_str;
  120. }catch (Exception e){
  121. return false;
  122. }
  123. }
  124. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
  125. try{
  126. String enconding = "UTF-8";
  127. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  128. Properties pro = new Properties();
  129. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  130. pro.load(br);
  131. String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
  132. String start = "aaaabbbbbABW";
  133. String gy = "GY" + rightPad(craft, 6);
  134. String reslx = "LX" + rightPad(lx, 2);
  135. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  136. String rs = "RS"+ rightPad(result, 2);
  137. String da = "DA" + DateLocalUtils.getCurrentDate();
  138. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  139. String yh = "YH" + rightPad(user, 20);
  140. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  141. System.out.println("message="+aqdw_str);
  142. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  143. nettyClient.future.channel().writeAndFlush(aqdw_str);
  144. return true;
  145. }catch (Exception e){
  146. return false;
  147. }
  148. }
  149. public static String rightPad(final String str, final int size) {
  150. if (str == null) {
  151. return null;
  152. }
  153. String strret = str;
  154. if(str.length() > size){
  155. strret = str.substring(0,size);
  156. }
  157. return String.format("%-"+size+"s", strret);
  158. }
  159. public static JSONObject getCheckResult() {
  160. try{
  161. String enconding = "UTF-8";
  162. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  163. Properties pro = new Properties();
  164. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  165. pro.load(br);
  166. String mes_server_ip = pro.getProperty("mes.server_ip");
  167. String oprno = pro.getProperty("mes.gw").trim();
  168. String lineSn = pro.getProperty("mes.line_sn").trim();
  169. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessCheckRecord/check";
  170. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  171. System.out.println("params="+params);
  172. String result = doPost(url,params);
  173. System.out.println("result="+result);
  174. if(result.equalsIgnoreCase("false")) {
  175. return null;
  176. }else {
  177. return JSONObject.parseObject(result);
  178. }
  179. }catch (Exception e){
  180. return null;
  181. }
  182. }
  183. public static JSONObject getBindMaterail() {
  184. try{
  185. String enconding = "UTF-8";
  186. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  187. Properties pro = new Properties();
  188. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  189. pro.load(br);
  190. String mes_server_ip = pro.getProperty("mes.server_ip");
  191. String oprno = pro.getProperty("mes.gw").trim();
  192. String lineSn = pro.getProperty("mes.line_sn").trim();
  193. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  194. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  195. System.out.println("params="+params);
  196. String result = doPost(url,params);
  197. System.out.println("result="+result);
  198. if(result.equalsIgnoreCase("false")) {
  199. return null;
  200. }else {
  201. return JSONObject.parseObject(result);
  202. }
  203. }catch (Exception e){
  204. return null;
  205. }
  206. }
  207. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  208. try{
  209. String enconding = "UTF-8";
  210. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  211. Properties pro = new Properties();
  212. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  213. pro.load(br);
  214. String mes_server_ip = pro.getProperty("mes.server_ip");
  215. String oprno = pro.getProperty("mes.gw").trim();
  216. String lineSn = pro.getProperty("mes.line_sn").trim();
  217. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  218. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  219. System.out.println("params="+params);
  220. String result = doPost(url,params);
  221. System.out.println("result="+result);
  222. if(result.equalsIgnoreCase("false")) {
  223. return null;
  224. }else {
  225. return JSONObject.parseObject(result);
  226. }
  227. }catch (Exception e){
  228. return null;
  229. }
  230. }
  231. /**
  232. * 调用mescloud绑定规则校验接口,校验工件码与前梁码是否匹配
  233. * @param code1 工件码前22位
  234. * @param code2 前梁码前12位
  235. * @return true=匹配 false=不匹配 null=请求失败
  236. */
  237. public static Boolean checkBindRule(String code1, String code2) {
  238. try{
  239. String enconding = "UTF-8";
  240. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  241. Properties pro = new Properties();
  242. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  243. pro.load(br);
  244. String mes_server_ip = pro.getProperty("mes.server_ip");
  245. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialBindRule/check";
  246. String params = "code1="+code1+"&code2="+code2;
  247. System.out.println("params="+params);
  248. String result = doPost(url,params);
  249. System.out.println("result="+result);
  250. if(result == null) {
  251. return null;
  252. }
  253. result = result.trim();
  254. if(result.equalsIgnoreCase("true")) {
  255. return true;
  256. }
  257. if(result.equalsIgnoreCase("false")) {
  258. return false;
  259. }
  260. return null;
  261. }catch (Exception e){
  262. e.printStackTrace();
  263. return null;
  264. }
  265. }
  266. public static JSONObject sendResult(String sn,String ret,String user,String wsn) {
  267. try{
  268. String enconding = "UTF-8";
  269. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  270. Properties pro = new Properties();
  271. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  272. pro.load(br);
  273. String mes_server_ip = pro.getProperty("mes.server_ip");
  274. String oprno = pro.getProperty("mes.gw").trim();
  275. String lineSn = pro.getProperty("mes.line_sn").trim();
  276. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/create";
  277. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&ucode="+user+"&result="+ret+"&sn="+sn+"&wsn="+wsn;
  278. System.out.println("params="+params);
  279. String result = doPost(url,params);
  280. System.out.println("result="+result);
  281. if(result.equalsIgnoreCase("false")) {
  282. return null;
  283. }else {
  284. return JSONObject.parseObject(result);
  285. }
  286. }catch (Exception e){
  287. return null;
  288. }
  289. }
  290. public static String doPost(String httpUrl, String param) {
  291. HttpURLConnection connection = null;
  292. InputStream is = null;
  293. OutputStream os = null;
  294. BufferedReader br = null;
  295. String result = null;
  296. try {
  297. URL url = new URL(httpUrl);
  298. connection = (HttpURLConnection) url.openConnection();
  299. connection.setRequestMethod("POST");
  300. connection.setConnectTimeout(15000);
  301. connection.setReadTimeout(60000);
  302. connection.setDoOutput(true);
  303. connection.setDoInput(true);
  304. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  305. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  306. os = connection.getOutputStream();
  307. os.write(param.getBytes());
  308. if (connection.getResponseCode() == 200) {
  309. is = connection.getInputStream();
  310. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  311. StringBuffer sbf = new StringBuffer();
  312. String temp = null;
  313. while ((temp = br.readLine()) != null) {
  314. sbf.append(temp);
  315. sbf.append("\r\n");
  316. }
  317. result = sbf.toString();
  318. }
  319. } catch (MalformedURLException e) {
  320. e.printStackTrace();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. } finally {
  324. if (null != br) {
  325. try {
  326. br.close();
  327. } catch (IOException e) {
  328. e.printStackTrace();
  329. }
  330. }
  331. if (null != os) {
  332. try {
  333. os.close();
  334. } catch (IOException e) {
  335. e.printStackTrace();
  336. }
  337. }
  338. if (null != is) {
  339. try {
  340. is.close();
  341. } catch (IOException e) {
  342. e.printStackTrace();
  343. }
  344. }
  345. connection.disconnect();
  346. }
  347. return result;
  348. }
  349. }