Added a first data logging implementation.

This commit is contained in:
Michael Möller
2013-08-11 21:26:56 +00:00
parent 5117c1292d
commit 92d4954da6
5 changed files with 193 additions and 5 deletions

View File

@@ -4,7 +4,7 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
Copyright (C) 2009-2013 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
@@ -95,6 +95,7 @@ namespace OpenHardwareMonitor.GUI {
this.timer = new System.Windows.Forms.Timer(this.components);
this.splitContainer = new OpenHardwareMonitor.GUI.SplitContainerAdv();
this.treeView = new Aga.Controls.Tree.TreeViewAdv();
this.logSensorsMenuItem = new System.Windows.Forms.MenuItem();
this.splitContainer.Panel1.SuspendLayout();
this.splitContainer.SuspendLayout();
this.SuspendLayout();
@@ -355,7 +356,8 @@ namespace OpenHardwareMonitor.GUI {
this.temperatureUnitsMenuItem,
this.plotLocationMenuItem,
this.webMenuItemSeparator,
this.webMenuItem});
this.webMenuItem,
this.logSensorsMenuItem});
this.optionsMenuItem.Text = "Options";
//
// startMinMenuItem
@@ -432,7 +434,7 @@ namespace OpenHardwareMonitor.GUI {
this.plotRightMenuItem.RadioCheck = true;
this.plotRightMenuItem.Text = "Right";
//
// MenuItem4
// webMenuItemSeparator
//
this.webMenuItemSeparator.Index = 7;
this.webMenuItemSeparator.Text = "-";
@@ -537,6 +539,11 @@ namespace OpenHardwareMonitor.GUI {
this.treeView.MouseMove += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseMove);
this.treeView.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseUp);
//
// logSensorsMenuItem
//
this.logSensorsMenuItem.Index = 9;
this.logSensorsMenuItem.Text = "Log Sensors";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -620,6 +627,7 @@ namespace OpenHardwareMonitor.GUI {
private System.Windows.Forms.MenuItem gpuMenuItem;
private System.Windows.Forms.MenuItem fanControllerMenuItem;
private System.Windows.Forms.MenuItem ramMenuItem;
private System.Windows.Forms.MenuItem logSensorsMenuItem;
}
}

View File

@@ -66,6 +66,9 @@ namespace OpenHardwareMonitor.GUI {
private UserOption runWebServer;
private HttpServer server;
private UserOption logSensors;
private Logger logger;
private bool selectionDragging = false;
public MainForm() {
@@ -277,6 +280,10 @@ namespace OpenHardwareMonitor.GUI {
server.StopHTTPListener();
};
logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
settings);
logger = new Logger(computer);
InitializePlotForm();
startupMenuItem.Visible = startupManager.IsAvailable;
@@ -500,6 +507,9 @@ namespace OpenHardwareMonitor.GUI {
if (wmiProvider != null)
wmiProvider.Update();
if (logSensors.Value)
logger.Log();
}
private void SaveConfiguration() {

View File

@@ -125,6 +125,7 @@
<Compile Include="GUI\UserRadioGroup.cs" />
<Compile Include="Properties\AssemblyVersion.cs" />
<Compile Include="Utilities\HttpServer.cs" />
<Compile Include="Utilities\Logger.cs" />
<Compile Include="Utilities\PersistentSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GUI\AboutBox.cs">

View File

@@ -10,5 +10,5 @@
using System.Reflection;
[assembly: AssemblyVersion("0.6.0.7")]
[assembly: AssemblyInformationalVersion("0.6.0.7 Alpha")]
[assembly: AssemblyVersion("0.6.0.8")]
[assembly: AssemblyInformationalVersion("0.6.0.8 Alpha")]

169
Utilities/Logger.cs Normal file
View File

@@ -0,0 +1,169 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2013 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using OpenHardwareMonitor.Hardware;
namespace OpenHardwareMonitor.Utilities {
public class Logger {
private const string fileNameFormat =
"OpenHardwareMonitorLog-{0:yyyy-MM-dd}.csv";
private readonly IComputer computer;
private DateTime day = DateTime.MinValue;
private string fileName;
private ISensor[] sensors;
public Logger(IComputer computer) {
this.computer = computer;
this.computer.HardwareAdded += HardwareAdded;
this.computer.HardwareRemoved += HardwareRemoved;
}
private void HardwareRemoved(IHardware hardware) {
hardware.SensorRemoved -= SensorRemoved;
foreach (ISensor sensor in hardware.Sensors)
SensorRemoved(sensor);
foreach (IHardware subHardware in hardware.SubHardware)
HardwareRemoved(subHardware);
}
private void HardwareAdded(IHardware hardware) {
hardware.SensorRemoved += SensorRemoved;
foreach (IHardware subHardware in hardware.SubHardware)
HardwareAdded(subHardware);
}
private void SensorRemoved(ISensor sensor) {
for (int i = 0; i < sensors.Length; i++) {
if (sensor == sensors[i])
sensors[i] = null;
}
}
private static string GetFileName(DateTime date) {
return AppDomain.CurrentDomain.BaseDirectory +
Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
}
private void AddSensorRemovedHandler() {
for (int i = 0; i < sensors.Length; i++) {
ISensor sensor = sensors[i];
int index = i;
SensorEventHandler handler = null;
handler = s => {
if (s != sensor)
return;
sensors[index] = null;
sensor.Hardware.SensorRemoved -= handler;
};
sensor.Hardware.SensorRemoved += handler;
}
}
private bool OpenExistingLogFile() {
if (!File.Exists(fileName))
return false;
string[] identifiers;
try {
String line;
using (StreamReader reader = new StreamReader(fileName))
line = reader.ReadLine();
if (string.IsNullOrEmpty(line))
return false;
identifiers = line.Split(',').Skip(1).ToArray();
} catch {
return false;
}
if (identifiers.Length == 0)
return false;
sensors = new ISensor[identifiers.Length];
SensorVisitor visitor = new SensorVisitor(sensor => {
for (int i = 0; i < identifiers.Length; i++)
if (sensor.Identifier.ToString() == identifiers[i])
sensors[i] = sensor;
});
visitor.VisitComputer(computer);
return true;
}
private void CreateNewLogFile() {
IList<ISensor> list = new List<ISensor>();
SensorVisitor visitor = new SensorVisitor(sensor => {
list.Add(sensor);
});
visitor.VisitComputer(computer);
sensors = list.ToArray();
using (StreamWriter writer = new StreamWriter(fileName, false)) {
writer.Write(",");
for (int i = 0; i < sensors.Length; i++) {
writer.Write(sensors[i].Identifier);
if (i < sensors.Length - 1)
writer.Write(",");
else
writer.WriteLine();
}
writer.Write("Time,");
for (int i = 0; i < sensors.Length; i++) {
writer.Write('"');
writer.Write(sensors[i].Name);
writer.Write('"');
if (i < sensors.Length - 1)
writer.Write(",");
else
writer.WriteLine();
}
}
}
public void Log() {
var now = DateTime.Now;
if (day != now.Date) {
day = now.Date;
fileName = GetFileName(day);
if (!OpenExistingLogFile())
CreateNewLogFile();
}
using (StreamWriter writer = new StreamWriter(fileName, true)) {
writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
writer.Write(",");
for (int i = 0; i < sensors.Length; i++) {
if (sensors[i] != null) {
float? value = sensors[i].Value;
if (value.HasValue)
writer.Write(
value.Value.ToString("R", CultureInfo.InvariantCulture));
}
if (i < sensors.Length - 1)
writer.Write(",");
else
writer.WriteLine();
}
}
}
}
}