diff --git a/src/main/java/club/joylink/xiannccda/ats/message/convertor/DeviceStatusConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/convertor/DeviceStatusConvertor.java index 3122d7c..f70d276 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/convertor/DeviceStatusConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/convertor/DeviceStatusConvertor.java @@ -4,6 +4,7 @@ import club.joylink.xiannccda.ats.message.MessageData; import club.joylink.xiannccda.ats.message.line3.device.DeviceStatus; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.dto.protos.DeviceStatusProto; +import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.GeneratedMessageV3; import java.lang.reflect.Array; @@ -112,6 +113,15 @@ public class DeviceStatusConvertor { return convert(deviceStatusEnum, statusBitMap); } + public static void fillField(GeneratedMessageV3.Builder build, String attrName, Object vals) { + Optional fd = build.getDescriptorForType().getFields().stream().filter(d -> StringUtils.equals(d.getName(), attrName)).findFirst(); + if (fd.isEmpty()) { + throw new RuntimeException(String.format("未找到对应的属性 build:[%s] attrName:[%s],val:[%s]", build.getClass().getName(), attrName, vals)); + } + FieldDescriptor ffdd = fd.get(); + build.setField(ffdd, vals); + } + public static void copyAttr(GeneratedMessageV3.Builder build, String attrName, Object... vals) { Class targetClass = build.getClass(); Optional optional = Arrays.stream(targetClass.getMethods()).filter(d -> StringUtils.startsWith(d.getName(), "set") && StringUtils.endsWithIgnoreCase(d.getName(), attrName)).findFirst(); diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusBitmapResponse.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusBitmapResponse.java index ce71da4..f5de219 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusBitmapResponse.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusBitmapResponse.java @@ -72,7 +72,8 @@ public class DeviceStatusBitmapResponse extends MessageResponse { DeviceType dt = deviceTypeEntity.getType(); for (DeviceEntity deviceEntity : deviceTypeEntity.deviceList) { GeneratedMessageV3.Builder builder = DeviceStatusConvertor.convert(dt, deviceEntity.status); - DeviceStatusConvertor.copyAttr(builder, "id", deviceEntity.getDevName()); + + DeviceStatusConvertor.fillField(builder, "id", deviceEntity.devName); DeviceStatusConvertor.convertForTrack(deviceEntity.getStatus(), builder); DeviceStatusConvertor.convertForPlatform(deviceEntity.getStatus(), (Objects.isNull(deviceEntity.getSpare()) ? 0 : deviceEntity.getSpare()), builder); DeviceStatusConvertor.convertForSwitch(Objects.isNull(deviceEntity.getSpare()) ? 0 : deviceEntity.getSpare(), builder); diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusChangeResponse.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusChangeResponse.java index d8ef8a1..b44872a 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusChangeResponse.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/DeviceStatusChangeResponse.java @@ -5,14 +5,18 @@ import club.joylink.xiannccda.ats.message.MessageResponse; import club.joylink.xiannccda.ats.message.convertor.DeviceStatusConvertor; import club.joylink.xiannccda.ats.message.line3.device.DeviceStatus; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import club.joylink.xiannccda.dto.protos.DeviceStatusProto; import com.google.common.collect.Lists; +import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.GeneratedMessageV3.Builder; import io.netty.buffer.ByteBuf; import java.util.List; import java.util.Objects; +import java.util.Optional; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; /** * 2.7.5 设备状态变化消息 @@ -79,10 +83,27 @@ public class DeviceStatusChangeResponse extends MessageResponse { this.spare = buf.readInt(); } + public static void main(String[] args) { + GeneratedMessageV3.Builder rtuBuild = DeviceStatusProto.Rtu.newBuilder(); + +// rtuBuild.setId("asd"); + rtuBuild.getDescriptorForType().getFields().forEach(d -> System.out.println(d.getName())); + Optional fd = rtuBuild.getDescriptorForType().getFields().stream().filter(d -> StringUtils.equals(d.getName(), "id")).findFirst(); + if (fd.isEmpty()) { + + } + FieldDescriptor ffdd = fd.get(); + rtuBuild.setField(ffdd, "asdfasdf"); + + System.out.println(rtuBuild.getAllFields()); + System.out.println(rtuBuild.getClass().getName()); + } + @Override public List generateProto() throws Exception { GeneratedMessageV3.Builder builder = DeviceStatusConvertor.convert(this.type, this.deviceStatus); - DeviceStatusConvertor.copyAttr(builder, "id", this.getDevName()); + + DeviceStatusConvertor.fillField(builder, "id", this.devName); DeviceStatusConvertor.convertForTrack(this.deviceStatus, builder); DeviceStatusConvertor.convertForPlatform(this.deviceStatus, Objects.isNull(this.spare) ? 0 : this.spare, builder); DeviceStatusConvertor.convertForSwitch(Objects.isNull(this.spare) ? 0 : this.spare, builder); diff --git a/src/main/java/club/joylink/xiannccda/controller/PublishedGiController.java b/src/main/java/club/joylink/xiannccda/controller/PublishedGiController.java index 53949c5..94e2481 100644 --- a/src/main/java/club/joylink/xiannccda/controller/PublishedGiController.java +++ b/src/main/java/club/joylink/xiannccda/controller/PublishedGiController.java @@ -1,6 +1,7 @@ package club.joylink.xiannccda.controller; import club.joylink.xiannccda.dto.PublishedGIDTO; +import club.joylink.xiannccda.dto.PublishedGIDTO.LineType; import club.joylink.xiannccda.dto.PublishedGIQueryDTO; import club.joylink.xiannccda.entity.PublishedGi; import club.joylink.xiannccda.repository.IPublishedGiRepository; @@ -17,9 +18,7 @@ import java.security.Principal; import java.util.List; /** - *

* 发布图形界面 前端控制器 - *

* * @author walker-sheng * @since 2023-06-08 @@ -29,65 +28,76 @@ import java.util.List; @Tag(name = "发布图形管理接口") public class PublishedGiController { - final IPublishedGiRepository publishedGiRepository; + final IPublishedGiRepository publishedGiRepository; - final PublishedGiService publishedGiService; + final PublishedGiService publishedGiService; - public PublishedGiController(IPublishedGiRepository publishedGiRepository, PublishedGiService publishedGiService) { - this.publishedGiRepository = publishedGiRepository; - this.publishedGiService = publishedGiService; - } + public PublishedGiController( + IPublishedGiRepository publishedGiRepository, PublishedGiService publishedGiService) { + this.publishedGiRepository = publishedGiRepository; + this.publishedGiService = publishedGiService; + } - @GetMapping("/paging") - @SecurityRequirement(name = "jwt") - @Operation(summary = "查询分页发布图形数据") - @ApiResponse(description = "查询发布图形数据") - public Page paging(PublishedGIQueryDTO query) { - return publishedGiRepository.paging(query); - } + @GetMapping("/paging") + @SecurityRequirement(name = "jwt") + @Operation(summary = "查询分页发布图形数据") + @ApiResponse(description = "查询发布图形数据") + public Page paging(PublishedGIQueryDTO query) { + return publishedGiRepository.paging(query); + } - @GetMapping("/list") - @SecurityRequirement(name = "jwt") - @Operation(summary = "查询发布图形数据列表") - @ApiResponse(description = "查询发布图形数据列表") - public List list(PublishedGIQueryDTO query) { - return publishedGiRepository.list(query); - } + @GetMapping("/list") + @SecurityRequirement(name = "jwt") + @Operation(summary = "查询发布图形数据列表") + @ApiResponse(description = "查询发布图形数据列表") + public List list(PublishedGIQueryDTO query) { + return publishedGiRepository.list(query); + } - @GetMapping("/{id}") - @SecurityRequirement(name = "jwt") - @Operation(summary = "根据主键获取发布图形数据详情") - @ApiResponse(description = "根据主键获取发布图形数据详情") - public PublishedGi queryById(@PathVariable Integer id) { - return publishedGiRepository.getById(id); - } + @GetMapping("/{id}") + @SecurityRequirement(name = "jwt") + @Operation(summary = "根据主键获取发布图形数据详情") + @ApiResponse(description = "根据主键获取发布图形数据详情") + public PublishedGi queryById(@PathVariable Integer id) { + return publishedGiRepository.getById(id); + } - @GetMapping("/{type}/{lineId}") - @SecurityRequirement(name = "jwt") - @Operation(summary = "根据线路号和类型获取发布图形数据详情") - @ApiResponse(description = "根据线路号和类型获取发布图形数据详情") - public PublishedGi queryByTypeAndLineId(@PathVariable String type, @PathVariable Integer lineId) { - PublishedGIQueryDTO dto = new PublishedGIQueryDTO(); - dto.setType(type); - dto.setLineId(lineId); - return publishedGiRepository.queryByDto(dto); - } + @GetMapping("/{type}/{lineId}") + @SecurityRequirement(name = "jwt") + @Operation(summary = "根据线路号和类型获取发布图形数据详情") + @ApiResponse(description = "根据线路号和类型获取发布图形数据详情") + public PublishedGi queryByTypeAndLineId(@PathVariable String type, @PathVariable Integer lineId) { + PublishedGIQueryDTO dto = new PublishedGIQueryDTO(); + dto.setType(type); + dto.setLineId(lineId); + return publishedGiRepository.queryByDto(dto); + } + @PostMapping("/publish") + @SecurityRequirement(name = "jwt") + @Operation(summary = "发布草稿图形数据") + @ApiResponse(description = "发布草稿图形数据") + public PublishedGi publish( + Principal user, + @RequestBody @Validated(PublishedGIDTO.Publish.class) PublishedGIDTO publishedDto) { + return publishedGiService.publish(user, publishedDto); + } - @PostMapping("/publish") - @SecurityRequirement(name = "jwt") - @Operation(summary = "发布草稿图形数据") - @ApiResponse(description = "发布草稿图形数据") - public PublishedGi publish(Principal user, @RequestBody @Validated(PublishedGIDTO.Publish.class) PublishedGIDTO publishedDto) { - return publishedGiService.publish(user, publishedDto); - } - - @DeleteMapping("/{id}") - @SecurityRequirement(name = "jwt") - @Operation(summary = "删除发布图形数据") - @ApiResponse(description = "删除发布图形数据") - public boolean deleteById(@PathVariable Integer id) { - return publishedGiRepository.removeById(id); - } + @DeleteMapping("/{id}") + @SecurityRequirement(name = "jwt") + @Operation(summary = "删除发布图形数据") + @ApiResponse(description = "删除发布图形数据") + public boolean deleteById(@PathVariable Integer id) { + return publishedGiRepository.removeById(id); + } + @GetMapping("/lineNetwork") + @SecurityRequirement(name = "jwt") + @Operation(summary = "获取已发布的现网数据") + @ApiResponse(description = "获取已发布的现网数据") + public PublishedGi queryLineNetwork() { + PublishedGIQueryDTO query = new PublishedGIQueryDTO(); + query.setType(LineType.LineNetwork.name()); + return publishedGiRepository.queryByDto(query); + } } diff --git a/src/main/java/club/joylink/xiannccda/dto/PublishedGIDTO.java b/src/main/java/club/joylink/xiannccda/dto/PublishedGIDTO.java index af6eda8..1c3b850 100644 --- a/src/main/java/club/joylink/xiannccda/dto/PublishedGIDTO.java +++ b/src/main/java/club/joylink/xiannccda/dto/PublishedGIDTO.java @@ -14,14 +14,18 @@ public class PublishedGIDTO { private String name; @Schema(description = "关联的线路号") - @NotNull(message = "关联线路不能为空", groups = { Publish.class }) private Integer lineId; @Schema(description = "草稿数据主键") - @NotNull(message = "请选择草稿数据", groups = { Publish.class }) + @NotNull( + message = "请选择草稿数据", + groups = {Publish.class}) private Integer draftingId; - public interface Publish { + public interface Publish {} + public enum LineType { + Line, + LineNetwork } } diff --git a/src/main/java/club/joylink/xiannccda/entity/PublishedGi.java b/src/main/java/club/joylink/xiannccda/entity/PublishedGi.java index 8d41acc..36e995b 100644 --- a/src/main/java/club/joylink/xiannccda/entity/PublishedGi.java +++ b/src/main/java/club/joylink/xiannccda/entity/PublishedGi.java @@ -1,5 +1,7 @@ package club.joylink.xiannccda.entity; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.time.LocalDateTime; import io.swagger.v3.oas.annotations.media.Schema; @@ -8,10 +10,6 @@ import lombok.Setter; import lombok.experimental.Accessors; /** - *

- * - *

- * * @author walker-sheng * @since 2023-06-08 */ @@ -22,6 +20,7 @@ import lombok.experimental.Accessors; @Schema(name = "PublishedGi", description = "发布的图形界面数据") public class PublishedGi { + @TableId(value = "id", type = IdType.AUTO) @Schema(description = "id") private Integer id; diff --git a/src/main/java/club/joylink/xiannccda/mock/message/NccMockData.java b/src/main/java/club/joylink/xiannccda/mock/message/NccMockData.java index 823822e..b05f9f1 100644 --- a/src/main/java/club/joylink/xiannccda/mock/message/NccMockData.java +++ b/src/main/java/club/joylink/xiannccda/mock/message/NccMockData.java @@ -44,8 +44,8 @@ public class NccMockData { public static final String MOCK_RECEIVE_TIME = "mock_receive_time"; - private static final String MSG_TYPE = "msg_type"; - private static final String ACTION_TYPE = "ACTION_type"; + public static final String MSG_TYPE = "msg_type"; + public static final String ACTION_TYPE = "action_type"; public enum MsgTypeEnum { REAL_TIME, UNREAL_TIME diff --git a/src/main/java/club/joylink/xiannccda/mock/message/NccMockDataService.java b/src/main/java/club/joylink/xiannccda/mock/message/NccMockDataService.java index da54ba6..781d557 100644 --- a/src/main/java/club/joylink/xiannccda/mock/message/NccMockDataService.java +++ b/src/main/java/club/joylink/xiannccda/mock/message/NccMockDataService.java @@ -32,13 +32,18 @@ public class NccMockDataService { private static boolean FINISH = false; - private final static int PAGE_SIZE = 200; + private static int PAGE_SIZE = 200; public void reset() { LAST_ID = 0L; FINISH = false; } + public void reset(int pageSize) { + this.reset(); + PAGE_SIZE = 1; + } + public boolean finish() { return FINISH; } @@ -94,6 +99,8 @@ public class NccMockDataService { private List findData() { QueryWrapper queryWrapper = Wrappers.query(); queryWrapper.gt(NccMockData.ID, LAST_ID); + queryWrapper.eq(NccMockData.MSG_TYPE, MsgTypeEnum.REAL_TIME.name()); + queryWrapper.eq(NccMockData.ACTION_TYPE, ActionTypeEnum.UPDATES.name()); queryWrapper.last(String.format(" limit %s", PAGE_SIZE)); List list = this.nccMockDataRepository.list(queryWrapper); return list; diff --git a/src/main/java/club/joylink/xiannccda/mock/message/ws/MockDeviceMessageServer.java b/src/main/java/club/joylink/xiannccda/mock/message/ws/MockDeviceMessageServer.java index 47dab29..bab82a2 100644 --- a/src/main/java/club/joylink/xiannccda/mock/message/ws/MockDeviceMessageServer.java +++ b/src/main/java/club/joylink/xiannccda/mock/message/ws/MockDeviceMessageServer.java @@ -1,15 +1,41 @@ package club.joylink.xiannccda.mock.message.ws; +import club.joylink.xiannccda.ats.message.MessageData; +import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository; +import club.joylink.xiannccda.dto.protos.WsMessageProto.WsLineMessage; +import club.joylink.xiannccda.dto.protos.WsMessageProto.WsLineNetMessage; +import club.joylink.xiannccda.mock.message.NccMockDataService; import club.joylink.xiannccda.ws.IMessageServer; import club.joylink.xiannccda.ws.TopicMessage; import club.joylink.xiannccda.ws.WsMessageServerManager; import com.alibaba.fastjson2.JSON; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import com.google.protobuf.Descriptors.FieldDescriptor.Type; +import com.google.protobuf.GeneratedMessageV3; +import com.google.protobuf.GeneratedMessageV3.Builder; +import com.google.protobuf.Message; +import jakarta.annotation.PostConstruct; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import org.springframework.util.AntPathMatcher; +import org.springframework.util.CollectionUtils; +import org.springframework.util.PropertyPlaceholderHelper; @Component @RequiredArgsConstructor @@ -18,30 +44,121 @@ public class MockDeviceMessageServer implements IMessageServer { private final WsMessageServerManager serverManager; + private final NccMockDataService nccMockDataService; + + private final static String SUBSCRIPTION_PATH = "/queue/line/{lineId}"; + + private final Set lineIdSet = Sets.newHashSet(); + + private static PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper( + "{", "}", ":", false); + + + @PostConstruct + public void init() { + + List dataList = this.nccMockDataService.loadALLData(); + try { + for (MessageData messageData : dataList) { + List builders = messageData.generateProto(); + DeviceStatusDataRepository.addDeviceStatusDataList("3", builders); + } + } catch (Exception e) { + e.printStackTrace(); + + } + + this.serverManager.registerMessageServer(this); + this.nccMockDataService.reset(1); + this.circleQueryMockData(); + } + + static final ScheduledExecutorService CIRCLE_QUERY_THREAD = Executors.newSingleThreadScheduledExecutor(); + + private void circleQueryMockData() { + CIRCLE_QUERY_THREAD.scheduleAtFixedRate(() -> { + if (lineIdSet.isEmpty()) { + return; + } + List messageDataList = this.nccMockDataService.getMessageData(); + if (CollectionUtils.isEmpty(messageDataList)) { + System.out.println("没有新的数据"); + } + for (MessageData messageData : messageDataList) { + try { + DeviceStatusDataRepository.addDeviceStatusDataList("3", messageData.generateProto()); + } catch (Exception e) { + e.printStackTrace(); + } + } + }, this.getInterval(), this.getInterval(), TimeUnit.MILLISECONDS); + } @Override public String getDestinationPattern() { -// return String.format("/queue/line/%s", this.lineId); - return null; + return SUBSCRIPTION_PATH; } @Override public Object onSubscription(String destination, Map paramMap) { String lineId = paramMap.get("lineId"); + lineIdSet.add(lineId); + log.info("线路lineId={}订阅,发布全量数据", lineId); -// byte[] bytes = DeviceStatusDataRepository.collectLineAllMessage(lineId).toByteArray(); -// System.out.println(JSON.toJSONString(bytes)); -// return bytes; - return null; + DeviceStatusDataRepository.getDeviceStatusData(lineId).getAllDeviceMap(); + WsLineMessage.Builder builder = WsLineMessage.newBuilder(); + fillBuilderFunction(fun -> { + Map builderMap = DeviceStatusDataRepository.getDeviceStatusData(lineId).getAllDeviceMap().get(fun); + if (Objects.isNull(builderMap)) { + return Map.of(); + } + Map messageMap = Maps.newHashMap(); + builderMap.forEach((k, v) -> messageMap.put(k, v.build())); + return messageMap; + }, builder); + WsLineMessage buildMsg = builder.build(); + return buildMsg.toByteArray(); + +// return new TopicMessage(destination, buildMsg); } @Override public int getInterval() { - return 500; + return 1500; } @Override public List onTick() { - return null; + if (this.lineIdSet.isEmpty()) { + return Collections.emptyList(); + } + Properties properties = new Properties(); + List messages = Lists.newArrayList(); + for (String lineId : this.lineIdSet) { + WsLineMessage.Builder msg = WsLineMessage.newBuilder(); + fillBuilderFunction((field) -> DeviceStatusDataRepository.getDeviceStatusData(lineId).getStatusVOMap().get(field), msg); + DeviceStatusDataOperate.clearStatusVOMap(DeviceStatusDataRepository.getDeviceStatusData(lineId)); + properties.put("lineId", lineId); + String destination = helper.replacePlaceholders(SUBSCRIPTION_PATH, properties); + messages.add(new TopicMessage(destination, msg.build().toByteArray())); + properties.clear(); + + } + return messages; + } + + + public void fillBuilderFunction(Function> compareFun, GeneratedMessageV3.Builder builder) { + // 消息体字段列表 + builder.getDescriptorForType().getFields().stream() + .filter(f -> f.getType().equals(Type.MESSAGE)) + .forEach( + field -> { + String fieldType = field.getMessageType().getName(); // 字段类型 + Map allDeviceMap = compareFun.apply(fieldType); + if (!CollectionUtils.isEmpty(allDeviceMap)) { + builder.setField(field, new ArrayList<>(allDeviceMap.values())); + } + }); } } diff --git a/src/main/java/club/joylink/xiannccda/mock/message/ws/MockLoadDataService.java b/src/main/java/club/joylink/xiannccda/mock/message/ws/MockLoadDataService.java deleted file mode 100644 index 3e2284e..0000000 --- a/src/main/java/club/joylink/xiannccda/mock/message/ws/MockLoadDataService.java +++ /dev/null @@ -1,27 +0,0 @@ -package club.joylink.xiannccda.mock.message.ws; - -import club.joylink.xiannccda.ats.message.MessageData; -import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository; -import club.joylink.xiannccda.mock.message.NccMockDataService; -import com.google.protobuf.GeneratedMessageV3; -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.stereotype.Component; - -@Component -@RequiredArgsConstructor -public class MockLoadDataService implements ApplicationRunner { - - private final NccMockDataService dataService; - - @Override - public void run(ApplicationArguments args) throws Exception { - List dataList = this.dataService.loadALLData(); - for (MessageData messageData : dataList) { - List builders = messageData.generateProto(); - DeviceStatusDataRepository.addDeviceStatusDataList("3", builders); - } - } -} diff --git a/src/main/java/club/joylink/xiannccda/repository/IPublishedGiRepository.java b/src/main/java/club/joylink/xiannccda/repository/IPublishedGiRepository.java index fad895e..e9c5e4f 100644 --- a/src/main/java/club/joylink/xiannccda/repository/IPublishedGiRepository.java +++ b/src/main/java/club/joylink/xiannccda/repository/IPublishedGiRepository.java @@ -8,18 +8,16 @@ import com.baomidou.mybatisplus.extension.service.IService; import java.util.List; /** - *

- * 服务类 - *

+ * 服务类 * * @author walker-sheng * @since 2023-06-08 */ public interface IPublishedGiRepository extends IService { - Page paging(PublishedGIQueryDTO query); + Page paging(PublishedGIQueryDTO query); - List list(PublishedGIQueryDTO query); + List list(PublishedGIQueryDTO query); - PublishedGi queryByDto(PublishedGIQueryDTO query); + PublishedGi queryByDto(PublishedGIQueryDTO query); } diff --git a/src/main/java/club/joylink/xiannccda/repository/impl/PublishedGiRepository.java b/src/main/java/club/joylink/xiannccda/repository/impl/PublishedGiRepository.java index eb4d6a5..e9a4c83 100644 --- a/src/main/java/club/joylink/xiannccda/repository/impl/PublishedGiRepository.java +++ b/src/main/java/club/joylink/xiannccda/repository/impl/PublishedGiRepository.java @@ -1,5 +1,6 @@ package club.joylink.xiannccda.repository.impl; +import club.joylink.xiannccda.dto.PublishedGIDTO.LineType; import club.joylink.xiannccda.dto.PublishedGIQueryDTO; import club.joylink.xiannccda.entity.PublishedGi; import club.joylink.xiannccda.mapper.PublishedGiMapper; @@ -14,47 +15,46 @@ import org.springframework.stereotype.Service; import java.util.List; /** - *

- * 服务实现类 - *

+ * 服务实现类 * * @author walker-sheng * @since 2023-06-08 */ @Service -public class PublishedGiRepository extends ServiceImpl implements IPublishedGiRepository { +public class PublishedGiRepository extends ServiceImpl + implements IPublishedGiRepository { - @Override - public Page paging(PublishedGIQueryDTO query) { - LambdaQueryWrapper wrapper = getQueryWrapper(query); - wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto")); - return page(query, wrapper); - } + @Override + public Page paging(PublishedGIQueryDTO query) { + LambdaQueryWrapper wrapper = getQueryWrapper(query); + wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto")); + return page(query, wrapper); + } - @Override - public List list(PublishedGIQueryDTO query) { - LambdaQueryWrapper wrapper = getQueryWrapper(query); - wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto")); - return list(wrapper); - } + @Override + public List list(PublishedGIQueryDTO query) { + LambdaQueryWrapper wrapper = getQueryWrapper(query); + wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto")); + return list(wrapper); + } - @Override - public PublishedGi queryByDto(PublishedGIQueryDTO query) { - LambdaQueryWrapper wrapper = getQueryWrapper(query); - return getOne(wrapper); - } + @Override + public PublishedGi queryByDto(PublishedGIQueryDTO query) { + LambdaQueryWrapper wrapper = getQueryWrapper(query); + return getOne(wrapper); + } - private static LambdaQueryWrapper getQueryWrapper(PublishedGIQueryDTO query) { - LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); - if (StringUtils.isNotEmpty(query.getName())) { - wrapper.like(PublishedGi::getName, query.getName()); - } - if (StringUtils.isNotEmpty(query.getType())) { - wrapper.eq(PublishedGi::getType, query.getType()); - } - if (query.getLineId() != null) { - wrapper.eq(PublishedGi::getLineId, query.getLineId()); - } - return wrapper; + private static LambdaQueryWrapper getQueryWrapper(PublishedGIQueryDTO query) { + LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); + if (StringUtils.isNotEmpty(query.getName())) { + wrapper.like(PublishedGi::getName, query.getName()); } + if (StringUtils.isNotEmpty(query.getType())) { + wrapper.eq(PublishedGi::getType, query.getType()); + } + if (query.getLineId() != null) { + wrapper.eq(PublishedGi::getLineId, query.getLineId()); + } + return wrapper; + } } diff --git a/src/main/java/club/joylink/xiannccda/service/PublishedGiService.java b/src/main/java/club/joylink/xiannccda/service/PublishedGiService.java index a99f7c8..64c99c0 100644 --- a/src/main/java/club/joylink/xiannccda/service/PublishedGiService.java +++ b/src/main/java/club/joylink/xiannccda/service/PublishedGiService.java @@ -1,6 +1,7 @@ package club.joylink.xiannccda.service; import club.joylink.xiannccda.dto.PublishedGIDTO; +import club.joylink.xiannccda.dto.PublishedGIDTO.LineType; import club.joylink.xiannccda.entity.Drafting; import club.joylink.xiannccda.entity.LineInfo; import club.joylink.xiannccda.entity.PublishedGi; @@ -10,6 +11,7 @@ import club.joylink.xiannccda.repository.ILineInfoRepository; import club.joylink.xiannccda.repository.IPublishedGiRepository; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import java.util.Objects; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,56 +22,69 @@ import java.time.LocalDateTime; @Service public class PublishedGiService { - final IPublishedGiRepository publishedGiRepository; + final IPublishedGiRepository publishedGiRepository; - final IDraftingRepository draftingRepository; + final IDraftingRepository draftingRepository; - final ILineInfoRepository lineInfoRepository; + final ILineInfoRepository lineInfoRepository; + public PublishedGiService( + IPublishedGiRepository publishedGiRepository, + IDraftingRepository draftingRepository, + ILineInfoRepository lineInfoRepository) { + this.publishedGiRepository = publishedGiRepository; + this.draftingRepository = draftingRepository; + this.lineInfoRepository = lineInfoRepository; + } - public PublishedGiService(IPublishedGiRepository publishedGiRepository, IDraftingRepository draftingRepository, ILineInfoRepository lineInfoRepository) { - this.publishedGiRepository = publishedGiRepository; - this.draftingRepository = draftingRepository; - this.lineInfoRepository = lineInfoRepository; + /** + * 发布草稿数据 + * + * @param user 用户 + * @param publishedDto 草稿数据 + * @return 发布后的实体数据 + */ + @Transactional(rollbackFor = Exception.class) + public PublishedGi publish(Principal user, PublishedGIDTO publishedDto) { + // 查询草稿信息 + Drafting drafting = draftingRepository.getById(publishedDto.getDraftingId()); + if (drafting == null) { + throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception( + String.format("草稿[%s]信息不存在", publishedDto.getDraftingId())); + } + if (drafting.getProto() == null) { + throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception( + String.format("草稿[%s]绘图数据信息为空", publishedDto.getDraftingId())); } - /** - * 发布草稿数据 - * @param user 用户 - * @param publishedDto 草稿数据 - * @return 发布后的实体数据 - */ - @Transactional(rollbackFor = Exception.class) - public PublishedGi publish(Principal user, PublishedGIDTO publishedDto) { - // 查询草稿信息 - Drafting drafting = draftingRepository.getById(publishedDto.getDraftingId()); - if (drafting == null) { - throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("草稿[%s]信息不存在", publishedDto.getDraftingId())); - } - if (drafting.getProto() == null) { - throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("草稿[%s]绘图数据信息为空", publishedDto.getDraftingId())); - } - // 查询线路信息是否存在 - LambdaQueryWrapper lineInfoWrapper = Wrappers.lambdaQuery(); - lineInfoWrapper.eq(LineInfo::getLineId, publishedDto.getLineId()); - LineInfo lineInfo = lineInfoRepository.getOne(lineInfoWrapper); - if (lineInfo == null) { - throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("线路[%s]信息不存在", publishedDto.getLineId())); - } - // 删除已发布的绘图数据 - LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); - String name = StringUtils.isNotEmpty(publishedDto.getName()) ? publishedDto.getName() : drafting.getName(); - wrapper.eq(PublishedGi::getType, drafting.getType()).eq(PublishedGi::getLineId, publishedDto.getLineId()); - publishedGiRepository.remove(wrapper); // 删除发布数据 - // 保存发布信息 - PublishedGi publishedGi = new PublishedGi(); - publishedGi.setName(name); - publishedGi.setType(drafting.getType()); - publishedGi.setLineId(lineInfo.getLineId()); - publishedGi.setProto(drafting.getProto()); - publishedGi.setUserId(Integer.valueOf(user.getName())); - publishedGi.setPublishAt(LocalDateTime.now()); - publishedGiRepository.save(publishedGi); - return publishedGi; + // 删除已发布的绘图数据 + LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); + wrapper.eq(PublishedGi::getType, drafting.getType()); + if (Objects.equals(drafting.getType(), LineType.Line.name())) { + // 查询线路信息是否存在 + LambdaQueryWrapper lineInfoWrapper = Wrappers.lambdaQuery(); + lineInfoWrapper.eq(LineInfo::getLineId, publishedDto.getLineId()); + LineInfo lineInfo = lineInfoRepository.getOne(lineInfoWrapper); + if (lineInfo == null) { + throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception( + String.format("线路[%s]信息不存在", publishedDto.getLineId())); + } + wrapper.eq(PublishedGi::getLineId, publishedDto.getLineId()); } + publishedGiRepository.remove(wrapper); // 删除发布数据 + // 保存发布信息 + PublishedGi publishedGi = new PublishedGi(); + String name = + StringUtils.isNotEmpty(publishedDto.getName()) + ? publishedDto.getName() + : drafting.getName(); + publishedGi.setName(name); + publishedGi.setType(drafting.getType()); + publishedGi.setLineId(publishedDto.getLineId()); + publishedGi.setProto(drafting.getProto()); + publishedGi.setUserId(Integer.valueOf(user.getName())); + publishedGi.setPublishAt(LocalDateTime.now()); + publishedGiRepository.save(publishedGi); + return publishedGi; + } } diff --git a/src/main/java/club/joylink/xiannccda/ws/WsMessageServerManager.java b/src/main/java/club/joylink/xiannccda/ws/WsMessageServerManager.java index e911cd0..3b2a006 100644 --- a/src/main/java/club/joylink/xiannccda/ws/WsMessageServerManager.java +++ b/src/main/java/club/joylink/xiannccda/ws/WsMessageServerManager.java @@ -16,7 +16,9 @@ import org.springframework.messaging.simp.stomp.StompHeaderAccessor; import org.springframework.messaging.support.MessageHeaderAccessor; import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; +import org.springframework.web.socket.messaging.SessionDisconnectEvent; import org.springframework.web.socket.messaging.SessionSubscribeEvent; +import org.springframework.web.socket.messaging.SessionUnsubscribeEvent; @Component @Slf4j @@ -80,4 +82,14 @@ public class WsMessageServerManager { } }); } + +/* @EventListener + public void disconnect(SessionDisconnectEvent event) { + System.out.println(event + "-----------dis"); + } + + @EventListener + public void unSubecribe(SessionUnsubscribeEvent event) { + System.out.println(event + "------- UnsubscribeEvent"); + }*/ } diff --git a/src/test/java/club/joylink/xiannccda/service/StompTest.java b/src/test/java/club/joylink/xiannccda/service/StompTest.java index 4bd9322..b3fc2c4 100644 --- a/src/test/java/club/joylink/xiannccda/service/StompTest.java +++ b/src/test/java/club/joylink/xiannccda/service/StompTest.java @@ -5,6 +5,7 @@ import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; @@ -17,7 +18,7 @@ import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.messaging.WebSocketStompClient; public class StompTest { - + @Test public void subscribe() { StandardWebSocketClient socketClient = new StandardWebSocketClient(); @@ -27,7 +28,7 @@ public class StompTest { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); StompHeaders headers1 = new StompHeaders(); headers1.set("Authorization", - "Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiNiIsImV4cCI6MTY4NzUwNzI0MiwiaWF0IjoxNjg3MjQ4MDQyfQ.SEMBj2lYxyy5ebLCLg6YKfOqi20cAOnEcEaLU9Xa_PAV7MJNY6iakXy0DmL2PshKGtsikEaEpDg0S353NuSmBBBthk2U14tc4aYvIGJ9GxlGWDx3vOI3DX4X0Q7ZROq921cxpWPPGi-KKSPMnp3dkzofIS8La77rNYynOp78dmFojeo6FwPGnaOT5ve_d2l72sN9KO5Bd_hjGxUk0SIphGBDjPWoQeSfPG-3tlIFxfNPeRifwgGjfmRG5Gg6VvQfLUsRR7yMwuxnDnvDuxRmQ-7cTx2p1QSo3nILyYMdLT5LUHuejWJteiMSz9SrFbyyqclyqlDRV-5jFdgqj1uHLQ"); + "Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiOSIsImV4cCI6MTY4NzkzODcwNCwiaWF0IjoxNjg3Njc5NTA0fQ.W__1N-IyXIJ4SIPsFviK-1q_0H9lqQsr0uI6VhdpZsPME63Aes8Fr23XItIlVqb14pgWVeVxTnRMKnG8gKzqFj8FTJs6c2jo5wDYX2UTBED3GPBIdzPTNETQATwh2vQNoItxyGcepVzToN4jmjtIScmtly3ppIXmCgYUNtwniWRz_5FAbDyy18cawd7-rr9ex37uahUewdohgrQFDOHDRzxC0M5AGTpfxKIiXHYs-rGdyfqhvhpoy26bM0TTMr8_EpssODLY8EvSLFfQgqtzhlXbrOQWuOjG7fQ4RQa1cKFHzFtQPosIkE3gsvyCRw2BCLERlqmGoAor-3RZ5Mqv_w"); CompletableFuture future = stompClient.connectAsync("ws://127.0.0.1:9081/ws-default", headers, headers1, handler); @@ -37,7 +38,7 @@ public class StompTest { } catch (Exception e) { e.printStackTrace(); } - stompSession.subscribe("/queue/lineNet", new StompFrameHandler() { + stompSession.subscribe("/queue/line/3", new StompFrameHandler() { @Override public Type getPayloadType(StompHeaders stompHeaders) { return byte[].class; @@ -46,6 +47,10 @@ public class StompTest { @Override public void handleFrame(StompHeaders stompHeaders, Object o) { String recv = null; + if (Objects.isNull(o)) { + System.out.println("接收数据为空"); + return; + } try { recv = new String((byte[]) o, "UTF-8"); } catch (UnsupportedEncodingException e) { diff --git a/xian-ncc-da-message b/xian-ncc-da-message index 3bd4f02..f42a98c 160000 --- a/xian-ncc-da-message +++ b/xian-ncc-da-message @@ -1 +1 @@ -Subproject commit 3bd4f023cd1810dd1ec9611c3714d626a08377d2 +Subproject commit f42a98cd93767b1fddec263181cb6bed53729665