Small refactor, reducing global variables and redundant includes

main
xad1 3 years ago
parent 73e770da1c
commit a8bb0008a2

@ -1,13 +0,0 @@
#include <Arduino.h>
#ifndef GLOBAL_H_
#define GLOBAL_H_
extern String selectedNode;
extern int selectedItem;
extern int selectedLXC;
extern int selectedVM;
extern String selectedDisk;
extern String selectedPool;
// TODO remove later
#endif

@ -1,6 +1,6 @@
#ifndef MENU_H_ #ifndef MENU_H_
#define MENU_H_ #define MENU_H_
#include <ArduinoJson.h>
#include <Arduino.h> #include <Arduino.h>
#include <json/retrieve.h> #include <json/retrieve.h>
// Struct for static menu items such as on the main menu. Has the name of the menu item and a function to run when it is selected. // Struct for static menu items such as on the main menu. Has the name of the menu item and a function to run when it is selected.
@ -10,10 +10,10 @@ typedef struct {
} MenuItem; } MenuItem;
void listNodes(Node* nodes, const int &numItems); void listNodes(Node* nodes, const int &numItems);
void listContainers(Container* containers, const int &numItems); int listContainers(Container* containers, const int &numItems);
void listVMs(VM* vms, const int &numItems); int listVMs(VM* vms, const int &numItems);
void listDisks(Disk* disks, const int &numItems); String listDisks(Disk* disks, const int &numItems);
void listPools(Pool* pools, const int &numItems); String listPools(Pool* pools, const int &numItems);
void mainMenu(); void mainMenu();
void manageContainerMenu(); void manageContainerMenu();
void manageVMMenu(); void manageVMMenu();
@ -23,4 +23,6 @@ typedef void (*MenuPrintCallback)(void*);
extern int selectedItem; extern int selectedItem;
extern int selectedPage; extern int selectedPage;
extern String selectedNode;
#endif #endif

@ -1,5 +1,4 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h> #include <WiFi.h>
#include <odroid_go.h> #include <odroid_go.h>
#include <server.h> #include <server.h>

@ -1,7 +1,6 @@
#include <json/retrieve.h> #include <json/retrieve.h>
#include <json/utils.h> #include <json/utils.h>
#include <json/send.h> #include <json/send.h>
#include <global.h>
#include <menu.h> #include <menu.h>
#include <odroid_go.h> #include <odroid_go.h>
#include <utils.h> #include <utils.h>
@ -11,10 +10,10 @@ void vmRestart()
{ {
Serial.println("vm restart"); Serial.println("vm restart");
selectedItem = 0; selectedItem = 0;
selectedVM = 0;
int numVMs; int numVMs;
VM *vms = getVMInfo(numVMs, selectedNode); VM *vms = getVMInfo(numVMs, selectedNode);
listVMs(vms, numVMs); int selectedVM = listVMs(vms, numVMs);
delete[] vms; delete[] vms;
if (selectedVM > 0) if (selectedVM > 0)
{ {
@ -32,10 +31,9 @@ void containerRestart()
{ {
Serial.println("lxc restart"); Serial.println("lxc restart");
selectedItem = 0; selectedItem = 0;
selectedLXC = 0;
int numContainers; int numContainers;
Container *containers = getContainerInfo(numContainers, selectedNode); Container *containers = getContainerInfo(numContainers, selectedNode);
listContainers(containers, numContainers); int selectedLXC = listContainers(containers, numContainers);
delete[] containers; delete[] containers;
if (selectedLXC > 0) if (selectedLXC > 0)
{ {
@ -54,10 +52,9 @@ void vmStart()
{ {
Serial.println("vm start"); Serial.println("vm start");
selectedItem = 0; selectedItem = 0;
selectedVM = 0;
int numVMs; int numVMs;
VM *vms = getVMInfo(numVMs, selectedNode); VM *vms = getVMInfo(numVMs, selectedNode);
listVMs(vms, numVMs); int selectedVM = listVMs(vms, numVMs);
delete[] vms; delete[] vms;
if (selectedVM > 0) if (selectedVM > 0)
{ {
@ -78,10 +75,9 @@ void containerStart()
{ {
Serial.println("lxc start"); Serial.println("lxc start");
selectedItem = 0; selectedItem = 0;
selectedLXC = 0;
int numContainers; int numContainers;
Container *containers = getContainerInfo(numContainers, selectedNode); Container *containers = getContainerInfo(numContainers, selectedNode);
listContainers(containers, numContainers); int selectedLXC = listContainers(containers, numContainers);
delete[] containers; delete[] containers;
if (selectedLXC > 0) if (selectedLXC > 0)
{ {
@ -100,10 +96,9 @@ void vmStop()
{ {
Serial.println("vm stop"); Serial.println("vm stop");
selectedItem = 0; selectedItem = 0;
selectedVM = 0;
int numVMs; int numVMs;
VM *vms = getVMInfo(numVMs, selectedNode); VM *vms = getVMInfo(numVMs, selectedNode);
listVMs(vms, numVMs); int selectedVM = listVMs(vms, numVMs);
delete[] vms; delete[] vms;
if (selectedVM > 0) if (selectedVM > 0)
{ {
@ -124,10 +119,9 @@ void containerStop()
{ {
Serial.println("lxc stop"); Serial.println("lxc stop");
selectedItem = 0; selectedItem = 0;
selectedLXC = 0;
int numContainers; int numContainers;
Container *containers = getContainerInfo(numContainers, selectedNode); Container *containers = getContainerInfo(numContainers, selectedNode);
listContainers(containers, numContainers); int selectedLXC = listContainers(containers, numContainers);
delete[] containers; delete[] containers;
if (selectedLXC > 0) if (selectedLXC > 0)
{ {

@ -1,9 +1,7 @@
#include <menu.h> #include <menu.h>
#include <Arduino.h> #include <Arduino.h>
#include <odroid_go.h> #include <odroid_go.h>
#include <ArduinoJson.h>
#include <statistics.h> #include <statistics.h>
#include <global.h>
#include <manage.h> #include <manage.h>
// UI constants for the menu system. // UI constants for the menu system.
@ -366,7 +364,7 @@ void listNodes(Node *nodes, const int &numItems)
* @param containers The array of containers to list. * @param containers The array of containers to list.
* @param numItems The number of containers to list. * @param numItems The number of containers to list.
*/ */
void listContainers(Container *containers, const int &numItems) int listContainers(Container *containers, const int &numItems)
{ {
selectedItem = 0; selectedItem = 0;
void *item; void *item;
@ -375,8 +373,9 @@ void listContainers(Container *containers, const int &numItems)
if (item != nullptr) if (item != nullptr)
{ {
Container *container = static_cast<Container *>(item); Container *container = static_cast<Container *>(item);
selectedLXC = (*container).id; return (*container).id;
} }
return 0;
} }
/** /**
@ -391,7 +390,7 @@ void listContainers(Container *containers, const int &numItems)
* @param vms The array of VMs to list. * @param vms The array of VMs to list.
* @param numItems The number of VMs to list. * @param numItems The number of VMs to list.
*/ */
void listVMs(VM *vms, const int &numItems) int listVMs(VM *vms, const int &numItems)
{ {
selectedItem = 0; selectedItem = 0;
void *item; void *item;
@ -399,8 +398,9 @@ void listVMs(VM *vms, const int &numItems)
if (item != nullptr) if (item != nullptr)
{ {
VM *vm = static_cast<VM *>(item); VM *vm = static_cast<VM *>(item);
selectedVM = (*vm).id; return (*vm).id;
} }
return 0;
} }
/** /**
@ -415,7 +415,7 @@ void listVMs(VM *vms, const int &numItems)
* @param disks The array of disks to list. * @param disks The array of disks to list.
* @param numItems The number of disks to list. * @param numItems The number of disks to list.
*/ */
void listDisks(Disk *disks, const int &numItems) String listDisks(Disk *disks, const int &numItems)
{ {
selectedItem = 0; selectedItem = 0;
void *item; void *item;
@ -424,8 +424,9 @@ void listDisks(Disk *disks, const int &numItems)
if (item != nullptr) if (item != nullptr)
{ {
Disk *disk = static_cast<Disk *>(item); Disk *disk = static_cast<Disk *>(item);
selectedDisk = (*disk).devpath; return (*disk).devpath;
} }
return "";
} }
/** /**
@ -440,7 +441,7 @@ void listDisks(Disk *disks, const int &numItems)
* @param pools The array of pools to list. * @param pools The array of pools to list.
* @param numItems The number of pools to list. * @param numItems The number of pools to list.
*/ */
void listPools(Pool *pools, const int &numItems) String listPools(Pool *pools, const int &numItems)
{ {
selectedItem = 0; selectedItem = 0;
void *item; void *item;
@ -448,6 +449,7 @@ void listPools(Pool *pools, const int &numItems)
if (item != nullptr) if (item != nullptr)
{ {
Pool *pool = static_cast<Pool *>(item); Pool *pool = static_cast<Pool *>(item);
selectedPool = (*pool).name; return (*pool).name;
} }
return "";
} }

@ -1,6 +1,5 @@
#include <json/retrieve.h> #include <json/retrieve.h>
#include <json/utils.h> #include <json/utils.h>
#include <global.h>
#include <menu.h> #include <menu.h>
#include <odroid_go.h> #include <odroid_go.h>
#include <utils.h> #include <utils.h>
@ -211,12 +210,11 @@ void containerInfo()
Serial.println("container info"); Serial.println("container info");
selectedItem = 0; selectedItem = 0;
selectedPage = 0; selectedPage = 0;
selectedLXC = 0;
int numContainers; int numContainers;
Container *containers = getContainerInfo(numContainers, selectedNode); Container *containers = getContainerInfo(numContainers, selectedNode);
listContainers(containers, numContainers); int selectedLXC = listContainers(containers, numContainers);
delete[] containers; delete[] containers;
unsigned long lastUpdate = 0; unsigned long lastUpdate = 0;
@ -251,10 +249,9 @@ void vmInfo()
Serial.println("vm info"); Serial.println("vm info");
selectedItem = 0; selectedItem = 0;
selectedPage = 0; selectedPage = 0;
selectedVM = 0;
int numVMs; int numVMs;
VM *vms = getVMInfo(numVMs, selectedNode); VM *vms = getVMInfo(numVMs, selectedNode);
listVMs(vms, numVMs); int selectedVM = listVMs(vms, numVMs);
delete[] vms; delete[] vms;
unsigned long lastUpdate = 0; unsigned long lastUpdate = 0;
@ -287,11 +284,11 @@ void diskInfo()
Serial.println("disk info"); Serial.println("disk info");
selectedItem = 0; selectedItem = 0;
selectedPage = 0; selectedPage = 0;
selectedDisk = "";
int numDisks; int numDisks;
Disk *disks = getDiskInfo(numDisks, selectedNode); Disk *disks = getDiskInfo(numDisks, selectedNode);
listDisks(disks, numDisks); String selectedDisk = listDisks(disks, numDisks);
delete[] disks; delete[] disks;
if (selectedDisk != "") if (selectedDisk != "")
@ -323,11 +320,11 @@ void poolInfo()
Serial.println("pool info"); Serial.println("pool info");
selectedItem = 0; selectedItem = 0;
selectedPage = 0; selectedPage = 0;
selectedPool = "";
int numPools; int numPools;
Pool *pools = getPoolInfo(numPools, selectedNode); Pool *pools = getPoolInfo(numPools, selectedNode);
listPools(pools, numPools); String selectedPool = listPools(pools, numPools);
delete[] pools; delete[] pools;
if (selectedPool != "") if (selectedPool != "")

@ -2,7 +2,6 @@
#include <wifi_info.h> #include <wifi_info.h>
#include <WiFi.h> #include <WiFi.h>
#include <pin.h> #include <pin.h>
#include <json/retrieve.h>
/** /**

Loading…
Cancel
Save