using Aitex.Core.Common; using Aitex.Core.UI.MVVM; using FurnaceUI.Controls.Common; using MECF.Framework.Common.Equipment; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using CB = MECF.Framework.UI.Client.ClientBase; namespace FurnaceUI.Controls.Parts { /// /// ATMDualArmRobot.xaml 的交互逻辑 /// public partial class BoatElevatorRobot : UserControl, INotifyPropertyChanged { protected readonly int MoveTime = 300; private const int AnimationTimeout = 3000; // seconds public int MinValue { get { return (int)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } } // Using a DependencyProperty as the backing store for MinValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(int), typeof(BoatElevatorRobot), new PropertyMetadata(0)); public int MaxValue { get { return (int)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } } // Using a DependencyProperty as the backing store for MaxValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(int), typeof(BoatElevatorRobot), new PropertyMetadata(1000)); public int CurrentValue { get { return (int)GetValue(CurrentValueProperty); } set { SetValue(CurrentValueProperty, value); } } // Using a DependencyProperty as the backing store for CurrentValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(int), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender)); public int RotateAngle { get { return (int)GetValue(RotateAngleProperty); } set { SetValue(RotateAngleProperty, value); } } // Using a DependencyProperty as the backing store for RotateAngel. This enables animation, styling, binding, etc... public static readonly DependencyProperty RotateAngleProperty = DependencyProperty.Register("RotateAngel", typeof(int), typeof(BoatElevatorRobot), new PropertyMetadata(0)); public ModuleName Station { get { return (ModuleName)GetValue(StationProperty); } set { SetValue(StationProperty, value); } } // Using a DependencyProperty as the backing store for Station. This enables animation, styling, binding, etc... public static readonly DependencyProperty StationProperty = DependencyProperty.Register("Station", typeof(ModuleName), typeof(BoatElevatorRobot), new PropertyMetadata(ModuleName.Robot)); public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc... public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(BoatElevatorRobot), new PropertyMetadata(null)); public string RobotTarget { get { return (string)GetValue(RobotTargetProperty); } set { SetValue(RobotTargetProperty, value); } } public static readonly DependencyProperty RobotTargetProperty = DependencyProperty.Register("RobotTarget", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); public Dictionary StationPosition { get { return (Dictionary)GetValue(StationPositionProperty); } set { SetValue(StationPositionProperty, value); } } // Using a DependencyProperty as the backing store for StationPosition. This enables animation, styling, binding, etc... public static readonly DependencyProperty StationPositionProperty = DependencyProperty.Register("StationPosition", typeof(Dictionary), typeof(BoatElevatorRobot), new PropertyMetadata(null, PropertyChangedCallback)); public string ArmAExtended { get { return (string)GetValue(ArmAExtendedProperty); } set { SetValue(ArmAExtendedProperty, value); } } // Using a DependencyProperty as the backing store for ArmAExtended. This enables animation, styling, binding, etc... public static readonly DependencyProperty ArmAExtendedProperty = DependencyProperty.Register("ArmAExtended", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata(RobotConstant.RobotRetract, FrameworkPropertyMetadataOptions.AffectsRender)); public string ArmBExtended { get { return (string)GetValue(ArmBExtendedProperty); } set { SetValue(ArmBExtendedProperty, value); } } // Using a DependencyProperty as the backing store for armBExtended. This enables animation, styling, binding, etc... public static readonly DependencyProperty ArmBExtendedProperty = DependencyProperty.Register("ArmBExtended", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata(RobotConstant.RobotRetract, FrameworkPropertyMetadataOptions.AffectsRender)); public WaferInfo[] RobotWafers { get { return (WaferInfo[])GetValue(RobotWafersProperty); } set { SetValue(RobotWafersProperty, value); } } // Using a DependencyProperty as the backing store for RobotWafers. This enables animation, styling, binding, etc... public static readonly DependencyProperty RobotWafersProperty = DependencyProperty.Register("RobotWafers", typeof(WaferInfo[]), typeof(BoatElevatorRobot), new PropertyMetadata(null, PropertyChangedCallback)); public CB.WaferInfo Wafer1 { get { return (CB.WaferInfo)GetValue(Wafer1Property); } set { SetValue(Wafer1Property, value); } } // Using a DependencyProperty as the backing store for Wafer1. This enables animation, styling, binding, etc... public static readonly DependencyProperty Wafer1Property = DependencyProperty.Register("Wafer1", typeof(CB.WaferInfo), typeof(BoatElevatorRobot), new PropertyMetadata(null)); public CB.WaferInfo Wafer2 { get { return (CB.WaferInfo)GetValue(Wafer2Property); } set { SetValue(Wafer2Property, value); } } // Using a DependencyProperty as the backing store for Wafer2. This enables animation, styling, binding, etc... public static readonly DependencyProperty Wafer2Property = DependencyProperty.Register("Wafer2", typeof(CB.WaferInfo), typeof(BoatElevatorRobot), new PropertyMetadata(null)); private string CurrentPosition; private string CurrentArmA; private string CurrentArmB; public string BoatStatus { get { return (string)GetValue(BoatStatusProperty); } set { SetValue(BoatStatusProperty, value); } } // Using a DependencyProperty as the backing store for armBExtended. This enables animation, styling, binding, etc... public static readonly DependencyProperty BoatStatusProperty = DependencyProperty.Register("BoatStatus", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata("Init", RobotStatusChangedCallback)); public string RootImagePath { get { return (string)GetValue(RootImagePathProperty); } set { SetValue(RootImagePathProperty, value); } } // Using a DependencyProperty as the backing store for RootImagePath. This enables animation, styling, binding, etc... public static readonly DependencyProperty RootImagePathProperty = DependencyProperty.Register("RootImagePath", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata("/FurnaceUI;component/Resources/Images/Controls3/02.png", FrameworkPropertyMetadataOptions.AffectsRender)); public string Canvas1ImagePath { get { return (string)GetValue(Canvas1ImagePathProperty); } set { SetValue(Canvas1ImagePathProperty, value); } } // Using a DependencyProperty as the backing store for RootImagePath. This enables animation, styling, binding, etc... public static readonly DependencyProperty Canvas1ImagePathProperty = DependencyProperty.Register("Canvas1ImagePath", typeof(string), typeof(BoatElevatorRobot), new FrameworkPropertyMetadata("/FurnaceUI;component/Resources/Images/Controls3/lghg.png", FrameworkPropertyMetadataOptions.AffectsRender)); static void RobotStatusChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (BoatElevatorRobot)d; var test = d; switch ((string)e.NewValue) { case "Homing": self.Canvas1ImagePath = "/FurnaceUI;component/Resources/Images/Controls3/lghg.png"; break; case "Error": self.Canvas1ImagePath = "/FurnaceUI;component/Resources/Images/Controls3/lghg.png"; break; default: self.Canvas1ImagePath = "/FurnaceUI;component/Resources/Images/Controls3/lghg.png"; break; } } private List menu; public List Menu { get { return menu; } set { menu = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Menu")); } } public ICommand MoveCommand { get { return new DelegateCommand(MoveTo); } } private AnimationQueue queue; protected int homePositionX = 0; protected int processPositionX = 0; public event PropertyChangedEventHandler PropertyChanged; static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { //var self = (BoatElevatorRobot)d; //switch (e.Property.Name) //{ // case "StationPosition": // var positions = (Dictionary)e.NewValue; // //var menu1 = new MenuItem() { Header = "Arm" }; // //var subMenu1 = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = new Tuple(x.Key, RobotConstant.RobotExtend, RobotConstant.RobotRetract) }).ToList(); // //foreach (var menu in subMenu1) // //{ // // menu1.Items.Add(menu); // //} // //self.menu = subMenu1;// new List().Add(subMenu1); // break; // case "RobotWafers": // //if (e.NewValue == null) // //{ // // self.Wafer1 = null; // // self.Wafer2 = null; // //} // //else // //{ // // if (self.RobotWafers.Length > 1) // // { // // self.Wafer1 = self.RobotWafers[0]; // // self.Wafer2 = self.RobotWafers[1]; // // } // //} // break; // default: // break; //} var self = (BoatElevatorRobot)d; switch (e.Property.Name) { case "StationPosition": var positions = (Dictionary)e.NewValue; //var param = new AnimationParameter() { Target = positions.Keys.ToList()[0], ArmA = RobotConstant.RobotExtend, ArmB = RobotConstant.RobotRetract }; ////if (self.CurrentPosition != param.Target || self.CurrentArmA != param.ArmA || self.CurrentArmB != param.ArmB) ////{ //// self.MoveRobot((AnimationParameter)param); ////} //self.CurrentPosition = param.Target; //self.CurrentArmA = param.ArmA; //self.CurrentArmB = param.ArmB; //var menu1 = new MenuItem() { Header = "Arm" }; //var subMenu1 = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = new Tuple(x.Key, RobotConstant.RobotExtend, RobotConstant.RobotRetract) }).ToList(); //foreach (var menu in subMenu1) //{ // menu1.Items.Add(menu); //} //self.menu = subMenu1;// new List().Add(subMenu1); self.homePositionX = (int)positions["HomePosition"]?.StartPosition.X; self.processPositionX = (int)positions["ProcessPosition"]?.StartPosition.X; break; } } public BoatElevatorRobot() { #if DEBUG System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical; #endif InitializeComponent(); root.DataContext = this; queue = new AnimationQueue("Robot Animation"); //queue.StatusUpdated += Queue_StatusUpdated; //canvas1.Rotate(200); //canvas2.Rotate(260); //canvasRoot.Rotate(90); CurrentPosition = ModuleName.System.ToString(); CurrentArmA = CurrentArmB = RobotConstant.RobotRetract; } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); if (DesignerProperties.GetIsInDesignMode(this)) { return; } //if (RobotTarget != null || ArmAExtended != null || ArmBExtended != null) //{ // LogMsg(string.Format("Received ------ Target: {0} ArmA: {1} ArmB: {2} ", RobotTarget, ArmAExtended, ArmBExtended)); Invoke(() => MoveRobot(new AnimationParameter() { Target = RobotTarget, ArmA = ArmAExtended, ArmB = ArmBExtended })); //} } private void Queue_StatusUpdated(object sender, StatusUpdateArgs e) { LogMsg(string.Format("Target: {0} - {1}", CurrentPosition, e.Parameter.Target)); var nextEvent = new AutoResetEvent(false); var next = new Action(() => { nextEvent.Set(); }); var wait = new Action(() => { if (!nextEvent.WaitOne(AnimationTimeout)) { LogMsg("Aniamtion time out!"); } }); var done = new Action(() => { CurrentPosition = e.Parameter.Target; CurrentArmA = e.Parameter.ArmA; CurrentArmB = e.Parameter.ArmB; e.Event.Set(); }); var needMove = CurrentPosition != e.Parameter.Target || CurrentArmA != e.Parameter.ArmA || CurrentArmB != e.Parameter.ArmB; if (needMove) { Invoke(() => MoveRobot(e.Parameter, next)); wait(); } done(); } private void MoveRobot(AnimationParameter parameter, Action onComplete = null) { MoveToStart(parameter.Target, () => TranslateTo(parameter.Target, () => MoveToEnd(parameter, onComplete))); } private void MoveToStart(string station, Action onComplete = null) { //if (StationPosition == null || station == "Unknown" || station == null) // return; //var position = StationPosition[station]; //canvas1.Rotate(position.StartPosition.Root, true, MoveTime); //CurrentPosition = station; onComplete?.Invoke(); } private void MoveToEnd(AnimationParameter parameter, Action onComplete = null) { //var position = StationPosition[parameter.Target]; //var extended = false; //if (parameter.ArmA == RobotConstant.RobotExtend) //{ // extended = true; // canvas1.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete); // canvas2.Rotate(position.EndPosition.Arm, true, MoveTime); //} //CurrentPosition = parameter.Target; //if (!extended) //{ onComplete?.Invoke(); //} } private void TranslateTo(string station, Action onComplete = null) { //var position = StationPosition[station]; //if (position.StartPosition.X.HasValue) //{ // Translate(position.StartPosition.X.Value, onComplete); //} //else //{ Translate(CurrentValue, onComplete); onComplete?.Invoke(); //} } private int oldCurrentValue = 0; int oldX = 0; private void Translate(int x, Action onComplete = null) { //var storyBoard = new Storyboard(); //var oldX = translate.X; //AnimationHelper.TranslateY(canvasRoot, (int)oldX, x, MoveTime, onComplete); //oldX = x; //var animation = new DoubleAnimation(oldX, x, TimeSpan.FromMilliseconds(MoveTime)); //storyBoard.Children.Add(animation); //Storyboard.SetTarget(animation, root); //var propertyChain = new DependencyProperty[] // { // Canvas.RenderTransformProperty, // TransformGroup.ChildrenProperty, // TranslateTransform.XProperty // }; //string thePath = "(0).(1)[1].(2)"; //PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); //Storyboard.SetTargetProperty(animation, myPropertyPath); //storyBoard.Completed += (s, e) => //{ // //InvalidateVisual(); // if (onComplete != null) // { // onComplete(); // } //}; //storyBoard.Begin(); if (oldCurrentValue != x) { var xValue = (int)((((float)x) / (MinValue - MaxValue)) * (homePositionX - processPositionX)); AnimationHelper.TranslateY(canvasRoot, oldX, xValue, MoveTime, onComplete); oldX = xValue; oldCurrentValue = x; } } private void MoveTo(object obj) { var para = (Tuple)obj; MoveRobot(new AnimationParameter() { Target = para.Item1, ArmA = para.Item2, ArmB = para.Item3 }); } private void Invoke(Action action) { Dispatcher.Invoke(action); } private void LogMsg(string msg) { var source = "ATMRobot"; Console.WriteLine("{0} {1}", source, msg); } } }