From 1b4c6718db46841cbfff33b26336f3d1e5cb19d4 Mon Sep 17 00:00:00 2001
From: 于紫祥_1901 <email@yuzixiang_1910>
Date: Sat, 10 Oct 2020 11:31:50 +0800
Subject: [PATCH] update
---
src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java | 62 ++++++++++++++++++++++++------
1 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java b/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java
index 570884f..9b42ef0 100644
--- a/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java
+++ b/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;
@@ -25,6 +26,13 @@
@Slf4j
@ServerEndpoint("/web/testWebSocket/{param}")
@Component
+/**
+* @Description: ���������������websocket
+ * @Param:
+ * @return:
+ * @Author: ������������
+ * @Date: 2020/9/30
+ */
public class BSTestWebsocketServer {
public static DeviceService deviceService;
@@ -51,6 +59,7 @@
private final String exchange = "screens_data";
private static Map<String, Sensor> sensors;
+
@PostConstruct
@@ -85,30 +94,39 @@
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 {
+ //������MQ������������������������������������������������������������
Map message = (Map) JSON.parse((String) JSON.parse(new String(body)));
sendWindInfo(message);
+
+ //���������������������������������������mac���������������������������������mac������������
if(mac!=null&&(!mac.equals(0)))
- sendDeviceInfo(message);
- channel.basicAck(envelope.getDeliveryTag(), false);
+ sendDeviceInfo(message,deviceMap);
+
+ //������������
+ channel.basicAck(envelope.getDeliveryTag(), true);
+
//������socket������������������
if (!webSocketMap.containsKey(accountId)) {
RabbitMQUtils.closeConnectionChannel(connection, channel);
@@ -132,7 +150,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 +171,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,22 +191,35 @@
}
}
- //������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,Map<String,Device> deviceMap) {
String deviceMac = (String) param.get("mac");
if (mac.equals(deviceMac)) {
try {
- Map<String, Object> map = new HashMap<>();
+ Map<String,Object> sortMap = new LinkedHashMap<>();
+
+ //���������������������������������
+ 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);
- sendMessage(JSON.toJSONString(map));
+
+ sendMessage(JSON.toJSONString(sortMap));
} catch (IOException e) {
log.error("������mac������������������������");
}
--
Gitblit v1.8.0