Hardware/Hardware.cs
author moel.mich
Tue, 30 Dec 2014 21:04:54 +0000
changeset 430 6b24e39f1b84
parent 298 96263190189a
permissions -rw-r--r--
Fixed Issue 651.
moel@31
     1
/*
moel@31
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@31
     6
 
moel@344
     7
  Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@31
     9
*/
moel@31
    10
moel@31
    11
using System;
moel@165
    12
using OpenHardwareMonitor.Collections;
moel@31
    13
moel@31
    14
namespace OpenHardwareMonitor.Hardware {
moel@165
    15
  internal abstract class Hardware : IHardware {
moel@31
    16
moel@275
    17
    private readonly Identifier identifier;
moel@275
    18
    protected readonly string name;
moel@275
    19
    private string customName;
moel@275
    20
    protected readonly ISettings settings;
moel@275
    21
    protected readonly ListSet<ISensor> active = new ListSet<ISensor>();
moel@275
    22
moel@275
    23
    public Hardware(string name, Identifier identifier, ISettings settings) {
moel@275
    24
      this.settings = settings;
moel@275
    25
      this.identifier = identifier;
moel@275
    26
      this.name = name;
moel@275
    27
      this.customName = settings.GetValue(
moel@275
    28
        new Identifier(Identifier, "name").ToString(), name);
moel@275
    29
    }
moel@31
    30
moel@64
    31
    public IHardware[] SubHardware {
moel@64
    32
      get { return new IHardware[0]; }
moel@64
    33
    }
moel@64
    34
moel@176
    35
    public virtual IHardware Parent {
moel@176
    36
      get { return null; }
moel@176
    37
    }
moel@176
    38
moel@275
    39
    public virtual ISensor[] Sensors {
moel@31
    40
      get { return active.ToArray(); }
moel@31
    41
    }
moel@31
    42
moel@275
    43
    protected virtual void ActivateSensor(ISensor sensor) {
moel@110
    44
      if (active.Add(sensor)) 
moel@31
    45
        if (SensorAdded != null)
moel@31
    46
          SensorAdded(sensor);
moel@31
    47
    }
moel@31
    48
moel@275
    49
    protected virtual void DeactivateSensor(ISensor sensor) {
moel@110
    50
      if (active.Remove(sensor))
moel@31
    51
        if (SensorRemoved != null)
moel@110
    52
          SensorRemoved(sensor);     
moel@31
    53
    }
moel@31
    54
moel@275
    55
    public string Name {
moel@275
    56
      get {
moel@275
    57
        return customName;
moel@275
    58
      }
moel@275
    59
      set {
moel@275
    60
        if (!string.IsNullOrEmpty(value))
moel@275
    61
          customName = value;
moel@275
    62
        else
moel@275
    63
          customName = name;
moel@275
    64
        settings.SetValue(new Identifier(Identifier, "name").ToString(), 
moel@275
    65
          customName);
moel@275
    66
      }
moel@275
    67
    }
moel@275
    68
moel@275
    69
    public Identifier Identifier {
moel@275
    70
      get {
moel@275
    71
        return identifier;
moel@275
    72
      }
moel@275
    73
    }
moel@275
    74
moel@64
    75
    #pragma warning disable 67
moel@31
    76
    public event SensorEventHandler SensorAdded;
moel@31
    77
    public event SensorEventHandler SensorRemoved;
moel@64
    78
    #pragma warning restore 67
moel@110
    79
  
moel@275
    80
    
moel@165
    81
    public abstract HardwareType HardwareType { get; }
moel@110
    82
moel@110
    83
    public virtual string GetReport() {
moel@110
    84
      return null;
moel@110
    85
    }
moel@110
    86
moel@110
    87
    public abstract void Update();
moel@110
    88
moel@298
    89
    public event HardwareEventHandler Closing;
moel@298
    90
moel@298
    91
    public virtual void Close() {
moel@298
    92
      if (Closing != null)
moel@298
    93
        Closing(this);
moel@298
    94
    }
moel@298
    95
moel@110
    96
    public void Accept(IVisitor visitor) {
moel@167
    97
      if (visitor == null)
moel@167
    98
        throw new ArgumentNullException("visitor");
moel@167
    99
      visitor.VisitHardware(this);
moel@110
   100
    }
moel@110
   101
moel@275
   102
    public virtual void Traverse(IVisitor visitor) {
moel@110
   103
      foreach (ISensor sensor in active)
moel@110
   104
        sensor.Accept(visitor);
moel@110
   105
    }
moel@31
   106
  }
moel@31
   107
}