using Container.Common; using Container.Model; using Container.Services; using Container.Viewmodel; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; 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.Shapes; using System.Windows.Threading; using System.IO; using Microsoft.Win32; using System.Diagnostics; namespace Container.ChildWindows { /// /// ServiceInfo.xaml 的交互逻辑 /// public partial class ServiceInfo : Window { BackgroundWorker backgroundWorker; DispatcherTimer timer = new DispatcherTimer(); App app; int type = 1; string ErrorLog = ""; private Class_Log log = new Class_Log(); string BuildingName = ""; string BuildingId = ""; string FloorName = ""; string FloorId = ""; string DeviceType = ""; string FileType = ""; int second = 10; public ServiceInfo() { InitializeComponent(); this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; } //SelectBox mytest; private void Services_Loaded(object sender, RoutedEventArgs e) { try { app = ((App)System.Windows.Application.Current); backgroundWorker = new BackgroundWorker(); backgroundWorker.WorkerReportsProgress = true; backgroundWorker.WorkerSupportsCancellation = true; backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged; backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted; backgroundWorker.DoWork += BackgroundWorker_DoWork; backgroundWorker.RunWorkerAsync(); second = 60; timer.Tick += new EventHandler(timer_Tick); timer.Interval = TimeSpan.FromSeconds(1); timer.Start(); Text_Device.Text = app.devicemark = System.Net.Dns.GetHostName(); this.Text_FilePath.Text = app.HttpUrl; HttpMessage httpclient = new HttpMessage(); string ErrorLog = ""; if (httpclient.GetDeviceTypeList(ref ErrorLog)) { if (app.DeviceTypeList.Count() > 0) { //app.Floors = list.ElementAt(0); this.ComBox_Type.ItemsSource = (new BuildingOperate(3)).BuildingMenu; ComBox_Type.SelectedValuePath = "code"; ComBox_Type.DisplayMemberPath = "name"; ComBox_Type.SelectedIndex = 0; ComBox_Type.IsDropDownOpen = false; } } this.ComBox_File.ItemsSource = (new BuildingOperate(4)).BuildingMenu; ComBox_File.SelectedValuePath = "code"; ComBox_File.DisplayMemberPath = "code"; ComBox_File.SelectedIndex = 0; ComBox_File.IsDropDownOpen = false; } catch (Exception ex) { log.WriteLogFile("异常" + ex.ToString(), "ServiceInfoError"); } } //倒计时 private void timer_Tick(object sender, EventArgs e) { if (second > 0) { second--; if (Loading.Visibility == System.Windows.Visibility.Visible) this.Time.Text = "倒计时" + second.ToString() + "s"; } else { if (Loading.Visibility == System.Windows.Visibility.Visible) { if (backgroundWorker.IsBusy == true) { timer.Stop(); backgroundWorker.ReportProgress(1, null); } } else if (UpdateSuccess.Visibility == System.Windows.Visibility.Visible) { timer.Stop(); this.DialogResult = true; } } } private void Button_Click(object sender, RoutedEventArgs e) { lab_msg.Visibility = System.Windows.Visibility.Hidden; //if (type == 1) //{ BuildingName = ""; BuildingId = ""; if (ComBox_Building.Text == "" || ComBox_Building.SelectedValue == null) { lab_msg.Content = "请先选择楼栋信息"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } BuildingName = ComBox_Building.Text.ToString(); BuildingId = ComBox_Building.SelectedValue.ToString(); //} //else if (type == 2) //{ FloorName = ""; FloorId = ""; if (ComBox_Building.Text == "" || ComBox_Building.SelectedValue == null) { lab_msg.Content = "请先选择楼层信息"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } FloorName = ComBox_Floor.Text.ToString(); FloorId = ComBox_Floor.SelectedValue.ToString(); log.WriteLogFile("ComBox_Type.Text="+ ComBox_Type.Text+ ";ComBox_Type.SelectedValue="+ ComBox_Type.SelectedValue, "123"); if (ComBox_Type.Text == "" || ComBox_Type.SelectedValue == null) { lab_msg.Content = "请选择设备类型"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } DeviceType = ComBox_Type.SelectedValue.ToString(); if (ComBox_File.Text == "" || ComBox_File.SelectedValue == null) { lab_msg.Content = "请先选择文件存储"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } FileType= ComBox_File.SelectedValue.ToString(); //if (Text_Device.Text.Trim() == "") { //lab_msg.Content = "请填写设备标识"; //lab_msg.Visibility = System.Windows.Visibility.Visible; //return; app.devicemark = Text_Device.Text.Trim(); } switch (FileType) { case "本地部署": app.FileServerPath = app.HttpUrl; break; case "前置离线": app.FileServerPath = Text_FilePath.Text.Trim(); break; case "直联云": app.FileServerPath = app.OBSPath; break; default: break; } if (string.IsNullOrEmpty(app.FileServerPath)) { lab_msg.Content = "文件服务地址不能为空"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } //} if (backgroundWorker.IsBusy) { lab_msg.Content = "线程正忙,请稍后再试"; lab_msg.Visibility = System.Windows.Visibility.Visible; } else { type = 2; this.deviceinfo.Visibility = System.Windows.Visibility.Hidden; this.Loading.Visibility = System.Windows.Visibility.Visible; string errorMsg = ""; StartExE(app.configstartexe, (app.HttpUrl.Replace("http://", "")).Split('/')[0] + "|" + app.LocalIP + "|" + app.configwebsocket + "|1"); HttpMessage httpmessage = new HttpMessage(); httpmessage.LoadAppConfig(ref errorMsg); httpmessage.LoadBG(); httpmessage.LoadScreenTime(); backgroundWorker.RunWorkerAsync(); app.IsCanConnect = true; } } private void StartExE(string path, string param = "") { try { timer.Stop(); if (!File.Exists(path) && !path.StartsWith("http:")) { if (!File.Exists(System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/")) + path)) { return; } else { path = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/")) + path; } } log.WriteLogFile(path.Substring(path.Length - 4, 4).ToLower(), "startlog"); if (path.Substring(path.Length - 4, 4).ToLower().Equals(".bat")) { param = ""; Process myprocess = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(path, param); path = path.Replace("\\", "/"); startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录 startInfo.Verb = "runas"; startInfo.CreateNoWindow = true; myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; myprocess.Start(); app.AppPlayName = "chrome"; app.CurAppPlayName = app.AppPlayName; app.NowAppType = "bat"; timer.Start(); log.WriteLogFile("打开成功", "startlog"); } else { Process myprocess = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(path, param); path = path.Replace("\\", "/"); startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录 startInfo.Verb = "runas"; startInfo.CreateNoWindow = true; myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; myprocess.Start(); app.AppPlayName = myprocess.ProcessName; app.CurAppPlayName = app.AppPlayName; app.NowAppType = "exe"; timer.Start(); } } catch (Exception ex) { log.WriteLogFile(ex.ToString()); } } void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { //BackgroundWorker worker = sender as BackgroundWorker; HttpMessage httpclient = new HttpMessage(); try { ErrorLog = ""; if (type == 1) { if (httpclient.GetBuildinginfo(ref ErrorLog)) { if (app.Buildinginfo.Count > 0) { //查询成功 backgroundWorker.ReportProgress(0, null); } else { if (ErrorLog.Trim() == "") ErrorLog = "未查到楼栋相关记录";//未查到 backgroundWorker.ReportProgress(1, null); } } else { //ErrorLog = "连接服务器失败。"; backgroundWorker.ReportProgress(1, null); } } else { int i = 0; string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); } } // string[] IP = AddressIP.Split('.'); // if (IP.Count() == 4) //app.deviceid = BuildingName + FloorName + IP[2] + IP[3]; while (true) { if (httpclient.UploadDeviceInfo(BuildingId, FloorId, DeviceType, ref ErrorLog)) { WebSocketManageForLive webForLive = new WebSocketManageForLive(); webForLive.OpenWebSocket(); backgroundWorker.ReportProgress(0, null);//上传成功 break; } else { i++; if (i > 3) { //if (ErrorLog.Trim() == "") // ErrorLog = "上传失败"; backgroundWorker.ReportProgress(1, null); break; } } } } } catch (Exception ex) { ErrorLog = "操作失败"; log.WriteLogFile("异常" + ex.ToString(),"ServiceInfoError"); backgroundWorker.ReportProgress(1, null); } e.Cancel = true; return; } void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { int progressPercentage = e.ProgressPercentage; string userState = (string)e.UserState; try { switch (progressPercentage) { case 0: //reclient.Visibility = System.Windows.Visibility.Hidden; if (type == 1) { timer.Stop(); this.ComBox_Building.ItemsSource = app.Buildinginfo; this.ComBox_Building.ItemsSource = (new BuildingOperate(type)).BuildingMenu; ComBox_Building.SelectedValuePath = "code"; ComBox_Building.DisplayMemberPath = "name"; ComBox_Building.IsDropDownOpen = true; this.Loading.Visibility = System.Windows.Visibility.Hidden; this.deviceinfo.Visibility = Visibility.Visible; } else { second = 2; this.Loading.Visibility = System.Windows.Visibility.Hidden; this.UpdateSuccess.Visibility = System.Windows.Visibility.Visible; timer.Start(); } backgroundWorker.CancelAsync(); break; case 1: if (type == 1) { this.ConnectFailed.Visibility = System.Windows.Visibility.Visible; this.Loading.Visibility = System.Windows.Visibility.Hidden; } else { UpdateErrorInfo.Text = ErrorLog; UpdateErrorInfo.Visibility = System.Windows.Visibility.Visible; this.UpdateFailed.Visibility = System.Windows.Visibility.Visible; this.Loading.Visibility = System.Windows.Visibility.Hidden; } timer.Stop(); lab_msg.Content = ErrorLog; lab_msg.Visibility = System.Windows.Visibility.Visible; backgroundWorker.CancelAsync(); break; case 2: break; default: break; } } catch (System.Exception ex) { log.WriteLogFile("异常" + ex.ToString()); } } private void ButtonBack_Click(object sender, RoutedEventArgs e) { lab_msg.Visibility = System.Windows.Visibility.Hidden; if (backgroundWorker.IsBusy) { lab_msg.Content = "线程正忙,请稍后再试"; lab_msg.Visibility = System.Windows.Visibility.Visible; } else { type = 1; backgroundWorker.RunWorkerAsync(); } } private void reclient_Click(object sender, RoutedEventArgs e) { Button_Click(null,null); } private void Services_Closed(object sender, EventArgs e) { if (backgroundWorker.IsBusy) backgroundWorker.CancelAsync(); timer.Stop(); } string buildingid = ""; private void Combox_Building_changed(object sender, SelectionChangedEventArgs e) { this.ComBox_Floor.Text = ""; this.ComBox_Floor.ItemsSource = null; if (buildingid.Trim().Equals(this.ComBox_Building.SelectedValue.ToString().Trim())) { return; } else { buildingid = this.ComBox_Building.SelectedValue.ToString(); } IEnumerable> list = (app.Buildinginfo.Where(i => i.code.ToString() == this.ComBox_Building.SelectedValue.ToString()).Select(i => i.floors)); if (list.Count() > 0) { app.Floors = list.ElementAt(0); this.ComBox_Floor.ItemsSource = (new BuildingOperate(2)).BuildingMenu; ComBox_Floor.SelectedValuePath = "code"; ComBox_Floor.DisplayMemberPath = "name"; ComBox_Floor.IsDropDownOpen = true; } else { } } private void Combox_File_changed(object sender, SelectionChangedEventArgs e) { if (this.ComBox_File.SelectedValue.ToString() == "前置离线") { Label_FilePath.Visibility = Visibility.Visible; Text_FilePath.Visibility = Visibility.Visible; } else { Label_FilePath.Visibility = Visibility.Hidden; Text_FilePath.Visibility = Visibility.Hidden; } } private void Btn_ReConnection(object sender, RoutedEventArgs e) { this.Close(); //if (backgroundWorker.IsBusy) //{ // ErrorInfo.Content = "线程正忙,请稍后再试"; // ErrorInfo.Visibility = System.Windows.Visibility.Visible; //} //else //{ // ErrorInfo.Visibility = System.Windows.Visibility.Hidden; // type = 1; // second = 60; // timer.Start(); // this.ConnectFailed.Visibility = System.Windows.Visibility.Hidden; // this.Loading.Visibility = System.Windows.Visibility.Visible; // backgroundWorker.RunWorkerAsync(); //} } private void Btn_ReUpdate(object sender, RoutedEventArgs e) { if (backgroundWorker.IsBusy) { this.UpdateErrorInfo.Text = "线程正忙,请稍后再试"; UpdateErrorInfo.Visibility = System.Windows.Visibility.Visible; } else { //type = 2; //second = 60; //timer.Start(); UpdateErrorInfo.Visibility = System.Windows.Visibility.Hidden; this.UpdateFailed.Visibility = System.Windows.Visibility.Hidden; this.Loading.Visibility = System.Windows.Visibility.Hidden; deviceinfo.Visibility = System.Windows.Visibility.Visible; //backgroundWorker.RunWorkerAsync(); } } private void Combox_LeftButtonDown(object sender, MouseButtonEventArgs e) { ComboBox cm = (ComboBox)sender; cm.IsDropDownOpen = true; } } }