using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PipeGallery.Chart; using PipeGallery.Correlator; using PipeGallery.Manage; using PipeGallery.Model; using PipeGallery.ViewModel; using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; namespace PipeGallery.View { /// <summary> /// HomePageView.xaml 的交互逻辑 /// </summary> public partial class HomePageView : UserControl { int iSnapshot = 0; public HomePageView() { InitializeComponent(); this.DataContext = HomePageViewModel.Instance; this.Loaded += HomePageView_Loaded; this.btnSnapshot.Click += btnSnapshot_Click; this.btnAudition.Click += btnAudition_Click; this.btnFilter.Click += btnFilter_Click; this.btnMenu.Click += btnMenu_Click; this.btnStart.Click += btnStart_Click; this.btnReset.Click += btnReset_Click; this.btnSave.Click += btnSave_Click; this.btnShutdown.Click += btnShutdown_Click; this.btnRanging.Click += btnRanging_Click; //this.btnSnapshot.IsEnabled = true; //this.btnFilter.IsEnabled = true; //this.btnReset.IsEnabled = true; //this.btnSave.IsEnabled = false; //this.btnAudition.IsEnabled = true; //this.btnStart.IsEnabled = true; //this.btnRanging.IsEnabled = true; //HomePageViewModel.Instance.BtnAuditionIsEnabled = true; HomePageViewModel.Instance.TransmitterAFault = true; HomePageViewModel.Instance.TransmitterBFault = true; } void btnStart_Click(object sender, RoutedEventArgs e) { if (HomePageViewModel.Instance.StartState) { DataFactory.SnapshotList.Clear(); //CalculteSatus.isLastCalculate = false; CalculteSatus.MaterialDatas.Clear();//清除缓存的管线数据 HomePageViewModel.Instance.StartState = false; CalculteSatus.msp.collectData = false; CalculteSatus.isCalculateStop = true; HomePageViewModel.Instance.BtnFilterIsEnabled = false; HomePageViewModel.Instance.BtnSaveIsEnabled = false; HomePageViewModel.Instance.BtnSnapshotIsEnabled = false; HomePageViewModel.Instance.BtnStartIsEnabled = false; } else { DataFactory.ResetHomePageData(); //DataFactory.SnapshotList.Clear(); CreateView(1);//复位 CalculteSatus.isCalculateStop = false; if (/*CalculteSatus.isLastCalculate &&*/ CalculteSatus.MaterialDatas.Count > 0)//管线条件不为空 { String txt = "上次计算条件:\r管道:"; foreach (PipeMaterialData pipeMaterialData in CalculteSatus.MaterialDatas) { txt += pipeMaterialData.Diameter + "mm," + pipeMaterialData.Length + "m" + pipeMaterialData.Material + ";"; } //string txt = "管道:" + CalculteSatus.diameterData + "mm," + CalculteSatus.lengthData + "m" + Correlator.CalculteSatus.MaterialData txt += "\r干扰频段:" + ((CalculteSatus.cutFreLow < 0 || CalculteSatus.cutFreHigh < 0) ? "-" : (CalculteSatus.cutFreLow.ToString() + "-" + CalculteSatus.cutFreHigh.ToString()) + "Hz") + "\r是否是新管线?"; bool? b = new PopupIsOrNotWindow(txt).ShowDialog(); if (b == true)//新管线 { //CalculteSatus.isLastCalculate = false; CalculteSatus.MaterialDatas.Clear();//清除缓存的管线数据 isNewPipe(); startCreateView();//开始监测创建对比视图 SelectPipeMaterialView selectPipeMaterialView = new SelectPipeMaterialView(); Global.GetMainView().bdrPreposition.Child = selectPipeMaterialView; } else//不是新管线 { if (CalculteSatus.isCompleteCalculate)//一轮计算已完成 { startCreateView();//开始监测创建对比视图 CalculteSatus.isCompleteCalculate = false; } /* 线程优化1 Thread calThread = new Thread(new ThreadStart(execute)); calThread.IsBackground = true; calThread.Start(); * */ Task calThread = new Task(() => execute()); calThread.Start(); } } else//管线条件为空,默认为新管线 { isNewPipe(); startCreateView();//开始监测创建对比视图 CalculteSatus.isCompleteCalculate = false; SelectPipeMaterialView selectPipeMaterialView = new SelectPipeMaterialView(); Global.GetMainView().bdrPreposition.Child = selectPipeMaterialView; } } } void btnSnapshot_Click(object sender, RoutedEventArgs e) { if (DataFactory.SnapshotList.Count < 3 && HomePageViewModel.Instance.BtnFilterIsEnabled) { ColumnGroupInfo snapshot = DataFactory.ChartBaseDictionary[DataFactory.HomePageChartId] as ColumnGroupInfo; ColumnGroupInfo snapshot_copy = new ColumnGroupInfo(); snapshot_copy.ColumnListInfo = snapshot.ColumnListInfo; snapshot_copy.ID = Guid.NewGuid().ToString(); iSnapshot++; snapshot_copy.Name = "快照" + iSnapshot; //snapshot_copy.Name = "快照" + DateTime.Now.ToString("yyyyMMddHHmmss"); DataFactory.SnapshotList.Add(snapshot_copy.ID); DataFactory.ChartBaseDictionary[snapshot_copy.ID] = snapshot_copy; CreateView(DataFactory.SnapshotList.Count + 1); } SaveSnapshot(); } private void CreateView(int viewCount) { if (viewCount < 1) { return; } gridRow.RowDefinitions.Clear(); if (gridRow.Children.Count > 1) { gridRow.Children.RemoveRange(1, gridRow.Children.Count - 1); } for (int i = 0; i < viewCount; i++) { RowDefinition row = new RowDefinition(); row.Height = new GridLength(1, GridUnitType.Star); gridRow.RowDefinitions.Add(row); } Grid.SetRow(gridPipelinechart, viewCount - 1); for (int i = viewCount - 2; i >= 0; i--) { BaseControl baseControl = new BaseControl(DataFactory.SnapshotList[i], DataFactory.ChartBaseDictionary[DataFactory.HomePageChartId].Coordinates); //baseControl.btnClose.Visibility = Visibility.Visible; //baseControl.gridName.Visibility = Visibility.Visible; //baseControl.btnClose.Click += delegate //{ // string id = baseControl.GetID(); // DataFactory.SnapshotList.Remove(id); // DataFactory.ChartBaseDictionary.Remove(id); // CreateView(DataFactory.SnapshotList.Count + 1); //}; gridRow.Children.Add(baseControl); Grid.SetRow(baseControl, i); } } private void startCreateView() { CalculteSatus.createView = 0;//恢复默认值0 DataFactory.SnapshotList.Clear(); //启动一个定时器 DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(500); if (!timer.IsEnabled) { timer.Tick += delegate { switch (CalculteSatus.createView) { case 1://取消定时 timer.Stop(); break; case 2: CreateView(DataFactory.SnapshotList.Count); DataFactory.SnapshotList.Clear(); CalculteSatus.isCompleteCalculate = true; //CalculteSatus.isLastCalculate = false; //CalculteSatus.MaterialDatas.Clear();//清除缓存的管线数据 timer.Stop(); break; default: break; } }; timer.Start(); } } private void SaveSnapshot() { //保存快照 //System.Drawing.Size screen = new System.Drawing.Size((int)gridHomePage.ActualWidth, (int)gridHomePage.ActualHeight); //Bitmap memoryImage = new Bitmap(screen.Width, screen.Height); int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; Bitmap memoryImage = new Bitmap(width, height); Graphics memoryGraphics = Graphics.FromImage(memoryImage); memoryGraphics.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy); MemoryStream data = new MemoryStream(); memoryImage.Save(data, System.Drawing.Imaging.ImageFormat.Png); string strAppPath = AppDomain.CurrentDomain.BaseDirectory; //获得可执行文件的路径 string strPath = strAppPath + "File\\snapshot\\快照_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; FileStream fs = new FileStream(strPath, FileMode.OpenOrCreate); BinaryWriter w = new BinaryWriter(fs); w.Write(data.ToArray()); fs.Close(); data.Close(); new PopupWindow("快照已保存").ShowDialog(); } void btnMenu_Click(object sender, RoutedEventArgs e) { //加载菜单 MenuView menuView = new MenuView(); Global.GetMainView().bdrPreposition.Child = menuView; } void btnFilter_Click(object sender, RoutedEventArgs e) { //加载过滤器页 FilterView filterView = new FilterView(); Global.GetMainView().bdrMain.Child = filterView; } void btnAudition_Click(object sender, RoutedEventArgs e) { bool? b = new PopupIsOrNotWindow("是否是新管线?","提示").ShowDialog(); if(b == true) { isNewPipe(); } //加载听音页 AuditionView auditionView = new AuditionView(); Global.GetMainView().bdrMain.Child = auditionView; //HomePageViewModel.Instance.BtnStartIsEnabled = false; } void HomePageView_Loaded(object sender, RoutedEventArgs e) { DataFactory.InitalHomePageData(canvasCoordinates, gridChartContainer); DataFactory.SnapshotList.Clear(); } /// <summary> /// 复位按钮功能 /// </summary> void btnReset_Click(object sender, RoutedEventArgs e) { DataFactory.ResetHomePageData(); DataFactory.SnapshotList.Clear(); CreateView(1); //CalculteSatus.isLastCalculate = false; CalculteSatus.MaterialDatas.Clear();//清除缓存的管线数据 HomePageViewModel.Instance.BtnStartIsEnabled = true; HomePageViewModel.Instance.BtnAuditionIsEnabled = true; HomePageViewModel.Instance.BtnFilterIsEnabled = true; HomePageViewModel.Instance.BtnSnapshotIsEnabled = true; } /// <summary> /// 保存音频按钮功能 /// </summary> void btnSave_Click(object sender, RoutedEventArgs e) { if (CorrelatorDataSaveThread.devDataValueMap.ContainsKey(DevCode.Dev2) && CorrelatorDataSaveThread.devDataValueMap.ContainsKey(DevCode.Dev1)) { SaveWav sw = new SaveWav(); string strPath = AppDomain.CurrentDomain.BaseDirectory + "File\\audio\\音频_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".wav"; FileStream fs = sw.CreateEmpty(strPath); for (int i = 0; i < CorrelatorDataSaveThread.devDataValueMap[DevCode.Dev2].Count; i++) { sw.WriteFilestream(fs, CorrelatorDataSaveThread.devDataValueMap[DevCode.Dev1][i], CorrelatorDataSaveThread.devDataValueMap[DevCode.Dev2][i]); } sw.WriteHeader(fs, 7500, 2);//重写头文件 new PopupWindow("音频已保存").ShowDialog(); } } /// <summary> /// 关机按钮功能 /// </summary> void btnShutdown_Click(object sender, RoutedEventArgs e) { bool? ret = new PopupWindow("是否确认关机?", "提示").ShowDialog(); if (ret == true) { Process p = new Process(); p.StartInfo.FileName = "shutdown.exe"; p.StartInfo.Arguments = "-s -f -t 0"; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start(); } } /// <summary> /// 测距按钮功能 /// </summary> void btnRanging_Click(object sender, RoutedEventArgs e) { SelectRangingPipeMaterialView selectRangingPipeMaterialView = new SelectRangingPipeMaterialView(); Global.GetMainView().bdrPreposition.Child = selectRangingPipeMaterialView; } /// <summary> /// 下发唤醒、计算指令 /// </summary> private void execute() { CalculteThread c = new CalculteThread(); c.exe(); } //判断是否是新管线 private void isNewPipe() { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// String josnString = File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "number.json", Encoding.UTF8); JObject jo = JObject.Parse(josnString); String date = jo["date"].ToString(); int pipe = Convert.ToInt32(jo["pipe"].ToString()); int locate = Convert.ToInt32(jo["locate"].ToString()); int listen = Convert.ToInt32(jo["listen"].ToString()); String today = System.DateTime.Now.ToString("yyyyMMdd"); if (date == today) { jo["pipe"] = ++pipe; CorrelatorDataSaveThread.Date = date; CorrelatorDataSaveThread.Pipe = pipe; CorrelatorDataSaveThread.Listen = listen; } else { jo["date"] = today; jo["pipe"] = 1; jo["listen"] = 1; CorrelatorDataSaveThread.Date = today; CorrelatorDataSaveThread.Pipe = 1; CorrelatorDataSaveThread.Listen = 1; } jo["locate"] = 1; CorrelatorDataSaveThread.Locate = 1; StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "number.json"); sw.Write(JsonConvert.SerializeObject(jo)); sw.Flush(); sw.Close(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } } }