| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSONObject;
- import com.mes.netty.NettyClient;
- import com.mes.util.DateLocalUtils;
- import com.mes.util.JdbcUtils;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.Map;
- import java.util.Properties;
- public class DataUtil {
- public static String lastUploadPressRivetMessage = "";
- public static Boolean synrTcp(NettyClient nettyClient,String mes_gw){
- try{
- //TCP杩炴帴鍚庯紝鐩存帴鍏堝彂鍚屾鎶ユ枃
- String start = "aaaabbbbbABW";
- String msgType = "SYNR";
- String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
- String da = "DA" + DateLocalUtils.getCurrentDate();
- String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
- String synr_str = start + msgType + gw + da + zt;
- //鎻掑叆鎶ユ枃鏈湴鏁版嵁
- String gy = "";
- String sn = "";
- JdbcUtils.insertData(gw, gy, synr_str, msgType, sn);
- nettyClient.run(synr_str);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
- try{
- //TCP杩炴帴鍚庯紝鐩存帴鍏堝彂鍚屾鎶ユ枃
- String start = "aaaabbbbbABW";
- String msgType = "AXTW";
- String gw = "GW"+DataUtil.rightPad(mes_gw, 6);
- String da = "DA" + DateLocalUtils.getCurrentDate();
- String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
- String axtw_str = start + msgType + gw + da + zt;
- //鎻掑叆鎶ユ枃鏈湴鏁版嵁
- String gy = "";
- String sn = "";
- JdbcUtils.insertData(gw, gy, axtw_str, msgType, sn);
- nettyClient.future.channel().writeAndFlush(axtw_str);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean checkQuality(NettyClient nettyClient, String sn, String user){
- try{
- String msgType = "AQDW";
- String gy = "100000";
- Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean startWork(NettyClient nettyClient,String sn,String user){
- try{
- String msgType = "MKSW";
- String gy = "";
- Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"00","");
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean bindUser(NettyClient nettyClient,String sn,String user){
- try{
- String msgType = "MBDW";
- String gy = "400001";
- String param = rightPad(user, 36);
- Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,"",user,"01",param);
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean bindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
- try{
- String msgType = "MBDW";
- String param = rightPad(wsn, 36);
- Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean unBindWarehouse(NettyClient nettyClient,String sn,String wsn,String user,String craft){
- try{
- String msgType = "MJBW";
- String param = rightPad(wsn, 36);
- Boolean aqdw_str = sendMessage(nettyClient,msgType,craft,"",sn,"",user,"01",param);
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean sendQuality(NettyClient nettyClient,String sn,String ret,String user){
- try{
- String msgType = "MQDW";
- String gy = "100000";
- String param = getPressParams();
- String paramNums = String.format("%02d", MesClient.pressParamTitle.length);
- Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,paramNums,param);
- return aqdw_str;
- }catch (Exception e){
- return false;
- }
- }
- public static String getPressParams(){
- StringBuilder params = new StringBuilder();
- for(int i=0; i<MesClient.pressParamTitle.length; i++){
- String title = MesClient.pressParamTitle[i] == null ? "" : MesClient.pressParamTitle[i];
- String value = MesClient.pressParamValue[i] == null ? "" : MesClient.pressParamValue[i];
- params.append(rightPad(title, 20));
- params.append(rightPad(value, 20));
- }
- return params.toString();
- }
- public static Boolean uploadPressRivetData(String sn, Map<Integer, Map<String, String>> pressRivetMap){
- try{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String mesServerIp = pro.getProperty("mes.server_ip");
- String oprno = pro.getProperty("mes.gw").trim();
- String lineSn = pro.getProperty("mes.line_sn").trim();
- String url = pro.getProperty("mes.press_rivet_upload_url");
- if(url == null || url.trim().isEmpty()){
- url = "http://"+mesServerIp+":8980/js/a/mes/mesProductRecord/yamao";
- }
- JSONObject json = new JSONObject();
- json.put("sn", sn == null ? "" : sn);
- json.put("oprno", oprno);
- json.put("lineSn", lineSn);
- json.put("datas", pressRivetMap.values());
- String params = json.toJSONString();
- System.out.println("json="+params);
- String result = doPostJson(url,params);
- System.out.println("result="+result);
- if(result == null || result.trim().isEmpty()){
- lastUploadPressRivetMessage = "上传失败,服务器无响应";
- return false;
- }
- if(result.trim().equalsIgnoreCase("false")){
- lastUploadPressRivetMessage = "上传失败";
- return false;
- }
- JSONObject resultObj = JSONObject.parseObject(result);
- lastUploadPressRivetMessage = resultObj.getString("Message");
- String uploadResult = resultObj.getString("Result");
- if((lastUploadPressRivetMessage == null || lastUploadPressRivetMessage.trim().isEmpty()) && !"SUCCESS".equalsIgnoreCase(uploadResult)){
- lastUploadPressRivetMessage = "上传失败";
- }
- return "SUCCESS".equalsIgnoreCase(uploadResult);
- }catch (Exception e){
- e.printStackTrace();
- lastUploadPressRivetMessage = "上传异常:" + e.getMessage();
- return false;
- }
- }
- public static Boolean sendQualityNew(NettyClient nettyClient,String sn,String ret,String user){
- try{
- String msgType = "MQDW";
- String craft = "100000";
- String params = "";
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
- String start = "aaaabbbbbABW";
- String gy = "GY" + rightPad(craft, 6);
- String reslx = "LX" + rightPad("", 2);
- String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
- String rs = "RS"+ rightPad(ret, 2);
- String da = "DA" + DateLocalUtils.getCurrentDate();
- String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
- String yh = "YH" + rightPad(user, 20);
- String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + "00" + params;
- System.out.println("message="+aqdw_str);
- JdbcUtils.insertSubmitRecord(gw, sn, aqdw_str);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public static Boolean sendMessage(NettyClient nettyClient,String msgType,String craft,String lx,String sn,String result,String user,String paramNums,String params){
- try{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String gw = "GW"+rightPad(pro.getProperty("mes.gw"), 6);
- String start = "aaaabbbbbABW";
- String gy = "GY" + rightPad(craft, 6);
- String reslx = "LX" + rightPad(lx, 2);
- String id = pro.getProperty("mes.line_sn") + rightPad(sn, 36);
- String rs = "RS"+ rightPad(result, 2);
- String da = "DA" + DateLocalUtils.getCurrentDate();
- String zt = "ZT" + DateLocalUtils.getCurrentTimeHMS();
- String yh = "YH" + rightPad(user, 20);
- String aqdw_str = start + msgType + gw + gy + reslx + id + rs + da + zt + yh + paramNums + params;
- System.out.println("message="+aqdw_str);
- JdbcUtils.insertData(gw, gy, aqdw_str, msgType, sn);
- nettyClient.future.channel().writeAndFlush(aqdw_str);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public static String rightPad(final String str, final int size) {
- if (str == null) {
- return null;
- }
- String strret = str;
- if(str.length() > size){
- strret = str.substring(0,size);
- }
- return String.format("%-"+size+"s", strret);
- }
- public static JSONObject getBindMaterail() {
- try{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String mes_server_ip = pro.getProperty("mes.server_ip");
- String oprno = pro.getProperty("mes.gw").trim();
- String lineSn = pro.getProperty("mes.line_sn").trim();
- String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
- String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
- System.out.println("params="+params);
- String result = doPost(url,params);
- System.out.println("result="+result);
- if(result.equalsIgnoreCase("false")) {
- return null;
- }else {
- return JSONObject.parseObject(result);
- }
- }catch (Exception e){
- return null;
- }
- }
- public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
- try{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String mes_server_ip = pro.getProperty("mes.server_ip");
- String oprno = pro.getProperty("mes.gw").trim();
- String lineSn = pro.getProperty("mes.line_sn").trim();
- String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
- String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
- System.out.println("params="+params);
- String result = doPost(url,params);
- System.out.println("result="+result);
- if(result.equalsIgnoreCase("false")) {
- return null;
- }else {
- return JSONObject.parseObject(result);
- }
- }catch (Exception e){
- return null;
- }
- }
- public static String doPost(String httpUrl, String param) {
- HttpURLConnection connection = null;
- InputStream is = null;
- OutputStream os = null;
- BufferedReader br = null;
- String result = null;
- try {
- URL url = new URL(httpUrl);
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setConnectTimeout(5000);
- connection.setReadTimeout(10000);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
- os = connection.getOutputStream();
- os.write(param.getBytes());
- if (connection.getResponseCode() == 200) {
- is = connection.getInputStream();
- br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- StringBuffer sbf = new StringBuffer();
- String temp = null;
- while ((temp = br.readLine()) != null) {
- sbf.append(temp);
- sbf.append("\r\n");
- }
- result = sbf.toString();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (null != br) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != os) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != is) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return result;
- }
- public static String doPostJson(String httpUrl, String json) {
- HttpURLConnection connection = null;
- InputStream is = null;
- OutputStream os = null;
- BufferedReader br = null;
- String result = null;
- try {
- URL url = new URL(httpUrl);
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setConnectTimeout(5000);
- connection.setReadTimeout(10000);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
- connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
- os = connection.getOutputStream();
- os.write(json.getBytes(StandardCharsets.UTF_8));
- if (connection.getResponseCode() == 200) {
- is = connection.getInputStream();
- br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- StringBuffer sbf = new StringBuffer();
- String temp = null;
- while ((temp = br.readLine()) != null) {
- sbf.append(temp);
- sbf.append("\r\n");
- }
- result = sbf.toString();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (null != br) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != os) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != is) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return result;
- }
- public static JSONObject upAlarm(String sn,String ucode) {
- try{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- String mes_server_ip = pro.getProperty("mes.server_ip");
- String oprno = pro.getProperty("mes.gw").trim();
- String lineSn = pro.getProperty("mes.line_sn").trim();
- String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProcessAlarm/add";
- String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+ucode;
- System.out.println("params="+params);
- String result = doPost(url,params);
- System.out.println("result="+result);
- if(result.equalsIgnoreCase("false")) {
- return null;
- }else {
- return JSONObject.parseObject(result);
- }
- }catch (Exception e){
- return null;
- }
- }
- }
|