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 <m.moeller@gmx.ch>.
moel@1:   Portions created by the Initial Developer are Copyright (C) 2009-2010
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@63: using OpenHardwareMonitor.Utilities;
moel@1: 
moel@1: namespace OpenHardwareMonitor.Hardware {
moel@1: 
moel@1:   public class Sensor : ISensor {
moel@1: 
moel@1:     private string defaultName;
moel@1:     private string name;
moel@1:     private int index;
moel@1:     private SensorType sensorType;
moel@1:     private IHardware hardware;
moel@63:     private ReadOnlyArray<IParameter> parameters;
moel@1:     private float? value;
moel@1:     private float? min;
moel@1:     private float? max;
moel@1:     private float? limit;
moel@1:     private float? defaultLimit;
moel@1:     private Queue<ISensorEntry> entries = 
moel@1:       new Queue<ISensorEntry>(MAX_MINUTES * 15);
moel@1:     
moel@1:     private float sum = 0;
moel@1:     private int count = 0;
moel@1: 
moel@1:     private const int MAX_MINUTES = 120;
moel@28:    
moel@1:     public Sensor(string name, int index, SensorType sensorType,
moel@63:       IHardware hardware) : this(name, index, null, sensorType, hardware, 
moel@63:       new ParameterDescription[0]) { }
moel@1: 
moel@63:     public Sensor(string name, int index, float? limit,
moel@63:       SensorType sensorType, IHardware hardware) : this(name, index, limit, 
moel@63:       sensorType, hardware, new ParameterDescription[0]) { }    
moel@63: 
moel@63:     public Sensor(string name, int index, float? limit, SensorType sensorType, 
moel@63:       IHardware hardware, ParameterDescription[] parameterDescriptions) 
moel@1:     {
moel@1:       this.defaultName = name;      
moel@1:       this.index = index;
moel@1:       this.defaultLimit = limit;
moel@1:       this.sensorType = sensorType;
moel@1:       this.hardware = hardware;
moel@63:       Parameter[] parameters = new Parameter[parameterDescriptions.Length];
moel@63:       for (int i = 0; i < parameters.Length; i++ ) 
moel@63:         parameters[i] = new Parameter(parameterDescriptions[i], this);
moel@63:       this.parameters = parameters;
moel@63: 
moel@63:       string configName = Config.Settings[Identifier + "/name"];
moel@1:       if (configName != null)
moel@1:         this.name = configName;
moel@1:       else
moel@1:         this.name = name;
moel@63:       string configLimit = Config.Settings[Identifier + "/limit"];
moel@1:       if (configLimit != null && configLimit != "")
moel@1:         this.limit = float.Parse(configLimit);
moel@1:       else
moel@1:         this.limit = limit;
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@28:     public string Identifier {
moel@28:       get {
moel@28:         return hardware.Identifier + "/" + sensorType.ToString().ToLower() +
moel@28:           "/" + index;
moel@28:       }
moel@28:     }
moel@28: 
moel@1:     public string Name {
moel@1:       get { 
moel@1:         return name; 
moel@1:       }
moel@1:       set {
moel@1:         if (value != "") 
moel@1:           name = value;          
moel@1:         else 
moel@1:           name = defaultName;
moel@63:         Config.Settings[Identifier + "/name"] = name;
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public int Index {
moel@1:       get { return index; }
moel@1:     }
moel@1: 
moel@63:     public IReadOnlyArray<IParameter> Parameters {
moel@63:       get { return parameters; }
moel@63:     }
moel@63: 
moel@1:     public float? Value {
moel@1:       get { 
moel@1:         return value; 
moel@1:       }
moel@1:       set {
moel@1:         while (entries.Count > 0 && 
moel@1:           (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1:           entries.Dequeue();
moel@1: 
moel@1:         if (value.HasValue) {
moel@1:           sum += value.Value;
moel@1:           count++;
moel@1:           if (count == 4) {
moel@1:             entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1:             sum = 0;
moel@1:             count = 0;
moel@1:           }
moel@1:         }
moel@1: 
moel@1:         this.value = value;
moel@1:         if (min > value || !min.HasValue)
moel@1:           min = value;
moel@1:         if (max < value || !max.HasValue)
moel@1:           max = value;
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public float? Min { get { return min; } }
moel@1:     public float? Max { get { return max; } }
moel@1: 
moel@1:     public float? Limit {
moel@1:       get {
moel@1:         return limit;
moel@1:       }
moel@1: 
moel@1:       set {
moel@1:         if (value.HasValue) {
moel@1:           limit = value;
moel@63:           Config.Settings[Identifier + "/limit"] =
moel@1:             limit.ToString();
moel@1:         } else {
moel@1:           limit = defaultLimit;
moel@63:           Config.Settings[Identifier + "/limit"] = "";          
moel@1:         }        
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public IEnumerable<ISensorEntry> Plot {
moel@1:       get { return entries; }
moel@1:     }
moel@1: 
moel@1:     public struct Entry : ISensorEntry {
moel@1:       private float value;
moel@1:       private DateTime time;
moel@1: 
moel@1:       public Entry(float value, DateTime time) {
moel@1:         this.value = value;
moel@1:         this.time = time;
moel@1:       }
moel@1: 
moel@1:       public float Value { get { return value; } }
moel@1:       public DateTime Time { get { return time; } }
moel@1:     }
moel@1:   }
moel@1: }