Skip to content

Commit

Permalink
Work on debug component stability (#17)
Browse files Browse the repository at this point in the history
- rework to apply good practices on async/await
- add cancelation token to several methods (work in progress)
- rework code on send and receive on serial to reuse datareader/writer and semephore
- add a bunch of debug writelines
- work on test apps
- code clean-up
- update Nuget packages
- bumped Nuget version to preview 0.4.0-preview006

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes authored Jun 5, 2017
1 parent 7b12b03 commit 215cf86
Show file tree
Hide file tree
Showing 43 changed files with 675 additions and 419 deletions.
4 changes: 2 additions & 2 deletions source/USB Test App WPF/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
</configuration>
11 changes: 5 additions & 6 deletions source/USB Test App WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using USB_Test_App_WPF.ViewModel;

namespace USB_Test_App_WPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
public partial class App : System.Windows.Application
{
ViewModelLocator vml;

internal static PortBase NanoFrameworkSerialDebugClient;
public ViewModelLocator vml;

public App()
{
Expand All @@ -44,8 +41,10 @@ private void App_Activated(object sender, EventArgs e)

private INFSerialDebugClientService CreateSerialDebugClient()
{
//virtualApp = Windows.UI.Xaml.Application.Current;

// TODO: check app lifecycle
var serialDebugClient = PortBase.CreateInstanceForSerial("", App.Current);
var serialDebugClient = PortBase.CreateInstanceForSerial("");

return new NFSerialDebugClientService(serialDebugClient);
}
Expand Down
2 changes: 1 addition & 1 deletion source/USB Test App WPF/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<PropertyChanged />
<PropertyChanging />
Expand Down
2 changes: 2 additions & 0 deletions source/USB Test App WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
</DataGrid.Columns>
</DataGrid>
<Button Content="Connect" HorizontalAlignment="Left" Margin="59,219,0,0" VerticalAlignment="Top" Width="75" Click="ConnectDeviceButton_ClickAsync"/>
<Button Content="Ping" HorizontalAlignment="Left" Margin="59,259,0,0" VerticalAlignment="Top" Width="75" Click="PingButton_Click" />
<Button Content="Disconnect" HorizontalAlignment="Left" Margin="196,219,0,0" VerticalAlignment="Top" Width="75" Click="DisconnectDeviceButton_Click"/>

</Grid>
</Window>
62 changes: 54 additions & 8 deletions source/USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using USB_Test_App_WPF.ViewModel;

namespace USB_Test_App_WPF
Expand All @@ -32,21 +33,66 @@ private async void ConnectDeviceButton_ClickAsync(object sender, RoutedEventArgs
// disable button
(sender as Button).IsEnabled = false;

await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(async () =>
{

bool connectResult = await (DataContext as MainViewModel).AvailableDevices[0].DebugEngine.ConnectAsync(3, 1000);
bool connectResult = await (DataContext as MainViewModel).AvailableDevices[0].DebugEngine.ConnectAsync(3, 500);
//var di = await App.NETMFUsbDebugClient.MFDevices[0].GetDeviceInfoAsync();
var di = await (DataContext as MainViewModel).AvailableDevices[0].GetDeviceInfoAsync();
Debug.WriteLine("");
Debug.WriteLine("");
//Debug.WriteLine(di.ToString());
Debug.WriteLine("");
Debug.WriteLine("");
Debug.WriteLine("");
Debug.WriteLine("");
Debug.WriteLine(di.ToString());
Debug.WriteLine("");
Debug.WriteLine("");
}));

// enable button
(sender as Button).IsEnabled = true;

}

private async void PingButton_Click(object sender, RoutedEventArgs e)
{
// disable button
(sender as Button).IsEnabled = false;

await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(async () => {
var p = await (DataContext as MainViewModel).AvailableDevices[0].PingAsync();
Debug.WriteLine("");
Debug.WriteLine("");
Debug.WriteLine("Ping response: " + p.ToString());
Debug.WriteLine("");
Debug.WriteLine("");
}));

// enable button
(sender as Button).IsEnabled = true;

}

private object await(MainViewModel mainViewModel)
{
throw new NotImplementedException();
}

private void DisconnectDeviceButton_Click(object sender, RoutedEventArgs e)
{
// disable button
(sender as Button).IsEnabled = false;

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action( () => {
(DataContext as MainViewModel).AvailableDevices[0].DebugEngine.Disconnect();
}));

// enable button
(sender as Button).IsEnabled = true;
}
}
}
19 changes: 10 additions & 9 deletions source/USB Test App WPF/USB Test App WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>USB_Test_App_WPF</RootNamespace>
<AssemblyName>USB Test App WPF</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand All @@ -18,7 +18,7 @@
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
Expand Down Expand Up @@ -49,8 +49,8 @@
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference>
<Reference Include="PropertyChanged, Version=1.53.0.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>..\packages\PropertyChanged.Fody.1.53.0\lib\netstandard10\PropertyChanged.dll</HintPath>
<Reference Include="PropertyChanged, Version=2.1.1.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>..\packages\PropertyChanged.Fody.2.1.1\lib\netstandard1.0\PropertyChanged.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="PropertyChanging, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -130,21 +130,22 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<None Include="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\nanoFramework.Tools.DebugLibrary.Net\nanoFramework.Tools.DebugLibrary.Net.csproj">
<Project>{101d57ad-d22f-4905-a992-de15e723f164}</Project>
<Name>nanoFramework.Tools.DebugLibrary.Net</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="FodyWeavers.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Fody.2.0.0\build\netstandard1.4\Fody.targets" Condition="Exists('..\packages\Fody.2.0.0\build\netstandard1.4\Fody.targets')" />
<Import Project="..\packages\Fody.2.1.0\build\netstandard1.0\Fody.targets" Condition="Exists('..\packages\Fody.2.1.0\build\netstandard1.0\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.2.0.0\build\netstandard1.4\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.0.0\build\netstandard1.4\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Fody.2.1.0\build\netstandard1.0\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.1.0\build\netstandard1.0\Fody.targets'))" />
</Target>
</Project>
22 changes: 12 additions & 10 deletions source/USB Test App WPF/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Threading;

namespace USB_Test_App_WPF.ViewModel
{
Expand Down Expand Up @@ -53,8 +55,10 @@ private void SerialDebugClient_DeviceEnumerationCompleted(object sender, EventAr
SerialDebugService.SerialDebugClient.DeviceEnumerationCompleted -= SerialDebugClient_DeviceEnumerationCompleted;
//WindowWrapper.Current().Dispatcher.Dispatch(() =>
//{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
SelectedTransportType = TransportType.Serial;
UpdateAvailableDevices();
}));
//});
}

Expand All @@ -66,15 +70,14 @@ private void UpdateAvailableDevices()
{
case TransportType.Serial:

//WindowWrapper.Current().Dispatcher.Dispatch(() =>
//{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
//BusySrv.ShowBusy(Res.GetString("HC_Searching"));
AvailableDevices = new ObservableCollection<NanoDeviceBase>(SerialDebugService.SerialDebugClient.NanoFrameworkDevices);
SerialDebugService.SerialDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged;
// if there's just one, select it
SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null;
//BusySrv.HideBusy();
//});
SerialDebugService.SerialDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged;
// if there's just one, select it
SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null;
//BusySrv.HideBusy();
}));
break;

case TransportType.Usb:
Expand Down Expand Up @@ -107,8 +110,7 @@ private void UpdateAvailableDevices()

private void NanoFrameworkDevices_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//WindowWrapper.Current().Dispatcher.Dispatch(() =>
//{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
// handle this according to the selected device type
switch (SelectedTransportType)
{
Expand All @@ -128,7 +130,7 @@ private void NanoFrameworkDevices_CollectionChanged(object sender, System.Collec
// if there's just one, select it
SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null;
//});
}));
}

public NanoDeviceBase SelectedDevice { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions source/USB Test App WPF/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="net462" />
<package id="Fody" version="2.0.0" targetFramework="net462" developmentDependency="true" />
<package id="Fody" version="2.1.0" targetFramework="net46" developmentDependency="true" />
<package id="MvvmLight" version="5.3.0.0" targetFramework="net462" />
<package id="MvvmLightLibs" version="5.3.0.0" targetFramework="net462" />
<package id="PropertyChanged.Fody" version="1.53.0" targetFramework="net462" developmentDependency="true" />
<package id="PropertyChanged.Fody" version="2.1.1" targetFramework="net46" developmentDependency="true" />
<package id="PropertyChanging.Fody" version="1.28.0" targetFramework="net462" developmentDependency="true" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
//

using nanoFramework.Tools.Debugger.PortSerial;
using nanoFramework.Tools.Debugger.Usb;
using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace nanoFramework.Tools.Debugger
{
public abstract partial class PortBase
{
public static PortBase CreateInstanceForSerial(string displayName, Application callerApp)
public static PortBase CreateInstanceForSerial(string displayName, object callerApp = null)
{
return new SerialPort(callerApp);
}

//public static PortBase CreateInstanceForUsb(string displayName, Application callerApp)
//{
// return new UsbPort(callerApp);
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ public partial class EventHandlerForSerialDevice

private SuspendingEventHandler appSuspendCallback;

// A pointer back to the calling app. This is needed to reach methods and events there
private static System.Windows.Application _callerApp;
public static System.Windows.Application CallerApp
{
private get { return _callerApp; }
set { _callerApp = value; }
}

/// <summary>
/// Register for app suspension/resume events. See the comments
/// for the event handlers for more information on what is being done to the device.
Expand All @@ -45,18 +37,18 @@ private void RegisterForAppEvents()
appResumeEventHandler = new EventHandler(Current.OnAppResume);

// This event is raised when the app is exited and when the app is suspended
_callerApp.Deactivated += appSuspendEventHandler;
CallerApp.Deactivated += appSuspendEventHandler;

_callerApp.Activated += appResumeEventHandler;
CallerApp.Activated += appResumeEventHandler;
}

private void UnregisterFromAppEvents()
{
// This event is raised when the app is exited and when the app is suspended
_callerApp.Deactivated -= appSuspendEventHandler;
CallerApp.Deactivated -= appSuspendEventHandler;
appSuspendEventHandler = null;

_callerApp.Activated -= appResumeEventHandler;
CallerApp.Activated -= appResumeEventHandler;
appResumeEventHandler = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@

namespace nanoFramework.Tools.Debugger.PortSerial
{
public partial class SerialPort : PortBase, IPort
{
/// <summary>
/// Creates an Serial debug client
/// </summary>
public SerialPort(Application callerApp)
{
mapDeviceWatchersToDeviceSelector = new Dictionary<DeviceWatcher, String>();
NanoFrameworkDevices = new ObservableCollection<NanoDeviceBase>();
SerialDevices = new List<Serial.SerialDeviceInformation>();
//public partial class SerialPort : PortBase, IPort
//{
///// <summary>
///// Creates an Serial debug client
///// </summary>
//public SerialPort(Application callerApp)
//{
// mapDeviceWatchersToDeviceSelector = new Dictionary<DeviceWatcher, String>();
// NanoFrameworkDevices = new ObservableCollection<NanoDeviceBase>();
// SerialDevices = new List<Serial.SerialDeviceInformation>();

// set caller app property
EventHandlerForSerialDevice.CallerApp = callerApp;
// // set caller app property
// EventHandlerForSerialDevice.CallerApp = callerApp;

// init semaphore
semaphore = new SemaphoreSlim(1, 1);
// // if callerApp is null means this a background task
// if (callerApp == null)
// {
// IsRunningOnBackground = true;
// }

Task.Factory.StartNew(() =>
{
StartSerialDeviceWatchers();
});
}
}
// // init semaphore
// semaphore = new SemaphoreSlim(1, 1);

// Task.Factory.StartNew(() =>
// {
// StartSerialDeviceWatchers();
// });
//}
//}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.3.*")]
[assembly: AssemblyFileVersion("0.3.5.0")]
[assembly: AssemblyVersion("0.4.*")]
[assembly: AssemblyFileVersion("0.4.0.0")]
Loading

0 comments on commit 215cf86

Please sign in to comment.