package com.whyc.webSocket;
|
|
import com.whyc.service.Fbs9100StateService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.websocket.EncodeException;
|
import javax.websocket.OnOpen;
|
import javax.websocket.Session;
|
import javax.websocket.server.PathParam;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
import java.security.Principal;
|
|
@ServerEndpoint(value = "/deviceWorkState/{deviceId}/{workState}/{uId}",encoders = WebSocketEncoder.class)
|
@Component
|
public class DeviceWorkStateSocket {
|
|
private volatile Thread thread;
|
|
private static final int executeTime = 15000;
|
|
private static Fbs9100StateService service;
|
|
@Autowired
|
public void setService(Fbs9100StateService service) {
|
DeviceWorkStateSocket.service = service;
|
}
|
|
@OnOpen
|
public void onOpen(Session session, @PathParam("deviceId") Integer deviceId, @PathParam("workState") Integer workState, @PathParam("uId") Long uId){
|
thread = new Thread("Thread_batteryData") {
|
public void run() {
|
while (!thread.isInterrupted()) {
|
try {
|
if (session.isOpen()) {
|
session.getBasicRemote().sendObject(service.getList(deviceId,workState,uId));
|
}
|
sleep(executeTime);
|
} catch (IOException | InterruptedException | EncodeException e) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
}
|
|
}
|