From d500b36a19ab556f5591a69310c184efadb18c81 Mon Sep 17 00:00:00 2001 From: cam Date: Wed, 25 Sep 2024 09:23:28 +1200 Subject: [PATCH 1/3] Add comms to config page and small code refactor --- api/api.go | 53 +++----- go.mod | 6 +- go.sum | 12 +- html/about.html | 2 +- html/config.html | 80 ++++++++++++ static/js/config.js | 308 ++++++++++++++++++-------------------------- static/js/signal.js | 8 +- 7 files changed, 237 insertions(+), 232 deletions(-) diff --git a/api/api.go b/api/api.go index ef54283..572d639 100644 --- a/api/api.go +++ b/api/api.go @@ -37,7 +37,6 @@ import ( goapi "github.com/TheCacophonyProject/go-api" goconfig "github.com/TheCacophonyProject/go-config" - "github.com/TheCacophonyProject/lepton3" signalstrength "github.com/TheCacophonyProject/management-interface/signal-strength" saltrequester "github.com/TheCacophonyProject/salt-updater" "github.com/godbus/dbus" @@ -399,10 +398,12 @@ func (api *ManagementAPI) SetConfig(w http.ResponseWriter, r *http.Request) { newConfigRaw := r.FormValue("config") newConfig := map[string]interface{}{} if err := json.Unmarshal([]byte(newConfigRaw), &newConfig); err != nil { + log.Printf("Error with unmarshal: %s", err) badRequest(&w, err) return } if err := api.config.SetFromMap(section, newConfig, false); err != nil { + log.Printf("Error with SetFromMap: %s", err) badRequest(&w, err) return } @@ -416,50 +417,26 @@ func (api *ManagementAPI) GetConfig(w http.ResponseWriter, r *http.Request) { return } - configDefaults := map[string]interface{}{ - goconfig.AudioRecordingKey: goconfig.DefaultAudioRecording(), - goconfig.AudioBaitKey: goconfig.DefaultAudioBait(), - goconfig.GPIOKey: goconfig.DefaultGPIO(), - goconfig.LeptonKey: goconfig.DefaultLepton(), - goconfig.ModemdKey: goconfig.DefaultModemd(), - goconfig.PortsKey: goconfig.DefaultPorts(), - goconfig.TestHostsKey: goconfig.DefaultTestHosts(), - goconfig.ThermalMotionKey: goconfig.DefaultThermalMotion(lepton3.Model35), // TODO don't assume that model 3.5 is being used - goconfig.ThermalRecorderKey: goconfig.DefaultThermalRecorder(), - goconfig.ThermalThrottlerKey: goconfig.DefaultThermalThrottler(), - goconfig.WindowsKey: goconfig.DefaultWindows(), - } - - configValues := map[string]interface{}{ - goconfig.AudioRecordingKey: &goconfig.AudioRecording{}, - goconfig.AudioBaitKey: &goconfig.AudioBait{}, - goconfig.GPIOKey: &goconfig.GPIO{}, - goconfig.LeptonKey: &goconfig.Lepton{}, - goconfig.ModemdKey: &goconfig.Modemd{}, - goconfig.PortsKey: &goconfig.Ports{}, - goconfig.TestHostsKey: &goconfig.TestHosts{}, - goconfig.ThermalMotionKey: &goconfig.ThermalMotion{}, - goconfig.ThermalRecorderKey: &goconfig.ThermalRecorder{}, - goconfig.ThermalThrottlerKey: &goconfig.ThermalThrottler{}, - goconfig.WindowsKey: &goconfig.Windows{}, - } - - for section, sectionStruct := range configValues { - if err := api.config.Unmarshal(section, sectionStruct); err != nil { - serverError(&w, err) - return - } + defaultValues := map[string]interface{}{} + for k, v := range goconfig.GetDefaults() { + defaultValues[toCamelCase(k)] = v + } + + values, err := api.config.GetAllValues() + if err != nil { + serverError(&w, err) + return } configValuesCC := map[string]interface{}{} - for k, v := range configValues { + for k, v := range values { configValuesCC[toCamelCase(k)] = v } - configValues = configValuesCC + values = configValuesCC valuesAndDefaults := map[string]interface{}{ - "values": configValues, - "defaults": configDefaults, + "values": values, + "defaults": defaultValues, } jsonString, err := json.Marshal(valuesAndDefaults) diff --git a/go.mod b/go.mod index cc54b73..27a02b9 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.22.3 require ( github.com/TheCacophonyProject/audiobait/v3 v3.0.1 github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231 - github.com/TheCacophonyProject/go-api v1.2.0 - github.com/TheCacophonyProject/go-config v1.18.0 + github.com/TheCacophonyProject/go-api v1.2.1 + github.com/TheCacophonyProject/go-config v1.22.0 github.com/TheCacophonyProject/go-cptv v0.0.0-20211109233846-8c32a5d161f7 github.com/TheCacophonyProject/lepton3 v0.0.0-20211005194419-22311c15d6ee github.com/TheCacophonyProject/rtc-utils v1.2.0 @@ -30,7 +30,7 @@ require ( ) require ( - github.com/TheCacophonyProject/event-reporter/v3 v3.7.0 // indirect + github.com/TheCacophonyProject/event-reporter/v3 v3.8.0 // indirect github.com/TheCacophonyProject/window v0.0.0-20200312071457-7fc8799fdce7 // indirect github.com/alexflint/go-scalar v1.1.0 // indirect github.com/boltdb/bolt v1.3.1 // indirect diff --git a/go.sum b/go.sum index ea4327c..936ce55 100644 --- a/go.sum +++ b/go.sum @@ -53,19 +53,19 @@ github.com/TheCacophonyProject/audiobait/v3 v3.0.1/go.mod h1:EDM0fyF6oHPUmtOfY4Q github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231 h1:nLqfSx3zDBzghlP/S8X2j/WRtW0e3BZvSkMm1J/Fa8U= github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231/go.mod h1:kei6S/4x+VHp5yiwYPqvcNnhuRpZ8iLsh95Sue6kPvc= github.com/TheCacophonyProject/event-reporter/v3 v3.3.0/go.mod h1:dGIYfhABsJHKjcsxtftDwpdcfLOWTYKeIyCYxCOIMrc= -github.com/TheCacophonyProject/event-reporter/v3 v3.7.0 h1:B+a5PXqBh7Bkf2HDo+j94Ascqw6SCTlOhSN9TXZfjY8= -github.com/TheCacophonyProject/event-reporter/v3 v3.7.0/go.mod h1:WTppJtTBxduasM1Or5SAh4Mm0YrTDnprOChjnGYgyEI= +github.com/TheCacophonyProject/event-reporter/v3 v3.8.0 h1:FYNR+MX5ypyG7UWpogDnDqsTV+23WS1sIr9o6rhjf/8= +github.com/TheCacophonyProject/event-reporter/v3 v3.8.0/go.mod h1:WTppJtTBxduasM1Or5SAh4Mm0YrTDnprOChjnGYgyEI= github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c/go.mod h1:FfMpa4cFhNXQ9tuKG18HO6yLExezcJhzjUjBOFocrQw= github.com/TheCacophonyProject/go-api v1.0.4/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8= -github.com/TheCacophonyProject/go-api v1.2.0 h1:5cEUlv3Xe8T4SuLmIPK2ppkPt/Mi41OSvp/vCPPUxuY= -github.com/TheCacophonyProject/go-api v1.2.0/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8= +github.com/TheCacophonyProject/go-api v1.2.1 h1:QS8mzStQHzb3oAqqwjoLqCvXsRM8fx7F9I7hpgKND5c= +github.com/TheCacophonyProject/go-api v1.2.1/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8= github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88/go.mod h1:gPUJLVu408NRz9/P3BrsxzOzLc+KJLrv+jVdDw3RI0Y= github.com/TheCacophonyProject/go-config v0.0.0-20190927054511-c93578ae648a/go.mod h1:QCgT+KCrz1CmLVpeeOMl5L8/X1QvWwpsLzR7afTmEJc= github.com/TheCacophonyProject/go-config v1.4.0/go.mod h1:oARW/N3eJbcewCqB+Jc7TBwuODawwYgpo56UO6yBdKU= github.com/TheCacophonyProject/go-config v1.6.4/go.mod h1:eDQfBjmTDh/l+2QLBgsotmJFd/1x/7w4SwwBUxMM86w= github.com/TheCacophonyProject/go-config v1.9.0/go.mod h1:+y80PSRZudMYuVrYTGOvzc66NxVJWKS4TuU442vmvhY= -github.com/TheCacophonyProject/go-config v1.18.0 h1:R3zOjfHBxsI8t5J7PSuv6Wb5xwj2LPMC8vqD+d5Azd8= -github.com/TheCacophonyProject/go-config v1.18.0/go.mod h1:XZwQmNl2wQXhYR18RQtwZ6LwFwgAx73yzJfymYLz68s= +github.com/TheCacophonyProject/go-config v1.22.0 h1:ElKhWV4w0m4xX59h6ORrSDsAkBsAScJskrouCjKchqY= +github.com/TheCacophonyProject/go-config v1.22.0/go.mod h1:Nf1iBI/gqYf7UCcUZezO7bj9hzY2jRPdzDW+kOI9/Fo= github.com/TheCacophonyProject/go-cptv v0.0.0-20200116020937-858bd8b71512/go.mod h1:8H6Aaft5549sIWxcsuCIL2o60/TQkoF93fVoSTpgZb8= github.com/TheCacophonyProject/go-cptv v0.0.0-20200616224711-fc633122087a/go.mod h1:Vg73Ezn4kr8qDNP9LNgjki9qgi+5T/0Uc9oDyflaYUY= github.com/TheCacophonyProject/go-cptv v0.0.0-20200818214604-bd5d4aa36043/go.mod h1:wG4/P/TsGtk33uBClYPjRlSbcdQrIASXutOUV8LMn2o= diff --git a/html/about.html b/html/about.html index 0be17d9..8000410 100644 --- a/html/about.html +++ b/html/about.html @@ -96,7 +96,7 @@

Device

Previous Output:

-

+

         

Previous Success:

diff --git a/html/config.html b/html/config.html index 164a2f8..cd0e2a3 100644 --- a/html/config.html +++ b/html/config.html @@ -114,7 +114,87 @@

Thermal Motion

+ +
+

Comms port

+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Enable
Trap Default State
Bluetooth Enablued
Power Output +
+ +
+
Power On Duration
Comms Type +
+ +
+
Trap Species
Trap Duration
Protect Species
Protect Species
+
+
+
+
+ +
+
diff --git a/static/js/config.js b/static/js/config.js index 9e08633..528efcc 100644 --- a/static/js/config.js +++ b/static/js/config.js @@ -1,210 +1,152 @@ "use strict"; -window.onload = function () { - loadConfig(); + +window.onload = async function () { + await loadConfig(); }; -function loadConfig() { - var xmlHttp = new XMLHttpRequest(); - xmlHttp.open("GET", "/api/config", true); - xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers")); - - xmlHttp.onload = async function () { - if (xmlHttp.status == 200) { - var response = JSON.parse(xmlHttp.response); - console.log(response); - $("#input-start-recording").attr( - "placeholder", - response.defaults.windows.StartRecording - ); - $("#input-stop-recording").attr( - "placeholder", - response.defaults.windows.StopRecording - ); - $("#input-power-on").attr( - "placeholder", - response.defaults.windows.PowerOn - ); - $("#input-power-off").attr( - "placeholder", - response.defaults.windows.PowerOff - ); - - $("#input-initial-on-duration").attr( - "placeholder", - formatDuration(response.defaults.modemd.InitialOnDuration) - ); - $("#input-find-modem-timeout").attr( - "placeholder", - formatDuration(response.defaults.modemd.FindModemTimeout) - ); - $("#input-connection-timeout").attr( - "placeholder", - formatDuration(response.defaults.modemd.ConnectionTimeout) - ); - - console.log(response.values.windows.StartRecording); - $("#input-start-recording").val(response.values.windows.StartRecording); - $("#input-stop-recording").val(response.values.windows.StopRecording); - $("#input-power-on").val(response.values.windows.PowerOn); - $("#input-power-off").val(response.values.windows.PowerOff); - if (response.values.modemd.InitialOnDuration != 0) { - $("#input-initial-on-duration").val( - formatDuration(response.values.modemd.InitialOnDuration) - ); - } - if (response.values.modemd.FindModemTimeout != 0) { - $("#input-find-modem-timeout").val( - formatDuration(response.values.modemd.FindModemTimeout) - ); - } - if (response.values.modemd.ConnectionTimeout != 0) { - $("#input-connection-timeout").val( - formatDuration(response.values.modemd.ConnectionTimeout) - ); - } - $("#input-do-tracking").prop( - "checked", - response.values.thermalMotion.DoTracking - ); - $("#input-run-classifier").prop( - "checked", - response.values.thermalMotion.RunClassifier - ); - $("#input-tracking-events").prop( - "checked", - response.values.thermalMotion.TrackingEvents - ); - } else { - console.log("error with getting device details"); - } - }; +async function loadConfig() { + try { + const response = await fetch("/api/config", { + headers: { + Authorization: "Basic " + btoa("admin:feathers"), + }, + }); - xmlHttp.onerror = async function () { - console.log("error with getting device config"); - }; + if (!response.ok) { + throw new Error("Failed to load config"); + } - xmlHttp.send(null); + const data = await response.json(); + console.log(data); + + // Set placeholders and values for windows + document.querySelector("#input-start-recording").placeholder = data.defaults.windows.StartRecording; + document.querySelector("#input-stop-recording").placeholder = data.defaults.windows.StopRecording; + document.querySelector("#input-power-on").placeholder = data.defaults.windows.PowerOn; + document.querySelector("#input-power-off").placeholder = data.defaults.windows.PowerOff; + + document.querySelector("#input-start-recording").value = data.values.windows.StartRecording; + document.querySelector("#input-stop-recording").value = data.values.windows.StopRecording; + document.querySelector("#input-power-on").value = data.values.windows.PowerOn; + document.querySelector("#input-power-off").value = data.values.windows.PowerOff; + + // Set placeholders and values for modem + document.querySelector("#input-initial-on-duration").placeholder = formatDuration(data.defaults.modemd.InitialOnDuration); + document.querySelector("#input-find-modem-timeout").placeholder = formatDuration(data.defaults.modemd.FindModemTimeout); + document.querySelector("#input-connection-timeout").placeholder = formatDuration(data.defaults.modemd.ConnectionTimeout); + + document.querySelector("#input-initial-on-duration").value = formatDuration(data.values.modemd.InitialOnDuration); + document.querySelector("#input-find-modem-timeout").value = formatDuration(data.values.modemd.FindModemTimeout); + document.querySelector("#input-connection-timeout").value = formatDuration(data.values.modemd.ConnectionTimeout); + + // Set values for thermal motion + document.querySelector("#input-do-tracking").checked = data.values.thermalMotion.DoTracking; + document.querySelector("#input-run-classifier").checked = data.values.thermalMotion.RunClassifier; + document.querySelector("#input-tracking-events").checked = data.values.thermalMotion.TrackingEvents; + + // Set values for comms + document.querySelector("#input-comms-enable").checked = data.values.comms.Enable; + document.querySelector("#input-comms-trap-default").checked = data.values.comms.TrapEnabledByDefault; + document.querySelector("#input-comms-bluetooth").checked = data.values.comms.Bluetooth; + document.querySelector("#input-comms-power-output").value = data.values.comms.PowerOutput; + document.querySelector("#input-comms-power-up-duration").value = formatDuration(data.values.comms.PowerUpDuration); + document.querySelector("#input-comms-power-up-duration").placeholder = formatDuration(data.defaults.comms.PowerUpDuration); + document.querySelector("#input-comms-type-select").value = data.values.comms.CommsOut; + document.querySelector("#input-comms-trap-species").value = JSON.stringify(data.values.comms.TrapSpecies); + document.querySelector("#input-comms-trap-duration").value = formatDuration(data.values.comms.TrapDuration); + document.querySelector("#input-comms-trap-duration").placeholder = formatDuration(data.defaults.comms.TrapDuration); + document.querySelector("#input-comms-protect-species").value = JSON.stringify(data.values.comms.ProtectSpecies); + document.querySelector("#input-comms-protect-duration").value = formatDuration(data.values.comms.ProtectDuration); + document.querySelector("#input-comms-protect-duration").placeholder = formatDuration(data.defaults.comms.ProtectDuration); + + } catch (error) { + console.error("Error loading config:", error); + alert("Error loading config"); + } } function formatDuration(nanoseconds) { - var seconds = Math.floor(nanoseconds / 1000_000_000); - var hours = Math.floor(seconds / 3600); - var minutes = Math.floor((seconds % 3600) / 60); - seconds = seconds % 60; - - var result = ""; - if (hours > 0) { - result += hours + "h"; - } - if (minutes > 0) { - result += minutes + "m"; + if (!nanoseconds) { + return ""; } - result += seconds + "s"; + const seconds = Math.floor(nanoseconds / 1_000_000_000); + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + const remainingSeconds = seconds % 60; - return result; + return `${hours > 0 ? `${hours}h` : ""}${minutes > 0 ? `${minutes}m` : ""}${remainingSeconds}s`; } -function saveWindowsConfig() { - var xmlHttp = new XMLHttpRequest(); - xmlHttp.open("POST", "/api/config", true); - xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers")); - xmlHttp.onload = async function () { - if (xmlHttp.status == 200) { - alert("Config saved"); - loadConfig(); - } else { - configError(); - } - }; - xmlHttp.onerror = async function () { - configError(); +async function saveWindowsConfig() { + const data = { + "start-recording": document.querySelector("#input-start-recording").value || undefined, + "stop-recording": document.querySelector("#input-stop-recording").value || undefined, + "power-on": document.querySelector("#input-power-on").value || undefined, + "power-off": document.querySelector("#input-power-off").value || undefined, }; - var data = {}; - if ($("#input-start-recording").val() != "") { - data["start-recording"] = $("#input-start-recording").val(); - } - if ($("#input-stop-recording").val() != "") { - data["stop-recording"] = $("#input-stop-recording").val(); - } - if ($("#input-power-on").val() != "") { - data["power-on"] = $("#input-power-on").val(); - } - if ($("#input-power-off").val() != "") { - data["power-off"] = $("#input-power-off").val(); - } - - var formData = new FormData(); - formData.append("section", "windows"); - formData.append("config", JSON.stringify(data)); - xmlHttp.send(formData); + await saveConfig("windows", data); } -function saveModemConfig() { - var xmlHttp = new XMLHttpRequest(); - xmlHttp.open("POST", "/api/config", true); - xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers")); - xmlHttp.onload = async function () { - if (xmlHttp.status == 200) { - alert("Modem config saved"); - loadConfig(); - } else { - configError(); - } - }; - xmlHttp.onerror = async function () { - configError(); +async function saveModemConfig() { + const data = { + "initial-on-duration": document.querySelector("#input-initial-on-duration").value || undefined, + "find-modem-timeout": document.querySelector("#input-find-modem-timeout").value || undefined, + "connection-timeout": document.querySelector("#input-connection-timeout").value || undefined, }; - var data = {}; - if ($("#input-initial-on-duration").val() != "") { - data["initial-on-duration"] = $("#input-initial-on-duration").val(); - } - if ($("#input-find-modem-timeout").val() != "") { - data["find-modem-timeout"] = $("#input-find-modem-timeout").val(); - } - if ($("#input-connection-timeout").val() != "") { - data["connection-timeout"] = $("#input-connection-timeout").val(); - } + await saveConfig("modemd", data); +} - var formData = new FormData(); - formData.append("section", "modemd"); - formData.append("config", JSON.stringify(data)); - xmlHttp.send(formData); +async function saveThermalMotionConfig() { + const data = { + "do-tracking": document.querySelector("#input-do-tracking").checked, + "run-classifier": document.querySelector("#input-run-classifier").checked, + "tracking-events": document.querySelector("#input-tracking-events").checked, + }; + + await saveConfig("thermal-motion", data); } -function saveThermalMotionConfig() { - var data = {}; - data["do-tracking"] = $("#input-do-tracking").prop("checked"); - data["run-classifier"] = $("#input-run-classifier").prop("checked"); - data["tracking-events"] = $("#input-tracking-events").prop("checked"); +async function saveCommsConfig() { + const data = { + "enable": document.querySelector("#input-comms-enable").checked, + "trap-enabled-by-default": document.querySelector("#input-comms-trap-default").checked, + "comms-out": document.querySelector("#input-comms-type-select").value || undefined, + "bluetooth": document.querySelector("#input-comms-bluetooth").checked, + "power-output": document.querySelector("#input-comms-power-output").value || undefined, + "power-up-duration": document.querySelector("#input-comms-power-up-duration").value || undefined, + "trap-species": JSON.parse(document.querySelector("#input-comms-trap-species").value) || undefined, + "trap-duration": document.querySelector("#input-comms-trap-duration").value || undefined, + "protect-species": JSON.parse(document.querySelector("#input-comms-protect-species").value) || undefined, + "protect-duration": document.querySelector("#input-comms-protect-duration").value || undefined, + }; + + await saveConfig("comms", data); +} - var formData = new FormData(); - formData.append("section", "thermal-motion"); +async function saveConfig(section, data) { + const formData = new FormData(); + formData.append("section", section); formData.append("config", JSON.stringify(data)); - fetch("/api/config", { - method: "POST", - headers: { - Authorization: "Basic " + btoa("admin:feathers"), - }, - body: formData, - }) - .then((response) => { - if (response.ok) { - alert("Thermal motion config saved"); - loadConfig(); - } else { - configError(); - } - }) - .catch((error) => { - console.error("Error:", error); - configError(); + try { + const response = await fetch("/api/config", { + method: "POST", + headers: { + Authorization: "Basic " + btoa("admin:feathers"), + }, + body: formData, }); -} -function configError() { - loadConfig(); - alert("Error saving config"); + if (response.ok) { + alert(`${section} config saved`); + await loadConfig(); + } else { + throw new Error("Failed to save config"); + } + } catch (error) { + console.error("Error saving config:", error); + alert("Error saving config"); + } } diff --git a/static/js/signal.js b/static/js/signal.js index 2c5af41..e65c23f 100644 --- a/static/js/signal.js +++ b/static/js/signal.js @@ -10,7 +10,13 @@ var refreshTime = refreshMillis; var signalFails = 0; async function updateSignalTC2() { - var response = await apiGetJSON("/api/modem"); + var response; + try { + response = await apiGetJSON("/api/modem"); + } catch (e) { + console.log(e); + return; + } if (response.simCard) { if (response.simCard.simCardStatus == "READY") { From 866d6c94c170e974822e06ae60ed93b0d22c9d86 Mon Sep 17 00:00:00 2001 From: cam Date: Wed, 25 Sep 2024 10:34:28 +1200 Subject: [PATCH 2/3] Fix typo --- html/config.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/config.html b/html/config.html index cd0e2a3..bbb7134 100644 --- a/html/config.html +++ b/html/config.html @@ -134,7 +134,7 @@

Comms port

- Bluetooth Enablued + Bluetooth Enabled From 9a43fe5eb7da59d87a731b6f272d56bc0ae17fe1 Mon Sep 17 00:00:00 2001 From: cam Date: Wed, 25 Sep 2024 10:51:28 +1200 Subject: [PATCH 3/3] better handling of bad JSON comms species --- html/config.html | 2 +- static/js/config.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/html/config.html b/html/config.html index bbb7134..3411ecd 100644 --- a/html/config.html +++ b/html/config.html @@ -182,7 +182,7 @@

Comms port

- Protect Species + Protect Duration diff --git a/static/js/config.js b/static/js/config.js index 528efcc..7579a4d 100644 --- a/static/js/config.js +++ b/static/js/config.js @@ -109,6 +109,18 @@ async function saveThermalMotionConfig() { } async function saveCommsConfig() { + + try { + var protectSpecies = JSON.parse(document.querySelector("#input-comms-protect-species").value) || undefined; + console.log("protectSpecies", protectSpecies); + var trapSpecies = JSON.parse(document.querySelector("#input-comms-trap-species").value) || undefined; + console.log("TrapSpecies", trapSpecies); + } catch (error) { + console.error("Error parsing JSON:", error); + alert("Error parsing JSON for trap or protect species"); + return; + } + const data = { "enable": document.querySelector("#input-comms-enable").checked, "trap-enabled-by-default": document.querySelector("#input-comms-trap-default").checked, @@ -116,9 +128,9 @@ async function saveCommsConfig() { "bluetooth": document.querySelector("#input-comms-bluetooth").checked, "power-output": document.querySelector("#input-comms-power-output").value || undefined, "power-up-duration": document.querySelector("#input-comms-power-up-duration").value || undefined, - "trap-species": JSON.parse(document.querySelector("#input-comms-trap-species").value) || undefined, + "trap-species": trapSpecies, "trap-duration": document.querySelector("#input-comms-trap-duration").value || undefined, - "protect-species": JSON.parse(document.querySelector("#input-comms-protect-species").value) || undefined, + "protect-species": protectSpecies, "protect-duration": document.querySelector("#input-comms-protect-duration").value || undefined, };