parent
12cf1008cb
commit
22388b1fe5
@ -0,0 +1,103 @@
|
||||
package com.zhehekeji.web.service.ksec;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.zhehekeji.common.util.SpringContextUtil;
|
||||
import com.zhehekeji.core.pojo.Result;
|
||||
import com.zhehekeji.web.config.ConfigProperties;
|
||||
import com.zhehekeji.web.entity.IndustrialCameraReqVO;
|
||||
import com.zhehekeji.web.lib.CameraControlModule;
|
||||
import com.zhehekeji.web.lib.CameraDelayTask;
|
||||
import com.zhehekeji.web.lib.TaskDelayExecutor;
|
||||
import com.zhehekeji.web.pojo.IndustrialCameraVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.zhehekeji.web.service.KuKouService.setTrayCode;
|
||||
|
||||
@Slf4j
|
||||
public class CommandQueue {
|
||||
// 增加queue
|
||||
public static void addCommand(IndustrialCameraVO scTransmission) {
|
||||
queue.add(scTransmission);
|
||||
}
|
||||
private static ExecutorService exec = Executors.newFixedThreadPool(1);
|
||||
|
||||
private static LinkedBlockingQueue<IndustrialCameraVO> queue = new LinkedBlockingQueue<>();
|
||||
|
||||
public static void sentHttp(RestTemplate restTemplate, ConfigProperties configProperties){
|
||||
exec.execute(new Consumer(restTemplate, configProperties));
|
||||
}
|
||||
|
||||
private static class Consumer implements Runnable {
|
||||
RestTemplate restTemplate ;
|
||||
ConfigProperties configProperties;
|
||||
Consumer(RestTemplate restTemplate, ConfigProperties configProperties){
|
||||
this.restTemplate = restTemplate;
|
||||
this.configProperties = configProperties;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
// CameraControlModule cameraControlModule = SpringContextUtil.getBean(CameraControlModule.class);
|
||||
IndustrialCameraVO scTransmission = queue.take();
|
||||
if(scTransmission != null){
|
||||
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Content-Type", "application/json");
|
||||
headers.set("User-Agent", "Mozilla/5.0");
|
||||
|
||||
scTransmission.setTrayCode(setTrayCode(scTransmission.getTrayCode()));
|
||||
IndustrialCameraReqVO industrialCameraReqVO = new IndustrialCameraReqVO();
|
||||
BeanUtil.copyProperties(scTransmission, industrialCameraReqVO);
|
||||
industrialCameraReqVO.setFlag(1);
|
||||
|
||||
log.info("发送盘点请求: {}", scTransmission.toString());
|
||||
Result<IndustrialCameraReqVO> result = Result.success(industrialCameraReqVO, "图像识别完成");
|
||||
|
||||
HttpEntity<Result<IndustrialCameraReqVO>> entity = new HttpEntity<>(result, headers);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
configProperties.getKsec().getReportHttp(),
|
||||
HttpMethod.POST,
|
||||
entity,
|
||||
new ParameterizedTypeReference<String>() {}
|
||||
);
|
||||
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
log.info("请求成功,响应:{}", response.getBody());
|
||||
} else {
|
||||
log.warn("请求未成功,状态码:{}", response.getStatusCode());
|
||||
addCommand(scTransmission);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
scTransmission.setSentCount(scTransmission.getSentCount() + 1);
|
||||
log.error("第{}请求失败 参数{}", scTransmission.getSentCount() , scTransmission.toString(), e);
|
||||
if (scTransmission.getSentCount() < 30) {
|
||||
|
||||
addCommand(scTransmission);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue