diff --git a/IoTGateway.ViewModel/BasicData/DeviceVMs/CopyVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVMs/CopyVM.cs index 9ca4ced..03911e0 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVMs/CopyVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVMs/CopyVM.cs @@ -47,7 +47,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs DriverId = device.DriverId, Description = device.Description, DeviceTypeEnum = device.DeviceTypeEnum, - Parent= device.Parent + Parent = device.Parent }; DC.Set().Add(newDevice); devices.Add(newDevice); @@ -67,7 +67,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs DC.Set().Add(newDeviceConfig); } - foreach (var deviceVariable in deviceVariables.OrderBy(x=>x.Index)) + foreach (var deviceVariable in deviceVariables.OrderBy(x => x.Index)) { var newDeviceVariable = new DeviceVariable { @@ -75,6 +75,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs Name = deviceVariable.Name, Description = deviceVariable.Description, DataType = deviceVariable.DataType, + IsTrigger = deviceVariable.IsTrigger, Method = deviceVariable.Method, ProtectType = deviceVariable.ProtectType, Expressions = deviceVariable.Expressions, diff --git a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs index ddf9979..cc3dd3a 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs @@ -10,6 +10,7 @@ using IoTGateway.Model; using PluginInterface; using Plugin; using Newtonsoft.Json; +using System.Collections.Generic; namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs { @@ -104,31 +105,25 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs }; } - public override void AfterDoSearcher() - { - var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; - Parallel.ForEach(EntityList, item => - { - var dapThread = deviceService!.DeviceThreads.FirstOrDefault(x => x.Device.ID == item.DeviceId); - var variable = dapThread?.DeviceValues?.Where(x => x.Key == item.ID).Select(x=>x.Value).FirstOrDefault(); - - if (variable is { Value: not null }) - { - item.Value = variable.Value; - item.CookedValue = variable.CookedValue; - item.StatusType = variable.StatusType; - item.Timestamp = variable.Timestamp; - item.Message = variable.Message; - } - }); - - } public override IOrderedQueryable GetSearchQuery() { if (Searcher.DeviceId != null) IoTBackgroundService.VariableSelectDeviceId = Searcher.DeviceId; - var query = DC.Set().Include(x => x.Device) + + var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; + //设备线程中的所有设备 + var threadDeviceIds = deviceService?.DeviceThreads.Select(x => x.Device.ID).Distinct(x => x); + //设备线程中的变量 + var threadVariables = + deviceService?.DeviceThreads.SelectMany(deviceThread => deviceThread.Device.DeviceVariables); + //查找数据库中额外的变量 + var dcVariables = DC.Set().AsNoTracking().Include(x => x.Device) + .Where(x => !threadDeviceIds.Contains((Guid)x.DeviceId)).AsEnumerable(); + + var variables = dcVariables.Union(threadVariables).AsQueryable(); + + var query = variables .CheckContain(Searcher.Name, x => x.Name) .CheckContain(Searcher.Alias, x => x.Alias) .CheckContain(Searcher.Method, x => x.Method) @@ -148,13 +143,19 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs IsTrigger = x.IsTrigger, EndianType = x.EndianType, Expressions = x.Expressions, - IsUpload=x.IsUpload, + IsUpload = x.IsUpload, ProtectType = x.ProtectType, DeviceName_view = x.Device.DeviceName, Alias = x.Alias, - Device = x.Device + Device = x.Device, + Value = x.Value, + CookedValue = x.CookedValue, + StatusType = x.StatusType, + Timestamp = x.Timestamp, + Message = x.Message, }) - .OrderBy(x => x.Index).ThenBy(x => x.DeviceName_view).ThenBy(x => x.Alias).ThenBy(x => x.Method).ThenBy(x => x.DeviceAddress); + .OrderBy(x => x.Index).ThenBy(x => x.DeviceName_view).ThenBy(x => x.Alias).ThenBy(x => x.Method) + .ThenBy(x => x.DeviceAddress); return query; }