DataUtil.java 12 KB

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