Junjie
昨天 a4f07b2a0ddb6c210e05afbbb491feeb466203e7
src/main/java/com/zy/asrs/controller/ConsoleController.java
@@ -3,8 +3,8 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
@@ -59,6 +59,8 @@
    private LocMastService locMastService;
    @Autowired
    private BasMapService basMapService;
    @Autowired
    private StationCycleCapacityService stationCycleCapacityService;
    @PostMapping("/system/running/status")
    @ManagerAuth(memo = "系统运行状态")
@@ -90,10 +92,10 @@
    public R stationLatestData() {
        List<StationLatestDataVo> vos = new ArrayList<>();
        WrkLastno inTaskRange = wrkLastnoService.selectById(WrkIoType.IN.id);
        WrkLastno outTaskRange = wrkLastnoService.selectById(WrkIoType.OUT.id);
        WrkLastno inTaskRange = wrkLastnoService.getById(WrkIoType.IN.id);
        WrkLastno outTaskRange = wrkLastnoService.getById(WrkIoType.OUT.id);
        List<DeviceConfig> devpList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
        List<DeviceConfig> devpList = deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Devp)));
        for (DeviceConfig deviceConfig : devpList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp,
@@ -124,6 +126,7 @@
                vo.setErrorMsg(stationProtocol.getErrorMsg()); // 报警信息
                vo.setBarcode(stationProtocol.getBarcode()); // 条码
                vo.setWeight(stationProtocol.getWeight());//重量
                vo.setTaskWriteIdx(stationProtocol.getTaskWriteIdx());//任务可写区
                String stationStatus = StationStatusType.process(stationProtocol).toString().toLowerCase().replaceAll("_", "-");
                if (stationProtocol.isAutoing() && stationProtocol.isLoading() && stationProtocol.getTaskNo() > 0 && !stationProtocol.isRunBlock()) {
                    String taskClass = getStationTaskClass(stationProtocol.getTaskNo(), inTaskRange, outTaskRange);
@@ -164,7 +167,7 @@
    public R crnLatestData() {
        List<CrnLatestDataVo> vos = new ArrayList<>();
        List<DeviceConfig> crnList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
        List<DeviceConfig> crnList = deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Crn)));
        for (DeviceConfig deviceConfig : crnList) {
            // 获取堆垛机信息
@@ -191,7 +194,7 @@
                vo.setCrnStatus(CrnStatusType.MACHINE_ERROR);
            } else {
                if (crnProtocol.getTaskNo() > 0) {
                    WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo());
                    WrkMast wrkMast = wrkMastService.getById(crnProtocol.getTaskNo());
                    if (wrkMast != null) {
                        vo.setCrnStatus(CrnStatusType.process(wrkMast.getIoType()));
                    } else {
@@ -212,7 +215,7 @@
    @ManagerAuth(memo = "双工位堆垛机实时数据")
    public R dualCrnLatestData() {
        List<CrnLatestDataVo> vos = new ArrayList<>();
        List<DeviceConfig> dualCrnList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
        List<DeviceConfig> dualCrnList = deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.DualCrn)));
        for (DeviceConfig deviceConfig : dualCrnList) {
            DualCrnThread crnThread = (DualCrnThread) SlaveConnection.get(SlaveType.DualCrn, deviceConfig.getDeviceNo());
@@ -245,7 +248,7 @@
    @ManagerAuth(memo = "RGV实时数据")
    public R rgvLatestData(){
        List<RgvLatestDataVo> vos = new ArrayList<>();
        List<DeviceConfig> rgvList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
        List<DeviceConfig> rgvList = deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Rgv)));
        for (DeviceConfig deviceConfig : rgvList) {
            RgvThread rgvThread = (RgvThread) SlaveConnection.get(SlaveType.Rgv, deviceConfig.getDeviceNo());
@@ -264,6 +267,11 @@
            vos.add(vo);
        }
        return R.ok().add(vos);
    }
    @GetMapping("/latest/data/station/cycle/capacity")
    public R stationCycleCapacity() {
        return R.ok().add(stationCycleCapacityService.getLatestSnapshot());
    }
    // @PostMapping("/latest/data/barcode")
@@ -292,7 +300,7 @@
        }
        CrnDetailVo vo = new CrnDetailVo();
        DeviceConfig deviceConfig = deviceConfigService.selectOne(new EntityWrapper<DeviceConfig>()
        DeviceConfig deviceConfig = deviceConfigService.getOne(new QueryWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Crn))
                .eq("device_no", crnNo));
@@ -307,12 +315,12 @@
        vo.setCrnStatus(crnProtocol.getStatusType().desc);
        if (crnProtocol.getAlarm() > 0) {
            BasCrnpErr crnError = basCrnpErrService.selectById(crnProtocol.getAlarm());
            BasCrnpErr crnError = basCrnpErrService.getById(crnProtocol.getAlarm());
            vo.setError(crnError == null ? "未知异常" : crnError.getErrName());
        }
        if (crnProtocol.getTaskNo() > 0) {
            WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo());
            WrkMast wrkMast = wrkMastService.getById(crnProtocol.getTaskNo());
            if (wrkMast != null) {
                vo.setSourceStaNo(String.valueOf(wrkMast.getSourceStaNo()));
                vo.setStaNo(String.valueOf(wrkMast.getStaNo()));
@@ -342,19 +350,124 @@
     */
    @GetMapping("/map/{lev}/auth")
    public R getLocMap(@PathVariable Integer lev) {
        Object object = redisUtil.get(RedisKeyType.LOC_MAP_BASE.key);
        List<List<HashMap<String, Object>>> mapNodeList = null;
        if (object != null) {
            mapNodeList = (List<List<HashMap<String, Object>>>) object;
        List<List<HashMap<String, Object>>> mapNodeList = getLocMapBaseSnapshot();
        if (mapNodeList == null || mapNodeList.isEmpty()) {
            return R.error("请先初始化地图");
        }
        List<LocMast> locMastList = locMastService.selectLocByLev(lev);
        boolean needRefreshBase = false;
        for (LocMast locMast : locMastList) {
            String[] locType = locMast.getLocType().split("-");
            HashMap<String, Object> mapNode = mapNodeList.get(Integer.parseInt(locType[0])).get(Integer.parseInt(locType[1]));
            Integer[] pos = parseLocTypePos(locMast.getLocType());
            if (pos == null || !isValidMapNodeIndex(mapNodeList, pos[0], pos[1])) {
                needRefreshBase = true;
                break;
            }
        }
        if (needRefreshBase) {
            refreshLocMapBaseCache();
            mapNodeList = getLocMapBaseSnapshot();
        }
        for (LocMast locMast : locMastList) {
            Integer[] pos = parseLocTypePos(locMast.getLocType());
            if (pos == null || !isValidMapNodeIndex(mapNodeList, pos[0], pos[1])) {
                log.warn("locMap skip invalid locType, locNo={}, locType={}", locMast.getLocNo(), locMast.getLocType());
                continue;
            }
            HashMap<String, Object> mapNode = mapNodeList.get(pos[0]).get(pos[1]);
            mapNode.put("locSts", locMast.getLocSts());
            mapNode.put("locNo", locMast.getLocNo());
        }
        return R.ok().add(mapNodeList);
    }
    private List<List<HashMap<String, Object>>> getLocMapBaseSnapshot() {
        Object object = redisUtil.get(RedisKeyType.LOC_MAP_BASE.key);
        if (!(object instanceof List)) {
            return buildLocMapBase();
        }
        return cloneMapNodeList((List<List<HashMap<String, Object>>>) object);
    }
    private void refreshLocMapBaseCache() {
        List<List<HashMap<String, Object>>> base = buildLocMapBase();
        if (base != null && !base.isEmpty()) {
            redisUtil.set(RedisKeyType.LOC_MAP_BASE.key, base);
        }
    }
    private List<List<HashMap<String, Object>>> buildLocMapBase() {
        BasMap basMap = basMapService.getOne(new QueryWrapper<BasMap>().eq("lev", 1));
        if (Cools.isEmpty(basMap) || Cools.isEmpty(basMap.getData())) {
            return null;
        }
        List<List<JSONObject>> dataList = JSON.parseObject(basMap.getData(), List.class);
        List<List<HashMap<String, Object>>> mapNodeList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            List<JSONObject> row = dataList.get(i);
            List<HashMap<String, Object>> mapNodeRow = new ArrayList<>();
            for (int j = 0; j < row.size(); j++) {
                JSONObject map = row.get(j);
                HashMap<String, Object> mapNode = new HashMap<>();
                mapNode.put("id", i + "-" + j);
                String nodeType = map.getString("type");
                mapNode.put("type", nodeType);
                if ("shelf".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.NORMAL_PATH.id);
                } else if ("devp".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.DISABLE.id);
                } else if ("crn".equals(nodeType) || "dualCrn".equals(nodeType) || "rgv".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.MAIN_PATH.id);
                } else {
                    mapNode.put("value", MapNodeType.DISABLE.id);
                }
                mapNodeRow.add(mapNode);
            }
            mapNodeList.add(mapNodeRow);
        }
        return mapNodeList;
    }
    private List<List<HashMap<String, Object>>> cloneMapNodeList(List<List<HashMap<String, Object>>> source) {
        List<List<HashMap<String, Object>>> copy = new ArrayList<>();
        for (List<HashMap<String, Object>> row : source) {
            List<HashMap<String, Object>> rowCopy = new ArrayList<>();
            if (row != null) {
                for (HashMap<String, Object> item : row) {
                    rowCopy.add(item == null ? new HashMap<>() : new HashMap<>(item));
                }
            }
            copy.add(rowCopy);
        }
        return copy;
    }
    private Integer[] parseLocTypePos(String locType) {
        if (Cools.isEmpty(locType)) {
            return null;
        }
        String[] parts = locType.split("-");
        if (parts.length < 2) {
            return null;
        }
        try {
            return new Integer[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1])};
        } catch (NumberFormatException e) {
            log.warn("parse locType fail, locType={}", locType, e);
            return null;
        }
    }
    private boolean isValidMapNodeIndex(List<List<HashMap<String, Object>>> mapNodeList, Integer rowIdx, Integer colIdx) {
        if (mapNodeList == null || rowIdx == null || colIdx == null || rowIdx < 0 || colIdx < 0 || rowIdx >= mapNodeList.size()) {
            return false;
        }
        List<HashMap<String, Object>> row = mapNodeList.get(rowIdx);
        return row != null && colIdx < row.size();
    }
    @RequestMapping(value = "/map/locList")
@@ -364,9 +477,9 @@
            JSONArray data = JSON.parseArray(object.toString());
            return R.ok().add(data);
        }
        EntityWrapper<LocMast> wrapper = new EntityWrapper<>();
        QueryWrapper<LocMast> wrapper = new QueryWrapper<>();
        wrapper.eq("lev1", 1);
        List<LocMast> locMasts = locMastService.selectList(wrapper);
        List<LocMast> locMasts = locMastService.list(wrapper);
        redisUtil.set(RedisKeyType.LOC_MAST_MAP_LIST.key, JSON.toJSONString(locMasts), 60 * 60 * 24);
        return R.ok().add(locMasts);
    }