313 lines
9.4 KiB
C#
313 lines
9.4 KiB
C#
using GalaSoft.MvvmLight;
|
||
using GalaSoft.MvvmLight.Command;
|
||
using Plane.CommunicationManagement;
|
||
using Plane.Copters;
|
||
using Plane.Windows.Messages;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
|
||
namespace Plane.FormationCreator.Formation
|
||
{
|
||
public partial class FlightTaskSingleCopterInfo
|
||
{
|
||
|
||
public static FlightTaskSingleCopterInfo CreateForFlyToTask(ICopter copter)
|
||
{
|
||
var info = new FlightTaskSingleCopterInfo(copter)
|
||
{
|
||
|
||
};
|
||
return info;
|
||
}
|
||
|
||
public void AddLEDInfo()
|
||
{
|
||
LEDInfo LedInfo = new LEDInfo();
|
||
LEDInfos.Add(LedInfo);
|
||
LedInfo.RemoveEvent += new LEDInfo.RemoveEventHandler(LEDRemove);
|
||
LedInfo.PropertyChanged += (sender, e) =>{RefLEDSumTime();};
|
||
RefLEDSumTime();
|
||
}
|
||
|
||
public void AddLEDInfo(LEDInfo LedInfo)
|
||
{
|
||
LEDInfos.Add(LedInfo);
|
||
LedInfo.RemoveEvent += new LEDInfo.RemoveEventHandler(LEDRemove);
|
||
LedInfo.PropertyChanged += (sender, e) => { RefLEDSumTime(); };
|
||
RefLEDSumTime();
|
||
}
|
||
|
||
public void LEDRemove(object sender)
|
||
{
|
||
LEDInfo LedInfo = sender as LEDInfo;
|
||
LEDInfos.Remove(LedInfo);
|
||
RefLEDSumTime();
|
||
}
|
||
|
||
|
||
public ObservableCollection<LEDInfo> LEDInfos { get; } = new ObservableCollection<LEDInfo>();
|
||
|
||
|
||
//设置飞机航点中灯光
|
||
private ICommand _AddLEDCommand;
|
||
public ICommand AddLEDCommand
|
||
{
|
||
get
|
||
{
|
||
return _AddLEDCommand ?? (_AddLEDCommand = new RelayCommand<double>(async =>
|
||
{
|
||
AddLEDInfo();
|
||
|
||
}));
|
||
}
|
||
}
|
||
private CommModuleManager _commModuleManager = CommModuleManager.Instance;
|
||
|
||
private ICommand _SetAllCopterLEDCommand;
|
||
public ICommand SetAllCopterLEDCommand
|
||
{
|
||
get
|
||
{
|
||
return _SetAllCopterLEDCommand ?? (_SetAllCopterLEDCommand = new RelayCommand<double>(async =>
|
||
{
|
||
foreach (FlightTaskSingleCopterInfo info in _flightTaskManager.SelectedTask.SingleCopterInfos)
|
||
{
|
||
if (!info.Equals(this))
|
||
{
|
||
info.LEDInfos.Clear();
|
||
foreach (LEDInfo led in LEDInfos)
|
||
{
|
||
LEDInfo addLED = new LEDInfo();
|
||
addLED.CopyForm(led);
|
||
info.AddLEDInfo(addLED);
|
||
}
|
||
}
|
||
}
|
||
}));
|
||
}
|
||
}
|
||
|
||
public void RefLEDSumTime()
|
||
{
|
||
float _ledSumTime = 0;
|
||
foreach (LEDInfo led in LEDInfos)
|
||
{
|
||
_ledSumTime += (float)led.Delay;
|
||
}
|
||
LEDSumTime =(float) Math.Round(_ledSumTime, 1);
|
||
}
|
||
|
||
|
||
//灯光总时长
|
||
private float _LEDSumTime = 0;
|
||
public float LEDSumTime
|
||
{
|
||
get { return _LEDSumTime; }
|
||
set { Set(nameof(LEDSumTime), ref _LEDSumTime, value); }
|
||
|
||
}
|
||
|
||
|
||
|
||
bool LEDTestRun = false;
|
||
/*
|
||
private string _TestLEDtxt = "测试";
|
||
public string TestLEDtxt
|
||
{
|
||
get { return _TestLEDtxt; }
|
||
set { Set(nameof(TestLEDtxt), ref _TestLEDtxt, value); }
|
||
}
|
||
*/
|
||
/*
|
||
|
||
LEDMode 0 = "常亮"
|
||
1="同步闪烁(单色)" />
|
||
2 = "异步闪烁(随机)" />
|
||
3="渐亮" />
|
||
4 = "渐暗" />
|
||
5="呼吸灯" />
|
||
6 = "同步闪烁(随机)" />
|
||
7="异步闪烁(单色)" />
|
||
8 = "拉烟" />(飞控是50 ? )
|
||
*/
|
||
//测试灯光
|
||
private ICommand _TestCopterLEDCommand;
|
||
public ICommand TestCopterLEDCommand
|
||
{
|
||
get
|
||
{
|
||
return _TestCopterLEDCommand ?? (_TestCopterLEDCommand = new RelayCommand<double>(async =>
|
||
{
|
||
if (!LEDTestRun)
|
||
{
|
||
|
||
Task.Run(() =>
|
||
{
|
||
LEDTestRun = true;
|
||
foreach (LEDInfo vLEDInfo in LEDInfos)
|
||
{
|
||
Thread.Sleep((int)vLEDInfo.Delay * 1000);
|
||
if (!LEDTestRun) break;
|
||
_commModuleManager.LED_TaskAsync(vLEDInfo.LEDMode, vLEDInfo.LEDInterval, vLEDInfo.LEDTimes, vLEDInfo.LEDRGB, this.Copter);
|
||
}
|
||
//LEDTestRun = false;
|
||
});
|
||
}else
|
||
{
|
||
LEDTestRun = false;
|
||
_commModuleManager.LED_TaskAsync(255, 0, 0, "000000", this.Copter);
|
||
}
|
||
|
||
}));
|
||
}
|
||
}
|
||
|
||
|
||
private ICommand _SetSelectedCopterLEDCommand;
|
||
public ICommand SetSelectedCopterLEDCommand
|
||
{
|
||
get
|
||
{
|
||
return _SetSelectedCopterLEDCommand ?? (_SetSelectedCopterLEDCommand = new RelayCommand<double>(async =>
|
||
{
|
||
foreach (FlightTaskSingleCopterInfo info in _flightTaskManager.SelectedTask.SingleCopterInfos)
|
||
{
|
||
if (_copterManager.AcceptingControlCopters.Contains(info.Copter))
|
||
{
|
||
if (!info.Equals(this))
|
||
{
|
||
info.LEDInfos.Clear();
|
||
foreach (LEDInfo led in LEDInfos)
|
||
{
|
||
LEDInfo addLED = new LEDInfo();
|
||
addLED.CopyForm(led);
|
||
info.AddLEDInfo(addLED);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}));
|
||
}
|
||
}
|
||
}
|
||
|
||
public class LEDInfo :INotifyPropertyChanged
|
||
{
|
||
private double delay = 1; //灯光延时 单位:100ms (从当前任务开为0,延时几秒后设置LED)
|
||
private int ledmode = 0; //灯光模式 0常亮 1闪烁 2随机闪烁(RGB无意义)
|
||
private float ledInterval = 0; //闪烁延时 单位:秒
|
||
private int ledrate = 0; //为了和以前文件兼容,自动转为ledInterval
|
||
private int ledtimes = 0; //次数 (预留、暂取消其意义)
|
||
private string ledRGB = "FFFFFF"; //灯光颜色
|
||
private SolidColorBrush _DrawRGB = new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0xFF));
|
||
|
||
protected internal virtual void OnPropertyChanged(string propertyName)
|
||
{
|
||
if (PropertyChanged != null)
|
||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||
}
|
||
public event PropertyChangedEventHandler PropertyChanged;
|
||
|
||
|
||
public LEDInfo()
|
||
{
|
||
|
||
}
|
||
|
||
public void CopyForm(LEDInfo led)
|
||
{
|
||
delay = led.delay;
|
||
ledmode = led.ledmode;
|
||
ledInterval = led.ledInterval;
|
||
LEDRGB = led.ledRGB;
|
||
}
|
||
public double Delay
|
||
{
|
||
get { return delay; }
|
||
set {
|
||
delay = Math.Round(value, 1);
|
||
OnPropertyChanged("delay");
|
||
}
|
||
}
|
||
|
||
public int LEDMode
|
||
{
|
||
get { return ledmode; }
|
||
set { ledmode = value; }
|
||
}
|
||
public float LEDInterval
|
||
{
|
||
get { return ledInterval; }
|
||
set { ledInterval = value;
|
||
if (ledInterval != 0)
|
||
ledrate = (int)Math.Round(1 / ledInterval); //用于兼容老版本
|
||
}
|
||
}
|
||
|
||
public int LEDRate
|
||
{
|
||
get { return ledrate; }
|
||
set { ledrate = value;
|
||
if ((ledrate!=0)&&(ledInterval==0))
|
||
ledInterval = 1 / ledrate;
|
||
}
|
||
}
|
||
|
||
|
||
public int LEDTimes
|
||
{
|
||
get { return ledtimes; }
|
||
set { ledtimes = value; }
|
||
}
|
||
|
||
public string LEDRGB
|
||
{
|
||
get { return ledRGB; }
|
||
set {
|
||
|
||
ledRGB = value;
|
||
DrawRGB.Color= (Color)ColorConverter.ConvertFromString("#" + ledRGB);
|
||
|
||
}
|
||
}
|
||
|
||
[Newtonsoft.Json.JsonIgnore]
|
||
public SolidColorBrush DrawRGB
|
||
{
|
||
get
|
||
{
|
||
return _DrawRGB;
|
||
}
|
||
set
|
||
{
|
||
_DrawRGB = value;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public delegate void RemoveEventHandler(object sender);
|
||
public event RemoveEventHandler RemoveEvent;
|
||
private ICommand _RemoveLEDCommand;
|
||
[Newtonsoft.Json.JsonIgnore]
|
||
public ICommand RemoveLEDCommand
|
||
{
|
||
get
|
||
{
|
||
return _RemoveLEDCommand ?? (_RemoveLEDCommand = new RelayCommand<double>(async =>
|
||
{
|
||
RemoveEvent(this);
|
||
}));
|
||
}
|
||
}
|
||
}
|
||
}
|