Skip to content

Commit

Permalink
Fix several issues with connect/disconnect workflow (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Oct 30, 2017
1 parent dd3bcdb commit 59ac398
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ private async void AddDeviceToList(DeviceInformation deviceInformation, String d
newNanoFrameworkDevice.DebugEngine = new Engine(this, newNanoFrameworkDevice);
newNanoFrameworkDevice.Transport = TransportType.Serial;

tentativeNanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase);

// perform check for valid nanoFramework device is this is not the initial enumeration
if (IsDevicesEnumerationComplete)
{
Expand All @@ -262,19 +264,20 @@ private async void AddDeviceToList(DeviceInformation deviceInformation, String d
NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase);
Debug.WriteLine($"New Serial device: {newNanoFrameworkDevice.Description} {(((NanoDevice<NanoSerialDevice>)newNanoFrameworkDevice).Device.DeviceInformation.DeviceInformation.Id)}");

// clear tentative list
tentativeNanoFrameworkDevices.Clear();

// done here
return;
}
}

Debug.WriteLine($"Removing { deviceInformation.Id } from collection...");
// clear tentative list
tentativeNanoFrameworkDevices.Clear();

// can't do anything with this one, better dispose it
newNanoFrameworkDevice.Dispose();
}
else
{
tentativeNanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase);
}
}
}
}
Expand All @@ -288,9 +291,10 @@ private void RemoveDeviceFromList(string deviceId)

SerialDevices.Remove(deviceEntry);

// get device
// get device...
var device = FindNanoFrameworkDevice(deviceId);
// yes, remove it from collection

// ... and remove it from collection
NanoFrameworkDevices.Remove(device);

device = null;
Expand Down Expand Up @@ -335,6 +339,10 @@ private NanoDeviceBase FindNanoFrameworkDevice(string deviceId)
// try now in tentative list
return tentativeNanoFrameworkDevices.FirstOrDefault(d => ((d as NanoDevice<NanoSerialDevice>).Device.DeviceInformation as SerialDeviceInformation).DeviceInformation.Id == deviceId);
}
else
{
return device;
}
}

return null;
Expand Down Expand Up @@ -403,6 +411,9 @@ private void OnDeviceEnumerationComplete(DeviceWatcher sender, object args)
// all watchers have completed enumeration
IsDevicesEnumerationComplete = true;

// clean list of tentative nanoFramework Devices
tentativeNanoFrameworkDevices.Clear();

Debug.WriteLine($"Serial device enumeration completed. Found {NanoFrameworkDevices.Count} devices");

// fire event that Serial enumeration is complete
Expand Down Expand Up @@ -455,10 +466,14 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync()
if (serialNumber.Contains("NANO_"))
{
var device = FindNanoFrameworkDevice(EventHandlerForSerialDevice.Current.DeviceInformation.Id);
device.Description = serialNumber + " @ " + EventHandlerForSerialDevice.Current.Device.PortName;

// should be a valid nanoFramework device, done here
return true;
if (device != null)
{
device.Description = serialNumber + " @ " + EventHandlerForSerialDevice.Current.Device.PortName;

// should be a valid nanoFramework device, done here
return true;
}
}
}

Expand Down

0 comments on commit 59ac398

Please sign in to comment.