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/IoT/IoTServices/FBeeService/Controllers/HomeController.cs

154 lines
4.8 KiB

using Application.Domain.Entities;
using Application.Models;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Http;
namespace FBeeService.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : Controller
{
private readonly IRepository<Device> _deviceRepo;
private readonly IHttpClientFactory _httpClientFactory;
private readonly DeviceService _deviceService;
public HomeController(IHttpClientFactory httpClientFactory,
DeviceService deviceService,
IRepository<Device> deviceRepo)
{
this._deviceRepo = deviceRepo;
this._httpClientFactory = httpClientFactory;
this._deviceService = deviceService;
}
#region ui
public IActionResult Index()
{
var list = this._deviceRepo.ReadOnlyTable()
.Include(o => o.Data)
.Where(o => o.Info.DeviceType == DeviceType.Gateway)
.OrderBy(o => o.Number)
.ToList();
return View(list);
}
public IActionResult Gateway(string sn)
{
var list = this._deviceRepo.ReadOnlyTable()
.Include(o => o.Data)
.Where(o => o.GatewayNumber == sn)
.OrderBy(o => o.CategoryNumber)
.ToList();
return View(list);
}
#endregion ui
#region IR
[SwaggerOperation("版本")]
[HttpGet]
[Route("/ir/version")]
public ApiResponse Version([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id)
{
try
{
this._deviceService.XA70080(sn, id);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[Route("/ir/test")]
public ApiResponse IRControl([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, byte type, ushort code)
{
try
{
this._deviceService.XA70082(sn, id, type, code);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[Route("/ir/study")]
public ApiResponse IRStudy([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, byte type, ushort code)
{
try
{
this._deviceService.XA70083(sn, id, type, code);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("版本")]
[HttpGet]
[Route("/ir/control")]
public ApiResponse IRControl([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, int power, int pattern, int temperature, int direction, int wind)
{
try
{
var keyCode = power == 1 ? 1 : power + pattern + temperature + direction + wind;
this._deviceService.XA70082(sn, id, 0x01, (ushort)keyCode);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion IR
#region tools
private string GetApiJson(string prefix)
{
var url = new UriBuilder
{
Host = Request.Host.Host,
Port = Request.Host.Port ?? 80,
Path = "/swagger/v1/swagger.json"
}.ToString();
var hc = this._httpClientFactory.CreateClient();
var result = hc.GetStringAsync(url).Result;
var json = JsonConvert.DeserializeObject(result) as JObject;
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
var names = paths.Properties().Select(o => o.Name).ToList();
foreach (var item in names)
{
if (!item.StartsWith(prefix))
{
paths.Remove(item);
}
}
var realResult = JsonConvert.SerializeObject(json);
return realResult;
}
#endregion tools
}
}