diff --git a/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 b/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 index e4617a7..0c0f44d 100644 Binary files a/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 and b/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/IoTGateway/v16/.suo b/.vs/IoTGateway/v16/.suo index e879c89..de2fcc3 100644 Binary files a/.vs/IoTGateway/v16/.suo and b/.vs/IoTGateway/v16/.suo differ diff --git a/IoTGateway.ViewModel/BasicData/DeviceConfigVMs/DeviceConfigListVM.cs b/IoTGateway.ViewModel/BasicData/DeviceConfigVMs/DeviceConfigListVM.cs index 7d8e32a..e8bbee6 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceConfigVMs/DeviceConfigListVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceConfigVMs/DeviceConfigListVM.cs @@ -50,11 +50,11 @@ namespace IoTGateway.ViewModel.BasicData.DeviceConfigVMs protected override IEnumerable> InitGridHeader() { return new List>{ - this.MakeGridHeader(x => x.DeviceConfigName), - this.MakeGridHeader(x => x.Description), - this.MakeGridHeader(x => x.Value), + this.MakeGridHeader(x => x.DeviceConfigName).SetWidth(100), + this.MakeGridHeader(x => x.Description).SetWidth(100), + this.MakeGridHeader(x => x.Value).SetWidth(100), + this.MakeGridHeader(x => x.DeviceName_view).SetWidth(100), this.MakeGridHeader(x => x.EnumInfo), - this.MakeGridHeader(x => x.DeviceName_view), this.MakeGridHeaderAction(width: 200) }; } @@ -74,7 +74,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceConfigVMs EnumInfo = x.EnumInfo, DeviceName_view = x.Device.DeviceName, }) - .OrderBy(x => x.ID); + .OrderBy(x => x.DeviceName_view).ThenBy(x=>x.DeviceConfigName); return query; } diff --git a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs index 01f543b..0e5b663 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs @@ -58,7 +58,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs this.MakeGridHeader(x => x.Value).SetWidth(80), this.MakeGridHeader(x => x.CookedValue).SetWidth(80), this.MakeGridHeader(x => x.State).SetWidth(80), - this.MakeGridHeader(x => x.Expressions).SetSort(true).SetWidth(80), + this.MakeGridHeader(x => x.Expressions).SetWidth(150), //this.MakeGridHeader(x => x.ProtectType).SetSort(true), this.MakeGridHeader(x => x.DeviceName_view).SetSort(true).SetWidth(90), this.MakeGridHeader(x=> "detail").SetHide().SetFormat((a,b)=>{ @@ -104,7 +104,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs ProtectType = x.ProtectType, DeviceName_view = x.Device.DeviceName, }) - .OrderBy(x => x.ID); + .OrderBy(x => x.DeviceName_view).ThenBy(x => x.DeviceAddress); return query; } diff --git a/IoTGateway.ViewModel/BasicData/UpdateDevices.cs b/IoTGateway.ViewModel/BasicData/UpdateDevices.cs index 78dcc03..b971a73 100644 --- a/IoTGateway.ViewModel/BasicData/UpdateDevices.cs +++ b/IoTGateway.ViewModel/BasicData/UpdateDevices.cs @@ -46,7 +46,7 @@ namespace IoTGateway.ViewModel.BasicData if (FC.ContainsKey("Entity.DeviceId")) { StringValues id = (StringValues)FC["Entity.DeviceId"]; - var device = DC.Set().Where(x => x.ID == Guid.Parse(id)).Include(x => x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); + var device = DC.Set().AsNoTracking().Where(x => x.ID == Guid.Parse(id)).Include(x => x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); if (!devices.Where(x => x.ID == device.ID).Any()) devices.Add(device); } @@ -58,7 +58,7 @@ namespace IoTGateway.ViewModel.BasicData var deviceVariable = DC.Set().Where(x => x.ID == varId).SingleOrDefault(); if (deviceVariable != null) { - var device = DC.Set().Where(x => x.ID == deviceVariable.DeviceId).Include(x=>x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); + var device = DC.Set().AsNoTracking().Where(x => x.ID == deviceVariable.DeviceId).Include(x=>x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); if (!devices.Where(x => x.ID == device.ID).Any()) devices.Add(device); } @@ -66,7 +66,7 @@ namespace IoTGateway.ViewModel.BasicData case FromVM.Config: foreach (var deviceConfigId in Ids) { - var deviceConfig = DC.Set().Where(x => x.ID == deviceConfigId).SingleOrDefault(); + var deviceConfig = DC.Set().AsNoTracking().Where(x => x.ID == deviceConfigId).SingleOrDefault(); if (deviceConfig != null) { var device = DC.Set().Where(x => x.ID == deviceConfig.DeviceId).Include(x => x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); @@ -78,7 +78,7 @@ namespace IoTGateway.ViewModel.BasicData case FromVM.Device: foreach (var deviceId in Ids) { - var device = DC.Set().Where(x => x.ID == deviceId).Include(x => x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); + var device = DC.Set().AsNoTracking().Where(x => x.ID == deviceId).Include(x => x.DeviceVariables).Include(x => x.Driver).SingleOrDefault(); if (!devices.Where(x => x.ID == device.ID).Any()) devices.Add(device); } diff --git a/IoTGateway/Areas/BasicData/Views/DeviceConfig/Edit.cshtml b/IoTGateway/Areas/BasicData/Views/DeviceConfig/Edit.cshtml index 02ff285..831f956 100644 --- a/IoTGateway/Areas/BasicData/Views/DeviceConfig/Edit.cshtml +++ b/IoTGateway/Areas/BasicData/Views/DeviceConfig/Edit.cshtml @@ -1,7 +1,7 @@ @model IoTGateway.ViewModel.BasicData.DeviceConfigVMs.DeviceConfigVM @inject IStringLocalizer Localizer; - + @**@ @{ diff --git a/IoTGateway/Areas/BasicData/Views/DeviceConfig/Index.cshtml b/IoTGateway/Areas/BasicData/Views/DeviceConfig/Index.cshtml index 5e5a709..55de42f 100644 --- a/IoTGateway/Areas/BasicData/Views/DeviceConfig/Index.cshtml +++ b/IoTGateway/Areas/BasicData/Views/DeviceConfig/Index.cshtml @@ -4,10 +4,10 @@ + - - + diff --git a/IoTGateway/IoTGateway.csproj.user b/IoTGateway/IoTGateway.csproj.user index 81b1159..7a791b3 100644 --- a/IoTGateway/IoTGateway.csproj.user +++ b/IoTGateway/IoTGateway.csproj.user @@ -2,7 +2,7 @@ IoTGateway - D:\Repos\iotgateway\IoTGateway\Properties\PublishProfiles\FolderProfile.pubxml + FolderProfile ProjectDebugger diff --git a/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user b/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user index 9a4d120..c919999 100644 --- a/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - <_PublishTargetUrl>D:\Repos\iotgateway\IoTGateway\bin\Release\net5.0\publish\ - True|2021-12-12T06:11:08.8380502Z; + <_PublishTargetUrl>E:\workbench\iotgateway\IoTGateway\bin\Release\net5.0\publish\ + True|2021-12-17T11:11:07.1655146Z;True|2021-12-12T14:11:08.8380502+08:00; \ No newline at end of file diff --git a/IoTGateway/iotgateway.db b/IoTGateway/iotgateway.db index 3226771..629522e 100644 Binary files a/IoTGateway/iotgateway.db and b/IoTGateway/iotgateway.db differ diff --git a/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj b/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj index 8891b66..8579554 100644 --- a/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj +++ b/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj @@ -2,11 +2,8 @@ net5.0 - - - false - ../../../IoTGateway/bin/Debug/net5.0/drivers + ../../../IoTGateway/bin/Debug/net5.0/drivers diff --git a/Plugins/Plugin/DeviceService.cs b/Plugins/Plugin/DeviceService.cs index 0cf1593..7544195 100644 --- a/Plugins/Plugin/DeviceService.cs +++ b/Plugins/Plugin/DeviceService.cs @@ -14,6 +14,7 @@ using System.Threading.Tasks; using WalkingTec.Mvvm.Core; using IoTGateway.DataAccess; using IoTGateway.Model; +using DynamicExpresso; namespace Plugin { @@ -25,6 +26,7 @@ namespace Plugin public MyMqttClient _MyMqttClient; private string connnectSetting = IoTBackgroundService.connnectSetting; private DBTypeEnum DBType = IoTBackgroundService.DBType; + private Interpreter interpreter = new(); public DeviceService(IConfiguration ConfigRoot, DrvierService drvierManager, MyMqttClient myMqttClient) { @@ -133,7 +135,7 @@ namespace Plugin p.SetValue(DeviceObj, value); } - var deviceThread = new DeviceThread(Device, DeviceObj, systemManage.GatewayName, _MyMqttClient); + var deviceThread = new DeviceThread(Device, DeviceObj, systemManage.GatewayName, _MyMqttClient, interpreter); DeviceThreads.Add(deviceThread); } @@ -149,12 +151,15 @@ namespace Plugin public void RemoveDeviceThread(Device Device) { - var DeviceThread = DeviceThreads.Where(x => x.Device.ID == Device.ID).FirstOrDefault(); - if (DeviceThread != null) + if (Device != null) { - DeviceThread.StopThread(); - DeviceThread.Dispose(); - DeviceThreads.Remove(DeviceThread); + var DeviceThread = DeviceThreads.Where(x => x.Device.ID == Device.ID).FirstOrDefault(); + if (DeviceThread != null) + { + DeviceThread.StopThread(); + DeviceThread.Dispose(); + DeviceThreads.Remove(DeviceThread); + } } } diff --git a/Plugins/Plugin/DeviceThread.cs b/Plugins/Plugin/DeviceThread.cs index 6860169..d3d7036 100644 --- a/Plugins/Plugin/DeviceThread.cs +++ b/Plugins/Plugin/DeviceThread.cs @@ -22,12 +22,13 @@ namespace Plugin private Task task { get; set; } = null; private DateTime TsStartDt = new DateTime(1970, 1, 1); private CancellationTokenSource tokenSource = new CancellationTokenSource(); - private Interpreter interpreter = new Interpreter(); + private Interpreter Interpreter = null; - public DeviceThread(Device device, IDriver driver, string ProjectId, MyMqttClient myMqttClient) + public DeviceThread(Device device, IDriver driver, string ProjectId, MyMqttClient myMqttClient, Interpreter interpreter) { Device = device; Driver = driver; + Interpreter = interpreter; Methods = Driver.GetType().GetMethods().Where(x => x.GetCustomAttribute(typeof(MethodAttribute)) != null).ToList(); if (Device.AutoStart) { @@ -83,7 +84,16 @@ namespace Plugin DeviceValues[item.ID] = ret; if (ret.StatusType == VaribaleStatusTypeEnum.Good && !string.IsNullOrWhiteSpace(item.Expressions?.Trim())) - ret.CookedValue = interpreter.Eval(item.Expressions.Replace("raw", ret.Value.ToString())); + { + try + { + ret.CookedValue = interpreter.Eval(DealMysqlStr(item.Expressions).Replace("raw", ret.Value.ToString())); + } + catch (Exception) + { + ret.StatusType = VaribaleStatusTypeEnum.ExpressionError; + } + } else ret.CookedValue = ret.Value; @@ -139,6 +149,12 @@ namespace Plugin Driver.Dispose(); Console.WriteLine($"{Device.DeviceName},释放"); } + + //mysql会把一些符号转义,没找到原因,先临时处理下 + private string DealMysqlStr(string Expression) + { + return Expression.Replace("<", ">").Replace(">", "<").Replace("&", "&"); + } } } diff --git a/Plugins/PluginInterface/VaribaleStatusTypeEnum.cs b/Plugins/PluginInterface/VaribaleStatusTypeEnum.cs index 62789be..a0116c7 100644 --- a/Plugins/PluginInterface/VaribaleStatusTypeEnum.cs +++ b/Plugins/PluginInterface/VaribaleStatusTypeEnum.cs @@ -11,6 +11,7 @@ namespace PluginInterface Good=0, AddressError, MethodError, + ExpressionError, Bad, UnKnow, Custome1,