<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using LibreHardwareMonitor.Hardware;
using Computer = LibreHardwareMonitor.Hardware.Computer;

namespace Aurora.Utils
{
    public static class HardwareMonitor
    {
        private static readonly Computer _computer;
        private static readonly IEnumerable<IHardware> _hardware;

        private static GPUUpdater _gpu;
        public static GPUUpdater GPU => _gpu ?? (_gpu = new GPUUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.GpuAmd|| hw.HardwareType == HardwareType.GpuNvidia)));

        private static CPUUpdater _cpu;
        public static CPUUpdater CPU => _cpu ?? (_cpu = new CPUUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.Cpu)));

        private static RAMUpdater _ram;
        public static RAMUpdater RAM => _ram ?? (_ram = new RAMUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.Memory)));

        static HardwareMonitor()
        {
            _computer = new Computer()
            {
                IsCpuEnabled = true,
                IsGpuEnabled = true,
                IsMemoryEnabled = true
            };
            try
            {
                _computer.Open();
                _hardware = _computer.Hardware;
            }
            catch (Exception e)
            {
                Global.logger.Error(e);
            }
        }

        private static ISensor FindSensor(this IHardware hardware, string identifier)
        {
            return Array.Find(hardware.Sensors, s => s.Identifier.ToString().Contains(identifier));
        }

        public abstract class HardwareUpdater
        {
            protected IHardware hw;
            protected bool inUse;
            public int Interval { get; }
            private readonly Timer _useTimer;
            private readonly Timer _updateTimer;

            protected HardwareUpdater()
            {
                _useTimer = new Timer(5000);
                _useTimer.Elapsed += (a, b) =>
                {
                    inUse = false;
                    _useTimer.Stop();
                };
                _useTimer.Start();

                _updateTimer = new Timer(Interval);
                _updateTimer.Elapsed += (a, b) =>
                {
                    if (inUse)
                        hw.Update();
                };
                _updateTimer.Start();
            }

            protected float GetValue(ISensor sensor)
            {
                inUse = true;
                _useTimer.Stop();
                _useTimer.Start();
                return sensor.Value ?? 0;
            }

            public void SetUpdateTimer(int interval)
            {
                _updateTimer.Interval = interval;
                _updateTimer.Stop();<commit_msg>fixed interval missing<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using LibreHardwareMonitor.Hardware;
using Computer = LibreHardwareMonitor.Hardware.Computer;

namespace Aurora.Utils
{
    public static class HardwareMonitor
    {
        private static readonly Computer _computer;
        private static readonly IEnumerable<IHardware> _hardware;

        private static GPUUpdater _gpu;
        public static GPUUpdater GPU => _gpu ?? (_gpu = new GPUUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.GpuAmd|| hw.HardwareType == HardwareType.GpuNvidia)));

        private static CPUUpdater _cpu;
        public static CPUUpdater CPU => _cpu ?? (_cpu = new CPUUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.Cpu)));

        private static RAMUpdater _ram;
        public static RAMUpdater RAM => _ram ?? (_ram = new RAMUpdater(_hardware.FirstOrDefault(hw => hw.HardwareType == HardwareType.Memory)));

        static HardwareMonitor()
        {
            _computer = new Computer()
            {
                IsCpuEnabled = true,
                IsGpuEnabled = true,
                IsMemoryEnabled = true
            };
            try
            {
                _computer.Open();
                _hardware = _computer.Hardware;
            }
            catch (Exception e)
            {
                Global.logger.Error(e);
            }
        }

        private static ISensor FindSensor(this IHardware hardware, string identifier)
        {
            return Array.Find(hardware.Sensors, s => s.Identifier.ToString().Contains(identifier));
        }

        public abstract class HardwareUpdater
        {
            protected IHardware hw;
            protected bool inUse;

            private readonly Timer _useTimer;
            private readonly Timer _updateTimer;

            protected HardwareUpdater()
            {
                _useTimer = new Timer(5000);
                _useTimer.Elapsed += (a, b) =>
                {
                    inUse = false;
                    _useTimer.Stop();
                };
                _useTimer.Start();

                _updateTimer = new Timer(200);
                _updateTimer.Elapsed += (a, b) =>
                {
                    if (inUse)
                        hw.Update();
                };
                _updateTimer.Start();
            }

            protected float GetValue(ISensor sensor)
            {
                inUse = true;
                _useTimer.Stop();
                _useTimer.Start();
                return sensor.Value ?? 0;
            }

            public void SetUpdateTimer(int interval)
            {
                _updateTimer.Interval = interval;
                _updateTimer.Stop();
{'fraction_non_alphanumeric': 0.08217993079584775, 'fraction_numerical': 0.002249134948096886, 'mean_word_length': 1.5808035714285715, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3745396', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1418, 'n_words': 337}