using Plane.Communication; using Plane.Copters; using Plane.FormationCreator.Formation; using Plane.Logging; using Plane.Windows; using Plane.Windows.Messages; using GalaSoft.MvvmLight.Ioc; using Microsoft.Practices.ServiceLocation; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Net; using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows; using Plane.CommunicationManagement; namespace Plane.FormationCreator { /// /// Interaction logic for App.xaml /// public partial class App : Application { public App() { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); ServiceLocatorConfigurer.Instance.Configure(); _logger = ServiceLocator.Current.GetInstance(); _copterManager = ServiceLocator.Current.GetInstance(); _formationController = ServiceLocator.Current.GetInstance(); _mapManager = ServiceLocator.Current.GetInstance(); AppDomain.CurrentDomain.AssemblyResolve += (s, e) => { var simpleName = new AssemblyName(e.Name).Name; if (simpleName.EndsWith(".resources")) { return null; } return LoadAssembly(simpleName); }; this.DispatcherUnhandledException += (s, e) => { _logger.Log(e.Exception); // await _formationController.AllStop(); TcpServerConnectionManager.Instance.StopListening(); UdpServerConnectionManager.Instance.StopReceiving(); }; this.Exit += (s, e) => { try { // TcpServerConnectionManager.Instance.StopListening(); UdpServerConnectionManager.Instance.StopReceiving(); } catch (Exception ex) { // RaiseExceptionThrown(ex); } }; //new Test().Prepare().Run(); } private ILogger _logger; private CopterManager _copterManager; private FormationController _formationController; private MapManager _mapManager; private Assembly LoadAssembly(string simpleName) { String resourceName = this.GetType().Namespace + ".AssemblyLoadingAndReflection." + simpleName + ".dll"; using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { Byte[] assemblyData = new Byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var md = Resources.MergedDictionaries; // md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/VS/Colors.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/VS/Styles.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Plane.Windows.Messages;component/Styles.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Styles/Colors.xaml") }); md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Styles.xaml") }); /* new UpdateChecker("http://dl.Plane.com/tools/ver.php?app=FormationCreator", _logger) .CheckAsync(ver => { var currentVersion = this.GetType().Assembly.GetName().Version; if (currentVersion < ver && Alert.Show("检测到新版本,请下载。", "更新提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { Process.Start("http://dl.Plane.com/tools/FormationCreatorSetup.exe"); this.Shutdown(); } }); */ System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; MainWindow = new MainWindow(); MainWindow.Show(); try { /* by panxu tcp 不使用了 --使用TCP监听在自动获取IP的情况下,刚开始会导致界面无响应,过一会就好 TcpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished; if (!TcpServerConnectionManager.Instance.StartListening()) { Alert.Show("网络连接不正常,无法连接飞机。"); return; } */ /* UdpServerConnectionManager.Instance.ExceptionThrown += (sender, e1) => { _logger.Log(e1.Exception); }; UdpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished; UdpServerConnectionManager.Instance.StartReceiving(); */ CommModuleManager.Instance.CommunicationReceived += CommtionReceivedCopterInfo; CommModuleManager.Instance.CommunicationCopterDisconnect += CommCopterDisconnect; CommModuleManager.Instance.CommunicationConnected += CommCopterconnected; CommModuleManager.Instance.Connect(); //CommModuleManager.Instance.test(); } catch (Exception ex) { Alert.Show("网络连接不正常,无法连接飞机。"); return; } } protected override void OnExit(ExitEventArgs e) { CommModuleManager.Instance.CloseConnection(); base.OnExit(e); } private void Copter_TextReceived(object sender, MessageCreatedEventArgs e) { _logger.Log(e.Message ); } private async Task AddOrUpdateCopter(string ip, IConnection Connection) { var copters = _copterManager.Copters; var copterStatus = _copterManager.CopterStatus; var copter = copters.FirstOrDefault(c => c.Id == ip); if (copter == null) { await Dispatcher.BeginInvoke(new Action(() => { copter = new Copter(Connection, SynchronizationContext.Current, _mapManager.Center.Lat, _mapManager.Center.Lng) { Id = ip, Name = ip.Substring(ip.LastIndexOf('.') + 1) }; int _index; _index=copters.AddCopter(copter); copterStatus.Insert(_index,false); copter.TextReceived += Copter_TextReceived; })); } else { copter.Connection = Connection; } await copter.ConnectAsync().ConfigureAwait(false); } private async void ConnectionManager_ConnectionEstablished(object sender, ConnectionEstablishedEventArgs e) { await AddOrUpdateCopter(e.RemoteAddress, e.Connection); } private async void CommtionReceivedCopterInfo(object sender, CommunicationReceiveCopterInfoEventArgs e) { await UpdateCommCopterInfo(e.Id, e.Package); //await TaskUtils.CompletedTask; } private async void CommCopterDisconnect(object sender, CommunicationCopterDisconnectEventArgs e) { await DisconnectCopter(e.Id); } private async void CommCopterconnected(object sender, CommunicationConnectEventArgs e) { await AddCommCopter(e.Id); } private async Task DisconnectCopter(int id) { var copters = _copterManager.Copters; var copter = copters.FirstOrDefault(c => c.Id == id.ToString()); if (copter != null) { if (copter is PLCopter) { PLCopter plcopter = (PLCopter)copter; plcopter.CommModuleConnected = false; } } await TaskUtils.CompletedTask; } private static readonly object locker = new object(); private async Task AddCommCopter(int id) { await Dispatcher.BeginInvoke(new Action(() => { lock(locker) { var copter = _copterManager.Copters.FirstOrDefault(c => c.Id == id.ToString()); if (copter == null) { var copterStatus = _copterManager.CopterStatus; var connection = new CommConnection(); copter = new Copter(connection, SynchronizationContext.Current, _mapManager.Center.Lat, _mapManager.Center.Lng) { Id = id.ToString(), Name = id.ToString(), }; int _index; _index = _copterManager.Copters.AddCopter(copter); copterStatus.Insert(_index, false); copter.TextReceived += Copter_TextReceived; } } })); } private async Task UpdateCommCopterInfo(int id, byte[] package) { await AddCommCopter(id); lock (locker) { var copter = _copterManager.Copters.FirstOrDefault(c => c.Id == id.ToString()); if (copter!= null && copter is PLCopter) { PLCopter plcopter = (PLCopter)copter; if (!plcopter.CommModuleConnected) plcopter.CommModuleConnected = true; plcopter.InternalCopter.AnalyzeCommMouldePositionIntPacket(package); plcopter.CommModuleConnected = true; } } } } }