package com.whyc.websocket;
|
|
import com.whyc.service.PowerAlarmService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.websocket.*;
|
import javax.websocket.server.PathParam;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
|
@ServerEndpoint(value = "/powerAlarm/{method}/{userId}",encoders = WebSocketEncoder.class)
|
@Component
|
@Slf4j
|
public class PowerAlarmWebSocket {
|
|
private Session session;
|
|
private static PowerAlarmService service;
|
|
private volatile Thread thread;
|
|
private static final int executeTime = 15000;
|
|
@Autowired
|
public void setService(PowerAlarmService service) {
|
PowerAlarmWebSocket.service = service;
|
}
|
|
@OnOpen
|
public void onOpen(Session session, @PathParam("method") String method,@PathParam("userId")Integer userId){
|
|
switch (method){
|
case "acInput":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getACInputAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "error":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getErrorAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "acABC":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getAcABCAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "rectifier":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getRectifierAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "status":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getStatus(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "batteryGroup":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getBatteryGroupAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "stationCount":{
|
if(session!=null) {
|
thread = new Thread("Thread_powerAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getAlarmStationCount(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
}
|
}
|
|
@OnClose
|
public void onClose(){
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
log.warn("webSocket会话关闭了:{}",session);
|
}
|
|
@OnError
|
public void onError(Throwable error) {
|
error.printStackTrace();
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
}
|
|
}
|