#
luxiaotao1123
2024-12-23 4f8eab6610413317b16fe6127c60ff994616e85a
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
package com.zy.acs.manager.fake;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.domain.AgvProtocol;
import com.zy.acs.common.domain.protocol.AGV_01_DOWN;
import com.zy.acs.common.domain.protocol.AGV_01_UP;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.core.service.MapService;
import com.zy.acs.manager.manager.entity.Action;
import com.zy.acs.manager.manager.entity.Agv;
import com.zy.acs.manager.manager.enums.ActionStsType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
/**
 * Created by vincent on 11/9/2024
 */
@Slf4j
@Component
public class FakeProcessor {
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    public static final Map<Long, Boolean> AGV_PROCESSING_MAP = new ConcurrentHashMap<>();
 
    private ExecutorService executorService;
 
    private List<Agv> agvList = new ArrayList<>();
 
    private Thread heartBeatThread;
 
    private Thread responseThread;
 
    private Thread simulatorTread;
 
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private MapService mapService;
    @Autowired
    private JamService jamService;
 
    @PostConstruct
    public void init() {
        // heartBeat
        this.heartBeatThread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
 
                    if (configService.getVal("fakeSign", Boolean.class)) {
                        this.processOnline();
                    }
 
                } catch (Exception e) {
                    log.error("FakeProcessor.heartBeatThread fail", e);
                }
            }
        });
        this.heartBeatThread.start();
 
        // response the request
        this.responseThread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(100);
 
                    if (configService.getVal("fakeSign", Boolean.class)) {
                        this.responseTheRequest();
                    }
 
                } catch (Exception e) {
                    log.error("FakeProcessor.responseThread fail", e);
                }
            }
        });
        this.responseThread.start();
 
        /**
         * 1.AgvDataService.dataProcess [ agvDetail: vol, code, agvAngle, agvStatus ]
         * 2.MainService.upDataSubscribe
         * 3.AgvCmdService.executeRequest {@link com.zy.acs.manager.core.service.AgvCmdService#executeAgvActionCmd}
         * 4.AgvServiceImpl.judgeOnline
         */
        // Simulator
        this.simulatorTread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(100);
 
                    if (configService.getVal("fakeSign", Boolean.class)) {
 
                        List<Agv> agvList = this.getAgvList();
                        if (null == this.executorService) {
                            int count = agvList.size();
                            this.executorService = Executors.newFixedThreadPool(count);
                        }
 
                        for (Agv agv : agvList) {
 
                            if (!AGV_PROCESSING_MAP.get(agv.getId())) {
                                List<Action> actionList = actionService.queryLatestGroup(agv.getId(), ActionStsType.ISSUED);
                                if (!Cools.isEmpty(actionList)) {
                                    AGV_PROCESSING_MAP.put(agv.getId(), true);
                                    executorService.submit(new AgvSimulatorTask(
                                            agv
                                            , redis
                                            , agvDetailService
                                            , actionService
                                            , codeService
                                            , mapService
                                            , jamService
                                            , actionList
                                    ));
                                }
                            }
 
                        }
 
                    } else {
                        this.shutdownFakeThreads();
                    }
 
                } catch (Exception e) {
                    log.error("FakeProcessor.simulatorTread fail", e);
                }
            }
        });
        this.simulatorTread.start();
    }
 
    private void processOnline() {
        for (Agv agv : this.getAgvList()) {
            redis.setObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid(), 1, 30);
        }
    }
 
    private void responseTheRequest() {
        AgvProtocol protocol = redis.pop(RedisConstant.AGV_PATH_DOWN_FLAG);
        if (null != protocol) {
            AGV_01_DOWN agv_01_down = (AGV_01_DOWN) protocol.getMessageBody();
 
            AGV_01_UP agv_01_up = new AGV_01_UP();
            agv_01_up.setSerialNo(agv_01_down.getSerialNo());
 
            redis.setObject(RedisConstant.AGV_PATH_UP_FLAG
                    , protocol.getAgvNo() + "_" + agv_01_up.getSerialNo()
                    , agv_01_up);
        }
    }
 
    public List<Agv> getAgvList() {
        if (Cools.isEmpty(this.agvList)) {
            this.agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
            this.agvList.forEach(agv -> {AGV_PROCESSING_MAP.putIfAbsent(agv.getId(), false);});
        }
        return this.agvList;
    }
 
    public void shutdownFakeThreads() {
        if (this.executorService != null && !this.executorService.isShutdown()) {
            this.executorService.shutdown();
            try {
                if (!this.executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                    this.executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                this.executorService.shutdownNow();
                Thread.currentThread().interrupt();
            }
            this.executorService = null; // 释放资源
        }
        if (!Cools.isEmpty(this.agvList)) {
            agvList = new ArrayList<>();
        }
        if (!AGV_PROCESSING_MAP.isEmpty()) {
            AGV_PROCESSING_MAP.clear();
        }
    }
 
    @PreDestroy
    public void shutDown(){
        if (this.heartBeatThread != null) {
            this.heartBeatThread .interrupt();
        }
        if (this.responseThread != null) {
            this.responseThread .interrupt();
        }
        if (this.simulatorTread != null) {
            this.simulatorTread.interrupt();
        }
        this.shutdownFakeThreads();
    }
 
 
}