MESHelper.java
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.huaheng.api.mes.service;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.api.mes.domain.MESUri;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.system.config.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
@Service
public class MESHelper {
private ApiLog apiLog;
@Autowired
private IConfigService configService;
public synchronized HttpURLConnection initUrlConn(String url, String paras)
throws Exception {
System.out.println(paras);
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
String MESCookie = configService.selectConfigByKey("MESCookie");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");// 设置请求方式
connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式
connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式
connection.setRequestProperty("Authorization", MESCookie);
apiLog = ApiLogAspect.initApiLog(connection, paras, "MES");
// DataOutputStream out = new DataOutputStream(connection.getOutputStream());
// out.writeBytes(paras);
OutputStreamWriter out = null;
out = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");// utf-8编码
out.append(paras);
out.flush();
out.close();
return connection;
}
public void insertAgvPort(String param) throws Exception {
HttpURLConnection connection = initUrlConn(MESUri.INSERT_AGV_PORT.getApiUri(), param);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String sResult=null;
while ((line = reader.readLine()) != null) {
sResult = new String(line.getBytes(), "UTF-8");
System.out.println(sResult);
}
reader.close();
connection.disconnect();
ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
// HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
}
public void insertContainer(String param) throws Exception {
HttpURLConnection connection = initUrlConn(MESUri.INSERT_CONTAINER.getApiUri(), param);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String sResult=null;
while ((line = reader.readLine()) != null) {
sResult = new String(line.getBytes(), "UTF-8");
System.out.println(sResult);
}
reader.close();
connection.disconnect();
ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
// HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
}
public void returnMESEmptyFinsh(String param) throws Exception {
HttpURLConnection connection = initUrlConn(MESUri.returnEmptyFinish.getApiUri(), param);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String sResult=null;
while ((line = reader.readLine()) != null) {
sResult = new String(line.getBytes(), "UTF-8");
System.out.println(sResult);
}
reader.close();
connection.disconnect();
ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
// HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
}
public JSONObject returnOrderCode(String param) throws Exception {
HttpURLConnection connection = initUrlConn(MESUri.returnOrderCode.getApiUri(), param);
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
String sResult = null;
while ((line = reader.readLine()) != null) {
sResult= new String(line.getBytes(), "utf-8");
System.out.println(sResult);
}
JSONObject jsonObject = JSONObject.parseObject(sResult);
connection.disconnect();
return jsonObject;
}
}