New file |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | } |