FreeDATA/gui/src/js/settingsHandler.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-10-03 13:15:17 +00:00
import path from "node:path";
import fs from "fs";
2023-11-04 02:12:06 +00:00
import { setColormap } from "./waterfallHandler";
2023-09-06 10:23:20 +00:00
// pinia store setup
2023-10-03 13:15:17 +00:00
import { setActivePinia } from "pinia";
import pinia from "../store/index";
2023-09-06 10:23:20 +00:00
setActivePinia(pinia);
2023-10-03 13:15:17 +00:00
import { useSettingsStore } from "../store/settingsStore.js";
2023-09-06 10:23:20 +00:00
const settings = useSettingsStore(pinia);
2023-11-08 10:24:52 +00:00
2023-09-06 10:23:20 +00:00
// ---------------------------------
2023-10-22 08:12:00 +00:00
console.log(process.env);
2023-10-22 09:13:02 +00:00
var appDataFolder = "undefined";
2023-10-22 08:12:00 +00:00
if (typeof process.env["APPDATA"] !== "undefined") {
2023-10-22 09:13:02 +00:00
appDataFolder = process.env["APPDATA"];
2023-10-22 08:12:00 +00:00
console.log(appDataFolder);
2023-10-04 21:34:36 +00:00
} else {
2023-10-22 08:12:00 +00:00
switch (process.platform) {
case "darwin":
2023-10-22 09:13:02 +00:00
appDataFolder = process.env["HOME"] + "/Library/Application Support";
2023-10-22 08:12:00 +00:00
console.log(appDataFolder);
break;
case "linux":
2023-10-22 09:13:02 +00:00
appDataFolder = process.env["HOME"] + "/.config";
2023-10-22 08:12:00 +00:00
console.log(appDataFolder);
2023-10-22 09:19:25 +00:00
break;
2023-10-22 08:12:00 +00:00
case "win32":
2023-10-22 09:13:02 +00:00
appDataFolder = "undefined";
2023-10-22 08:12:00 +00:00
break;
default:
2023-10-22 09:13:02 +00:00
appDataFolder = "undefined";
2023-10-22 08:12:00 +00:00
break;
}
2023-10-04 21:34:36 +00:00
}
2023-09-06 10:23:20 +00:00
var configFolder = path.join(appDataFolder, "FreeDATA");
var configPath = path.join(configFolder, "config.json");
2023-10-22 08:12:00 +00:00
console.log(appDataFolder);
console.log(configFolder);
console.log(configPath);
2023-10-04 21:34:36 +00:00
2023-09-06 10:23:20 +00:00
// create config folder if not exists
if (!fs.existsSync(configFolder)) {
fs.mkdirSync(configFolder);
}
// create config file if not exists with defaults
2023-11-08 10:24:52 +00:00
const configDefaultSettings = `{
"modem_host": "127.0.0.1",
"modem_port": 3000,
"spectrum": "waterfall",
"theme": "default",
"screen_height": 430,
"screen_width": 1050,
"update_channel": "latest",
"wftheme": 2,
"enable_sys_notification": 1
}`;
var parsedConfig = JSON.parse(configDefaultSettings);
2023-09-06 10:23:20 +00:00
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, configDefaultSettings);
}
2023-10-03 13:15:17 +00:00
export function loadSettings() {
// load settings
var config = require(configPath);
2023-09-06 12:48:45 +00:00
2023-10-03 13:15:17 +00:00
//config validation
// check running config against default config.
// if parameter not exists, add it to running config to prevent errors
console.log("CONFIG VALIDATION ----------------------------- ");
2023-09-06 20:20:18 +00:00
2023-10-03 13:15:17 +00:00
for (var key in parsedConfig) {
if (config.hasOwnProperty(key)) {
console.log("FOUND SETTTING [" + key + "]: " + config[key]);
2023-09-06 20:20:18 +00:00
} else {
2023-10-03 13:15:17 +00:00
console.log("MISSING SETTTING [" + key + "] : " + parsedConfig[key]);
config[key] = parsedConfig[key];
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}
try {
2023-11-04 02:12:06 +00:00
if (key=="wftheme") {
setColormap(config[key]);
}
2023-10-03 13:15:17 +00:00
if (key == "mycall") {
settings.mycall = config[key].split("-")[0];
settings.myssid = config[key].split("-")[1];
} else {
settings[key] = config[key];
}
} catch (e) {
console.log(e);
2023-09-06 20:20:18 +00:00
}
2023-09-06 10:23:20 +00:00
}
2023-09-06 12:48:45 +00:00
}
2023-10-03 13:15:17 +00:00
export function saveSettingsToFile() {
console.log("save settings to file...");
let config = settings.getJSON();
console.log(config);
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}
2023-11-08 10:24:52 +00:00
export function processModemConfig(data){
console.log(data)
// basic test if we received settings
// we should iterate through JSON, by using equal variables here like in modem config
// STATION SETTINGS
settings.mycall = data["STATION"].mycall
settings.mygrid = data["STATION"].mygrid
}