Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled cache use on discovery modes #201

Merged
merged 3 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SharpBrick.PoweredUp.Cli/Commands/DevicesList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public DevicesList(ILegoWirelessProtocol protocol, DiscoverPorts discoverPorts)
public async Task ExecuteAsync(SystemType knownSystemType)
{
Console.WriteLine("Discover Ports. Receiving Messages ...");
protocol.DiscoveryMode = true;

await protocol.ConnectAsync(knownSystemType); // registering to bluetooth notification

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public DumpStaticPortInfo(ILegoWirelessProtocol protocol, DiscoverPorts discover
public async Task ExecuteAsync(SystemType knownSystemType, byte portId, bool headerEnabled)
{
Console.WriteLine($"Discover Port {portId}. Receiving Messages ...");
protocol.DiscoveryMode = true;

await protocol.ConnectAsync(knownSystemType); // registering to bluetooth notification

Expand Down
6 changes: 6 additions & 0 deletions src/SharpBrick.PoweredUp/Protocol/ILegoWirelessProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public interface ILegoWirelessProtocol : IDisposable
ProtocolKnowledge Knowledge { get; }

IServiceProvider ServiceProvider { get; }

/// <summary>
/// Indicates if this is running in normal mode, using known configurations/behavior of hub and devices (default).
/// Or in discovery mode where all hub and devices are queried for their knowledge and any known configuration is ignored.
/// </summary>
bool DiscoveryMode { get; set; }
}
22 changes: 19 additions & 3 deletions src/SharpBrick.PoweredUp/Protocol/Knowledge/KnowledgeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ public static bool ApplyStaticProtocolKnowledge(LegoWirelessMessage message, Pro

return applicableMessage;
}
public static Task ApplyDynamicProtocolKnowledge(LegoWirelessMessage message, ProtocolKnowledge knowledge, IDeviceFactory deviceFactory)

/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="knowledge"></param>
/// <param name="deviceFactory"></param>
/// <param name="useCachedInformation">Indicates to apply known information about reported devices.
/// On normal operation this should be used a discovery can takes tens of seconds. Don't use this on discovery as the cached information will be mixed with received information.</param>
/// <returns></returns>
public static Task ApplyDynamicProtocolKnowledge(LegoWirelessMessage message, ProtocolKnowledge knowledge, IDeviceFactory deviceFactory, bool useCachedInformation = true)
{
HubInfo hub;
PortInfo port;
Expand All @@ -125,7 +135,10 @@ public static Task ApplyDynamicProtocolKnowledge(LegoWirelessMessage message, Pr
port.HardwareRevision = msg.HardwareRevision;
port.SoftwareRevision = msg.SoftwareRevision;

AddCachePortAndPortModeInformation(msg.IOTypeId, msg.HardwareRevision, msg.SoftwareRevision, hub, port, knowledge, deviceFactory);
if (useCachedInformation)
{
AddCachePortAndPortModeInformation(msg.IOTypeId, msg.HardwareRevision, msg.SoftwareRevision, hub, port, knowledge, deviceFactory);
}
break;
case HubAttachedIOForDetachedDeviceMessage msg:
port = knowledge.Port(msg.HubId, msg.PortId);
Expand All @@ -144,7 +157,10 @@ public static Task ApplyDynamicProtocolKnowledge(LegoWirelessMessage message, Pr
port.HardwareRevision = partOfVirtual.HardwareRevision;
port.SoftwareRevision = partOfVirtual.SoftwareRevision;

AddCachePortAndPortModeInformation(msg.IOTypeId, partOfVirtual.HardwareRevision, partOfVirtual.SoftwareRevision, hub, port, knowledge, deviceFactory);
if (useCachedInformation)
{
AddCachePortAndPortModeInformation(msg.IOTypeId, partOfVirtual.HardwareRevision, partOfVirtual.SoftwareRevision, hub, port, knowledge, deviceFactory);
}

port.IsVirtual = true;
port.PortAId = msg.PortAId;
Expand Down
16 changes: 12 additions & 4 deletions src/SharpBrick.PoweredUp/Protocol/LegoWirelessProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ public class LegoWirelessProtocol : ILegoWirelessProtocol
private readonly ILogger<LegoWirelessProtocol> _logger;
private readonly IDeviceFactory _deviceFactory;
private readonly Subject<(byte[] data, LegoWirelessMessage message)> _upstreamSubject;

public ProtocolKnowledge Knowledge { get; } = new ProtocolKnowledge();

public IObservable<(byte[] data, LegoWirelessMessage message)> UpstreamRawMessages => _upstreamSubject;
public IObservable<LegoWirelessMessage> UpstreamMessages => _upstreamSubject.Select(x => x.message);
public IServiceProvider ServiceProvider { get; }

/// <inheritdoc/>
public bool DiscoveryMode { get; set; } = false;

public LegoWirelessProtocol(BluetoothKernel kernel, ILogger<LegoWirelessProtocol> logger, IDeviceFactory deviceFactory, IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
Expand All @@ -39,7 +42,7 @@ public async Task ConnectAsync(SystemType knownSystemType = default)
await KnowledgeManager.ApplyDynamicProtocolKnowledge(new HubPropertyMessage<SystemType>(HubProperty.SystemTypeId, HubPropertyOperation.Update, knownSystemType)
{
HubId = 0x00,
}, Knowledge, _deviceFactory);
}, Knowledge, _deviceFactory, UseCachedInformation());

await _kernel.ConnectAsync();
_logger.LogInformation("Connected to device, getting protocol information");
Expand All @@ -49,7 +52,7 @@ await _kernel.ReceiveBytesAsync(async data =>
{
var message = MessageEncoder.Decode(data, Knowledge);

await KnowledgeManager.ApplyDynamicProtocolKnowledge(message, Knowledge, _deviceFactory);
await KnowledgeManager.ApplyDynamicProtocolKnowledge(message, Knowledge, _deviceFactory, UseCachedInformation());

_upstreamSubject.OnNext((data, message));
}
Expand All @@ -73,7 +76,7 @@ public async Task SendMessageAsync(LegoWirelessMessage message)
{
var data = MessageEncoder.Encode(message, Knowledge);

await KnowledgeManager.ApplyDynamicProtocolKnowledge(message, Knowledge, _deviceFactory);
await KnowledgeManager.ApplyDynamicProtocolKnowledge(message, Knowledge, _deviceFactory, UseCachedInformation());

await _kernel.SendBytesAsync(data);
}
Expand All @@ -84,6 +87,11 @@ public async Task SendMessageAsync(LegoWirelessMessage message)
throw;
}
}

private bool UseCachedInformation()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only feedback here: Why using a method here instead of a property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, habit I guess :)

{
return !DiscoveryMode;
}

#region Disposable Pattern
private bool disposedValue;
Expand Down
Loading