DataUtil.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.io.*;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Properties;
  13. public class DataUtil {
  14. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  15. public static Boolean synrTcp(NettyClient nettyClient,String mes_gw){
  16. try{
  17. //TCP连接后,直接先发同步报文
  18. String start = "aaaabbbbbABW";
  19. String msgType = "SYNR";
  20. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  21. String da = "DA" + DateLocalUtils.getCurrentDate();
  22. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  23. String synr_str = start + msgType + gw + da + zt;
  24. //插入报文本地数据
  25. String gy = "";
  26. String sn = "";
  27. JdbcUtils.insertData(gw, gy, synr_str, msgType, sn);
  28. nettyClient.run(synr_str);
  29. return true;
  30. }catch (Exception e){
  31. return false;
  32. }
  33. }
  34. public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
  35. try{
  36. //TCP连接后,直接先发同步报文
  37. String start = "aaaabbbbbABW";
  38. String msgType = "AXTW";
  39. String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
  40. String da = "DA" + DateLocalUtils.getCurrentDate();
  41. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  42. String axtw_str = start + msgType + gw + da + zt;
  43. //插入报文本地数据
  44. String gy = "";
  45. String sn = "";
  46. JdbcUtils.insertData(gw, gy, axtw_str, msgType, sn);
  47. nettyClient.future.channel().writeAndFlush(axtw_str);
  48. return true;
  49. }catch (Exception e){
  50. return false;
  51. }
  52. }
  53. public static Boolean checkQuality(NettyClient nettyClient, String sn, String user,String op){
  54. try{
  55. String msgType = "AQDW";
  56. String gy = "100000";
  57. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","",op);
  58. return aqdw_str;
  59. }catch (Exception e){
  60. return false;
  61. }
  62. }
  63. public static Boolean startWork(NettyClient nettyClient,String sn,String user,String op){
  64. try{
  65. String msgType = "MKSW";
  66. String gy = "";
  67. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","",op);
  68. return aqdw_str;
  69. }catch (Exception e){
  70. return false;
  71. }
  72. }
  73. public static Boolean bindUser(NettyClient nettyClient,String sn,String user){
  74. try{
  75. String msgType = "MBDW";
  76. String gy = "400001";
  77. String param = rightPad(user, 36);
  78. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"01",param,"");
  79. return aqdw_str;
  80. }catch (Exception e){
  81. return false;
  82. }
  83. }
  84. public static Boolean bindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  85. try{
  86. String msgType = "MBDW";
  87. String param = rightPad(wsn, 36);
  88. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param,"");
  89. return aqdw_str;
  90. }catch (Exception e){
  91. return false;
  92. }
  93. }
  94. public static Boolean unBindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
  95. try{
  96. String msgType = "MJBW";
  97. String param = rightPad(wsn, 36);
  98. Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param,"");
  99. return aqdw_str;
  100. }catch (Exception e){
  101. return false;
  102. }
  103. }
  104. public static Boolean sendQuality(NettyClient nettyClient,String sn,String ret,String user,String op){
  105. // return false;
  106. try{
  107. String msgType = "MQDW";
  108. String gy = "100000";
  109. String param = "";
  110. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,"00",param,op);
  111. return aqdw_str;
  112. }catch (Exception e){
  113. return false;
  114. }
  115. }
  116. // public static Boolean sendQuality(String sn,String result,String user,String op){
  117. // try{
  118. // String msgType = "MQDW";
  119. // String craft = "100000";
  120. // String lx = "";
  121. // String paramNums = "00";
  122. // String params = "";
  123. //
  124. // InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  125. // Properties pro = new Properties();
  126. // BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  127. // pro.load(br);
  128. // String gw = "GW"+rightPad(pro.getProperty("mes.gw")+op, 6);
  129. // String start = "aaaabbbbbABW";
  130. // String gy = "GY" + rightPad(craft, 6);
  131. // String reslx = "LX" + rightPad(lx, 2);
  132. // String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  133. // String rs = "RS"+ rightPad(result, 2);
  134. // String da = "DA" + DateLocalUtils.getCurrentDate();
  135. // String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  136. // String yh = "YH" + rightPad(user, 20);
  137. // String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  138. // JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  139. // JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
  140. // return true;
  141. // }catch (Exception e){
  142. // return false;
  143. // }
  144. // }
  145. /**
  146. * 按工位标识映射上传工位号:
  147. * gwflag=A:A面->OP080A,B面->OP080B
  148. * gwflag=B:A面->OP080C,B面->OP080D
  149. */
  150. public static String buildGwOprno(String mesGw, String sideOp){
  151. String gw = mesGw == null ? "" : mesGw.trim();
  152. String op = sideOp == null ? "" : sideOp;
  153. if("B".equals(MesClient.mes_gwflag)){
  154. if("A".equals(op)){
  155. op = "C";
  156. }else if("B".equals(op)){
  157. op = "D";
  158. }
  159. }
  160. return rightPad(gw + op, 6);
  161. }
  162. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params,String op){
  163. try{
  164. String enconding = "UTF-8";
  165. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  166. Properties pro = new Properties();
  167. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  168. pro.load(br);
  169. // 080A机:AB面上传OP080A/OP080B;080B机:AB面上传OP080C/OP080D
  170. if(MesClient.mes_gwflag != null && MesClient.mes_gwflag.equals("B")){
  171. if(op.equals("A")){
  172. op = "C";
  173. }else if(op.equals("B")){
  174. op = "D";
  175. }
  176. }
  177. String gw = "GW"+rightPad(pro.getProperty("mes.gw")+op, 6);
  178. String start = "aaaabbbbbABW";
  179. String gy = "GY" + rightPad(craft, 6);
  180. String reslx = "LX" + rightPad(lx, 2);
  181. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  182. String rs = "RS"+ rightPad(result, 2);
  183. String da = "DA" + DateLocalUtils.getCurrentDate();
  184. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  185. String yh = "YH" + rightPad(user, 20);
  186. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  187. System.out.println("message="+aqdw_str);
  188. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  189. log.info("SEND:"+aqdw_str);
  190. nettyClient.future.channel().writeAndFlush(aqdw_str);
  191. return true;
  192. }catch (Exception e){
  193. return false;
  194. }
  195. }
  196. public static String rightPad(final String str, final int size) {
  197. if (str == null) {
  198. return null;
  199. }
  200. String strret = str;
  201. if(str.length() > size){
  202. strret = str.substring(0,size);
  203. }
  204. return String.format("%-"+size+"s", strret);
  205. }
  206. public static JSONObject upParams(String upparams) {
  207. try{
  208. String enconding = "UTF-8"; //涓嶇windows杩樻槸linux閮芥槸UTF-8
  209. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  210. Properties pro = new Properties();
  211. //閬垮厤涓枃涔辩爜
  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 url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductCmt/batchsave";
  217. String params = "__ajax=json&params="+upparams;
  218. System.out.println("params="+params);
  219. String result = doPost(url,params);
  220. System.out.println("result="+result);
  221. if(result.equalsIgnoreCase("false")) {
  222. return null;
  223. }else {
  224. return JSONObject.parseObject(result);
  225. }
  226. }catch (Exception e){
  227. return null;
  228. }
  229. }
  230. public static JSONObject getBindMaterail() {
  231. try{
  232. String enconding = "UTF-8";
  233. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  234. Properties pro = new Properties();
  235. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  236. pro.load(br);
  237. String mes_server_ip = pro.getProperty("mes.server_ip");
  238. String oprno = pro.getProperty("mes.gw").trim();
  239. String lineSn = pro.getProperty("mes.line_sn").trim();
  240. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  241. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  242. System.out.println("params="+params);
  243. String result = doPost(url,params);
  244. System.out.println("result="+result);
  245. if(result.equalsIgnoreCase("false")) {
  246. return null;
  247. }else {
  248. return JSONObject.parseObject(result);
  249. }
  250. }catch (Exception e){
  251. return null;
  252. }
  253. }
  254. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  255. try{
  256. String enconding = "UTF-8";
  257. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  258. Properties pro = new Properties();
  259. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  260. pro.load(br);
  261. String mes_server_ip = pro.getProperty("mes.server_ip");
  262. String oprno = pro.getProperty("mes.gw").trim();
  263. String lineSn = pro.getProperty("mes.line_sn").trim();
  264. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  265. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  266. System.out.println("params="+params);
  267. String result = doPost(url,params);
  268. System.out.println("result="+result);
  269. if(result.equalsIgnoreCase("false")) {
  270. return null;
  271. }else {
  272. return JSONObject.parseObject(result);
  273. }
  274. }catch (Exception e){
  275. return null;
  276. }
  277. }
  278. public static JSONObject saveGzCheck(String content,String userCode) {
  279. try{
  280. String enconding = "UTF-8";
  281. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  282. Properties pro = new Properties();
  283. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  284. pro.load(br);
  285. String mes_server_ip = pro.getProperty("mes.server_ip");
  286. String oprno = pro.getProperty("mes.gw").trim();
  287. String lineSn = pro.getProperty("mes.line_sn").trim();
  288. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessCheckGz/submit";
  289. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&createBy="+userCode;
  290. System.out.println("params="+params);
  291. String result = doPost(url,params);
  292. System.out.println("result="+result);
  293. if(result.equalsIgnoreCase("false")) {
  294. return null;
  295. }else {
  296. return JSONObject.parseObject(result);
  297. }
  298. }catch (Exception e){
  299. return null;
  300. }
  301. }
  302. public static String doPost(String httpUrl, String param) {
  303. HttpURLConnection connection = null;
  304. InputStream is = null;
  305. OutputStream os = null;
  306. BufferedReader br = null;
  307. String result = null;
  308. try {
  309. URL url = new URL(httpUrl);
  310. connection = (HttpURLConnection) url.openConnection();
  311. connection.setRequestMethod("POST");
  312. connection.setConnectTimeout(15000);
  313. connection.setReadTimeout(60000);
  314. connection.setDoOutput(true);
  315. connection.setDoInput(true);
  316. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  317. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  318. os = connection.getOutputStream();
  319. os.write(param.getBytes());
  320. if (connection.getResponseCode() == 200) {
  321. is = connection.getInputStream();
  322. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  323. StringBuffer sbf = new StringBuffer();
  324. String temp = null;
  325. while ((temp = br.readLine()) != null) {
  326. sbf.append(temp);
  327. sbf.append("\r\n");
  328. }
  329. result = sbf.toString();
  330. }
  331. } catch (MalformedURLException e) {
  332. e.printStackTrace();
  333. } catch (IOException e) {
  334. e.printStackTrace();
  335. } finally {
  336. if (null != br) {
  337. try {
  338. br.close();
  339. } catch (IOException e) {
  340. e.printStackTrace();
  341. }
  342. }
  343. if (null != os) {
  344. try {
  345. os.close();
  346. } catch (IOException e) {
  347. e.printStackTrace();
  348. }
  349. }
  350. if (null != is) {
  351. try {
  352. is.close();
  353. } catch (IOException e) {
  354. e.printStackTrace();
  355. }
  356. }
  357. connection.disconnect();
  358. }
  359. return result;
  360. }
  361. public static JSONObject bindWarehouseData(String sn,String wsn,String userCode,String craft){
  362. try{
  363. String enconding = "UTF-8";
  364. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  365. Properties pro = new Properties();
  366. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  367. pro.load(br);
  368. String mes_server_ip = pro.getProperty("mes.server_ip");
  369. String oprno = pro.getProperty("mes.gw").trim();
  370. String lineSn = pro.getProperty("mes.line_sn").trim();
  371. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/bindWarehouse";
  372. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&ucode="+userCode+"&sn="+sn+"&wsn="+wsn+"&craft="+craft;
  373. System.out.println("params="+params);
  374. String result = doPost(url,params);
  375. System.out.println("result="+result);
  376. if(result.equalsIgnoreCase("false")) {
  377. return null;
  378. }else {
  379. return JSONObject.parseObject(result);
  380. }
  381. }catch (Exception e){
  382. return null;
  383. }
  384. }
  385. /**
  386. * HTTP绑定压铸件二维码到工艺号400002的materiel_sn(同步等待返回)
  387. */
  388. public static JSONObject bindYzjHttp(String sn, String materielSn, String user, String craft){
  389. try{
  390. String mes_server_ip = MesClient.mes_server_ip;
  391. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  392. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  393. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/bindYzj";
  394. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result=OK&ucode="+user
  395. + "&craft="+craft+"&wsn="+materielSn;
  396. System.out.println("params="+params);
  397. String result = doPost(url,params);
  398. System.out.println("result="+result);
  399. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  400. return null;
  401. }else {
  402. return JSONObject.parseObject(result);
  403. }
  404. }catch (Exception e){
  405. e.printStackTrace();
  406. return null;
  407. }
  408. }
  409. public static JSONObject unBindWarehouseData(String sn,String wsn,String userCode,String craft){
  410. try{
  411. String enconding = "UTF-8";
  412. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  413. Properties pro = new Properties();
  414. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  415. pro.load(br);
  416. String mes_server_ip = pro.getProperty("mes.server_ip");
  417. String oprno = pro.getProperty("mes.gw").trim();
  418. String lineSn = pro.getProperty("mes.line_sn").trim();
  419. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/unBindWarehouse";
  420. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&ucode="+userCode+"&sn="+sn+"&wsn="+wsn+"&craft="+craft;
  421. System.out.println("params="+params);
  422. String result = doPost(url,params);
  423. System.out.println("result="+result);
  424. if(result.equalsIgnoreCase("false")) {
  425. return null;
  426. }else {
  427. return JSONObject.parseObject(result);
  428. }
  429. }catch (Exception e){
  430. return null;
  431. }
  432. }
  433. }