using ACS.SPiiPlusNET; using Aurora; using Aurora.Api; using Aurora.Api.Devices; using Aurora.Api.Manager; using Aurora.Framework.Core; using Aurora.Util.HislogPlus; using Aurora.View; using AuroraACS; using CoreUnit.Scanner.UserControl.ScannerCanvas; using DB.Recipe; using Hermes; using Hermes.Functional; #region Gun에 있던 것 - 전부 Common에 옮기기 using AuroraHeightSensor; using WeldingCommon.Alarm; using WeldingCommon.Device.ACS; using WeldingCommon.Process; using WeldingCommon.Recipes; using WeldingCommon.Settings; #endregion using OpenCvSharp.Dnn; using PropertyTools.Wpf; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.IO; using System.Linq; using System.Reactive.Linq; using System.Runtime.Remoting.Contexts; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; using System.Windows.Forms; using System.Windows.Input; using VisionCommon; using WeldingCommon; //using WeldingCommon.Alarm; //using WeldingCommon.Settings; using WeldingCommon.Utils; using WeldingCommon.Utils.Diff; using static WeldingCommon.Devices.PLCController; using DelegateCommand = Aurora.View.DelegateCommand; using WeldingCommon.Devices.ACS; namespace WeldingCommon.Views { internal class UCMainRecipeViewModel : NotifyPropertyChangedBase, IRecipeViewModel { private Logger Log = new Logger("UCMainRecipeViewModel"); private readonly IAuroraEnvironment _env; public ICommand CMD_VisibleChanged { get; } public ICommand CMD_GetPosition { get; } public ICommand CMD_MovePosition { get; } public double JogMm { get => _JogMm; set { if (value > 3) { System.Windows.MessageBox.Show("3보다 클 수 없습니다."); SetProperty(ref _JogMm, 3); return; } SetProperty(ref _JogMm, value); } } private double _JogMm = 0.05; public ICommand CMD_YP { get; } public ICommand CMD_XM { get; } public ICommand CMD_XP { get; } public ICommand CMD_YM { get; } public bool IsManualStatus { get => _IsManualStatus; set => SetProperty(ref _IsManualStatus, value); } private bool _IsManualStatus = true; private readonly IRecipeManager _Manager; private (int id, string name) _RecipeId; private IScannerACS _motion; // 변경 private ScannerACS _acs; // 변경 private ACSMotor _AxisX; // AxisX,Y는 갈보모터 기준이고 private ACSMotor _AxisY; private ACSMotor _MotorY1, _MotorY2; public MainRecipe Recipe { get => _Recipe; private set => SetProperty(ref _Recipe, value); } private MainRecipe _Recipe; private readonly IDeviceManager _devices; private MainRecipe _RecipeBeforeChanged; public string _recipe; public string PositionSettingGuide { get; set; } public string LaserOnOffRamping { get; set; } public UCMainRecipeViewModel(IAuroraEnvironment auroraEnv, (int, string) recipe, IRecipeItem recipeItem, string recipeId) { _env = auroraEnv; _motion = _acs = _env.DeviceManager[CommonDeviceIds.ACS] as ScannerACS; // 변경 _RecipeId = recipe; _recipe = recipeId; _Manager = auroraEnv.RecipeManager; _devices = auroraEnv.DeviceManager; var setting = _env.SettingEntry[CommonSettingIds.SystemParameter] as WeldingCommon.Settings.SystemParameter; PositionSettingGuide = Path.Combine(setting.ResourceFolderPath, "TeachingPosition.jpg"); LaserOnOffRamping = Path.Combine(setting.ResourceFolderPath, "LaserOnOff.jpg"); //if (recipeId.Contains("A")) { // stage = eStage.A; // _motorX = _env.DeviceManager[MotorIds.ALineX] as ACSMotor; // _motorY = _env.DeviceManager[MotorIds.ALineY] as ACSMotor; //} else { // stage = eStage.B; // _motorX = _env.DeviceManager[MotorIds.BLineX] as ACSMotor; // _motorY = _env.DeviceManager[MotorIds.BLineY] as ACSMotor; //} Recipe = recipeItem as MainRecipe; //_MotorY1 = _devices[CommonMotorIds.AxisY1] as ACSMotor; //변경 //_MotorY2 = _devices[CommonMotorIds.AxisY2] as ACSMotor; //변경 _AxisX = _env.DeviceManager[CommonMotorIds.AxisX2] as ACSMotor; // 변경 //if (Recipe.YAxisUseType == MainRecipe.YAxisType.Y1) { // _MotorY1.Move(); // _AxisY = _env.DeviceManager[MotorIds.AxisY1] as ACSMotor; // _acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)MotorIds.eMotorIdx.AxisY1); //} //else { // _AxisY = _env.DeviceManager[MotorIds.AxisY2] as ACSMotor; // _acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)MotorIds.eMotorIdx.AxisY2); //} CMD_VisibleChanged = new DelegateCommand(VisibleChanged); CMD_GetPosition = new DelegateCommand(GetPosition); CMD_MovePosition = new DelegateCommand(MovePosition); //CMD_AddHeightParam = new DelegateCommand(AddHeightParam); //CMD_DeleteHeightParam = new DelegateCommand(DeleteHeightParam); CMD_YP = new DelegateCommand(OnYPlus); CMD_XM = new DelegateCommand(OnXMinus); CMD_XP = new DelegateCommand(OnXPlus); CMD_YM = new DelegateCommand(OnYMinus); _env.EventObservable.AsObservable(CommonEventIds.EVENT_EQUIPMENT_STATUS_CHANGE) .Subscribe(x => OnChangedEquipmentStatus()); _env.EventObservable.AsObservable(CommonEventIds.EVENT_CHANGE_DOOR_LOCK) .Where(param => param.Param is bool && param.Param != null) .Select(param => (bool)param.Param); } private object OnChangedEquipmentStatus() { IsManualStatus = Status.EquipmentStatus == eEqpStatus.Manual; //_env.RecipeManager.SaveLock(_recipe, !IsManualStatus); return null; } private void OnYPlus(object obj) { if (!_acs.IsActivated) { System.Windows.MessageBox.Show("ACS Deactivated"); return; } _AxisY.SetTargetPos(JogMm, TargetPosType.REL); _AxisY.SetMoveVelocityImm(Speed.L); _AxisY.Move(); } private void OnXMinus(object obj) { if (!_acs.IsActivated) { System.Windows.MessageBox.Show("ACS Deactivated"); return; } _AxisX.SetTargetPos(-JogMm, TargetPosType.REL); _AxisX.SetMoveVelocityImm(Speed.L); _AxisX.Move(); } private void OnXPlus(object obj) { if (!_acs.IsActivated) { System.Windows.MessageBox.Show("ACS Deactivated"); return; } _AxisX.SetTargetPos(JogMm, TargetPosType.REL); _AxisX.SetMoveVelocityImm(Speed.L); _AxisX.Move(); } private void OnYMinus(object obj) { if (!_acs.IsActivated) { System.Windows.MessageBox.Show("ACS Deactivated"); return; } _AxisY.SetTargetPos(-JogMm, TargetPosType.REL); _AxisY.SetMoveVelocityImm(Speed.L); _AxisY.Move(); } private void GetPosition(object obj) { if (!_acs.IsActivated) { System.Windows.MessageBox.Show("ACS Deactivated"); return; } if (System.Windows.MessageBox.Show("위치를 가져오시겠습니까?", "Get Position", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { if (obj is ProcessUnit param) { param.PosX = _AxisX.GetActualPos(); param.PosY = _AxisY.GetActualPos(); } else if (obj is HeightMeareUnit measureUnit) { measureUnit.PosX = _AxisX.GetActualPos(); measureUnit.PosY = _AxisY.GetActualPos(); } } } //private void AddHeightParam(object obj) //{ // if (Recipe.HeightSensorMeasurePoints.Count >= 10) { // MessageUtil.CreateAndShowMessageDialog("Fail", $"List Limit Over [Current:{Recipe.HeightSensorMeasurePoints.Count} Max: 5]"); // return; // } // if (SelectedHeightParam == null) { // Recipe.HeightSensorMeasurePoints.Add(new HeightMeareUnit() { Index = Recipe.HeightSensorMeasurePoints.Count + 1 }); // return; // } // Recipe.HeightSensorMeasurePoints.Insert(Recipe.HeightSensorMeasurePoints.IndexOf(SelectedHeightParam) + 1, new HeightMeareUnit()); // for (int i = 0, j = 1; i < Recipe.HeightSensorMeasurePoints.Count; i++, j++) // Recipe.HeightSensorMeasurePoints[i].Index = j; //} //private void DeleteHeightParam(object obj) //{ // if (SelectedHeightParam == null) // return; // Recipe.HeightSensorMeasurePoints.Remove(SelectedHeightParam); // for (int i = 0, j = 1; i < Recipe.HeightSensorMeasurePoints.Count; i++, j++) // Recipe.HeightSensorMeasurePoints[i].Index = j; //} public bool CheckValidPosition(double xPos, double yPos) { if (!_acs.IsActivated) return false; return false; } private void MovePosition(object obj) { if (!_acs.IsActivated) return; if (System.Windows.MessageBox.Show("이동하시겠습니까?", "Motion Move", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { if (obj is ProcessUnit param) { var teachingSetting = _env.SettingEntry[CommonSettingIds.TeachingPos] as TeachingPositionSetting; // 26.04.13 _AxisX.SetTargetPos(param.PosX, TargetPosType.ABS); _AxisX.SetMoveVelocityImm(Speed.L); _AxisX.Move(); _AxisY.SetTargetPos(param.PosY, TargetPosType.ABS); _AxisY.SetMoveVelocityImm(Speed.L); _AxisY.Move(); } else if(obj is HeightMeareUnit measureUnit) { var teachingSetting = _env.SettingEntry[CommonSettingIds.TeachingPos] as TeachingPositionSetting; //26.04.13 var MoveDirectionNeg = measureUnit.PosX + 0.1 < _AxisX.GetActualPos(); var XList = Recipe.HeightSensorMeasurePoints.Select(t => t.PosX).ToList(); var minValue = XList.Min(); if (_AxisX.GetActualPos() < minValue - 0.01) { System.Windows.MessageBox.Show($"현재위치({_AxisX.GetActualPos()})가 측정위치의 최소값({minValue})보다 작은 경우, 이동이 불가합니다."); return; } _AxisX.SetTargetPos(measureUnit.PosX, TargetPosType.ABS); _AxisX.SetMoveVelocityImm(Speed.L); _AxisX.Move(); _AxisY.SetTargetPos(measureUnit.PosY, TargetPosType.ABS); _AxisY.SetMoveVelocityImm(Speed.L); _AxisY.Move(); } } } private void VisibleChanged(object obj) { //bool visible = (bool)obj; //if (visible){ //} } public void OnChanged((int id, string name) recipe) { var teachingSetting = _env.SettingEntry[CommonSettingIds.TeachingPos] as TeachingPositionSetting; // 26.04.13 Recipe = _Manager.RecipeItem(recipe.id, _recipe) as MainRecipe; _RecipeBeforeChanged = Recipe.Clone() as MainRecipe; if (_MotorY1.IsActivated&& _MotorY2.IsActivated) { if (Recipe.YAxisUseType == MainRecipe.YAxisType.Y1) { _MotorY2.SetTargetPos(teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY2, TargetPosType.ABS); _MotorY2.SetMoveVelocityImm(Speed.L); _MotorY2.Move(); Log.V($"Move Y2 Position : {teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY2}"); //_AxisY = _env.DeviceManager[CommonMotorIds.AxisY1] as ACSMotor; //_acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)CommonMotorIds.eMotorIdx.AxisY1); } else { _MotorY1.SetTargetPos(teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY1, TargetPosType.ABS); _MotorY1.SetMoveVelocityImm(Speed.L); _MotorY1.Move(); Log.V($"Move Y1 Position : {teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY1}"); //_AxisY = _env.DeviceManager[CommonMotorIds.AxisY2] as ACSMotor; //_acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)CommonMotorIds.eMotorIdx.AxisY2); } } } public void Reloaded((int id, string name) recipe) { Recipe = _Manager.RecipeItem(recipe.id, _recipe) as MainRecipe; var teachingSetting = _env.SettingEntry[CommonSettingIds.TeachingPos] as TeachingPositionSetting; if (Recipe.YAxisUseType == MainRecipe.YAxisType.Y1) { _MotorY2.SetTargetPos(teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY2, TargetPosType.ABS); _MotorY2.SetMoveVelocityImm(Speed.L); _MotorY2.Move(); Log.V($"Move Y2 Position : {teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY2}"); //_AxisY = _env.DeviceManager[CommonMotorIds.AxisY1] as ACSMotor; //_acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)CommonMotorIds.eMotorIdx.AxisY1); } else { _MotorY1.SetTargetPos(teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY1, TargetPosType.ABS); _MotorY1.SetMoveVelocityImm(Speed.L); _MotorY1.Move(); Log.V($"Move Y1 Position : {teachingSetting.SettingPositions[(int)eTeachingPosition.StageWeldingStartPos].AxisY1}"); //_AxisY = _env.DeviceManager[CommonMotorIds.AxisY2] as ACSMotor; //_acs.WriteVariableInt("iPCget_LCI_ASMASK_Y", (int)CommonMotorIds.eMotorIdx.AxisY2); } } public void OnModelSelectChanged(string modelName) { } public bool PreSaved(int id, string recipeName) { try { if(ApplicationExt.PermissionLevel > (uint)PermissionLevel.LEVEL3) { MessageUtil.ShowErrorToast("Engineer 권한으로 로그인 하시기 바랍니다.", Log); return false; } if (!IsManualStatus) { MessageUtil.ShowErrorToast("Auto 상태에서 레시피 변경 불가", Log); return false; } return true; } catch (Exception ex) { Log.E($"PreSaved failed: {ex}"); } return false; } public void OnSaved(int id, string recipeName) { var recipe = _Manager.RecipeItem(id, recipeName) as MainRecipe; var diffs = new ObservableCollection(); CompareHelper.Fill(_RecipeBeforeChanged, recipe, diffs, new CompareHelper.FieldSpec("Acceleration", x => x.WeldingAcc), new CompareHelper.FieldSpec("StartPosX", x => x.StartPos.PosX), new CompareHelper.FieldSpec("StartPosY", x => x.StartPos.PosY), new CompareHelper.FieldSpec("EndPosX", x => x.EndPos.PosX), new CompareHelper.FieldSpec("EndPosY", x => x.EndPos.PosY), new CompareHelper.FieldSpec("LaserOnPosition", x => x.LaserOnPosition), new CompareHelper.FieldSpec("RampingOffStartPos", x => x.RampingOffStartPos), new CompareHelper.FieldSpec(nameof(MainRecipe.Arc1), x => x.Arc1.Size), new CompareHelper.FieldSpec(nameof(MainRecipe.Arc2), x => x.Arc2.Size), new CompareHelper.FieldSpec(nameof(MainRecipe.Arc3), x => x.Arc3.Size), new CompareHelper.FieldSpec(nameof(MainRecipe.Arc4), x => x.Arc4.Size), new CompareHelper.FieldSpec("TeachingPos1X", x => x.Units[0].PosX), new CompareHelper.FieldSpec("TeachingPos1Y", x => x.Units[0].PosY), new CompareHelper.FieldSpec("TeachingPos2X", x => x.Units[1].PosX), new CompareHelper.FieldSpec("TeachingPos2Y", x => x.Units[1].PosY), new CompareHelper.FieldSpec("TeachingPos3X", x => x.Units[2].PosX), new CompareHelper.FieldSpec("TeachingPos3Y", x => x.Units[2].PosY), new CompareHelper.FieldSpec("TeachingPos4X", x => x.Units[3].PosX), new CompareHelper.FieldSpec("TeachingPos4Y", x => x.Units[3].PosY), new CompareHelper.FieldSpec("TeachingPos5X", x => x.Units[4].PosX), new CompareHelper.FieldSpec("TeachingPos5Y", x => x.Units[4].PosY), new CompareHelper.FieldSpec("TeachingPos6X", x => x.Units[5].PosX), new CompareHelper.FieldSpec("TeachingPos6Y", x => x.Units[5].PosY), new CompareHelper.FieldSpec("TeachingPos7X", x => x.Units[6].PosX), new CompareHelper.FieldSpec("TeachingPos7Y", x => x.Units[6].PosY), new CompareHelper.FieldSpec("TeachingPos8X", x => x.Units[7].PosX), new CompareHelper.FieldSpec("TeachingPos8Y", x => x.Units[7].PosY) ); if (diffs.Count > 0) { Log.FuncI($"Recipe Changed"); var recipeInfo = _Manager.Recipes().FirstOrDefault(x => x.Id == id); _Manager.RchHandler().AppendChangedItem(recipeInfo.Name, diffs); foreach (var diff in diffs) Log.V($"{diff.ToString()}"); } _RecipeBeforeChanged = recipe.Clone() as MainRecipe; } } public class EnumToBooleanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || parameter == null) return false; return value.ToString().Equals(parameter.ToString()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if ((bool)value) return Enum.Parse(targetType, parameter.ToString()); return System.Windows.Data.Binding.DoNothing; } } }