#
zwl
2026-02-09 ad79ba405b2d1ac96423f88f4e8a76c584b9d38a
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
package com.zy.core.network.fake;
 
import com.alibaba.fastjson.JSON;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.core.enums.CrnStatusType;
import com.zy.core.enums.CrnTaskModeType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.network.api.ZyCrnConnectApi;
import com.zy.core.network.entity.ZyCrnStatusEntity;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ZyCrnFakeConnect implements ZyCrnConnectApi {
 
    private ZyCrnStatusEntity crnStatus;
    private DeviceConfig deviceConfig;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
 
    public ZyCrnFakeConnect(DeviceConfig deviceConfig) {
        this.deviceConfig = deviceConfig;
        this.crnStatus = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyCrnStatusEntity.class);
    }
 
    @Override
    public boolean connect() {
        return true;
    }
 
    @Override
    public boolean disconnect() {
        try { executor.shutdownNow(); } catch (Exception ignore) {}
        return true;
    }
 
    @Override
    public ZyCrnStatusEntity getStatus() {
        return this.crnStatus;
    }
 
    @Override
    public CommandResponse sendCommand(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        if (command.getTaskMode().intValue() == CrnTaskModeType.LOC_MOVE.id) {
            //取放货
            executor.submit(() -> commandTake(command));
        } else if (command.getTaskMode().intValue() == CrnTaskModeType.CRN_MOVE.id) {
            //移动
            executor.submit(() -> commandMove(command));
        } else if (command.getTaskMode().intValue() == CrnTaskModeType.NONE.id) {
            //复位
            executor.submit(() -> commandTaskComplete(command));
        }
        response.setResult(true);
        return response;
    }
 
    @Override
    public CommandResponse sendCommand1(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        if (command.getTaskMode().intValue() == CrnTaskModeType.LOC_MOVE.id) {
            //取放货
            executor.submit(() -> commandTake(command));
        } else if (command.getTaskMode().intValue() == CrnTaskModeType.CRN_MOVE.id) {
            //移动
            executor.submit(() -> commandMove(command));
        } else if (command.getTaskMode().intValue() == CrnTaskModeType.NONE.id) {
            //复位
            executor.submit(() -> commandTaskComplete(command));
        }
        response.setResult(true);
        return response;
    }
 
    @Override
    public CommandResponse sendCommand2(CrnCommand command) {
        return null;
    }
 
    @Override
    public CommandResponse sendCommand3(CrnCommand command) {
        return null;
    }
 
    private void commandTaskComplete(CrnCommand command) {
        this.crnStatus.setTaskNo(0);
        this.crnStatus.setStatus(CrnStatusType.IDLE.id);
    }
 
    private void commandMove(CrnCommand command) {
        int destinationPosX = command.getDestinationPosX().intValue();
        int destinationPosY = command.getDestinationPosY().intValue();
        int destinationPosZ = command.getDestinationPosZ().intValue();
        int taskMode = command.getTaskMode().intValue();
        int taskNo = command.getTaskNo().intValue();
 
        this.crnStatus.setTaskNo(taskNo);
        this.crnStatus.setStatus(CrnStatusType.MOVING.id);
        moveY(this.crnStatus.getBay(), destinationPosY);
        moveZ(this.crnStatus.getLevel(), destinationPosZ);
        this.crnStatus.setStatus(CrnStatusType.WAITING.id);
    }
 
    private void commandTake(CrnCommand command) {
        int sourcePosX = command.getSourcePosX().intValue();
        int sourcePosY = command.getSourcePosY().intValue();
        int sourcePosZ = command.getSourcePosZ().intValue();
        int destinationPosX = command.getDestinationPosX().intValue();
        int destinationPosY = command.getDestinationPosY().intValue();
        int destinationPosZ = command.getDestinationPosZ().intValue();
        int taskMode = command.getTaskMode().intValue();
        int taskNo = command.getTaskNo().intValue();
 
        this.crnStatus.setTaskNo(taskNo);
        this.crnStatus.setMode(taskMode);
        this.crnStatus.setStatus(CrnStatusType.FETCH_MOVING.id);
        moveY(this.crnStatus.getBay(), sourcePosY);
        moveZ(this.crnStatus.getLevel(), sourcePosZ);
        this.crnStatus.setStatus(CrnStatusType.FETCHING.id);
        sleep(2000);
        if (Thread.currentThread().isInterrupted()) {
            return;
        }
 
        this.crnStatus.setLoaded(1);
        this.crnStatus.setStatus(CrnStatusType.PUT_MOVING.id);
        moveY(this.crnStatus.getBay(), destinationPosY);
        moveZ(this.crnStatus.getLevel(), destinationPosZ);
        this.crnStatus.setStatus(CrnStatusType.PUTTING.id);
        sleep(2000);
        if (Thread.currentThread().isInterrupted()) {
            return;
        }
        this.crnStatus.setLoaded(0);
        this.crnStatus.setStatus(CrnStatusType.WAITING.id);
    }
 
    private void moveZ(int sourcePosZ, int destinationPosZ) {
        if(destinationPosZ - sourcePosZ > 0) {
            int moveLength = destinationPosZ - sourcePosZ;
            int initSourcePosZ = sourcePosZ;
            for(int i = 0; i < moveLength; i++) {
                initSourcePosZ++;
                this.crnStatus.setLevel(initSourcePosZ);
                sleep(1000);
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
            }
        }else {
            int moveLength = sourcePosZ - destinationPosZ;
            int initSourcePosZ = sourcePosZ;
            for(int i = 0; i < moveLength; i++) {
                initSourcePosZ--;
                this.crnStatus.setLevel(initSourcePosZ);
                sleep(1000);
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
            }
        }
    }
 
    private void moveY(int sourcePosY, int destinationPosY) {
        if(destinationPosY - sourcePosY > 0) {
            int moveLength = destinationPosY - sourcePosY;
            int initSourcePosY = sourcePosY;
            for(int i = 0; i < moveLength; i++) {
                initSourcePosY++;
                this.crnStatus.setBay(initSourcePosY);
                sleep(1000);
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
            }
        }else {
            int moveLength = sourcePosY - destinationPosY;
            int initSourcePosY = sourcePosY;
            for(int i = 0; i < moveLength; i++) {
                initSourcePosY--;
                this.crnStatus.setBay(initSourcePosY);
                sleep(1000);
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
            }
        }
    }
 
    private void sleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}