Added initial support for AMD family 12h and 15h CPUs.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2011
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Globalization;
42 using System.IO.Compression;
43 using OpenHardwareMonitor.Collections;
45 namespace OpenHardwareMonitor.Hardware {
47 internal class Sensor : ISensor {
49 private readonly string defaultName;
51 private readonly int index;
52 private readonly bool defaultHidden;
53 private readonly SensorType sensorType;
54 private readonly Hardware hardware;
55 private readonly ReadOnlyArray<IParameter> parameters;
56 private float? currentValue;
57 private float? minValue;
58 private float? maxValue;
59 private readonly RingCollection<SensorValue>
60 values = new RingCollection<SensorValue>();
61 private readonly ISettings settings;
62 private IControl control;
67 public Sensor(string name, int index, SensorType sensorType,
68 Hardware hardware, ISettings settings) :
69 this(name, index, sensorType, hardware, null, settings) { }
71 public Sensor(string name, int index, SensorType sensorType,
72 Hardware hardware, ParameterDescription[] parameterDescriptions,
74 this(name, index, false, sensorType, hardware,
75 parameterDescriptions, settings) { }
77 public Sensor(string name, int index, bool defaultHidden,
78 SensorType sensorType, Hardware hardware,
79 ParameterDescription[] parameterDescriptions, ISettings settings)
82 this.defaultHidden = defaultHidden;
83 this.sensorType = sensorType;
84 this.hardware = hardware;
85 Parameter[] parameters = new Parameter[parameterDescriptions == null ?
86 0 : parameterDescriptions.Length];
87 for (int i = 0; i < parameters.Length; i++ )
88 parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
89 this.parameters = parameters;
91 this.settings = settings;
92 this.defaultName = name;
93 this.name = settings.GetValue(
94 new Identifier(Identifier, "name").ToString(), name);
96 GetSensorValuesFromSettings();
98 hardware.Closing += delegate(IHardware h) {
99 SetSensorValuesToSettings();
103 private void SetSensorValuesToSettings() {
104 using (MemoryStream m = new MemoryStream()) {
105 using (GZipStream c = new GZipStream(m, CompressionMode.Compress))
106 using (BinaryWriter writer = new BinaryWriter(c)) {
107 foreach (SensorValue sensorValue in values) {
108 writer.Write(sensorValue.Time.ToBinary());
109 writer.Write(sensorValue.Value);
112 settings.SetValue(new Identifier(Identifier, "values").ToString(),
113 Convert.ToBase64String(m.ToArray()));
117 private void GetSensorValuesFromSettings() {
118 string s = settings.GetValue(
119 new Identifier(Identifier, "values").ToString(), null);
123 array = Convert.FromBase64String(s);
124 using (MemoryStream m = new MemoryStream(array))
125 using (GZipStream c = new GZipStream(m, CompressionMode.Decompress))
126 using (BinaryReader reader = new BinaryReader(c)) {
129 DateTime time = DateTime.FromBinary(reader.ReadInt64());
130 float value = reader.ReadSingle();
131 AppendValue(value, time);
133 } catch (EndOfStreamException) { }
136 if (values.Count > 0)
137 AppendValue(float.NaN, DateTime.Now);
140 private void AppendValue(float value, DateTime time) {
141 if (values.Count >= 2 && values.Last.Value == value &&
142 values[values.Count - 2].Value == value) {
143 values.Last = new SensorValue(value, time);
147 values.Append(new SensorValue(value, time));
150 public IHardware Hardware {
151 get { return hardware; }
154 public SensorType SensorType {
155 get { return sensorType; }
158 public Identifier Identifier {
160 return new Identifier(hardware.Identifier,
161 sensorType.ToString().ToLowerInvariant(),
162 index.ToString(CultureInfo.InvariantCulture));
171 if (!string.IsNullOrEmpty(value))
175 settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
180 get { return index; }
183 public bool IsDefaultHidden {
184 get { return defaultHidden; }
187 public IReadOnlyArray<IParameter> Parameters {
188 get { return parameters; }
191 public float? Value {
196 DateTime now = DateTime.Now;
197 while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
200 if (value.HasValue) {
204 AppendValue(sum / count, now);
210 this.currentValue = value;
211 if (minValue > value || !minValue.HasValue)
213 if (maxValue < value || !maxValue.HasValue)
218 public float? Min { get { return minValue; } }
219 public float? Max { get { return maxValue; } }
221 public void ResetMin() {
225 public void ResetMax() {
229 public IEnumerable<SensorValue> Values {
230 get { return values; }
233 public void Accept(IVisitor visitor) {
235 throw new ArgumentNullException("visitor");
236 visitor.VisitSensor(this);
239 public void Traverse(IVisitor visitor) {
240 foreach (IParameter parameter in parameters)
241 parameter.Accept(visitor);
244 public IControl Control {
249 this.control = value;