2017-02-27 02:06:48 +08:00
|
|
|
|
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;
|
2018-08-03 11:43:23 +08:00
|
|
|
|
using Plane.CommunicationManagement;
|
|
|
|
|
using Plane.Protocols;
|
|
|
|
|
using Microsoft.Practices.ServiceLocation;
|
|
|
|
|
using System.Windows.Media;
|
2017-02-27 02:06:48 +08:00
|
|
|
|
|
|
|
|
|
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 =>
|
|
|
|
|
{
|
2018-08-03 11:43:23 +08:00
|
|
|
|
await ConnectAsync();
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ICommand _SendCommand;
|
|
|
|
|
public ICommand SendCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _SendCommand ?? (_SendCommand = new RelayCommand(async () =>
|
|
|
|
|
{
|
2018-08-11 12:13:21 +08:00
|
|
|
|
await SendCommandAsync(CopterNum);
|
2018-08-03 11:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 17:06:22 +08:00
|
|
|
|
// 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(() =>
|
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
commModule.CloseConnection();
|
2018-08-04 17:06:22 +08:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int _CopterNum;
|
|
|
|
|
public int CopterNum
|
|
|
|
|
{
|
|
|
|
|
get { return _CopterNum; }
|
|
|
|
|
set { Set(nameof(CopterNum), ref _CopterNum, value); }
|
|
|
|
|
}
|
2018-08-03 11:43:23 +08:00
|
|
|
|
|
|
|
|
|
private ICommand _WriteIdCommand;
|
|
|
|
|
public ICommand WriteIdCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _WriteIdCommand ?? (_WriteIdCommand = new RelayCommand(async () =>
|
|
|
|
|
{
|
2018-08-11 12:13:21 +08:00
|
|
|
|
await WriteIdCommandAsync(CopterNum, MavComm.COMM_SEARCH_MODE);
|
2018-08-05 13:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ICommand _ChangeWriteMissionCommand;
|
|
|
|
|
public ICommand ChangeWriteMissionCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _ChangeWriteMissionCommand ?? (_ChangeWriteMissionCommand = new RelayCommand(async () =>
|
|
|
|
|
{
|
2018-08-11 12:13:21 +08:00
|
|
|
|
await WriteIdCommandAsync(0, MavComm.COMM_DOWNLOAD_MODE);
|
2018-08-03 11:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 15:35:01 +08:00
|
|
|
|
private ICommand _StateInquireCommand;
|
|
|
|
|
public ICommand StateInquireCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _StateInquireCommand ?? (_StateInquireCommand = new RelayCommand(async () =>
|
|
|
|
|
{
|
|
|
|
|
await WriteIdCommandAsync(0, 0x00);
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 22:30:56 +08:00
|
|
|
|
private ICommand _QueryAllCopterCommand;
|
|
|
|
|
public ICommand QueryAllCopterCommand
|
2018-08-08 15:35:01 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
return _QueryAllCopterCommand ?? (_QueryAllCopterCommand = new RelayCommand(async () =>
|
2018-08-08 15:35:01 +08:00
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-08 15:35:01 +08:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 10:25:21 +08:00
|
|
|
|
private ICommand _UpdateAllCopterCommand;
|
|
|
|
|
public ICommand UpdateAllCopterCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _UpdateAllCopterCommand ?? (_UpdateAllCopterCommand = new RelayCommand(async () =>
|
|
|
|
|
{
|
|
|
|
|
await commModule.UpdateCommModule();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 22:30:56 +08:00
|
|
|
|
private ICommand _CommDataAsync;
|
|
|
|
|
public ICommand CommDataAsync
|
2018-08-03 11:43:23 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
return _CommDataAsync ?? (_CommDataAsync = new RelayCommand(async () =>
|
2018-08-03 11:43:23 +08:00
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
await commModule.TestLED((short)CopterNum);
|
|
|
|
|
}
|
|
|
|
|
));
|
2017-02-27 02:06:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 17:06:22 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 11:43:23 +08:00
|
|
|
|
|
2018-08-05 13:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task WriteIdCommandAsync(int num, short messageType)
|
2018-08-03 11:43:23 +08:00
|
|
|
|
{
|
2018-08-23 22:30:56 +08:00
|
|
|
|
await commModule.WriteCommPacketAsync((short)num, messageType);
|
2018-08-03 11:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-11 12:13:21 +08:00
|
|
|
|
private async Task SendCommandAsync(int Count)
|
2018-08-03 11:43:23 +08:00
|
|
|
|
{
|
2018-08-11 12:13:21 +08:00
|
|
|
|
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);
|
2018-08-03 11:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 15:35:01 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-08-03 11:43:23 +08:00
|
|
|
|
|
|
|
|
|
|
2018-08-23 22:30:56 +08:00
|
|
|
|
CommModuleManager commModule = CommModuleManager.Instance;
|
2018-08-03 11:43:23 +08:00
|
|
|
|
|
|
|
|
|
private async Task ConnectAsync()
|
|
|
|
|
{
|
|
|
|
|
await commModule.ConnectAsync();
|
2018-08-04 17:06:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DisConnetAsync()
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(20).ConfigureAwait(false);
|
|
|
|
|
}
|
2018-08-03 11:43:23 +08:00
|
|
|
|
|
2017-02-27 02:06:48 +08:00
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|