summaryrefslogtreecommitdiff
path: root/client/ws_client.html
blob: 62cf70cd1c1438d4b7a840d7ff6e38e7d9034be4 (plain)
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
<script src='https://cdn.bootcdn.net/ajax/libs/echarts/5.2.2/echarts.min.js'></script>
<title>CSI Motion Detection</title>
<div id='chart' style='width: 1200px; height: 800px;'></div>
<script type="text/javascript">
    var chart = echarts.init(document.getElementById('chart'));
    var option = {
    title: {
    text: '实时数据',
    backgroundColor: 'rgb(128,128,128)',
    textStyle:{fontSize:48},
    left: 'right'
    },
    tooltip: {
    trigger: 'axis'
    },
    xAxis: {
    type: 'category',
    data: [] // 这里用于存放X轴数据
    },
    yAxis: {
    type: 'value',
    min:0,
    max:1
    },
    series: [{
    data: [], // 这里用于存放Y轴数据
    type: 'line'
    }]
    };
    chart.setOption(option);

    function updateChart(data) {
        
        option.xAxis.data.push(data.t);
        option.series[0].data.push(data.x); 
        if (option.xAxis.data.length >= 20){
            option.xAxis.data.shift();
            option.series[0].data.shift();
        }
        
        if (data.x > 0.5){
            option.title.text='Motion Detected';
            option.title.backgroundColor='rgb(0,200,0)';
        }
        else {
            option.title.text='No Motion';
            option.title.backgroundColor='rgb(128,128,128)';
        }

        chart.setOption(option); 
    }


    var socket = new WebSocket("ws://localhost:4200"); // 替换为你的WebSocket服务器地址
    socket.onopen = function(event) {
    console.log('WebSocket connected');
    };
    socket.onmessage = function(event) {
    var data = JSON.parse(event.data); // 假设服务器发送的数据是JSON格式的字符串
    updateChart(data); // 调用函数更新图表数据
    };
    socket.onclose = function(event) {
    console.log('WebSocket closed');
    };
    socket.onerror = function(error) {
    console.log('WebSocket error: ' + error);
    };
</script>