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(); return iotNodeClient.GetApiJson(prefix); } catch (Exception ex) { ex.PrintStack(); return null; } } #pragma warning disable CA1822 // Mark members as static public ApiResponse Action(Func 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(); } } }