You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTNode/Controllers/BaseDeviceController.cs

83 lines
2.2 KiB

using Application.Models;
using Infrastructure.Extensions;
using IoT.Shared.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
namespace IoTNode.Controllers
{
public class BaseDeviceController : Controller
{
internal readonly IServiceProvider applicationService;
public BaseDeviceController(IServiceProvider applicationServices)
{
this.applicationService = applicationServices;
}
[ApiExplorerSettings(IgnoreApi = true)]
public string Api()
{
try
{
var prefix = $"/{RouteData.Values["controller"]}/";
using var scope = applicationService.CreateScope();
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
return iotNodeClient.GetApiJson(prefix);
}
catch (Exception ex)
{
ex.PrintStack();
return null;
}
}
#pragma warning disable CA1822 // Mark members as static
public ApiResponse Action(Func<object> action)
#pragma warning restore CA1822 // Mark members as static
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
try
{
return ApiResponse.Success(action());
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.GetMessage());
}
}
#pragma warning disable CA1822 // Mark members as static
public ApiResponse AsyncAction(Action action)
#pragma warning restore CA1822 // Mark members as static
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
try
{
Task.Run(() =>
{
action();
});
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.GetMessage());
}
return ApiResponse.AsyncSuccess();
}
}
}