whyclxw
2022-02-24 bee7271e1d8d3bab4ae9b6d2b253c9c61adb573a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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.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.util.HashMap;
import java.util.Map;
 
@Component
@ServerEndpoint(value = "/Satation3D",encoders = WebSocketEncoder.class)
public class Satation3DWebsocket {
    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 BattRtdataService rtdataservice;     //单体
 
    private Session session;
 
    @Autowired
    public void setService(BattRtstateService rtstateservice) {
        Satation3DWebsocket.rtstateservice = rtstateservice;
    }
 
    @Autowired
    public void setService(BattRtdataService rtdataservice) {
        Satation3DWebsocket.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 ( !this.isInterrupted()&&!exit) {
                    try {
                        if (session.isOpen()) {
                            Response res=new Response();
                            res=getStation3D(realTimePar.getStationId());
                            //推送信息
                            session.getBasicRemote().sendObject(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();
        }
        threadMap.remove(session.getId());
    }
 
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
 
    //标准的实时页面信息
    public Response getStation3D(String stationId){
        Map<String,Response> res=new HashMap<>();
        //3D查询机房下电池组端信息
        Response rtstateRes=rtstateservice.getStation3D(stationId);
        res.put("rtstate3D",rtstateRes);
        //3D查询机房下电池组单体信息
        Response rtdataRes=rtdataservice.getStation3D(stationId);
        res.put("rtdata3D",rtdataRes);
        return  new Response().set(1,res);
    }
}