Code cleanup

This commit is contained in:
VladMordock
2021-02-17 18:25:59 +01:00
parent bf2d436fef
commit e8ee60ef5e
4 changed files with 34 additions and 43 deletions

View File

@@ -6,62 +6,50 @@ using System.Management;
class HardwareMonitorClass class HardwareMonitorClass
{ {
// Class for HW monitoring
protected PerformanceCounter PerformanceCounterCPU;
protected PerformanceCounter PerformanceCounterRAM;
protected PerformanceCounter cpuCounter; public string LastCurrentCpuUsage = "-1"; // Last measured CPU usage
protected PerformanceCounter ramCounter; public string LastCurrentRamUsage = "-1"; // Last measured RAM usage
public HardwareMonitorClass() public HardwareMonitorClass()
{ {
// Prepare CPU counter // Prepare CPU counter
cpuCounter = new PerformanceCounter(); PerformanceCounterCPU = new PerformanceCounter();
cpuCounter.CategoryName = "Processor"; PerformanceCounterCPU.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time"; PerformanceCounterCPU.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total"; PerformanceCounterCPU.InstanceName = "_Total";
// Prepare RAM counter // Prepare RAM counter
ramCounter = new PerformanceCounter(); PerformanceCounterRAM = new PerformanceCounter();
ramCounter.CategoryName = "Memory"; PerformanceCounterRAM.CategoryName = "Memory";
ramCounter.CounterName = "% Committed Bytes In Use"; PerformanceCounterRAM.CounterName = "% Committed Bytes In Use";
} }
// Get CPU usage // Get CPU usage
public string getCurrentCpuUsage() public string getCurrentCpuUsage()
{ {
return cpuCounter.NextValue().ToString("0.00"); LastCurrentCpuUsage = PerformanceCounterCPU.NextValue().ToString("0.00");
return LastCurrentCpuUsage;
} }
// Get available RAM memory // Get available RAM memory
public string getAvailableRAM() public string getCurrentRamUsage()
{ {
return ramCounter.NextValue().ToString("0.00"); LastCurrentRamUsage = PerformanceCounterRAM.NextValue().ToString("0.00");
return LastCurrentRamUsage;
} }
} }
class CurrentMissionClass class CurrentMissionClass
{ {
// Class holding current mission/DCS server status
public string Theatre = ""; // Mission theathre public string Theatre = ""; // Mission theathre
public string Mission = ""; // Mission name public string Mission = ""; // Mission name
public string Pause = ""; // Mission pause public string Pause = ""; // Mission pause
public int PlayerCount = 0; // Actual player time public int PlayerCount = 0; // Actual player time
public string ModelTime = ""; public string ModelTime = ""; // Mission time
public string RealTime = ""; public string RealTime = ""; // Server time
public string ToInfoString()
{
// Return mission information as string
if (this.Mission != "")
{
// Return mission information as string
return "Mission: " + this.Mission + "(" + this.Theatre + ") Pause:" + this.Pause + " Players: " + this.PlayerCount;
} else
{
// Mission information is not available
return "Mission: Unknown";
}
}
} }
internal class Globals internal class Globals
@@ -88,8 +76,8 @@ internal class Globals
public static string VersionDatabase = ""; // Version - Database public static string VersionDatabase = ""; // Version - Database
public static string VersionPerun = "DEBUG"; // Version - Perun public static string VersionPerun = "DEBUG"; // Version - Perun
public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information single ton
public static HardwareMonitorClass HardwareMonitor = new HardwareMonitorClass(); public static HardwareMonitorClass HardwareMonitor = new HardwareMonitorClass(); // Hardware monitor singleton
} }

View File

@@ -1,9 +1,9 @@
using System; using System;
using System.IO; using System.IO;
// Singleton class - log controller
class LogController class LogController
{ {
// Singleton class - log controller
private static LogController _instance = new LogController(); // Singleton instance private static LogController _instance = new LogController(); // Singleton instance
public int level; // Level of logging public int level; // Level of logging
@@ -17,8 +17,10 @@ class LogController
public void WriteLog(int logLevel, string strLog) public void WriteLog(int logLevel, string strLog)
{ {
// Write log entry to file
if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit
// Declare variables
StreamWriter LogStreamWriter; StreamWriter LogStreamWriter;
FileStream LogFileStream = null; FileStream LogFileStream = null;
DirectoryInfo LogDirectoryInfo = null; DirectoryInfo LogDirectoryInfo = null;
@@ -41,7 +43,7 @@ class LogController
} }
catch catch
{ {
// Do nothing // Do nothing - TBD error handling
} }
} }
try try
@@ -52,7 +54,7 @@ class LogController
} }
catch catch
{ {
// Do nothing // Do nothing - TBD error handling
} }
// TBD: log rotation // TBD: log rotation

View File

@@ -35,6 +35,7 @@ internal class PerunHelper
// Declare values // Declare values
string LogDirection; string LogDirection;
string LogMarker; string LogMarker;
// Set direction marker // Set direction marker
switch (intDirection) switch (intDirection)
{ {
@@ -76,7 +77,7 @@ internal class PerunHelper
// Set frame type // Set frame type
strType = strType.PadLeft(3, ' '); strType = strType.PadLeft(3, ' ');
if (!bSkipGui) if (!bSkipGui) // Shall we skip GUI?
{ {
// Rotate log history // Rotate log history
for (int i = 0; i < arrLogHistory.Length - 1; i++) for (int i = 0; i < arrLogHistory.Length - 1; i++)
@@ -85,13 +86,13 @@ internal class PerunHelper
} }
// Add new entry // Add new entry
arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " " + LogDirection + " " + LogType + " " + strEntryToAdd; // Add entry at the last position arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss.fff") + " " + LogDirection + " " + LogType + " " + strEntryToAdd; // Add entry at the last position
// Update control at my window // Update control at my window
Globals.AppUpdateGUI = true; Globals.AppUpdateGUI = true;
} }
// Add the entry to log file // Add the entry to log file
LogController.instance.WriteLog(logLevel, DateTime.Now.ToString("yyyy-MM-dd ") + " " + DateTime.Now.ToString("HH:mm:ss") + " | Instance: " + Globals.AppInstanceID + " | " + LogMarker + " | " + LogDirection + " | " + strType + " | " + strEntryToAdd); LogController.instance.WriteLog(logLevel, DateTime.Now.ToString("yyyy-MM-dd ") + " " + DateTime.Now.ToString("HH:mm:ss.fff") + " | Instance: " + Globals.AppInstanceID + " | " + LogMarker + " | " + LogDirection + " | " + strType + " | " + strEntryToAdd);
} }
// Gets build version // Gets build version
@@ -141,7 +142,7 @@ internal class PerunHelper
} }
public static string ConvertSecoundsToString (Double NumberOfSecounds){ public static string ConvertSecoundsToString (Double NumberOfSecounds){
// TBD // TBD - convert number of secounds to HHhMMm format
return NumberOfSecounds.ToString(); return NumberOfSecounds.ToString();
} }
} }

View File

@@ -739,7 +739,7 @@ namespace Perun_v1
{ {
//Handle getting of system status //Handle getting of system status
label28.Text = Globals.HardwareMonitor.getCurrentCpuUsage() + "%"; label28.Text = Globals.HardwareMonitor.getCurrentCpuUsage() + "%";
label29.Text = Globals.HardwareMonitor.getAvailableRAM() + "%"; label29.Text = Globals.HardwareMonitor.getCurrentRamUsage() + "%";
} }
private void TIM_Autostart_Tick(object sender, EventArgs e) private void TIM_Autostart_Tick(object sender, EventArgs e)