Plane.FormationCreator/Plane.FormationCreator/ViewModels/MainViewModel.cs
2019-12-11 20:40:19 +08:00

451 lines
16 KiB
C#

using Plane.Communication;
using Plane.FormationCreator.Formation;
using Plane.Windows.Messages;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Win32;
using Microsoft.Practices.ServiceLocation;
using System.IO;
using Newtonsoft.Json;
using Plane.Geography;
namespace Plane.FormationCreator.ViewModels
{
public class MainViewModel : ViewModelBase
{
public MainViewModel(CopterListViewModel copterListViewModel)
{
_copterListViewModel = copterListViewModel;
Plane.Windows.Messages.Message.Configure(showAction: msg => this.Message = msg);
Plane.Windows.Messages.Message.Configure(connectAction: connected => this.CommunicationModuleConnected = connected);
this.SwitchVelocityModeButtonContent = GetSwitchVelocityModeButtonContent();
AppEx.Current.PropertyChanged += AppEx_PropertyChanged;
}
private CopterListViewModel _copterListViewModel;
public CopterListViewModel CopterListViewModel { get { return _copterListViewModel; } }
private FlightTaskManager _flightTaskManager = ServiceLocator.Current.GetInstance<FlightTaskManager>();
private GroupManager _groupManager = ServiceLocator.Current.GetInstance<GroupManager>();
private CopterManager _copterManager = ServiceLocator.Current.GetInstance<CopterManager>();
public AppEx AppEx { get; } = AppEx.Current;
private void AppEx_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(AppEx.IsInFastMode):
this.SwitchVelocityModeButtonContent = GetSwitchVelocityModeButtonContent();
break;
default:
break;
}
}
private string _SwitchVelocityModeButtonContent;
public string SwitchVelocityModeButtonContent
{
get { return _SwitchVelocityModeButtonContent; }
set { Set(nameof(SwitchVelocityModeButtonContent), ref _SwitchVelocityModeButtonContent, value); }
}
private string _Message;
public string Message
{
get { return _Message; }
set
{
Set(nameof(Message), ref _Message, value);
Logs += (Environment.NewLine + value);
}
}
private bool _CommunicationModuleConnected;
public bool CommunicationModuleConnected
{
get { return _CommunicationModuleConnected; }
set
{
Set(nameof(CommunicationModuleConnected), ref _CommunicationModuleConnected, value);
}
}
private List<string> _MessageList = new List<string>();
public string Messages
{
get { return string.Join("\r\n", _MessageList.ToArray()); }
}
private string _Logs = "----------软件日志----------";
public string Logs
{
get { return _Logs; }
set { Set(nameof(Logs), ref _Logs, value); }
}
private ICommand _RestartListeningCommand;
public ICommand RestartListeningCommand
{
get
{
return _RestartListeningCommand ?? (_RestartListeningCommand = new RelayCommand(() =>
{
TcpServerConnectionManager.Instance.StopListening();
if (TcpServerConnectionManager.Instance.StartListening())
{
Alert.Show("已重启对连接请求的监听。");
}
else
{
Alert.Show("网络连接不正常,无法启动监听。");
}
UdpServerConnectionManager.Instance.StopReceiving();
UdpServerConnectionManager.Instance.StartReceiving();
}));
}
}
private ICommand _SwitchVelocityModeCommand;
public ICommand SwitchVelocityModeCommand
{
get
{
return _SwitchVelocityModeCommand ?? (_SwitchVelocityModeCommand = new RelayCommand(() =>
{
AppEx.Current.IsInFastMode = !AppEx.Current.IsInFastMode;
}));
}
}
private ICommand _SwitchAppModeCommand;
public ICommand SwitchAppModeCommand
{
get
{
return _SwitchAppModeCommand ?? (_SwitchAppModeCommand = new RelayCommand<AppMode>(mode =>
{
AppEx.Current.AppMode = mode;
}));
}
}
private ICommand _ShowOrHideModifyTaskViewCommand;
public ICommand ShowOrHideModifyTaskViewCommand
{
get
{
return _ShowOrHideModifyTaskViewCommand ?? (_ShowOrHideModifyTaskViewCommand = new RelayCommand(() =>
{
AppEx.Current.ShowModifyTaskView = !AppEx.Current.ShowModifyTaskView;
}));
}
}
private int _MapMode=0;
public int MapMode
{
get { return _MapMode; }
set { Set(nameof(MapMode), ref _MapMode, value); }
}
private ICommand _ChangeMapModeCommand;
public ICommand ChangeMapModeCommand
{
get
{
return _ChangeMapModeCommand ?? (_ChangeMapModeCommand = new RelayCommand(() =>
{
if (MapMode == 0)
MapMode = 1;
else
MapMode = 0;
}));
}
}
private bool _OnlyImpotWaypointS = false;
public bool OnlyImpotWaypointS
{
get { return _OnlyImpotWaypointS; }
set { Set(nameof(OnlyImpotWaypointS), ref _OnlyImpotWaypointS, value); }
}
private ICommand _ExportTasksCommand;
public ICommand ExportTasksCommand
{
get
{
return _ExportTasksCommand ?? (_ExportTasksCommand = new RelayCommand(() =>
{
if (_flightTaskManager.OriginLat == 0 && _flightTaskManager.OriginLng == 0)
{
Alert.Show("作为参照的原点未设置,无法导出相对位置!", "提示");
return;
}
var dialog = new SaveFileDialog
{
DefaultExt = "fcgm",
Filter = "编队任务 (*.fcgm)|*.fcgm|旧编队任务(*.fcg)|*.fcg"
};
if (dialog.ShowDialog() == true)
{
IEnumerable<object> taskObject;
string extension = Path.GetExtension(dialog.FileName);
if (extension == ".fcgm")
taskObject = _flightTaskManager.ExportTasksToMeter();
else
taskObject = _flightTaskManager.ExportTasks();
string exportedText;
if (OnlyImpotWaypointS)
{
exportedText = JsonConvert.SerializeObject(taskObject);
}
else
{
var groupObject = _groupManager.ExportGroups();
var locateObject = ExportLocate();
object obj = new
{
groups = groupObject,
locate = locateObject,
tasks = taskObject
};
exportedText = JsonConvert.SerializeObject(obj);
}
File.WriteAllText(dialog.FileName, exportedText);
}
}));
}
}
private List<object> ExportLocate()
{
List<object> locateList = new List<object>();
foreach (var copter in _copterManager.Copters)
{
double[] locate = new double[3];
locate[0] = copter.Latitude - _flightTaskManager.OriginLat;
locate[1] = copter.Longitude - _flightTaskManager.OriginLng;
locate[2] = copter.GroundAlt;
locateList.Add(locate);
}
return locateList;
}
private int _txtStarindex = 0;
public int txtStarindex
{
get { return _txtStarindex; }
set { Set(nameof(txtStarindex), ref _txtStarindex, value); }
}
private int _txtendindex = 0;
public int txtendindex
{
get { return _txtendindex; }
set { Set(nameof(txtendindex), ref _txtendindex, value); }
}
private ICommand _ImportTasksCommand;
public ICommand ImportTasksCommand
{
get
{
return _ImportTasksCommand ?? (_ImportTasksCommand = new RelayCommand(() =>
{
if (_flightTaskManager.OriginLat == 0 && _flightTaskManager.OriginLng == 0)
{
Alert.Show("作为参照的原点未设置,无法导入相对位置!", "提示");
return;
}
var dialog = new OpenFileDialog
{
DefaultExt = "fcgm",
Filter = "编队任务 (*.fcgm)|*.fcgm|旧编队任务(*.fcg)|*.fcg"
};
if (dialog.ShowDialog() == true)
{
int _startindex = txtStarindex;
int _endindex = txtendindex;
var importText = File.ReadAllText(dialog.FileName);
dynamic importInfo = JsonConvert.DeserializeObject(importText);
dynamic taskinfo = null;
if (importInfo is Newtonsoft.Json.Linq.JObject)
{
taskinfo = importInfo.tasks;
if (importInfo.locate != null)
{
ImportCoptersLocate(importInfo.locate);
}
if (importInfo.groups != null)
{
_groupManager.ImportGroupsInfo(importInfo.groups);
}
}
else if (importInfo is Newtonsoft.Json.Linq.JArray)
{
taskinfo = importInfo;
}
//int task Newtonsoft.Json.Linq.JArray
string extension = Path.GetExtension(dialog.FileName);
bool isMeter = extension == ".fcgm";
if ((txtStarindex == 0) && (txtendindex == 0))
{
_flightTaskManager.ImportTasks(taskinfo, isMeter);
}
else
{
_endindex = txtendindex;
if (_startindex == 0)
_startindex = 1;
if (_endindex == 0)
_endindex = _startindex;
_flightTaskManager.ImportTasksindex(taskinfo, _startindex, _endindex, isMeter);
}
}
}));
}
}
private ICommand _ImportGroupCommand;
public ICommand ImportGroupCommand
{
get
{
return _ImportGroupCommand ?? (_ImportGroupCommand = new RelayCommand(() =>
{
var dialog = new OpenFileDialog
{
DefaultExt = "fcg",
Filter = "编队飞行任务 (*.fcg)|*.fcg"
};
if (dialog.ShowDialog() == true)
{
var importText = File.ReadAllText(dialog.FileName);
dynamic importInfo = JsonConvert.DeserializeObject(importText);
dynamic taskinfo = null;
if (importInfo is Newtonsoft.Json.Linq.JObject)
{
taskinfo = importInfo.tasks;
if (importInfo.groups != null)
{
_groupManager.ImportGroupsInfo(importInfo.groups);
}
}
else if (importInfo is Newtonsoft.Json.Linq.JArray)
{
taskinfo = importInfo;
}
}
}));
}
}
private ICommand _ImportTaskTimeCommand;
public ICommand ImportTaskTimeCommand
{
get
{
return _ImportTaskTimeCommand ?? (_ImportTaskTimeCommand = new RelayCommand(() =>
{
var dialog = new SaveFileDialog
{
DefaultExt = "txt",
Filter = "文本文件 (*.txt)|*.txt"
};
if (dialog.ShowDialog() == true)
{
var importText = File.ReadAllText(dialog.FileName);
dynamic importInfo = JsonConvert.DeserializeObject(importText);
dynamic taskinfo = null;
if (importInfo is Newtonsoft.Json.Linq.JObject)
{
taskinfo = importInfo.tasks;
if (importInfo.groups != null)
{
_groupManager.ImportGroupsInfo(importInfo.groups);
}
}
else if (importInfo is Newtonsoft.Json.Linq.JArray)
{
taskinfo = importInfo;
}
}
}));
}
}
private void ImportCoptersLocate(dynamic coptersLocate)
{
//if (Newtonsoft.Json.Linq.JArray)
try
{
int index = 0;
foreach (var locate in coptersLocate)
{
_copterManager.Copters[index].GroundAlt = Convert.ToSingle(locate[2]);
index++;
}
}
catch (Exception)
{
}
}
private ICommand _ClearLogs;
public ICommand ClearLogs
{
get
{
return _ClearLogs ?? (_ClearLogs = new RelayCommand(() =>
{
Logs = "";
}));
}
}
private string GetSwitchVelocityModeButtonContent()
=> AppEx.Current.IsInFastMode ? "高速模式" : "低速模式";
}
}