whyclxw
2024-04-10 b243b60548a15529cbfa43e67e738c94571757d9
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
112
113
114
115
116
117
118
package com.whyc.websocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.BreakAlarmDto;
import com.whyc.dto.Response;
import com.whyc.pojo.db_ckpwrdev_data_rt.CKPowerDevBattRtData;
import com.whyc.pojo.db_ckpwrdev_data_rt.CKPowerDevBattRtState;
import com.whyc.pojo.db_ckpwrdev_data_rt.CKPowerDevRt;
import com.whyc.pojo.db_ckpwrdev_data_rt.CKPowerDevSignal;
import com.whyc.service.CKPowerDevBattRtDataService;
import com.whyc.service.CKPowerDevBattRtStateService;
import com.whyc.service.CKPowerDevRtService;
import com.whyc.service.CKPowerDevSignalService;
import com.whyc.util.JsonUtil;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 测控电源遥测量和遥信量
 * +核容装置状态
 */
@Component
@ServerEndpoint(value = "/ckRtAndSignalAndHr",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class CKRtAndSignalAndHrSocket {
 
    private Session session;
 
    private Thread thread;
 
    private volatile Map<String,Thread> threadMap = new HashMap<>();
 
    private static CKPowerDevRtService ckPowerDevRtService;
 
    private static CKPowerDevSignalService ckPowerDevSignalService;
 
    private static CKPowerDevBattRtStateService ckPowerDevBattRtStateService;
 
    private static CKPowerDevBattRtDataService ckPowerDevBattRtDataService;
 
    @Autowired
    public void setCkPowerDevRtService(CKPowerDevRtService ckPowerDevRtService) {
        CKRtAndSignalAndHrSocket.ckPowerDevRtService = ckPowerDevRtService;
    }
 
    @Autowired
    public void setCkPowerDevSignalService(CKPowerDevSignalService ckPowerDevSignalService) {
        CKRtAndSignalAndHrSocket.ckPowerDevSignalService = ckPowerDevSignalService;
    }
 
    @Autowired
    public void setCkPowerDevBattRtStateService(CKPowerDevBattRtStateService ckPowerDevBattRtStateService) {
        CKRtAndSignalAndHrSocket.ckPowerDevBattRtStateService = ckPowerDevBattRtStateService;
    }
 
    @Autowired
    public void setCkPowerDevBattRtDataService(CKPowerDevBattRtDataService ckPowerDevBattRtDataService) {
        CKRtAndSignalAndHrSocket.ckPowerDevBattRtDataService = ckPowerDevBattRtDataService;
    }
 
    @OnOpen
    public void onOpen(Session session){
        this.session = session;
    }
    @OnMessage
    public void onMessage(String message){
        Thread thread = new Thread() {
            @Override
            public void run() {
                try{
                    while (!currentThread().isInterrupted()) {
                        CKPowerDevRt ckPowerDevRt = ckPowerDevRtService.get();
                        CKPowerDevSignal ckPowerDevSignal = ckPowerDevSignalService.get();
                        CKPowerDevBattRtState ckPowerDevBattRtState = ckPowerDevBattRtStateService.get(-1);
                        List<CKPowerDevBattRtData> ckPowerDevBattRtData = ckPowerDevBattRtDataService.getList(-1);
                        session.getBasicRemote().sendObject(new Response<>().setV(1,ckPowerDevRt,ckPowerDevSignal,ckPowerDevBattRtState,ckPowerDevBattRtData,new Date(),"查询完成"));
                        sleep(4000);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
        //停止老的socket线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
        }
        //将线程存储,便于调用定位
        threadMap.put(session.getId(), this.thread);
    }
    @OnClose
    public void onClose(CloseReason closeReason) throws IOException {
        System.err.println("closeReason = " + closeReason);
        if(session.isOpen()){
            session.close();
        }
    }
 
    @OnError
    public void onError(Throwable error) throws IOException {
        error.printStackTrace();
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        }
    }
 
}