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.
iot/projects/IoTCenter/Api/ApiController.cs

169 lines
6.1 KiB

using Application.Domain.Entities;
using Application.Models;
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.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace UserCenter.Controllers
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class ApiController : ControllerBase
{
private readonly ILogger<ApiController> _logger;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<SceneCommand> _sceneCommandRepo;
private readonly IRepository<Command> _commandRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<SceneTimer> _sceneTimerRepo;
private readonly IHubContext<IoTCenterHub> _hub;
public ApiController(
ILogger<ApiController> logger,
IRepository<Scene> sceneRepo,
IRepository<SceneCommand> sceneCommandRepo,
IRepository<Command> commandRepo,
IRepository<Device> deviceRepo,
IRepository<SceneTimer> sceneTimerRepo,
IHubContext<IoTCenterHub> hub)
{
this._logger = logger;
this._sceneRepo = sceneRepo;
this._sceneCommandRepo = sceneCommandRepo;
this._commandRepo = commandRepo;
this._deviceRepo = deviceRepo;
this._sceneTimerRepo = sceneTimerRepo;
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)
{
ex.PrintStack();
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)
{
try
{
this._hub.ServerToClient(Methods.ExecCommand, command.Id, command.Device.Node.Number, null);
}
catch (Exception ex)
{
ex.PrintStack();
}
}
}
}
return Ok();
}
catch (Exception ex)
{
ex.PrintStack();
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)
{
ex.PrintStack();
return Problem(ex.Message);
}
}
[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)
{
try
{
this._hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null);
}
catch (Exception ex)
{
ex.PrintStack();
}
}
}
else
{
this._logger.LogError($"timer {id} does not exist");
}
return Ok();
}
catch (Exception ex)
{
ex.PrintStack();
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);
}
}
}
}