88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using IniParser.Model;
|
|
using System;
|
|
using System.Threading;
|
|
|
|
namespace Barsik
|
|
{
|
|
/// <summary>
|
|
/// Parses output of "sensors" command and puts CPU core temperatures into
|
|
/// registers
|
|
/// </summary>
|
|
class SensorsMonitoringUnit : BaseMonitoringUnit
|
|
{
|
|
const string CORE_STR = "Core{0} Temp:";
|
|
|
|
public SensorsMonitoringUnit(SectionData config, RegistersContainer registers) : base(config, registers)
|
|
{
|
|
}
|
|
|
|
public override void Run()
|
|
{
|
|
int cores = int.Parse(config.Keys["Cores"]);
|
|
var multiplier = float.Parse(config.Keys["Multiplier"]);
|
|
while (true)
|
|
{
|
|
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("sensors");
|
|
procStartInfo.RedirectStandardOutput = true;
|
|
procStartInfo.UseShellExecute = false;
|
|
procStartInfo.CreateNoWindow = true;
|
|
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
|
|
{
|
|
proc.StartInfo = procStartInfo;
|
|
proc.Start();
|
|
while (!proc.HasExited)
|
|
{
|
|
Thread.Sleep(100);
|
|
}
|
|
string result = proc.StandardOutput.ReadToEnd();
|
|
var lines = result.Split('\n');
|
|
// lines in output will be of format:
|
|
// Core0 Temp: +29.0°C
|
|
// and there could be several lines for one core
|
|
// so let's take all of them and use maximal value as core temp
|
|
float[] t = new float[cores];
|
|
for (int i = 0; i < cores; i++)
|
|
{
|
|
t[i] = 0;
|
|
}
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
for (int j = 0; j < cores; j++)
|
|
{
|
|
string toFind = string.Format(CORE_STR, j);
|
|
if (lines[i].Contains(toFind))
|
|
{
|
|
var value = ExtractValue(lines[i].Substring(toFind.Length));
|
|
t[j] = Math.Max(t[j], value);
|
|
}
|
|
}
|
|
}
|
|
// fill register data starting from baseAddress
|
|
for (int i = 0; i < cores; i++)
|
|
{
|
|
registers[baseAddress + i] = (int)(t[i] * multiplier);
|
|
}
|
|
}
|
|
Thread.Sleep(2000);
|
|
}
|
|
}
|
|
|
|
bool Good(char c)
|
|
{
|
|
if (c == '-' || c == '+' || c == '.') return true;
|
|
if (c >= '0' && c <= '9') return true;
|
|
return false;
|
|
}
|
|
|
|
private float ExtractValue(string v)
|
|
{
|
|
int pos = 0;
|
|
while (pos < v.Length && !Good(v[pos])) pos++;
|
|
int pos2 = pos;
|
|
while (pos2 < v.Length && Good(v[pos2])) pos2++;
|
|
float res;
|
|
float.TryParse(v.Substring(pos, pos2 - pos), out res);
|
|
return res;
|
|
}
|
|
}
|
|
} |