jinpengyong
2020-09-28 121a5b01a2461b5d9d5c188f9dd36c2e4673db9d
实时风向与因子数据从同一个websokceket获取优化
3 files modified
127 ■■■■ changed files
src/main/java/com/moral/webSocketServer/WebSocketServer.java 31 ●●●● patch | view | raw | blame | history
src/main/webapp/js/newmoralmap.js 89 ●●●●● patch | view | raw | blame | history
src/main/webapp/view/newmap.jsp 7 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/webSocketServer/WebSocketServer.java
@@ -17,12 +17,18 @@
import com.moral.common.util.ParameterUtils;
import com.moral.entity.Device;
import com.moral.service.DeviceService;
import com.moral.util.RabbitMQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.QueueingConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
@@ -66,7 +72,7 @@
        int flag = param.indexOf("&");
        int regionCodeIndex = param.indexOf("_");
        orgId = param.substring(0, flag);
        accountId = param.substring(flag + 1,regionCodeIndex);
        accountId = param.substring(flag + 1, regionCodeIndex);
        regionCode = param.substring(regionCodeIndex + 1);
        String QUEUE_NAME = "deviceInfo_" + accountId;
@@ -79,15 +85,16 @@
        try {
            //打开连接和创建频道,与发送端一样
            ConnectionFactory factory = new ConnectionFactory();
            /*ConnectionFactory factory = new ConnectionFactory();
            //设置MabbitMQ所在主机ip或者主机名
            factory.setHost("47.96.15.25");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest_pass");
            factory.setPassword("guest_pass");*/
            //Connection connection = RabbitMQUtils.getConnection();
            String routingKey;
            connection = factory.newConnection();
            channel = connection.createChannel();
            this.connection = RabbitMQUtils.getConnection();
            channel = this.connection.createChannel();
            //声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。
            channel.queueDeclare(QUEUE_NAME, false, false, true, null);
            for (Device d : deviceList) {
@@ -95,7 +102,8 @@
                channel.queueBind(QUEUE_NAME, "screens_data", routingKey);
            }
            //创建队列消费者
            QueueingConsumer consumer = new QueueingConsumer(channel);
            /*java废弃方法*/
            /*QueueingConsumer consumer = new QueueingConsumer(channel);
            //指定消费队列
            channel.basicConsume(QUEUE_NAME, true, consumer);
@@ -104,7 +112,15 @@
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());
                sendMessage(message);
            }
            }*/
            channel.basicConsume(QUEUE_NAME, false, new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String msg = new String(body, "utf-8");
                    sendMessage(msg);
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage());
        }
@@ -118,7 +134,6 @@
        /**从安全Set中 移除当前连接对象*/
        webSocketSet.remove(this);
        try {
            //channel.queueDelete("deviceInfo_" + accountId);
            connection.close();
        } catch (IOException e) {
            log.error(e.getMessage());
src/main/webapp/js/newmoralmap.js
@@ -218,7 +218,7 @@
                    var longitude = 106.638135;
                    var latitude = 29.709625;
                    point = new BMap.Point(longitude, latitude);
                }else if("射阳县" == regoinNames[2]){
                } else if ("射阳县" == regoinNames[2]) {
                    var longitude = 120.332826;
                    var latitude = 33.772274;
                    point = new BMap.Point(longitude, latitude);
@@ -399,7 +399,7 @@
        var timer = setTimeout(function () {
            baiduMap.closeInfoWindow.apply(baiduMap, arguments);
            moralMap["_closeInfoWinTimer"] = null;
        }, 1200);
        }, 500);
        moralMap["_closeInfoWinTimer"] = timer;
    }
    moralMap.clearOverlays = function () {
@@ -948,56 +948,47 @@
        }
        var mouseOverHandle = option['mouseover'];
        moralMarker.addEventListener("mouseover", function () {
            var obj = {};
            obj['methodName'] = 'deliverMac';
            obj['mac'] = option['mac'];
            var objStr = JSON.stringify(obj);
            if (!!window['external'] && !!window['external']['callWin']) {
                window['external'].callWin(objStr);
            } else {
                alert(objStr);
            }
            moralMap.showSensors = function (jsonData) {
                /* var deviceSensors = jsonData;
                 var mac = deviceSensors["mac"];
                 if (mac == option['mac']) {*/
                var deviceSensors = jsonData;
                var mac = deviceSensors["mac"];
                if (!jsonData.hasOwnProperty('mac')) {
                    infoWindow.setContent("");
                    infoWindow.redraw();
                } else {
                    var adressOutHtml = '<div style="min-height:20px;overflow:auto;max-height:480px;font-size: 12px;" onmouseover="moralMap.clearCloseInfoWindow()" onmouseout="moralMap.closeInfoWindow()">';
                    var y, m, d, h, mm, s;
                    var date = new Date($.now());
                    y = 1900 + date.getYear();
                    m = "0" + (date.getMonth() + 1);
                    d = "0" + date.getDate();
                    h = "0" + date.getHours();
                    mm = "0" + date.getMinutes();
                    s = "0" + date.getSeconds();
                    date = y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length) + " " + h.substring(h.length - 2, h.length) + ":" + mm.substring(mm.length - 2, mm.length) + ":" + s.substring(s.length - 2, s.length);
                    adressOutHtml += '<p style="height: 18px;line-height: 18px">' + "时间:&nbsp;" + date + "&nbsp;</p>";
                    for (var prop in jsonData) {
                        if (jsonData.hasOwnProperty(prop) && prop != 'ver' && prop != 'methodName' && prop != 'mac' && prop != 'time') {
                            for (var key in sensorsDescriptionMap) {
                                if (prop == key) {
                                    var sensorsDescription = sensorsDescriptionMap[key];
                                }
                            }
                            for (var key in sensorsUnitMap) {
                                if (prop == key) {
                                    var sensorsUnit = sensorsUnitMap[key];
                                    if (sensorsUnit == null) {
                                        sensorsUnit = "";
                    if (mac == option['mac']) {
                        var adressOutHtml = '<div style="min-height:20px;overflow:auto;max-height:480px;font-size: 12px;" onmouseover="moralMap.clearCloseInfoWindow()" onmouseout="moralMap.closeInfoWindow()">';
                        var y, m, d, h, mm, s;
                        var date = new Date($.now());
                        y = 1900 + date.getYear();
                        m = "0" + (date.getMonth() + 1);
                        d = "0" + date.getDate();
                        h = "0" + date.getHours();
                        mm = "0" + date.getMinutes();
                        s = "0" + date.getSeconds();
                        date = y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length) + " " + h.substring(h.length - 2, h.length) + ":" + mm.substring(mm.length - 2, mm.length) + ":" + s.substring(s.length - 2, s.length);
                        adressOutHtml += '<p style="height: 18px;line-height: 18px">' + "时间:&nbsp;" + date + "&nbsp;</p>";
                        for (var prop in jsonData) {
                            if (jsonData.hasOwnProperty(prop) && prop != 'ver' && prop != 'methodName' && prop != 'mac' && prop != 'time') {
                                for (var key in sensorsDescriptionMap) {
                                    if (prop == key) {
                                        var sensorsDescription = sensorsDescriptionMap[key];
                                    }
                                }
                                for (var key in sensorsUnitMap) {
                                    if (prop == key) {
                                        var sensorsUnit = sensorsUnitMap[key];
                                        if (sensorsUnit == null) {
                                            sensorsUnit = "";
                                        }
                                    }
                                }
                                adressOutHtml += ('<p style="height: 18px;line-height: 18px">' + sensorsDescription + ":" + jsonData[prop] + sensorsUnit + "</p>");
                            }
                            adressOutHtml += ('<p style="height: 18px;line-height: 18px">' + sensorsDescription + ":" + jsonData[prop] + sensorsUnit + "</p>");
                        }
                        adressOutHtml += "</div>";
                        infoWindow.setContent(adressOutHtml);
                        infoWindow.redraw();
                    }
                    adressOutHtml += "</div>";
                    infoWindow.setContent(adressOutHtml);
                    infoWindow.redraw();
                }
            }
            // }
@@ -1008,14 +999,6 @@
        });
        moralMarker.addEventListener("mouseout", function () {
            var obj = {};
            obj['methodName'] = 'stopDeliverMac';
            var objStr = JSON.stringify(obj);
            if (!!window['external'] && !!window['external']['callWin']) {
                window['external'].callWin(objStr);
            } else {
                alert(objStr);
            }
            moralMap.closeInfoWindow();
        });
@@ -1122,15 +1105,15 @@
                        if (longitude < bounds.northeastLng && longitude > bounds.southwestLng && latitude < bounds.northeastLat && latitude > bounds.southwestLat) {
                            markers.push(oldmarker);
                        }
                        if("国控站"===option["monitorPoint"]["description"]){
                        if ("国控站" === option["monitorPoint"]["description"]) {
                            var icon05 = new BMap.Icon("/img/ico05.png", new BMap.Size(50, 50));
                            var mark05 = new BMap.Marker(new BMap.Point(option["monitorPoint"]["longitude"], option["monitorPoint"]["latitude"]), {icon: icon05});
                            markers.push(mark05);
                        }else if("省控站"===option["monitorPoint"]["description"]){
                        } else if ("省控站" === option["monitorPoint"]["description"]) {
                            var icon06 = new BMap.Icon("/img/ico06.png", new BMap.Size(50, 50));
                            var mark06 = new BMap.Marker(new BMap.Point(option["monitorPoint"]["longitude"], option["monitorPoint"]["latitude"]), {icon: icon06});
                            markers.push(mark06);
                        }else if("县控站"===option["monitorPoint"]["description"]){
                        } else if ("县控站" === option["monitorPoint"]["description"]) {
                            var icon08 = new BMap.Icon("/img/ico08.png", new BMap.Size(50, 50));
                            var mark08 = new BMap.Marker(new BMap.Point(option["monitorPoint"]["longitude"], option["monitorPoint"]["latitude"]), {icon: icon08});
                            markers.push(mark08);
src/main/webapp/view/newmap.jsp
@@ -710,8 +710,9 @@
                      moralMap.showSensors(JSON.parse(JSON.parse(msg.data)));
                  }*/
                if ($("#close").html() == "CloseMq") {
                    ws.close();
                    ws.close(JSON.parse(msg.data));
                }
                moralMap.showSensors(JSON.parse(JSON.parse(msg.data)));
            };
            //关闭事件
@@ -722,7 +723,6 @@
            ws.onerror = function () {
                $("#close").html("websocket关闭错误");
            };
        }
    })(jQuery);
@@ -839,9 +839,6 @@
        } else if ("RefreshState" == methodName) {
            var states = jsonData.states;
            moralMap.refreshState(states);
        } else if ("DeliverSensors" == methodName) {
            $("#close").html(methodName);
            moralMap.showSensors(jsonData);
        } else if ("CloseMq" == methodName) {
            $("#close").html(methodName);
        }