#
Junjie
2025-08-13 629959675a97b4d41c1c36eaa60ee1671c224e93
src/main/java/com/zy/asrs/controller/LiftController.java
New file
@@ -0,0 +1,361 @@
package com.zy.asrs.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.domain.param.LiftOperatorParam;
import com.zy.asrs.domain.vo.LiftDataVo;
import com.zy.asrs.domain.vo.LiftMsgTableVo;
import com.zy.asrs.domain.vo.LiftSensorDataVo;
import com.zy.asrs.entity.BasLift;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasLiftService;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.common.service.CommonService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.action.LiftAction;
import com.zy.core.cache.OutputQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.command.LiftAssignCommand;
import com.zy.core.model.command.LiftCommand;
import com.zy.core.model.command.LiftRedisCommand;
import com.zy.core.model.protocol.LiftProtocol;
import com.zy.core.model.protocol.LiftStaProtocol;
import com.zy.core.thread.LiftThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
 * 提升机接口
 */
@Slf4j
@RestController
@RequestMapping("/lift")
public class LiftController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private BasLiftService basLiftService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private LiftAction liftAction;
    @Autowired
    private DeviceConfigService deviceConfigService;
    @PostMapping("/table/lift/state")
    @ManagerAuth(memo = "提升机信息表")
    public R liftStateTable(){
        ArrayList<JSONObject> list = new ArrayList<>();
        List<DeviceConfig> liftList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Lift)));
        for (DeviceConfig device : liftList) {
            // 表格行
            JSONObject baseObj = new JSONObject();
            baseObj.put("liftNo", device.getDeviceNo());
            list.add(baseObj);
            // 获取提升机信息
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getDeviceNo());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getStatus();
            if (liftProtocol == null) {
                continue;
            }
            JSONObject data = JSON.parseObject(JSON.toJSONString(liftProtocol));
            List<LiftStaProtocol> liftStaProtocols = liftThread.getLiftStaProtocols();
            data.put("liftStaProtocols", liftStaProtocols);
            baseObj.putAll(data);
        }
        return R.ok().add(list);
    }
    @PostMapping("/table/lift/msg")
    @ManagerAuth(memo = "提升机数据表")
    public R liftMsgTable(){
        List<LiftMsgTableVo> list = new ArrayList<>();
        List<DeviceConfig> liftList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Lift)));
        for (DeviceConfig device : liftList) {
            // 表格行
            LiftMsgTableVo vo = new LiftMsgTableVo();
            vo.setLiftNo(device.getDeviceNo());   //  提升机号
            list.add(vo);
            // 获取提升机信息
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getDeviceNo());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getStatus();
            if (liftProtocol == null) {
                continue;
            }
            vo.setWorkNo(liftProtocol.getTaskNo());//任务号
            vo.setPakMk(liftProtocol.getPakMk()?"Y" : "N");    // 作业标记
            vo.setLiftStaProtocols(liftThread.getLiftStaProtocols());
        }
        return R.ok().add(list);
    }
    @PostMapping("/output/lift")
    @ManagerAuth
    public R liftOutput(){
        StringBuilder str = new StringBuilder();
        String s;
        int i = 0;
        while((s = OutputQueue.LIFT.poll()) != null && i <=10) {
            str.append("\n").append(s);
            i++;
        }
        return R.ok().add(str.toString());
    }
    @GetMapping("/detl/{liftNo}")
    public R liftDetl(@PathVariable("liftNo") Integer liftNo){
        LiftDataVo vo = new LiftDataVo();
        List<DeviceConfig> liftList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Lift)));
        for (DeviceConfig device : liftList) {
            if (liftNo.equals(device.getDeviceNo())) {
                vo.setLiftNo(device.getDeviceNo());
                BasLift basLift = basLiftService.selectById(device.getDeviceNo());
                if (!Cools.isEmpty(basLift)) {
                    vo.setWorkNo(basLift.getWrkNo());
                    vo.setPakMk(basLift.getPakMk());
                }
                break;
            }
        }
        return R.ok().add(vo);
    }
    @GetMapping("/sensor/detl/{liftNo}")
    public R liftSensorDetl(@PathVariable("liftNo") Integer liftNo){
        LiftSensorDataVo vo = new LiftSensorDataVo();
        List<DeviceConfig> liftList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Lift)));
        for (DeviceConfig device : liftList) {
            if (liftNo.equals(device.getDeviceNo())) {
                vo.setLiftNo(device.getDeviceNo());
                // 获取提升机信息
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getDeviceNo());
                if (liftThread == null) {
                    return R.error("设备不在线");
                }
                LiftProtocol liftProtocol = liftThread.getStatus();
                if (liftProtocol == null) {
                    return R.error("设备不在线");
                }
                break;
            }
        }
        return R.ok().add(vo);
    }
    @PostMapping("/detl/update")
    @ManagerAuth(memo = "修改数据")
    public R liftUpdate(@RequestParam Integer liftNo,
                        @RequestParam Integer workNo) {
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo);
        if (liftThread == null) {
            return R.error("plc已掉线");
        }
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            return R.error("plc已掉线");
        }
        if (workNo != null) {
            liftThread.setSyncTaskNo(workNo);
        }
        return R.ok();
    }
    @RequestMapping(value = "/command/query")
    public R liftCommandQuery(@RequestParam("wrkNo") Integer wrkNo) {
        Object o = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        if (o == null) {
            return R.error();
        }
        LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class);
        return R.ok().add(redisCommand);
    }
    //回退命令
    @RequestMapping(value = "/command/rollback")
    public R liftCommandRollback(@RequestParam("wrkNo") Integer wrkNo
            , @RequestParam("commandStep") Integer commandStep) {
        Object o = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        if (o == null) {
            return R.error();
        }
        LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class);
        redisCommand.setCommandStep(commandStep);
        redisUtil.set(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo, JSON.toJSONString(redisCommand));
        return R.ok();
    }
    //命令完成状态切换
    @RequestMapping(value = "/command/completeSwitch")
    public R liftCommandCompleteSwitch(@RequestParam("wrkNo") Integer wrkNo
            , @RequestParam("commandStep") Integer commandStep
            , @RequestParam("complete") Integer complete) {
        Object o = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        if (o == null) {
            return R.error();
        }
        LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class);
        LiftAssignCommand assignCommand = redisCommand.getAssignCommand();
        List<LiftCommand> commands = assignCommand.getCommands();
        LiftCommand command = commands.get(commandStep);
        command.setComplete(complete != 0);
        redisUtil.set(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo, JSON.toJSONString(redisCommand));
        return R.ok();
    }
    //重启任务(命令)
    @RequestMapping(value = "/command/restart")
    public R liftCommandCompleteSwitch(@RequestParam("wrkNo") Integer wrkNo) {
        Object o = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        if (o == null) {
            return R.error();
        }
        LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class);
        Integer liftNo = redisCommand.getLiftNo();
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo);
        if (liftThread == null) {
            return R.error();
        }
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            return R.error();
        }
        if (!liftThread.isIdle()) {
            return R.error();
        }
        //提升机处于空闲状态,进行任务的恢复
        liftThread.setSyncTaskNo(redisCommand.getWrkNo());//将提升机线程分配任务号
        return R.ok();
    }
    //删除任务(命令)
    @RequestMapping(value = "/command/del")
    public R liftCommandDel(@RequestParam("wrkNo") Integer wrkNo) {
        Object o = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        if (o == null) {
            return R.error();
        }
        redisUtil.del(RedisKeyType.LIFT_WORK_FLAG.key + wrkNo);
        return R.ok();
    }
    /****************************************************************/
    /************************** 手动操作 ******************************/
    /****************************************************************/
    @ManagerAuth(memo = "手动操作")
    @PostMapping("/operator/lift")
    public R liftOperator(LiftOperatorParam param){
        if (Cools.isEmpty(param.getLiftNo())) {
            return R.parse(BaseRes.PARAM);
        }
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, param.getLiftNo());
        if (liftThread == null) {
            throw new CoolException("提升机不在线");
        }
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            throw new CoolException("提升机不在线");
        }
        if (param.getLiftTaskMode().equals(LiftTaskModeType.SHUTTLE_SWITCH.id)) {
            //小车换层
            int workNo = commonService.getWorkNo(WrkIoType.MANUAL.id);//获取任务号
            Integer startSta = param.getSourceStaNo();
            Integer targetSta = param.getStaNo();
            //获取提升机命令
            LiftCommand liftCommand = liftThread.getShuttleSwitchCommand(workNo, startSta, targetSta);
            ArrayList<LiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(workNo);
            assignCommand.setAuto(false);//手动模式
            assignCommand.setTaskMode(LiftTaskModeType.SHUTTLE_SWITCH.id);
            liftAction.assignWork(liftProtocol.getLiftNo(), assignCommand);
            return R.ok();
        } else if (param.getLiftTaskMode().equals(LiftTaskModeType.PICK_PUT.id)) {
            //移动托盘
            int workNo = commonService.getWorkNo(WrkIoType.MANUAL.id);//获取任务号
            Integer startSta = param.getSourceStaNo();
            Integer targetSta = param.getStaNo();
            //获取提升机命令
            LiftCommand liftCommand = liftThread.getPickAndPutCommand(workNo, startSta, targetSta);
            ArrayList<LiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(workNo);
            assignCommand.setAuto(false);//手动模式
            assignCommand.setTaskMode(LiftTaskModeType.PICK_PUT.id);
            liftAction.assignWork(liftProtocol.getLiftNo(), assignCommand);
            return R.ok();
        } else if (param.getLiftTaskMode().equals(LiftTaskModeType.MOVE.id)) {
            //移动
            int workNo = commonService.getWorkNo(WrkIoType.MANUAL.id);//获取任务号
            Integer startSta = param.getSourceStaNo();
            Integer targetSta = param.getStaNo();
            //获取提升机命令
            LiftCommand liftCommand = liftThread.getMoveCommand(workNo, startSta, targetSta);
            ArrayList<LiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(workNo);
            assignCommand.setAuto(false);//手动模式
            assignCommand.setTaskMode(LiftTaskModeType.MOVE.id);
            liftAction.assignWork(liftProtocol.getLiftNo(), assignCommand);
            return R.ok();
        } else if (param.getLiftTaskMode().equals(LiftTaskModeType.RESET.id)) {
            //提升机复位
            liftThread.setSyncTaskNo(0);
            liftThread.setProtocolStatus(LiftProtocolStatusType.IDLE);
            return R.ok();
        } else {
            throw new CoolException("未知命令");
        }
    }
}