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 webSocketSet = new CopyOnWriteArraySet(); /** * 与某个客户端的连接会话,需要通过它来给客户端发送数据 */ 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 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 message = (Map) JSON.parse((String) JSON.parse(new String(body))); Map 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> entries = sortMap.entrySet(); List> list = new ArrayList<>(); list.addAll(entries); Collections.sort(list, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return o1.getKey()-o2.getKey(); } }); Map resultMap = new LinkedHashMap<>(); for (Map.Entry entry : list) { Map linkedMap = (Map) entry.getValue(); linkedMap.forEach((key,value)->{ resultMap.put(SensorSortEnum.valueOf(key).getSensorName(),value); }); } Map reMap = new LinkedHashMap<>(); List allSensors = sensorService.getAllSensors(); for (int i = 0; i