Skip to content

Commit 0da659a

Browse files
Ensure settings.json is always written and update UI to use it (#24)
Updated `src/downloadthread.cpp` to always write `settings.json` if `useSettings` is enabled, ensuring the file exists even if no specific settings are provided. Updated `src/configure.qml` to read and write `settings.json` for loading/saving configuration on the drive. Added logic to remove legacy .txt files (camera1.txt, air.txt, etc.) when writing the new JSON format to avoid conflicts. Analyze config files created on flashed device (#23) * Analyze config files created on flashed device Identified .txt files created on the target device during the flashing/configuration process. The primary logic is in `src/downloadthread.cpp`. Files identified: `config.txt`, `openhd/camera1.txt`, `openhd/air.txt`, `openhd/ground.txt`, `openhd/debug.txt`, and `openhd/{sbcValue}.txt`. * Refactor OpenHD config generation to use a single JSON file Replaced the creation of multiple .txt configuration files (camera1.txt, air.txt, ground.txt, debug.txt, {sbc}.txt) with a single `openhd/settings.json` file. The JSON file contains keys: "camera", "sbc", "role", and "debug". Preserved logic for `config.txt` modification and `QOpenHD.conf` copying. ---------
1 parent 77df823 commit 0da659a

2 files changed

Lines changed: 195 additions & 244 deletions

File tree

src/configure.qml

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -807,42 +807,40 @@ ImButton {
807807
mode = ""
808808
qopenhdConfPresent = false
809809

810-
var airFile = drivePath("air.txt")
811-
var groundFile = drivePath("ground.txt")
812-
if (imageWriter.fileExists(airFile)) {
810+
var settingsJson = imageWriter.readTextFile(drivePath("settings.json"))
811+
var settingsObj = {}
812+
if (settingsJson && settingsJson.length > 0) {
813+
try {
814+
settingsObj = JSON.parse(settingsJson)
815+
} catch (e) {
816+
console.log("[Configure] Error parsing settings.json:", e)
817+
}
818+
}
819+
820+
if (settingsObj.role === "air") {
813821
bootType = "Air"
814-
} else if (imageWriter.fileExists(groundFile)) {
822+
} else if (settingsObj.role === "ground") {
815823
bootType = "Ground"
816824
} else if (settingsMap.bootType && settingsMap.bootType.options && settingsMap.bootType.options.length > 0) {
817825
bootType = settingsMap.bootType.options[0].id
818826
}
819827

820-
// Detect SBC marker
821-
if (settingsMap.sbc && settingsMap.sbc.options) {
822-
for (var i = 0; i < settingsMap.sbc.options.length; i++) {
823-
var opt = settingsMap.sbc.options[i]
824-
var optPath = drivePath(opt.file)
825-
if (imageWriter.fileExists(optPath)) {
826-
sbc = opt.id
827-
break
828-
}
829-
}
830-
if (!sbc && settingsMap.sbc.options.length > 0) {
831-
sbc = settingsMap.sbc.options[0].id
832-
}
828+
if (settingsObj.sbc) {
829+
sbc = settingsObj.sbc
830+
} else if (settingsMap.sbc && settingsMap.sbc.options && settingsMap.sbc.options.length > 0) {
831+
sbc = settingsMap.sbc.options[0].id
833832
}
834833

835-
if (imageWriter.fileExists(drivePath("debug.txt"))) {
834+
if (settingsObj.debug) {
836835
mode = "debug"
837836
setDebug.checked = true
838837
} else {
839838
mode = ""
840839
setDebug.checked = false
841840
}
842841

843-
var cameraValue = imageWriter.readTextFile(drivePath("camera1.txt")).trim()
844-
if (cameraValue.length > 0) {
845-
setCameraFromValue(cameraValue)
842+
if (settingsObj.camera) {
843+
setCameraFromValue(settingsObj.camera)
846844
} else {
847845
camera = ""
848846
}
@@ -870,36 +868,44 @@ ImButton {
870868

871869
console.log("[Configure] Writing settings to", openhdRoot)
872870

873-
var bootFile = bootType === "Air" ? "air.txt" : (bootType === "Ground" ? "ground.txt" : "")
874-
if (bootFile) {
875-
imageWriter.writeTextFile(drivePath(bootFile), "")
876-
var otherBoot = bootType === "Air" ? "ground.txt" : "air.txt"
877-
imageWriter.removeFile(drivePath(otherBoot))
871+
var settingsObj = {}
872+
if (bootType === "Air") {
873+
settingsObj.role = "air"
874+
} else if (bootType === "Ground") {
875+
settingsObj.role = "ground"
878876
}
879877

880-
if (settingsMap.sbc && settingsMap.sbc.options) {
881-
for (var i = 0; i < settingsMap.sbc.options.length; i++) {
882-
var opt = settingsMap.sbc.options[i]
883-
var targetPath = drivePath(opt.file)
884-
if (opt.id === sbc) {
885-
imageWriter.writeTextFile(targetPath, "")
886-
} else {
887-
imageWriter.removeFile(targetPath)
888-
}
889-
}
878+
if (sbc && sbc.length > 0) {
879+
settingsObj.sbc = sbc
890880
}
891881

892882
if (mode === "debug") {
893-
imageWriter.writeTextFile(drivePath("debug.txt"), "")
894-
} else {
895-
imageWriter.removeFile(drivePath("debug.txt"))
883+
settingsObj.debug = true
896884
}
897885

898886
var camValue = cameraValueForSelection()
899887
if (camValue && camValue.length > 0) {
900-
imageWriter.writeTextFile(drivePath("camera1.txt"), camValue)
901-
} else {
888+
settingsObj.camera = camValue
889+
}
890+
891+
var jsonString = JSON.stringify(settingsObj, null, 4)
892+
if (imageWriter.writeTextFile(drivePath("settings.json"), jsonString)) {
893+
// Clean up old files if they exist, just in case
894+
imageWriter.removeFile(drivePath("air.txt"))
895+
imageWriter.removeFile(drivePath("ground.txt"))
896+
imageWriter.removeFile(drivePath("debug.txt"))
902897
imageWriter.removeFile(drivePath("camera1.txt"))
898+
// We don't easily know which SBC file might exist, so we might skip cleaning those up
899+
// or iterate through sbc options to delete them. For now, assuming fresh flash or JSON usage.
900+
if (settingsMap.sbc && settingsMap.sbc.options) {
901+
for (var i = 0; i < settingsMap.sbc.options.length; i++) {
902+
var opt = settingsMap.sbc.options[i]
903+
imageWriter.removeFile(drivePath(opt.file))
904+
}
905+
}
906+
} else {
907+
onError(qsTr("Failed to write settings.json to the drive."))
908+
return
903909
}
904910

905911
if (qopenhdConfPath && qopenhdConfPath.length > 0) {

0 commit comments

Comments
 (0)