Plane.FormationCreator/Plane.FormationCreator/ViewModels/ConnectViewModel.cs
zxd 725c4b6d71 通信模块协议的添加和修改
小批量、读写通信地面站配置、修改连接id总数
2019-04-11 13:21:50 +08:00

399 lines
12 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 commModule.SetCommCount(CopterCount, (short)(CopterStartNum - 1));
}
));
}
}
// 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 string _CopterColor = "";
public string CopterColor
{
get { return _CopterColor; }
set { Set(nameof(CopterColor), ref _CopterColor, value); }
}
private byte _TransmittedPower = 5;
public byte TransmittedPower
{
get { return _TransmittedPower; }
set { Set(nameof(TransmittedPower), ref _TransmittedPower, value); }
}
private short _CopterCount;
public short CopterCount
{
get { return _CopterCount; }
set { Set(nameof(CopterCount), ref _CopterCount, value); }
}
private short _CopterStartNum;
public short CopterStartNum
{
get { return _CopterStartNum; }
set { Set(nameof(CopterStartNum), ref _CopterStartNum, 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 () =>
{
Message.Show("-----开始空中升级------");
await commModule.UpdateCommModule();
}));
}
}
/// <summary>
/// 全部1S一次闪灯 主要用于测试通信
/// </summary>
private ICommand _CommDataAsync;
public ICommand CommDataAsync
{
get
{
return _CommDataAsync ?? (_CommDataAsync = new RelayCommand(async () =>
{
await commModule.TestLED((short)CopterNum, CopterColor);
}
));
}
}
/// <summary>
/// 设置回传功率
/// </summary>
private ICommand _CommSetPowerAsync;
public ICommand CommSetPowerAsync
{
get
{
return _CommSetPowerAsync ?? (_CommSetPowerAsync = new RelayCommand(async () =>
{
if (TransmittedPower >= 1 && TransmittedPower <= 16)
await commModule.SetCopterCommBackPower((short)CopterNum, TransmittedPower);
else
Alert.Show("功率范围只能设置为1-16");
}));
}
}
/// <summary>
/// 获取配置
/// </summary>
private ICommand _CommGetCurrSysParmAsync;
public ICommand CommGetCurrSysParmAsync
{
get
{
return _CommGetCurrSysParmAsync ?? (_CommGetCurrSysParmAsync = new RelayCommand(async () =>
{
await commModule.GetCurr_Sys_Parm();
}));
}
}
/// <summary>
/// 改写配置
/// </summary>
private ICommand _CommSetCurrSysParmAsync;
public ICommand CommSetCurrSysParmAsync
{
get
{
return _CommSetCurrSysParmAsync ?? (_CommSetCurrSysParmAsync = new RelayCommand(async () =>
{
await commModule.SetCurr_Sys_Parm();
}));
}
}
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();
});
}
}
}
}