FreeDATA/gui/src/store/settingsStore.js

142 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-11-27 02:37:28 +00:00
import { reactive, ref, watch } from "vue";
2023-11-18 09:44:17 +00:00
import { getConfig, setConfig } from "../js/api";
2024-01-17 18:49:14 +00:00
import { getAppDataPath } from "../js/freedata";
2024-01-17 18:44:39 +00:00
import fs from "fs";
2024-01-17 18:49:14 +00:00
const path = require("path");
2024-01-17 18:44:39 +00:00
const nconf = require("nconf");
2024-01-17 18:49:14 +00:00
var appDataPath = getAppDataPath();
2024-01-17 18:44:39 +00:00
var configFolder = path.join(appDataPath, "FreeDATA");
2024-01-23 10:44:34 +00:00
let configFile = "config.json";
2024-01-23 10:42:47 +00:00
2024-01-23 10:44:34 +00:00
const isGitHubActions = process.env.GITHUB_ACTIONS === "true";
2024-01-23 10:39:16 +00:00
if (isGitHubActions) {
2024-01-23 10:44:34 +00:00
configFile = "example.json";
configFolder = appDataPath;
2024-01-23 10:39:16 +00:00
}
2024-01-23 10:42:47 +00:00
2024-01-23 10:39:16 +00:00
var configPath = path.join(configFolder, configFile);
2024-01-17 18:44:39 +00:00
2024-01-17 18:49:14 +00:00
console.log("AppData Path:", appDataPath);
2024-01-17 18:44:39 +00:00
console.log(configFolder);
console.log(configPath);
2023-10-04 17:54:50 +00:00
2024-01-17 18:44:39 +00:00
nconf.file({ file: configPath });
// +++
//GUI DEFAULT SETTINGS........
//Set GUI defaults here, they will be used if not found in config/config.json
//They should be an exact mirror (variable wise) of settingsStore.local
//Nothing else should be needed aslong as components are using v-bind
// +++
2024-02-10 12:57:18 +00:00
const defaultConfig = {
2023-11-18 15:53:54 +00:00
local: {
host: "127.0.0.1",
port: "5000",
2023-11-27 02:37:28 +00:00
spectrum: "waterfall",
wf_theme: 2,
2023-12-03 23:54:44 +00:00
update_channel: "alpha",
enable_sys_notification: false,
2023-12-17 15:58:19 +00:00
grid_layout: "[]",
2024-01-11 03:18:15 +00:00
grid_preset: "[]",
grid_enabled: true,
2023-11-18 15:53:54 +00:00
},
2023-11-18 09:44:17 +00:00
remote: {
AUDIO: {
enable_auto_tune: false,
input_device: "",
output_device: "",
rx_audio_level: 0,
tx_audio_level: 0,
},
MESH: {
enable_protocol: false,
},
MODEM: {
enable_fsk: false,
enable_low_bandwidth_mode: false,
respond_to_cq: false,
rx_buffer_size: 0,
tuning_range_fmax: 0,
tuning_range_fmin: 0,
tx_delay: 0,
2023-11-19 08:56:00 +00:00
enable_hamc: false,
enable_morse_identifier: false,
2023-11-18 09:44:17 +00:00
},
RADIO: {
2023-11-18 11:57:36 +00:00
control: "disabled",
model_id: 0,
serial_port: "",
serial_speed: "",
data_bits: 0,
stop_bits: 0,
serial_handshake: "",
ptt_port: "",
ptt_type: "",
serial_dcd: "",
serial_dtr: "",
},
RIGCTLD: {
ip: "127.0.0.1",
port: 0,
path: "",
command: "",
arguments: "",
2023-11-18 09:44:17 +00:00
},
STATION: {
enable_explorer: false,
enable_stats: false,
2024-02-10 12:57:18 +00:00
mycall: "DEFAULT",
2023-11-18 11:57:36 +00:00
myssid: 0,
2023-11-18 09:44:17 +00:00
mygrid: "",
ssid_list: [],
},
TCI: {
tci_ip: "127.0.0.1",
tci_port: 0,
},
2024-02-18 20:08:14 +00:00
MESSAGES: {
enable_auto_repeat: false,
},
2023-11-18 09:44:17 +00:00
},
};
2024-02-10 12:57:18 +00:00
nconf.defaults(defaultConfig);
2024-02-10 12:57:18 +00:00
nconf.required(["local:host", "local:port"]);
export const settingsStore = reactive(defaultConfig);
2023-10-03 13:15:17 +00:00
//Save settings for GUI to config file
2023-12-10 20:58:12 +00:00
settingsStore.local = nconf.get("local");
2023-12-19 01:09:43 +00:00
saveLocalSettingsToConfig();
2023-11-18 09:44:17 +00:00
export function onChange() {
setConfig(settingsStore.remote).then((conf) => {
settingsStore.remote = conf;
});
}
2023-11-18 15:53:54 +00:00
export function getRemote() {
2023-11-20 13:24:45 +00:00
return getConfig().then((conf) => {
if (typeof conf !== "undefined") {
2024-02-10 12:57:18 +00:00
settingsStore.remote = conf;
onChange();
} else {
console.warn("Received undefined configuration, using default!");
2024-02-10 12:57:18 +00:00
settingsStore.remote = defaultConfig.remote;
}
2023-11-18 15:53:54 +00:00
});
}
2023-09-05 17:35:54 +00:00
2023-11-18 16:12:05 +00:00
watch(settingsStore.local, (oldValue, newValue) => {
//This function watches for changes, and triggers a save of local settings
2023-12-19 01:09:43 +00:00
saveLocalSettingsToConfig();
2023-11-18 16:12:05 +00:00
});
2023-12-19 01:09:43 +00:00
export function saveLocalSettingsToConfig() {
2023-12-10 20:58:12 +00:00
nconf.set("local", settingsStore.local);
nconf.save();
2023-12-19 01:09:43 +00:00
//console.log("Settings saved!");
2023-12-10 20:58:12 +00:00
}