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.
66 lines
2.0 KiB
66 lines
2.0 KiB
using Application.Models;
|
|
using Infrastructure.Extensions;
|
|
using IoT.Shared.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoT.Shared.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 deviceService = scope.ServiceProvider.GetService<NodeService>();
|
|
return deviceService.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();
|
|
}
|
|
}
|
|
} |