aboutsummaryrefslogtreecommitdiffstats
path: root/static/main.js
blob: 7f6b1104ccfa23d09b38e058bf601f7c9157e299 (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
async function btn_add_destination() {
    const template = document.getElementById('destination_template');

    let clon = template.content.cloneNode(true);
    document.getElementById('destinations').appendChild(clon);
}

async function btn_remove_destination(element_clicked) {
    element_clicked.parentElement.remove()
}

async function btn_send_packet() {
    let data = {
        'comment': null,
        //'simplex': null,
        'destinations': [],
    };

    if (document.getElementById('with_comment').checked) {
        data.comment = document.getElementById('whisker_comment').value;
    }

    /* not yet implemented in ham-cats
    if (document.getElementById('with_simplex').checked) {
        const simplex_freq_MHz = parseInt(document.getElementById('simplex_mode').value, 10);
        const mode_select = document.getElementById('simplex_mode')
        const i = mode_select.selectedIndex;
        const simplex_mode = mode_select.options[i].text;

        data.simplex = {'frequency': simplex_freq_MHz * 1e6, 'mode': simplex_mode};
    }
    */

    const destinations = document.getElementById('destinations');
    const destList = destinations.querySelectorAll("p.destination");
    for (let i = 0; i < destList.length; i++) {
        const dest_callsign = destList[i].querySelector("input.dest_callsign").value;
        const dest_ssid_str = destList[i].querySelector("input.dest_ssid").value;
        const dest_ssid = parseInt(dest_ssid_str, 10);
        if (dest_ssid < 0 || dest_ssid > 255) {
            alert("SSID must be between 0 and 255");
            return;
        }
        data.destinations.push({'callsign': dest_callsign, 'ssid': dest_ssid});
    }

    await post('/api/send_packet', data);
}

async function post(url, data) {
    const params = {
        method: "POST",
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data),
    };

    let response = await fetch(url, params);
    if (!response.ok) {
        const text = await response.text();
        alert(`Error Sending: ${response.statusText} ${text}`);
    }
}