kaiyu
2020-09-30 37e2672f7c9d59621672444b03d3713580dc1364
src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java
@@ -25,6 +25,13 @@
@Slf4j
@ServerEndpoint("/web/testWebSocket/{param}")
@Component
/**
* @Description: 用于测试的websocket
        * @Param:
        * @return:
        * @Author: 下雨听风
        * @Date: 2020/9/30
        */
public class BSTestWebsocketServer {
    public static DeviceService deviceService;
@@ -99,16 +106,23 @@
                channel.queueBind(queue, exchange, routingKey);
            }
            //消费消息,手动确认模式。
            channel.basicQos(1);//每次只消费一条数据
            channel.basicConsume(queue, false, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //对从MQ中取出的数据做转换,并且发送风速到客户端
                    Map message = (Map) JSON.parse((String) JSON.parse(new String(body)));
                    sendWindInfo(message);
                    //判断是否接收到客户端发送的mac,如果接收到则返回指定mac设备信息
                    if(mac!=null&&(!mac.equals(0)))
                        sendDeviceInfo(message);
                        sendDeviceInfo(message,deviceList);
                    //手动确认
                    channel.basicAck(envelope.getDeliveryTag(), false);
                    //判断socket是否已经断开
                    if (!webSocketMap.containsKey(accountId)) {
                        RabbitMQUtils.closeConnectionChannel(connection, channel);
@@ -132,7 +146,6 @@
    @OnMessage
    public void onMessage(String message, Session session) {
        if (!ObjectUtils.isEmpty(message)) {
            System.out.println(message);
            Map<String, Object> map = JSON.parseObject(message);
            this.mac = (String) map.get("mac");
        }
@@ -154,7 +167,13 @@
        }
    }
    //发送风向标数据
    /**
    * @Description: 从客户端连接到socket就一直开始发送风向数据
            * @Param: [param]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2020/9/30
            */
    private void sendWindInfo(Map<String, Object> param) {
        try {
            Map<String, Object> map = new HashMap<>();
@@ -168,8 +187,14 @@
        }
    }
    //根据mac发送device数据
    private void sendDeviceInfo(Map<String, Object> param) {
    /**
    * @Description: 接收前端mac以及accountid,传送前端指定mac号的设备信息
            * @Param: [param]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2020/9/30
            */
    private void sendDeviceInfo(Map<String, Object> param,List<Device> deviceList) {
        String deviceMac = (String) param.get("mac");
        if (mac.equals(deviceMac)) {
            try {
@@ -183,6 +208,14 @@
                });
                String mac = (String) param.get("mac");
                map.put("mac", mac);
                map.put("flag",1);
                for (Device device : deviceList) {
                    if(mac.equals(device.getMac())){
                        map.put("名称",device.getName());
                        map.put("地址",device.getAddress());
                        break;
                    }
                }
                sendMessage(JSON.toJSONString(map));
            } catch (IOException e) {
                log.error("根据mac发送设备数据异常");