package com.whyc.webSocket;
|
|
import com.whyc.config.WebSocketConfig;
|
import com.whyc.dto.Response;
|
import com.whyc.pojo.BattdischargePlan;
|
import com.whyc.service.BattalarmDataService;
|
import com.whyc.service.BattdischargePlanService;
|
import com.whyc.service.DevalarmDataService;
|
import com.whyc.service.PwrdevAlarmService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* 监控层首页
|
*/
|
@Component
|
@ServerEndpoint(value = "/homeMonitor", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
|
public class HomeMonitorSocket {
|
private volatile Thread thread;
|
|
private static final int executeTime = 5000;
|
|
private volatile boolean runFlag = true;
|
|
private volatile Map<String, Thread> threadMap = new HashMap<>();
|
|
private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
|
|
private static BattdischargePlanService planService;
|
|
private static BattalarmDataService bAlmService;
|
|
private static DevalarmDataService dAlmService;
|
|
private static PwrdevAlarmService pAlmService;
|
|
|
private Session session;
|
|
@Autowired
|
public void setBattdischargePlanService(BattdischargePlanService planService) {
|
HomeMonitorSocket.planService = planService;
|
}
|
|
@Autowired
|
public void setBattalarmDataService(BattalarmDataService bAlmService) {
|
HomeMonitorSocket.bAlmService = bAlmService;
|
}
|
|
@Autowired
|
public void setDevalarmDataService(DevalarmDataService dAlmService) {
|
HomeMonitorSocket.dAlmService = dAlmService;
|
}
|
|
@Autowired
|
public void setPwrdevAlarmService(PwrdevAlarmService pAlmService) {
|
HomeMonitorSocket.pAlmService = pAlmService;
|
}
|
|
@OnOpen
|
public void onOpen(Session session, EndpointConfig config) {
|
this.session = session;
|
/*HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
|
UserInf user = (UserInf) httpSession.getAttribute("user");
|
final int userId = user.getUId().intValue();*/
|
final int userId = 1001;
|
thread = new Thread("Thread_stationRt") {
|
@Override
|
public void run() {
|
while (runFlag && !isInterrupted()) {
|
Thread thread = currentThread();
|
threadFlagMap.put(thread.getId(), true);
|
try {
|
if (session.isOpen()) {
|
//推送信息
|
synchronized (session) {
|
Map<String, Object> map = new HashMap<>();
|
Response planRes = new Response<>();
|
List<BattdischargePlan> planList = new ArrayList<>();
|
//今日放电任务统计
|
try {
|
Map<String, Integer> planMap = new HashMap<>();
|
planList = planService.getPlanStaticToday(userId);
|
Map<Integer, List<BattdischargePlan>> disMap = planList.stream().collect(Collectors.groupingBy(BattdischargePlan::getDischargeState));
|
for (Integer dischargeState : disMap.keySet()) {
|
planMap.put(String.valueOf(dischargeState), disMap.get(dischargeState).size());
|
}
|
planMap.put("sumNum", planList.size());
|
planRes.setII(1, planList.size() > 0, map, "今日放电任务统计");
|
} catch (Exception e) {
|
planRes.set(1, false, "出现异常" + e.getMessage());
|
}
|
map.put("planRes", planRes);
|
map.put("planList", planList);
|
//今日实时告警
|
Response bAlmRes = bAlmService.getBalmToday(userId);
|
map.put("bAlmRes", bAlmRes);
|
Response bDebRes = dAlmService.getDalmToday(userId);
|
map.put("bDebRes", bDebRes);
|
Response bPwrRes = pAlmService.getPalmToday(userId);
|
map.put("bPwrRes", bPwrRes);
|
session.getBasicRemote().sendObject(map);
|
}
|
threadFlagMap.put(thread.getId(), false);
|
}
|
sleep(executeTime);
|
//} catch (IOException | InterruptedException | EncodeException e) {
|
} 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);
|
}
|
|
@OnMessage
|
public void onMessage(Session session, String message) {
|
}
|
|
@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());
|
}
|
}
|