package com.zy.acs.manager.core.scheduler; import com.alibaba.fastjson.JSON; import com.zy.acs.common.utils.Utils; import com.zy.acs.manager.common.domain.WebsocketDto; import com.zy.acs.manager.core.service.astart.DynamicNodeType; import com.zy.acs.manager.core.service.astart.MapDataDispatcher; import com.zy.acs.manager.core.service.astart.domain.DynamicNode; import com.zy.acs.manager.manager.entity.Code; import com.zy.acs.manager.manager.service.CodeService; import com.zy.acs.manager.manager.websocket.DigitalTwinWebSocket; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by vincent on 4/11/2024 */ @Component public class DigitalTwinDataScheduler { @Autowired private CodeService codeService; @Autowired private MapDataDispatcher mapDataDispatcher; @Scheduled(cron = "0/1 * * * * ? ") public void sync() { DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(null); String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(null); Map> dynamicAllot = new HashMap<>(); for (int i = 0; i < dynamicMatrix.length; i++) { for (int j = 0; j < dynamicMatrix[i].length; j++) { DynamicNode dynamicNode = dynamicMatrix[i][j]; String vehicle = dynamicNode.getVehicle(); if (DynamicNodeType.ACCESS.val.equals(vehicle) || DynamicNodeType.BLOCK.val.equals(vehicle)) { continue; } String codeData = codeMatrix[i][j]; Code code = codeService.selectByData(codeData); if (null == code) { continue; } if (dynamicAllot.containsKey(vehicle)) { dynamicAllot.get(vehicle).add(new CodePos(codeData, dynamicNode.getSerial()).synPosition(code.getX(), 0, code.getY())); } else { dynamicAllot.put(vehicle, Utils.singletonList(new CodePos(codeData, dynamicNode.getSerial()).synPosition(code.getX(), 0, code.getY()))); } } } for (Map.Entry> entry : dynamicAllot.entrySet()) { entry.getValue().sort(Comparator.comparingInt(CodePos::getSerial)); } DigitalTwinWebSocket.broadcast(JSON.toJSONString(new WebsocketDto<>("path", "", dynamicAllot.values()))); } @Data public static class CodePos { private String code; private int serial; private Number[] position; public CodePos synPosition(Number x, Number y, Number z) { position = new Number[] {x, y, z}; return this; } public CodePos(String code, int serial) { this.code = code; this.serial = serial; } } }