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 | |
| 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')
5 files changed, 157 insertions, 1 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs index ef8f1c2ad..c86cf148b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs @@ -17,6 +17,7 @@ namespace Tango.PPC.Common.Navigation MachineSetupView, MachineUpdateView, ExternalBridgeView, + StorageView, HomeModule, ShutdownView, } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs index fdcaa9830..3393689d1 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs @@ -13,6 +13,7 @@ using Tango.PPC.Common.ExternalBridge; using Tango.PPC.Common.Navigation; using Tango.PPC.Common.Notifications; using Tango.PPC.Common.Printing; +using Tango.PPC.Common.Storage; using Tango.Settings; using Tango.SharedUI; using static Tango.SharedUI.Controls.NavigationControl; @@ -81,6 +82,12 @@ namespace Tango.PPC.Common [TangoInject] public IConnectivityProvider ConnectivityProvider { get; set; } + /// <summary> + /// Gets or sets the storage provider. + /// </summary> + [TangoInject] + public IStorageProvider StorageProvider { get; set; } + private PPCSettings _settings; /// <summary> 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; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index fbb0a048b..eb90bfd77 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -153,6 +153,8 @@ <Compile Include="PPCModuleAttribute.cs" /> <Compile Include="PPCModuleBase.cs" /> <Compile Include="Printing\IPrintingManager.cs" /> + <Compile Include="Storage\DefaultStorageProvider.cs" /> + <Compile Include="Storage\IStorageProvider.cs" /> <Compile Include="Threading\IDispatcherProvider.cs" /> <Compile Include="Update\IPPCUpdateService.cs" /> <Compile Include="Update\LatestVersionRequest.cs" /> @@ -314,7 +316,7 @@ </Target> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> + <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> </VisualStudio> </ProjectExtensions> </Project>
\ No newline at end of file |
