Skip to content

Commit

Permalink
Revert "Commented out, unused/referenced code for now. Moved MeadowFo…
Browse files Browse the repository at this point in the history
…rMeadowAttach to where it is used most, cleaning up ListernProc."

This reverts commit e7aab15.
  • Loading branch information
CartBlanche committed Sep 9, 2024
1 parent e7aab15 commit fd9d099
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
38 changes: 38 additions & 0 deletions Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ public partial class SerialConnection

public event EventHandler<Exception> FileException = delegate { };

public override async Task WaitForMeadowAttach(CancellationToken? cancellationToken)
{
var timeout = 500;

while (timeout-- > 0)
{
if (cancellationToken?.IsCancellationRequested ?? false) throw new TaskCanceledException();
if (timeout <= 0) throw new TimeoutException();

if (State == ConnectionState.MeadowAttached)
{
if (Device == null)
{
// no device set - this happens when we are waiting for attach from DFU mode
await Attach(cancellationToken, 5);
}

return;
}

await Task.Delay(20);

if (!_port.IsOpen)
{
try
{
Open();
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to open port: {ex.Message}");
}
}
}

throw new TimeoutException();
}

private async Task ListenerProc()
{
var readBuffer = new byte[ReadBufferSizeBytes];
Expand Down
52 changes: 11 additions & 41 deletions Source/v2/Meadow.HCom/Connections/SerialConnection.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public partial class SerialConnection : ConnectionBase, IDisposable
private readonly ILogger? _logger;
private bool _isDisposed;
private ConnectionState _state;
// TODO Not currently used private readonly List<IConnectionListener> _listeners = new List<IConnectionListener>();
private readonly List<IConnectionListener> _listeners = new List<IConnectionListener>();
private readonly ConcurrentQueue<IRequest> _pendingCommands = new ConcurrentQueue<IRequest>();
// TODO Not currently used private bool _maintainConnection;
// TODO Not currently used private Thread? _connectionManager = null;
private bool _maintainConnection;
private Thread? _connectionManager = null;
private readonly List<string> _textList = new List<string>();
private int _messageCount = 0;
private ReadFileInfo? _readFileInfo = null;
Expand Down Expand Up @@ -58,7 +58,7 @@ public SerialConnection(string port, ILogger? logger = default)
.Start();
}

/* TODO Not currently used private bool MaintainConnection
private bool MaintainConnection
{
get => _maintainConnection;
set
Expand Down Expand Up @@ -127,7 +127,7 @@ public void RemoveListener(IConnectionListener listener)
}

// TODO: stop maintaining connection?
}*/
}

public override ConnectionState State
{
Expand All @@ -148,10 +148,7 @@ private void Open()
{
try
{
Debug.WriteLine($"Opening '{_port.PortName}' port...");
_port.Open();
Debug.WriteLine($"Opened '{_port.PortName}' port...");
State = ConnectionState.Connected;
}
catch (FileNotFoundException)
{
Expand All @@ -165,26 +162,27 @@ private void Open()
{
throw new Exception($"Unable to open port '{_port.PortName}' - {ex.Message}");
}

State = ConnectionState.Connected;
}
}

private void Close()
{
if (_port.IsOpen)
{
Debug.WriteLine($"Closing '{_port.PortName}' port...");
_port.Close();
Debug.WriteLine($"Closed '{_port.PortName}' port...");
State = ConnectionState.Disconnected;
}

State = ConnectionState.Disconnected;
}

public override void Detach()
{
/* TODO Not currently usedif (MaintainConnection)
if (MaintainConnection)
{
// TODO: close this up
}*/
}

Close();
}
Expand Down Expand Up @@ -600,34 +598,6 @@ private async Task<bool> WaitForConcluded(RequestType? requestType = null, Cance
}, cancellationToken);
}

public override async Task WaitForMeadowAttach(CancellationToken? cancellationToken)
{
var timeout = 500;

while (timeout-- > 0)
{
if (cancellationToken?.IsCancellationRequested ?? false) throw new TaskCanceledException();
if (timeout <= 0) throw new TimeoutException();

if (State == ConnectionState.MeadowAttached)
{
if (Device == null)
{
// no device set - this happens when we are waiting for attach from DFU mode
await Attach(cancellationToken, 5);
}

return;
}

await Task.Delay(20);

Open();
}

throw new TimeoutException();
}

public override async Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null)
{
var command = RequestBuilder.Build<SetRtcTimeRequest>();
Expand Down

0 comments on commit fd9d099

Please sign in to comment.