whycxzp
2021-05-20 3da521b9e0a86dd9cc0bb28b2dfa0e7084ffc00f
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.whyc.ws;
 
import com.whyc.dto.Response;
import com.whyc.pojo.ExperimentPoint;
import com.whyc.service.DeviceService;
import com.whyc.service.ExperimentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
import static java.lang.Thread.sleep;
 
@ServerEndpoint(value = "/experiment/{experimentId}", encoders = WebSocketEncoder.class)
@Component
@Api(tags = "设备ws")
@Slf4j
public class ExperimentSocket {
 
    private Session session;
 
    private volatile Thread thread;
 
    private static ExperimentService service;
 
    @Autowired
    public void setService(ExperimentService service) {
        this.service = service;
    }
 
    /**
     * 点击开始试验,开启webSocket,传入试验id,更新试验状态
     *
     * @param session
     * @param experimentId
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("experimentId") String experimentId) {
        log.warn("ExperimentSocket会话开启了:{}", session);
        this.session = session;
        if (session.isOpen()) {
            //session.getBasicRemote().sendObject(service.updateStatus(experimentId));
            service.updateStatus(experimentId);
            sendMessage(experimentId);
 
        }
    }
 
    @OnMessage
    public void onMessage(String message) {
        try {
            this.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 动态返回步骤和执行信息
     * @param experimentId
     */
    private void sendMessage(String experimentId) {
        String type = experimentId.split("_")[0].toLowerCase();
        try {
            //检查步骤1,属于正式实验前的环境稳定性校验
            session.getBasicRemote().sendObject(service.checkPreconditionStep1(type));
 
            //正式测试点的数据返回
            //1.返回整体试验点
            Response pointResponse = service.getPoint(experimentId);
            List<ExperimentPoint> experimentPoints = (List<ExperimentPoint>) pointResponse.getData();
            experimentPoints.stream().forEach(experimentPoint -> {
                //启动第一个测试点,返回整体状态
                experimentPoint.setStatus(1);
                experimentPoint.setStartTime(new Date());
                service.updatePointStatus(experimentPoint);
                try {
                    session.getBasicRemote().sendObject(experimentPoints);
                } catch (IOException | EncodeException e) {
                    e.printStackTrace();
                }
                try {
                    //等待一个测试时间
                    sleep(experimentPoint.getDuration());
                    //更新第一个测试点的情况,获取整个时间的情况
 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
 
            });
 
        } catch (IOException | EncodeException e) {
            e.printStackTrace();
        }
        if(session!=null) {
            thread = new Thread("Thread_device") {
                public void run() {
                    while (!thread.isInterrupted()) {
 
                        if (session.isOpen()) {
                            //session.getBasicRemote().sendObject(service.getStatus());
                            try {
                                session.getBasicRemote().sendObject("yeah!");
                            } catch (IOException | EncodeException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
 
                    }
                }
            };
            thread.start();
        }
    }
 
    @OnClose
    public void onClose() {
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        log.warn("webSocket会话关闭了:{}", session);
 
    }
 
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
 
}