233 lines
8.5 KiB
C#
233 lines
8.5 KiB
C#
using Plane.Copters;
|
||
using Plane;
|
||
using Plane.FormationCreator.Formation;
|
||
using GalaSoft.MvvmLight;
|
||
using GalaSoft.MvvmLight.CommandWpf;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Input;
|
||
using System.Threading;
|
||
using Plane.Communication;
|
||
using Plane.Geography;
|
||
|
||
namespace Plane.FormationCreator.ViewModels
|
||
{
|
||
public class CopterListViewModel : ViewModelBase
|
||
{
|
||
public CopterListViewModel(CopterManager copterManager, MapManager mapManager, FlightTaskManager flightTaskManager)
|
||
{
|
||
_copterManager = copterManager;
|
||
_mapManager = mapManager;
|
||
_flightTaskManager = flightTaskManager;
|
||
System.Timers.Timer t = new System.Timers.Timer(5000); //实例化Timer类,设置间隔时间为1秒;
|
||
t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;
|
||
t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
|
||
t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
|
||
|
||
|
||
|
||
}
|
||
|
||
public void theout(object source, System.Timers.ElapsedEventArgs e)
|
||
{
|
||
int allrec = 0;
|
||
int allsend = 0;
|
||
int copterrec = 0;
|
||
int coptersend = 0;
|
||
foreach (var copter in _copterManager.Copters)
|
||
{
|
||
copter.GetCommunicationNumber(out copterrec, out coptersend);
|
||
allrec += copterrec;
|
||
allsend += coptersend;
|
||
copter.ResetCommunicationNumber();
|
||
|
||
|
||
}
|
||
allrec /= 5;
|
||
allsend /= 5;
|
||
if (_copterManager.Copters.Count>0)
|
||
Communinfo = "接收:" + allrec + "/s;发送:"+ allsend+"/s 单机:"+ allrec/ _copterManager.Copters.Count+"/s;"+
|
||
allsend / _copterManager.Copters.Count+"/s";
|
||
|
||
|
||
}
|
||
|
||
private CopterManager _copterManager;
|
||
private MapManager _mapManager;
|
||
private FlightTaskManager _flightTaskManager;
|
||
|
||
public CopterManager CopterManager { get { return _copterManager; } }
|
||
|
||
private ICopter _SelectedCopter;
|
||
public ICopter SelectedCopter
|
||
{
|
||
get { return _SelectedCopter; }
|
||
set { Set(nameof(SelectedCopter), ref _SelectedCopter, value); }
|
||
}
|
||
|
||
private int _virtualCopterId = 1;
|
||
private LatLng? _lastVirtualCopterLocation;
|
||
|
||
private string _Communinfo = "0字节/秒";
|
||
public string Communinfo
|
||
{
|
||
get { return _Communinfo; }
|
||
set { Set(nameof(Communinfo), ref _Communinfo, value); }
|
||
}
|
||
|
||
private int _IntervalNum;
|
||
public int IntervalNum
|
||
{
|
||
get { return _IntervalNum; }
|
||
set { Set(nameof(IntervalNum), ref _IntervalNum, value); }
|
||
}
|
||
|
||
private int _ContinuousNum;
|
||
public int ContinuousNum
|
||
{
|
||
get { return _ContinuousNum; }
|
||
set { Set(nameof(ContinuousNum), ref _ContinuousNum, value); }
|
||
}
|
||
|
||
private ICommand _IntervalSelectCoptersCommand;
|
||
public ICommand IntervalSelectCoptersCommand
|
||
{
|
||
get
|
||
{
|
||
return _IntervalSelectCoptersCommand ?? (_IntervalSelectCoptersCommand = new RelayCommand(() =>
|
||
{
|
||
if (_copterManager.AcceptingControlCopters.Count() != 1 || IntervalNum == 0 || ContinuousNum == 0)
|
||
return;
|
||
|
||
ICopter copter = _copterManager.SelectedCopters.FirstOrDefault();
|
||
_copterManager.Select(null);
|
||
int index = _copterManager.Copters.IndexOf(copter);
|
||
_copterManager.shiftkeydown = true;
|
||
for (; index < _copterManager.Copters.Count; index += IntervalNum)
|
||
{
|
||
for (int i = 0; i < ContinuousNum; i++)
|
||
{
|
||
if (index >= _copterManager.Copters.Count) break;
|
||
_copterManager.Select(_copterManager.Copters[index]);
|
||
index++;
|
||
}
|
||
}
|
||
_copterManager.shiftkeydown = false;
|
||
|
||
}));
|
||
}
|
||
}
|
||
|
||
private ICommand _AddVirtualCopterCommand;
|
||
public ICommand AddVirtualCopterCommand
|
||
{
|
||
get
|
||
{
|
||
return _AddVirtualCopterCommand ?? (_AddVirtualCopterCommand = new RelayCommand<int>(async addcount =>
|
||
{
|
||
var center = _mapManager.Center;
|
||
string id;
|
||
|
||
int colnum = 10; //自动生成列数=4
|
||
float coldis = 5;//列相距5米
|
||
float rowdis = 2.5f;//行相距5米
|
||
int currcol = 0; //当前列号
|
||
int currrow = 0; //当前行
|
||
Tuple<double, double> colheadLatLng = new Tuple<double, double>(0, 0);
|
||
Tuple<double, double> targetLatLng = new Tuple<double, double>(0, 0);
|
||
|
||
LatLng? colLatLng = new LatLng(center.Lat, center.Lng); ;
|
||
|
||
for (int i = 0; i < addcount; ++i, ++_virtualCopterId)
|
||
{
|
||
id = _virtualCopterId.ToString();
|
||
|
||
|
||
|
||
if (i == 0)
|
||
{
|
||
_lastVirtualCopterLocation = new LatLng(center.Lat, center.Lng);
|
||
|
||
colLatLng = _lastVirtualCopterLocation;
|
||
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
if (currcol < colnum)
|
||
targetLatLng = GeographyUtils.CalcLatLngSomeMetersAway2D(colLatLng.Value.Lat, colLatLng.Value.Lng , 90, currcol * coldis);
|
||
else
|
||
{
|
||
currrow++;
|
||
currcol = 0;
|
||
targetLatLng = GeographyUtils.CalcLatLngSomeMetersAway2D(colLatLng.Value.Lat, colLatLng.Value.Lng, 180, rowdis);
|
||
colLatLng = new LatLng(targetLatLng.Item1, targetLatLng.Item2);
|
||
|
||
colheadLatLng = targetLatLng;
|
||
}
|
||
_lastVirtualCopterLocation= new LatLng(targetLatLng.Item1, targetLatLng.Item2);
|
||
}
|
||
currcol++;
|
||
|
||
|
||
|
||
|
||
/* _lastVirtualCopterLocation =
|
||
_lastVirtualCopterLocation == null ?
|
||
new LatLng(center.Lat, center.Lng) :
|
||
new LatLng(_lastVirtualCopterLocation.Value.Lat, _lastVirtualCopterLocation.Value.Lng + 0.0001);
|
||
*/
|
||
|
||
|
||
|
||
var copter = new FakeCopter(SynchronizationContext.Current);
|
||
copter.SetProperties(
|
||
latitude: _lastVirtualCopterLocation.Value.Lat,
|
||
longitude: _lastVirtualCopterLocation.Value.Lng,
|
||
id: id,
|
||
name: id
|
||
);
|
||
await copter.ConnectAsync();
|
||
await copter.GetCopterDataAsync();
|
||
_copterManager.Copters.Add(copter);
|
||
_copterManager.CopterStatus.Add(false);
|
||
}
|
||
}));
|
||
}
|
||
}
|
||
|
||
private ICommand _ClearCoptersCommand;
|
||
public ICommand ClearCoptersCommand
|
||
{
|
||
get
|
||
{
|
||
return _ClearCoptersCommand ?? (_ClearCoptersCommand = new RelayCommand(async () =>
|
||
{
|
||
foreach (var copter in _copterManager.Copters)
|
||
{
|
||
await copter.DisconnectAsync();
|
||
}
|
||
await Task.Delay(100); // 如果不等待一段时间,很可能会再触发 DataStreamReceived 事件导致飞行器重新出现在地图上。
|
||
_copterManager.Copters.Clear();
|
||
_copterManager.CopterStatus.Clear();
|
||
_mapManager.ClearCopters();
|
||
_flightTaskManager.ClearTasks();
|
||
UdpServerConnectionManager.Instance.ClearConnections();
|
||
_virtualCopterId = 1;
|
||
_lastVirtualCopterLocation = null;
|
||
}));
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
}
|