2021-12-12 06:55:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
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()
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
AllDrivers = DC.Set<Driver>().GetSelectListItems(Wtm, y => y.FileName);
|
2024-10-10 01:19:51 +00:00
|
|
|
|
AllParents = DC.Set<Device>().Where(x => x.DeviceTypeEnum == DeviceTypeEnum.Group).GetSelectListItems(Wtm, y => y.DeviceName);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DoAdd()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
base.DoAdd();
|
|
|
|
|
//添加结束
|
|
|
|
|
if (this.Entity.DeviceTypeEnum == DeviceTypeEnum.Device)
|
|
|
|
|
{
|
|
|
|
|
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
|
2022-08-10 08:55:44 +00:00
|
|
|
|
deviceService.DrvierManager.AddConfigs(this.Entity.ID, this.Entity.DriverId);
|
2024-10-10 01:19:51 +00:00
|
|
|
|
var device = DC.Set<Device>().Where(x => x.ID == Entity.ID).Include(x => x.Parent).Include(x => x.Driver).Include(x => x.DeviceVariables).SingleOrDefault();
|
2022-08-10 08:55:44 +00:00
|
|
|
|
deviceService.CreateDeviceThread(device);
|
|
|
|
|
|
|
|
|
|
var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient;
|
|
|
|
|
myMqttClient.DeviceAdded(device);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
2022-08-10 16:07:02 +00:00
|
|
|
|
var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient;
|
|
|
|
|
myMqttClient.DeviceDeleted(Entity);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
var ret = DeleteDevices.doDelete(pluginManager, DC, Ids);
|
|
|
|
|
if (!ret.IsSuccess)
|
2022-08-10 08:55:44 +00:00
|
|
|
|
{
|
2021-12-12 06:55:48 +00:00
|
|
|
|
MSD.AddModelError("", ret.Message);
|
2022-08-10 08:55:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
2021-12-24 06:22:45 +00:00
|
|
|
|
public override DuplicatedInfo<Device> SetDuplicatedCheck()
|
|
|
|
|
{
|
|
|
|
|
var rv = CreateFieldsInfo(SimpleField(x => x.DeviceName));
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|