From 782e84d68779471c0640584678d4c4df3afa80a2 Mon Sep 17 00:00:00 2001
From: 于紫祥_1901 <email@yuzixiang_1910>
Date: Mon, 14 Dec 2020 17:29:08 +0800
Subject: [PATCH] 电子屏

---
 src/main/java/com/moral/service/DeviceService.java                      |    3 
 src/main/resources/mapper/DeviceMapper.xml                              |    6 +
 src/main/java/com/moral/config/WebSocketConfig.java                     |   11 +
 src/main/java/com/moral/mapper/DeviceMapper.java                        |    2 
 src/main/java/com/moral/controller/ScreenController.java                |    7 +
 src/main/java/com/moral/service/impl/DeviceServiceImpl.java             |    5 +
 src/main/java/com/moral/webSocketServer/ElectronicSWebSocketServer.java |  224 ++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 253 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/moral/config/WebSocketConfig.java b/src/main/java/com/moral/config/WebSocketConfig.java
index df8b756..80c4225 100644
--- a/src/main/java/com/moral/config/WebSocketConfig.java
+++ b/src/main/java/com/moral/config/WebSocketConfig.java
@@ -1,12 +1,10 @@
 package com.moral.config;
 
+import com.moral.service.AccountService;
 import com.moral.service.DeviceService;
 import com.moral.service.SensorService;
-import com.moral.webSocketServer.BSAQIWebSocketServer;
-import com.moral.webSocketServer.BSWebsocketServer;
-import com.moral.webSocketServer.WebSocketServer;
+import com.moral.webSocketServer.*;
 
-import com.moral.webSocketServer.WebSocketServerNew;
 import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
 import org.springframework.amqp.support.converter.MessageConverter;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,9 +34,14 @@
         BSAQIWebSocketServer.deviceService=deviceService;
     }
     @Autowired
+    public void setMessageService(AccountService accountService){
+        ElectronicSWebSocketServer.accountService=accountService;
+    }
+    @Autowired
     public void setSensorService( SensorService sensorService){
         BSWebsocketServer.sensorService=sensorService;
         BSAQIWebSocketServer.sensorService=sensorService;
+        ElectronicSWebSocketServer.sensorService=sensorService;
     }
 
 }
diff --git a/src/main/java/com/moral/controller/ScreenController.java b/src/main/java/com/moral/controller/ScreenController.java
index 65924e7..34b7118 100644
--- a/src/main/java/com/moral/controller/ScreenController.java
+++ b/src/main/java/com/moral/controller/ScreenController.java
@@ -2987,4 +2987,11 @@
         }
         return new ResultBean<List<Map<String,Object>>>(rList);
     }
+
+    @PostMapping("byAccountGetDevices")
+    public List<Device> byAccountGetDevices(@RequestBody Map<String, Object> parameters) {
+        String id =parameters.get("id").toString();
+        List<Device> devicesList = deviceService.getDevicesByAccountId(id);
+        return devicesList;
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/com/moral/mapper/DeviceMapper.java b/src/main/java/com/moral/mapper/DeviceMapper.java
index a30d2e1..4cedb72 100644
--- a/src/main/java/com/moral/mapper/DeviceMapper.java
+++ b/src/main/java/com/moral/mapper/DeviceMapper.java
@@ -95,5 +95,5 @@
 
     List<Device> getDeviceByCityCode();
 
-
+    List<Device> getDevicesByAccountId(String id);
 }
\ No newline at end of file
diff --git a/src/main/java/com/moral/service/DeviceService.java b/src/main/java/com/moral/service/DeviceService.java
index 0789f53..28038d1 100644
--- a/src/main/java/com/moral/service/DeviceService.java
+++ b/src/main/java/com/moral/service/DeviceService.java
@@ -90,4 +90,7 @@
 	List<Device> getDevice(String macOrName);
 
 	List<Device> getDeviceByCode();
+
+	List<Device> getDevicesByAccountId(String id);
+
 }
diff --git a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
index f7168de..2a020ef 100644
--- a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -1130,4 +1130,9 @@
         return deviceMapper.getDeviceByCityCode();
     }
 
+    @Override
+    public List<Device> getDevicesByAccountId(String id) {
+        return deviceMapper.getDevicesByAccountId(id);
+    }
+
 }
diff --git a/src/main/java/com/moral/webSocketServer/ElectronicSWebSocketServer.java b/src/main/java/com/moral/webSocketServer/ElectronicSWebSocketServer.java
new file mode 100644
index 0000000..75e24cb
--- /dev/null
+++ b/src/main/java/com/moral/webSocketServer/ElectronicSWebSocketServer.java
@@ -0,0 +1,224 @@
+package com.moral.webSocketServer;
+
+import com.alibaba.fastjson.JSON;
+import com.moral.entity.Account;
+import com.moral.entity.Device;
+import com.moral.entity.Sensor;
+import com.moral.service.AccountService;
+import com.moral.service.DeviceService;
+import com.moral.service.SensorService;
+import com.moral.util.RabbitMQUtils;
+import com.rabbitmq.client.*;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.CrossOrigin;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.websocket.*;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.TimeoutException;
+
+@Slf4j
+@ServerEndpoint("/web/ESWebSocket/{param}")
+@Component
+public class ElectronicSWebSocketServer {
+
+    public static AccountService accountService;
+
+    public static SensorService sensorService;
+
+    /**
+     * concurrent������������������Set���������������������������������������WebSocket���������
+     */
+    public static CopyOnWriteArraySet<ElectronicSWebSocketServer> webSocketSet = new CopyOnWriteArraySet<ElectronicSWebSocketServer>();
+
+    /**
+     * ������������������������������������������������������������������������������
+     */
+    private Session session;
+
+    private String orgId;
+    private String accountId;
+    private String mac;
+
+    private final String exchange = "screens_data";
+    Connection connection;
+    Channel channel;
+
+    @OnOpen
+    public void onOpen(Session session, @PathParam("param") String param) {
+
+        this.session = session;
+        webSocketSet.add(this);
+        String[] params = param.split("&");
+        this.accountId = params[0];
+        Map<String, Object> organizationIdByAccountId = accountService.getOrganizationIdByAccountId(this.accountId);
+        this.mac = params[1];
+        this.orgId = organizationIdByAccountId.get("organization_id").toString();
+        try {
+            connection = RabbitMQUtils.getConnection();
+            channel = connection.createChannel();
+
+            //������������������
+            String queue = channel.queueDeclare().getQueue();
+            //������������������������routingKey������������
+            String routingKey = "";
+            routingKey = this.orgId + "." + this.mac;
+            channel.queueBind(queue, exchange, routingKey);
+            //������������,���������������������
+            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 {
+                    //������MQ������������������������������������������������������������
+                    Map<String,Object> message = (Map) JSON.parse((String) JSON.parse(new String(body)));
+                    Map<Integer,Object> sortMap = new HashMap<>();
+                    message.forEach((key,value)->{
+                        SensorSortEnum sensorSortEnum = SensorSortEnum.valueOf(key);
+                        Map newMap = new HashMap();
+                        newMap.put(key,value);
+                        sortMap.put(sensorSortEnum.getSensorSort(),newMap);
+                    });
+
+                    Set<Map.Entry<Integer, Object>> entries = sortMap.entrySet();
+                    List<Map.Entry<Integer, Object>> list = new ArrayList<>();
+                    list.addAll(entries);
+                    Collections.sort(list, new Comparator<Map.Entry<Integer, Object>>() {
+                        @Override
+                        public int compare(Map.Entry<Integer, Object> o1, Map.Entry<Integer, Object> o2) {
+                            return o1.getKey()-o2.getKey();
+                        }
+                    });
+
+                    Map<String,Object> resultMap = new LinkedHashMap<>();
+                    for (Map.Entry<Integer, Object> entry : list) {
+                        Map<String,Object> linkedMap = (Map<String, Object>) entry.getValue();
+                        linkedMap.forEach((key,value)->{
+                            resultMap.put(SensorSortEnum.valueOf(key).getSensorName(),value);
+                        });
+                    }
+                     Map<String,Object> reMap = new LinkedHashMap<>();
+                     List<Sensor> allSensors = sensorService.getAllSensors();
+                     for (int i = 0; i <resultMap.keySet().size() ; i++) {
+                            for (Sensor sensor : allSensors) {
+                                if (Arrays.asList(resultMap.keySet().toArray(new String[resultMap.keySet().size()])).get(i).equals(sensor.getSensorKey())){
+                                    String value = resultMap.get(Arrays.asList(resultMap.keySet().toArray(new String[resultMap.keySet().size()])).get(i)).toString();
+                                   /* String e = Arrays.asList(resultMap.keySet().toArray(new String[resultMap.keySet().size()])).get(i);
+                                    resultMap.remove(e);*/
+                                    reMap.put(sensor.getName(), value+sensor.getUnit());
+                                    break;
+                                }else {
+                                    continue;
+                                }
+                        }
+                    }
+                    reMap.put("time",resultMap.get("time"));
+
+                    sendMessage(JSON.toJSONString(reMap));
+                    //������������
+                    channel.basicAck(envelope.getDeliveryTag(), true);
+                    //������socket������������������
+                }
+            });
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @OnClose
+    public void onClose() {
+        webSocketSet.remove(this); // ���set���������
+        try {
+            channel.close();
+            connection.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (
+                TimeoutException e) {
+            e.printStackTrace();
+        }
+    }
+
+    //������������������������������
+    @OnMessage
+    public void onMessage(String message, Session session) {
+
+    }
+    //���������������������������������������
+
+     enum SensorSortEnum {
+        e1(1,"e1"),
+         e20(2,"e20"),
+         e7(3,"e7"),
+        e2(4,"e2"),
+         e17(5,"e17"),
+         e6(6,"e6"),
+         e15(7,"e15"),
+         e27(8,"e27"),
+         e18(9,"e18"),
+        e11(10,"e11"),
+         e21(11,"e21"),
+         e23(12,"e23"),
+         e10(13,"e10"),
+         e25(14,"e25"),
+         e28(15,"e28"),
+        e16(16,"e16"),
+         e26(17,"e26"),
+         e12(18,"e12"),
+         e5(19,"e5"),
+         e3(20,"e3"),
+         e13(21,"e13"),
+         e9(22,"e9"),
+         e4(23,"e4"),
+         e51(24,"e51"),
+        mac(100,"mac"),
+        ver(101,"ver"),
+        time(102,"time"),
+        e8(14,"e8"),
+        e94(94,"e94"),
+        e92(92,"e92"),
+        e40(40,"e40"),
+        e93(93,"e93");
+
+        private final Integer sensorSort;
+        private final String sensorName;
+
+        SensorSortEnum(Integer sensorSort, String sensorName) {
+            this.sensorSort = sensorSort;
+            this.sensorName = sensorName;
+        }
+
+        public Integer getSensorSort() {
+            return sensorSort;
+        }
+
+        public String getSensorName() {
+            return sensorName;
+        }
+    }
+
+    @OnError
+    public void onError(Session session, Throwable error) {
+        log.error(error.getMessage());
+    }
+
+    public void sendMessage(String message) throws IOException {
+        try {
+            if (session.isOpen()) {
+                this.session.getBasicRemote().sendText(message);
+            }
+        } catch (IOException e) {
+
+            //log.error(e.getMessage());
+        }
+    }
+
+
+}
diff --git a/src/main/resources/mapper/DeviceMapper.xml b/src/main/resources/mapper/DeviceMapper.xml
index cbdd370..33cac0c 100644
--- a/src/main/resources/mapper/DeviceMapper.xml
+++ b/src/main/resources/mapper/DeviceMapper.xml
@@ -577,4 +577,10 @@
     <select id="getDeviceByCityCode" resultType="com.moral.entity.Device">
         SELECT d.* FROM `monitor_point` mt,device d where d.monitor_point_id=mt.id and d.state!=4 and mt.city_code=130900
     </select>
+    <select id="getDevicesByAccountId" resultType="com.moral.entity.Device">
+        select d.* from monitor_point as m,device as d
+where d.monitor_point_id=m.id and  d.monitor_point_id in
+(select m.id from organization as o,monitor_point as m where o.id = m.organization_id and o.id =
+(select o.id FROM  account as a,organization as o where a.organization_id = o.id and a.id = #{id})) and d.is_delete !=1
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0