自动化立体仓库 - WCS系统
Junjie
2024-11-22 2264dc290f69febf53c2f728dddd214505e50b93
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.zy.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.common.model.NavigateNode;
import com.zy.core.model.command.NyShuttleHttpCommand;
import com.zy.core.model.protocol.NyShuttleProtocol;
import springfox.documentation.spring.web.json.Json;
 
import java.io.*;
import java.net.Socket;
import java.util.*;
 
/**
 * 牛眼四向穿梭车HTTP请求工具类
 */
public class NyHttpUtils {
 
    private static final boolean DEBUG = true;//调试模式
 
    //获取HTTP请求标准结构体
    public static NyShuttleHttpCommand getHttpStandard(Integer shuttleNo, Integer wrkNo) {
        NyShuttleHttpCommand httpStandard = new NyShuttleHttpCommand();
        httpStandard.setMsgType("requestMsg");//请求消息
        httpStandard.setRobotId(shuttleNo);//车辆编号
        httpStandard.setWrkNo(wrkNo);//工作号
 
        //设置请求消息
        NyShuttleHttpCommand.NyRequest request = new NyShuttleHttpCommand.NyRequest();
        NyShuttleHttpCommand.NyRequest.NyHeader header = new NyShuttleHttpCommand.NyRequest.NyHeader();
        header.setVersion("1.0");//版本号
        header.setRequestId(getRequestId());//消息编号
 
        //设置请求头
        request.setHeader(header);
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取FAS 手动/自动切换
    public static NyShuttleHttpCommand getFASSwitchCommand(Integer shuttleNo, boolean auto) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", auto ? "switchAuto" : "switchManual");
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取移动请求
    public static NyShuttleHttpCommand getMoveCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "move");//移动命令
        body.put("taskId", getTaskId());//TaskID需要随机
        body.put("start", navigateNodeToNyPointNode(start));//起点
        body.put("target", navigateNodeToNyPointNode(target));//终点
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取进出提升机请求
    public static NyShuttleHttpCommand getInOutLiftCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target, boolean in) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        NyShuttleProtocol.NyShuttlePointClass nyStartPoint = new NyShuttleProtocol.NyShuttlePointClass();
        NyShuttleProtocol.NyShuttlePointClass nyTargetPoint = new NyShuttleProtocol.NyShuttlePointClass();
        if (in) {
            //进提升机
            nyStartPoint = navigateNodeToNyPointNode(start);
            nyTargetPoint.setX(target.getX());
            nyTargetPoint.setY(target.getY());
            nyTargetPoint.setZ(target.getZ());
        }else {
            //出提升机
            nyTargetPoint = navigateNodeToNyPointNode(target);
            nyStartPoint.setX(start.getX());
            nyStartPoint.setY(start.getY());
            nyStartPoint.setZ(start.getZ());
        }
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", in ? "intoLift" : "outLift");//进出提升机
        body.put("taskId", getTaskId());//TaskID需要随机
        body.put("start", nyStartPoint);//起点
        body.put("target", nyTargetPoint);//终点
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取顶升命令
    public static NyShuttleHttpCommand getPalletLiftCommand(Integer shuttleNo, Integer wrkNo, boolean lift) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", lift ? "liftUp" : "liftDown");//顶升或下降命令
        body.put("taskId", getTaskId());//TaskID需要随机
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取充电命令
    public static NyShuttleHttpCommand getChargeCommand(Integer shuttleNo, Integer wrkNo, boolean charge) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", charge ? "charge" : "stopCharge");//充电或停止充电
        body.put("taskId", getTaskId());//TaskID需要随机
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取管制命令
    public static NyShuttleHttpCommand getSuspendCommand(Integer shuttleNo, Integer wrkNo, boolean suspend) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", suspend ? "stop" : "resume");//管制或取消管制
        body.put("taskId", getTaskId());//TaskID需要随机
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新层坐标Z(楼层)命令
    public static NyShuttleHttpCommand getUpdateZCommand(Integer shuttleNo, Integer z, Integer wrkNo) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateFloor");//更新层Z
        body.put("z", z);//坐标Z
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取读FAS状态信息命令
    public static NyShuttleHttpCommand getReadStatusCommand(Integer shuttleNo) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "readState");//读FAS状态信息
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新FAS基准地图命令
    public static NyShuttleHttpCommand getUpdateFASBaseMapCommand(Integer shuttleNo, Integer xBase, Integer yBase, Integer zBase, Integer xEnd, Integer yEnd, Integer zEnd, Integer xBaseCoord, Integer yBaseCoord, Integer xDefaultSpace, Integer yDefaultSpace) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateMapStandard");//更新FAS基准地图
 
        HashMap<String, Object> standard = new HashMap<>();
        standard.put("xBase", xBase);//库位X向基点
        standard.put("yBase", yBase);//库位Y向基点
        standard.put("zBase", zBase);//库位Z向基点
        standard.put("xEnd", xEnd);//库位X向终点
        standard.put("yEnd", yEnd);//库位Y向终点
        standard.put("zEnd", zEnd);//库位Z向终点
        standard.put("xBaseCoord", xBaseCoord);//库位X向基点坐标 单位mm
        standard.put("yBaseCoord", yBaseCoord);//库位Y向基点坐标 单位mm
        standard.put("xDefaultSpace", xDefaultSpace);//库位X向标准间距 单位mm
        standard.put("yDefaultSpace", yDefaultSpace);//库位Y向标准间距 单位mm
        body.put("standard", standard);
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新FAS特殊X轴地图命令
    public static NyShuttleHttpCommand getUpdateMapSpecialXAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateMapSpecialXAxis");//更新FAS特殊X轴地图
        body.put("specialXAxis", list);
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新FAS特殊Y轴地图命令
    public static NyShuttleHttpCommand getUpdateMapSpecialYAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateMapSpecialYAxis");//更新FAS特殊Y轴地图
        body.put("specialYAxis", list);
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新FAS特殊库位点地图命令
    public static NyShuttleHttpCommand getUpdateMapSpecialPointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateMapSpecialPoint");//更新FAS特殊库位点地图
        body.put("specialPoint", list);
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取更新FAS提升机点位地图命令
    public static NyShuttleHttpCommand getUpdateMapDevicePointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "updateMapDevicePoint");//更新FAS提升机点位地图
        body.put("devicePoint", list);
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取单个RFID录写命令
    public static NyShuttleHttpCommand getWriteSingleRFIDCommand(Integer shuttleNo, Integer xPoint, Integer yPoint, Integer zPoint) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "WriteSingleRFID");//单个RFID录写
        body.put("xPoint", xPoint);//X 点位
        body.put("yPoint", yPoint);//Y 点位
        body.put("zPoint", zPoint);//Z 点位
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取自动录写RFID命令
    public static NyShuttleHttpCommand getAutoWriteRFIDCommand(Integer shuttleNo, Integer dir, Integer xBase, Integer yBase, Integer zBase, Integer pointEnd, List<Integer> specialPoint) {
        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
        HashMap<String, Object> body = new HashMap<>();
        body.put("requestType", "AutoWriteRFID");//自动录写RFID
        body.put("requestCode", dir);//方向xDir/yDir
        body.put("xBase", xBase);//X 起始点位
        body.put("yBase", yBase);//Y 起始点位
        body.put("zBase", zBase);//Z 起始点位
        body.put("pointEnd", pointEnd);//终点坐标 根据requestCode来决定终点坐标的方向
        body.put("specialPoint", specialPoint);//特殊点位过程中不需要录制的RFID点位
        request.setBody(body);
 
        httpStandard.setRequest(request);
        return httpStandard;
    }
 
    //获取请求编号
    public static Integer getRequestId() {
        Random random = new Random();
        return random.nextInt(9999999);
    }
 
    //获取TaskId
    public static Integer getTaskId() {
        Random random = new Random();
        return random.nextInt(999999);
    }
 
    //发出请求
    public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) throws IOException {
        if (socket == null) {
            return null;
        }
 
        //压缩数据包
        JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
        data.remove("nodes");
 
        // 获取输入流和输出流
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
        writer.write(JSON.toJSONString(data) + "\r\n");
        writer.flush();
//            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
 
        // 读取服务器的响应
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[2048];//缓冲区
        while (true) {
            reader.read(chars);
            String trim = new String(chars);
            sb.append(trim);
            if (trim.lastIndexOf("\r\n") != -1) {
                break;
            }
        }
 
        JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集
        if (!result.get("msgType").equals("responseMsg")) {//不是响应内容
            return null;
        }
 
        JSONObject resultResponse = JSON.parseObject(result.get("response").toString());
        JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
        int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
        if (!DEBUG && responseId != httpCommand.getRequest().getHeader().getRequestId()) {
            return null;//响应ID与请求ID不一致,不在调试模式下
        }
 
        return filterBodyData(result);//返回Body结果集
    }
 
    public static JSONObject filterBodyData(JSONObject data) {
        Object response = data.get("response");
        if (response == null) {
            return null;
        }
 
        JSONObject result = JSON.parseObject(response.toString());
        Object body = result.get("body");
        if (body == null) {
            return null;
        }
        JSONObject jsonBody = JSON.parseObject(body.toString());
        return jsonBody;
    }
 
    //地图节点转换牛眼节点
    public static NyShuttleProtocol.NyShuttlePointClass navigateNodeToNyPointNode(NavigateNode node) {
        int[] NyPosition = NavigatePositionConvert.WCSXyzToNyXyz(node.getX(), node.getY(), node.getZ());//WCS系统坐标转牛眼坐标
        NyShuttleProtocol.NyShuttlePointClass point = new NyShuttleProtocol.NyShuttlePointClass();
        point.setX(NyPosition[0]);
        point.setY(NyPosition[1]);
        point.setZ(NyPosition[2]);
        return point;
    }
 
}