Skip to content

Commit

Permalink
IApp clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jun 17, 2024
1 parent 5bf89d0 commit 170f36f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion source/Meadow.Core/Bases/AppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected AppBase()
/// <param name="state">An optional state object to pass to the Action</param>
public virtual void InvokeOnMainThread(Action<object?> action, object? state = null)
{
ExecutionContext.Run(executionContext, new ContextCallback(action), state);
action.Invoke(state);
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions source/Meadow.Core/Hardware/Communications/SerialPortBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ private void ReadThreadProc()
{
// if the event handler throws, we don't want this to die
Resolver.Log.Error($"Serial event handler threw: {ex.Message}");
// the serial handler threw, we need to prevent a tight loop, so just sleep
Thread.Sleep(1000);
}
}
else
Expand Down
24 changes: 8 additions & 16 deletions source/Meadow.Core/MeadowOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,13 @@ private static (Type DeviceType, Type? HardwareProviderType) FindDeviceTypeParam
{
var genericArgs = type.GetGenericArguments();

Resolver.Log.Info($"{type.Name} generic args:");
foreach (var arg in genericArgs)
{
Resolver.Log.Info(arg.Name);
if (typeof(IMeadowDevice).IsAssignableFrom(arg))
{
deviceType = arg;
}
//else if (typeof(IMeadowAppEmbeddedHardwareProvider<IMeadowAppEmbeddedHardware>).IsAssignableFrom(arg))
// not a fan of this magic string, but the generic param type is unknown to us, so we can't look for it
else if (arg.GetInterfaces().Any(i => i.Name.StartsWith("IMeadowAppEmbeddedHardwareProvider")))
{
hardwareProviderType = arg;
Expand All @@ -408,7 +406,6 @@ private static (Type appType, Type deviceType, Type? hardwareProviderType)? Find
throw new Exception("Cannot find an IApp implementation");
}

Resolver.Log.Info("IApps found:");
foreach (var app in allApps)
{
Resolver.Log.Info(app.Name);
Expand Down Expand Up @@ -679,29 +676,24 @@ private static bool Initialize(string[]? args, IApp? app)

if (createMethod != null)
{
Resolver.Log.Trace($"Creating provider '{hardwareProviderType.Name}'");
var provider = Activator.CreateInstance(hardwareProviderType);
Resolver.Log.Trace($"Creating hardware provider '{hardwareProviderType.Name}'");
// allow using non-public constructors
var provider = Activator.CreateInstance(
hardwareProviderType, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null, null, null);

Resolver.Log.Trace($"Using provider to create hardware...");
Resolver.Log.Trace($"Using hardware provider to create hardware...");
var hardware = createMethod.Invoke(provider, new object[] { CurrentDevice });

Resolver.Log.Trace($"Hardware is a {hardware.GetType().Name}");
appHardwareInstance = hardware as IMeadowAppEmbeddedHardware;
if (appHardwareInstance != null)
{
Resolver.Log.Trace($"Hardware is an IMeadowAppEmbeddedHardware");
}
}
else
{
Resolver.Log.Trace($"No appropriavte Create method found");
Resolver.Log.Warn($"No appropriate Create method found in {hardwareProviderType.Name}");
}

}
else
{
Resolver.Log.Trace($"No hardware provider type");
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 170f36f

Please sign in to comment.