using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System; using System.Reflection; using System.Threading.Tasks; namespace IoTDameon.Controllers { public class HomeController : Controller { private readonly IWebHostEnvironment _env; private readonly UpdateIoTNodeService _service; private readonly IHubContext _hub; public HomeController(IWebHostEnvironment env, UpdateIoTNodeService service, IHubContext hub) { this._env = env; this._service = service; this._hub = hub; } public IActionResult Index() { return View(); } public IActionResult Command(string command) { if (!string.IsNullOrEmpty(command)) { try { this._hub.Clients.All.SendAsync("ServerToClient", command.Bash()); } catch (Exception ex) { this._hub.Clients.All.SendAsync("ServerToClient", ex.ToString()); return Problem(ex.Message); } } return Ok(); } public IActionResult Update() { try { Task.Run(() => { this._service.Update(); }); } catch (Exception ex) { this._hub.Clients.All.SendAsync("ServerToClient", ex.ToString()); return Problem(ex.Message); } return Ok(); } public IActionResult GetVersion() { return Content(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); } } }