package com.zy.acs.manager.core.integrate.wms;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.TypeReference;
|
import com.zy.acs.framework.common.Cools;
|
import com.zy.acs.framework.common.R;
|
import com.zy.acs.manager.common.config.UplinkProperties;
|
import com.zy.acs.manager.common.utils.HttpGo;
|
import com.zy.acs.manager.core.integrate.dto.HttpResult;
|
import com.zy.acs.manager.core.service.ThreadPoolRegulator;
|
import com.zy.acs.manager.manager.entity.Task;
|
import com.zy.acs.manager.manager.enums.TaskStsType;
|
import com.zy.acs.manager.manager.enums.TaskUplinkStateType;
|
import com.zy.acs.manager.manager.service.TaskService;
|
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.HashMap;
|
import java.util.Map;
|
import java.util.concurrent.CompletableFuture;
|
|
@Slf4j
|
@Service
|
public class TaskReportService {
|
|
@Autowired
|
private UplinkProperties uplinkProperties;
|
@Autowired
|
private TaskService taskService;
|
@Autowired
|
private ThreadPoolRegulator threadPoolRegulator;
|
|
private HttpGo http;
|
|
@PostConstruct
|
public void init() {
|
int timeoutSeconds = uplinkProperties.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();
|
}
|
|
public boolean reportFinished(Task task) {
|
if (Cools.isEmpty(task)) {
|
return false;
|
}
|
if (!uplinkProperties.getEnabled()) {
|
return false;
|
}
|
if (!task.getTaskSts().equals(TaskStsType.COMPLETE.val())) {
|
return false;
|
}
|
TaskUplinkStateType uplinkStateType = TaskUplinkStateType.of(task.getUplinkSts());
|
if (!uplinkStateType.equals(TaskUplinkStateType.PENDING) && !uplinkStateType.equals(TaskUplinkStateType.FAILED)) {
|
return false;
|
}
|
|
// block
|
// Future<R> future = threadPoolRegulator.getInstance().submit(() -> {
|
// mapDataDispatcher.modifyDynamicMatrix(null, null, param.getAgvNo(), true);
|
// return success();
|
// });
|
// System.out.println(JSON.toJSONString(future.get()));
|
|
// non-block
|
CompletableFuture<?> completableFuture = CompletableFuture.supplyAsync(() -> {
|
// mapDataDispatcher.modifyDynamicMatrix(null, null, param.getAgvNo(), true);
|
// avoidWaveCalculator.calcDynamicNodeByVehicle(agv, null);
|
return R.ok();
|
}, threadPoolRegulator.getInstance());
|
|
// url
|
String url = this.http.buildUrl(uplinkProperties.getHost(), uplinkProperties.getPort(), "/cv/station/query");
|
// headers
|
Map<String, String> headers = new HashMap<>();
|
// params
|
Map<String, Object> params = new HashMap<>();
|
|
HttpResult<?> result;
|
try {
|
result = postForResult(url, headers, params);
|
Integer code = result.getCode();
|
if (null == code || 200 != code) {
|
return false;
|
}
|
Object data = result.getData();
|
if (Cools.isEmpty(data)) {
|
return false;
|
}
|
} catch (Exception e) {
|
log.error("Uplink report failed, taskId={}",
|
JSON.toJSONString(task),
|
e);
|
return false;
|
}
|
return true;
|
}
|
|
private HttpResult<?> 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("Uplink HTTP error: status=" + status + ", body=" + response.body());
|
}
|
|
String body = response.body();
|
if (Cools.isEmpty(body)) {
|
throw new RuntimeException("Uplink empty response body.");
|
}
|
|
HttpResult<?> result = JSON.parseObject(body, new TypeReference<HttpResult<?>>() {});
|
if (result == null) {
|
throw new RuntimeException("Uplink parse HttpResult failed: body=" + body);
|
}
|
return result;
|
}
|
|
|
}
|