97 lines
2.9 KiB
C#
97 lines
2.9 KiB
C#
using IniParser.Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Barsik
|
|
{
|
|
class BaseMonitoringUnit
|
|
{
|
|
protected SectionData config;
|
|
protected RegistersContainer registers;
|
|
|
|
protected int baseAddress;
|
|
|
|
public BaseMonitoringUnit(SectionData config, RegistersContainer registers)
|
|
{
|
|
this.config = config;
|
|
this.registers = registers;
|
|
baseAddress = int.Parse(config.Keys["BaseAddress"]);
|
|
}
|
|
|
|
public virtual void Run()
|
|
{
|
|
}
|
|
|
|
public static BaseMonitoringUnit Create(SectionData config, RegistersContainer registers)
|
|
{
|
|
var type = config.Keys["Type"];
|
|
if (type == "Sensors")
|
|
{
|
|
return new SensorsMonitoringUnit(config, registers);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Unknown unit type " + type);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
class SensorsMonitoringUnit : BaseMonitoringUnit
|
|
{
|
|
public SensorsMonitoringUnit(SectionData config, RegistersContainer registers) : base(config, registers)
|
|
{
|
|
}
|
|
|
|
public override void Run()
|
|
{
|
|
var stringToFind = config.Keys["String"];
|
|
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;
|
|
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');
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
if (lines[i].Contains(stringToFind))
|
|
{
|
|
var value = ExtractValue(lines[i]);
|
|
registers[baseAddress] = (int)(value * multiplier);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = 0;
|
|
float.TryParse(v.Substring(pos, pos2 - pos), out res);
|
|
return res;
|
|
}
|
|
}
|
|
}
|