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.

86 lines
2.5 KiB

using System;
using System.Net.Http;
using Application.Domain.Entities;
using Application.Models;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using SPService.Applicaiton.Models;
using Swashbuckle.AspNetCore.Annotations;
namespace SerialPortService.Controllers
{
public class HomeController : Controller
{
private readonly IRepository<Button> _cameraRepo;
private readonly IHttpClientFactory _httpClientFactory;
private readonly SPService _spService;
public HomeController(IHttpClientFactory httpClientFactory, SPService spService, IRepository<Button> cameraRepo)
{
this._cameraRepo = cameraRepo;
this._httpClientFactory = httpClientFactory;
this._spService = spService;
}
public IActionResult Index()
{
this._spService.Notify();
return View(new EditButtonModel { Name = "test" });
}
[HttpPost]
public IActionResult Index(EditButtonModel model)
{
if (ModelState.IsValid)
{
try
{
var button = new Button().From(model);
this._spService.Exec(button);
ViewBag.Result = "success";
}
catch (Exception ex)
{
ex.PrintStack();
ModelState.AddModelError("", ex.Message);
}
}
return View(model);
}
[HttpGet]
[Route("/sp/api")]
[SwaggerOperation("API")]
[ApiExplorerSettings(IgnoreApi = true)]
public string Api()
{
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;
return result;
}
[HttpGet]
[Route("/sp/send")]
[SwaggerOperation("执行")]
public ApiResponse Exec([SwaggerParameter("设备Id")]string id, string code)
{
try
{
this._spService.Exec(code);
return ApiResponse.AsyncSuccess();
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex);
}
}
}
}