<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MDKControl.Core.Models;
using Ble = Robotics.Mobile.Core.Bluetooth.LE;

namespace MDKControl.Core.Services
{
    public class BleMoCoBusCommService : MoCoBusCommServiceBase
    {
        private readonly Ble.IAdapter _adapter;
        private readonly AutoResetEvent _newRxBytesReceived = new AutoResetEvent(false);
        private ConcurrentQueue<byte[]> _rxBytesQueue = new ConcurrentQueue<byte[]>();
        private Ble.IDevice _device;
        private Ble.IService _moCoBusService;
        private Ble.ICharacteristic _moCoBusRxCharacteristic;
        private Ble.ICharacteristic _moCoBusTxCharacteristic;

        public BleMoCoBusCommService(Ble.IDevice device, Ble.IAdapter adapter)
        {
            Debug.WriteLine("Create BleMoCoBusCommService");
            
            _device = device;
            _adapter = adapter;

            _adapter.DeviceConnected += AdapterOnDeviceConnected;
            _adapter.DeviceDisconnected += AdapterOnDeviceDisconnected;
        }

        private void AdapterOnDeviceConnected(object sender, Ble.DeviceConnectionEventArgs e)
        {
            if (e.Device.ID != _device.ID)
                return;

            _device = e.Device;
            _device.ServicesDiscovered += DeviceOnServicesDiscovered;
            _device.DiscoverServices();
        }

        private void AdapterOnDeviceDisconnected(object sender, Ble.DeviceConnectionEventArgs e)
        {
            if (e.Device.ID != _device.ID)
                return;

            _device = e.Device;
            ConnectionState = ConnectionState.Disconnected;
        }

        private void DeviceOnServicesDiscovered(object sender, EventArgs e)
        {
            _moCoBusService = _device.Services
                .FirstOrDefault(svc => svc.ID == BleConstants.ServiceMoCoBus);
            if (_moCoBusService == null)
                return;

            _moCoBusService.CharacteristicsDiscovered += MoCoBusServiceOnCharacteristicsDiscovered;
            _moCoBusService.DiscoverCharacteristics();
        }

        private void MoCoBusServiceOnCharacteristicsDiscovered(object sender, EventArgs e)
        {
            _moCoBusRxCharacteristic = _moCoBusService.Characteristics
                .FirstOrDefault(ch => ch.ID == BleConstants.ServiceMoCoBusCharacteristicRx);
            _moCoBusTxCharacteristic = _moCoBusService.Characteristics
                .FirstOrDefault(ch => ch.ID == BleConstants.ServiceMoCoBusCharacteristicTx);

            if (_moCoBusRxCharacteristic != null)<commit_msg>Fixed duplicated connection event when reconnecting after device was disconnected<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MDKControl.Core.Models;
using Ble = Robotics.Mobile.Core.Bluetooth.LE;

namespace MDKControl.Core.Services
{
    public class BleMoCoBusCommService : MoCoBusCommServiceBase
    {
        private readonly Ble.IAdapter _adapter;
        private readonly AutoResetEvent _newRxBytesReceived = new AutoResetEvent(false);
        private ConcurrentQueue<byte[]> _rxBytesQueue = new ConcurrentQueue<byte[]>();
        private Ble.IDevice _device;
        private Ble.IService _moCoBusService;
        private Ble.ICharacteristic _moCoBusRxCharacteristic;
        private Ble.ICharacteristic _moCoBusTxCharacteristic;

        public BleMoCoBusCommService(Ble.IDevice device, Ble.IAdapter adapter)
        {
            Debug.WriteLine("Create BleMoCoBusCommService");
            
            _device = device;
            _adapter = adapter;

            _adapter.DeviceConnected += AdapterOnDeviceConnected;
            _adapter.DeviceDisconnected += AdapterOnDeviceDisconnected;
        }

        private void AdapterOnDeviceConnected(object sender, Ble.DeviceConnectionEventArgs e)
        {
            if (e.Device.ID != _device.ID)
                return;

            _device = e.Device;
            _device.ServicesDiscovered += DeviceOnServicesDiscovered;
            _device.DiscoverServices();
        }

        private void AdapterOnDeviceDisconnected(object sender, Ble.DeviceConnectionEventArgs e)
        {
            if (e.Device.ID != _device.ID)
                return;

            _device = e.Device;
            _device.ServicesDiscovered -= DeviceOnServicesDiscovered;
            try 
            {
                _moCoBusService.CharacteristicsDiscovered -= MoCoBusServiceOnCharacteristicsDiscovered;
            }
            catch
            {
                // ignore
            }
            try 
            {
                _moCoBusRxCharacteristic.ValueUpdated -= MoCoBusRxCharacteristicOnValueUpdated;
            }
            catch
            {
                // ignore
            }

            ConnectionState = ConnectionState.Disconnected;
        }

        private void DeviceOnServicesDiscovered(object sender, EventArgs e)
        {
            _moCoBusService = _device.Services
                .FirstOrDefault(svc => svc.ID == BleConstants.ServiceMoCoBus);
            if (_moCoBusService == null)
                return;

            _moCoBusService.CharacteristicsDiscovered += MoCoBusServiceOnCharacteristicsDiscovered;
            _moCoBusService.DiscoverCharacteristics();
        }

        private void MoCoBusServiceOnCharacteristicsDiscovered(object sender, EventArgs e)
        {
            _moCoBusRxCharacteristic = _moCoBusService.Characteristics
                .FirstOrDefault(ch => ch.ID == BleConstants.ServiceMoCoBusCharacteristicRx);
            _moCoBusTxCharacteristic = _moCoBusService.Characteristics
                .FirstOrDefault(ch => ch.ID == BleConstants.ServiceMoCoBusCharacteristicTx);

            if (_moCoBusRxCharacteristic != null)
{'fraction_non_alphanumeric': 0.06739312657166806, 'fraction_numerical': 0.0, 'mean_word_length': 2.401368301026226, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1555550', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1473, 'n_words': 282}