Commit c3484161 authored by wanghao's avatar wanghao

1 定时任务上料指令处理

parent 6fa1628f
...@@ -2,6 +2,7 @@ package com.zehong.system.mapper; ...@@ -2,6 +2,7 @@ package com.zehong.system.mapper;
import java.util.List; import java.util.List;
import com.zehong.system.domain.RobotArmCommand; import com.zehong.system.domain.RobotArmCommand;
import org.apache.ibatis.annotations.Param;
/** /**
* 机械臂指令Mapper接口 * 机械臂指令Mapper接口
...@@ -21,6 +22,9 @@ public interface RobotArmCommandMapper ...@@ -21,6 +22,9 @@ public interface RobotArmCommandMapper
public RobotArmCommand findExecutingCommand(String trayCode, String storeyCode); public RobotArmCommand findExecutingCommand(String trayCode, String storeyCode);
public RobotArmCommand selectLatestPendingTray();
public RobotArmCommand findByTrayAndStatus(@Param("trayCode") String trayCode, @Param("status") String status);
/** /**
* 查询机械臂指令列表 * 查询机械臂指令列表
* *
......
package com.zehong.system.modbus.service;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.zehong.system.domain.RobotArmCommand;
import com.zehong.system.mapper.RobotArmCommandMapper;
import com.zehong.system.service.IRobotArmCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.stereotype.Service;
/**
* @author lenovo
* @date 2025/8/6
* @description TODO
*/
@Service
public class ConveyorBeltMonitor {
@Value("${modbus.ip:192.168.2.11}")
private String modbusIp;
@Value("${modbus.port:502}")
private int modbusPort;
@Value("${modbus.deviceId:1}")
private int deviceId;
@Resource
private IRobotArmCommandService commandService;
@Resource
private RobotArmCommandMapper commandMapper;
private ModbusMaster modbusMaster;
private boolean lastConveyorState = false;
// 存储已检测到但未处理的托盘
private final ConcurrentMap<String, Long> detectedTrays = new ConcurrentHashMap<>();
@PostConstruct
public void init() throws ModbusInitException {
modbusMaster = getMaster(modbusIp, modbusPort);
}
//@Scheduled(fixedRate = 5000) // 每5秒执行一次
public void monitorConveyorBelt() {
try {
// 读取Modbus离散输入
boolean[] inputs = readDiscreteInputs(modbusMaster, deviceId, 0, 2);
// inputs[1] 表示传送带上是否有托盘
boolean currentState = inputs.length > 1 && inputs[1];
// 检测到新托盘(状态从false变为true)
if (currentState && !lastConveyorState) {
handleNewTrayDetected();
}
lastConveyorState = currentState;
} catch (Exception e) {
e.printStackTrace();
// 处理异常,如重新初始化Modbus连接
try {
modbusMaster = getMaster(modbusIp, modbusPort);
} catch (ModbusInitException ex) {
ex.printStackTrace();
}
}
}
private void handleNewTrayDetected() {
// 检查是否有待处理的托盘指令
if (detectedTrays.isEmpty()) {
// 查找最近扫码但未处理的托盘
RobotArmCommand pendingCommand = commandMapper.selectLatestPendingTray();
if (pendingCommand != null) {
detectedTrays.put(pendingCommand.getTrayCode(), System.currentTimeMillis());
createLoadingCommand(pendingCommand.getTrayCode());
} else {
System.out.println("检测到传送带上有托盘,但未找到扫码记录的托盘编号");
// 可以添加报警逻辑
}
}
}
public void createLoadingCommand(String trayCode) {
// 检查是否已存在相同托盘的待执行指令
RobotArmCommand existingCommand = commandMapper.findByTrayAndStatus(trayCode, "0");
if (existingCommand != null) {
System.out.println("托盘 " + trayCode + " 的指令已存在,跳过创建");
return;
}
// 创建新指令
RobotArmCommand command = new RobotArmCommand();
command.setTrayCode(trayCode);
command.setType("0"); // 待上料
command.setStatus("0"); // 待执行
command.setCreateTime(new Date());
commandMapper.insertRobotArmCommand(command);
System.out.println("已为托盘 " + trayCode + " 创建上料指令");
// 通知指令更新
commandService.processPendingCommands();
}
// 当指令执行完成时调用
public void onCommandCompleted(String trayCode) {
detectedTrays.remove(trayCode);
}
// 工具方法
private boolean[] readDiscreteInputs(ModbusMaster master, int deviceId, int startAddress, int length)
throws ModbusTransportException {
// 实现您的离散输入读取逻辑
// 这里简化为返回示例数据
return new boolean[]{false, true}; // 示例数据
}
private ModbusMaster getMaster(String ip, int port) throws ModbusInitException {
// 实现您的Modbus Master创建逻辑
return null; // 实际实现
}
}
...@@ -49,6 +49,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -49,6 +49,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND f_status = '1' <!-- 状态为执行中 --> AND f_status = '1' <!-- 状态为执行中 -->
LIMIT 1 LIMIT 1
</select> </select>
<!-- 查找最近扫码但未处理的托盘 -->
<select id="selectLatestPendingTray" resultMap="RobotArmCommandResult">
<include refid="selectRobotArmCommandVo"/>
WHERE f_status = '0' AND f_type = '0'
ORDER BY f_create_time DESC
LIMIT 1
</select>
<!-- 根据托盘编号和状态查找指令 -->
<select id="findByTrayAndStatus" resultMap="RobotArmCommandResult">
<include refid="selectRobotArmCommandVo"/>
WHERE f_tray_code = #{trayCode} AND f_status = #{status}
LIMIT 1
</select>
<insert id="insertRobotArmCommand" parameterType="RobotArmCommand" useGeneratedKeys="true" keyProperty="robotArmCommandId"> <insert id="insertRobotArmCommand" parameterType="RobotArmCommand" useGeneratedKeys="true" keyProperty="robotArmCommandId">
insert into t_robot_arm_command insert into t_robot_arm_command
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment