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.framework.common.SnowflakeIdWorker;
|
import com.zy.acs.manager.common.config.ConveyorProperties;
|
import com.zy.acs.manager.common.constant.Constants;
|
import com.zy.acs.manager.common.utils.HttpGo;
|
import com.zy.acs.manager.core.domain.type.NamespaceType;
|
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.IntegrationRecord;
|
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.IntegrationDirectionType;
|
import com.zy.acs.manager.manager.enums.StaReserveType;
|
import com.zy.acs.manager.manager.enums.StatusType;
|
import com.zy.acs.manager.manager.service.IntegrationRecordService;
|
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.*;
|
|
@Slf4j
|
@Service
|
public class SiemensConveyorStationService implements ConveyorStationService {
|
|
@Autowired
|
private ConveyorProperties conveyorProperties;
|
@Autowired
|
private SnowflakeIdWorker snowflakeIdWorker;
|
@Autowired
|
private IntegrationRecordService integrationRecordService;
|
|
private HttpGo http;
|
|
@PostConstruct
|
public void init() {
|
int timeoutSeconds = conveyorProperties.getTimeout() / 1000;
|
this.http = HttpGo.builder()
|
.connectTimeout(Duration.ofSeconds(timeoutSeconds))
|
.readTimeout(Duration.ofSeconds(timeoutSeconds))
|
// .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();
|
Date now = new Date();
|
|
// url
|
String url = this.http.buildUrl(conveyorProperties.getHost(), conveyorProperties.getPort(), conveyorProperties.getUrl());
|
// 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);
|
|
IntegrationRecord integrationRecord = new IntegrationRecord(
|
String.valueOf(snowflakeIdWorker.nextId()).substring(3), // 编号
|
NamespaceType.RCS_TASK_REPORT.desc, // 名称空间
|
conveyorProperties.getUrl(), // 接口地址
|
null, // 平台密钥
|
Constants.RCS, // 调用方标识
|
IntegrationDirectionType.OUTBOUND.value, // 方向[非空]
|
String.valueOf(now.getTime()), // 时间戳
|
conveyorProperties.getHost(), // 客户端IP
|
JSON.toJSONString(params), // 请求内容
|
null, // 响应内容
|
null, // 异常内容
|
0, // 结果
|
null, // 耗时
|
StatusType.ENABLE.val, // 状态
|
now, // 添加时间[非空]
|
now, // 修改时间[非空]
|
null // 备注
|
);
|
|
// do request
|
HttpResult<List<ConveyorStaDto>> result;
|
try {
|
result = postForResult(url, headers, params);
|
integrationRecord.setResponse(JSON.toJSONString(result));
|
|
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;
|
}
|
}
|
|
integrationRecord.setResult(1);
|
} catch (Exception e) {
|
log.error("Conveyor query failed, FAIL-OPEN. staNo={}, taskId={}, type={}",
|
sta.getStaNo(),
|
task.getSeqNum(),
|
type,
|
e);
|
integrationRecord.setErr(e.getMessage());
|
return false;
|
} finally {
|
integrationRecord.setCostMs((int) (System.currentTimeMillis() - now.getTime()));
|
integrationRecordService.syncRecord(integrationRecord);
|
}
|
// ok
|
return true;
|
}
|
|
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;
|
}
|
|
}
|