iotgateway/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableVM.cs

82 lines
2.7 KiB
C#
Raw Normal View History

2021-12-12 06:55:48 +00:00
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;
using Microsoft.Extensions.Primitives;
namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
{
public partial class DeviceVariableVM : BaseCRUDVM<DeviceVariable>
{
public List<ComboSelectListItem> AllDevices { get; set; }
public List<ComboSelectListItem> AllMethods { get; set; }
public DeviceVariableVM()
{
SetInclude(x => x.Device);
}
protected override void InitVM()
{
AllDevices = DC.Set<Device>().AsNoTracking().Where(x => x.DeviceTypeEnum == DeviceTypeEnum.Device)
.OrderBy(x => x.Parent.Index).ThenBy(x => x.Parent.DeviceName)
.OrderBy(x => x.Index).ThenBy(x => x.DeviceName)
.GetSelectListItems(Wtm, y => y.DeviceName);
2022-05-09 15:57:46 +00:00
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
2021-12-12 06:55:48 +00:00
if (Entity.DeviceId != null)
{
AllMethods = deviceService.GetDriverMethods(Entity.DeviceId);
}
2022-05-09 15:57:46 +00:00
else if (IoTBackgroundService.VariableSelectDeviceId != null)
2022-04-18 08:35:33 +00:00
{
2022-05-09 15:57:46 +00:00
Entity.DeviceId = IoTBackgroundService.VariableSelectDeviceId;
AllMethods = deviceService.GetDriverMethods(Entity.DeviceId);
2022-04-18 08:35:33 +00:00
}
2022-05-09 15:57:46 +00:00
if (AllMethods?.Count() > 0)
2022-05-09 15:57:46 +00:00
AllMethods[0].Selected = true;
2021-12-12 06:55:48 +00:00
}
public override void DoAdd()
2022-05-09 15:57:46 +00:00
{
2021-12-12 06:55:48 +00:00
base.DoAdd();
UpdateVaribale();
}
public override void DoEdit(bool updateAllFields = false)
{
base.DoEdit(updateAllFields);
UpdateVaribale();
}
public override void DoDelete()
{
//先获取id
2022-05-09 15:57:46 +00:00
var id = UpdateDevices.FC2Guids(FC);
var deviceId = DC.Set<DeviceVariable>().Where(x => id.Contains(x.ID)).Select(x => x.DeviceId).FirstOrDefault();
FC["Entity.DeviceId"] = (StringValues)deviceId.ToString();
2021-12-12 06:55:48 +00:00
base.DoDelete();
UpdateVaribale();
}
private void UpdateVaribale()
{
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
UpdateDevices.UpdateVaribale(DC, deviceService, FC);
}
public override DuplicatedInfo<DeviceVariable> SetDuplicatedCheck()
{
var rv = CreateFieldsInfo(SimpleField(x => x.DeviceId), SimpleField(x => x.Name));
return rv;
}
2021-12-12 06:55:48 +00:00
}
}