Junjie
2 天以前 63b01db83d9aad8a15276b4236a9a22e4aeef065
src/main/java/com/zy/asrs/controller/OpenController.java
@@ -6,11 +6,13 @@
import com.core.exception.CoolException;
import com.zy.asrs.domain.Result.CancelTaskBatchResult;
import com.zy.asrs.domain.param.*;
import com.core.annotations.ManagerAuth;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.RuntimeConfigService;
import com.zy.asrs.service.WrkMastService;
import com.zy.common.annotations.OpenApiLog;
import com.zy.common.service.CommonService;
@@ -21,13 +23,13 @@
import com.zy.core.model.protocol.DualCrnProtocol;
import com.zy.core.model.protocol.RgvProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.network.fake.FakeConfigKeys;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.DualCrnThread;
import com.zy.core.thread.RgvThread;
import com.zy.core.thread.StationThread;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import com.zy.system.service.HighPrivilegeGrantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,12 +37,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Slf4j
@RestController
@@ -64,9 +66,7 @@
    @Autowired
    private DeviceConfigService deviceConfigService;
    @Autowired
    private HighPrivilegeGrantService highPrivilegeGrantService;
    @Autowired
    private HttpServletRequest request;
    private RuntimeConfigService runtimeConfigService;
    // 移库任务
    @PostMapping("/createLocMoveTask")
@@ -144,7 +144,7 @@
        if (param == null) {
            return R.error("参数不能为空");
        }
        boolean completeTask = commonService.cancelTask(param, false);
        boolean completeTask = commonService.cancelTask(param);
        if (completeTask) {
            return R.ok();
        }
@@ -170,7 +170,7 @@
        if (param == null) {
            return R.error("参数不能为空");
        }
        CancelTaskBatchResult result = commonService.cancelTaskBatch(param, false);
        CancelTaskBatchResult result = commonService.cancelTaskBatch(param);
        return R.ok().add(result);
    }
@@ -391,6 +391,15 @@
        return R.ok().add(map);
    }
    @PostMapping("/runtimeConfig/update")
    @OpenApiLog(memo = "修改运行参数")
    public R updateRuntimeConfig(@RequestBody RuntimeConfigUpdateParam param) {
        if (param == null || param.getConfigMap() == null || param.getConfigMap().isEmpty()) {
            return R.error("参数不能为空");
        }
        return R.ok().add(runtimeConfigService.updateRuntimeConfig(param.getConfigMap()));
    }
    @GetMapping("/getFakeSystemRunStatus")
    public R getFakeSystemRunStatus() {
        HashMap<String, Object> map = new HashMap<>();
@@ -431,4 +440,87 @@
        return R.ok();
    }
    @GetMapping("/fakeConfig/auth")
    @ManagerAuth
    public R getFakeConfig() {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        for (String key : FakeConfigKeys.ALL_KEYS) {
            String defaultValue = FakeConfigKeys.DEFAULTS.get(key);
            map.put(key, configService.getConfigValue(key, defaultValue));
        }
        return R.ok().add(map);
    }
    @PostMapping("/fakeConfig/save/auth")
    @ManagerAuth
    public R saveFakeConfig(@RequestBody FakeConfigSaveParam param) {
        if (param == null || param.getConfigMap() == null || param.getConfigMap().isEmpty()) {
            return R.error("参数不能为空");
        }
        Set<String> allowKeys = FakeConfigKeys.ALL_KEYS;
        Map<String, String> normalizedMap = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : param.getConfigMap().entrySet()) {
            String key = entry.getKey();
            if (!allowKeys.contains(key)) {
                return R.error("不支持的配置项: " + key);
            }
            normalizedMap.put(key, normalizeFakeConfigValue(key, entry.getValue()));
        }
        validateFakeCrnLaserConfig(normalizedMap);
        for (Map.Entry<String, String> entry : normalizedMap.entrySet()) {
            if (!configService.saveConfigValue(entry.getKey(), entry.getValue())) {
                return R.error("保存失败: " + entry.getKey());
            }
        }
        configService.refreshSystemConfigCache();
        return getFakeConfig();
    }
    private void validateFakeCrnLaserConfig(Map<String, String> normalizedMap) {
        long bayMin = resolveFakeLongValue(normalizedMap, FakeConfigKeys.FAKE_CRN_BAY_MIN);
        long bayMax = resolveFakeLongValue(normalizedMap, FakeConfigKeys.FAKE_CRN_BAY_MAX);
        long laserMin = resolveFakeLongValue(normalizedMap, FakeConfigKeys.FAKE_CRN_LASER_MIN);
        long laserMax = resolveFakeLongValue(normalizedMap, FakeConfigKeys.FAKE_CRN_LASER_MAX);
        if (bayMax <= bayMin) {
            throw new CoolException("堆垛机 bay 最大值必须大于最小值");
        }
        if (laserMax <= laserMin) {
            throw new CoolException("堆垛机镭射最大值必须大于最小值");
        }
    }
    private long resolveFakeLongValue(Map<String, String> normalizedMap, String key) {
        String value = normalizedMap.get(key);
        if (value == null) {
            value = configService.getConfigValue(key, FakeConfigKeys.DEFAULTS.get(key));
        }
        return Long.parseLong(value);
    }
    private String normalizeFakeConfigValue(String key, Object rawValue) {
        if (rawValue == null) {
            throw new CoolException(key + " 参数不能为空");
        }
        String value = String.valueOf(rawValue).trim();
        if (FakeConfigKeys.BOOLEAN_KEYS.contains(key)) {
            if (!"Y".equalsIgnoreCase(value) && !"N".equalsIgnoreCase(value)) {
                throw new CoolException(key + " 仅支持 Y/N");
            }
            return value.toUpperCase();
        }
        if (FakeConfigKeys.LONG_KEYS.contains(key)) {
            long parsed;
            try {
                parsed = Long.parseLong(value);
            } catch (Exception e) {
                throw new CoolException(key + " 必须为非负整数");
            }
            if (parsed < 0) {
                throw new CoolException(key + " 必须为非负整数");
            }
            return String.valueOf(parsed);
        }
        throw new CoolException("不支持的配置项: " + key);
    }
}