<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KLibrary.Labs.ObservableModel;
using Microsoft.Kinect;

namespace DepthMonitor2
{
    public class AsyncKinectManager
    {
        public ISettableProperty<KinectSensor> Sensor { get; private set; }

        // アプリケーション起動時に既にデバイスが接続されている場合には発生しません。
        public ISettableProperty<KinectSensor> SensorConnected { get; private set; }
        public ISettableProperty<KinectSensor> SensorDisconnected { get; private set; }

        public AsyncKinectManager()
        {
            Sensor = ObservableProperty.CreateSettable<KinectSensor>(FindSensor());

            SensorConnected = ObservableProperty.CreateSettable<KinectSensor>(null, true);
            SensorDisconnected = ObservableProperty.CreateSettable<KinectSensor>(null, true);

            Task.Run(() =>
            {
                KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
            });
        }

        void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            if (Sensor.Value == null)
            {
                if (e.Status == KinectStatus.Connected)
                {
                    Sensor.Value = e.Sensor;
                    SensorConnected.OnNext(Sensor.Value);
                }
            }
            else
            {
                if (e.Sensor == Sensor.Value && e.Status != KinectStatus.Connected)
                {
                    SensorDisconnected.OnNext(Sensor.Value);
                    Sensor.Value = FindSensor();
                    if (Sensor.Value != null) SensorConnected.OnNext(Sensor.Value);
                }
            }
        }

        static KinectSensor FindSensor()
        {
            return KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected);
        }
    }
}
<commit_msg>Add Initialize method<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using KLibrary.Labs.ObservableModel;
using Microsoft.Kinect;

namespace DepthMonitor2
{
    public class AsyncKinectManager
    {
        ISettableProperty<KinectSensor> _Sensor;
        public IGetOnlyProperty<KinectSensor> Sensor { get; private set; }
        KinectSensor _sensorCache;

        // アプリケーション起動時に既にデバイスが接続されている場合も発生します。
        public IGetOnlyProperty<KinectSensor> SensorConnected { get; private set; }
        public IGetOnlyProperty<KinectSensor> SensorDisconnected { get; private set; }

        public AsyncKinectManager()
        {
            _Sensor = ObservableProperty.CreateSettable<KinectSensor>(null);
            Sensor = _Sensor.ToGetOnlyMask();

            SensorDisconnected = _Sensor
                .Select(s => _sensorCache)
                .Where(s => s != null)
                .ToGetOnly(null, true);
            SensorConnected = _Sensor
                .Do(s => _sensorCache = s)
                .Where(s => s != null)
                .ToGetOnly(null, true);
        }

        // SensorConnected を購読した後で呼び出してください。
        public void Initialize()
        {
            Task.Run(() =>
            {
                KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
                _Sensor.Value = FindSensor();
            });
        }

        void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            if (Sensor.Value == null)
            {
                if (e.Status == KinectStatus.Connected)
                {
                    _Sensor.Value = e.Sensor;
                }
            }
            else if (Sensor.Value == e.Sensor)
            {
                if (e.Status != KinectStatus.Connected)
                {
                    _Sensor.Value = FindSensor();
                }
            }
        }

        static KinectSensor FindSensor()
        {
            return KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected);
        }
    }
}

{'fraction_non_alphanumeric': 0.08477796247922109, 'fraction_numerical': 0.0004749465685110425, 'mean_word_length': 1.6607706885660138, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 22, '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': '7582066', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1210, 'n_words': 191}