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

fix: improve error handling and await device actions in RoomCombinati… #1211

Merged
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
25 changes: 15 additions & 10 deletions src/PepperDash.Essentials.Core/Devices/DeviceJsonApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


using Crestron.SimplSharp;
using Newtonsoft.Json;
using PepperDash.Core;
Expand All @@ -11,7 +9,6 @@
using System.Reflection;
using System.Threading.Tasks;


namespace PepperDash.Essentials.Core
{
public class DeviceJsonApi
Expand Down Expand Up @@ -141,18 +138,26 @@ public static async Task DoDeviceActionAsync(DeviceActionWrapper action)
.Select((p, i) => ConvertType(action.Params[i], p.ParameterType))
.ToArray();

await Task.Run(() =>
try
{
try
Debug.LogMessage(LogEventLevel.Verbose, "Calling method {methodName} on device {deviceKey} with {@params}", null, method.Name, action.DeviceKey, action.Params);
var result = method.Invoke(obj, convertedParams);

// If the method returns a Task, await it
if (result is Task task)
{
Debug.LogMessage(LogEventLevel.Verbose, "Calling method {methodName} on device {deviceKey} with {@params}", null, method.Name, action.DeviceKey, action.Params);
method.Invoke(obj, convertedParams);
await task;
}
catch (Exception e)
// If the method returns a Task<T>, await it
else if (result != null && result.GetType().IsGenericType && result.GetType().GetGenericTypeDefinition() == typeof(Task<>))
{
Debug.LogMessage(e, "Error invoking method {methodName} on device {deviceKey}", null, method.Name, action.DeviceKey);
await (Task)result;
}
});
}
catch (Exception e)
{
Debug.LogMessage(e, "Error invoking method {methodName} on device {deviceKey}", null, method.Name, action.DeviceKey);
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ public async Task Activate()
foreach (var action in activationActions)
{
this.LogInformation("Running Activation action {@action}", action);
tasks.Add(DeviceJsonApi.DoDeviceActionAsync(action));
await DeviceJsonApi.DoDeviceActionAsync(action);
}
}

await Task.WhenAll(tasks);

IsActive = true;
}

Expand All @@ -101,12 +99,10 @@ public async Task Deactivate()
foreach (var action in deactivationActions)
{
this.LogInformation("Running deactivation action {actionDeviceKey}:{actionMethod}", action.DeviceKey, action.MethodName);
tasks.Add( DeviceJsonApi.DoDeviceActionAsync(action));
await DeviceJsonApi.DoDeviceActionAsync(action);
}
}

await Task.WhenAll(tasks);

IsActive = false;
}

Expand Down