kaiyu
2020-10-09 c04171420e9522422d110acc165bd8c608c592fb
优化响应速度,取消flag标记以及具体设备的mac。
1 files modified
36 ■■■■ changed files
src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java 36 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java
@@ -61,6 +61,7 @@
    private static Map<String, Sensor> sensors;
    @PostConstruct
    public void init() {
        sensors = new HashMap<>();
@@ -93,23 +94,25 @@
        paramMap.put("regionCode", regionCode);
        ParameterUtils.getRegionType4RegionCode(paramMap);
        List<Device> deviceList = deviceService.queryDevice(paramMap);
        Map<String,Device> deviceMap = new HashMap<>();
        try {
            Connection connection = RabbitMQUtils.getConnection();
            final Channel channel = connection.createChannel();
            //生成临时队列
            String queue = channel.queueDeclare().getQueue();
            //交换机与队列通过routingKey进行绑定
            String routingKey = "";
            for (Device d : deviceList) {
                deviceMap.put(d.getMac(),d);
                routingKey = orgId + "." + d.getMac();
                channel.queueBind(queue, exchange, routingKey);
            }
            //消费消息,手动确认模式。
            channel.basicQos(1);//每次只消费一条数据
            channel.basicQos(30);//预先读取数
            channel.basicConsume(queue, false, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
@@ -119,10 +122,10 @@
                    
                    //判断是否接收到客户端发送的mac,如果接收到则返回指定mac设备信息
                    if(mac!=null&&(!mac.equals(0)))
                        sendDeviceInfo(message,deviceList);
                        sendDeviceInfo(message,deviceMap);
                    
                    //手动确认
                    channel.basicAck(envelope.getDeliveryTag(), false);
                    channel.basicAck(envelope.getDeliveryTag(), true);
                    
                    //判断socket是否已经断开
                    if (!webSocketMap.containsKey(accountId)) {
@@ -195,30 +198,27 @@
            * @Author: 陈凯裕
            * @Date: 2020/9/30
            */ 
    private void sendDeviceInfo(Map<String, Object> param,List<Device> deviceList) {
    private void sendDeviceInfo(Map<String, Object> param,Map<String,Device> deviceMap) {
        String deviceMac = (String) param.get("mac");
        if (mac.equals(deviceMac)) {
            try {
                Map<String,Object> sortMap = new LinkedHashMap<>();
                Map<String, Object> map = new HashMap<>();
                //先放名称地址,用于排序
                    Device device =deviceMap.get(mac);
                    sortMap.put("名称",device.getName());
                    sortMap.put("地址",device.getAddress());
                //将传感器代号转化为汉字
                param.forEach((key, value) -> {
                    Sensor sensor = sensors.get(key);
                    if (!ObjectUtils.isEmpty(sensor)) {
                        String unit = ObjectUtils.isEmpty(sensor.getUnit()) ? "" : (String) sensor.getUnit();
                        map.put(sensor.getName(), value + unit);
                        sortMap.put(sensor.getName(), value + unit);
                    }
                });
                String mac = (String) param.get("mac");
                map.put("mac", mac);
                map.put("flag",1);
                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发送设备数据异常");