DataUtil.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params,String op){
  146. try{
  147. String enconding = "UTF-8";
  148. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  149. Properties pro = new Properties();
  150. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  151. pro.load(br);
  152. if(MesClient.mes_gwflag.equals("B")){
  153. if(op.equals("A")){
  154. op = "C";
  155. }else{
  156. op = "D";
  157. }
  158. }
  159. String gw = "GW"+rightPad(pro.getProperty("mes.gw")+op, 6);
  160. String start = "aaaabbbbbABW";
  161. String gy = "GY" + rightPad(craft, 6);
  162. String reslx = "LX" + rightPad(lx, 2);
  163. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  164. String rs = "RS"+ rightPad(result, 2);
  165. String da = "DA" + DateLocalUtils.getCurrentDate();
  166. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  167. String yh = "YH" + rightPad(user, 20);
  168. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  169. System.out.println("message="+aqdw_str);
  170. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  171. log.info("SEND:"+aqdw_str);
  172. nettyClient.future.channel().writeAndFlush(aqdw_str);
  173. return true;
  174. }catch (Exception e){
  175. return false;
  176. }
  177. }
  178. public static String rightPad(final String str, final int size) {
  179. if (str == null) {
  180. return null;
  181. }
  182. String strret = str;
  183. if(str.length() > size){
  184. strret = str.substring(0,size);
  185. }
  186. return String.format("%-"+size+"s", strret);
  187. }
  188. public static JSONObject upParams(String upparams) {
  189. try{
  190. String enconding = "UTF-8"; //涓嶇windows杩樻槸linux閮芥槸UTF-8
  191. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  192. Properties pro = new Properties();
  193. //閬垮厤涓枃涔辩爜
  194. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  195. pro.load(br);
  196. String mes_server_ip = pro.getProperty("mes.server_ip");
  197. String oprno = pro.getProperty("mes.gw").trim();
  198. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductCmt/batchsave";
  199. String params = "__ajax=json&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 JSONObject getBindMaterail() {
  213. try{
  214. String enconding = "UTF-8";
  215. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  216. Properties pro = new Properties();
  217. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  218. pro.load(br);
  219. String mes_server_ip = pro.getProperty("mes.server_ip");
  220. String oprno = pro.getProperty("mes.gw").trim();
  221. String lineSn = pro.getProperty("mes.line_sn").trim();
  222. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  223. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  224. System.out.println("params="+params);
  225. String result = doPost(url,params);
  226. System.out.println("result="+result);
  227. if(result.equalsIgnoreCase("false")) {
  228. return null;
  229. }else {
  230. return JSONObject.parseObject(result);
  231. }
  232. }catch (Exception e){
  233. return null;
  234. }
  235. }
  236. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  237. try{
  238. String enconding = "UTF-8";
  239. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  240. Properties pro = new Properties();
  241. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  242. pro.load(br);
  243. String mes_server_ip = pro.getProperty("mes.server_ip");
  244. String oprno = pro.getProperty("mes.gw").trim();
  245. String lineSn = pro.getProperty("mes.line_sn").trim();
  246. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  247. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  248. System.out.println("params="+params);
  249. String result = doPost(url,params);
  250. System.out.println("result="+result);
  251. if(result.equalsIgnoreCase("false")) {
  252. return null;
  253. }else {
  254. return JSONObject.parseObject(result);
  255. }
  256. }catch (Exception e){
  257. return null;
  258. }
  259. }
  260. public static JSONObject saveGzCheck(String content,String userCode) {
  261. try{
  262. String enconding = "UTF-8";
  263. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  264. Properties pro = new Properties();
  265. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  266. pro.load(br);
  267. String mes_server_ip = pro.getProperty("mes.server_ip");
  268. String oprno = pro.getProperty("mes.gw").trim();
  269. String lineSn = pro.getProperty("mes.line_sn").trim();
  270. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessCheckGz/submit";
  271. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&createBy="+userCode;
  272. System.out.println("params="+params);
  273. String result = doPost(url,params);
  274. System.out.println("result="+result);
  275. if(result.equalsIgnoreCase("false")) {
  276. return null;
  277. }else {
  278. return JSONObject.parseObject(result);
  279. }
  280. }catch (Exception e){
  281. return null;
  282. }
  283. }
  284. public static String doPost(String httpUrl, String param) {
  285. HttpURLConnection connection = null;
  286. InputStream is = null;
  287. OutputStream os = null;
  288. BufferedReader br = null;
  289. String result = null;
  290. try {
  291. URL url = new URL(httpUrl);
  292. connection = (HttpURLConnection) url.openConnection();
  293. connection.setRequestMethod("POST");
  294. connection.setConnectTimeout(15000);
  295. connection.setReadTimeout(60000);
  296. connection.setDoOutput(true);
  297. connection.setDoInput(true);
  298. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  299. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  300. os = connection.getOutputStream();
  301. os.write(param.getBytes());
  302. if (connection.getResponseCode() == 200) {
  303. is = connection.getInputStream();
  304. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  305. StringBuffer sbf = new StringBuffer();
  306. String temp = null;
  307. while ((temp = br.readLine()) != null) {
  308. sbf.append(temp);
  309. sbf.append("\r\n");
  310. }
  311. result = sbf.toString();
  312. }
  313. } catch (MalformedURLException e) {
  314. e.printStackTrace();
  315. } catch (IOException e) {
  316. e.printStackTrace();
  317. } finally {
  318. if (null != br) {
  319. try {
  320. br.close();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. if (null != os) {
  326. try {
  327. os.close();
  328. } catch (IOException e) {
  329. e.printStackTrace();
  330. }
  331. }
  332. if (null != is) {
  333. try {
  334. is.close();
  335. } catch (IOException e) {
  336. e.printStackTrace();
  337. }
  338. }
  339. connection.disconnect();
  340. }
  341. return result;
  342. }
  343. public static JSONObject bindWarehouseData(String sn,String wsn,String userCode,String craft){
  344. try{
  345. String enconding = "UTF-8";
  346. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  347. Properties pro = new Properties();
  348. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  349. pro.load(br);
  350. String mes_server_ip = pro.getProperty("mes.server_ip");
  351. String oprno = pro.getProperty("mes.gw").trim();
  352. String lineSn = pro.getProperty("mes.line_sn").trim();
  353. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/bindWarehouse";
  354. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&ucode="+userCode+"&sn="+sn+"&wsn="+wsn+"&craft="+craft;
  355. System.out.println("params="+params);
  356. String result = doPost(url,params);
  357. System.out.println("result="+result);
  358. if(result.equalsIgnoreCase("false")) {
  359. return null;
  360. }else {
  361. return JSONObject.parseObject(result);
  362. }
  363. }catch (Exception e){
  364. return null;
  365. }
  366. }
  367. public static JSONObject unBindWarehouseData(String sn,String wsn,String userCode,String craft){
  368. try{
  369. String enconding = "UTF-8";
  370. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  371. Properties pro = new Properties();
  372. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  373. pro.load(br);
  374. String mes_server_ip = pro.getProperty("mes.server_ip");
  375. String oprno = pro.getProperty("mes.gw").trim();
  376. String lineSn = pro.getProperty("mes.line_sn").trim();
  377. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/unBindWarehouse";
  378. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&ucode="+userCode+"&sn="+sn+"&wsn="+wsn+"&craft="+craft;
  379. System.out.println("params="+params);
  380. String result = doPost(url,params);
  381. System.out.println("result="+result);
  382. if(result.equalsIgnoreCase("false")) {
  383. return null;
  384. }else {
  385. return JSONObject.parseObject(result);
  386. }
  387. }catch (Exception e){
  388. return null;
  389. }
  390. }
  391. }