diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-11-21 16:24:37 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-11-21 16:24:37 +0200 |
| commit | a9c3aaed4d5c007f138bfc16f05aecdee73f1268 (patch) | |
| tree | bc41f31dec6a0f96e4c6e16372f8884053c7c17f /Software/Visual_Studio/PPC/Tango.PPC.Common/Storage | |
| parent | 9ee373ebf7518c96fdf685da792568680dd7f135 (diff) | |
| download | Tango-a9c3aaed4d5c007f138bfc16f05aecdee73f1268.tar.gz Tango-a9c3aaed4d5c007f138bfc16f05aecdee73f1268.zip | |
Working on PPC Storage Provider !!!
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/Storage')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs | 111 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/IStorageProvider.cs | 35 |
2 files changed, 146 insertions, 0 deletions
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 +{ + /// <summary> + /// Represents the default <see cref="IStorageProvider"/>. + /// </summary> + /// <seealso cref="Tango.Core.ExtendedObject" /> + /// <seealso cref="Tango.PPC.Common.Storage.IStorageProvider" /> + public class DefaultStorageProvider : ExtendedObject, IStorageProvider + { + private Thread _scanThread; + + /// <summary> + /// Occurs when a new storage drive has been inserted. + /// </summary> + public event EventHandler<DriveInfo> StorageConnected; + + /// <summary> + /// Occurs when a storage drive has been removed. + /// </summary> + public event EventHandler<DriveInfo> StorageDisconnected; + + private bool _isConnected; + /// <summary> + /// Gets a value indicating whether a storage drive is currently inserted. + /// </summary> + public bool IsConnected + { + get { return _isConnected; } + set { _isConnected = value; RaisePropertyChangedAuto(); } + } + + private DriveInfo _drive; + /// <summary> + /// Gets the inserted storage drive information. + /// </summary> + public DriveInfo Drive + { + get { return _drive; } + set { _drive = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Initializes a new instance of the <see cref="DefaultStorageProvider"/> class. + /// </summary> + 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; + } + + /// <summary> + /// Handles the ApplicationReady event of the ApplicationManager. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> + 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 +{ + /// <summary> + /// Represents a storage provider responsible of notifying about flash drives connection/disconnection. + /// </summary> + public interface IStorageProvider + { + /// <summary> + /// Occurs when a new storage drive has been inserted. + /// </summary> + event EventHandler<DriveInfo> StorageConnected; + + /// <summary> + /// Occurs when a storage drive has been removed. + /// </summary> + event EventHandler<DriveInfo> StorageDisconnected; + + /// <summary> + /// Gets a value indicating whether a storage drive is currently inserted. + /// </summary> + bool IsConnected { get; } + + /// <summary> + /// Gets the inserted storage drive information. + /// </summary> + DriveInfo Drive { get; } + } +} |
