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

62 lines
1.9 KiB

using Application.Models;
using Infrastructure.Extensions;
using IoT.Shared.Services;
using IoTNode.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)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
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;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
public ApiResponse AsyncAction(Action action)
{
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();
}
}
}