diff --git a/BPASmartClient.Compiler/BPASmartClient.Compiler.csproj b/BPASmartClient.Compiler/BPASmartClient.Compiler.csproj index 5a3a25e5..efb7ba9f 100644 --- a/BPASmartClient.Compiler/BPASmartClient.Compiler.csproj +++ b/BPASmartClient.Compiler/BPASmartClient.Compiler.csproj @@ -13,6 +13,7 @@ + diff --git a/BPASmartClient.Compiler/CSharpConfig.cs b/BPASmartClient.Compiler/CSharpConfig.cs index 16cca283..faab7eee 100644 --- a/BPASmartClient.Compiler/CSharpConfig.cs +++ b/BPASmartClient.Compiler/CSharpConfig.cs @@ -9,6 +9,10 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Runtime; +using System.Runtime.CompilerServices; namespace BPASmartClient.Compiler { @@ -36,6 +40,8 @@ namespace BPASmartClient.Compiler } + + public object RunCSharp(string code,object[] objValue,string error = "") { object strretu = string.Empty; @@ -43,23 +49,63 @@ namespace BPASmartClient.Compiler { string funName = "main"; StringBuilder builder = new StringBuilder(); + //builder.Append("using System;\n"); + //builder.Append("using System.Linq;\n"); + builder.Append("using Newtonsoft.Json;\n"); + builder.Append("using Newtonsoft.Json.Linq;\n"); + //builder.Append("using System.IO;\n"); + //builder.Append("using System.Runtime;\n"); + //builder.Append("using System.Text;\n"); + builder.Append("using System.Collections.Generic;\n"); builder.Append("namespace YF \n{\n class CSharpConfigRun\n"); builder.Append(" {\n"); builder.Append(code); builder.Append(" }\n}\n"); - - - + //1.添加要引用的程序集 + string output = Path.GetTempFileName(); + string assemblyName = Path.GetFileName(output); + var refPaths = new[] { + Assembly.GetExecutingAssembly().Location, + typeof(object).GetTypeInfo().Assembly.Location, + Path.Combine(Path.GetDirectoryName(typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.Location), "System.dll"), + Path.Combine(Path.GetDirectoryName(typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.Location), "System.Collections.dll"), + Path.Combine(Path.GetDirectoryName(typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.Location), "System.Threading.dll"), + Path.Combine(Path.GetDirectoryName(typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.Location), "System.Runtime.dll"), + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Newtonsoft.Json.dll"), + typeof(DynamicAttribute).GetTypeInfo().Assembly.Location, + }; + MetadataReference[] references = refPaths.Select(r => MetadataReference.CreateFromFile(r)).ToArray(); + + List refs = new List() { + MetadataReference.CreateFromFile (typeof (object).Assembly.Location), + MetadataReference.CreateFromFile (typeof (List).Assembly.Location), + MetadataReference.CreateFromFile (typeof (ASCIIEncoding).Assembly.Location), + MetadataReference.CreateFromFile (typeof (JsonConvert).Assembly.Location), + MetadataReference.CreateFromFile (typeof (JObject).Assembly.Location), + MetadataReference.CreateFromFile (typeof (Object).Assembly.Location), + MetadataReference.CreateFromFile (typeof (JContainer).Assembly.Location), + MetadataReference.CreateFromFile (typeof (JToken).Assembly.Location), + MetadataReference.CreateFromFile (typeof (Enumerable).Assembly.Location), + }; + //2.需要编辑的C#代码 SyntaxTree tree = CSharpSyntaxTree.ParseText(builder.ToString()); - var compilation = - CSharpCompilation.Create("YF") - .AddReferences( - MetadataReference.CreateFromFile( - typeof(object).Assembly.Location) - ) - .AddSyntaxTrees(tree) - .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - + //3.生成C#编译 + var compilation = CSharpCompilation.Create(Guid.NewGuid().ToString() + ".dll") + .WithOptions(new CSharpCompilationOptions( + Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary + , + usings: null, + optimizationLevel: OptimizationLevel.Debug, // TODO + checkOverflow: false, // TODO + allowUnsafe: true, // TODO + platform: Platform.AnyCpu, + warningLevel: 4, + xmlReferenceResolver: null + )) + .AddReferences(references) + .AddSyntaxTrees(tree); + + //var emitResult = compilation.Emit(output); EmitResult emitResult; byte[] dllBytes; using (var stream = new MemoryStream()) @@ -67,15 +113,13 @@ namespace BPASmartClient.Compiler emitResult = compilation.Emit(stream); dllBytes = stream.ToArray(); } + if (emitResult.Success) { - - // Assembly assembly = Assembly.LoadFrom("d:\\test.dll"); Assembly assembly = Assembly.Load(dllBytes); var obj = assembly.CreateInstance("YF.CSharpConfigRun"); var method = obj.GetType().GetMethod("main",new Type[] { typeof(string) }); strretu = method.Invoke(obj,objValue); - } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs index 4a1b290b..487e5127 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs @@ -4,8 +4,10 @@ using BPASmartClient.DATABUS; using BPASmartClient.MessageName.EnumHelp; using BPASmartClient.SCADAControl; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using StackExchange.Redis; using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -94,7 +96,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls DispatcherTimer timer = new DispatcherTimer(); public void Register() { - timer.Interval = TimeSpan.FromSeconds(TimeCount); + timer.Interval = TimeSpan.FromMilliseconds(TimeCount); timer.Tick += Timer_Tick; timer.Start(); @@ -118,7 +120,28 @@ namespace BPASmartClient.SCADAControl.CustomerControls if (!string.IsNullOrEmpty(DataSouceInformation)) { Direction = 1; - FDataSouce = HttpRequestHelper.HttpGetRequest(DataSouceInformation); + string returndata=string.Empty; + string obj_data= HttpRequestHelper.HttpGetRequest(DataSouceInformation); + Dictionary obj = JsonConvert.DeserializeObject>(obj_data); + if (obj.ContainsKey("data")) + { + returndata = obj_data.ToString(); + } else + { + foreach (var item in obj) + { + if(item.Value is JObject) + { + Dictionary itemkey = JsonConvert.DeserializeObject>(item.Value.ToString()); + if (itemkey.ContainsKey("data")) + { + returndata = item.Value.ToString(); + } + } + } + } + //List> obj2 = JsonConvert.DeserializeObject>>(obj1["data"].ToString()); + FDataSouce = returndata; } break; case InterfaceModeEnum.PUT: @@ -206,7 +229,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls set { SetValue(TimeCountProperty,value); } } public static readonly DependencyProperty TimeCountProperty = - DependencyProperty.Register("TimeCount",typeof(int),typeof(TheAPI),new PropertyMetadata(5)); + DependencyProperty.Register("TimeCount",typeof(int),typeof(TheAPI),new PropertyMetadata(2000)); [Category("数据绑定-数据来源")] public InterfaceModeEnum InterfaceMode { @@ -231,15 +254,15 @@ namespace BPASmartClient.SCADAControl.CustomerControls set { SetValue(DataSouceInformationProperty,value); } } public static readonly DependencyProperty DataSouceInformationProperty = - DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheAPI),new PropertyMetadata("http://localhost:9092/api/User/UsersTestSwagger")); - [Category("数据绑定-数据来源")] - public string DeviceName - { - get { return (string)GetValue(DeviceNameProperty); } - set { SetValue(DeviceNameProperty,value); } - } - public static readonly DependencyProperty DeviceNameProperty = - DependencyProperty.Register("DeviceName",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty)); + DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheAPI),new PropertyMetadata("http://datav.dev1.com/api/Alarm/Query")); + //[Category("数据绑定-数据来源")] + //public string DeviceName + //{ + // get { return (string)GetValue(DeviceNameProperty); } + // set { SetValue(DeviceNameProperty,value); } + //} + //public static readonly DependencyProperty DeviceNameProperty = + // DependencyProperty.Register("DeviceName",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty)); [Category("数据绑定")] public string FDataSouce { @@ -289,5 +312,8 @@ namespace BPASmartClient.SCADAControl.CustomerControls + } } + + diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs index c37d9618..6e90ae52 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; +using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -35,8 +36,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); - MinWidth = 100; - MinHeight = 100; + //MinWidth = 100; + //MinHeight = 100; + //Width = 100; + //Height = 100; } public DataSouceModel ItemsString @@ -70,11 +73,13 @@ namespace BPASmartClient.SCADAControl.CustomerControls /// public void Register() { - - timer.Interval = TimeSpan.FromMilliseconds(TimeCount); - timer.Tick += Timer_Tick; ; - timer.Start(); + if (DataSouceType == DataTypeEnum.API接口) + { + timer.Interval = TimeSpan.FromMilliseconds(TimeCount); + timer.Tick += Timer_Tick; ; + timer.Start(); + } } private void Timer_Tick(object? sender,EventArgs e) @@ -168,6 +173,8 @@ namespace BPASmartClient.SCADAControl.CustomerControls { try { + //Dictionary keys= JsonConvert.DeserializeObject>(GenerateData); + //List> obj2 = JsonConvert.DeserializeObject>>(keys["data"].ToString()); ItemsString = JsonConvert.DeserializeObject(GenerateData); // 运行时进行项目绑定 Binding binding = new Binding(); @@ -182,6 +189,47 @@ namespace BPASmartClient.SCADAControl.CustomerControls } } + + public DataTable ListToDataTable(List> arrayList) + { + DataTable dataTable = new DataTable(); //实例化 + DataTable result; + try + { + if (arrayList.Count > 0) + { + foreach (Dictionary dictionary in arrayList) + { + if (dictionary.Keys.Count() == 0) + { + result = dataTable; + return result; + } + //在第一次循环时确定datatable的列名,后续循环无需再更改 + if (dataTable.Columns.Count == 0) + { + //此处仅考虑一层json字符串的形式,多层结构需要深入得到叶子节点的key + foreach (string current in dictionary.Keys) + { + dataTable.Columns.Add(current);//, dictionary[current].GetType()); + } + } + //每次循环增加datatable的Rows + DataRow dataRow = dataTable.NewRow(); + foreach (string current in dictionary.Keys) + { + dataRow[current] = dictionary[current]; + } + dataTable.Rows.Add(dataRow); //循环添加行到DataTable中 + } + } + } + catch + { + } + result = dataTable; + return result; + } #endregion diff --git a/BPASmartClient.SCADAControl/Themes/Generic.xaml b/BPASmartClient.SCADAControl/Themes/Generic.xaml index 49bec208..f0ebfe2c 100644 --- a/BPASmartClient.SCADAControl/Themes/Generic.xaml +++ b/BPASmartClient.SCADAControl/Themes/Generic.xaml @@ -295,6 +295,9 @@ + + + @@ -305,7 +308,7 @@ - + - + + --> - + --> @@ -351,12 +356,20 @@ + + + + + + + + - + @@ -399,7 +414,7 @@