package com.moral.webSocketServer;
|
|
import com.alibaba.fastjson.JSON;
|
import com.moral.common.util.ParameterUtils;
|
import com.moral.entity.Device;
|
import com.moral.entity.Sensor;
|
import com.moral.service.DeviceService;
|
import com.moral.service.SensorService;
|
import com.rabbitmq.client.*;
|
import com.moral.util.RabbitMQUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.ObjectUtils;
|
|
import javax.annotation.PostConstruct;
|
import javax.websocket.*;
|
import javax.websocket.server.PathParam;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@Slf4j
|
@ServerEndpoint("/web/webSocket/{param}")
|
@Component
|
public class BSWebsocketServer {
|
|
public static DeviceService deviceService;
|
|
public static SensorService sensorService;
|
|
/**
|
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
*/
|
private static ConcurrentHashMap<String, BSWebsocketServer> webSocketMap = new ConcurrentHashMap<>();
|
/**
|
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
*/
|
private Session session;
|
|
private String orgId;
|
|
private String accountId;
|
|
private String regionCode;
|
|
|
private final String exchange = "screens_data";
|
|
private static Map<String, Sensor> sensors;
|
|
|
@PostConstruct
|
public void init() {
|
sensors = new HashMap<>();
|
List<Sensor> allSensors = sensorService.getAllSensors();
|
for (Sensor sensor : allSensors) {
|
sensors.put(sensor.getSensorKey(), sensor);
|
}
|
}
|
|
/**
|
* 连接建立成功调用的方法
|
*/
|
@OnOpen
|
public void onOpen(Session session, @PathParam("param") String param) {
|
this.session = session;
|
String[] params = param.split("&");
|
this.orgId = params[1];
|
this.accountId = params[0];
|
this.regionCode = params[2];
|
|
if (webSocketMap.containsKey(accountId)) {
|
webSocketMap.remove(accountId);
|
webSocketMap.put(accountId, this);
|
} else {
|
webSocketMap.put(accountId, this);
|
}
|
|
Map<String, Object> paramMap = new HashMap<String, Object>();
|
paramMap.put("orgId", orgId);
|
paramMap.put("regionCode", regionCode);
|
ParameterUtils.getRegionType4RegionCode(paramMap);
|
List<Device> deviceList = deviceService.queryDevice(paramMap);
|
|
try {
|
Connection connection = RabbitMQUtils.getConnection();
|
final Channel channel = connection.createChannel();
|
//生成临时队列
|
String queue = channel.queueDeclare().getQueue();
|
|
//交换机与队列通过routingKey进行绑定
|
String routingKey = "";
|
for (Device d : deviceList) {
|
routingKey = orgId + "." + d.getMac();
|
channel.queueBind(queue, exchange, routingKey);
|
}
|
|
//消费消息,手动确认模式。
|
channel.basicConsume(queue, false, new DefaultConsumer(channel) {
|
@Override
|
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
|
Map message = (Map) JSON.parse((String) JSON.parse(new String(body)));
|
message = messageFormat(message);
|
sendMessage(JSON.toJSONString(message));
|
channel.basicAck(envelope.getDeliveryTag(), false);
|
}
|
});
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
@OnClose
|
public void onClose() {
|
if (webSocketMap.containsKey(accountId)) {
|
webSocketMap.remove(accountId);
|
}
|
}
|
|
//接收到客户端消息操作
|
@OnMessage
|
public void onMessage(String message, Session session) {
|
|
}
|
|
@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());
|
}
|
}
|
|
//将MQ取出的数据进行格式化处理
|
private Map<String,Object> messageFormat( Map<String,Object> param) {
|
Map<String, Object> map = new HashMap<>();
|
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);
|
}
|
});
|
map.put("mac",param.get("mac"));
|
return map;
|
}
|
|
}
|