Skip to content

Commit

Permalink
[icue-link] handle reconnect and change mode when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanMulawski committed Apr 13, 2024
1 parent 39c354b commit 248fbe2
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/devices/icue_link/ICueLinkHubDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private static class DataTypes
private readonly IHidDeviceProxy _device;
private readonly IDeviceGuardManager _guardManager;
private readonly byte _pumpPowerMinimum;
private bool _isChangingDeviceMode;
private readonly ChannelTrackingStore _requestedChannelPower = new();
private readonly Dictionary<int, SpeedSensor> _speedSensors = new();
private readonly Dictionary<int, TemperatureSensor> _temperatureSensors = new();
Expand Down Expand Up @@ -78,6 +79,7 @@ public override bool Connect()
if (opened)
{
Initialize();
_device.OnReconnect(() => _ = TryChangeDeviceMode());
return true;
}

Expand All @@ -89,19 +91,34 @@ public override bool Connect()
return false;
}

private void Initialize()
private bool TryChangeDeviceMode()
{
if (CanLogDebug)
if (_isChangingDeviceMode)
{
var fw = GetFirmwareVersion();
LogDebug($"FW: {fw}");
LogWarning("Device mode change requested during device mode change.");
return false;
}

using (_guardManager.AwaitExclusiveAccess())
{
_isChangingDeviceMode = true;
LogInfo("Changing device mode to software-controlled");
SendCommand(Commands.EnterSoftwareMode);
_isChangingDeviceMode = false;
}

return true;
}

private void Initialize()
{
if (CanLogDebug)
{
var fw = GetFirmwareVersion();
LogDebug($"FW: {fw}");
}

TryChangeDeviceMode();
RefreshImpl(initialize: true);
}

Expand Down Expand Up @@ -375,12 +392,32 @@ private void Write(byte[] buffer)
LogDebug($"WRITE: {buffer.ToHexString()}");
}

_device.Write(buffer);
try
{
_device.Write(buffer);
}
catch (TimeoutException)
{
if (!TryChangeDeviceMode())
{
throw;
}
}
}

private void Read(byte[] buffer)
{
_device.Read(buffer);
try
{
_device.Read(buffer);
}
catch (TimeoutException)
{
if (!TryChangeDeviceMode())
{
throw;
}
}

if (CanLogDebug)
{
Expand Down

0 comments on commit 248fbe2

Please sign in to comment.