whycxzp
2021-10-08 2d2f0048b80a47b8764b751c968ff196e0e6c5bb
更新电池数据/电池实时 webSocket接口
2个文件已添加
270 ■■■■■ 已修改文件
src/main/java/com/whyc/websocket/BatteryDataWebSocket.java 195 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/websocket/BatteryRTWebSocket.java 75 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/websocket/BatteryDataWebSocket.java
New file
@@ -0,0 +1,195 @@
package com.whyc.websocket;
import com.whyc.service.BatteryDataService;
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 = "/batteryData/{method}/{userId}",encoders = WebSocketEncoder.class)
@Component
@Slf4j
public class BatteryDataWebSocket {
    private Session session;
    private static BatteryDataService service;
    private volatile Thread thread;
    private static final int executeTime = 4000;
    @Autowired
    public void setService(BatteryDataService service) {
        BatteryDataWebSocket.service = service;
    }
    @OnOpen
    public void onOpen(Session session, @PathParam("method") String method,@PathParam("userId")Integer userId){
        switch (method){
            case "endurance":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getEndurance(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "batteryCap":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getBatteryCap(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "capStatus":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonCapStatus(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "monVol":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonVol(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "monTemp":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonTemp(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "monRes":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonRes(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "monCap":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryData") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonCap(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();
        }
    }
}
src/main/java/com/whyc/websocket/BatteryRTWebSocket.java
New file
@@ -0,0 +1,75 @@
package com.whyc.websocket;
import com.whyc.service.BatteryRTService;
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 = "/battery/{method}/{userId}",encoders = WebSocketEncoder.class)
@Component
@Slf4j
public class BatteryRTWebSocket {
    private Session session;
    private static BatteryRTService service;
    private volatile Thread thread;
    private static final int executeTime = 4000;
    @Autowired
    public void setService(BatteryRTService service) {
        BatteryRTWebSocket.service = service;
    }
    @OnOpen
    public void onOpen(Session session, @PathParam("method") String method,@PathParam("userId")Integer userId){
        switch (method) {
            case "status": {
                if (session != null) {
                    thread = new Thread("Thread_battery") {
                        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;
        }
    }
    @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();
        }
    }
}