| | |
| | | import com.zy.api.controller.params.ReceviceTaskParams; |
| | | import com.zy.api.controller.params.StopOutTaskParams; |
| | | import com.zy.api.controller.params.WorkTaskParams; |
| | | import com.zy.api.entity.CrnProtocol; |
| | | import com.zy.api.entity.DeviceStatusVo; |
| | | import com.zy.api.entity.StationProtocol; |
| | | import com.zy.api.service.WcsApiService; |
| | | import com.zy.asrs.entity.*; |
| | | import com.zy.asrs.service.*; |
| | |
| | | @Slf4j |
| | | @Service |
| | | public class WcsApiServiceImpl implements WcsApiService { |
| | | private static final Long WCS_SYNC_USER = 9999L; |
| | | private static final String YES = "Y"; |
| | | private static final String NO = "N"; |
| | | |
| | | @Autowired |
| | | private LocMastService locMastService; |
| | |
| | | |
| | | @Value("${wcs.address.stopOutTask}") |
| | | private String stopOutTask; |
| | | @Value("${wcs.address.getDeviceStatus:/openapi/getDeviceStatus}") |
| | | private String getDeviceStatus; |
| | | @Value("${wcs.status-sync.method:GET}") |
| | | private String deviceStatusMethod; |
| | | @Autowired |
| | | private CommonService commonService; |
| | | @Autowired |
| | | private BasDevpService basDevpService; |
| | | @Autowired |
| | | private BasCrnpService basCrnpService; |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public R syncDeviceStatusFromWcs() { |
| | | if (!Boolean.parseBoolean(String.valueOf(switchValue))) { |
| | | return R.ok("WCS开关关闭"); |
| | | } |
| | | String response = null; |
| | | try { |
| | | response = requestDeviceStatusFromWcs(); |
| | | JSONObject jsonObject = JSON.parseObject(response == null ? "{}" : response); |
| | | Integer code = jsonObject.getInteger("code"); |
| | | if (!Objects.equals(code, 200)) { |
| | | String msg = jsonObject.getString("msg"); |
| | | return R.error(Cools.isEmpty(msg) ? "获取WCS设备状态失败" : msg); |
| | | } |
| | | JSONObject data = jsonObject.getJSONObject("data"); |
| | | DeviceStatusVo deviceStatusVo = data == null |
| | | ? new DeviceStatusVo() |
| | | : JSON.parseObject(data.toJSONString(), DeviceStatusVo.class); |
| | | |
| | | int stationCount = syncStationStatus(deviceStatusVo.getStationList()); |
| | | int crnCount = syncCrnStatus(deviceStatusVo.getCrnList()); |
| | | |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("stationCount", stationCount); |
| | | result.put("crnCount", crnCount); |
| | | log.info("同步WCS设备状态成功, stationCount={}, crnCount={}", stationCount, crnCount); |
| | | return R.ok("同步成功").add(result); |
| | | } catch (Exception e) { |
| | | log.error("同步WCS设备状态异常, response={}", response, e); |
| | | return R.error("同步WCS设备状态失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private boolean requiresOutboundErpConfirm(WrkMast wrkMast) { |
| | |
| | | } |
| | | } |
| | | |
| | | private String requestDeviceStatusFromWcs() throws IOException { |
| | | HttpHandler.Builder builder = new HttpHandler.Builder() |
| | | .setUri(wcs_address) |
| | | .setPath(getDeviceStatus) |
| | | .setTimeout(10, TimeUnit.SECONDS); |
| | | String method = Cools.isEmpty(deviceStatusMethod) ? "GET" : deviceStatusMethod.trim().toUpperCase(Locale.ROOT); |
| | | if ("POST".equals(method)) { |
| | | return builder.setJson("{}").build().doPost(); |
| | | } |
| | | return builder.build().doGet(); |
| | | } |
| | | |
| | | private int syncStationStatus(List<StationProtocol> stationList) { |
| | | if (stationList == null || stationList.isEmpty()) { |
| | | return 0; |
| | | } |
| | | int count = 0; |
| | | Date now = new Date(); |
| | | for (StationProtocol stationProtocol : stationList) { |
| | | if (stationProtocol == null || stationProtocol.getStationId() == null) { |
| | | continue; |
| | | } |
| | | BasDevp basDevp = basDevpService.selectById(stationProtocol.getStationId()); |
| | | boolean isNew = Objects.isNull(basDevp); |
| | | if (isNew) { |
| | | basDevp = new BasDevp(); |
| | | basDevp.setDevNo(stationProtocol.getStationId()); |
| | | basDevp.setAppeUser(WCS_SYNC_USER); |
| | | basDevp.setAppeTime(now); |
| | | } |
| | | basDevp.setInEnable(toFlag(stationProtocol.isInEnable())); |
| | | basDevp.setOutEnable(toFlag(stationProtocol.isOutEnable())); |
| | | basDevp.setAutoing(toFlag(stationProtocol.isAutoing())); |
| | | basDevp.setLoading(toFlag(stationProtocol.isLoading())); |
| | | basDevp.setCanining(toFlag(stationProtocol.isEnableIn())); |
| | | basDevp.setCanouting(toFlag(!stationProtocol.isRunBlock())); |
| | | basDevp.setWrkNo(defaultZero(stationProtocol.getTaskNo())); |
| | | basDevp.setBarcode(normalizeText(stationProtocol.getBarcode())); |
| | | basDevp.setGrossWt(stationProtocol.getWeight() == null ? 0D : stationProtocol.getWeight()); |
| | | basDevp.setModiUser(WCS_SYNC_USER); |
| | | basDevp.setModiTime(now); |
| | | if (isNew) { |
| | | if (!basDevpService.insert(basDevp)) { |
| | | throw new CoolException("新增站点状态失败, stationId=" + stationProtocol.getStationId()); |
| | | } |
| | | } else if (!basDevpService.updateById(basDevp)) { |
| | | throw new CoolException("更新站点状态失败, stationId=" + stationProtocol.getStationId()); |
| | | } |
| | | count++; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | private int syncCrnStatus(List<CrnProtocol> crnList) { |
| | | if (crnList == null || crnList.isEmpty()) { |
| | | return 0; |
| | | } |
| | | int count = 0; |
| | | Date now = new Date(); |
| | | for (CrnProtocol crnProtocol : crnList) { |
| | | if (crnProtocol == null || crnProtocol.getCrnNo() == null) { |
| | | continue; |
| | | } |
| | | BasCrnp basCrnp = basCrnpService.selectById(crnProtocol.getCrnNo()); |
| | | boolean isNew = Objects.isNull(basCrnp); |
| | | if (isNew) { |
| | | basCrnp = new BasCrnp(); |
| | | basCrnp.setCrnNo(crnProtocol.getCrnNo()); |
| | | basCrnp.setInEnable(YES); |
| | | basCrnp.setOutEnable(YES); |
| | | basCrnp.setAppeUser(WCS_SYNC_USER); |
| | | basCrnp.setAppeTime(now); |
| | | } |
| | | // crn_sts 本地表存的是“堆垛机模式(手动/自动/电脑)”,因此必须写 mode,不能写 status。 |
| | | basCrnp.setCrnSts(defaultZero(crnProtocol.getMode())); |
| | | basCrnp.setWrkNo(defaultZero(crnProtocol.getTaskNo())); |
| | | basCrnp.setCrnErr(crnProtocol.getAlarm() == null ? 0L : Long.valueOf(crnProtocol.getAlarm())); |
| | | basCrnp.setModiUser(WCS_SYNC_USER); |
| | | basCrnp.setModiTime(now); |
| | | if (isNew) { |
| | | if (!basCrnpService.insert(basCrnp)) { |
| | | throw new CoolException("新增堆垛机状态失败, crnNo=" + crnProtocol.getCrnNo()); |
| | | } |
| | | } else if (!basCrnpService.updateById(basCrnp)) { |
| | | throw new CoolException("更新堆垛机状态失败, crnNo=" + crnProtocol.getCrnNo()); |
| | | } |
| | | count++; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | private Integer defaultZero(Integer value) { |
| | | return value == null ? 0 : value; |
| | | } |
| | | |
| | | private String normalizeText(String value) { |
| | | return Cools.isEmpty(value) ? "" : value; |
| | | } |
| | | |
| | | private String toFlag(boolean value) { |
| | | return value ? YES : NO; |
| | | } |
| | | |
| | | } |