moel@31: /*
moel@31:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@31:  
moel@344:   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@31: */
moel@31: 
moel@31: using System;
moel@165: using OpenHardwareMonitor.Collections;
moel@31: 
moel@31: namespace OpenHardwareMonitor.Hardware {
moel@165:   internal abstract class Hardware : IHardware {
moel@31: 
moel@275:     private readonly Identifier identifier;
moel@275:     protected readonly string name;
moel@275:     private string customName;
moel@275:     protected readonly ISettings settings;
moel@275:     protected readonly ListSet<ISensor> active = new ListSet<ISensor>();
moel@275: 
moel@275:     public Hardware(string name, Identifier identifier, ISettings settings) {
moel@275:       this.settings = settings;
moel@275:       this.identifier = identifier;
moel@275:       this.name = name;
moel@275:       this.customName = settings.GetValue(
moel@275:         new Identifier(Identifier, "name").ToString(), name);
moel@275:     }
moel@31: 
moel@64:     public IHardware[] SubHardware {
moel@64:       get { return new IHardware[0]; }
moel@64:     }
moel@64: 
moel@176:     public virtual IHardware Parent {
moel@176:       get { return null; }
moel@176:     }
moel@176: 
moel@275:     public virtual ISensor[] Sensors {
moel@31:       get { return active.ToArray(); }
moel@31:     }
moel@31: 
moel@275:     protected virtual void ActivateSensor(ISensor sensor) {
moel@110:       if (active.Add(sensor)) 
moel@31:         if (SensorAdded != null)
moel@31:           SensorAdded(sensor);
moel@31:     }
moel@31: 
moel@275:     protected virtual void DeactivateSensor(ISensor sensor) {
moel@110:       if (active.Remove(sensor))
moel@31:         if (SensorRemoved != null)
moel@110:           SensorRemoved(sensor);     
moel@31:     }
moel@31: 
moel@275:     public string Name {
moel@275:       get {
moel@275:         return customName;
moel@275:       }
moel@275:       set {
moel@275:         if (!string.IsNullOrEmpty(value))
moel@275:           customName = value;
moel@275:         else
moel@275:           customName = name;
moel@275:         settings.SetValue(new Identifier(Identifier, "name").ToString(), 
moel@275:           customName);
moel@275:       }
moel@275:     }
moel@275: 
moel@275:     public Identifier Identifier {
moel@275:       get {
moel@275:         return identifier;
moel@275:       }
moel@275:     }
moel@275: 
moel@64:     #pragma warning disable 67
moel@31:     public event SensorEventHandler SensorAdded;
moel@31:     public event SensorEventHandler SensorRemoved;
moel@64:     #pragma warning restore 67
moel@110:   
moel@275:     
moel@165:     public abstract HardwareType HardwareType { get; }
moel@110: 
moel@110:     public virtual string GetReport() {
moel@110:       return null;
moel@110:     }
moel@110: 
moel@110:     public abstract void Update();
moel@110: 
moel@298:     public event HardwareEventHandler Closing;
moel@298: 
moel@298:     public virtual void Close() {
moel@298:       if (Closing != null)
moel@298:         Closing(this);
moel@298:     }
moel@298: 
moel@110:     public void Accept(IVisitor visitor) {
moel@167:       if (visitor == null)
moel@167:         throw new ArgumentNullException("visitor");
moel@167:       visitor.VisitHardware(this);
moel@110:     }
moel@110: 
moel@275:     public virtual void Traverse(IVisitor visitor) {
moel@110:       foreach (ISensor sensor in active)
moel@110:         sensor.Accept(visitor);
moel@110:     }
moel@31:   }
moel@31: }