moel@1: /* moel@1: moel@1: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@1: moel@1: The contents of this file are subject to the Mozilla Public License Version moel@1: 1.1 (the "License"); you may not use this file except in compliance with moel@1: the License. You may obtain a copy of the License at moel@1: moel@1: http://www.mozilla.org/MPL/ moel@1: moel@1: Software distributed under the License is distributed on an "AS IS" basis, moel@1: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@1: for the specific language governing rights and limitations under the License. moel@1: moel@1: The Original Code is the Open Hardware Monitor code. moel@1: moel@1: The Initial Developer of the Original Code is moel@1: Michael Möller . moel@298: Portions created by the Initial Developer are Copyright (C) 2009-2011 moel@1: the Initial Developer. All Rights Reserved. moel@1: moel@1: Contributor(s): moel@1: moel@1: Alternatively, the contents of this file may be used under the terms of moel@1: either the GNU General Public License Version 2 or later (the "GPL"), or moel@1: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@1: in which case the provisions of the GPL or the LGPL are applicable instead moel@1: of those above. If you wish to allow use of your version of this file only moel@1: under the terms of either the GPL or the LGPL, and not to allow others to moel@1: use your version of this file under the terms of the MPL, indicate your moel@1: decision by deleting the provisions above and replace them with the notice moel@1: and other provisions required by the GPL or the LGPL. If you do not delete moel@1: the provisions above, a recipient may use your version of this file under moel@1: the terms of any one of the MPL, the GPL or the LGPL. moel@1: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@166: using System.Globalization; moel@298: using System.IO; moel@298: using System.IO.Compression; moel@165: using OpenHardwareMonitor.Collections; moel@1: moel@1: namespace OpenHardwareMonitor.Hardware { moel@1: moel@165: internal class Sensor : ISensor { moel@1: moel@195: private readonly string defaultName; moel@1: private string name; moel@195: private readonly int index; moel@195: private readonly bool defaultHidden; moel@195: private readonly SensorType sensorType; moel@298: private readonly Hardware hardware; moel@195: private readonly ReadOnlyArray parameters; moel@167: private float? currentValue; moel@167: private float? minValue; moel@167: private float? maxValue; moel@298: private readonly RingCollection moel@298: values = new RingCollection(); moel@195: private readonly ISettings settings; moel@247: private IControl control; moel@1: moel@195: private float sum; moel@195: private int count; moel@28: moel@1: public Sensor(string name, int index, SensorType sensorType, moel@298: Hardware hardware, ISettings settings) : moel@165: this(name, index, sensorType, hardware, null, settings) { } moel@1: moel@134: public Sensor(string name, int index, SensorType sensorType, moel@298: Hardware hardware, ParameterDescription[] parameterDescriptions, moel@165: ISettings settings) : moel@134: this(name, index, false, sensorType, hardware, moel@165: parameterDescriptions, settings) { } moel@63: moel@109: public Sensor(string name, int index, bool defaultHidden, moel@298: SensorType sensorType, Hardware hardware, moel@165: ParameterDescription[] parameterDescriptions, ISettings settings) moel@165: { moel@1: this.index = index; moel@109: this.defaultHidden = defaultHidden; moel@1: this.sensorType = sensorType; moel@1: this.hardware = hardware; moel@109: Parameter[] parameters = new Parameter[parameterDescriptions == null ? moel@109: 0 : parameterDescriptions.Length]; moel@63: for (int i = 0; i < parameters.Length; i++ ) moel@165: parameters[i] = new Parameter(parameterDescriptions[i], this, settings); moel@63: this.parameters = parameters; moel@63: moel@165: this.settings = settings; moel@165: this.defaultName = name; moel@166: this.name = settings.GetValue( moel@165: new Identifier(Identifier, "name").ToString(), name); moel@298: moel@298: GetSensorValuesFromSettings(); moel@298: moel@298: hardware.Closing += delegate(IHardware h) { moel@298: SetSensorValuesToSettings(); moel@298: }; moel@298: } moel@298: moel@298: private void SetSensorValuesToSettings() { moel@298: using (MemoryStream m = new MemoryStream()) { moel@298: using (GZipStream c = new GZipStream(m, CompressionMode.Compress)) moel@298: using (BinaryWriter writer = new BinaryWriter(c)) { moel@298: foreach (SensorValue sensorValue in values) { moel@298: writer.Write(sensorValue.Time.ToBinary()); moel@298: writer.Write(sensorValue.Value); moel@298: } moel@298: } moel@298: settings.SetValue(new Identifier(Identifier, "values").ToString(), moel@298: Convert.ToBase64String(m.ToArray())); moel@298: } moel@298: } moel@298: moel@298: private void GetSensorValuesFromSettings() { moel@298: string s = settings.GetValue( moel@298: new Identifier(Identifier, "values").ToString(), null); moel@298: moel@298: byte[] array = null; moel@298: try { moel@298: array = Convert.FromBase64String(s); moel@298: using (MemoryStream m = new MemoryStream(array)) moel@298: using (GZipStream c = new GZipStream(m, CompressionMode.Decompress)) moel@298: using (BinaryReader reader = new BinaryReader(c)) { moel@298: try { moel@298: while (true) { moel@298: DateTime time = DateTime.FromBinary(reader.ReadInt64()); moel@298: float value = reader.ReadSingle(); moel@298: AppendValue(value, time); moel@298: } moel@298: } catch (EndOfStreamException) { } moel@298: } moel@298: } catch { } moel@298: if (values.Count > 0) moel@298: AppendValue(float.NaN, DateTime.Now); moel@298: } moel@298: moel@298: private void AppendValue(float value, DateTime time) { moel@298: if (values.Count >= 2 && values.Last.Value == value && moel@298: values[values.Count - 2].Value == value) { moel@298: values.Last = new SensorValue(value, time); moel@298: return; moel@298: } moel@298: moel@298: values.Append(new SensorValue(value, time)); moel@1: } moel@1: moel@28: public IHardware Hardware { moel@28: get { return hardware; } moel@28: } moel@28: moel@1: public SensorType SensorType { moel@1: get { return sensorType; } moel@1: } moel@1: moel@109: public Identifier Identifier { moel@28: get { moel@166: return new Identifier(hardware.Identifier, moel@166: sensorType.ToString().ToLowerInvariant(), moel@166: index.ToString(CultureInfo.InvariantCulture)); moel@28: } moel@28: } moel@28: moel@1: public string Name { moel@1: get { moel@1: return name; moel@1: } moel@1: set { moel@167: if (!string.IsNullOrEmpty(value)) moel@1: name = value; moel@1: else moel@1: name = defaultName; moel@166: settings.SetValue(new Identifier(Identifier, "name").ToString(), name); moel@1: } moel@1: } moel@1: moel@1: public int Index { moel@1: get { return index; } moel@1: } moel@1: moel@109: public bool IsDefaultHidden { moel@109: get { return defaultHidden; } moel@109: } moel@109: moel@63: public IReadOnlyArray Parameters { moel@63: get { return parameters; } moel@63: } moel@63: moel@1: public float? Value { moel@1: get { moel@167: return currentValue; moel@1: } moel@1: set { moel@298: DateTime now = DateTime.Now; moel@298: while (values.Count > 0 && (now - values.First.Time).TotalDays > 1) moel@298: values.Remove(); moel@1: moel@1: if (value.HasValue) { moel@1: sum += value.Value; moel@1: count++; moel@1: if (count == 4) { moel@298: AppendValue(sum / count, now); moel@1: sum = 0; moel@1: count = 0; moel@1: } moel@1: } moel@1: moel@167: this.currentValue = value; moel@167: if (minValue > value || !minValue.HasValue) moel@167: minValue = value; moel@167: if (maxValue < value || !maxValue.HasValue) moel@167: maxValue = value; moel@1: } moel@1: } moel@1: moel@167: public float? Min { get { return minValue; } } moel@167: public float? Max { get { return maxValue; } } moel@1: moel@151: public void ResetMin() { moel@167: minValue = null; moel@151: } moel@151: moel@151: public void ResetMax() { moel@167: maxValue = null; moel@151: } moel@151: moel@159: public IEnumerable Values { moel@159: get { return values; } moel@159: } moel@110: moel@110: public void Accept(IVisitor visitor) { moel@167: if (visitor == null) moel@167: throw new ArgumentNullException("visitor"); moel@167: visitor.VisitSensor(this); moel@110: } moel@110: moel@110: public void Traverse(IVisitor visitor) { moel@110: foreach (IParameter parameter in parameters) moel@110: parameter.Accept(visitor); moel@110: } moel@247: moel@247: public IControl Control { moel@247: get { moel@247: return control; moel@247: } moel@247: internal set { moel@247: this.control = value; moel@247: } moel@247: } moel@1: } moel@1: }