FreeDATA/gui/src/js/api.js

86 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-11-18 15:53:54 +00:00
import { settingsStore as settings } from "../store/settingsStore.js";
2023-11-09 18:46:29 +00:00
2023-11-18 15:53:54 +00:00
function buildURL(endpoint) {
const url =
"http://" + settings.local.host + ":" + settings.local.port + endpoint;
return url;
2023-11-09 18:46:29 +00:00
}
2023-11-18 15:53:54 +00:00
async function apiGet(endpoint) {
const response = await fetch(buildURL(endpoint));
if (!response.ok) {
throw new Error(`REST response not ok: ${response.statusText}`);
}
const data = await response.json();
return data;
}
export async function apiPost(endpoint, payload = {}) {
try {
const response = await fetch(buildURL(endpoint), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`REST response not ok: ${response.statusText}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error posting to REST:", error);
}
}
2023-11-18 15:56:39 +00:00
export function getVersion() {
return apiGet("/version");
}
2023-11-18 15:53:54 +00:00
export function getConfig() {
return apiGet("/config");
}
export function setConfig(config) {
return apiPost("/config", config);
}
export function getAudioDevices() {
return apiGet("/devices/audio");
}
export function getSerialDevices() {
return apiGet("/devices/serial");
2023-11-09 18:46:29 +00:00
}
2023-11-18 15:56:39 +00:00
export function setModemBeacon(enabled = false) {
return apiPost("/modem/beacon", { enabled: enabled });
}
export function sendModemCQ() {
return apiPost("/modem/cqcqcq");
}
export function sendModemPing(dxcall) {
return apiPost("/modem/cqcqcq", { dxcall: dxcall });
}
2023-11-09 21:11:53 +00:00
export function startModem() {
2023-11-18 15:53:54 +00:00
return apiPost("/modem/start");
2023-11-09 18:46:29 +00:00
}
2023-11-09 21:11:53 +00:00
export function stopModem() {
2023-11-18 15:53:54 +00:00
return apiPost("/modem/stop");
2023-11-09 18:46:29 +00:00
}
2023-11-13 17:50:46 +00:00
export function getModemVersion() {
2023-11-18 15:53:54 +00:00
getFromServer("/version");
2023-11-13 17:50:46 +00:00
}
2023-11-18 15:56:39 +00:00
2023-11-17 21:35:52 +00:00
export function getModemCurrentState() {
getFromServer(settings.modem_host, settings.modem_port, "modem/state", null);
2023-11-17 21:36:37 +00:00
}