| | |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | log.info("执行大车设备操作: deviceId={}, deviceName={}, operation={}", |
| | | deviceId, deviceConfig.getDeviceName(), operation); |
| | | |
| | |
| | | Map<String, Object> logicParams) { |
| | | |
| | | VehicleTask task = new VehicleTask(); |
| | | task.setTaskId(generateTaskId(deviceConfig.getDeviceId())); |
| | | task.setTaskId(generateTaskId(String.valueOf(deviceConfig.getId()))); |
| | | task.setTaskName("大车设备-" + operation); |
| | | task.setOperation(operation); |
| | | |
| | |
| | | Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoFeed); |
| | | |
| | | List<GlassInfo> plannedGlasses = planGlassLoading(glassInfos, vehicleCapacity, glassGap, |
| | | deviceConfig.getDeviceId()); |
| | | String.valueOf(deviceConfig.getId())); |
| | | if (plannedGlasses == null) { |
| | | // 玻璃没有长度时返回null表示错误 |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | // 如果执行成功,更新位置信息到状态 |
| | | if (Boolean.TRUE.equals(result.getSuccess())) { |
| | | VehicleStatus status = statusManager.getOrCreateVehicleStatus( |
| | | deviceConfig.getDeviceId(), deviceConfig.getDeviceName()); |
| | | String.valueOf(deviceConfig.getId()), deviceConfig.getDeviceName()); |
| | | if (positionCode != null || positionValue != null) { |
| | | VehiclePosition position = new VehiclePosition(positionCode, positionValue); |
| | | status.setCurrentPosition(position); |
| | |
| | | |
| | | // 重置时,清除任务并恢复为空闲状态,停止监控 |
| | | if (Boolean.TRUE.equals(result.getSuccess())) { |
| | | statusManager.clearVehicleTask(deviceConfig.getDeviceId()); |
| | | statusManager.updateVehicleStatus(deviceConfig.getDeviceId(), VehicleState.IDLE); |
| | | stopStateMonitoring(deviceConfig.getDeviceId()); |
| | | statusManager.clearVehicleTask(String.valueOf(deviceConfig.getId())); |
| | | statusManager.updateVehicleStatus(String.valueOf(deviceConfig.getId()), VehicleState.IDLE); |
| | | stopStateMonitoring(String.valueOf(deviceConfig.getId())); |
| | | handleStopTaskMonitor(deviceConfig); |
| | | handleStopIdleMonitor(deviceConfig); |
| | | updateDeviceOnlineStatus(deviceConfig, true); |
| | | } else { |
| | | // 即便重置失败,也尝试停止内部监控,避免任务取消后仍然反复写PLC |
| | | stopStateMonitoring(deviceConfig.getDeviceId()); |
| | | stopStateMonitoring(String.valueOf(deviceConfig.getId())); |
| | | handleStopTaskMonitor(deviceConfig); |
| | | handleStopIdleMonitor(deviceConfig); |
| | | } |
| | |
| | | |
| | | // 清空后,恢复为空闲状态,停止监控 |
| | | if (Boolean.TRUE.equals(result.getSuccess())) { |
| | | statusManager.clearVehicleTask(deviceConfig.getDeviceId()); |
| | | statusManager.updateVehicleStatus(deviceConfig.getDeviceId(), VehicleState.IDLE); |
| | | stopStateMonitoring(deviceConfig.getDeviceId()); |
| | | statusManager.clearVehicleTask(String.valueOf(deviceConfig.getId())); |
| | | statusManager.updateVehicleStatus(String.valueOf(deviceConfig.getId()), VehicleState.IDLE); |
| | | stopStateMonitoring(String.valueOf(deviceConfig.getId())); |
| | | handleStopTaskMonitor(deviceConfig); |
| | | handleStopIdleMonitor(deviceConfig); |
| | | updateDeviceOnlineStatus(deviceConfig, true); |
| | | } else { |
| | | // 写入失败也尝试停止监控,避免任务取消后仍旧运行 |
| | | stopStateMonitoring(deviceConfig.getDeviceId()); |
| | | stopStateMonitoring(String.valueOf(deviceConfig.getId())); |
| | | handleStopTaskMonitor(deviceConfig); |
| | | handleStopIdleMonitor(deviceConfig); |
| | | } |
| | |
| | | deviceStatusService.updateDeviceOnlineStatus(deviceConfig.getId(), status); |
| | | } catch (Exception e) { |
| | | log.warn("同步设备在线状态到数据库失败: deviceId={}, online={}, error={}", |
| | | deviceConfig.getDeviceId(), online, e.getMessage()); |
| | | String.valueOf(deviceConfig.getId()), online, e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | * 定期检查大车的 state1~6,当检测到 state=1 时自动协调卧转立设备 |
| | | */ |
| | | private void startStateMonitoring(DeviceConfig deviceConfig, Map<String, Object> logicParams) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | |
| | | // 如果已经在监控,先停止旧的监控任务 |
| | | stopStateMonitoring(deviceId); |
| | |
| | | * 检查大车状态并协调卧转立设备(内部方法,由监控线程调用) |
| | | */ |
| | | private void checkAndCoordinateState(DeviceConfig deviceConfig) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | List<String> alreadyCoordinated = coordinatedStates.get(deviceId); |
| | | if (alreadyCoordinated == null) { |
| | | alreadyCoordinated = new CopyOnWriteArrayList<>(); |
| | |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | |
| | | // 停止旧的监控任务 |
| | | handleStopIdleMonitor(deviceConfig); |
| | |
| | | * 停止空闲监控 |
| | | */ |
| | | private DevicePlcVO.OperationResult handleStopIdleMonitor(DeviceConfig deviceConfig) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | ScheduledFuture<?> future = idleMonitoringTasks.remove(deviceId); |
| | | if (future != null && !future.isCancelled()) { |
| | | future.cancel(false); |
| | |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | PlcClient plcClient = getPlcClient(deviceConfig); |
| | | if (plcClient == null) { |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | */ |
| | | private Integer getCurrentPosition(DeviceConfig deviceConfig, Map<String, Object> logicParams) { |
| | | // 从状态管理器获取 |
| | | VehicleStatus status = statusManager.getVehicleStatus(deviceConfig.getDeviceId()); |
| | | VehicleStatus status = statusManager.getVehicleStatus(String.valueOf(deviceConfig.getId())); |
| | | if (status != null && status.getCurrentPosition() != null) { |
| | | return status.getCurrentPosition().getPositionValue(); |
| | | } |
| | |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | |
| | | // 停止旧的监控任务 |
| | | handleStopTaskMonitor(deviceConfig); |
| | |
| | | MesTaskInfo taskInfo, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | PlcClient plcClient = getPlcClient(deviceConfig); |
| | | if (plcClient == null) { |
| | | return; |
| | |
| | | if (taskInfo.brokenGlassIndices != null && taskInfo.brokenGlassIndices.contains(i)) { |
| | | updateStateIfNeeded(deviceConfig, plcClient, stateValues, stateField, 8, taskInfo); |
| | | log.info("玻璃标记为破损: deviceId={}, stateField={}, glassIndex={}", |
| | | deviceConfig.getDeviceId(), stateField, i); |
| | | String.valueOf(deviceConfig.getId()), stateField, i); |
| | | continue; |
| | | } |
| | | |
| | |
| | | if (elapsed >= state3TimeoutTime && (currentState == null || currentState < 2)) { |
| | | updateStateIfNeeded(deviceConfig, plcClient, stateValues, stateField, 3, taskInfo); |
| | | log.warn("任务超时未完成: deviceId={}, stateField={}, elapsed={}ms, expectedTime={}ms", |
| | | deviceConfig.getDeviceId(), stateField, elapsed, state2Time); |
| | | String.valueOf(deviceConfig.getId()), stateField, elapsed, state2Time); |
| | | continue; |
| | | } |
| | | |
| | |
| | | // 注意:如果当前state已经是3(未完成)或8(破损),不再更新 |
| | | if (currentState != null && (currentState == 3 || currentState == 8)) { |
| | | log.debug("任务状态已为异常状态,不再更新: deviceId={}, stateField={}, currentState={}, targetState={}", |
| | | deviceConfig.getDeviceId(), stateField, currentState, targetState); |
| | | String.valueOf(deviceConfig.getId()), stateField, currentState, targetState); |
| | | return false; |
| | | } |
| | | |
| | |
| | | plcClient.writeData(payload); |
| | | |
| | | log.info("任务状态已更新到PLC: deviceId={}, stateField={}, currentState={}, targetState={}", |
| | | deviceConfig.getDeviceId(), stateField, currentState, targetState); |
| | | String.valueOf(deviceConfig.getId()), stateField, currentState, targetState); |
| | | // 返回true表示状态发生了变化 |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("写入PLC state字段失败: deviceId={}, stateField={}, targetState={}, error={}", |
| | | deviceConfig.getDeviceId(), stateField, targetState, e.getMessage()); |
| | | String.valueOf(deviceConfig.getId()), stateField, targetState, e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | .map(g -> g.glassId) |
| | | .collect(java.util.stream.Collectors.joining(",")); |
| | | log.info("已给MES汇报({}任务): deviceId={}, glassCount={}, glassIds=[{}]", |
| | | taskType, deviceConfig.getDeviceId(), taskInfo.glasses.size(), glassIds); |
| | | taskType, String.valueOf(deviceConfig.getId()), taskInfo.glasses.size(), glassIds); |
| | | |
| | | // 多设备任务场景下,不在这里阻塞等待MES确认,由任务引擎定时调用checkMesConfirm |
| | | } catch (Exception e) { |
| | | log.error("给MES汇报异常: deviceId={}", deviceConfig.getDeviceId(), e); |
| | | log.error("给MES汇报异常: deviceId={}", String.valueOf(deviceConfig.getId()), e); |
| | | } |
| | | } |
| | | |
| | |
| | | public DevicePlcVO.OperationResult checkMesConfirm(DeviceConfig deviceConfig, |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | PlcClient plcClient = getPlcClient(deviceConfig); |
| | | if (plcClient == null) { |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | } catch (Exception e) { |
| | | log.warn("MES确认超时时清空任务状态失败: deviceId={}, error={}", deviceId, e.getMessage()); |
| | | } |
| | | statusManager.updateVehicleStatus(deviceConfig.getDeviceId(), VehicleState.ERROR); |
| | | statusManager.clearVehicleTask(deviceConfig.getDeviceId()); |
| | | currentTasks.remove(deviceConfig.getDeviceId()); |
| | | statusManager.updateVehicleStatus(String.valueOf(deviceConfig.getId()), VehicleState.ERROR); |
| | | statusManager.clearVehicleTask(String.valueOf(deviceConfig.getId())); |
| | | currentTasks.remove(String.valueOf(deviceConfig.getId())); |
| | | handleStopTaskMonitor(deviceConfig); |
| | | |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | |
| | | // 任务完成,恢复为空闲状态 |
| | | statusManager.updateVehicleStatus( |
| | | deviceConfig.getDeviceId(), VehicleState.IDLE); |
| | | statusManager.clearVehicleTask(deviceConfig.getDeviceId()); |
| | | String.valueOf(deviceConfig.getId()), VehicleState.IDLE); |
| | | statusManager.clearVehicleTask(String.valueOf(deviceConfig.getId())); |
| | | |
| | | // 移除任务记录(如果有) |
| | | currentTasks.remove(deviceConfig.getDeviceId()); |
| | | currentTasks.remove(String.valueOf(deviceConfig.getId())); |
| | | |
| | | // 停止任务监控 |
| | | handleStopTaskMonitor(deviceConfig); |
| | |
| | | payload.put("plcRequest", 1); |
| | | plcClient.writeData(payload); |
| | | |
| | | log.info("MES任务已确认完成: deviceId={}", deviceConfig.getDeviceId()); |
| | | log.info("MES任务已确认完成: deviceId={}", String.valueOf(deviceConfig.getId())); |
| | | String taskType = (taskInfo != null && taskInfo.isOutbound) ? "出片" : "进片"; |
| | | return DevicePlcVO.OperationResult.builder() |
| | | .success(true) |
| | |
| | | .data(data) |
| | | .build(); |
| | | } catch (Exception e) { |
| | | log.error("检查MES确认状态异常: deviceId={}", deviceConfig.getDeviceId(), e); |
| | | log.error("检查MES确认状态异常: deviceId={}", String.valueOf(deviceConfig.getId()), e); |
| | | return DevicePlcVO.OperationResult.builder() |
| | | .success(false) |
| | | .message("检查MES确认状态异常: " + e.getMessage()) |
| | |
| | | private DevicePlcVO.OperationResult handleMarkBroken(DeviceConfig deviceConfig, |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | MesTaskInfo taskInfo = currentTasks.get(deviceId); |
| | | if (taskInfo == null) { |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | payload.put("plcReport", 0); |
| | | plcClient.writeData(payload); |
| | | } catch (Exception e) { |
| | | log.error("清空任务状态异常: deviceId={}", deviceConfig.getDeviceId(), e); |
| | | log.error("清空任务状态异常: deviceId={}", String.valueOf(deviceConfig.getId()), e); |
| | | } |
| | | } |
| | | |
| | |
| | | * 停止任务监控 |
| | | */ |
| | | private DevicePlcVO.OperationResult handleStopTaskMonitor(DeviceConfig deviceConfig) { |
| | | String deviceId = deviceConfig.getDeviceId(); |
| | | String deviceId = String.valueOf(deviceConfig.getId()); |
| | | ScheduledFuture<?> future = taskMonitoringTasks.remove(deviceId); |
| | | if (future != null && !future.isCancelled()) { |
| | | future.cancel(false); |