package com.qianwen.smartman.modules.cps.controller; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.ImmutableMap; import io.swagger.annotations.Api; import java.util.Collections; import com.qianwen.smartman.common.websocket.entity.FmsArea; import com.qianwen.smartman.common.websocket.entity.FmsWorkstation; import com.qianwen.smartman.common.websocket.fms.FmsVisualDataResponseMessage; import com.qianwen.core.scanner.modular.stereotype.ApiResource; import com.qianwen.core.tool.api.R; import com.qianwen.core.tool.utils.SpringUtil; import com.qianwen.core.websocket.distribute.MessageDO; import com.qianwen.core.websocket.distribute.RedisMessageDistributor; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @ApiResource({"smis/test"}) @Api(value = "测试websocket", tags = {"测试websocket"}) @RestController public class WebSocketTestController { @RequestMapping({"/send-message"}) public R sendMessage() { RedisMessageDistributor messageDistributor = (RedisMessageDistributor) SpringUtil.getBean(RedisMessageDistributor.class); MessageDO messageOne = new MessageDO(); messageOne.setNeedBroadcast(Boolean.FALSE); JSONObject json = new JSONObject(); json.put("type", "realTimeData"); json.put("id", "111"); json.put("data", ImmutableMap.of("data", "hello realTimeData")); messageOne.setMessageText(json.toJSONString()); messageDistributor.distribute(messageOne); JSONObject typeTwo = new JSONObject(); typeTwo.put("type", "internalMessageData"); typeTwo.put("data", "hello internalMessageData"); MessageDO messageTwo = new MessageDO(); messageTwo.setNeedBroadcast(false); messageTwo.setMessageText(typeTwo.toJSONString()); messageDistributor.distribute(messageTwo); return R.success(""); } @PostMapping({"/send-fms-area"}) public R sendFmsMessage(@RequestBody FmsArea fmsArea) { FmsVisualDataResponseMessage responseMessage = new FmsVisualDataResponseMessage(); responseMessage.setFmsAreaList(Collections.singletonList(fmsArea)); responseMessage.setFmsAreaList(Collections.singletonList(fmsArea)); responseMessage.setFmsAreaList(Collections.singletonList(fmsArea)); responseMessage.setFmsAreaList(Collections.singletonList(fmsArea)); MessageDO messageDO = new MessageDO(); messageDO.setNeedBroadcast(true); messageDO.setMessageText(JSONObject.toJSONString(responseMessage)); RedisMessageDistributor messageDistributor = (RedisMessageDistributor) SpringUtil.getBean(RedisMessageDistributor.class); messageDistributor.distribute(messageDO); return R.success(""); } @PostMapping({"/send-fms-carry"}) public R sendFmsCarry(@RequestBody FmsWorkstation fmsCarry) { FmsVisualDataResponseMessage responseMessage = new FmsVisualDataResponseMessage(); responseMessage.setFmsCarryList(Collections.singletonList(fmsCarry)); MessageDO messageDO = new MessageDO(); messageDO.setNeedBroadcast(false); messageDO.setMessageText(JSONObject.toJSONString(responseMessage)); RedisMessageDistributor messageDistributor = (RedisMessageDistributor) SpringUtil.getBean(RedisMessageDistributor.class); messageDistributor.distribute(messageDO); return R.success(""); } @PostMapping({"/send-fms-manual"}) public R sendFmsManual(@RequestBody FmsWorkstation station) { FmsVisualDataResponseMessage responseMessage = new FmsVisualDataResponseMessage(); responseMessage.setFmsManualStationList(Collections.singletonList(station)); MessageDO messageDO = new MessageDO(); messageDO.setNeedBroadcast(false); messageDO.setMessageText(JSONObject.toJSONString(responseMessage)); RedisMessageDistributor messageDistributor = (RedisMessageDistributor) SpringUtil.getBean(RedisMessageDistributor.class); messageDistributor.distribute(messageDO); return R.success(""); } @PostMapping({"/send-fms-station"}) public R sendFmsStation(@RequestBody FmsWorkstation workstation) { FmsVisualDataResponseMessage responseMessage = new FmsVisualDataResponseMessage(); responseMessage.setFmsWorkstationList(Collections.singletonList(workstation)); MessageDO messageDO = new MessageDO(); messageDO.setNeedBroadcast(false); messageDO.setMessageText(JSONObject.toJSONString(responseMessage)); RedisMessageDistributor messageDistributor = (RedisMessageDistributor) SpringUtil.getBean(RedisMessageDistributor.class); messageDistributor.distribute(messageDO); return R.success(""); } }