using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Application.Domain.Entities; using Application.Models; using Application.Services; using Infrastructure.Data; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace IoTNode.Controllers { public class CommandController : Controller { private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; private readonly IRepository _deviceRepo; private readonly IRepository _senceRepo; private readonly INodeService _nodeService; public CommandController(ILogger logger, IHttpClientFactory httpClientFactory, IRepository deviceRepo, IRepository senceRepo, INodeService nodeService) { this._logger = logger; this._httpClientFactory = httpClientFactory; this._deviceRepo = deviceRepo; this._senceRepo = senceRepo; this._nodeService = nodeService; } public IActionResult Exec(string node, string cmd, Guid id) { try { var query = string.Empty; foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id"))) { query = query.SetParam(item.Key, item.Value); } return Json(this._nodeService.Exec(id, cmd, query)); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } public IActionResult ExecAll(string node, string cmd, List id) { try { var query = string.Empty; foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id"))) { query = query.SetParam(item.Key, item.Value); } return Json(this._nodeService.ExecAll(id, cmd, query)); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } public IActionResult Sence(string node, Guid id) { try { return Json(this._nodeService.Sence(id)); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } } }