-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemStats.cs
More file actions
114 lines (99 loc) · 3.94 KB
/
SystemStats.cs
File metadata and controls
114 lines (99 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using ODIF;
using System.Threading;
using System.Management;
namespace SystemStats
{
[PluginInfo(
PluginName = "System Stats",
PluginDescription = "",
PluginID = 0,
PluginAuthorName = "InputMapper",
PluginAuthorEmail = "",
PluginAuthorURL = "",
PluginIconPath = @""
)]
public class SystemStats_Plugin : InputDevicePlugin
{
public SystemStats_Plugin()
{
this.Devices.Add(new ThisPCStats());
}
}
public class ThisPCStats : InputDevice
{
InputChannelTypes.JoyAxis CPU_Ussage, Memory_Ussage, Memory_Total, Memory_Percent, Battery_Percent;
Thread MonitoringThread;
public ThisPCStats()
{
this.DeviceName = "This PC Stats";
CPU_Ussage = new InputChannelTypes.JoyAxis("CPU Ussage", "Percentage of total CPU in use") { min_Value = 0, max_Value = 1 };
Memory_Ussage = new InputChannelTypes.JoyAxis("Memory Used", "Memory used in MB") { min_Value = 0, max_Value = 0 };
Memory_Percent = new InputChannelTypes.JoyAxis("Memory Percentage", "Percentage of total memory in use") { min_Value = 0, max_Value = 1 };
Memory_Total = new InputChannelTypes.JoyAxis("Total Memory", "Total system memory in MB") { min_Value = 0, max_Value = 0 };
Battery_Percent = new InputChannelTypes.JoyAxis("Battery Percentage", "Percentage of battery remaining") { min_Value = 0, max_Value = 1 };
InputChannels.Add(CPU_Ussage);
InputChannels.Add(Memory_Ussage);
InputChannels.Add(Memory_Total);
InputChannels.Add(Memory_Percent);
InputChannels.Add(Battery_Percent);
MonitoringThread = new Thread(monitoringThread);
MonitoringThread.Start();
}
public void monitoringThread()
{
EventWaitHandle MyEventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
while (!Global.IsShuttingDown)
{
CPU_Ussage.Value = Stats.getCPUUsage();
Memory_Percent.Value = Stats.getMemoryPercent();
Memory_Ussage.Value = Stats.getMemoryUssage();
Memory_Total.Value = Stats.getMemoryTotal();
Battery_Percent.Value = Stats.getBatteryPercent();
MyEventWaitHandle.WaitOne(100);
}
}
protected override void Dispose(bool disposing)
{
MonitoringThread.Abort();
}
}
internal static class Stats
{
static PerformanceCounter pCntr = new PerformanceCounter("Memory", "Available KBytes");
static ManagementObject processor = new ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
static System.Windows.Forms.PowerStatus powerStatus = System.Windows.Forms.SystemInformation.PowerStatus;
private static double _systemMem;
internal static double getCPUUsage()
{
processor.Get();
return double.Parse(processor.Properties["PercentProcessorTime"].Value.ToString())/100d;
}
internal static double getMemoryUssage()
{
return (double)pCntr.NextValue();
}
internal static double getMemoryPercent()
{
double memAvailable, memPhysical;
memAvailable = getMemoryUssage();
memPhysical = getMemoryTotal();
return (memPhysical - memAvailable) / memPhysical;
}
internal static double getMemoryTotal()
{
if (_systemMem == 0)
_systemMem = (double)(new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory)/1024d;
return _systemMem;
}
internal static double getBatteryPercent()
{
return powerStatus.BatteryLifePercent;
}
}
}