/* * Copyright (c) 2022 LG Electronics Inc. * SPDX-License-Identifier: LicenseRef-LGE-Proprietary */ using Aurora.Api; using Aurora.Framework.Seq; using Aurora.View; 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.Controls; using System.Windows.Threading; using System.Windows; using System.Windows.Input; using AuroraUIComponent.Sequence; using Aurora; using Hermes; namespace AuroraUIComponent.UserControls.SequenceDrawer { public class UCSequenceDiagramControlViewModel : NotifyPropertyChangedBase { private UCSequenceDiagram _ContentDiagram; public UCSequenceDiagram ContentDiagram { get => _ContentDiagram; set { if (SetProperty(ref _ContentDiagram, value)) { NotifyPropertyChanged(nameof(ShowZoomButton)); NotifyPropertyChanged(nameof(EnableZoomButton)); } } } public ICommand CMD_Loaded { get; } public ICommand CMD_Closing { get; } public ICommand CMD_Closed { get; } public ICommand CMD_Refresh { get; } public ICommand CMD_Export { get; } public ICommand CMD_Zoom { get; } public ICommand CMD_Start { get; } public ICommand CMD_Pause { get; } private SeqDrawerParamEx _Param = new SeqDrawerParamEx(); public SeqDrawerParamEx Param { get => _Param; set => SetProperty(ref _Param, value); } public class SeqDrawerParamEx : NotifyPropertyChangedBase { private SequenceBuilder _Builder; public SequenceBuilder Builder { get => _Builder; set => SetProperty(ref _Builder, value); } private ISequence _Seqeunce; public ISequence Seqeunce { get => _Seqeunce; set => SetProperty(ref _Seqeunce, value); } private eDiagramType _DiagramType = eDiagramType.Layered; public eDiagramType DiagramType { get => _DiagramType; set { if (SetProperty(ref _DiagramType, value)) { NotifyPropertyChanged(nameof(IsOptionLayer)); NotifyPropertyChanged(nameof(IsOptionTree)); } } } private eLineType _LineType = eLineType.Curved; public eLineType LineType { get => _LineType; set { if (SetProperty(ref _LineType, value)) { NotifyPropertyChanged(nameof(IsOptionStraight)); NotifyPropertyChanged(nameof(IsOptionCurved)); NotifyPropertyChanged(nameof(IsOptionRightAngle)); } } } private List _VisibleKeys = new List(); public List VisibleKeys { get => _VisibleKeys; set => SetProperty(ref _VisibleKeys, value); } private bool _IsShowSubSequence = false; public bool IsShowSubSequence { get => _IsShowSubSequence; set => SetProperty(ref _IsShowSubSequence, value); } public bool IsOptionLayer { get => DiagramType == eDiagramType.Layered; set { DiagramType = eDiagramType.Layered; } } public bool IsOptionTree { get => DiagramType == eDiagramType.Tree; set { DiagramType = eDiagramType.Tree; } } public bool IsOptionStraight { get => LineType == eLineType.Straight; set { LineType = eLineType.Straight; } } public bool IsOptionCurved { get => LineType == eLineType.Curved; set { LineType = eLineType.Curved; } } public bool IsOptionRightAngle { get => LineType == eLineType.RightAngle; set { LineType = eLineType.RightAngle; } } public bool IsStepMode { get => Seqeunce?.StepMode ?? false; set { if (Seqeunce != null) { Seqeunce.StepMode = value; } } } private string _SeqState; public string SeqState { get { if (Seqeunce != null) { return Seqeunce.RunState.ToString(); } return string.Empty; } } internal void UpdateSeqState() { NotifyPropertyChanged(nameof(SeqState)); } } public string SequenceName { get => SelectedSeq?.SeqId; } public SequenceBuilder SeqBuilder { get => SelectedSeq.SeqBuilder; } private SeqInfo _SelectedSeq; public SeqInfo SelectedSeq { get => _SelectedSeq; set { if (value != _SelectedSeq) { if (_SelectedSeq != null && _SelectedSeq.ISeq != null) { _SelectedSeq.ISeq.RunStateChanged -= RunStateChanged; } if (value != null && value.ISeq != null) { value.ISeq.RunStateChanged += RunStateChanged; } } if (SetProperty(ref _SelectedSeq, value)) { NotifyPropertyChanged(nameof(SequenceName)); NotifyPropertyChanged(nameof(SeqBuilder)); } if (Param.Seqeunce != value.ISeq) { Param.Seqeunce = value.ISeq; Param.Builder = value.SeqBuilder; } if (value.ISeq != null && value.ISeq.RunState != SequenceRunState.Idle) { UpdateRunState(value.ISeq.RunState); } //ContentDiagram = Application.Current.AuroraEnv().SequenceEntry.GetUCSequenceDrawer(); // TODO: 고정부 구현 후 테스트 #warning TODO: 고정부 인터페이스 변경되면 업데이트 필요 (예: GetUCSequenceDrawer(string SequenceID)) ContentDiagram = new UCSequenceDiagram(); ContentDiagram.SequenceID = SequenceName; ContentDiagram.DiagramType = Param.DiagramType; ContentDiagram.LineType = Param.LineType; ContentDiagram.ShowSubSequence = Param.IsShowSubSequence; ContentDiagram.SetVisibleKeys(Param.VisibleKeys); } } private bool _IsRunning; public bool IsRunning { get => _IsRunning; set => SetProperty(ref _IsRunning, value); } private bool _IsPaused; public bool IsPaused { get => _IsPaused; set => SetProperty(ref _IsPaused, value); } public object SequenceParam { get; set; } public Action ResultCallback { get; set; } public List VisibleKeys { get; set; } = new List(); public bool ShowZoomButton { get => ContentDiagram?.HasSubSequence ?? false; } private bool _EnableZoomButton; public bool EnableZoomButton { get => _EnableZoomButton; set => SetProperty(ref _EnableZoomButton, value); } public bool IsStartImmediately = false; public Func GetSeqParamCallback; public UCSequenceDiagramControlViewModel() { ContentDiagram = new UCSequenceDiagram(); CMD_Loaded = new DelegateCommand(OnLoaded); CMD_Closing = new DelegateCommand(OnClosing); CMD_Closed = new DelegateCommand(OnClosed); CMD_Export = new DelegateCommand(OnExport); CMD_Zoom = new DelegateCommand(OnZoom); CMD_Refresh = new DelegateCommand(DoRefresh); CMD_Start = new DelegateCommand(DoStart); CMD_Pause = new DelegateCommand(DoPause); //ShowOptions(false); // ContentDiagram.SetSubNodeCallback(SetSubNode); //ContentDiagram.SetFlowChanged(OnFlowChanged) Param.PropertyChanged += OnDisplayParamChanged; } private void OnDisplayParamChanged(object sender, PropertyChangedEventArgs e) { if (ContentDiagram != null && Param != null) { ContentDiagram.DiagramType = Param.DiagramType; ContentDiagram.LineType = Param.LineType; ContentDiagram.ShowSubSequence = Param.IsShowSubSequence; ContentDiagram.SetVisibleKeys(Param.VisibleKeys); } } private void DoStart(object obj) { if (obj is bool isRunning) { if (isRunning) { SelectedSeq.ISeq.Stop(); } else { SelectedSeq.ISeq.Start(); } } } private void DoPause(object obj) { if (!IsRunning) return; if (obj is bool isPaused) { if (isPaused) { SelectedSeq.ISeq.Resume(); } else { SelectedSeq.ISeq.Pause(); } } } private void OnZoom(object obj) { #warning TODO: 고정부 업데이트 후 연결 //ContentDiagram.Zoom(); } private void OnExport(object obj) { #warning TODO: 고정부 업데이트 후 연결 //ContentDiagram.Export(SequenceName); } private void DoRefresh(object obj) { ContentDiagram.ResetStatus(); } private UserControl _CustomControl; public UserControl CustomControl { get => _CustomControl; set => SetProperty(ref _CustomControl, value); } public void SetCustomControl(UserControl control) { CustomControl = control; } private void OnLoaded(object obj) { //ContentDiagram.SetSequenceParam(Param); } private void RunStateChanged(object sender, (string sequenceId, SequenceRunState state) e) { Application.Current.Dispatcher.InvokeAsync(delegate { UpdateRunState(e.state); }); } private void UpdateRunState(SequenceRunState newState) { var oldState = IsRunning; if (newState == SequenceRunState.Running) { IsRunning = true; } else if (newState == SequenceRunState.Idle) { IsRunning = false; } oldState = IsPaused; if (newState == SequenceRunState.Paused) { IsPaused = true; } else { IsPaused = false; } //Param.SeqState = newState.ToString(); Param.UpdateSeqState(); if (ContentDiagram.HasSubSequence) { if (newState == SequenceRunState.Running) { EnableZoomButton = false; } else { EnableZoomButton = true; } } } private void OnComplete(SequenceActionResult obj) { Application.Current.Dispatcher.InvokeAsync(delegate { IsRunning = false; SelectedSeq?.ISeq.Resume(); }); if (ResultCallback != null) { ResultCallback(obj.Error); } } private async void OnStart() { if (SelectedSeq?.ISeq.RunState == SequenceRunState.Running) { return; } if (GetSeqParamCallback != null) { SequenceParam = GetSeqParamCallback(); if (SequenceParam == null) { return; } } ContentDiagram.ResetStatus(); await Task.Run(() => Thread.Sleep(100)); SelectedSeq?.ISeq.Start(SequenceParam, OnComplete); } private void OnStop() { if (SelectedSeq?.ISeq.RunState == SequenceRunState.Idle) { return; } if (SelectedSeq?.ISeq.RunState == SequenceRunState.Running || SelectedSeq?.ISeq.RunState == SequenceRunState.Paused) { SelectedSeq?.ISeq.Stop(); } } private void Pause() { if (SelectedSeq?.ISeq.RunState == SequenceRunState.Running) { SelectedSeq?.ISeq.Pause(); } } private void Resume() { if (SelectedSeq?.ISeq.RunState == SequenceRunState.Paused) { SelectedSeq?.ISeq.Resume(); } } private void OnClosing(object obj) { if (IsRunning) { //e.Cancel = true; } } private void OnClosed(object obj) { if (SelectedSeq != null) { SelectedSeq.ISeq.StepMode = false; } ContentDiagram.Destroy(); } private bool _ShowOptions; public bool ShowOptions { get => _ShowOptions; set => SetProperty(ref _ShowOptions, value); } } }