From a9c3aaed4d5c007f138bfc16f05aecdee73f1268 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 21 Nov 2018 16:24:37 +0200 Subject: Working on PPC Storage Provider !!! --- .../Storage/DefaultStorageProvider.cs | 111 +++++++++++++++++++++ .../Tango.PPC.Common/Storage/IStorageProvider.cs | 35 +++++++ 2 files changed, 146 insertions(+) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/IStorageProvider.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/Storage') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs new file mode 100644 index 000000000..5714f5dfb --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.DI; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Threading; + +namespace Tango.PPC.Common.Storage +{ + /// + /// Represents the default . + /// + /// + /// + public class DefaultStorageProvider : ExtendedObject, IStorageProvider + { + private Thread _scanThread; + + /// + /// Occurs when a new storage drive has been inserted. + /// + public event EventHandler StorageConnected; + + /// + /// Occurs when a storage drive has been removed. + /// + public event EventHandler StorageDisconnected; + + private bool _isConnected; + /// + /// Gets a value indicating whether a storage drive is currently inserted. + /// + public bool IsConnected + { + get { return _isConnected; } + set { _isConnected = value; RaisePropertyChangedAuto(); } + } + + private DriveInfo _drive; + /// + /// Gets the inserted storage drive information. + /// + public DriveInfo Drive + { + get { return _drive; } + set { _drive = value; RaisePropertyChangedAuto(); } + } + + /// + /// Initializes a new instance of the class. + /// + public DefaultStorageProvider(IPPCApplicationManager applicationManager) + { + var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList(); + + if (drives.Count > 0) + { + IsConnected = true; + Drive = drives.FirstOrDefault(); + } + + applicationManager.ApplicationReady += ApplicationManager_ApplicationReady; + } + + /// + /// Handles the ApplicationReady event of the ApplicationManager. + /// + /// The source of the event. + /// The instance containing the event data. + private void ApplicationManager_ApplicationReady(object sender, EventArgs e) + { + Initialize(); + } + + private void Initialize() + { + _scanThread = new Thread(ScanThreadMethod); + _scanThread.IsBackground = true; + _scanThread.Start(); + } + + private void ScanThreadMethod() + { + while (true) + { + var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList(); + + if (IsConnected && !drives.Exists(x => x.RootDirectory.FullName == Drive.RootDirectory.FullName)) + { + StorageDisconnected?.Invoke(this, Drive); + Drive = null; + IsConnected = false; + } + + if (!IsConnected && drives.Count > 0) + { + Drive = drives.FirstOrDefault(); + IsConnected = true; + StorageConnected?.Invoke(this, Drive); + } + + Thread.Sleep(5000); + } + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/IStorageProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/IStorageProvider.cs new file mode 100644 index 000000000..164cdfe0a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/IStorageProvider.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.Storage +{ + /// + /// Represents a storage provider responsible of notifying about flash drives connection/disconnection. + /// + public interface IStorageProvider + { + /// + /// Occurs when a new storage drive has been inserted. + /// + event EventHandler StorageConnected; + + /// + /// Occurs when a storage drive has been removed. + /// + event EventHandler StorageDisconnected; + + /// + /// Gets a value indicating whether a storage drive is currently inserted. + /// + bool IsConnected { get; } + + /// + /// Gets the inserted storage drive information. + /// + DriveInfo Drive { get; } + } +} -- cgit v1.3.1