package com.whyc.webSocket;
|
|
import com.whyc.dto.Response;
|
import com.whyc.dto.paramter.RealTimePar;
|
import com.whyc.service.BattRtdataService;
|
import com.whyc.service.BattRtstateService;
|
import com.whyc.service.Fbs9100StateService;
|
import com.whyc.util.ActionUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
import java.sql.SQLException;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Component
|
@ServerEndpoint(value = "/RealTime",encoders = WebSocketEncoder.class)
|
public class RealTimeWebsocket {
|
private volatile Thread thread;
|
|
private static final int executeTime = 5000;
|
|
private static boolean exit=false;
|
|
private static Map<String,Thread> threadMap = new HashMap<>();
|
|
private static BattRtstateService rtstateservice;
|
|
private static Fbs9100StateService f9100service;
|
|
private static BattRtdataService rtdataservice;
|
|
private Session session;
|
|
@Autowired
|
public void setService(BattRtstateService rtstateservice) {
|
RealTimeWebsocket.rtstateservice = rtstateservice;
|
}
|
|
@Autowired
|
public void setService(Fbs9100StateService f9100service) {
|
RealTimeWebsocket.f9100service = f9100service;
|
}
|
|
@Autowired
|
public void setService(BattRtdataService rtdataservice) {
|
RealTimeWebsocket.rtdataservice = rtdataservice;
|
}
|
|
@OnOpen
|
public void onOpen(Session session){
|
this.session=session;
|
}
|
@OnMessage
|
public void onMessage(Session session, String message){
|
//停止当前socket的线程
|
Thread threadBefore = threadMap.get(session.getId());
|
if(threadBefore !=null && threadBefore.isAlive()){
|
threadBefore.interrupt();
|
exit=true;
|
}
|
RealTimePar realTimePar= ActionUtil.getGson().fromJson(message,RealTimePar.class);
|
thread = new Thread("Thread_RealTime") {
|
public void run() {
|
exit=false;
|
while ( !thread.isInterrupted()&&!exit) {
|
try {
|
if (session.isOpen()) {
|
Map<String,Response> res=new HashMap<>();
|
//查询fbs9100信息
|
Response f9100stateRes=f9100service.serchContactorState(realTimePar.getDevId());
|
res.put("f9100state",f9100stateRes);
|
//查询电池组端信息
|
Response rtstateRes=rtstateservice.serchByCondition(realTimePar.getBattGroupId());
|
res.put("rtstate",rtstateRes);
|
//查询电池组单体信息
|
Response rtdataRes=rtdataservice.serchByCondition(realTimePar.getBattGroupId());
|
res.put("rtdata",rtdataRes);
|
//推送信息
|
session.getBasicRemote().sendObject(new Response().set(1,res));
|
}
|
sleep(executeTime);
|
} catch ( IOException | InterruptedException | EncodeException e ) {
|
interrupt();
|
}
|
}
|
}
|
};
|
thread.start();
|
//将线程存储,便于调用定位
|
threadMap.put(session.getId(), this.thread);
|
}
|
|
@OnClose
|
public void onClose(){
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
}
|
|
@OnError
|
public void onError(Throwable error) {
|
error.printStackTrace();
|
if (thread != null && thread.isAlive()) {
|
thread.interrupt();
|
}
|
}
|
}
|