Plane.FormationCreator/Plane.FormationCreator/ViewModels/ConnectViewModel.cs
zxd 039f2a57af 修改背景和UI
添加飞机摆放设置
修改为Gmap目前使用的bing模式
添加3D视图和背景
取消断线重连的提示改为状态显示
2018-11-03 10:25:21 +08:00

319 lines
9.6 KiB
C#

using Plane;
using Plane.Collections;
using Plane.FormationCreator.Formation;
using Plane.Logging;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Plane.Communication;
using Plane.Copters;
using Plane.Windows.Messages;
using Plane.FormationCreator.Properties;
using System.IO;
using Plane.CommunicationManagement;
using Plane.Protocols;
using Microsoft.Practices.ServiceLocation;
using System.Windows.Media;
namespace Plane.FormationCreator.ViewModels
{
public class ConnectViewModel : ViewModelBase
{
public ConnectViewModel(CopterManager copterManager)
{
_copterManager = copterManager;
}
private CopterManager _copterManager;
private const int PORT = 5250;
// 不严谨,稍微验证一下。
private Regex _ipListRegex = new Regex(@"^(?:" + Environment.NewLine + @")*\d{1,3}(?:\.\d{1,3}){3}(?:" + Environment.NewLine + @"\d{1,3}(?:\.\d{1,3}){3})*(?:" + Environment.NewLine + @")*$", RegexOptions.Singleline | RegexOptions.Compiled);
// private string _IPs = @"192.168.1.11
//192.168.1.12
//192.168.1.13
//192.168.1.14
//192.168.1.15
//192.168.1.16
//192.168.1.17
//192.168.1.18
//192.168.1.19
//192.168.1.20
//";
private string _IPs = Settings.Default.IPs;
public string IPs
{
get { return _IPs; }
set { Set(nameof(IPs), ref _IPs, value); }
}
private bool _IsProcessing;
public bool IsProcessing
{
get { return _IsProcessing; }
set { Set(nameof(IsProcessing), ref _IsProcessing, value); }
}
private ICommand _ConnectCommand;
public ICommand ConnectCommand
{
get
{
return _ConnectCommand ?? (_ConnectCommand = new RelayCommand<string>(async connectionType =>
{
await ConnectAsync();
}
));
}
}
private ICommand _SendCommand;
public ICommand SendCommand
{
get
{
return _SendCommand ?? (_SendCommand = new RelayCommand(async () =>
{
await SendCommandAsync(CopterNum);
}
));
}
}
// private ICommand _WriteCommand;
// public ICommand WriteCommand
// {
// get
// {
// return _WriteCommand ?? (_WriteCommand = new RelayCommand(async () =>
// {
// await WriteCommandAsync();
// }
// ));
// }
// }
private ICommand _CloseCommand;
public ICommand CloseCommand
{
get
{
return _CloseCommand ?? (_CloseCommand = new RelayCommand(() =>
{
commModule.CloseConnection();
}
));
}
}
private int _CopterNum;
public int CopterNum
{
get { return _CopterNum; }
set { Set(nameof(CopterNum), ref _CopterNum, value); }
}
private ICommand _WriteIdCommand;
public ICommand WriteIdCommand
{
get
{
return _WriteIdCommand ?? (_WriteIdCommand = new RelayCommand(async () =>
{
await WriteIdCommandAsync(CopterNum, MavComm.COMM_SEARCH_MODE);
}
));
}
}
private ICommand _ChangeWriteMissionCommand;
public ICommand ChangeWriteMissionCommand
{
get
{
return _ChangeWriteMissionCommand ?? (_ChangeWriteMissionCommand = new RelayCommand(async () =>
{
await WriteIdCommandAsync(0, MavComm.COMM_DOWNLOAD_MODE);
}
));
}
}
private ICommand _StateInquireCommand;
public ICommand StateInquireCommand
{
get
{
return _StateInquireCommand ?? (_StateInquireCommand = new RelayCommand(async () =>
{
await WriteIdCommandAsync(0, 0x00);
}
));
}
}
private ICommand _QueryAllCopterCommand;
public ICommand QueryAllCopterCommand
{
get
{
return _QueryAllCopterCommand ?? (_QueryAllCopterCommand = new RelayCommand(async () =>
{
if (commModule.CommModuleCopterCount > 0)
{
short queryCount = 40;
short begin = 1;
while (begin < commModule.CommModuleCopterCount)
{
short end = (short)(begin + queryCount -1);
if (end > commModule.CommModuleCopterCount)
end = (short)commModule.CommModuleCopterCount;
byte[] beginByte = BitConverter.GetBytes(begin);
byte[] endByte = BitConverter.GetBytes(end);
byte[] query = beginByte.Concat(endByte).ToArray();
await commModule.WriteCommPacketAsync(0, 0x20, query);
begin += queryCount;
await Task.Delay(1000);
}
}
}
));
}
}
private ICommand _UpdateAllCopterCommand;
public ICommand UpdateAllCopterCommand
{
get
{
return _UpdateAllCopterCommand ?? (_UpdateAllCopterCommand = new RelayCommand(async () =>
{
await commModule.UpdateCommModule();
}));
}
}
private ICommand _CommDataAsync;
public ICommand CommDataAsync
{
get
{
return _CommDataAsync ?? (_CommDataAsync = new RelayCommand(async () =>
{
await commModule.TestLED((short)CopterNum);
}
));
}
}
private List<IMission> CreateLEDMissions(IEnumerable<LEDInfo> LEDInfos)
{
List<IMission> missions = new List<IMission>();
foreach (LEDInfo ledInfo in LEDInfos)
{
Color color = (Color)ColorConverter.ConvertFromString("#" + ledInfo.LEDRGB);
IMission LEDMission = Mission.CreateLEDControlMission(
(int)(ledInfo.Delay * 10),
ledInfo.LEDMode,
ledInfo.LEDRate,
0, //ledInfo.LEDTimes,
color.R,
color.G,
color.B
);
missions.Add(LEDMission);
}
return missions;
}
private async Task WriteIdCommandAsync(int num, short messageType)
{
await commModule.WriteCommPacketAsync((short)num, messageType);
}
private async Task SendCommandAsync(int Count)
{
MavComm.comm_set_mav_count mavCount = new MavComm.comm_set_mav_count();
mavCount.mav_count = (short)Count;
await commModule.GenerateDataAsync(0, MavComm.COMM_SET_MAV_COUNT, mavCount);
}
public async Task<bool> DoCommandAsync(MAVLink.MAV_CMD actionid, float p1, float p2, float p3, float p4, float p5, float p6, float p7)
{
MAVLink.mavlink_command_long_t req = new MAVLink.mavlink_command_long_t();
req.target_system = 1;
req.target_component = 1;
if (actionid == MAVLink.MAV_CMD.COMPONENT_ARM_DISARM)
{
req.target_component = (byte)MAVLink.MAV_COMPONENT.MAV_COMP_ID_SYSTEM_CONTROL;
}
req.command = (ushort)actionid;
req.param1 = p1;
req.param2 = p2;
req.param3 = p3;
req.param4 = p4;
req.param5 = p5;
req.param6 = p6;
req.param7 = p7;
await Task.Delay(10);
return true;
}
CommModuleManager commModule = CommModuleManager.Instance;
private async Task ConnectAsync()
{
await commModule.ConnectAsync();
}
private async Task DisConnetAsync()
{
await Task.Delay(20).ConfigureAwait(false);
}
private Action _closeWindowCallback;
public void SetCloseWindowAction(Action closeWindowAction)
{
_closeWindowCallback = closeWindowAction;
}
private void CloseWindow()
{
if (_closeWindowCallback == null) return;
var dispatcher = App.Current.Dispatcher;
if (dispatcher.CheckAccess())
{
_closeWindowCallback();
}
else
{
dispatcher.InvokeAsync(() =>
{
_closeWindowCallback.Invoke();
});
}
}
}
}