fix: improve error handling and await device actions in RoomCombinationScenario

This commit is contained in:
jtalborough
2025-01-28 09:00:21 -05:00
parent cc724ddf19
commit 4ef481375c
2 changed files with 17 additions and 16 deletions

View File

@@ -1,5 +1,3 @@
using Crestron.SimplSharp;
using Newtonsoft.Json;
using PepperDash.Core;
@@ -11,7 +9,6 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace PepperDash.Essentials.Core
{
public class DeviceJsonApi
@@ -141,18 +138,26 @@ namespace PepperDash.Essentials.Core
.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)
{

View File

@@ -81,12 +81,10 @@ namespace PepperDash.Essentials.Core
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;
}
@@ -101,12 +99,10 @@ namespace PepperDash.Essentials.Core
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;
}