#
Junjie
13 小时以前 26784989e73fc36c6315e54939d1b13a50eb5020
src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
@@ -107,6 +107,7 @@
        OperateResultExOne<byte[]> result = siemensNet.Read("DB100.0", (short) (statusList.size() * 10));
        if (result.IsSuccess) {
            byte[] taskBufferRaw = readTaskBufferRaw();
            for (int i = 0; i < statusList.size(); i++) {
                ZyStationStatusEntity statusEntity = statusList.get(i); // 站点编号
                statusEntity.setTaskNo(siemensNet.getByteTransform().TransInt32(result.Content, i * 10)); // 工作号
@@ -139,7 +140,7 @@
                statusEntity.setError(0);//默认无报警
                statusEntity.setTaskWriteIdx((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 10 + 8));//任务可写区
                fillTaskBufferStatus(i, statusEntity);
                fillTaskBufferStatus(taskBufferRaw, i, statusEntity);
            }
        }
@@ -243,32 +244,6 @@
            return commandResponse;
        }
        if (isDuplicateStationCommand(statusEntity, command)) {
            log.info("输送线命令重复,已跳过当前站点命令。任务号={},站点号={},目标站={},taskWriteIdx={},currentTaskNo={},currentTargetStaNo={}",
                    command.getTaskNo(),
                    command.getStationId(),
                    command.getTargetStaNo(),
                    taskWriteIdx,
                    statusEntity == null ? null : statusEntity.getTaskNo(),
                    statusEntity == null ? null : statusEntity.getTargetStaNo());
            commandResponse.setResult(true);
            commandResponse.setMessage("命令重复,已跳过下发");
            return commandResponse;
        }
        Integer duplicateSlotIdx = findDuplicateTaskAreaSlot(stationIdx, command);
        if (duplicateSlotIdx != null) {
            log.info("输送线命令重复,已跳过任务写入区重复命令。任务号={},站点号={},目标站={},taskWriteIdx={},duplicateSlotIdx={}",
                    command.getTaskNo(),
                    command.getStationId(),
                    command.getTargetStaNo(),
                    taskWriteIdx,
                    duplicateSlotIdx);
            commandResponse.setResult(true);
            commandResponse.setMessage("任务区已有相同命令,已跳过下发");
            return commandResponse;
        }
        int useTaskWriteIdx = getTaskWriteIdx(stationIdx, taskWriteIdx);
        if (useTaskWriteIdx == -1) {
            commandResponse.setMessage("命令下发超时,无法找到可用下发区域");
@@ -295,6 +270,44 @@
        log.info("写入输送线命令成功。任务号={},站点数据={}", command.getTaskNo(), JSON.toJSON(command));
        commandResponse.setResult(true);
        return commandResponse;
    }
    @Override
    public CommandResponse clearTaskBufferSlot(Integer deviceNo, Integer stationId, Integer slotIdx) {
        CommandResponse commandResponse = new CommandResponse(false);
        if (stationId == null) {
            commandResponse.setMessage("站点号为空");
            return commandResponse;
        }
        if (slotIdx == null || slotIdx <= 0 || slotIdx > TASK_AREA_SLOT_COUNT) {
            commandResponse.setMessage("缓存区槽位无效");
            return commandResponse;
        }
        getStatus(deviceNo);
        int stationIdx = findIndex(stationId);
        if (stationIdx < 0) {
            commandResponse.setMessage("未找到站点状态");
            return commandResponse;
        }
        int slotBaseOffset = stationIdx * TASK_AREA_LENGTH + slotIdx * TASK_AREA_SLOT_SIZE;
        CommandResponse clearTaskNo = sendOriginCommand("DB13." + slotBaseOffset, new short[]{0, 0});
        if (clearTaskNo == null || !Boolean.TRUE.equals(clearTaskNo.getResult())) {
            commandResponse.setMessage(clearTaskNo == null ? "清空任务号失败" : clearTaskNo.getMessage());
            return commandResponse;
        }
        CommandResponse clearTarget = sendOriginCommand("DB13." + (slotBaseOffset + 6), new short[]{0});
        if (clearTarget == null || !Boolean.TRUE.equals(clearTarget.getResult())) {
            commandResponse.setMessage(clearTarget == null ? "清空目标站失败" : clearTarget.getMessage());
            return commandResponse;
        }
        commandResponse.setResult(true);
        commandResponse.setMessage("缓存区槽位清理成功");
        return commandResponse;
    }
@@ -359,53 +372,38 @@
        return useIdx;
    }
    private boolean isDuplicateStationCommand(ZyStationStatusEntity statusEntity, StationCommand command) {
        if (statusEntity == null || command == null) {
            return false;
        }
        return command.getTaskNo() != null
                && command.getTargetStaNo() != null
                && command.getTaskNo().equals(statusEntity.getTaskNo())
                && command.getTargetStaNo().equals(statusEntity.getTargetStaNo());
    }
    private Integer findDuplicateTaskAreaSlot(int stationIdx, StationCommand command) {
        if (stationIdx < 0 || command == null || command.getTaskNo() == null || command.getTargetStaNo() == null) {
    private byte[] readTaskBufferRaw() {
        int totalLength = statusList == null ? 0 : statusList.size() * TASK_AREA_LENGTH;
        if (totalLength <= 0) {
            return null;
        }
        OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13." + (stationIdx * TASK_AREA_LENGTH), (short) TASK_AREA_LENGTH);
        if (!resultTask.IsSuccess || resultTask.Content == null) {
        OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13.0", (short) totalLength);
        if (!resultTask.IsSuccess || resultTask.Content == null || resultTask.Content.length < totalLength) {
            return null;
        }
        for (int slotIdx = 1; slotIdx <= TASK_AREA_SLOT_COUNT; slotIdx++) {
            int offset = slotIdx * TASK_AREA_SLOT_SIZE;
            int taskNo = siemensNet.getByteTransform().TransInt32(resultTask.Content, offset);
            int targetPoint = siemensNet.getByteTransform().TransInt16(resultTask.Content, offset + 6);
            if (command.getTaskNo().equals(taskNo)
                    && command.getTargetStaNo().equals(targetPoint)) {
                return slotIdx;
            }
        }
        return null;
        return resultTask.Content;
    }
    private void fillTaskBufferStatus(int stationIdx, ZyStationStatusEntity statusEntity) {
        if (statusEntity == null || stationIdx < 0) {
    private void fillTaskBufferStatus(byte[] taskBufferRaw, int stationIdx, ZyStationStatusEntity statusEntity) {
        if (statusEntity == null || stationIdx < 0 || taskBufferRaw == null) {
            statusEntity.setTaskBufferItems(new ArrayList<>());
            return;
        }
        List<StationTaskBufferItem> itemList = new ArrayList<>();
        OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13." + (stationIdx * TASK_AREA_LENGTH), (short) TASK_AREA_LENGTH);
        if (resultTask.IsSuccess && resultTask.Content != null) {
            for (int slotIdx = 1; slotIdx <= TASK_AREA_SLOT_COUNT; slotIdx++) {
                int offset = slotIdx * TASK_AREA_SLOT_SIZE;
                int taskNo = siemensNet.getByteTransform().TransInt32(resultTask.Content, offset);
                int targetPoint = siemensNet.getByteTransform().TransInt16(resultTask.Content, offset + 6);
                StationTaskBufferItem item = new StationTaskBufferItem();
                item.setSlotIdx(slotIdx);
                item.setTaskNo(taskNo);
                item.setTargetStaNo(targetPoint);
                itemList.add(item);
            }
        int stationOffset = stationIdx * TASK_AREA_LENGTH;
        if (stationOffset + TASK_AREA_LENGTH > taskBufferRaw.length) {
            statusEntity.setTaskBufferItems(itemList);
            return;
        }
        for (int slotIdx = 1; slotIdx <= TASK_AREA_SLOT_COUNT; slotIdx++) {
            int offset = stationOffset + (slotIdx * TASK_AREA_SLOT_SIZE);
            int taskNo = siemensNet.getByteTransform().TransInt32(taskBufferRaw, offset);
            int targetPoint = siemensNet.getByteTransform().TransInt16(taskBufferRaw, offset + 6);
            StationTaskBufferItem item = new StationTaskBufferItem();
            item.setSlotIdx(slotIdx);
            item.setTaskNo(taskNo);
            item.setTargetStaNo(targetPoint);
            itemList.add(item);
        }
        statusEntity.setTaskBufferItems(itemList);
    }