|
|
|
|
@ -3,38 +3,26 @@ package cn.iocoder.yudao.module.camera.controller.admin.kesc;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.dal.dataobject.checklog.CheckLogDO;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.dal.dataobject.order.OrderDO;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.dal.dataobject.resources.URLResourcesDo;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.dal.dataobject.stock.StockDO;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.dal.entity.ScanData;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.framework.netty.ksec.KsecDataInfo;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.service.checklog.CheckLogService;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.service.plc.PLCService;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.service.resources.URLResourcesService;
|
|
|
|
|
import cn.iocoder.yudao.module.camera.service.stock.StockService;
|
|
|
|
|
import cn.iocoder.yudao.module.logistics.dal.dataobject.checkconf.CheckConfDO;
|
|
|
|
|
import cn.iocoder.yudao.module.logistics.service.checkconf.CheckConfService;
|
|
|
|
|
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
|
|
|
|
|
import cn.iocoder.yudao.module.system.service.sse.SseClient;
|
|
|
|
|
import cn.iocoder.yudao.module.system.util.RedisUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import jakarta.annotation.security.PermitAll;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
@ -53,15 +41,6 @@ public class StockControlController {
|
|
|
|
|
SseClient sseClient;
|
|
|
|
|
@Resource
|
|
|
|
|
RedisUtil redisUtil;
|
|
|
|
|
@Resource
|
|
|
|
|
DictDataService dictDataService;
|
|
|
|
|
@Resource
|
|
|
|
|
OkHttpClient client;
|
|
|
|
|
@Resource
|
|
|
|
|
URLResourcesService urlResourcesService;
|
|
|
|
|
|
|
|
|
|
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
|
|
private final Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
// 创建一个有界队列的线程池用于异步执行PLC操作。
|
|
|
|
|
// 核心线程 16、最大线程 32、有界队列 200、空闲线程 60s 回收。
|
|
|
|
|
@ -150,264 +129,56 @@ public class StockControlController {
|
|
|
|
|
log.info("盘点任务:{}", kescEntity);
|
|
|
|
|
// kescEntity = kescEntity.fromBySRMNumber(kescEntity);
|
|
|
|
|
kescEntity.setWmsExtendInfoIsEmpty();
|
|
|
|
|
// 幂等:50 秒内相同 taskId + cmd 只执行一次,失败时删除标记允许重试
|
|
|
|
|
String dedupKey = "check:dedup:" + kescEntity.getTaskId() + ":" + kescEntity.getCmdName();
|
|
|
|
|
|
|
|
|
|
// 幂等:使用 Redis List,key 记录 taskId,value 记录已处理过的 cmdName
|
|
|
|
|
String dedupKey = "check:dedup:" + kescEntity.getTaskId();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
executorService.submit(() -> {
|
|
|
|
|
// 移动盘点同步
|
|
|
|
|
if (kescEntity.getCmdName().equals("inventoryArrived")) {
|
|
|
|
|
plcService.checkStart(kescEntity);
|
|
|
|
|
plcService.check(kescEntity.getTaskId(), kescEntity.getCmdName());
|
|
|
|
|
inventoryEndReport(kescEntity, "arrivedEnd");
|
|
|
|
|
|
|
|
|
|
} else if (kescEntity.getCmdName().contains("inventoryLoad")) {
|
|
|
|
|
// 到位盘点
|
|
|
|
|
CheckLogDO checkLogDO;
|
|
|
|
|
if (!redisUtil.setIfAbsent(dedupKey, "1", 50)) {
|
|
|
|
|
log.info("check 幂等拦截,50秒内重复请求,taskId:{}, cmd:{}", kescEntity.getTaskId(), kescEntity.getCmdName());
|
|
|
|
|
checkLogDO = checkLogService.getOne(
|
|
|
|
|
new QueryWrapper<CheckLogDO>()
|
|
|
|
|
.eq("task_id", kescEntity.getTaskId())
|
|
|
|
|
.last("limit 1").orderByDesc("create_time")
|
|
|
|
|
);
|
|
|
|
|
}else {
|
|
|
|
|
checkLogDO = plcService.check(kescEntity.getTaskId(), kescEntity.getCmdName());
|
|
|
|
|
}
|
|
|
|
|
checkLogDO = checkLogService.getById(checkLogDO.getId());
|
|
|
|
|
String cmdName = kescEntity.getCmdName();
|
|
|
|
|
boolean keyExists = redisUtil.hasKey(dedupKey);
|
|
|
|
|
List<Object> cmdList = keyExists ? redisUtil.lGet(dedupKey, 0, -1) : null;
|
|
|
|
|
boolean valueExists = cmdList != null && cmdList.contains(cmdName);
|
|
|
|
|
|
|
|
|
|
inventoryEndReport(kescEntity, "loadEnd");
|
|
|
|
|
resultReport(checkLogDO);
|
|
|
|
|
if (!keyExists) {
|
|
|
|
|
// 不存在 key:首次请求,启动盘点
|
|
|
|
|
plcService.checkStart(kescEntity);
|
|
|
|
|
}
|
|
|
|
|
if (!valueExists) {
|
|
|
|
|
// 存在 key,不存在 value:新的命令,执行盘点
|
|
|
|
|
plcService.check(kescEntity.getTaskId(), cmdName);
|
|
|
|
|
redisUtil.lSet(dedupKey, cmdName, 300);
|
|
|
|
|
} else {
|
|
|
|
|
// 存在 key 和 value:幂等拦截
|
|
|
|
|
log.info("check 幂等拦截,5分钟内重复请求,taskId:{}, cmd:{}", kescEntity.getTaskId(), cmdName);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return CommonResult.success("OK");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateResult(OrderDO order) {
|
|
|
|
|
if (order == null) {
|
|
|
|
|
log.warn("resultReport stockDO is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 从字典配置读取上层系统地址(key 可按实际配置调整)
|
|
|
|
|
// String host = dictDataService.parseDictData("base_conf", "result_report_ip").getValue();
|
|
|
|
|
// String port =
|
|
|
|
|
//cssc_update_status_api
|
|
|
|
|
String url = dictDataService.parseDictData("base_conf", "cssc_update_status_api").getValue();
|
|
|
|
|
List<CheckConfDO> checkConfList = checkConfService.list(new QueryWrapper<CheckConfDO>()
|
|
|
|
|
.ne("check_scan_type", 0)
|
|
|
|
|
.eq("is_Enable", 1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 组装请求体
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
body.put("taskId", order.getTaskId());
|
|
|
|
|
body.put("tid", order.getTid()); // 上层系统 jobid
|
|
|
|
|
|
|
|
|
|
String pic[] = order.getPics().split(";");
|
|
|
|
|
body.put("url", (pic[pic.length - 1]));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
body.put("resultType", "monitorMode");
|
|
|
|
|
|
|
|
|
|
String json = gson.toJson(body);
|
|
|
|
|
okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(json, JSON);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
log.info("resultReport 上报结果 url:{} body:{} response:{}", url, json, response.code());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("resultReport 上报异常 url:{} body:{}", url, json, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plcService.inventoryEndReport(kescEntity, toEndType(cmdName));
|
|
|
|
|
|
|
|
|
|
@PostMapping("/action")
|
|
|
|
|
@Operation(summary = "随行开始任务")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
@PermitAll
|
|
|
|
|
public CommonResult<String> action(@RequestBody KsecDataInfo kescEntity) {
|
|
|
|
|
log.info("随行开始任务:{}", kescEntity);
|
|
|
|
|
// kescEntity = kescEntity.fromBySRMNumber(kescEntity);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 随行开始
|
|
|
|
|
if (kescEntity.getCmdName().equals("B1")) {
|
|
|
|
|
plcService.orderStart(kescEntity);
|
|
|
|
|
} else if (kescEntity.getCmdName().contains("C")) {
|
|
|
|
|
// 随行正在进行
|
|
|
|
|
plcService.action(kescEntity.getTaskId(), kescEntity.getCmdName());
|
|
|
|
|
} else if (kescEntity.getCmdName().equals("B2")) {
|
|
|
|
|
// 随行结束
|
|
|
|
|
plcService.orderStop(kescEntity.getTaskId());
|
|
|
|
|
}
|
|
|
|
|
return CommonResult.success("OK");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setResult(String taskId, String result) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将识别结果单独上报给上层系统
|
|
|
|
|
* Request URL:http://[IP]:[Port]/api/resultReport
|
|
|
|
|
* <p>
|
|
|
|
|
* 说明:
|
|
|
|
|
* - shelfCode / pltCode / itemCode / countNumber 的识别值在 extendInfo 中,预期值在 wmsExtendInfo 中(key 为检查项 checkName)
|
|
|
|
|
* - 图片 url:stockDO.checkPic 存的是 uuid,真实 url 在 logistics_url_resources 表中
|
|
|
|
|
* 将 cmdName 转换为上报类型:去除 inventory 前缀,首字母小写,追加 "End"。
|
|
|
|
|
* 例如 inventoryArrived -> arrivedEnd,inventoryLoad -> loadEnd
|
|
|
|
|
*/
|
|
|
|
|
private void resultReport(CheckLogDO checkLogDO) {
|
|
|
|
|
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
if (checkLogDO == null) {
|
|
|
|
|
log.error("resultReport checkLogDO is null");
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
|
|
// 组装请求体
|
|
|
|
|
body.put("taskId", checkLogDO.getTaskId());
|
|
|
|
|
body.put("tid", checkLogDO.getTid()); // 上层系统 jobid
|
|
|
|
|
body.put("batchNumber", checkLogDO.getBatchNumber());
|
|
|
|
|
|
|
|
|
|
body.put("url", queryPicUrl(checkLogDO.getPic()));
|
|
|
|
|
}
|
|
|
|
|
// 从字典配置读取上层系统地址(key 可按实际配置调整)
|
|
|
|
|
// String host = dictDataService.parseDictData("base_conf", "result_report_ip").getValue();
|
|
|
|
|
// String port =
|
|
|
|
|
String url = dictDataService.parseDictData("base_conf", "cssc_update_result_api").getValue();
|
|
|
|
|
if (url == null) {
|
|
|
|
|
log.error("resultReport url is null");
|
|
|
|
|
}
|
|
|
|
|
List<CheckConfDO> checkConfList = checkConfService.list(new QueryWrapper<CheckConfDO>()
|
|
|
|
|
.ne("check_scan_type", 0)
|
|
|
|
|
.eq("is_Enable", 1));
|
|
|
|
|
|
|
|
|
|
Map<String, String> extendInfo = checkLogDO != null ? checkLogDO.getExtendInfo() : new HashMap<>();
|
|
|
|
|
Map<String, String> wmsExtendInfo =checkLogDO != null ? checkLogDO.getWmsExtendInfo() : new HashMap<>();
|
|
|
|
|
|
|
|
|
|
// // 识别结果(实际扫描值)
|
|
|
|
|
// String shelfCode = extendInfo.get("shelfCode");
|
|
|
|
|
// String pltCode = extendInfo.get("pltCode");
|
|
|
|
|
// String itemCode = extendInfo.get("itemCode");
|
|
|
|
|
// String countNumber = extendInfo.get("countNumber");
|
|
|
|
|
// // 预期值(WMS 下发)
|
|
|
|
|
// String wmsShelfCode = wmsExtendInfo.get("shelfCode");
|
|
|
|
|
// String wmsPltCode = wmsExtendInfo.get("pltCode");
|
|
|
|
|
// String wmsItemCode = wmsExtendInfo.get("itemCode");
|
|
|
|
|
// String wmsCountNumber = wmsExtendInfo.get("countNumber");
|
|
|
|
|
|
|
|
|
|
// body.put("shelfCode", shelfCode);
|
|
|
|
|
// body.put("pltCode", pltCode);
|
|
|
|
|
// body.put("itemCode", itemCode);
|
|
|
|
|
// body.put("countNumber", countNumber);
|
|
|
|
|
// body.put("shelfCodeResult", shelfCode);
|
|
|
|
|
// body.put("pltCodeResult", pltCode);
|
|
|
|
|
// body.put("itemCodeResult", itemCode);
|
|
|
|
|
// body.put("countNumberResult", countNumber);
|
|
|
|
|
for (CheckConfDO checkConfDO : checkConfList) {
|
|
|
|
|
String checkName = checkConfDO.getCheckName();
|
|
|
|
|
body.put(checkName, extendInfo.get(checkName));
|
|
|
|
|
body.put(checkName + "Result", wmsExtendInfo.get(checkName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 比对结果
|
|
|
|
|
Map<String, Object> inventoryResult = new HashMap<>();
|
|
|
|
|
for (CheckConfDO checkConfDO : checkConfList) {
|
|
|
|
|
inventoryResult.put(checkConfDO.getCheckName(), compare(extendInfo.get(checkConfDO.getCheckName()), wmsExtendInfo.get(checkConfDO.getCheckName())));
|
|
|
|
|
inventoryResult.put(checkConfDO.getCheckName() + "Msg", compareMsg(extendInfo.get(checkConfDO.getCheckName()), wmsExtendInfo.get(checkConfDO.getCheckName())));
|
|
|
|
|
}
|
|
|
|
|
body.put("inventoryResult", inventoryResult);
|
|
|
|
|
|
|
|
|
|
String json = gson.toJson(body);
|
|
|
|
|
okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(json, JSON);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
log.info("resultReport 上报结果 url:{} body:{} response:{}", url, json, response.code());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("resultReport 上报异常 url:{} body:{}", url, json, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 盘点结束上报给上层系统
|
|
|
|
|
* Request URL:http://[IP]:[Port]/api/inventoryEndReport
|
|
|
|
|
*
|
|
|
|
|
* @param stockDO 盘点状态 DO
|
|
|
|
|
*/
|
|
|
|
|
private void inventoryEndReport(KsecDataInfo stockDO, String type) {
|
|
|
|
|
if (stockDO == null) {
|
|
|
|
|
log.warn("inventoryEndReport stockDO is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 从字典配置读取上层系统地址
|
|
|
|
|
String url = dictDataService.parseDictData("base_conf", "cssc_inventory_end_report_api").getValue();
|
|
|
|
|
if (url == null) {
|
|
|
|
|
return;
|
|
|
|
|
private static String toEndType(String cmdName) {
|
|
|
|
|
if (cmdName == null || cmdName.isEmpty()) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 组装请求体
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
body.put("taskId", stockDO.getTaskId()); // 任务号
|
|
|
|
|
// 结果类型:盘点
|
|
|
|
|
body.put("cmdName", type);
|
|
|
|
|
|
|
|
|
|
body.put("resultType", "inventory"); // 结果类型:盘点
|
|
|
|
|
// 命令码
|
|
|
|
|
body.put("shelfCode", stockDO.getShelfCode()); // 货架号
|
|
|
|
|
body.put("pltCode", stockDO.getPltCode()); // 托盘条码
|
|
|
|
|
body.put("batchNumber", stockDO.getBatchNumber());// 盘点批次
|
|
|
|
|
|
|
|
|
|
String json = gson.toJson(body);
|
|
|
|
|
okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(json, JSON);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
log.info("inventoryEndReport 上报结果 url:{} body:{} response:{}", url, json, response.code());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("inventoryEndReport 上报异常 url:{} body:{}", url, json, e);
|
|
|
|
|
String suffix = cmdName;
|
|
|
|
|
if (suffix.startsWith("inventory")) {
|
|
|
|
|
suffix = suffix.substring("inventory".length());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 uuid 从 logistics_url_resources 表查询图片真实 url
|
|
|
|
|
*/
|
|
|
|
|
private String queryPicUrl(String uuid) {
|
|
|
|
|
if (uuid == null || uuid.isEmpty()) {
|
|
|
|
|
return "";
|
|
|
|
|
if (suffix.isEmpty()) {
|
|
|
|
|
return "End";
|
|
|
|
|
}
|
|
|
|
|
List<URLResourcesDo> list = urlResourcesService.list(
|
|
|
|
|
new LambdaQueryWrapper<URLResourcesDo>()
|
|
|
|
|
.eq(URLResourcesDo::getUuid, uuid)
|
|
|
|
|
.orderByAsc(URLResourcesDo::getCreateTime)
|
|
|
|
|
.last("limit 1"));
|
|
|
|
|
return list == null || list.isEmpty() ? "" : list.get(0).getUrl();
|
|
|
|
|
// 首字母小写
|
|
|
|
|
suffix = Character.toLowerCase(suffix.charAt(0)) + suffix.substring(1);
|
|
|
|
|
return suffix + "End";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 比对两个值是否一致,返回 "true"/"false"
|
|
|
|
|
*/
|
|
|
|
|
private String compare(String actual, String expect) {
|
|
|
|
|
if (actual == null && expect == null) return "true";
|
|
|
|
|
if (actual == null || expect == null) return "false";
|
|
|
|
|
return actual.equals(expect) ? "true" : "false";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 比对失败时的原因文案
|
|
|
|
|
*/
|
|
|
|
|
private String compareMsg(String actual, String expect) {
|
|
|
|
|
if (actual == null && expect == null) return "";
|
|
|
|
|
if (actual == null) return "错误原因:未扫到";
|
|
|
|
|
if (expect == null) return "错误原因:无预期值";
|
|
|
|
|
return actual.equals(expect) ? "" : "错误原因:比对错误";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
CheckConfService checkConfService;
|
|
|
|
|
|