|
|
|
@ -0,0 +1,91 @@
|
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
|
using Application.Models;
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
|
using Infrastructure.Jwt;
|
|
|
|
|
using IoTCenter.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
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 : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IHostEnvironment _env;
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
private readonly IJwtHelper _jwtHelper;
|
|
|
|
|
private readonly IRepository<Category> _categoryRepo;
|
|
|
|
|
private readonly IRepository<Product> _productRepo;
|
|
|
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
|
|
|
private readonly IRepository<Scene> _sceneRepo;
|
|
|
|
|
private readonly IRepository<SceneCommand> _sceneCommandRepo;
|
|
|
|
|
private readonly IRepository<Command> _commandRepo;
|
|
|
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
|
private readonly IRepository<LiveRecord> _liveRecordRepo;
|
|
|
|
|
private readonly IHubContext<IoTCenterHub> _hub;
|
|
|
|
|
|
|
|
|
|
public ApiController(
|
|
|
|
|
IHostEnvironment env,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
IJwtHelper jwtHelper,
|
|
|
|
|
IRepository<Category> categoryRepo,
|
|
|
|
|
IRepository<Product> productRepo,
|
|
|
|
|
IRepository<Node> nodeRepo,
|
|
|
|
|
IRepository<Scene> sceneRepo,
|
|
|
|
|
IRepository<SceneCommand> sceneCommandRepo,
|
|
|
|
|
IRepository<Command> commandRepo,
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IRepository<LiveRecord> liveRecordRepo,
|
|
|
|
|
IHubContext<IoTCenterHub> hub)
|
|
|
|
|
{
|
|
|
|
|
this._env = env;
|
|
|
|
|
this._configuration = configuration;
|
|
|
|
|
this._jwtHelper = jwtHelper;
|
|
|
|
|
this._categoryRepo = categoryRepo;
|
|
|
|
|
this._productRepo = productRepo;
|
|
|
|
|
this._nodeRepo = nodeRepo;
|
|
|
|
|
this._sceneRepo = sceneRepo;
|
|
|
|
|
this._sceneCommandRepo = sceneCommandRepo;
|
|
|
|
|
this._commandRepo = commandRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._liveRecordRepo = liveRecordRepo;
|
|
|
|
|
this._hub = hub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public ActionResult Exec([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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|