|
|
@@ -9,20 +9,23 @@ 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连接后,直接先发同步报文
|
|
|
+ //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);
|
|
|
@@ -37,14 +40,14 @@ public class DataUtil {
|
|
|
|
|
|
public static Boolean heartBeat(NettyClient nettyClient,String mes_gw){
|
|
|
try{
|
|
|
- //TCP连接后,直接先发同步报文
|
|
|
+ //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);
|
|
|
@@ -116,14 +119,71 @@ public class DataUtil {
|
|
|
try{
|
|
|
String msgType = "MQDW";
|
|
|
String gy = "100000";
|
|
|
- String param = "";
|
|
|
- Boolean aqdw_str = sendMessage(nettyClient,msgType,gy,"",sn,ret,user,"00",param);
|
|
|
+ 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";
|
|
|
@@ -242,26 +302,69 @@ public class DataUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static JSONObject saveLife(String sn,String ps) {
|
|
|
- try{
|
|
|
- String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductCncLife/saveLife";
|
|
|
- String params = "__ajax=json&oprno="+MesClient.mes_gw+"&lineSn="+MesClient.mes_line_sn+"&sn="+sn+"¶ms="+ps;
|
|
|
- System.out.println("url="+url);
|
|
|
- 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);
|
|
|
+ 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();
|
|
|
}
|
|
|
- }catch (Exception e){
|
|
|
- return null;
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- public static String doPost(String httpUrl, String param) {
|
|
|
+ public static String doPostJson(String httpUrl, String json) {
|
|
|
HttpURLConnection connection = null;
|
|
|
InputStream is = null;
|
|
|
OutputStream os = null;
|
|
|
@@ -275,10 +378,10 @@ public class DataUtil {
|
|
|
connection.setReadTimeout(10000);
|
|
|
connection.setDoOutput(true);
|
|
|
connection.setDoInput(true);
|
|
|
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
|
|
|
os = connection.getOutputStream();
|
|
|
- os.write(param.getBytes());
|
|
|
+ os.write(json.getBytes(StandardCharsets.UTF_8));
|
|
|
if (connection.getResponseCode() == 200) {
|
|
|
is = connection.getInputStream();
|
|
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
@@ -316,7 +419,9 @@ public class DataUtil {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
- connection.disconnect();
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
}
|
|
|
return result;
|
|
|
}
|