package com.whyc.webSocket;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.whyc.config.WebSocketConfig;
|
import com.whyc.dto.Response;
|
import com.whyc.factory.ThreadPoolExecutorFactory;
|
import com.whyc.pojo.PowerInf;
|
import com.whyc.pojo.RtState;
|
import com.whyc.pojo.StationInf;
|
import com.whyc.service.*;
|
import com.whyc.util.ActionUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpSession;
|
import javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
/**
|
* packageName com.whyc.websocket
|
*
|
* @author lxw
|
* @version JDK 8
|
* @className PowerSocket (此处以class为例)
|
* @date 2024/6/15
|
* @description TODO
|
*/
|
@Component
|
@ServerEndpoint(value = "/power", encoders = WebSocketEncoder.class)
|
public class PowerSocket{
|
private Session session;
|
|
private Thread thread;
|
|
private volatile boolean runFlag = true;
|
|
private volatile Map<String, Thread> threadMap = new HashMap<>();
|
|
|
private static final int executeTime = 5000;
|
|
private static PowerInfService pinfService;
|
|
private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
|
|
|
@Autowired
|
public void setPowerInfService(PowerInfService pinfService) {
|
PowerSocket.pinfService = pinfService;
|
}
|
|
|
@OnOpen
|
public void onOpen(Session session) {
|
this.session = session;
|
}
|
|
@OnMessage
|
public void onMessage(Session session, String message) {
|
int powerId=Integer.valueOf(message);
|
thread = new Thread("Thread_PowerSocket") {
|
@Override
|
public void run() {
|
while (runFlag && !isInterrupted()) {
|
Thread thread = currentThread();
|
threadFlagMap.put(thread.getId(), true);
|
try {
|
Map<String,Object> map=getPowerRt(powerId);
|
if (session.isOpen()) {
|
//推送信息
|
synchronized (session) {
|
session.getBasicRemote().sendObject(new Response().set(1, map));
|
}
|
threadFlagMap.put(thread.getId(), false);
|
}
|
sleep(executeTime);
|
} catch (Exception e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
threadFlagMap.put(thread.getId(),true);
|
//停止老的socket线程
|
Thread threadBefore = threadMap.get(session.getId());
|
if(threadBefore !=null && threadBefore.isAlive()){
|
while (threadFlagMap.get(threadBefore.getId())){
|
}
|
threadBefore.interrupt();
|
}
|
//将线程存储,便于调用定位
|
threadMap.put(session.getId(), this.thread);
|
}
|
//获取电源的实时数据
|
private Map<String,Object> getPowerRt(int powerId) {
|
Map<String,Object> map=pinfService.getInfById(powerId);
|
return map;
|
}
|
|
@OnClose
|
public void onClose(CloseReason closeReason){
|
System.err.println("closeReason = " + closeReason);
|
runFlag = false;
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
threadMap.remove(session.getId());
|
}
|
|
@OnError
|
public void onError(Throwable error) {
|
error.printStackTrace();
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
threadMap.remove(session.getId());
|
}
|
}
|