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

323 lines
9.9 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 api
[ApiExplorerSettings(IgnoreApi = true)]
[SwaggerOperation("设备开关")]
[HttpGet]
[Route("/api/x82")]
public ApiResponse X82(string sn, string ieee, byte status)
{
try
{
this._deviceService.X82(sn, ieee, status);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[ApiExplorerSettings(IgnoreApi = true)]
[SwaggerOperation("设置亮度")]
[HttpGet]
[Route("/api/x83")]
public ApiResponse X83(string sn, string ieee, [Range(0, 255)]byte brightness)
{
try
{
this._deviceService.X83(sn, ieee, brightness);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[ApiExplorerSettings(IgnoreApi = true)]
[SwaggerOperation("设置颜色")]
[HttpGet]
[Route("/api/x84")]
public ApiResponse X84(string sn, string ieee, [Range(0, 255)]byte hue, [Range(0, 255)]byte saturation)
{
try
{
this._deviceService.X84(sn, ieee, hue, saturation);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion api
#region smart plug
[HttpGet]
[Route("/smartplug/api")]
[SwaggerOperation("API")]
[ApiExplorerSettings(IgnoreApi = true)]
public string Switch2Api()
{
return GetApiJson("/smartplug/");
}
[SwaggerOperation("开")]
[HttpGet]
[Route("/smartplug/21on")]
public ApiResponse SmartPlugOn([SwaggerParameter("设备Id")]string id)
{
try
{
this._deviceService.X82(null, id, 1);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("关")]
[HttpGet]
[Route("/smartplug/22off")]
public ApiResponse SmartPlugOff([SwaggerParameter("设备Id")]string id)
{
try
{
this._deviceService.X82(null, id, 0);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion smart plug
#region colorlight
[HttpGet]
[Route("/colorlight/api")]
[SwaggerOperation("API")]
[ApiExplorerSettings(IgnoreApi = true)]
public string ColorLightApi()
{
return GetApiJson("/colorlight/");
}
[SwaggerOperation("亮度")]
[HttpGet]
[Route("/colorlight/23setbrightness")]
public ApiResponse SetBrightness([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [SwaggerParameter("亮度")][Range(0, 255)]int brightness)
{
try
{
this._deviceService.X83(sn, id, (byte)brightness);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("颜色")]
[HttpGet]
[Route("/colorlight/24setcolor")]
public ApiResponse SetColor([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [SwaggerParameter("色度")][Range(0, 255)]int hue, [SwaggerParameter("饱和度")][Range(0, 255)]int saturation)
{
try
{
this._deviceService.X84(sn, id, (byte)hue, (byte)saturation);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("色温")]
[HttpGet]
[Route("/colorlight/25setcolortemperature")]
public ApiResponse SetColorTemperature([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [SwaggerParameter("色温")][Range(2700, 6500)]int colorTemperature)
{
try
{
this._deviceService.XA8(sn, id, (ushort)colorTemperature);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion colorlight
#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
}
}