215 lines
8.4 KiB
C#
215 lines
8.4 KiB
C#
using Aurora;
|
|
using Aurora.Api;
|
|
using Aurora.Api.Manager;
|
|
using Aurora.Framework.Core;
|
|
using Aurora.View;
|
|
using CoreUnit.Control.UserControl.Canvas;
|
|
using CoreUnit.Scanner.UserControl.ScannerCanvas;
|
|
using DB.Recipe;
|
|
using GUI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WeldingCommon.Devices;
|
|
using WeldingCommon.Recipes;
|
|
|
|
namespace WeldingCommon.Views
|
|
{
|
|
public class UCScannerObjectTeachingViewModel : NotifyPropertyChangedBase, IRecipeViewModel
|
|
{
|
|
private readonly Logger Log = new Logger("UCScannerObjectTeachingViewModel");
|
|
|
|
public delegate bool ExternalRecipeChangeRequest(Logger log, IAuroraEnvironment aurora);
|
|
public static ExternalRecipeChangeRequest RecipeChangeRequest;
|
|
|
|
public delegate void ExternalRecipeChangeComplete(Logger Log, IAuroraEnvironment aurora);
|
|
public static ExternalRecipeChangeComplete RecipeChangeComplete;
|
|
|
|
private readonly IAuroraEnvironment _Aurora;
|
|
private readonly IRecipeManager _RecipeManager;
|
|
private readonly IModelManager _ModelManager;
|
|
|
|
private PLCController _PLC;
|
|
|
|
public UCScannerObjectTeachingControlBoxViewModel ScannerObjectTeachingControlBoxViewModel { get; }
|
|
|
|
public UCScannerObjectTeachingViewModel(IAuroraEnvironment aurora,
|
|
ExternalRecipeChangeRequest externalRecipeChangeRequest,
|
|
ExternalRecipeChangeComplete externalRecipeChangeComplete)
|
|
{
|
|
_Aurora = aurora;
|
|
_RecipeManager = aurora.RecipeManager;
|
|
_ModelManager = aurora.ModelManager;
|
|
|
|
_PLC = aurora.DeviceManager[CommonDeviceIds.PLCDevice] as PLCController;
|
|
|
|
ScannerObjectTeachingControlBoxViewModel = new UCScannerObjectTeachingControlBoxViewModel(_Aurora);
|
|
|
|
RecipeChangeRequest = externalRecipeChangeRequest;
|
|
RecipeChangeComplete = externalRecipeChangeComplete;
|
|
|
|
// For LASA image update, after scout
|
|
_Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_SETTING_CHANGED)
|
|
.ObserveOnDispatcher()
|
|
.Subscribe(OnSettingReloaded);
|
|
|
|
// New, Delete, Copy, Rename
|
|
_Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_RECIPE_ENTRY_CHANGED)
|
|
.ObserveOnDispatcher()
|
|
.Subscribe(OnRecipeEntryChanged);
|
|
|
|
_Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_RECIPE_ACTIVATED)
|
|
.ObserveOnDispatcher()
|
|
.Subscribe(OnActivatedRecipe);
|
|
|
|
_Aurora.EventObservable.AsObservable(CommonEventIds.EVENT_RECIPE_CHANGE_REQUEST)
|
|
.ObserveOnDispatcher()
|
|
.Subscribe(OnRecipeChangeRequest);
|
|
|
|
var activeRecipe = _RecipeManager.ActivatedRecipe();
|
|
Reloaded((activeRecipe.Id, activeRecipe.Name));
|
|
WeldingRecipeManager.UpdateAutoRunWeldingPage();
|
|
}
|
|
|
|
private void OnSettingReloaded(object obj)
|
|
{
|
|
// ScannerCanvasCtrlTeaching Update
|
|
WeldingRecipeManager.GetWeldingPage().ReloadSetting();
|
|
}
|
|
|
|
private void OnRecipeEntryChanged(object obj)
|
|
{
|
|
Log.I("SYS_RECIPE_ENTRY_CHANGED, OnRecipeEntryChanged");
|
|
|
|
if (obj == null || !(obj is EventMessage msg)) {
|
|
return;
|
|
}
|
|
|
|
if (msg.Param is RecipeEntryChangedParam param) {
|
|
if (param.AddedId != -1) { //New
|
|
var recipe = _RecipeManager.Recipes().FirstOrDefault(x => x.Id == param.AddedId);
|
|
if (recipe == null) return;
|
|
WeldingRecipeManager.SelectedRecipe = (recipe.Id, recipe.Name);
|
|
WeldingRecipeManager.SaveWeldingRecipe(recipe.Name, includeModel: false);
|
|
} else if (param.RemovedId != -1) { //Remove
|
|
|
|
} else if (param.CopiedToId != -1 && param.CopiedFromId != -1) { //Copy
|
|
|
|
} else if (param.RenameNewId != -1 && param.RenameOldId != -1) { //Rename
|
|
|
|
} else {
|
|
string stringFormat = string.Format($"Error : OnRecipeEntryChanged\n" +
|
|
$"[Add] [{param.AddedId}]\n" +
|
|
$"[Remove] [{param.RemovedId}]\n" +
|
|
$"[Copy] [{param.CopiedFromId}] [{param.CopiedToId}]\n" +
|
|
$"[Rename] [{param.RenameOldId}] [{param.RenameNewId}]");
|
|
MessageUtil.CreateAndShowMessageDialog("Error", stringFormat);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnActivatedRecipe(object obj)
|
|
{
|
|
Log.D("SYS_RECIPE_ACTIVATED OnActivatedRecipe");
|
|
if (obj == null || !(obj is EventMessage msg)) {
|
|
return;
|
|
}
|
|
|
|
// @deprecated temporary support
|
|
// Recipe를 선택하는데 Z축 동작이 되는 것은 굉장히 위험한 상황이다.
|
|
// 한시적으로만 사용한다..
|
|
|
|
//LHS 라인에서 Recipe Rename시 Mdoel이 Activate 되지 않아서 Rename시가 아니라
|
|
//Recipe Active시 Model Active 될 수 있도로 추가
|
|
string modelname = _ModelManager.Models().FirstOrDefault();
|
|
if (modelname.Trim() == "" || modelname == null) {
|
|
string stringFormat = string.Format($"Error : Model, 현재 Recipe에 Model list가 없습니다.");
|
|
MessageUtil.CreateAndShowMessageDialog("Error", stringFormat);
|
|
return;
|
|
}
|
|
_ModelManager.ActivateModel(modelname);
|
|
Log.I($"Activate Model Name : {modelname}");
|
|
|
|
var activeRecipe = _RecipeManager.ActivatedRecipe();
|
|
WeldingRecipeManager.SaveWeldingRecipe(activeRecipe.Name);
|
|
}
|
|
|
|
public void OnRecipeChangeRequest(EventMessage message)
|
|
{
|
|
Log.D("EVENT_RECIPE_CHANGE_REQUEST OnRecipeChangeRequest");
|
|
|
|
//1. 현재 편집 중인 Recipe 저장
|
|
WeldingRecipeManager.OnSaveRecipeForEditing();
|
|
|
|
//2. Activated Recipe Load
|
|
WeldingRecipeManager.OnActiveReloaded();
|
|
|
|
//3. 현재 Page의 object 정보를 가져옴
|
|
if (RecipeChangeRequest != null) {
|
|
if (RecipeChangeRequest(Log, _Aurora) == false) {
|
|
// Recipe 저장하지 않음.
|
|
return;
|
|
}
|
|
} else {
|
|
//Log.W("Check Machine Type");
|
|
Log.W("Check OnRecipeChangeRequest and need function!!");
|
|
}
|
|
|
|
//6. Recipe 저장
|
|
var activeRecipe = _RecipeManager.ActivatedRecipe();
|
|
WeldingRecipeManager.SaveWeldingRecipe(activeRecipe.Name);
|
|
|
|
Log.I("Complete OnRecipeChangeRequest");
|
|
|
|
if (RecipeChangeComplete != null) {
|
|
RecipeChangeComplete(Log, _Aurora);
|
|
} else {
|
|
//Log.W("Check Machine Type");
|
|
Log.W("Check RecipeChangeComplete and need function!!");
|
|
}
|
|
}
|
|
|
|
public void OnChanged((int id, string name) recipe)
|
|
{
|
|
if (WeldingRecipeManager.SelectedRecipe.id == recipe.id && WeldingRecipeManager.SelectedRecipe.name == recipe.name) {
|
|
return;
|
|
}
|
|
|
|
WeldingRecipeManager.SelectedRecipe = recipe;
|
|
WeldingRecipeManager.ReloadWeldingRecipe(recipe.name);
|
|
}
|
|
|
|
public void Reloaded((int id, string name) recipe)
|
|
{
|
|
WeldingRecipeManager.SelectedRecipe = recipe;
|
|
WeldingRecipeManager.ReloadWeldingRecipe(recipe.name);
|
|
}
|
|
|
|
public void OnModelSelectChanged(string modelName)
|
|
{
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
public bool PreSaved(int id, string recipeName)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void OnSaved(int id, string recipeName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(WeldingRecipeManager.SelectedRecipe.name)) {
|
|
return;
|
|
}
|
|
|
|
WeldingRecipeManager.SaveWeldingRecipe(WeldingRecipeManager.SelectedRecipe.name);
|
|
var activeRecipe = _RecipeManager.ActivatedRecipe();
|
|
if (WeldingRecipeManager.SelectedRecipe.id == activeRecipe.Id) {
|
|
WeldingRecipeManager.UpdateAutoRunWeldingPage();
|
|
}
|
|
}
|
|
}
|
|
}
|