#
Junjie
2025-11-21 7f9435bb4a074b4964f290c4d6905e0e233a19ec
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
package com.zy.core.network.fake;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasMapService;
import com.zy.common.model.NavigateNode;
import com.zy.common.utils.NavigateUtils;
import com.zy.core.enums.RgvStatusType;
import com.zy.core.enums.RgvTaskModeType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.command.RgvCommand;
import com.zy.core.network.api.ZyRgvConnectApi;
import com.zy.core.network.entity.ZyRgvStatusEntity;
 
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ZyRgvFakeConnect implements ZyRgvConnectApi {
    
    private ZyRgvStatusEntity status;
    private final DeviceConfig deviceConfig;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
 
    public ZyRgvFakeConnect(DeviceConfig deviceConfig) {
        this.deviceConfig = deviceConfig;
        ZyRgvStatusEntity init = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyRgvStatusEntity.class);
        this.status = init == null ? new ZyRgvStatusEntity() : init;
    }
 
    @Override
    public boolean connect() {
        return true;
    }
 
    @Override
    public boolean disconnect() {
        return true;
    }
 
    @Override
    public ZyRgvStatusEntity getStatus() {
        return status;
    }
 
    @Override
    public CommandResponse sendCommand(RgvCommand command) {
        CommandResponse response = new CommandResponse(true);
        if (command.getTaskMode() == RgvTaskModeType.FETCH_PUT.id) {
            //取放货
            executor.submit(() -> commandTake(command));
        } else if (command.getTaskMode() == RgvTaskModeType.X_MOVE.id) {
            //移动
            executor.submit(() -> commandMove(command));
        }else if (command.getTaskMode() == RgvTaskModeType.NONE.id) {
            //复位
            executor.submit(() -> commandTaskComplete(command));
        }
        return response;
    }
 
    private void commandTake(RgvCommand command) {
        int taskNo = command.getTaskNo();
        int taskMode = command.getTaskMode();
        int sourcePos = command.getSourcePos();
        int targetPos = command.getTargetPos();
 
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return;
        }
 
        BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
        if (basMapService == null) {
            return;
        }
 
        List<Integer> levList = basMapService.getLevList();
 
        List<NavigateNode> navigateNodes = null;
        try {
            for (Integer lev : levList) {
                navigateNodes = navigateUtils.calcByTrackSiteNo(lev, status.getRgvPos(), sourcePos);
                if (navigateNodes != null) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (navigateNodes == null) {
            return;
        }
 
        List<NavigateNode> targetNavigateNodes = null;
        try {
            for (Integer lev : levList) {
                targetNavigateNodes = navigateUtils.calcByTrackSiteNo(lev, sourcePos, targetPos);
                if (targetNavigateNodes != null) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (targetNavigateNodes == null) {
            return;
        }
 
        status.setTaskNo(taskNo);
        status.setStatus(RgvStatusType.FETCHING.id);
        for (int i = 0; i < navigateNodes.size(); i++) {
            NavigateNode navigateNode = navigateNodes.get(i);
            JSONObject valueObject = JSON.parseObject(navigateNode.getNodeValue());
            Integer currentTrackSiteNo = valueObject.getInteger("trackSiteNo");
 
            status.setRgvPos(currentTrackSiteNo);
            sleep(1000);
        }
 
        status.setStatus(RgvStatusType.PUTTING.id);
        status.setLoaded(1);
        sleep(1000);
 
        for (int i = 0; i < targetNavigateNodes.size(); i++) {
            NavigateNode navigateNode = targetNavigateNodes.get(i);
            JSONObject valueObject = JSON.parseObject(navigateNode.getNodeValue());
            Integer currentTrackSiteNo = valueObject.getInteger("trackSiteNo");
 
            status.setRgvPos(currentTrackSiteNo);
            sleep(1000);
        }
 
        sleep(1000);
        status.setStatus(RgvStatusType.WAITING.id);
    }
 
    private void commandMove(RgvCommand command) {
        int taskNo = command.getTaskNo();
        int taskMode = command.getTaskMode();
        int targetPos = command.getTargetPos();
 
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return;
        }
 
        BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
        if (basMapService == null) {
            return;
        }
 
        List<Integer> levList = basMapService.getLevList();
 
        List<NavigateNode> navigateNodes = null;
        try {
            for (Integer lev : levList) {
                navigateNodes = navigateUtils.calcByTrackSiteNo(lev, status.getRgvPos(), targetPos);
                if (navigateNodes != null) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (navigateNodes == null) {
            return;
        }
 
        status.setTaskNo(taskNo);
        status.setStatus(RgvStatusType.WORKING.id);
        for (int i = 0; i < navigateNodes.size(); i++) {
            NavigateNode navigateNode = navigateNodes.get(i);
            JSONObject valueObject = JSON.parseObject(navigateNode.getNodeValue());
            Integer currentTrackSiteNo = valueObject.getInteger("trackSiteNo");
 
            status.setRgvPos(currentTrackSiteNo);
            sleep(1000);
        }
        status.setStatus(RgvStatusType.WAITING.id);
    }
 
    private void commandTaskComplete(RgvCommand command) {
        status.setTaskNo(0);
        status.setStatus(RgvStatusType.IDLE.id);
    }
 
    private void sleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}