package com.whyc.websocket;
|
|
import com.whyc.config.WebSocketConfig;
|
import com.whyc.dto.Response;
|
import com.whyc.service.CKPowerDevBattRsAlarmService;
|
import com.whyc.service.CKPowerDevBattRtAlarmService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 测控台设备告警和单体告警
|
*/
|
@Component
|
@ServerEndpoint(value = "/ckDevRsAndRtAlm",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
|
public class CKDevRsAndRtAlmSocket {
|
|
private Session session;
|
|
private Thread thread;
|
|
private static CKPowerDevBattRsAlarmService rsAlarmService;
|
|
private static CKPowerDevBattRtAlarmService rtAlarmService;
|
|
@Autowired
|
public void setRsAlarmService(CKPowerDevBattRsAlarmService rsAlarmService) {
|
CKDevRsAndRtAlmSocket.rsAlarmService = rsAlarmService;
|
}
|
|
@Autowired
|
public void setRtAlarmService(CKPowerDevBattRtAlarmService rtAlarmService) {
|
CKDevRsAndRtAlmSocket.rtAlarmService = rtAlarmService;
|
}
|
|
|
@OnOpen
|
public void onOpen(Session session, EndpointConfig config){
|
this.session = session;
|
Thread thread = new Thread() {
|
@Override
|
public void run() {
|
try{
|
while (!currentThread().isInterrupted()) {
|
Map<String,Object>map=new HashMap<>();
|
Response rsAlmRes = rsAlarmService.getRsAlm();
|
map.put("rsAlmRes",rsAlmRes);
|
Response rtAlmRes = rtAlarmService.getRtAlm();
|
map.put("rtAlmRes",rtAlmRes);
|
session.getBasicRemote().sendObject(new Response<>().set(1,map,"查询完成"));
|
sleep(4000);
|
}
|
} catch (Exception e) {
|
this.interrupt();
|
}
|
}
|
};
|
thread.start();
|
this.thread = thread;
|
}
|
|
@OnClose
|
public void onClose(CloseReason closeReason) throws IOException {
|
System.err.println("closeReason = " + closeReason);
|
if(session.isOpen()){
|
session.close();
|
}
|
}
|
|
@OnError
|
public void onError(Throwable error) throws IOException {
|
error.printStackTrace();
|
thread.isInterrupted();
|
if(session.isOpen()){
|
session.close();
|
}
|
}
|
}
|