#
vincentlu
7 小时以前 d58a5160d46bd0f83fdd73e9e49061b33e1d241b
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
package com.zy.acs.manager.core.integrate.conveyor;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.config.ConveyorProperties;
import com.zy.acs.manager.common.utils.HttpGo;
import com.zy.acs.manager.core.integrate.dto.ConveyorStaDto;
import com.zy.acs.manager.core.integrate.dto.HttpResult;
import com.zy.acs.manager.manager.entity.Segment;
import com.zy.acs.manager.manager.entity.Sta;
import com.zy.acs.manager.manager.entity.Task;
import com.zy.acs.manager.manager.enums.StaReserveType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class SiemensConveyorStationService implements ConveyorStationService {
 
    @Autowired
    private ConveyorProperties conveyorProperties;
 
    private HttpGo http;
 
    @PostConstruct
    public void init() {
        this.http = HttpGo.builder()
                .connectTimeout(Duration.ofSeconds(8))
                .readTimeout(Duration.ofSeconds(15))
//                .defaultHeader("User-Agent", "HttpGo/1.0")
                // .trustAllSsl(true) // ONLY if you really need it (self-signed internal)
                .build();
    }
 
    @Override
    public boolean allowAgvWork(Sta sta, Task task, Segment seg, StaReserveType type) {
        final String staNo = sta.getStaNo();
 
        // url
        String url = this.buildUrl("/station/query");
        // headers
        Map<String, String> headers = new HashMap<>();
        // params
        Map<String, Object> params = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(staNo);
        params.put("staNos", list);
        // do request
        HttpResult<List<ConveyorStaDto>> result;
        try {
            result = postForResult(url, headers, params);
            Integer code = result.getCode();
            if (null == code || 200 != code) {
                return false;
            }
            List<ConveyorStaDto> dtoList = result.getData();
            if (Cools.isEmpty(list)) {
                return false;
            }
            ConveyorStaDto staDto = dtoList.stream()
                    .filter(dto -> !Cools.isEmpty(dto.getStaNo()) && dto.getStaNo().equals(staNo))
                    .findFirst().orElse(null);
            if (null == staDto) {
                return false;
            }
            if (!staDto.getOnline()) {
                return false;
            }
 
            // outbound: checkout nothing and inEnable
            if (type.equals(StaReserveType.IN)) {
                if (staDto.getOccupied()) {
                    return false;
                }
                if (!staDto.getInEnable()) {
                    return false;
                }
            }
            // inbound: checkout occupied and outEnable
            if (type.equals(StaReserveType.OUT)) {
                if (!staDto.getOccupied()) {
                    return false;
                }
                if (!staDto.getOutEnable()) {
                    return false;
                }
            }
        } catch (Exception e) {
            log.error("Conveyor query failed, FAIL-OPEN. staNo={}, taskId={}, type={}",
                    sta.getStaNo(),
                    task.getSeqNum(),
                    type,
                    e);
            return false;
        }
        // ok
        return true;
    }
 
    private String buildUrl(String path) {
        String host = conveyorProperties.getHost();
        Integer port = conveyorProperties.getPort();
 
        String p = (path == null) ? "" : (path.startsWith("/") ? path : ("/" + path));
        return "http://" + host + ":" + port + p;
    }
 
    private HttpResult<List<ConveyorStaDto>> postForResult(String url
            , Map<String, String> headers, Map<String, Object> params) throws Exception {
        String json = JSON.toJSONString(params);
        HttpGo.HttpResponse response = this.http.postJson(url, headers, json);
 
        int status = response.statusCode();
        if (status != 200) {
            throw new RuntimeException("Conveyor HTTP error: status=" + status + ", body=" + response.body());
        }
 
        String body = response.body();
        if (Cools.isEmpty(body)) {
            throw new RuntimeException("Conveyor empty response body.");
        }
 
        HttpResult<List<ConveyorStaDto>> result = JSON.parseObject(body, new TypeReference<HttpResult<List<ConveyorStaDto>>>() {});
        if (result == null) {
            throw new RuntimeException("Conveyor parse HttpResult failed: body=" + body);
        }
        return result;
    }
 
}