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.
234 lines
8.7 KiB
234 lines
8.7 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Hangfire;
|
|
using Hangfire.Storage;
|
|
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoTCenter.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
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 IConfiguration _cfg;
|
|
private readonly ISettingService _settingService;
|
|
private readonly ILogger<ApiController> _logger;
|
|
private readonly IRepository<Scene> _sceneRepo;
|
|
private readonly IRepository<SceneCommand> _sceneCommandRepo;
|
|
private readonly IRepository<OrganScene> _organSceneRepo;
|
|
private readonly IRepository<OrganSceneCommand> _organSceneCommandRepo;
|
|
private readonly IRepository<OrganSceneTimer> _organSceneTimerRepo;
|
|
private readonly IRepository<Command> _commandRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IHubContext<IoTCenterHub> _hub;
|
|
|
|
public ApiController(IConfiguration cfg,
|
|
ISettingService settingService,
|
|
ILogger<ApiController> logger,
|
|
IRepository<Scene> sceneRepo,
|
|
IRepository<SceneCommand> sceneCommandRepo,
|
|
IRepository<OrganScene> organSceneRepo,
|
|
IRepository<OrganSceneCommand> organSceneCommandRepo,
|
|
IRepository<OrganSceneTimer> sceneTimerRepo,
|
|
IRepository<Command> commandRepo,
|
|
IRepository<Device> deviceRepo,
|
|
IHubContext<IoTCenterHub> hub)
|
|
{
|
|
this._cfg = cfg;
|
|
this._settingService = settingService;
|
|
this._logger = logger;
|
|
this._sceneRepo = sceneRepo;
|
|
this._organSceneRepo = organSceneRepo;
|
|
this._organSceneCommandRepo = organSceneCommandRepo;
|
|
this._organSceneTimerRepo = 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(ErrorMessage = nameof(RequiredAttribute))] 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 ExecOrganScene([FromBody][Required(ErrorMessage = nameof(RequiredAttribute))] Guid id)
|
|
{
|
|
try
|
|
{
|
|
var scene = this._organSceneRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == id);
|
|
if (scene != null)
|
|
{
|
|
var commands = this._organSceneCommandRepo.ReadOnlyTable()
|
|
.Include(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node)
|
|
.Where(o => o.OrganSceneId == 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(ErrorMessage = nameof(RequiredAttribute))] 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);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult UpdateTimer()
|
|
{
|
|
using var conn = JobStorage.Current.GetConnection();
|
|
var jobs = conn.GetRecurringJobs();
|
|
foreach (var job in jobs)
|
|
{
|
|
RecurringJob.RemoveIfExists(job.Id);
|
|
}
|
|
var timers = this._organSceneTimerRepo.ReadOnlyTable().ToList();
|
|
foreach (var timer in timers)
|
|
{
|
|
try
|
|
{
|
|
RecurringJob.AddOrUpdate(timer.Id.ToString(), () => Handle(timer.Id), timer.Cron, TimeZoneInfo.Local);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
this._logger.LogError(ex.ToString());
|
|
}
|
|
}
|
|
return Ok();
|
|
}
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public void Handle(Guid id)
|
|
{
|
|
IoTCenterEventHandler.GlobalTimerExec(_logger, _settingService, _organSceneTimerRepo, _hub, id);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |