using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Aurora.Api;
using VisionCommon;
using System.Reactive.Linq;
namespace VisionUI.Setup
{
///
/// Interaction logic for VisionRepeatView.xaml
///
public partial class VisionRepeatView : UserControl, IUpdateRecipe, IDisposable
{
private static Logger _Logger;
private FovInfo _FovInfo;
private FovRecipe _FovRecipe;
//Patern
private int _patternId;
private bool _isFovPattern;
private const int PATTERN_NUM_1 = 1;
private const int PATTERN_MAX_COUNT = 3;
private const string PATTERN_UNDEFINED = "Undefined";
private PatternAgentBase _FovPatternAgent = null;
private PatternAgentBase _CamPatternAgent = null;
private List _FovPatternInfos = null;
private List _CamPatternInfos = new List();
private Dictionary _CbVisionRepeatPatterns;
private List _patternIndexes = new List();
//end Pattern
private bool _isShapeModelFinder;
public bool IsShapeModelFinder {
get { return _isShapeModelFinder; }
set { _isShapeModelFinder = value; }
}
///
/// 생성자
///
/// Fov 정보
public VisionRepeatView(FovInfo fovInfo)
{
_FovInfo = fovInfo;
_FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
_Logger = new Logger($"VisionRepeatView[{_FovInfo.SubSysName}][{_FovRecipe.FovName}]");
_FovPatternAgent = FovPatternAgentManager.GetAgent(_FovInfo.ToSubSysInfo());
_CamPatternAgent = CamPatternAgentManager.GetAgent(_FovRecipe.CamRecipe.CamId);
InitializeComponent();
}
private void Init() {
CreateCameraView();
CreateComboBoxes();
SetObservable();
}
private void CreateCameraView() {
var cameraView = IpViewManager.CreateCameraView(_FovInfo, eDisplayType.VisionRepeat);
gridCameraView.Children.Add(cameraView);
}
private void CreateComboBoxes() {
_CbVisionRepeatPatterns = new Dictionary(PATTERN_MAX_COUNT);
_CbVisionRepeatPatterns.Add(PATTERN_NUM_1, cbVisionRepeatPattern);
}
private void SetObservable() {
VisionEnv.AuroraEnv.EventObservable.AsObservable(VisionEventId.VISION_REPEAT_SAVE_PARAMS).
Where(x => x.From == _FovRecipe.FovName).Subscribe(msg => UpdatePatternId(msg));
}
private void UpdatePatternId(EventMessage msg) {
var visionRepeatRecipe = VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo);
//visionRepeatRecipe.Enabled = (msg.Param as VisionRepeatRecipe).Enabled;
visionRepeatRecipe.RepeatCount = (msg.Param as VisionRepeatRecipe).RepeatCount;
visionRepeatRecipe.PatternId = _patternId;
visionRepeatRecipe.IsFovPattern = _isFovPattern;
RecipeFileManager.SaveVisionRecipe();
}
private void cbVisionRepeatPattern_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if ((sender as ComboBox).SelectedIndex != -1) {
_patternId = _patternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex].PatternId;
_isFovPattern = _patternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex] is FovPatternIndex;
if (_isFovPattern) {
var patternInfo = _FovPatternAgent.GetPatternInfo(_patternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex]);
(DataContext as VisionRepeatViewModel).SelectedPatternInfo = patternInfo;
if(patternInfo.PatternType == ePatternType.ShapeModelFinder) {
(DataContext as VisionRepeatViewModel).IsShapeModelFinder = true;
}
else {
(DataContext as VisionRepeatViewModel).IsShapeModelFinder = false;
}
}
else {
var patternInfo = _CamPatternAgent.GetPatternInfo(_patternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex]);
(DataContext as VisionRepeatViewModel).SelectedPatternInfo = patternInfo;
if (patternInfo.PatternType == ePatternType.ShapeModelFinder) {
(DataContext as VisionRepeatViewModel).IsShapeModelFinder = true;
}
else {
(DataContext as VisionRepeatViewModel).IsShapeModelFinder = false;
}
}
}
}
private void InitPatternList() {
try {
_FovPatternInfos = _FovPatternAgent.GetMainPatterns(_FovInfo);
if (VisionSettingManager.VisionSetting.UseCamPattern && _CamPatternAgent != null) {
_CamPatternInfos = _CamPatternAgent.GetMainPatterns(_FovInfo);
}
ClearComboBoxItems(_CbVisionRepeatPatterns);
AddComboBoxeItems(_CbVisionRepeatPatterns);
SelectComboBoxItem(_CbVisionRepeatPatterns);
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void AddComboBoxeItems(Dictionary cbPatterns) {
try {
foreach (var cbPatternPair in cbPatterns) {
ComboBox cb = cbPatternPair.Value;
for (int i = 0; i < _FovPatternInfos.Count(); i++) {
_patternIndexes.Add(new FovPatternIndex(_FovInfo, _FovRecipe.FovName, _FovPatternInfos[i].GetPatternId()));
cb.Items.Add(PatternUtil.GetPatternDescription(_FovPatternInfos[i]));
}
if (VisionSettingManager.VisionSetting.UseCamPattern) {
for (int i = 0; i < _CamPatternInfos.Count(); i++) {
_patternIndexes.Add(new CamPatternIndex(_FovRecipe.CamRecipe.CamId, _CamPatternInfos[i].GetPatternId()));
cb.Items.Add(PatternUtil.GetPatternDescription(_CamPatternInfos[i]));
}
}
}
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void SelectComboBoxItem(Dictionary cbPatterns) {
try {
if (VisionSettingManager.VisionSetting.UseCamPattern) {
var startIndex = 0;
var endIndex = _FovPatternInfos.Count();
if (!VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).IsFovPattern) {
startIndex = _FovPatternInfos.Count();
endIndex = _patternIndexes.Count();
}
for (int i = startIndex; i < endIndex; i++) {
if (_patternIndexes[i].PatternId == VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).PatternId) {
cbPatterns[PATTERN_NUM_1].SelectedIndex = i;
}
}
}
else {
for (int i = 0; i < _patternIndexes.Count(); i++) {
if (_patternIndexes[i].PatternId == VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).PatternId) {
cbPatterns[PATTERN_NUM_1].SelectedIndex = i;
}
}
}
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void ClearComboBoxItems(Dictionary cbPatterns) {
foreach (var cbPatternPair in cbPatterns) {
ComboBox cb = cbPatternPair.Value;
cb.Items.Clear();
}
}
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
bool isVisible = (bool)e.NewValue;
if (isVisible == true) {
RefreshVisionRecipes();
Init();
UpdateRecipeFromFile();
(DataContext as VisionRepeatViewModel).LoadCsvNow();
}
else {
ISequence seq = VisionEnv.AuroraEnv.SequenceEntry[VisionKeys.VisionRepeatSeqKey];
if (seq != null) {
if (seq.RunState != SequenceRunState.Idle) {
seq.EmergencyStop();
}
}
seq = VisionEnv.AuroraEnv.SequenceEntry[VisionKeys.VisionRepeatDynamicSeqKey];
if (seq != null) {
if (seq.RunState != SequenceRunState.Idle) {
seq.EmergencyStop();
}
}
Dispose();
}
}
public void RefreshVisionRecipes() {
_FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
(DataContext as VisionRepeatViewModel).RefreshVisionRecipes();
}
public void UpdateRecipeFromFile() {
InitPatternList();
}
public void UpdateRecipeFromUI() {
throw new NotImplementedException();
}
#region Dispose
public void Dispose() {
RemoveComboBoxes();
RemoveCameraView();
}
private void RemoveComboBoxes() {
_CbVisionRepeatPatterns.Clear();
_CbVisionRepeatPatterns = null;
}
private void RemoveCameraView() {
foreach (var child in gridCameraView.Children) {
if (child is UserControl view) {
IpViewManager.DisposeCameraView(view);
}
}
gridCameraView.Children.Clear();
}
#endregion
}
}