DataUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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,String op){
  51. try{
  52. String msgType = "AQDW";
  53. String gy = "100000";
  54. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","",op);
  55. return aqdw_str;
  56. }catch (Exception e){
  57. return false;
  58. }
  59. }
  60. public static Boolean startWork(NettyClient nettyClient,String sn,String user,String op){
  61. try{
  62. String msgType = "MKSW";
  63. String gy = "";
  64. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","",op);
  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,String op){
  102. try{
  103. String msgType = "MQDW";
  104. String gy = "100000";
  105. String paramNums = "01";
  106. String params = "";
  107. if(op.equals("A")){
  108. params = rightPad("Y"+MesClient.programNoA, 12);
  109. }else{
  110. params = rightPad("Y"+MesClient.programNoB, 12);
  111. }
  112. Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,paramNums,params,op);
  113. return aqdw_str;
  114. }catch (Exception e){
  115. return false;
  116. }
  117. }
  118. public static Boolean sendQuality2(String sn,String result,String user,String op){
  119. try{
  120. String msgType = "MQDW";
  121. String craft = "100000";
  122. String lx = "";
  123. String paramNums = "01";
  124. String params = "";
  125. if(op.equals("A")){
  126. params = rightPad("Y"+MesClient.programNoA, 12);
  127. }else{
  128. params = rightPad("Y"+MesClient.programNoB, 12);
  129. }
  130. if(MesClient.mes_gwflag.equals("B")){
  131. if(op.equals("A")){
  132. op = "C";
  133. }else{
  134. op = "D";
  135. }
  136. }
  137. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  138. Properties pro = new Properties();
  139. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  140. pro.load(br);
  141. String gw = "GW"+rightPad(pro.getProperty("mes.gw")+op, 6);
  142. String start = "aaaabbbbbABW";
  143. String gy = "GY" + rightPad(craft, 6);
  144. String reslx = "LX" + rightPad(lx, 2);
  145. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  146. String rs = "RS"+ rightPad(result, 2);
  147. String da = "DA" + DateLocalUtils.getCurrentDate();
  148. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  149. String yh = "YH" + rightPad(user, 20);
  150. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  151. // JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  152. JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
  153. return true;
  154. }catch (Exception e){
  155. return false;
  156. }
  157. }
  158. public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params,String op){
  159. try{
  160. String enconding = "UTF-8";
  161. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  162. Properties pro = new Properties();
  163. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  164. pro.load(br);
  165. if(MesClient.mes_gwflag.equals("B")){
  166. if(op.equals("A")){
  167. op = "C";
  168. }else{
  169. op = "D";
  170. }
  171. }else if(MesClient.mes_gwflag.equals("C")){
  172. if(op.equals("A")){
  173. op = "E";
  174. }else{
  175. op = "F";
  176. }
  177. }
  178. String gw = "GW"+rightPad(pro.getProperty("mes.gw")+op, 6);
  179. String start = "aaaabbbbbABW";
  180. String gy = "GY" + rightPad(craft, 6);
  181. String reslx = "LX" + rightPad(lx, 2);
  182. String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
  183. String rs = "RS"+ rightPad(result, 2);
  184. String da = "DA" + DateLocalUtils.getCurrentDate();
  185. String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
  186. String yh = "YH" + rightPad(user, 20);
  187. String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
  188. System.out.println("message="+aqdw_str);
  189. JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
  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";
  209. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  210. Properties pro = new Properties();
  211. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  212. pro.load(br);
  213. String mes_server_ip = pro.getProperty("mes.server_ip");
  214. String oprno = pro.getProperty("mes.gw").trim();
  215. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductCmt/batchsave";
  216. String params = "__ajax=json&params="+upparams;
  217. System.out.println("params="+params);
  218. String result = doPost(url,params);
  219. System.out.println("result="+result);
  220. if(result.equalsIgnoreCase("false")) {
  221. return null;
  222. }else {
  223. return JSONObject.parseObject(result);
  224. }
  225. }catch (Exception e){
  226. return null;
  227. }
  228. }
  229. public static JSONObject getBindMaterail() {
  230. try{
  231. String enconding = "UTF-8";
  232. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  233. Properties pro = new Properties();
  234. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  235. pro.load(br);
  236. String mes_server_ip = pro.getProperty("mes.server_ip");
  237. String oprno = pro.getProperty("mes.gw").trim();
  238. String lineSn = pro.getProperty("mes.line_sn").trim();
  239. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  240. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  241. System.out.println("params="+params);
  242. String result = doPost(url,params);
  243. System.out.println("result="+result);
  244. if(result.equalsIgnoreCase("false")) {
  245. return null;
  246. }else {
  247. return JSONObject.parseObject(result);
  248. }
  249. }catch (Exception e){
  250. return null;
  251. }
  252. }
  253. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  254. try{
  255. String enconding = "UTF-8";
  256. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  257. Properties pro = new Properties();
  258. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  259. pro.load(br);
  260. String mes_server_ip = pro.getProperty("mes.server_ip");
  261. String oprno = pro.getProperty("mes.gw").trim();
  262. String lineSn = pro.getProperty("mes.line_sn").trim();
  263. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  264. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  265. System.out.println("params="+params);
  266. String result = doPost(url,params);
  267. System.out.println("result="+result);
  268. if(result.equalsIgnoreCase("false")) {
  269. return null;
  270. }else {
  271. return JSONObject.parseObject(result);
  272. }
  273. }catch (Exception e){
  274. return null;
  275. }
  276. }
  277. public static JSONObject saveGzCheck(String content,String userCode) {
  278. try{
  279. String enconding = "UTF-8";
  280. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  281. Properties pro = new Properties();
  282. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  283. pro.load(br);
  284. String mes_server_ip = pro.getProperty("mes.server_ip");
  285. String oprno = pro.getProperty("mes.gw").trim();
  286. String lineSn = pro.getProperty("mes.line_sn").trim();
  287. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessCheckGz/submit";
  288. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&createBy="+userCode;
  289. System.out.println("params="+params);
  290. String result = doPost(url,params);
  291. System.out.println("result="+result);
  292. if(result.equalsIgnoreCase("false")) {
  293. return null;
  294. }else {
  295. return JSONObject.parseObject(result);
  296. }
  297. }catch (Exception e){
  298. return null;
  299. }
  300. }
  301. public static String doPost(String httpUrl, String param) {
  302. HttpURLConnection connection = null;
  303. InputStream is = null;
  304. OutputStream os = null;
  305. BufferedReader br = null;
  306. String result = null;
  307. try {
  308. URL url = new URL(httpUrl);
  309. connection = (HttpURLConnection) url.openConnection();
  310. connection.setRequestMethod("POST");
  311. connection.setConnectTimeout(15000);
  312. connection.setReadTimeout(60000);
  313. connection.setDoOutput(true);
  314. connection.setDoInput(true);
  315. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  316. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  317. os = connection.getOutputStream();
  318. os.write(param.getBytes());
  319. if (connection.getResponseCode() == 200) {
  320. is = connection.getInputStream();
  321. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  322. StringBuffer sbf = new StringBuffer();
  323. String temp = null;
  324. while ((temp = br.readLine()) != null) {
  325. sbf.append(temp);
  326. sbf.append("\r\n");
  327. }
  328. result = sbf.toString();
  329. }
  330. } catch (MalformedURLException e) {
  331. e.printStackTrace();
  332. } catch (IOException e) {
  333. e.printStackTrace();
  334. } finally {
  335. if (null != br) {
  336. try {
  337. br.close();
  338. } catch (IOException e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. if (null != os) {
  343. try {
  344. os.close();
  345. } catch (IOException e) {
  346. e.printStackTrace();
  347. }
  348. }
  349. if (null != is) {
  350. try {
  351. is.close();
  352. } catch (IOException e) {
  353. e.printStackTrace();
  354. }
  355. }
  356. connection.disconnect();
  357. }
  358. return result;
  359. }
  360. }