whyclxw
2022-02-09 7197f14e4b25a152549639ff1d621c0130a2d41f
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
109
110
111
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();
        }
    }
}