2017-02-27 02:06:48 +08:00
|
|
|
|
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;
|
2018-08-23 22:30:56 +08:00
|
|
|
|
using Plane.CommunicationManagement;
|
2019-01-02 11:35:32 +08:00
|
|
|
|
using Plane.FormationCreator.Util;
|
2017-02-27 02:06:48 +08:00
|
|
|
|
|
|
|
|
|
namespace Plane.FormationCreator
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for App.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class App : Application
|
|
|
|
|
{
|
|
|
|
|
public App()
|
|
|
|
|
{
|
|
|
|
|
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
ServiceLocatorConfigurer.Instance.Configure();
|
|
|
|
|
_logger = ServiceLocator.Current.GetInstance<ILogger>();
|
|
|
|
|
_copterManager = ServiceLocator.Current.GetInstance<CopterManager>();
|
|
|
|
|
_formationController = ServiceLocator.Current.GetInstance<FormationController>();
|
2018-07-23 16:27:01 +08:00
|
|
|
|
_mapManager = ServiceLocator.Current.GetInstance<MapManager>();
|
2017-02-27 02:06:48 +08:00
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
var simpleName = new AssemblyName(e.Name).Name;
|
|
|
|
|
if (simpleName.EndsWith(".resources"))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return LoadAssembly(simpleName);
|
|
|
|
|
};
|
2018-05-12 23:16:13 +08:00
|
|
|
|
this.DispatcherUnhandledException += (s, e) =>
|
2017-02-27 02:06:48 +08:00
|
|
|
|
{
|
|
|
|
|
_logger.Log(e.Exception);
|
2018-05-12 23:16:13 +08:00
|
|
|
|
// await _formationController.AllStop();
|
2017-02-27 02:06:48 +08:00
|
|
|
|
TcpServerConnectionManager.Instance.StopListening();
|
|
|
|
|
UdpServerConnectionManager.Instance.StopReceiving();
|
|
|
|
|
};
|
|
|
|
|
this.Exit += (s, e) =>
|
|
|
|
|
{
|
2017-03-11 02:50:07 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-12 23:16:13 +08:00
|
|
|
|
// TcpServerConnectionManager.Instance.StopListening();
|
2017-03-11 02:50:07 +08:00
|
|
|
|
UdpServerConnectionManager.Instance.StopReceiving();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// RaiseExceptionThrown(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 02:06:48 +08:00
|
|
|
|
};
|
|
|
|
|
//new Test().Prepare().Run();
|
2019-01-02 11:35:32 +08:00
|
|
|
|
|
|
|
|
|
VersionControl.GetVersionFromIni();
|
2017-02-27 02:06:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ILogger _logger;
|
|
|
|
|
private CopterManager _copterManager;
|
|
|
|
|
private FormationController _formationController;
|
2018-07-23 16:27:01 +08:00
|
|
|
|
private MapManager _mapManager;
|
2017-02-27 02:06:48 +08:00
|
|
|
|
|
|
|
|
|
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;
|
2017-03-10 19:58:18 +08:00
|
|
|
|
// md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
|
2017-02-27 02:06:48 +08:00
|
|
|
|
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") });
|
2018-08-03 11:43:23 +08:00
|
|
|
|
/*
|
|
|
|
|
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;
|
|
|
|
|
|
2017-02-27 02:06:48 +08:00
|
|
|
|
MainWindow = new MainWindow();
|
|
|
|
|
MainWindow.Show();
|
2019-11-15 11:14:00 +08:00
|
|
|
|
|
|
|
|
|
|
2018-04-30 19:41:13 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-12 23:16:13 +08:00
|
|
|
|
/* by panxu tcp 不使用了 --使用TCP监听在自动获取IP的情况下,刚开始会导致界面无响应,过一会就好
|
2018-04-30 19:41:13 +08:00
|
|
|
|
TcpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished;
|
|
|
|
|
if (!TcpServerConnectionManager.Instance.StartListening())
|
|
|
|
|
{
|
|
|
|
|
Alert.Show("网络连接不正常,无法连接飞机。");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-12 23:16:13 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2018-08-23 22:30:56 +08:00
|
|
|
|
/*
|
|
|
|
|
UdpServerConnectionManager.Instance.ExceptionThrown += (sender, e1) =>
|
|
|
|
|
{
|
|
|
|
|
_logger.Log(e1.Exception);
|
|
|
|
|
};
|
|
|
|
|
UdpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished;
|
|
|
|
|
UdpServerConnectionManager.Instance.StartReceiving();
|
|
|
|
|
*/
|
|
|
|
|
|
2019-07-01 21:45:44 +08:00
|
|
|
|
//初始化地面站连接
|
2018-08-23 22:30:56 +08:00
|
|
|
|
CommModuleManager.Instance.CommunicationReceived += CommtionReceivedCopterInfo;
|
|
|
|
|
CommModuleManager.Instance.CommunicationCopterDisconnect += CommCopterDisconnect;
|
|
|
|
|
CommModuleManager.Instance.CommunicationConnected += CommCopterconnected;
|
|
|
|
|
CommModuleManager.Instance.Connect();
|
2019-07-01 21:45:44 +08:00
|
|
|
|
|
2018-04-30 19:41:13 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2017-02-27 02:06:48 +08:00
|
|
|
|
{
|
2018-04-30 19:41:13 +08:00
|
|
|
|
Alert.Show("网络连接不正常,无法连接飞机。");
|
2017-03-11 02:50:07 +08:00
|
|
|
|
return;
|
2017-02-27 02:06:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-23 22:30:56 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
CommModuleManager.Instance.CloseConnection();
|
|
|
|
|
base.OnExit(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-07 18:00:24 +08:00
|
|
|
|
private void Copter_TextReceived(object sender, MessageCreatedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_logger.Log(e.Message );
|
|
|
|
|
}
|
2017-02-27 02:06:48 +08:00
|
|
|
|
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(() =>
|
|
|
|
|
{
|
2018-07-23 16:27:01 +08:00
|
|
|
|
copter = new Copter(Connection, SynchronizationContext.Current, _mapManager.Center.Lat, _mapManager.Center.Lng)
|
2017-02-27 02:06:48 +08:00
|
|
|
|
{
|
|
|
|
|
Id = ip,
|
|
|
|
|
Name = ip.Substring(ip.LastIndexOf('.') + 1)
|
2018-07-23 16:27:01 +08:00
|
|
|
|
|
2017-02-27 02:06:48 +08:00
|
|
|
|
};
|
2017-03-17 23:02:08 +08:00
|
|
|
|
int _index;
|
|
|
|
|
_index=copters.AddCopter(copter);
|
2018-07-23 16:27:01 +08:00
|
|
|
|
copterStatus.Insert(_index,false);
|
2017-09-07 18:00:24 +08:00
|
|
|
|
copter.TextReceived += Copter_TextReceived;
|
2017-02-27 02:06:48 +08:00
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
copter.Connection = Connection;
|
|
|
|
|
}
|
|
|
|
|
await copter.ConnectAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void ConnectionManager_ConnectionEstablished(object sender, ConnectionEstablishedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
await AddOrUpdateCopter(e.RemoteAddress, e.Connection);
|
|
|
|
|
}
|
2018-08-23 22:30:56 +08:00
|
|
|
|
|
|
|
|
|
private async void CommtionReceivedCopterInfo(object sender, CommunicationReceiveCopterInfoEventArgs e)
|
|
|
|
|
{
|
2018-08-26 22:08:32 +08:00
|
|
|
|
await UpdateCommCopterInfo(e.Id, e.Package, e.CommModuleVersion);
|
2018-08-23 22:30:56 +08:00
|
|
|
|
//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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 22:08:32 +08:00
|
|
|
|
private async Task UpdateCommCopterInfo(int id, byte[] package, byte CommModuleVersion)
|
2018-08-23 22:30:56 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
2018-08-26 22:08:32 +08:00
|
|
|
|
plcopter.InternalCopter.AnalyzeCommMouldePositionIntPacket(package, CommModuleVersion);
|
2018-08-23 22:30:56 +08:00
|
|
|
|
plcopter.CommModuleConnected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-02-27 02:06:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|