Junjie
19 小时以前 ef776e9fd5e4f64e4ad09a3faa12fb7bb646c79c
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
package com.zy.core.utils.station;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.BasStationOpt;
import com.zy.asrs.service.BasStationOptService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.command.StationCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
@Component
public class StationDispatchRuntimeStateSupport {
    private static final int STATION_IDLE_TRACK_EXPIRE_SECONDS = 60 * 60;
    private static final String IDLE_RECOVER_CLEARED_MEMO = "idleRecoverRerouteCleared";
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private BasStationOptService basStationOptService;
 
    public StationTaskIdleTrack touchIdleTrack(Integer taskNo, Integer stationId) {
        if (taskNo == null || taskNo <= 0 || stationId == null) {
            return null;
        }
        long now = System.currentTimeMillis();
        StationTaskIdleTrack idleTrack = loadIdleTrack(taskNo);
        if (idleTrack == null || !Objects.equals(idleTrack.getStationId(), stationId)) {
            idleTrack = new StationTaskIdleTrack(taskNo, stationId, now);
            saveIdleTrack(idleTrack);
        }
        return idleTrack;
    }
 
    public StationTaskIdleTrack loadIdleTrack(Integer taskNo) {
        if (taskNo == null || taskNo <= 0 || redisUtil == null) {
            return null;
        }
        Object obj = redisUtil.get(RedisKeyType.STATION_TASK_IDLE_TRACK_.key + taskNo);
        if (obj == null) {
            return null;
        }
        try {
            return JSON.parseObject(obj.toString(), StationTaskIdleTrack.class);
        } catch (Exception e) {
            return null;
        }
    }
 
    public void saveIdleTrack(StationTaskIdleTrack idleTrack) {
        if (idleTrack == null || idleTrack.getTaskNo() == null || idleTrack.getTaskNo() <= 0 || redisUtil == null) {
            return;
        }
        redisUtil.set(
                RedisKeyType.STATION_TASK_IDLE_TRACK_.key + idleTrack.getTaskNo(),
                JSON.toJSONString(idleTrack, SerializerFeature.DisableCircularReferenceDetect),
                STATION_IDLE_TRACK_EXPIRE_SECONDS
        );
    }
 
    public boolean hasRecentIssuedMoveCommand(Integer taskNo, Integer stationId, long thresholdMs) {
        if (taskNo == null || taskNo <= 0 || stationId == null || thresholdMs <= 0L || basStationOptService == null) {
            return false;
        }
        Date thresholdTime = new Date(System.currentTimeMillis() - thresholdMs);
        List<BasStationOpt> optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
                .select("id")
                .eq("task_no", taskNo)
                .eq("station_id", stationId)
                .eq("mode", String.valueOf(StationCommandType.MOVE))
                .eq("send", 1)
                .ge("send_time", thresholdTime)
                .orderByDesc("send_time")
                .last("limit 1"));
        return optList != null && !optList.isEmpty();
    }
 
    public int clearIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
                                                     Integer taskNo,
                                                     Integer stationId) {
        if (basStationOptService == null) {
            return 0;
        }
        List<BasStationOpt> optList;
        try {
            optList = listIssuedMoveCommandsDuringIdleStay(idleTrack, taskNo);
        } catch (Exception e) {
            return 0;
        }
        if (optList == null || optList.isEmpty()) {
            return 0;
        }
 
        Date now = new Date();
        String cleanupMemo = buildIdleRecoverClearedMemo(stationId);
        int clearedCount = 0;
        for (BasStationOpt opt : optList) {
            if (opt == null || opt.getId() == null) {
                continue;
            }
            opt.setSend(0);
            opt.setUpdateTime(now);
            opt.setMemo(appendCleanupMemo(opt.getMemo(), cleanupMemo));
            clearedCount++;
        }
        if (clearedCount > 0) {
            basStationOptService.updateBatchById(optList);
        }
        return clearedCount;
    }
 
    public boolean tryAcquireLock(String key, int seconds) {
        if (redisUtil == null || isBlank(key)) {
            return true;
        }
        Object lock = redisUtil.get(key);
        if (lock != null) {
            return false;
        }
        redisUtil.set(key, "lock", seconds);
        return true;
    }
 
    public boolean tryAcquireOutOrderDispatchLock(Integer wrkNo, Integer stationId, int seconds) {
        if (wrkNo == null || wrkNo <= 0 || stationId == null) {
            return true;
        }
        return tryAcquireLock(RedisKeyType.STATION_OUT_ORDER_DISPATCH_LIMIT_.key + wrkNo + "_" + stationId, seconds);
    }
 
    public void signalSegmentReset(Integer taskNo, long waitMs) {
        if (redisUtil == null || taskNo == null || taskNo <= 0) {
            return;
        }
        String key = RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo;
        redisUtil.set(key, "cancel", 3);
        try {
            if (waitMs > 0L) {
                Thread.sleep(waitMs);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception ignore) {
        }
        redisUtil.del(key);
    }
 
    public StationCommand loadWatchCircleCommand(Integer wrkNo) {
        if (wrkNo == null || wrkNo <= 0 || redisUtil == null) {
            return null;
        }
        Object circleObj = redisUtil.get(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
        if (circleObj == null) {
            return null;
        }
        try {
            return JSON.parseObject(circleObj.toString(), StationCommand.class);
        } catch (Exception ignore) {
            return null;
        }
    }
 
    public void saveWatchCircleCommand(Integer wrkNo, StationCommand command) {
        if (wrkNo == null || wrkNo <= 0 || command == null || redisUtil == null) {
            return;
        }
        redisUtil.set(
                RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo,
                JSON.toJSONString(command, SerializerFeature.DisableCircularReferenceDetect),
                60 * 60 * 24
        );
    }
 
    public void clearWatchCircleCommand(Integer wrkNo) {
        if (wrkNo == null || wrkNo <= 0 || redisUtil == null) {
            return;
        }
        redisUtil.del(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
    }
 
    private List<BasStationOpt> listIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
                                                                     Integer taskNo) {
        if (idleTrack == null || taskNo == null || taskNo <= 0 || idleTrack.getFirstSeenTime() == null || basStationOptService == null) {
            return Collections.emptyList();
        }
        List<BasStationOpt> optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
                .select("id", "task_no", "send_time", "target_station_id", "memo", "send")
                .eq("task_no", taskNo)
                .eq("mode", String.valueOf(StationCommandType.MOVE))
                .eq("send", 1)
                .ge("send_time", new Date(idleTrack.getFirstSeenTime()))
                .orderByAsc("send_time"));
        if (optList == null || optList.isEmpty()) {
            return Collections.emptyList();
        }
        return optList;
    }
 
    private String buildIdleRecoverClearedMemo(Integer stationId) {
        if (stationId == null) {
            return IDLE_RECOVER_CLEARED_MEMO;
        }
        return IDLE_RECOVER_CLEARED_MEMO + "(stationId=" + stationId + ")";
    }
 
    private String appendCleanupMemo(String memo, String cleanupMemo) {
        if (Cools.isEmpty(cleanupMemo)) {
            return memo;
        }
        if (Cools.isEmpty(memo)) {
            return cleanupMemo;
        }
        if (memo.contains(cleanupMemo)) {
            return memo;
        }
        return memo + " | " + cleanupMemo;
    }
 
    private boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }
}