whyczh
2021-04-07 284c3e1a5474eddec29f6c2debdab4253269cdf8
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
119
120
121
122
123
124
125
126
127
package com.whyc.ws;
 
import com.whyc.dto.Response;
import com.whyc.pojo.MotorState;
import com.whyc.pojo.TestPlan;
import com.whyc.service.MotorStateService;
import com.whyc.service.TestPlanService;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
 
@ServerEndpoint(value = "/testDataWebSocket",encoders = WebSocketEncoder.class)
@Component
@Slf4j
public class TestDataWebSocket {
    private static MotorStateService motorStateService;
    private static TestPlanService testPlanService;
 
    @Autowired
    public void setMotorStateService(MotorStateService motorStateService){
        TestDataWebSocket.motorStateService = motorStateService;
    }
    @Autowired
    public void setTestPlanService(TestPlanService testPlanService){
        TestDataWebSocket.testPlanService = testPlanService;
    }
 
 
    /**
     *  与某个客户端的连接对话,需要通过它来给客户端发送消息
     */
    private Session session;
 
    /**
     * 标识当前连接客户端的参数  试验计划id
     */
    //rivate Integer testPlanId;
 
    /**
     *  用于存所有的连接服务的客户端,这个对象存储是安全的
     */
    //private static ConcurrentHashMap<String,WebSocket> webSocketSet = new ConcurrentHashMap<>();
 
    private static CopyOnWriteArraySet<TestDataWebSocket> webSockets = new CopyOnWriteArraySet<>();
    @OnOpen
    public void onOpen(Session session){
        System.out.println("试验计划实时数据websocket...");
        this.session = session;
        //this.testPlanId = testPlanId;
        webSockets.add(this);
    }
 
 
    @OnMessage
    public void onMessage(String message,Session session) {
        try {
            this.sendMessage(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public void sendMessage(String testPlanId) throws IOException{
        Thread thread = new Thread("TestDataWebSocket"){
            @Override
            public void run() {
                while (true) {
                    try {
                        Response model = new Response();
                        //获取试验计划数据
                        TestPlan testPlan = testPlanService.getOneById(Integer.parseInt(testPlanId));
                        //获取试验参试设备
                        if(testPlan==null){
                            break;
                        }
                        String devices = testPlan.getDevices();
                        String[] devIds = devices.split(",");
                        List<MotorState> list = new ArrayList<>();
                        for (String deviceId:devIds) {
                            MotorState motorState = motorStateService.getByDeviceId(Integer.parseInt(deviceId));
                            list.add(motorState);
                        }
                        model.set(1,list);
                        if(session.isOpen()) {
                            session.getBasicRemote().sendObject(model);
                        }else{
                            break;
                        }
                        sleep(4000);
                    } catch (IOException | InterruptedException | EncodeException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
    }
 
 
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        System.out.println("closing");
        webSockets.remove(this);
    }
 
    /**
     * 发生错误时调用
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error){
        System.out.println("发生错误");
        error.printStackTrace();
    }
 
}