// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // Copyright (C) LibreHardwareMonitor and Contributors. // Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. using System; using System.Collections.Generic; using System.Linq; namespace LibreHardwareMonitor.Hardware; /// /// Object representing a component of the computer. /// /// Individual information can be read from the . /// /// public abstract class Hardware : IHardware { protected readonly HashSet _active = new(); protected readonly string _name; protected readonly ISettings _settings; private string _customName; /// /// Creates a new instance based on the data provided. /// /// Component name. /// Identifier that will be assigned to the device. Based on /// Additional settings passed by the . protected Hardware(string name, Identifier identifier, ISettings settings) { _settings = settings; _name = name; Identifier = identifier; _customName = settings.GetValue(new Identifier(Identifier, "name").ToString(), name); } /// /// Event triggered when is closing. /// public event HardwareEventHandler Closing; /// public abstract HardwareType HardwareType { get; } /// public Identifier Identifier { get; } /// public string Name { get { return _customName; } set { _customName = !string.IsNullOrEmpty(value) ? value : _name; _settings.SetValue(new Identifier(Identifier, "name").ToString(), _customName); } } /// public virtual IHardware Parent { get { return null; } } /// public virtual IDictionary Properties => new SortedDictionary(); /// public virtual ISensor[] Sensors { get { return _active.ToArray(); } } /// public IHardware[] SubHardware { get { return Array.Empty(); } } /// public virtual string GetReport() { return null; } /// public abstract void Update(); /// public void Accept(IVisitor visitor) { if (visitor == null) throw new ArgumentNullException(nameof(visitor)); visitor.VisitHardware(this); } /// public virtual void Traverse(IVisitor visitor) { foreach (ISensor sensor in _active) sensor.Accept(visitor); } /// protected virtual void ActivateSensor(ISensor sensor) { if (_active.Add(sensor)) SensorAdded?.Invoke(sensor); } /// protected virtual void DeactivateSensor(ISensor sensor) { if (_active.Remove(sensor)) SensorRemoved?.Invoke(sensor); } /// public virtual void Close() { Closing?.Invoke(this); } #pragma warning disable 67 public event SensorEventHandler SensorAdded; public event SensorEventHandler SensorRemoved; #pragma warning restore 67 }