Cleanup formatting inconsistencies

This commit is contained in:
Youssof 2024-10-24 16:50:04 +00:00
parent 0cfd11ca93
commit 0a55ee1fea
1 changed files with 92 additions and 134 deletions

View File

@ -34,109 +34,89 @@ document.onload = InitPage();
// Save file button // Save file button
document.getElementById("save").addEventListener("click", SaveSettings); document.getElementById("save").addEventListener("click", SaveSettings);
// This attempts to read the conf file, if it exists, then it will parse it and fill out the table // This attempts to read the conf file, if it exists, then it will parse it and fill out the table
// if it fails then the values are loaded with defaults. // if it fails then the values are loaded with defaults.
function InitPage() { function InitPage() {
cockpit.script(scriptLocation + "cockpitScript.sh -v") cockpit.script(scriptLocation + "cockpitScript.sh -v")
.then((content) => version.innerHTML=content) .then((content) => version.innerHTML=content)
.catch(error => Fail(error)); .catch(error => Fail(error));
cockpit.script(scriptLocation + "cockpitScript.sh -u") cockpit.script(scriptLocation + "cockpitScript.sh -u")
.then(function(content) { .then(function(content) {
ipsubnet1.innerHTML=content; ipsubnet1.innerHTML=content;
ipsubnet2.innerHTML=content; ipsubnet2.innerHTML=content;
}) })
.catch(error => Fail(error)); .catch(error => Fail(error));
file_location.innerHTML = confLocation + "main.conf"; file_location.innerHTML = confLocation + "main.conf";
cockpit.file(confLocation + "main.conf") cockpit.file(confLocation + "main.conf")
.read().then((content, tag) => SuccessReadFile(content)) .read().then((content, tag) => SuccessReadFile(content))
.catch(error => FailureReadFile(error)); .catch(error => FailureReadFile(error));
}
} function getValueByKey(text, sectionName, sectionLength, key) {
function getValueByKey(text, sectionName, sectionLength, key){
//function to extract key values from a file with sections //function to extract key values from a file with sections
//this should work if the section is commented out or not //this should work if the section is commented out or not
var lines = text.split("\n"); var lines = text.split("\n");
var sectionBody = ""; var sectionBody = "";
for(let t = 0; t < lines.length; t++){ for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName || lines[t] === "#" + sectionName) if (lines[t] === sectionName || lines[t] === "#" + sectionName) {
{
for(let n = 0; n < sectionLength; n++){ for(let n = 0; n < sectionLength; n++){
if (lines[t].startsWith("#")) if (lines[t].startsWith("#"))
{
sectionBody += lines[t+n+1].slice(1) + "\n"; sectionBody += lines[t+n+1].slice(1) + "\n";
}
else else
sectionBody += lines[t+n+1] + "\n"; sectionBody += lines[t+n+1] + "\n";
} }
break; break;
} }
} }
if (sectionBody === "") if (sectionBody === "")
return null; return null;
var regex = new RegExp("^" + key + " =(.*)$", "m"); var regex = new RegExp("^" + key + " =(.*)$", "m");
var match = regex.exec(sectionBody); var match = regex.exec(sectionBody);
if(match) if (match)
return match[1].trim(); return match[1].trim();
else else
return null; return null;
} }
function isSectionEnabled(text, sectionName) function isSectionEnabled(text, sectionName) {
{
var lines = text.split("\n"); var lines = text.split("\n");
for(let t = 0; t < lines.length; t++){ for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName) if (lines[t] === sectionName) {
{ if (!lines[t].startsWith("#")) {
if (!lines[t].startsWith("#"))
{
return "Enabled"; return "Enabled";
} }
return "Disabled"; return "Disabled";
break;
} }
} }
return "Disabled"; return "Disabled";
} }
function setSectionEnabled(text, sectionName, sectionLength, enabled) function setSectionEnabled(text, sectionName, sectionLength, enabled) {
{
//if enabled, find the section and remove leading # //if enabled, find the section and remove leading #
//if disabled, find the section and add leading # //if disabled, find the section and add leading #
//return content //return content
var lines = text.split("\n"); var lines = text.split("\n");
if (enabled) if (enabled) {
{
//do action to enable the section //do action to enable the section
for(let t = 0; t < lines.length; t++){ for(let t = 0; t < lines.length; t++){
if (lines[t] === "#" + sectionName) if (lines[t] === "#" + sectionName) {
{ for(let n = 0; n < sectionLength+1; n++){
for(let n = 0; n < sectionLength+1; n++){ if (lines[t+n].startsWith("#")) {
if (lines[t+n].startsWith("#")) lines[t+n] = lines[t+n].slice(1);
{ }
lines[t+n] = lines[t+n].slice(1); }
}
}
break; break;
} }
} }
} else {
}
else
{
//do action to disable the section //do action to disable the section
for(let t = 0; t < lines.length; t++){ for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName) if (lines[t] === sectionName) {
{
for(let n = 0; n < sectionLength+1; n++){ for(let n = 0; n < sectionLength+1; n++){
lines[t+n] = "#" + lines[t+n]; lines[t+n] = "#" + lines[t+n];
} }
break; break;
} }
@ -157,11 +137,9 @@ function setValueByKey(text, sectionName, sectionLength, key, value)
var lines = text.split("\n"); var lines = text.split("\n");
for(let t = 0; t < lines.length; t++){ for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName) if (lines[t] === sectionName) {
{
for(let n = 0; n < sectionLength; n++){ for(let n = 0; n < sectionLength; n++){
if (lines[t+n+1].split('=')[0].trim() == key) if (lines[t+n+1].split('=')[0].trim() == key) {
{
lines[t+n+1] = key + " = " + value; lines[t+n+1] = key + " = " + value;
break; break;
} }
@ -170,13 +148,11 @@ function setValueByKey(text, sectionName, sectionLength, key, value)
} }
return lines.join("\n"); return lines.join("\n");
} }
function SuccessReadFile(content) { function SuccessReadFile(content) {
try{ try {
//FMU Endpoint //FMU Endpoint
var currentfmuDevice = getValueByKey(content, "[UartEndpoint alpha]", 2, "Device"); var currentfmuDevice = getValueByKey(content, "[UartEndpoint alpha]", 2, "Device");
var currentbaudRate = getValueByKey(content, "[UartEndpoint alpha]", 2, "Baud"); var currentbaudRate = getValueByKey(content, "[UartEndpoint alpha]", 2, "Baud");
@ -194,8 +170,8 @@ function SuccessReadFile(content) {
losPort.value = getValueByKey(content, "[UdpEndpoint alpha]", 3, "Port"); losPort.value = getValueByKey(content, "[UdpEndpoint alpha]", 3, "Port");
$(udpStatus).change(function() { $(udpStatus).change(function() {
if ($(this).val() === null) if ($(this).val() === null) return;
return;
if ($(this).val() == "Disabled") { if ($(this).val() == "Disabled") {
isUdpEnabled = "Disabled"; isUdpEnabled = "Disabled";
$(udpMode).attr("disabled", "disabled"); $(udpMode).attr("disabled", "disabled");
@ -207,16 +183,13 @@ function SuccessReadFile(content) {
$(losHost).removeAttr("disabled"); $(losHost).removeAttr("disabled");
$(losPort).removeAttr("disabled"); $(losPort).removeAttr("disabled");
} }
}).trigger("change"); }).trigger("change");
AddDropDown(udpMode, udpModeArray, currentudpMode); AddDropDown(udpMode, udpModeArray, currentudpMode);
AddDropDown(udpStatus, udpStatusArray, isUdpEnabled); AddDropDown(udpStatus, udpStatusArray, isUdpEnabled);
// init state of UDP fields // init state of UDP fields
if (isUdpEnabled === "Disabled") if (isUdpEnabled === "Disabled") {
{
$(udpMode).attr("disabled", "disabled"); $(udpMode).attr("disabled", "disabled");
$(losHost).attr("disabled", "disabled"); $(losHost).attr("disabled", "disabled");
$(losPort).attr("disabled", "disabled"); $(losPort).attr("disabled", "disabled");
@ -228,10 +201,9 @@ function SuccessReadFile(content) {
tcpHost.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Address"); tcpHost.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Address");
tcpPort.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Port"); tcpPort.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Port");
$(tcpStatus).change(function() { $(tcpStatus).change(function() {
if ($(this).val() === null) if ($(this).val() === null) return;
return;
if ($(this).val() == "Disabled") { if ($(this).val() == "Disabled") {
istcpEnabled = "Disabled"; istcpEnabled = "Disabled";
$(tcpHost).attr("disabled", "disabled"); $(tcpHost).attr("disabled", "disabled");
@ -241,45 +213,41 @@ function SuccessReadFile(content) {
$(tcpHost).removeAttr("disabled"); $(tcpHost).removeAttr("disabled");
$(tcpPort).removeAttr("disabled"); $(tcpPort).removeAttr("disabled");
} }
}).trigger("change"); }).trigger("change");
AddDropDown(tcpStatus, tcpStatusArray, istcpEnabled); AddDropDown(tcpStatus, tcpStatusArray, istcpEnabled);
// init state of TCP fields // init state of TCP fields
if (istcpEnabled === "Disabled") if (istcpEnabled === "Disabled") {
{
$(tcpHost).attr("disabled", "disabled"); $(tcpHost).attr("disabled", "disabled");
$(tcpPort).attr("disabled", "disabled"); $(tcpPort).attr("disabled", "disabled");
} }
} }
catch(e){ catch(e) {
FailureReadFile(e); FailureReadFile(e);
} }
} }
function AddPathToDeviceFile(incomingArray){ function AddPathToDeviceFile(incomingArray) {
for(let t = 0; t < incomingArray.length; t++){ for(let t = 0; t < incomingArray.length; t++){
incomingArray[t] = "/dev/" + incomingArray[t]; incomingArray[t] = "/dev/" + incomingArray[t];
} }
return incomingArray; return incomingArray;
} }
function AddDropDown(box, theArray, defaultValue){ function AddDropDown(box, theArray, defaultValue) {
try{ try {
for(let t = 0; t < theArray.length; t++){ for(let t = 0; t < theArray.length; t++){
var option = document.createElement("option"); var option = document.createElement("option");
option.text = theArray[t]; option.text = theArray[t];
box.add(option); box.add(option);
if(defaultValue == option.text){ if(defaultValue == option.text) {
box.value = option.text; box.value = option.text;
} }
} }
} }
catch(e){ catch(e) {
Fail(e) Fail(e)
} }
} }
@ -300,91 +268,80 @@ function FailureReadFile(error) {
function SaveSettings() { function SaveSettings() {
//lets do some validation //lets do some validation
var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
var portformat = /^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
var errorFlag = false;
var errorText = "";
if (!losHost.value.match(ipformat) && (isUdpEnabled==="Enabled")) {
losHost.focus();
errorText += "Error in the UDP Host Address!<br>";
errorFlag = true;
}
if (!losPort.value.match(portformat) && (isUdpEnabled==="Enabled")) {
losPort.focus();
errorText += "Error in the UDP Port Number! (0-65535 allowed)<br>";
errorFlag = true;
}
if (!tcpHost.value.match(ipformat) && (istcpEnabled==="Enabled")) {
tcpHost.focus();
errorText += "Error in the TCP Host Address, should be x.x.x.x<br>";
errorFlag = true;
}
if (!tcpPort.value.match(portformat) && (istcpEnabled==="Enabled")) {
tcpPort.focus();
errorText += "Error in the TCP Port Number! (0-65535 allowed)<br>";
errorFlag = true;
}
if (errorFlag)
{
result.style.color = "red";
result.innerHTML = errorText;
return;
}
//open the file for writing, and callback function for modification var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
var portformat = /^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
var errorFlag = false;
var errorText = "";
cockpit.file(confLocation + "main.conf") if (!losHost.value.match(ipformat) && (isUdpEnabled==="Enabled")) {
losHost.focus();
errorText += "Error in the UDP Host Address!<br>";
errorFlag = true;
}
if (!losPort.value.match(portformat) && (isUdpEnabled==="Enabled")) {
losPort.focus();
errorText += "Error in the UDP Port Number! (0-65535 allowed)<br>";
errorFlag = true;
}
if (!tcpHost.value.match(ipformat) && (istcpEnabled==="Enabled")) {
tcpHost.focus();
errorText += "Error in the TCP Host Address, should be x.x.x.x<br>";
errorFlag = true;
}
if (!tcpPort.value.match(portformat) && (istcpEnabled==="Enabled")) {
tcpPort.focus();
errorText += "Error in the TCP Port Number! (0-65535 allowed)<br>";
errorFlag = true;
}
if (errorFlag) {
result.style.color = "red";
result.innerHTML = errorText;
return;
}
//open the file for writing, and callback function for modification
cockpit.file(confLocation + "main.conf")
.read().then((content, tag) => SuccessReadforSaveFile(content)) .read().then((content, tag) => SuccessReadforSaveFile(content))
.catch(error => FailureReadFile(error)); .catch(error => FailureReadFile(error));
} }
function SuccessReadforSaveFile(content) { function SuccessReadforSaveFile(content) {
try{ try {
//if udp is disabled, then skip those //if udp is disabled, then skip those
if (isUdpEnabled == "Disabled") if (isUdpEnabled == "Disabled") {
{
content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, false); content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, false);
} } else {
else
{
content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, true); content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, true);
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Address", losHost.value ) content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Address", losHost.value )
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Port", losPort.value ) content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Port", losPort.value )
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Mode", udpMode.value ) content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Mode", udpMode.value )
} }
if (istcpEnabled == "Disabled") if (istcpEnabled == "Disabled") {
{
content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, false); content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, false);
} } else {
else
{
content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, true); content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, true);
content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Address", tcpHost.value ) content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Address", tcpHost.value )
content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Port", tcpPort.value ) content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Port", tcpPort.value )
} }
//at this point we have the contents of the file, we need to replace keys //at this point we have the contents of the file, we need to replace keys
content = setValueByKey(content, "[UartEndpoint alpha]",2, "Device", fmuDevice.value ) content = setValueByKey(content, "[UartEndpoint alpha]",2, "Device", fmuDevice.value )
content = setValueByKey(content, "[UartEndpoint alpha]",2, "Baud", baudrate.value ) content = setValueByKey(content, "[UartEndpoint alpha]",2, "Baud", baudrate.value )
cockpit.file(confLocation + "main.conf", { superuser : "try" }).replace(content) cockpit.file(confLocation + "main.conf", { superuser : "try" }).replace(content)
.then(Success) .then(Success)
.catch(Fail); .catch(Fail);
cockpit.spawn(["systemctl", "restart", "mavlink-router"], { superuser : "try" }); cockpit.spawn(["systemctl", "restart", "mavlink-router"], { superuser : "try" });
} }
catch(e){ catch(e) {
FailureReadFile(e); FailureReadFile(e);
} }
} }
function Success() { function Success() {
@ -397,5 +354,6 @@ function Fail(error) {
result.style.color = "red"; result.style.color = "red";
result.innerHTML = error.message; result.innerHTML = error.message;
} }
// Send a 'init' message. This tells integration tests that we are ready to go // Send a 'init' message. This tells integration tests that we are ready to go
cockpit.transport.wait(function() { }); cockpit.transport.wait(function() { });