223 lines
7.9 KiB
C#
223 lines
7.9 KiB
C#
using GalaSoft.MvvmLight;
|
||
using GalaSoft.MvvmLight.Command;
|
||
using Microsoft.Practices.ServiceLocation;
|
||
using Plane.CommunicationManagement;
|
||
using Plane.CopterManagement;
|
||
using Plane.Copters;
|
||
using Plane.FormationCreator.Formation;
|
||
using Plane.Windows.Messages;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Input;
|
||
|
||
namespace Plane.FormationCreator.ViewModels
|
||
{
|
||
public class CalibrationViewModel : ViewModelBase
|
||
{
|
||
CommModuleManager commModule = CommModuleManager.Instance;
|
||
|
||
private CopterManager _copterManager = ServiceLocator.Current.GetInstance<CopterManager>();
|
||
|
||
private string _AccelerometerTips;
|
||
public string AccelerometerTips
|
||
{
|
||
get
|
||
{
|
||
switch (AccelerometerState)
|
||
{
|
||
case AccelerometerStates.Idle:
|
||
_AccelerometerTips = "点击“开始校准”后,开始校准加速计";
|
||
break;
|
||
case AccelerometerStates.Front:
|
||
_AccelerometerTips = "亮紫色: 飞机水平放置(Front)";
|
||
break;
|
||
case AccelerometerStates.Left:
|
||
_AccelerometerTips = "亮黄色:飞机左侧接触地面竖立放置(Left)";
|
||
break;
|
||
case AccelerometerStates.Right:
|
||
_AccelerometerTips = "亮青色:飞机右侧接触地面竖立放置(Right)";
|
||
break;
|
||
case AccelerometerStates.Down:
|
||
_AccelerometerTips = "紫色:飞机机头向下接触地面竖立放置(Down)";
|
||
break;
|
||
case AccelerometerStates.Up:
|
||
_AccelerometerTips = "黄色:飞机机尾向下接触地面竖立放置(Up)";
|
||
break;
|
||
case AccelerometerStates.Back:
|
||
_AccelerometerTips = "青色:飞机翻过来水平放置(Back)";
|
||
break;
|
||
}
|
||
return _AccelerometerTips;
|
||
}
|
||
}
|
||
|
||
private string _AccelerometerBtnText = "开始校准";
|
||
public string AccelerometerBtnText
|
||
{
|
||
get
|
||
{
|
||
switch (AccelerometerState)
|
||
{
|
||
case AccelerometerStates.Idle:
|
||
_AccelerometerBtnText = "开始校准";
|
||
break;
|
||
default:
|
||
_AccelerometerBtnText = "完成";
|
||
break;
|
||
}
|
||
return _AccelerometerBtnText;
|
||
}
|
||
}
|
||
|
||
//校准加速计的状态
|
||
public enum AccelerometerStates
|
||
{
|
||
Idle = 0,
|
||
Front = 1,
|
||
Left = 2,
|
||
Right = 3,
|
||
Down = 4,
|
||
Up = 5,
|
||
Back = 6
|
||
}
|
||
|
||
private AccelerometerStates _AccelerometerState = AccelerometerStates.Idle;
|
||
public AccelerometerStates AccelerometerState
|
||
{
|
||
get { return _AccelerometerState; }
|
||
set
|
||
{
|
||
Set(nameof(AccelerometerState), ref _AccelerometerState, value);
|
||
RaisePropertyChanged(nameof(AccelerometerBtnText));
|
||
RaisePropertyChanged(nameof(AccelerometerTips));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 开始校准加速计
|
||
/// </summary>
|
||
private ICommand _CalibrationAccelerometerStartCommand;
|
||
public ICommand CalibrationAccelerometerStartCommand
|
||
{
|
||
get
|
||
{
|
||
return _CalibrationAccelerometerStartCommand ?? (_CalibrationAccelerometerStartCommand = new RelayCommand(async () =>
|
||
{
|
||
if (_copterManager.SelectedCopters.Count() == 0) return;
|
||
await commModule.DoStartPreflightCompassAsync(_copterManager.SelectedCopters);
|
||
}));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校准加速计下一步
|
||
/// </summary>
|
||
private ICommand _CalibrationAccelerometerNextCommand;
|
||
public ICommand CalibrationAccelerometerNextCommand
|
||
{
|
||
get
|
||
{
|
||
return _CalibrationAccelerometerNextCommand ?? (_CalibrationAccelerometerNextCommand = new RelayCommand(async () =>
|
||
{
|
||
if (_copterManager.SelectedCopters.Count() == 0) return;
|
||
await commModule.DoNextPreflightCompassAsync(_copterManager.SelectedCopters);
|
||
}));
|
||
}
|
||
}
|
||
|
||
private int _CompassPercent;
|
||
public int CompassPercent
|
||
{
|
||
get { return _CompassPercent; }
|
||
set
|
||
{
|
||
Set(nameof(CompassPercent), ref _CompassPercent, value);
|
||
}
|
||
}
|
||
|
||
public bool IsCalibration { get; set; }
|
||
/// <summary>
|
||
/// 校准指南针
|
||
/// </summary>
|
||
private ICommand _CalibrationCompassCommand;
|
||
public ICommand CalibrationCompassCommand
|
||
{
|
||
get
|
||
{
|
||
return _CalibrationCompassCommand ?? (_CalibrationCompassCommand = new RelayCommand(async () =>
|
||
{
|
||
if (_copterManager.SelectedCopters.Count() == 0) return;
|
||
|
||
// ICopter copter = _copterManager.SelectedCopters.FirstOrDefault();
|
||
//
|
||
// short copterId = short.Parse(copter.Name);
|
||
Message.Show("开始校准指南针");
|
||
|
||
await commModule.DoCalibrationCompassAsync(_copterManager.SelectedCopters);
|
||
await Task.Delay(50).ConfigureAwait(false);
|
||
|
||
/*支持多飞行机同时校准 不再判断单架的返回值
|
||
|
||
IsCalibration = true;
|
||
CompassPercent = 0;
|
||
int State = 0; //4:成功 5失败 todo 改为枚举
|
||
|
||
|
||
while (IsCalibration && CompassPercent <= 100)
|
||
{
|
||
//两个255的时候表示 当前预留字节代表的意思是校准
|
||
if (copter.Retain[2] == 255 && copter.Retain[3] == 255)
|
||
{
|
||
CompassPercent = copter.Retain[0];
|
||
State = copter.Retain[1];
|
||
if (State == 4 || State == 5)
|
||
break;
|
||
}
|
||
|
||
await Task.Delay(100);
|
||
}
|
||
|
||
switch (State)
|
||
{
|
||
case 4:
|
||
Alert.Show($"校准成功,请重新上电{copterId}号", "指南针");
|
||
break;
|
||
case 5:
|
||
Alert.Show("校准失败", "指南针");
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
IsCalibration = false;
|
||
*/
|
||
}));
|
||
}
|
||
}
|
||
|
||
private ICommand _CancelCalibrationCompassCommand;
|
||
public ICommand CancelCalibrationCompassCommand
|
||
{
|
||
get
|
||
{
|
||
return _CancelCalibrationCompassCommand ?? (_CancelCalibrationCompassCommand = new RelayCommand(async () =>
|
||
{
|
||
if (_copterManager.SelectedCopters.Count() == 1) return;
|
||
|
||
ICopter copter = _copterManager.SelectedCopters.FirstOrDefault();
|
||
|
||
short copterId = short.Parse(copter.Name);
|
||
await commModule.DoCancelCalibrationCompassAsync(copterId);
|
||
IsCalibration = false;
|
||
Alert.Show("放弃校准", "指南针");
|
||
}));
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
}
|