diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 124607a..dc8aef0 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -24,7 +24,7 @@ namespace SunlightAggregationManager } private void Window_Loaded(object sender, RoutedEventArgs e) { - GlobalLogManager.RegisterTarget(LOG); + GlobalLogManager.RegisterTarget(LOG,LogGing.LogGingDATA); DataContext = new MainWindowViewModel(); } @@ -86,6 +86,7 @@ namespace SunlightAggregationManager { state.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Visible; + Picture.Content = new View.ExtendPage(); } private void ListViewItem_MouseLeftButtonUp_user(object sender, MouseButtonEventArgs e) { diff --git a/UserClass/AsyncHttpServer.cs b/UserClass/AsyncHttpServer.cs index b335c8b..cb80ea7 100644 --- a/UserClass/AsyncHttpServer.cs +++ b/UserClass/AsyncHttpServer.cs @@ -101,6 +101,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) {//错误 + LogGing.ERRDATA(ex); Console.WriteLine("Http:" + ex.ToString()); //client.SendAsync("Target instruction parsing error manager will close connection"); } diff --git a/UserClass/AsyncTcpClient.cs b/UserClass/AsyncTcpClient.cs index 7f08a64..5652b5b 100644 --- a/UserClass/AsyncTcpClient.cs +++ b/UserClass/AsyncTcpClient.cs @@ -38,7 +38,7 @@ namespace SunlightAggregationManager.UserClass tcpClient.Connected = (client, e) => { //设定显示状态 - System.Data.DataRow targetRow =ViewModel.MainWindowViewModel._service.Select("ID = 1").FirstOrDefault()!; + System.Data.DataRow targetRow =ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!; if (targetRow != null) { targetRow.BeginEdit(); @@ -60,7 +60,7 @@ namespace SunlightAggregationManager.UserClass };//成功连接到服务器 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closed = (client, e) => { - System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("ID = 1").FirstOrDefault()!; + System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!; if (targetRow != null) { targetRow.BeginEdit(); @@ -138,6 +138,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("Tcp:" + ex.ToString()); } } diff --git a/UserClass/AsyncTcpServer.cs b/UserClass/AsyncTcpServer.cs index 356e6dc..d244548 100644 --- a/UserClass/AsyncTcpServer.cs +++ b/UserClass/AsyncTcpServer.cs @@ -139,6 +139,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) {//错误 + LogGing.ERRDATA(ex); Console.WriteLine("Tcp:" + ex.ToString()); client.SendAsync("Target instruction parsing error manager will close connection"); foreach (var item in AsyncTcpServer.service.Clients) diff --git a/UserClass/DataBase.cs b/UserClass/DataBase.cs index e7feacd..2b20f6e 100644 --- a/UserClass/DataBase.cs +++ b/UserClass/DataBase.cs @@ -18,7 +18,7 @@ namespace SunlightAggregationManager.UserClass private string DB_IP = null!; private string DB_PASSWORD = null!; - public void Config(string ip, string name, string type, string id, string password) + public async void Config(string ip, string name, string type, string id, string password) { DB_IP = ip; DB_ID = id; @@ -26,7 +26,7 @@ namespace SunlightAggregationManager.UserClass DB_TYPE = type; DB_PASSWORD = password; - _= ConfigAsync(); + await ConfigAsync(); } public async Task ConfigAsync() @@ -63,6 +63,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("DataBase Link timeout\n"+ex.ToString()); // 失败处理 return; // 直接返回,不执行后续代码 @@ -76,7 +77,7 @@ namespace SunlightAggregationManager.UserClass // 创建一个空的 DataTable 用于存放查询结果 DataTable dataTable = new DataTable(); - // 沿用你原有的数据库连接字符串构建逻辑 + // 数据库连接字符串构建逻辑 var builder = new SqlConnectionStringBuilder { DataSource = DB_IP, @@ -109,8 +110,9 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("DataBase Query failed\n" + ex.ToString()); - // 这里可以选择将异常抛出给调用方,或者返回一个空的 DataTable + // 将异常抛出给调用方,或者返回一个空的 DataTable throw; } diff --git a/UserClass/LogGing.cs b/UserClass/LogGing.cs new file mode 100644 index 0000000..46369fd --- /dev/null +++ b/UserClass/LogGing.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; + +namespace SunlightAggregationManager.UserClass +{ + public class LogGing + { + public static void LogGingDATA(string dat) + { + string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录 + string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件 + string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:"; + + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(logPath))//检查日志文件并写入启动日志 + { + FileStream fs = new FileStream(logPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine(Log_time + dat); + wr.Close(); + } + else + { + try + { + FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write); + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine(Log_time + dat); + wr.Close(); + } + catch { } + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create();//创建日志路径 + } + } + public static void ERRDATA(System.Exception dat) + { + string Log_time = DateTime.Now.ToString("yyyy-MM-dd"); + string logpath = System.Environment.CurrentDirectory + "\\ERR";//日志文件目录 + // string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件 + // System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录 + string log_path = logpath + "\\ERR" + Log_time + ".txt"; + string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(log_path))//检查文件并写入 + { + // FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件 + // StreamWriter wr = new StreamWriter(fs);//创建文件 + // wr.Close(); + FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wfil = new StreamWriter(fil);//创建文件 + wfil.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.Source.ToString() + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.StackTrace.ToString()); + wfil.Close(); + } + else + { + FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.ToString() + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.ToString()); + wr.Close(); + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create(); + } + } + public static void ExchangeDATA(string dat) + { + string Log_time = DateTime.Now.ToString("yyyy-MM-dd"); + string logpath = System.Environment.CurrentDirectory + "\\Exchange";//日志文件目录 + // string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件 + // System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录 + string log_path = logpath + "\\Exchange" + Log_time + ".txt"; + string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(log_path))//检查文件并写入 + { + // FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件 + // StreamWriter wr = new StreamWriter(fs);//创建文件 + // wr.Close(); + FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wfil = new StreamWriter(fil);//创建文件 + wfil.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat); + wfil.Close(); + } + else + { + FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat); + wr.Close(); + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create(); + } + } + } +} diff --git a/UserClass/NetFwManger.cs b/UserClass/NetFwManger.cs index 344ced9..0fed532 100644 --- a/UserClass/NetFwManger.cs +++ b/UserClass/NetFwManger.cs @@ -46,6 +46,7 @@ namespace SunlightAggregationManager.UserClass //将规则添加到防火墙策略中 policy?.Rules.Add(rule); + Console.WriteLine("Add firewall port:"+port); } public static void DelPort(int port, string protocol) @@ -65,7 +66,7 @@ namespace SunlightAggregationManager.UserClass if (rule.Name == GetName(port, protocol)) { rules.Remove(rule.Name); - Console.WriteLine(@"Firewall rule deleted successfully."); + //Console.WriteLine(@"Firewall rule deleted successfully."); break; } } diff --git a/UserClass/TextWriter.cs b/UserClass/TextWriter.cs index 13c8148..a9eed1a 100644 --- a/UserClass/TextWriter.cs +++ b/UserClass/TextWriter.cs @@ -10,25 +10,32 @@ namespace SunlightAggregationManager.UserClass public class ControlWriter : TextWriter { private readonly TextBox _textbox; + private readonly Action _logGingDataAction; - public ControlWriter(TextBox textbox) + public ControlWriter(TextBox textbox,Action logGingDataAction) { _textbox = textbox; + _logGingDataAction = logGingDataAction; } - public override void Write(char value) => AppendText(DateTime.Now + ": " + value.ToString()); - public override void Write(string? value) => AppendText(DateTime.Now + ": " + value); - public override void WriteLine(string? value) => AppendText(DateTime.Now + ": "+value + Environment.NewLine); + public override void Write(char value) => AppendText(DateTime.Now + ": " + value.ToString(), true); + public override void Write(string? value) => AppendText(DateTime.Now + ": " + value, true); + public override void WriteLine(string? value) => AppendText(DateTime.Now + ": "+value + Environment.NewLine, true); - private void AppendText(string text) + private void AppendText(string text, bool invokeExternalLog) { if (!_textbox.Dispatcher.CheckAccess()) { - _textbox.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => AppendText(text))); + _textbox.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => AppendText(text, true))); return; } _textbox.AppendText(text); _textbox.ScrollToEnd(); + + if (invokeExternalLog) + { + _logGingDataAction?.Invoke(text); + } } public override Encoding Encoding => Encoding.UTF8; @@ -41,10 +48,10 @@ namespace SunlightAggregationManager.UserClass // 【核心功能】注册目标 TextBox // 子页面加载时调用这个方法 - public static void RegisterTarget(TextBox logBox) + public static void RegisterTarget(TextBox logBox ,Action logGingDataAction) { // 创建新的 Writer - _currentWriter = new ControlWriter(logBox); + _currentWriter = new ControlWriter(logBox, logGingDataAction); // 【关键】将 Console 的输出重定向到这个 Writer // 这样 Console.WriteLine("...") 就会自动写入到 logBox 中 diff --git a/UserWindow/User.xaml b/UserWindow/User.xaml new file mode 100644 index 0000000..f9a0dd3 --- /dev/null +++ b/UserWindow/User.xaml @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/UserWindow/User.xaml.cs b/UserWindow/User.xaml.cs new file mode 100644 index 0000000..64d948f --- /dev/null +++ b/UserWindow/User.xaml.cs @@ -0,0 +1,152 @@ +using SunlightAggregationManager.View; +using SunlightAggregationManager.ViewModel; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Text.RegularExpressions; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Xml.Linq; + +namespace SunlightAggregationManager.UserWindow +{ + /// + /// Machine.xaml 的交互逻辑 + /// + public partial class User : Window + { + CheckBox[] checkBoxes = new CheckBox[99]; + string[] strings = new string[8] {"修改系统设置", "修改用户设置", + "设备监控","设备控制","远程设置","远程查询","输送信息","历史查询"}; + public User() + { + WindowStartupLocation = WindowStartupLocation.CenterScreen; + InitializeComponent(); + } + + private void user_Loaded(object sender, RoutedEventArgs e)//打开页面执行 + { + for (int i = 0; i < strings.Length; i++) + { + checkBoxes[i] = new CheckBox(); + checkBoxes[i].Content = strings[i].ToString(); + checkBoxes[i].FontSize = 20; + checkBoxes[i].Width = 200; + checkBoxes[i].Height = 50; + Capacity.Children.Add(checkBoxes[i]); + } + DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; + } + + private void Tb_KeyFloating(object sender, TextCompositionEventArgs e)//输入事件 + { + //Regex re = new Regex("[^0-9.-]+"); + Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数 + e.Handled = !re.IsMatch(e.Text); + } + + private void DataGridMac_MouseDoubleClick(object sender, MouseButtonEventArgs e)//数据表双击事件 + { + string Capacity_; + int rownum = DataGriduser.SelectedIndex;//获取鼠标选中行并定义变量 + if (rownum != -1)//判断鼠标定位是否有效 + { + /*定位选中行及指定列单元格文本信息*/ + _Name.Text = (DataGriduser.Columns[1].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim();//定位第列 + _User.Text = (DataGriduser.Columns[0].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim();//定位第列 + GROUP.Text = (DataGriduser.Columns[4].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); + Capacity_ = (DataGriduser.Columns[5].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); + _Department.Text = (DataGriduser.Columns[3].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); + Pasword.Text = (DataGriduser.Columns[2].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); + + if (strings.Length > Capacity_.Length) + { + for (int i = 0; i < Capacity_.Length; i++) + { + if (Capacity_.Substring(i, 1) == "1") + { + checkBoxes[i].IsChecked = true; + } + else + { + checkBoxes[i].IsChecked = false; + } + } + } + else + { + for (int i = 0; i < strings.Length; i++) + { + if (Capacity_.Substring(i, 1) == "1") + { + checkBoxes[i].IsChecked = true; + } + else + { + checkBoxes[i].IsChecked = false; + } + } + } + } + } + + private void Button_Preservation(object sender, RoutedEventArgs e)//保存按钮事件 + { + Regex re_number = new Regex(@"^[0-9]+(.[0-9]{1,2})?$");//校验用正则表达式有1~2位小数的正实数 + Regex re_char = new Regex(@"^[A-Za-z0-9\s@()()/+!!_-]+$");//校验用正则表达式由数字,26个英文字母,空白字符和@()()/+!!_-组成的字符串 + + string user = _User.Text; + string name = _Name.Text; + string password = Pasword.Text; + string Group = GROUP.Text; + string Cap = ""; + string department = _Department.Text; + for (int i = 0; i < strings.Length; i++) + { + if ((bool)checkBoxes[i].IsChecked!) + { + Cap = Cap + "1"; + } + else { Cap = Cap + "0"; } + } + Dictionary USER_new = new Dictionary();//缓存函数 + USER_new.Add("User", user); + USER_new.Add("Name", name); + USER_new.Add("Password", password); + USER_new.Add("Groups", Group); + USER_new.Add("Capacity", Cap); + USER_new.Add("Department", department); + USER_new.Add("State", 0); + USER_new.Add("Enterprise", MainWindowViewModel.Enterprise); + + var rowdata = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "UserID", "User='" + user + "'"); + if (rowdata != null) + { + MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + user + "'", USER_new); + } + else + { + MainWindowViewModel.Insert_Memory(MainWindowViewModel._userData, USER_new);// 执行插入 + } + DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; + _Name.Text = null; + + } + + private void Button_Delete(object sender, RoutedEventArgs e)//删除按钮事件 + { + try + { + MainWindowViewModel.Delete_Memory(MainWindowViewModel._userData, "User='" + _User.Text + "'"); + } + catch (Exception) { } + finally + { + DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; + _Name.Text = null; + } + } + } +} diff --git a/UserWindow/UserSet.xaml.cs b/UserWindow/UserSet.xaml.cs index 34cf729..dde66de 100644 --- a/UserWindow/UserSet.xaml.cs +++ b/UserWindow/UserSet.xaml.cs @@ -17,7 +17,7 @@ namespace SunlightAggregationManager.UserWindow /// public partial class UserSet : Window { - public UserSet() + public UserSet(string id) { InitializeComponent(); } diff --git a/View/ExtendPage.xaml b/View/ExtendPage.xaml new file mode 100644 index 0000000..c7431e1 --- /dev/null +++ b/View/ExtendPage.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/View/ExtendPage.xaml.cs b/View/ExtendPage.xaml.cs new file mode 100644 index 0000000..279d4eb --- /dev/null +++ b/View/ExtendPage.xaml.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace SunlightAggregationManager.View +{ + /// + /// ExtendPage.xaml 的交互逻辑 + /// + public partial class ExtendPage : UserControl + { + public ExtendPage() + { + InitializeComponent(); + } + } +} diff --git a/View/UserPage.xaml b/View/UserPage.xaml index e422dfb..6fb10de 100644 --- a/View/UserPage.xaml +++ b/View/UserPage.xaml @@ -105,7 +105,7 @@ Binding="{Binding LastOfflineTime}" MinWidth="300" CanUserReorder="False"/> - diff --git a/View/UserPage.xaml.cs b/View/UserPage.xaml.cs index 5c8f220..cc41989 100644 --- a/View/UserPage.xaml.cs +++ b/View/UserPage.xaml.cs @@ -1,4 +1,5 @@ using SunlightAggregationManager.UserClass; +using SunlightAggregationManager.UserWindow; using SunlightAggregationManager.ViewModel; using System; using System.Collections.Generic; @@ -47,7 +48,8 @@ namespace SunlightAggregationManager.View private void MenuItem_Information(object sender, RoutedEventArgs e)//信息 { - + User userSet =new User(); + userSet.Show(); } private async void MenuItem_Offine(object sender, RoutedEventArgs e)//下线 @@ -79,9 +81,6 @@ namespace SunlightAggregationManager.View { MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + user_id + "'", "99", "State"); } - - - - + } } diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index 8802255..6df0451 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -84,8 +84,10 @@ namespace SunlightAggregationManager.ViewModel row["State"] = 0; row["LinkID"] = 0; } + Machines.RowChanged += Machines_Updata; UserData.RowChanged += UserData_Updata;//注册userdata表更新事件 + UserData.RowDeleting += UserData_Updata; } catch (Exception ex) { @@ -210,7 +212,45 @@ namespace SunlightAggregationManager.ViewModel SQLiteHelpers.InsertData("ActionLog", userlog); } } + public static void Insert_Memory(DataTable DB, Dictionary Value)//插入数据 + { + try + { + lock (DB) + { + DataRow drEmployee = DB.NewRow(); + drEmployee.BeginEdit(); + foreach (KeyValuePair kvp in Value) + { + drEmployee[kvp.Key] = kvp.Value; + } + drEmployee.EndEdit(); + DB.Rows.Add(drEmployee); + drEmployee.AcceptChanges(); + // drEmployee.ClearErrors(); + } + } + catch (Exception ex) + { + Console.WriteLine("Insert_Row:" + ex.ToString()); + } + } + public static void Delete_Memory(DataTable DB, string? key)//删除数据 + { + try + { + lock (DB) + { + DataRow drEmployee = DB.Select(key).First(); + drEmployee.Delete(); + } + } + catch (Exception ex) + { + Console.WriteLine("Delete_Row:" + ex.ToString()); + } + } public static DataRow? Selet_Row(DataTable DB, string? key)//查询 { try @@ -294,5 +334,6 @@ namespace SunlightAggregationManager.ViewModel Console.WriteLine("SDTD:" + ex.ToString()); } } + } }