Skip to content

Commit

Permalink
Commented out, unused/referenced code for now. Moved MeadowForMeadowA…
Browse files Browse the repository at this point in the history
…ttach to where it is used most, cleaning up ListernProc.
  • Loading branch information
CartBlanche committed Sep 9, 2024
1 parent f6d8811 commit e7aab15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
38 changes: 0 additions & 38 deletions Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@ 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: 41 additions & 11 deletions Source/v2/Meadow.HCom/Connections/SerialConnection.cs
100755 → 100644
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;
private readonly List<IConnectionListener> _listeners = new List<IConnectionListener>();
// TODO Not currently used private readonly List<IConnectionListener> _listeners = new List<IConnectionListener>();
private readonly ConcurrentQueue<IRequest> _pendingCommands = new ConcurrentQueue<IRequest>();
private bool _maintainConnection;
private Thread? _connectionManager = null;
// TODO Not currently used private bool _maintainConnection;
// TODO Not currently used 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();
}

private bool MaintainConnection
/* TODO Not currently used 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,7 +148,10 @@ 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 @@ -162,27 +165,26 @@ 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()
{
if (MaintainConnection)
/* TODO Not currently usedif (MaintainConnection)
{
// TODO: close this up
}
}*/

Close();
}
Expand Down Expand Up @@ -598,6 +600,34 @@ 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 e7aab15

Please sign in to comment.