<commit_before>﻿using Aurora.Profiles;
using CSScriptLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;

namespace Aurora.Devices
{
    public class DeviceContainer
    {
        public Device Device { get; set; }

        public BackgroundWorker Worker = new BackgroundWorker();
        public Thread UpdateThread { get; set; } = null;

        private Tuple<DeviceColorComposition, bool> currentComp = null;
        private bool newFrame = false;

        public DeviceContainer(Device device)
        {
            this.Device = device;
            Worker.DoWork += WorkerOnDoWork;
            Worker.RunWorkerCompleted += (sender, args) =>
            {
                if (newFrame && !Worker.IsBusy)
                    Worker.RunWorkerAsync();
            };
            Worker.WorkerSupportsCancellation = true;
        }

        private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            newFrame = false;
            Device.UpdateDevice(currentComp.Item1, doWorkEventArgs,
                currentComp.Item2);
        }

        public void UpdateDevice(DeviceColorComposition composition, bool forced = false)
        {
            if (Worker.IsBusy)
                Worker.CancelAsync();

            newFrame = true;
            currentComp = new Tuple<DeviceColorComposition, bool>(composition, forced);

            lock (Worker)
            {
                try
                {
                    if (!Worker.IsBusy)
                        Worker.RunWorkerAsync();
                }
                catch(Exception e)
                {
                    Global.logger.LogLine(e.ToString(), Logging_Level.Error);
                }
            }
        }
    }

    public class DeviceManager : IDisposable
    {
        private List<DeviceContainer> devices = new List<DeviceContainer>();

        public DeviceContainer[] Devices { get { return devices.ToArray(); } }

        private bool anyInitialized = false;
        private bool retryActivated = false;
        private const int retryInterval = 5000;
        private const int retryAttemps = 3;
        private int retryAttemptsLeft = retryAttemps;
        private Thread retryThread;

        private bool _InitializeOnceAllowed = false;

        public int RetryAttempts
        {
            get
            {
                return retryAttemptsLeft;
            }
        }
        public event EventHandler NewDevicesInitialized;

        public DeviceManager()
        {
            devices.Add(new DeviceContainer(new Devices.Logitech.LogitechDevice()));         // Logitech Device
            devices.Add(new DeviceContainer(new Devices.Corsair.CorsairDevice()));           // Corsair Device<commit_msg>Hopefully actually fixed the issue with BackgroundWorker<commit_after>﻿using Aurora.Profiles;
using CSScriptLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;

namespace Aurora.Devices
{
    public class DeviceContainer
    {
        public Device Device { get; set; }

        public BackgroundWorker Worker = new BackgroundWorker();
        public Thread UpdateThread { get; set; } = null;

        private Tuple<DeviceColorComposition, bool> currentComp = null;
        private bool newFrame = false;

        public DeviceContainer(Device device)
        {
            this.Device = device;
            Worker.DoWork += WorkerOnDoWork;
            Worker.RunWorkerCompleted += (sender, args) =>
            {
                lock (Worker)
                {
                    if (newFrame && !Worker.IsBusy)
                        Worker.RunWorkerAsync();
                 }
            };
            //Worker.WorkerSupportsCancellation = true;
        }

        private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            newFrame = false;
            Device.UpdateDevice(currentComp.Item1, doWorkEventArgs,
                currentComp.Item2);
        }

        public void UpdateDevice(DeviceColorComposition composition, bool forced = false)
        {
            newFrame = true;
            currentComp = new Tuple<DeviceColorComposition, bool>(composition, forced);
            lock (Worker)
            {
                if (Worker.IsBusy)
                    return;
                else
                    Worker.RunWorkerAsync();
            }
            /*lock (Worker)
            {
                try
                {
                    if (!Worker.IsBusy)
                        Worker.RunWorkerAsync();
                }
                catch(Exception e)
                {
                    Global.logger.LogLine(e.ToString(), Logging_Level.Error);
                }
            }*/
        }
    }

    public class DeviceManager : IDisposable
    {
        private List<DeviceContainer> devices = new List<DeviceContainer>();

        public DeviceContainer[] Devices { get { return devices.ToArray(); } }

        private bool anyInitialized = false;
        private bool retryActivated = false;
        private const int retryInterval = 5000;
        private const int retryAttemps = 3;
        private int retryAttemptsLeft = retryAttemps;
        private Thread retryThread;

        private bool _InitializeOnceAllowed = false;

        public int RetryAttempts
        {
            get
            {
                return retryAttemptsLeft;
            }
        }
        public event EventHandler NewDevicesInitialized;

        public DeviceManager()
        {
            devices.Add(new DeviceContainer(new Devices.Logitech.LogitechDevice()));         // Logitech Device
            devices.Add(new DeviceContainer(new Devices.Corsair.CorsairDevice()));           // Corsair Device
{'fraction_non_alphanumeric': 0.07416750756811302, 'fraction_numerical': 0.0023545240497813654, 'mean_word_length': 1.7204940530649588, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '1558969', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1385, 'n_words': 347}