using System; using System.Drawing; using System.IO; using System.Windows.Documents; using System.Windows.Forms; using System.Windows; using System.Deployment.Application; using System.Reflection; using SSWMS.Common; using log4net; namespace SSWMS.Server { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Window_Loaded(null, null); this.Closing += (sender, e) => { if (MessageDialog.ShowDialog("确认关闭仓库管理系统服务端")) { try { niNotifyIcon.Dispose(); Service.Stop(); } catch (Exception ex) { MessageDialog.ShowException(ex); } } else { e.Cancel = true; } }; } NotifyIcon niNotifyIcon = null; public static ILog log = LogManager.GetLogger("log"); private void Window_Loaded(object sender, RoutedEventArgs e) { // Initial System.Windows.Application.Current.MainWindow = this; niNotifyIcon = new NotifyIcon { Icon = new Icon(@"Icon.ico"), Visible = true, Text = "仓库管理系统服务端", ContextMenu = new ContextMenu(new MenuItem[] { new MenuItem("显示", (a, b) => { this.Visibility = Visibility.Visible; }), new MenuItem("隐藏", (a, b) => { this.Visibility = Visibility.Hidden; }), new MenuItem("退出", (a, b) => { this.Close(); }) }) }; niNotifyIcon.MouseDoubleClick += (a, b) => { this.Visibility = this.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden; }; this.rtbMessage.Document.Blocks.Clear(); this.miIOControl.Checked += (a, b) => { Service.bIOControl = true; AppSettings.SetValue("IOControlTask", "1"); }; this.miIOControl.Unchecked += (a, b) => { Service.bIOControl = false; AppSettings.SetValue("IOControlTask", "0"); }; this.miIOControlApply.Checked += (a, b) => { Service.bIOControlApply = true; AppSettings.SetValue("IOControlApplyTask", "1"); }; this.miIOControlApply.Unchecked += (a, b) => { Service.bIOControlApply = false; AppSettings.SetValue("IOControlApplyTask", "0"); }; this.miManage.Checked += (a, b) => { Service.bManage = true; AppSettings.SetValue("ManageTask", "1"); }; this.miManage.Unchecked += (a, b) => { Service.bManage = false; AppSettings.SetValue("ManageTask", "0"); }; this.miPlan.Checked += (a, b) => { Service.bPlan = true; AppSettings.SetValue("PlanTask", "1"); }; this.miPlan.Unchecked += (a, b) => { Service.bPlan = false; AppSettings.SetValue("PlanTask", "0"); }; this.miInterface.Checked += (a, b) => { Service.bInterface = true; AppSettings.SetValue("InterfaceTask", "1"); }; this.miInterface.Unchecked += (a, b) => { Service.bInterface = false; AppSettings.SetValue("InterfaceTask", "0"); }; this.miLED.Checked += (a, b) => { try { LED.InitSdk(); } catch (Exception ex) { this.AddMessage($"InitLED 异常 {ex}"); } Service.bLED = true; AppSettings.SetValue("LEDTask", "1"); }; this.miLED.Unchecked += (a, b) => { Service.bLED = false; AppSettings.SetValue("LEDTask", "0"); try { LED.ReleaseSdk(); } catch (Exception ex) { this.AddMessage($"ReleaseLED 异常 {ex}"); } }; this.miPLC.Checked += (a, b) => { Service.bPlc = true; AppSettings.SetValue("PLCTask", "1"); }; this.miPLC.Unchecked += (a, b) => { Service.bPlc = false; AppSettings.SetValue("PLCTask", "0"); }; Service.Log += (sFunctionName, sLog) => { this.AddMessage(string.Format("{0} {1}", sFunctionName, sLog)); }; this.miIOControl.IsChecked = AppSettings.GetValue("IOControlTask") == "1"; this.miIOControlApply.IsChecked = AppSettings.GetValue("IOControlApplyTask") == "1"; this.miManage.IsChecked = AppSettings.GetValue("ManageTask") == "1"; this.miPlan.IsChecked = AppSettings.GetValue("PlanTask") == "1"; this.miInterface.IsChecked = AppSettings.GetValue("InterfaceTask") == "1"; this.miLED.IsChecked = AppSettings.GetValue("LEDTask") == "1"; this.miPLC.IsChecked = AppSettings.GetValue("PLCTask") == "1"; AppSettings.Init(AppSettings.GetValue("IsNetworkDeployed") == "1"); Service.Start(); ShowServiceStatus(); } #region Message public void AddMessage(string sMessage) { try { this.Dispatcher.BeginInvoke(new Action(() => { Paragraph p = new Paragraph(); p.Inlines.Add(new Run(string.Format("{0} {1}", StringUtils.GetCurrentTime(), sMessage))); if (this.rtbMessage.Document.Blocks.FirstBlock == null) { this.rtbMessage.Document.Blocks.Add(p); } else { this.rtbMessage.Document.Blocks.InsertBefore(this.rtbMessage.Document.Blocks.FirstBlock, p); } while (true) { TextRange tr = new TextRange(this.rtbMessage.Document.ContentStart, this.rtbMessage.Document.ContentEnd); if (tr.Text.Length < 5000 || this.rtbMessage.Document.Blocks.Count < 3) { break; } this.rtbMessage.Document.Blocks.Remove(this.rtbMessage.Document.Blocks.LastBlock); } }), null); } catch (Exception ex) { MessageDialog.ShowException(ex); } } private void SaveMessage_Click(object sender, RoutedEventArgs e) { try { if (this.rtbMessage.Document.Blocks.Count == 0) { return; } SaveFileDialog sfd = new SaveFileDialog { Filter = "Text Documents (*.txt)|*.txt" }; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate)) { StreamWriter sw = new StreamWriter(fs); sw.Write(new TextRange(this.rtbMessage.Document.ContentStart, this.rtbMessage.Document.ContentEnd).Text); sw.Close(); sw.Dispose(); fs.Close(); fs.Dispose(); MessageDialog.Show("保存成功"); } } } catch (Exception ex) { MessageDialog.ShowException(ex); } } private void ClearMessage_Click(object sender, RoutedEventArgs e) { if (MessageDialog.ShowDialog("确认清空系统消息")) { this.rtbMessage.Document.Blocks.Clear(); } } #endregion #region Service private void StartWeb_Click(object sender, RoutedEventArgs e) { this.miStartWeb.IsEnabled = false; this.miStopWeb.IsEnabled = true; Service.StartWeb(); } private void StopWeb_Click(object sender, RoutedEventArgs e) { this.miStartWeb.IsEnabled = true; this.miStopWeb.IsEnabled = false; Service.StopWeb(); } private void StartService_Click(object sender, RoutedEventArgs e) { this.miStartService.IsEnabled = false; this.miStopService.IsEnabled = true; Service.StartService(); ShowServiceStatus(); } private void StopService_Click(object sender, RoutedEventArgs e) { this.miStartService.IsEnabled = true; this.miStopService.IsEnabled = false; Service.StopService(); ShowServiceStatus(); } private void ShowServiceStatus() { this.lvWCFService.Items.Clear(); foreach (WCFService service in Service.lWCFService) { try { this.lvWCFService.Items.Add(service); } catch (Exception ex) { this.AddMessage(string.Format("Show Service Status {0}", ex.Message)); } } } #endregion #region About private void Version_Click(object sender, RoutedEventArgs e) { try { if (ApplicationDeployment.IsNetworkDeployed) { MessageDialog.Show(string.Format("仓库管理系统服务端\n当前版本 {0}", ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString())); } else { MessageDialog.Show(string.Format("仓库管理系统服务端\n当前版本 {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString())); } } catch (Exception ex) { this.AddMessage(string.Format("Version {0}", ex.Message)); } } #endregion } }