using Application.Domain.Entities; using Application.Models; using Infrastructure.Application.Services.Settings; using Infrastructure.Data; using IoTCenter.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading; namespace IoTCenter.Api.Controllers { [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[controller]/[action]")] [ApiController] public class ApiController : ControllerBase { private readonly ISettingService _settingService; private readonly ILogger _logger; private readonly IRepository _sceneRepo; private readonly IRepository _sceneCommandRepo; private readonly IRepository _sceneTimerRepo; private readonly IRepository _commandRepo; private readonly IRepository _deviceRepo; private readonly IHubContext _hub; public ApiController( ISettingService settingService, ILogger logger, IRepository sceneRepo, IRepository sceneCommandRepo, IRepository sceneTimerRepo, IRepository commandRepo, IRepository deviceRepo, IHubContext hub) { this._settingService = settingService; this._logger = logger; this._sceneRepo = sceneRepo; this._sceneTimerRepo = sceneTimerRepo; this._sceneCommandRepo = sceneCommandRepo; this._commandRepo = commandRepo; this._deviceRepo = deviceRepo; this._hub = hub; } [HttpPost] public ActionResult ExecApi([FromBody] ApiRequestModel model) { try { this.CallApi(model.ConnectionId, model.Number, model.Method, model.Query); return Ok(); } catch (Exception ex) { this._logger.LogError(ex.ToString()); return Problem(ex.Message); } } [HttpPost] public ActionResult ExecScene([FromBody][Required] Guid id) { try { var scene = this._sceneRepo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Id == id); if (scene != null) { if (scene.NodeId != null) { this._hub.ServerToClient(Methods.ExecSceneRequest, id, scene.Node.Number, null); } else { var commands = this._sceneCommandRepo.ReadOnlyTable() .Include(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node) .Where(o => o.SceneId == id) .Select(o => o.Command) .ToList(); foreach (var command in commands.OrderBy(o => o.DisplayOrder)) { try { this._hub.ServerToClient(Methods.ExecCommand, command.Id, command.Device.Node.Number, null); Delay(command.Delay); } catch (Exception ex) { this._logger.LogError(ex.ToString()); } } } } return Ok(); } catch (Exception ex) { this._logger.LogError(ex.ToString()); return Problem(ex.Message); } } [HttpPost] public ActionResult ExecCommand([FromBody][Required] Guid id) { try { var command = this._commandRepo.ReadOnlyTable().Include(o => o.Device.Node).FirstOrDefault(o => o.Id == id); this._hub.ServerToClient(Methods.ExecCommand, command.Id, command.Device.Node.Number, null); return Ok(); } catch (Exception ex) { this._logger.LogError(ex.ToString()); return Problem(ex.Message); } } /// /// job server call back /// /// /// [HttpPost] [Route("{id}")] public ActionResult ExecTimer([Required] Guid id) { try { this._logger.LogDebug($"global timer exec:{id}"); var timer = this._sceneTimerRepo.ReadOnlyTable() .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node) .FirstOrDefault(o => o.Id == id); if (timer != null) { foreach (var sceneCommand in timer.Scene.SceneCommands.OrderBy(o => o.Command.DisplayOrder)) { try { this._hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null); Delay(sceneCommand.Command.Delay); } catch (Exception ex) { this._logger.LogError(ex.ToString()); } } } else { this._logger.LogError($"timer {id} does not exist"); } return Ok(); } catch (Exception ex) { this._logger.LogError(ex.ToString()); return Problem(ex.Message); } } private void CallApi(string connectionId, string number, string method, string query) { var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Number == number); if (device != null) { var message = $"{method}?number={number}{(string.IsNullOrEmpty(query) ? "" : "&")}{query}"; var group = device.Node.Number; this._hub.ServerToClient(Methods.ExecApiRequest, message, group, connectionId); } } private void Delay(int commandDelay) { var delay = 0; try { delay = Convert.ToInt32(this._settingService.GetSetting("delay").Value); } catch (Exception ex) { this._logger.LogError(ex.ToString()); } if (commandDelay > 0) { delay += commandDelay; } if (delay > 0) { Thread.Sleep(delay); } } } }