优先从内存查询设备变量,提升查询速度

This commit is contained in:
iioter 2024-06-01 21:24:17 +08:00
parent b88b2a5083
commit e0edf777dc
2 changed files with 27 additions and 25 deletions

View File

@ -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<Device>().Add(newDevice);
devices.Add(newDevice);
@ -67,7 +67,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs
DC.Set<DeviceConfig>().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,

View File

@ -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<DeviceVariable_View> GetSearchQuery()
{
if (Searcher.DeviceId != null)
IoTBackgroundService.VariableSelectDeviceId = Searcher.DeviceId;
var query = DC.Set<DeviceVariable>().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<DeviceVariable>().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;
}