File size: 2,492 Bytes
ec33ac6
 
d25dd0d
 
 
 
ec33ac6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d25dd0d
ec33ac6
 
 
 
 
d25dd0d
ec33ac6
 
 
 
 
6b27ded
ec33ac6
 
 
 
 
 
 
 
 
 
 
 
 
 
d25dd0d
ec33ac6
 
 
 
d25dd0d
 
 
ec33ac6
 
 
d25dd0d
ec33ac6
d25dd0d
ec33ac6
d25dd0d
ec33ac6
 
 
 
d25dd0d
 
 
ec33ac6
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
let reachyIp = 'localhost';
let reachyPort = 8000;



const reachyClient = {
    getHTTPStatus: async () => {
        const statusDiv = document.getElementById('status-text-http');
        const statusDot = document.getElementById('status-dot-http');

        try {
            const resp = await fetch(`http://${reachyIp}:${reachyPort}/api/daemon/status`, { mode: 'cors' });
            if (resp.ok) {
                const status = await resp.json();
                statusDiv.innerText = `Connected: ${status.state}`;
                statusDot.classList.remove('disconnected');
                statusDot.classList.add('connected');
                console.log("HTTP OK");
            } else {
                statusDiv.innerText = 'HTTP Connection Error';
                statusDot.classList.remove('connected');
                statusDot.classList.add('disconnected');
                console.log("HTTP ERROR");
            }

        } catch (error) {
            statusDiv.innerText = 'HTTP Connection Error';
            console.log("HTTP ERROR");
        }
    },
    getWsStatus: async () => {
        const statusDiv = document.getElementById('status-text-ws');
        const statusDot = document.getElementById('status-dot-ws');

        try {

            const ws = new WebSocket(`ws://${reachyIp}:${reachyPort}/api/state/ws/full`);

            ws.onmessage = (msg) => {
                statusDiv.innerText = 'Connected: running';
                statusDot.classList.remove('disconnected');
                statusDot.classList.add('connected');
                ws.close();
                console.log("WS OK");
            };
            ws.onerror = () => {
                const statusDiv = document.getElementById('status-text-ws');
                statusDiv.innerText = 'WebSocket Connection Error';
                console.log("WS ERROR");
            };
        } catch (error) {
            statusDiv.innerText = 'WebSocket Connection Error';
            statusDot.classList.remove('connected');
            statusDot.classList.add('disconnected');
            console.log("WS ERROR");
        }
    },
};

const tryConnect = async () => {
    const ipInput = document.getElementById('ip-input');
    reachyIp = ipInput.value || 'localhost';

    console.log(`Trying to connect to Reachy at ${reachyIp}...`);

    await reachyClient.getHTTPStatus();
    await reachyClient.getWsStatus();
};

document.addEventListener('DOMContentLoaded', () => {
    tryConnect();
});