iotgateway/IoTGateway.ViewModel/BasicData/DeviceVMs/DeviceVM.cs

75 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using IoTGateway.Model;
using Plugin;
using Microsoft.EntityFrameworkCore;
namespace IoTGateway.ViewModel.BasicData.DeviceVMs
{
public partial class DeviceVM : BaseCRUDVM<Device>
{
public List<ComboSelectListItem> AllDrivers { get; set; }
public List<ComboSelectListItem> AllParents { get; set; }
public DeviceVM()
{
SetInclude(x => x.Driver);
SetInclude(x => x.Parent);
}
protected override void InitVM()
{
AllDrivers = DC.Set<Driver>().GetSelectListItems(Wtm, y => y.DriverName);
AllParents = DC.Set<Device>().Where(x=>x.DeviceTypeEnum== DeviceTypeEnum.Group).GetSelectListItems(Wtm, y => y.DeviceName);
}
public override void DoAdd()
{
try
{
base.DoAdd();
//添加结束
if (this.Entity.DeviceTypeEnum == DeviceTypeEnum.Device)
{
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
deviceService._DrvierManager.AddConfigs(this.Entity.ID, this.Entity.DriverId);
var dap = DC.Set<Device>().Where(x => x.ID == Entity.ID).Include(x=>x.Parent).Include(x => x.Driver).SingleOrDefault();
deviceService.CreateDeviceThread(dap);
}
}
catch (Exception ex)
{
MSD.AddModelError("", $"添加失败,{ex.Message}");
}
}
public override void DoEdit(bool updateAllFields = false)
{
base.DoEdit(updateAllFields);
//修改结束
var pluginManager = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
UpdateDevices.UpdateDevice(DC, pluginManager, FC);
}
public override void DoDelete()
{
List<Guid> Ids = new List<Guid>() { Guid.Parse(FC["id"].ToString()) };
var pluginManager = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
var ret = DeleteDevices.doDelete(pluginManager, DC, Ids);
if (!ret.IsSuccess)
MSD.AddModelError("", ret.Message);
}
public override DuplicatedInfo<Device> SetDuplicatedCheck()
{
var rv = CreateFieldsInfo(SimpleField(x => x.DeviceName));
return rv;
}
}
}