FreeDATA/gui/src/js/api.js

193 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-11-18 15:53:54 +00:00
import { settingsStore as settings } from "../store/settingsStore.js";
2023-12-10 08:01:20 +00:00
import {
validateCallsignWithSSID,
validateCallsignWithoutSSID,
} from "./freedata";
2023-11-09 18:46:29 +00:00
2024-02-03 12:55:30 +00:00
import { processFreedataMessages } from "./messagesHandler";
2023-11-20 13:24:45 +00:00
function buildURL(params, endpoint) {
const url = "http://" + params.host + ":" + params.port + endpoint;
2023-11-18 15:53:54 +00:00
return url;
2023-11-09 18:46:29 +00:00
}
2023-11-18 15:53:54 +00:00
async function apiGet(endpoint) {
2023-11-26 06:07:52 +00:00
try {
const response = await fetch(buildURL(settings.local, endpoint));
if (!response.ok) {
throw new Error(`REST response not ok: ${response.statusText}`);
}
return await response.json();
2023-11-26 06:07:52 +00:00
} catch (error) {
2024-02-08 12:39:18 +00:00
//console.error("Error getting from REST:", error);
}
2023-11-18 15:53:54 +00:00
}
export async function apiPost(endpoint, payload = {}) {
try {
2023-11-20 13:24:45 +00:00
const response = await fetch(buildURL(settings.local, endpoint), {
2023-11-18 15:53:54 +00:00
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);
}
}
2024-01-28 11:32:16 +00:00
export async function apiDelete(endpoint, payload = {}) {
try {
const response = await fetch(buildURL(settings.local, endpoint), {
method: "DELETE",
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 deleting from REST:", error);
}
}
export async function getVersion() {
2023-11-23 06:31:28 +00:00
let data = await apiGet("/version").then((res) => {
return res;
});
2024-02-10 12:57:18 +00:00
if (typeof data !== "undefined" && typeof data.version !== "undefined") {
return data.version;
}
return 0;
2023-11-18 15:56:39 +00:00
}
export async function getConfig() {
2024-01-28 11:32:16 +00:00
return await apiGet("/config");
2023-11-18 15:53:54 +00:00
}
2024-01-28 11:32:16 +00:00
export async function setConfig(config) {
return await apiPost("/config", config);
2023-11-18 15:53:54 +00:00
}
2024-01-28 11:32:16 +00:00
export async function getAudioDevices() {
return await apiGet("/devices/audio");
2023-11-18 15:53:54 +00:00
}
2024-01-28 11:32:16 +00:00
export async function getSerialDevices() {
return await apiGet("/devices/serial");
2023-11-09 18:46:29 +00:00
}
2024-01-28 11:32:16 +00:00
export async function setModemBeacon(enabled = false) {
return await apiPost("/modem/beacon", { enabled: enabled });
2023-11-18 15:56:39 +00:00
}
2024-01-28 11:32:16 +00:00
export async function sendModemCQ() {
return await apiPost("/modem/cqcqcq");
2023-11-18 15:56:39 +00:00
}
2024-01-28 11:32:16 +00:00
export async function sendModemPing(dxcall) {
2023-12-10 08:01:20 +00:00
if (
validateCallsignWithSSID(dxcall) === false &&
validateCallsignWithoutSSID(dxcall) === true
) {
dxcall = String(dxcall).toUpperCase().trim();
dxcall = dxcall + "-0";
2023-12-10 07:05:48 +00:00
}
2023-12-10 08:01:20 +00:00
dxcall = String(dxcall).toUpperCase().trim();
2023-12-10 07:05:48 +00:00
if (validateCallsignWithSSID(dxcall))
2024-01-28 11:32:16 +00:00
return await apiPost("/modem/ping_ping", { dxcall: dxcall });
2023-11-18 15:56:39 +00:00
}
2024-01-28 11:32:16 +00:00
export async function sendModemARQRaw(mycall, dxcall, data, uuid) {
return await apiPost("/modem/send_arq_raw", {
mycallsign: mycall,
2023-12-15 15:22:38 +00:00
dxcall: dxcall,
data: data,
uuid: uuid,
});
}
2024-01-28 11:32:16 +00:00
export async function stopTransmission() {
return await apiPost("/modem/stop_transmission");
2023-12-21 14:05:22 +00:00
}
2024-01-28 11:32:16 +00:00
export async function sendModemTestFrame() {
return await apiPost("/modem/send_test_frame");
2023-11-18 18:06:29 +00:00
}
2024-01-28 11:32:16 +00:00
export async function startModem() {
return await apiPost("/modem/start");
2023-11-09 18:46:29 +00:00
}
2024-01-28 11:32:16 +00:00
export async function stopModem() {
return await apiPost("/modem/stop");
2023-11-09 18:46:29 +00:00
}
2023-11-13 17:50:46 +00:00
2024-01-28 11:32:16 +00:00
export async function getModemState() {
return await apiGet("/modem/state");
2023-11-17 21:36:37 +00:00
}
2023-12-16 16:11:16 +00:00
export async function setRadioParametersFrequency(frequency) {
2024-01-28 11:32:16 +00:00
return await apiPost("/radio", {
radio_frequency: frequency,
});
}
export async function setRadioParametersMode(mode) {
return await apiPost("/radio", {
radio_mode: mode
});
}
export async function setRadioParametersRFLevel(rf_level) {
return await apiPost("/radio", {
radio_rf_level: rf_level
});
2023-12-16 16:11:16 +00:00
}
2024-01-28 11:32:16 +00:00
export async function getRadioStatus() {
return await apiGet("/radio");
}
2024-02-03 12:55:30 +00:00
export async function getFreedataMessages() {
let res = await apiGet("/freedata/messages");
processFreedataMessages(res);
}
export async function getFreedataAttachmentBySha512(data_sha512) {
let res = await apiGet(`/freedata/messages/attachment/${data_sha512}`);
return res;
}
export async function sendFreedataMessage(destination, body, attachments) {
return await apiPost("/freedata/messages", {
2024-02-02 18:37:02 +00:00
destination: destination,
body: body,
attachments: attachments,
});
2024-01-28 11:32:16 +00:00
}
2024-02-03 13:16:23 +00:00
export async function retransmitFreedataMessage(id) {
return await apiPost(`/freedata/messages/${id}`);
}
2024-01-28 11:32:16 +00:00
export async function deleteFreedataMessage(id) {
return await apiDelete(`/freedata/messages/${id}`);
}
2024-02-03 12:55:30 +00:00
export async function getBeaconDataByCallsign(callsign) {
return await apiGet(`/freedata/beacons/${callsign}`);
}