using Aurora; using Aurora.Api; using Aurora.Api.Manager; using Aurora.View; using AuroraLaserScanner; using AuroraMassFlowMeter; using ILSOS_PrismaticGun.Device; using ILSOS_PrismaticGun.Process; using ILSOS_PrismaticGun.Recipes; using ILSOS_PrismaticGun.Tools; using ILSOS_PrismaticGun.Views.Main; using System; using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; using WeldingCommon; using WeldingCommon.Alarm; using WeldingCommon.Data; using WeldingCommon.Settings; using WeldingCommon.Utils; namespace ILSOS_PrismaticGun.Views { public class UCMainAutoRunViewModel : NotifyPropertyChangedBase { private readonly Logger Log = new Logger("UCMainAutoRunViewModel"); public ICommand CMD_VisibleChanged { get; } public ICommand CMD_RunSequenceAutoRun { get; } public ICommand CMD_StopSequenceAutoRun { get; } public ICommand CMD_ResetTotal { get; } public ICommand CMD_ResetToday { get; } private readonly IAuroraEnvironment _Aurora; private readonly IRecipeManager _RecipeManager; private readonly ISettingEntry _SettingEntry; private readonly IAlarmService _AlarmService; private readonly SystemParameter _SystemParameter; private readonly PLCControllerPrismaticGun _Plc; private MassFlowMeter _MFCstageA, _MFCstageB; private ISequence _SeqAutoRunStageA; private ISequence _SeqAutoRunStageB; public bool IsSequenceRunning_AutoRun { get => _IsSequenceRunning_AutoRun; set => SetProperty(ref _IsSequenceRunning_AutoRun, value); } private bool _IsSequenceRunning_AutoRun; private bool _IsAlarm; public bool IsAlarm { get => _IsAlarm; set => SetProperty(ref _IsAlarm, value); } public DateTime NowDateTime { get => _NowDateTime; set => SetProperty(ref _NowDateTime, value); } private DateTime _NowDateTime = DateTime.Now; public PalletInfo PalletInfoStageA { get; } public PalletInfo PalletInfoStageB { get; } public MainRecipe ActivatedRecipeStageA { get; set; } public MainRecipe ActivatedRecipeStageB { get; set; } public UCWorkStateViewModel UCStageAWorkState { get; } public UCWorkStateViewModel UCStageBWorkState { get; } //public UCCommonAutoRunViewModel CommonAutoRunViewModel { get; } private readonly UserControl _UCRecipe; private readonly DispatcherTimer _MonitorTimer; public ReadOnlyObservableCollection HistoryA { get; } public ReadOnlyObservableCollection HistoryB { get; } public double MFCCurrentStageA { get => _MFCCurrentStageA; set => SetProperty(ref _MFCCurrentStageA, value); } private double _MFCCurrentStageA = 0.0f; public double MFCCurrentStageB { get => _MFCCurrentStageB; set => SetProperty(ref _MFCCurrentStageB, value); } private double _MFCCurrentStageB = 0.0f; #region Canvas Items public PLCIO PLCIOStageA { get => _PLCIOStageA; set => _PLCIOStageA = value; } private PLCIO _PLCIOStageA = new PLCIO(); public PLCIO PLCIOStageB { get => _PLCIOStageB; set => _PLCIOStageB = value; } private PLCIO _PLCIOStageB = new PLCIO(); #endregion public UCMainAutoRunViewModel(IAuroraEnvironment aurora, string sequenceId) { _Aurora = aurora; _RecipeManager = _Aurora.RecipeManager; _SettingEntry = _Aurora.SettingEntry; _AlarmService = _Aurora.AlarmService; _SystemParameter = aurora.SettingEntry[CommonSettingIds.SystemParameter] as SystemParameter; _Plc = _Aurora.DeviceManager[CommonDeviceIds.PLCDevice] as PLCControllerPrismaticGun; CMD_VisibleChanged = new DelegateCommand(VisibleChanged); CMD_RunSequenceAutoRun = new DelegateCommand(PerformCmdRunSequenceAutoRun); CMD_StopSequenceAutoRun = new DelegateCommand(PerformCmdStopSequenceAutoRun); CMD_ResetTotal = new DelegateCommand(PerformCmdResetTotal); CMD_ResetToday = new DelegateCommand(PerformCmdResetToday); _MonitorTimer = new DispatcherTimer(); _MonitorTimer.Tick += OnMonitoring; _MonitorTimer.Interval = TimeSpan.FromMilliseconds(100); _Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_RECIPE_ACTIVATED) .Select(x => (RecipeInfo)x.Param) .Subscribe(x => OnActivatedRecipeChange(x)); _Aurora.EventObservable.AsObservable(CommonEventIds.EVENT_EQUIPMENT_STATUS_CHANGE) .Subscribe(x => OnChangedEquipmentStatus()); Observable.Merge( _Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_ALARM_SET_ALARM), _Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_ALARM_RESET_ALARM), _Aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_ALARM_RESET_ALL_ALARM)) .Subscribe(x => IsAlarm = AlarmManager.HasAlarmLevelHigh()); _SeqAutoRunStageA = _Aurora.SequenceEntry[SequenceIds.SeqAutoRunPrismaticGunStageA] as ISequence; _SeqAutoRunStageB = _Aurora.SequenceEntry[SequenceIds.SeqAutoRunPrismaticGunStageB] as ISequence; ActivatedRecipeStageA = _RecipeManager.ActivatedRecipeItem(RecipeIds.StageAMainRecipe) as MainRecipe; ActivatedRecipeStageB = _RecipeManager.ActivatedRecipeItem(RecipeIds.StageBMainRecipe) as MainRecipe; PalletInfoStageA = ProcessManager.Instance.PalletInStageA; PalletInfoStageB = ProcessManager.Instance.PalletInStageB; UCStageAWorkState = new UCWorkStateViewModel(aurora, eStage.A); UCStageBWorkState = new UCWorkStateViewModel(aurora, eStage.B); HistoryA = ProcessManager.Instance.HistoryReader.HistoryA; HistoryB = ProcessManager.Instance.HistoryReader.HistoryB; _MFCstageA = aurora.DeviceManager[DeviceIds.MassFlowMeterStageA] as MassFlowMeter; _MFCstageB = aurora.DeviceManager[DeviceIds.MassFlowMeterStageB] as MassFlowMeter; } private object OnChangedEquipmentStatus() { IsSequenceRunning_AutoRun = Status.EquipmentStatus == eEqpStatus.Auto; _Aurora.ViewManager.ChangeMenu(CommonViewIds.AutoRun); return null; } private object OnActivatedRecipeChange(RecipeInfo x) { ActivatedRecipeStageA = _RecipeManager.ActivatedRecipeItem(RecipeIds.StageAMainRecipe) as MainRecipe; ActivatedRecipeStageB = _RecipeManager.ActivatedRecipeItem(RecipeIds.StageBMainRecipe) as MainRecipe; return null; } private void VisibleChanged(object obj) { bool visible = (bool)obj; if (visible) { _MonitorTimer.Start(); } else { _MonitorTimer.Stop(); } } private void OnMonitoring(object sender, EventArgs e) { if(PalletInfoStageA.weldingData.WeldingStartTime.Day != DateTime.Now.Day) PalletInfoStageA.weldingData.TodayProduct = 0; if (PalletInfoStageB.weldingData.WeldingStartTime.Day != DateTime.Now.Day) PalletInfoStageB.weldingData.TodayProduct = 0; NowDateTime = DateTime.Now; PLCIOStageA.LoadRequest = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_LOAD_REQUEST).data; PLCIOStageA.LoadRequestAck = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_LOAD_REQUEST_ACK).data; PLCIOStageA.LoadComp = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_LOAD_COMPLETE).data; PLCIOStageA.LoadCompAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_LOAD_COMPLETE_ACK).data; PLCIOStageA.UnloadRequest = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_UNLOAD_REQUEST).data; PLCIOStageA.UnloadRequestAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_UNLOAD_REQUEST_ACK).data; PLCIOStageA.UnloadComp = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_UNLOAD_COMPLETE).data; PLCIOStageA.UnloadCompAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_UNLOAD_COMPLETE_ACK).data; PLCIOStageA.LoadEnableFromPLC = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_LOAD_ENABLE).data; PLCIOStageA.UnloadEnableFromPLC = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_A_UNLOAD_ENABLE).data; PLCIOStageA.LoadEnableFromPC = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_LOAD_ENABLE).data; PLCIOStageA.UnloadEnableFromPC = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_A_UNLOAD_ENABLE).data; PLCIOStageB.LoadRequest = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_LOAD_REQUEST).data; PLCIOStageB.LoadRequestAck = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_LOAD_REQUEST_ACK).data; PLCIOStageB.LoadComp = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_LOAD_COMPLETE).data; PLCIOStageB.LoadCompAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_LOAD_COMPLETE_ACK).data; PLCIOStageB.UnloadRequest = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_UNLOAD_REQUEST).data; PLCIOStageB.UnloadRequestAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_UNLOAD_REQUEST_ACK).data; PLCIOStageB.UnloadComp = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_UNLOAD_COMPLETE).data; PLCIOStageB.UnloadCompAck = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_UNLOAD_COMPLETE_ACK).data; PLCIOStageB.LoadEnableFromPLC = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_LOAD_ENABLE).data; PLCIOStageB.UnloadEnableFromPLC = _Plc.ReadBit(PLCControllerPrismaticGun.eBitInput.PLC_STAGE_B_UNLOAD_ENABLE).data; PLCIOStageB.LoadEnableFromPC = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_LOAD_ENABLE).data; PLCIOStageB.UnloadEnableFromPC = _Plc.ReadOutBit(PLCControllerPrismaticGun.eBitOutput.PC_STAGE_B_UNLOAD_ENABLE).data; MFCCurrentStageA = _MFCstageA.CurrentFlow.ToLPM(); MFCCurrentStageB = _MFCstageB.CurrentFlow.ToLPM(); } private void PerformCmdRunSequenceAutoRun(object obj) { if (_SeqAutoRunStageA.RunState == SequenceRunState.Running || _SeqAutoRunStageB.RunState == SequenceRunState.Running) { MessageUtil.ShowWarningToast(string.Format("이미 시퀀스가 Start 상태입니다.", "SeqAutoRunPrismaticGun")); return; } ProcessManager.Instance.AutoRunStart(); } private void PerformCmdStopSequenceAutoRun(object obj) { if (_SeqAutoRunStageA.RunState == SequenceRunState.Idle && _SeqAutoRunStageB.RunState == SequenceRunState.Idle) { return; } ProcessManager.Instance.AutoStart(false); } private void PerformCmdResetTotal(object commandParameter) { if (commandParameter is string enumName && Enum.TryParse(enumName, out eStage stage)) { if (stage is eStage.A) { PalletInfoStageA.weldingData.TotalProduct = 0; } else { PalletInfoStageB.weldingData.TotalProduct = 0; } } } private void PerformCmdResetToday(object commandParameter) { if (commandParameter is string enumName && Enum.TryParse(enumName, out eStage stage)) { if (stage is eStage.A) { PalletInfoStageA.weldingData.TodayProduct = 0; } else { PalletInfoStageB.weldingData.TodayProduct = 0; } } } private void AutoRunSeq_RunStateChanged(object sender, (string sequenceId, SequenceRunState state) e) { //AutoRun 상황에서 Recipe Save Lock //_RecipeManager.SaveLock( // CommonRecipeIds.ScannerObjectTeaching, // e.state != SequenceRunState.Idle); ////AutoRun 상황에서 Reicpe 화면 수정할 수 없도록 함 //if (e.state == SequenceRunState.Running) { // Application.Current.Dispatcher.InvokeAsync(() => { // _UCRecipe.IsEnabled = false; // }); //} else { // Application.Current.Dispatcher.InvokeAsync(() => { // _UCRecipe.IsEnabled = true; // }); //} } } public class PLCIO : NotifyPropertyChangedBase { public bool LoadRequest { get => _LoadRequest; set => SetProperty(ref _LoadRequest, value); } private bool _LoadRequest; public bool LoadRequestAck { get => _LoadRequestAck; set => SetProperty(ref _LoadRequestAck, value); } private bool _LoadRequestAck; public bool LoadComp { get => _LoadComp; set => SetProperty(ref _LoadComp, value); } private bool _LoadComp; public bool LoadCompAck { get => _LoadCompAck; set => SetProperty(ref _LoadCompAck, value); } private bool _LoadCompAck; public bool LoadEnableFromPLC{ get => _LoadEnableFromPLC; set => SetProperty(ref _LoadEnableFromPLC, value); } private bool _LoadEnableFromPLC; public bool LoadEnableFromPC { get => _LoadEnableFromPC; set => SetProperty(ref _LoadEnableFromPC, value); } private bool _LoadEnableFromPC; public bool UnloadRequest { get => _UnloadRequest; set => SetProperty(ref _UnloadRequest, value); } private bool _UnloadRequest; public bool UnloadRequestAck { get => _UnloadRequestAck; set => SetProperty(ref _UnloadRequestAck, value); } private bool _UnloadRequestAck; public bool UnloadComp { get => _UnloadComp; set => SetProperty(ref _UnloadComp, value); } private bool _UnloadComp; public bool UnloadCompAck { get => _UnloadCompAck; set => SetProperty(ref _UnloadCompAck, value); } private bool _UnloadCompAck; public bool UnloadEnableFromPLC { get => _UnloadEnableFromPLC; set => SetProperty(ref _UnloadEnableFromPLC, value); } private bool _UnloadEnableFromPLC; public bool UnloadEnableFromPC { get => _UnloadEnableFromPC; set => SetProperty(ref _UnloadEnableFromPC, value); } private bool _UnloadEnableFromPC; } }