using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; namespace IoTCenter.Areas.Admin.Controllers { [Authorize] [Area(nameof(Admin))] public class DeviceController : CrudController { private readonly IConfiguration _cfg; private readonly IRepository _productRepo; public DeviceController(IConfiguration cfg, IRepository productRepo, IRepository repo) : base(repo) { this._cfg = cfg; this._productRepo = productRepo; } public override IActionResult Add() { var model = new EditDeviceModel { CategoryNumber = "20", InfoNumber = "serialport", ConnectId = this._cfg["ConnectId"], Number = Guid.NewGuid().ToBase62(), NodeNumber = this._cfg["node.number"] }; return View(model); } public override void ToEntity(EditDeviceModel model, Device entity) { if (!this.repo.ReadOnlyTable().Any(o => o.Id == model.Id)) { entity.ProductId = this._productRepo.ReadOnlyTable().First().Id; entity.AddorUpdateData("SerialPort", "", DeviceDataType.String, "串口", hidden: true); entity.AddorUpdateData("Baud", "9600", DeviceDataType.Int, "波特率", hidden: true); entity.AddorUpdateData("Partity", "0", DeviceDataType.Int, "校验位", hidden: true); entity.AddorUpdateData("DataBits", "8", DeviceDataType.Int, "数据位", hidden: true); entity.AddorUpdateData("StopBits", "1", DeviceDataType.Int, "停止位", hidden: true); entity.AddorUpdateData("Buttons", new List> { new KeyValuePair("测试按键", "11111111") }.ToJson(), DeviceDataType.SelectList, "按键"); } } } }