package com.whyc.websocket;
|
|
import com.whyc.service.BatteryAlarmService;
|
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 = "/batteryAlarm/{method}/{userId}",encoders = WebSocketEncoder.class)
|
@Component
|
@Slf4j
|
public class BatteryAlarmWebSocket {
|
|
private Session session;
|
|
private static BatteryAlarmService service;
|
|
private volatile Thread thread;
|
|
private static final int executeTime = 15000;
|
|
@Autowired
|
public void setService(BatteryAlarmService service) {
|
BatteryAlarmWebSocket.service = service;
|
}
|
|
@OnOpen
|
public void onOpen(Session session, @PathParam("method") String method,@PathParam("userId")Integer userId){
|
|
switch (method){
|
case "monCapacity":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getMonCapacityLowAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "onlinegroupVolAnalysis":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getOnlineAndGroupVolAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "btsStatus":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getBTSEquipStatus(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "dischargeAnalysis":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getDischargeAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "chargeAnalysis":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getChargeAnalysis(userId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
}
|
break;
|
case "monVRTAnalysis":{
|
if(session!=null) {
|
thread = new Thread("Thread_batteryAlarm") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getMonVRTAnalysis(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();
|
}
|
}
|
|
}
|