Mu2_Deploy/ui/general/health.js

130 lines
5.6 KiB
JavaScript

// Initialize Drone Table Elements
const droneName = document.getElementById("droneName");
const ipAddress = document.getElementById("ipAddress");
const dockerContainer = document.getElementById("dockerContainer");
const connectionUpstream = document.getElementById("connectionUpstream");
const networkSSID = document.getElementById("networkSSID");
const cameraDevices = document.getElementById("cameraDevices");
const connectedDevices = document.getElementById("connectedDevices");
// Load page
document.onload = initPage();
// Function to initialize the page
function initPage() {
// Populate health table with:
// Drone Name
// Drone IP address
// Drone Docker Containers that are running
// Connection Upstream Status
// Network SSID
// Camera Devices List
// Connected Devices List
// Below are the list of Commands used to acquire the information to populate the health table
// Name corresponds to host computer's name
const droneNameCommand = "hostname"
// Host Computer's IP
const ipAddressCommand = "hostname -I"
// List of all Docker Container IDs
const dockerContainerCommand = "docker container ls"
// Ping Google to check upstream connection
const connectionUpstreamCommand = "ping google.com -c 1"
// SSID of the network the Drone is connected to
const networkSSIDCommand = "iwgetid -r"
// Camera devices are those listed by /dev/video*
const cameraDevicesCommand = "ls /dev/video*"
// Connected Devices are those listed under /dev/bus/usb
const connectedDevicesCommand = "lsusb"
// Extract the Drone Name from console output and insert it into the table
cockpit.spawn(["bash", "-c", droneNameCommand])
.then((data) => {
droneName.innerHTML = data.split(" ")[0];
})
.catch((error) => console.log(error));
// Extract the IP Address from console output and insert it into the table
cockpit.spawn(["bash", "-c", ipAddressCommand])
.then((data) => {
ipAddress.innerHTML = data.split(" ")[0];
})
.catch((error) => console.log(error));
// Extract the Docker Container IDs from console output and insert it into the table
cockpit.spawn(["bash", "-c", dockerContainerCommand])
.then((data) => {
const datalist = data.split("\n");
// If no Docker Containers up
if (datalist[1].split(" ")[0] == ""){
dockerContainer.className = "p-3 mb-2 bg-danger text-white";
dockerContainer.innerHTML += "Error: No Docker Containers to Display";
} else {
// List each container on a new line
for (let i = 0; i < datalist.length - 2; i++) {
dockerContainer.innerHTML += datalist[i + 1].split(" ")[0];
dockerContainer.innerHTML += "<br>";
}
dockerContainer.innerHTML += datalist[datalist.length - 1].split(" ")[0];
}
})
.catch((error) => console.log(error));
// Extract the Connection Upstream Status from console output and insert it into the table
cockpit.spawn(["bash", "-c", connectionUpstreamCommand])
.then((data) => {
const datalist = data.split(" ");
// If error no upstream returned
if (datalist[0] == "ping:") {connectionUpstream.innerHTML = "Connection LIMITED - No upstream";}
// If ping succeeded
else if (datalist[0] == "PING") {connectionUpstream.innerHTML = "Connection OK";}
// If some other output returned
else {connectionUpstream.innerHTML = "Connection DOWN";}
})
.catch((error) => console.log(error));
// Extract the Network SSID from console output and insert it into the table
cockpit.spawn(["bash", "-c", networkSSIDCommand])
.then((data) => {
if (data.length == 0) {
networkSSID.innerHTML = "None";
} else {
networkSSID.innerHTML = data
}
})
.catch((error) => console.log(error));
// Extract the Camera Devices from console output and insert it into the table
cockpit.spawn(["bash", "-c", cameraDevicesCommand])
.then((data) => {
// If no devices detected
if (data.length == 0) {cameraDevices.innerHTML = "No Camera Devices Detected";}
// Otherwise print the list of Camera Devices
else {
const datalist = data.replaceAll("\n", "<br>");
cameraDevices.innerHTML += datalist;
}
})
.catch((error) => console.log(error));
// Extract the Connected Devices List from console output and insert it into the table
cockpit.spawn(["bash", "-c", connectedDevicesCommand])
.then((data) => {
const datalist = data.split("\n");
// If no devices listed
if (data.length == 0) {connectedDevices.innerHTML = "No devices detected";}
// Otherwise, list each device by name and path
for (let i = 0; i < datalist.length - 1; i++) {
const name = datalist[i].split(" ");
for (let j = 6; j < name.length; j++) {
connectedDevices.innerHTML += name[j] + " ";
}
connectedDevices.innerHTML += "<br>";
connectedDevices.innerHTML += "/dev/bus/usb/" + name[1] + "/" + name[3].replace(":", "");
if (i != datalist.length - 2) {
connectedDevices.innerHTML += "<br><br>";
}
}
})
.catch((error) => console.log(error));
}