严智鑫
2025-05-20 8685b70d8725e38b597e0e69995eb47ca2c51d92
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package builder;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ng.db.DBHelper;
import ng.db.DBSession;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.ResultSet;
 
public class HttpApi {
    public static String Ip="192.168.30.111";
    public static void main(String[] args) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("thickness", 4);
        jsonObject.put("width", 3660);
        jsonObject.put("height", 2440);
        jsonObject.put("sameCount", 4);
        jsonObject.put("glassType", "CFL");
 
//        JSONObject C=shelfTaskApi(jsonObject);
//        System.out.println(C);
 
        JSONObject C2=thisShelfTaskApi();
        System.out.println(C2);
 
        //¼õÉÙÉÏÆ¬ÊýÁ¿
        //JSONObject b=loadRackReduction();
 
        //²éѯÉÏÆ¬Î»ÐÅÏ¢
        JSONObject a=selectLoadRack();
        if(a!=null){
            double width=Double.valueOf(a.get("width").toString());
            double height=Double.valueOf(a.get("height").toString());
            double thickness=Double.valueOf(a.get("thickness").toString());
            int sameCount=Integer.valueOf(a.get("sameCount").toString());
            System.out.println(width+","+height+","+thickness+","+sameCount);
        }
 
    }
    //·¢ËͲִ¢ÈÎÎñ
    public static boolean sendShelfTask(){
        DBSession sn=null;
        try{
            //¶ÁÈ¡É豸Áбí
            DBHelper db=DBHelper.getDBHelper("mes");
            sn=DBHelper.getDBHelper("mes").createSession(false);
            sn.createSql("select o.id,o.largeglass_length,o.largeglass_width,o.largeglass_no,o.largeglass_completed,r.thickness,r.color\n" +
                    "\t from `order` as o INNER JOIN recipe as r on o.recipe_no=r.id\n" +
                    "\twhere o.order_status=2 LIMIT 0,1;");
            ResultSet set= sn.query().getCurrentResult();
            //²éѯ½á¹û
            while(set.next()){
                Integer id= Integer.valueOf(set.getString(1)) ;
                double largeglass_length=set.getDouble(2);//¶©µ¥Ô­Æ¬³¤
                double largeglass_width=set.getDouble(3);//¶©µ¥Ô­Æ¬¿í
                int largeglass_no=set.getInt(4);//¶©µ¥Ô­Æ¬ÈÎÎñÊý
                int largeglass_completed=set.getInt(5);//¶©µ¥Ô­Æ¬Íê³ÉÊý
                int not_completed=largeglass_no-largeglass_completed;//¶©µ¥Ô­Æ¬Î´Íê³ÉÊý
 
                int thickness=set.getInt(6);//¶©µ¥Ô­Æ¬ºñ
                String color=set.getString(7);//¶©µ¥Ô­Æ¬Ä¤Ïµ
                if(not_completed>0){  //ÓÐδÍê³ÉµÄԭƬÈÎÎñ
                    JSONObject thisShelfTask = HttpApi.thisShelfTaskApi();//²éѯ²Ö´¢µ±Ç°ÈÎÎñ
                    if (thisShelfTask != null) {
                        double width_shelf = Double.valueOf(thisShelfTask.get("width").toString());
                        double height_shelf = Double.valueOf(thisShelfTask.get("height").toString());
                        int thickness_shelf = Integer.valueOf(thisShelfTask.get("thickness").toString());
                        int sameCount_shelf = Integer.valueOf(thisShelfTask.get("sameCount").toString());
                        String glassType_shelf = thisShelfTask.get("glass_type").toString();
                        if(largeglass_length!=width_shelf||largeglass_width!=height_shelf||thickness!=thickness_shelf||!color.equals(glassType_shelf)||sameCount_shelf==0){
                            JSONObject jsonObject=new JSONObject();
                            jsonObject.put("thickness", thickness);
                            jsonObject.put("width", largeglass_length);
                            jsonObject.put("height", largeglass_width);
                            jsonObject.put("sameCount", not_completed);
                            jsonObject.put("glassType", color);
                            JSONObject shelfJosn=shelfTaskApi(jsonObject);
                        }
                    }
                }
 
            }
        }
        catch(Exception e){
 
        }
        finally{
            if(sn!=null){
                sn.close();
            }
        }
        return false;
    }
    //·¢ËͲִ¢ÈÎÎñ
    public static JSONObject shelfTaskApi(JSONObject params){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            String requestBody =params.toString();
            JSONObject resultJSON= httpApi(requestURL,"POST",requestBody);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo  POST");
        }
        return null;
    }
    //MESµ±Ç°ÈÎÎñ
    public static JSONObject thisShelfTaskApi(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo  GET");
        }
        return null;
    }
    //²éѯÉÏÆ¬Î»ÐÅÏ¢
    public static JSONObject selectLoadRack(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo/getReady";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            JSONArray jsonArray=resultJSON.getJSONObject("data").getJSONArray("stocklist");
            if(jsonArray.size()>0){
                JSONObject loadRack=jsonArray.getJSONObject(0);
                String isPackages=loadRack.get("packages").toString();
                if("false".equals(isPackages) ){
                    return loadRack;
                }
            }
            return null;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://"+Ip+":5000/GlassInfo GET");
        }
        return null;
    }
    //ÉÏÆ¬Î»²£Á§-1
    public static JSONObject loadRackReduction(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo/finishGet";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://"+Ip+":5000/GlassInfo/finishGet GET");
        }
        return null;
    }
    //¿âÄÚ²£Á§ÐÅÏ¢²éѯ
    public static JSONObject storageRackInfo(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo GET");
        }
        return null;
    }
    public static JSONObject httpApi(String requestURL,String requestMethod,String requestBody){
        JSONObject jsonObject = null;
        try {
            //"http://192.168.112:8081/deviceInteraction/account/selectAccount"
            URL url = new URL(requestURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(requestMethod);
            connection.setDoOutput(true);
            connection.setDoInput(true);  // ÉèÖøÃÁ¬½Ó¿ÉÊäÈë
            connection.setDoOutput(true); // ÉèÖøÃÁ¬½Ó¿ÉÊä³ö
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            //connection.setRequestProperty("Charsert", "UTF-8");
            connection.setRequestProperty("Content-type", "application/json; charset=UTF-8");
            System.out.println(requestBody);
            if(requestBody!=null){
                try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                    byte requestBodyByte[]=requestBody.getBytes("UTF-8");
                    outputStream.write(requestBodyByte);
                    outputStream.flush();
                }
            }
            int responseCode = connection.getResponseCode();
            if (responseCode==200){
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
 
                String resultStr = response.toString();
                if (resultStr!=null&&resultStr.length()>0) {
                    jsonObject =JSONObject.parseObject(resultStr);
                }
                //System.out.println(jsonObject);
            }else{
                System.out.println("Response Code: " + responseCode);
            }
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}