-
Notifications
You must be signed in to change notification settings - Fork 1
/
owrxantswitcher.js
96 lines (81 loc) · 3.01 KB
/
owrxantswitcher.js
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
// Antenna switch UI plugin for OpenWebRX+
// License: MIT
// Original Example File Copyright (c) 2023 Stanislav Lechev [0xAF], LZ2SLL
//
// Modified by DL9UL to provide UI buttons used to call a WebAPI
// Init function of the plugin
Plugins.owrxantswitcher.init = function () {
// Function to send a letter via POST
function sendLetter(letter) {
// Use the base URL of the main document with port 5001
const apiEndpoint = `HTTP://${window.location.hostname}:5001/antennaswitch`;
fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ command: letter }),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
updateButtonState(data.payload.response);
//console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
// Function to update the button state based on the active antenna
function updateButtonState(activeAntenna) {
for (let i = 1; i <= 4; i++) {
const button = document.getElementById(`owrx-ant-button-${i}`);
button.classList.remove('highlighted');
if (i.toString() === activeAntenna) {
button.classList.add('highlighted');
}
}
}
// Create a openwebrx-section
const antSection = document.createElement('div');
antSection.classList.add('openwebrx-section');
const antPanelLine = document.createElement('div');
antPanelLine.classList.add('openwebrx-ant','openwebrx-panel-line');
antSection.appendChild(antPanelLine);
// Create buttons and add them to the container
const antGrid = document.createElement('div');
antGrid.classList.add('openwebrx-ant-grid');
antPanelLine.appendChild(antGrid);
for (let i = 1; i <= 4; i++) {
const button = document.createElement('div');
button.id = `owrx-ant-button-${i}`;
button.classList.add('openwebrx-button');
button.textContent = `Ant ${i}`;
button.onclick = function () {
const letter = String(i);
sendLetter(letter);
};
antGrid.appendChild(button);
}
// Section Divider to hide ANT panel
const antSectionDivider = document.createElement('div');
antSectionDivider.id = 'openwebrx-section-ant';
antSectionDivider.classList.add('openwebrx-section-divider');
antSectionDivider.onclick = function() { UI.toggleSection(this);};
antSectionDivider.innerHTML = "▾ Ant";
// Append the container above the "openwebrx-section-modes"
const targetElement = document.getElementById('openwebrx-section-modes');
targetElement.parentNode.insertBefore(antSectionDivider, targetElement);
targetElement.parentNode.insertBefore(antSection, targetElement);
// Retrieve initial button configuration
sendLetter("s");
// Schedule automatic update every 2000 ms to refelct changes from other clients
setInterval(function () {
sendLetter('s');
}, 2000);
return true;
};