kaiyu
2020-09-30 7cbb7a70344309684ef7bdab4603b06b07e61efb
src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java
@@ -18,6 +18,7 @@
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -106,6 +107,7 @@
                channel.queueBind(queue, exchange, routingKey);
            }
            //消费消息,手动确认模式。
            channel.basicQos(1);//每次只消费一条数据
            channel.basicConsume(queue, false, new DefaultConsumer(channel) {
@@ -117,7 +119,7 @@
                    
                    //判断是否接收到客户端发送的mac,如果接收到则返回指定mac设备信息
                    if(mac!=null&&(!mac.equals(0)))
                        sendDeviceInfo(message);
                        sendDeviceInfo(message,deviceList);
                    
                    //手动确认
                    channel.basicAck(envelope.getDeliveryTag(), false);
@@ -145,7 +147,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");
        }
@@ -194,10 +195,11 @@
            * @Author: 陈凯裕
            * @Date: 2020/9/30
            */ 
    private void sendDeviceInfo(Map<String, Object> param) {
    private void sendDeviceInfo(Map<String, Object> param,List<Device> deviceList) {
        String deviceMac = (String) param.get("mac");
        if (mac.equals(deviceMac)) {
            try {
                Map<String,Object> sortMap = new LinkedHashMap<>();
                Map<String, Object> map = new HashMap<>();
                param.forEach((key, value) -> {
                    Sensor sensor = sensors.get(key);
@@ -209,7 +211,15 @@
                String mac = (String) param.get("mac");
                map.put("mac", mac);
                map.put("flag",1);
                sendMessage(JSON.toJSONString(map));
                for (Device device : deviceList) {
                    if(mac.equals(device.getMac())){
                        sortMap.put("名称",device.getName());
                        sortMap.put("地址",device.getAddress());
                        sortMap.putAll(map);
                        break;
                    }
                }
                sendMessage(JSON.toJSONString(sortMap));
            } catch (IOException e) {
                log.error("根据mac发送设备数据异常");
            }