2026-03-30 17:09:29 +09:00

163 lines
5.8 KiB
C#

using Aurora;
using Aurora.Api.Devices;
using Aurora.Api.Manager;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace WeldingCommon.Views
{
class UCSingleAxisHomeViewModel : NotifyPropertyChangedBase
{
#region Binding Properties
private List<string> _MotorIDList;
public List<string> MotorIDList { get => _MotorIDList; set => SetProperty(ref _MotorIDList, value); }
private string _SelectedMotorID;
public string SelectedMotorID { get => _SelectedMotorID; set => SetProperty(ref _SelectedMotorID, value); }
// TODO: 아래 strMotorServoOnOffStatus 와 하나로 합치자. 이 부분은 background 색상 변경에 사용됨
private bool _bMotorServoOnOffStatus;
public bool bMotorServoOnOffStatus { get => _bMotorServoOnOffStatus; set => SetProperty(ref _bMotorServoOnOffStatus, value); }
private string _strMotorServoOnOffStatus;
public string strMotorServoOnOffStatus { get => _strMotorServoOnOffStatus; set => SetProperty(ref _strMotorServoOnOffStatus, value); }
private int _ProgressStatus;
public int ProgressStatus { get => _ProgressStatus; set => SetProperty(ref _ProgressStatus, value); }
#endregion
#region EventCommands
public ICommand CMD_VisibleChanged { get; }
public ICommand CMD_IncreaseComboMotorID { get; }
public ICommand CMD_DecreaseComboMotorID { get; }
public ICommand CMD_Home { get; }
public ICommand CMD_Stop { get; }
#endregion
private readonly IDeviceManager _DeviceManager;
private readonly DispatcherTimer _MonitorTimer;
internal UCSingleAxisHomeViewModel(IAuroraEnvironment aurora)
{
_DeviceManager = aurora.DeviceManager;
#region Initialize Binding Properties
// Motor ID 를 List 에 넣기. devicemanager 에서 motor ID 를 받아오자.
// motor 가 define 되지 않았으니 테스트를 위해 임시로 몇개 넣어둔다.
MotorIDList = new List<string> { "Welding Y1", "Welding Z" };
SelectedMotorID = MotorIDList[0];
#endregion
#region DelegateCommands
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
CMD_IncreaseComboMotorID = new DelegateCommand(PerformCmdIncreaseComboMotorID);
CMD_DecreaseComboMotorID = new DelegateCommand(PerformCmdDecreaseComboMotorID);
CMD_Home = new DelegateCommand(PerformCmdMotorHome);
CMD_Stop = new DelegateCommand(PerformCmdMotorStop);
#endregion
_MonitorTimer = new DispatcherTimer();
_MonitorTimer.Tick += OnMonitoring;
_MonitorTimer.Interval = TimeSpan.FromMilliseconds(100);
}
private void PerformCmdIncreaseComboMotorID(object obj)
{
try {
int nIndex = MotorIDList.IndexOf(SelectedMotorID);
if (nIndex >= MotorIDList.Count - 1) {
nIndex = MotorIDList.Count;
} else {
nIndex++;
}
SelectedMotorID = MotorIDList[nIndex];
} catch {
}
}
private void PerformCmdDecreaseComboMotorID(object obj)
{
try {
int nIndex = MotorIDList.IndexOf(SelectedMotorID);
if (nIndex < 1) {
nIndex = 0;
} else {
nIndex--;
}
SelectedMotorID = MotorIDList[nIndex];
} catch {
}
}
private void VisibleChanged(object obj)
{
bool visible = (bool)obj;
if (visible) {
_MonitorTimer.Start();
} else {
_MonitorTimer.Stop();
}
}
private BaseMotor GetComboSelectedBaseMotor()
{
return _DeviceManager[SelectedMotorID] as BaseMotor;
}
private void OnMonitoring(object sender, EventArgs e)
{
try {
// Status textbox back ground color update
var motor = GetComboSelectedBaseMotor();
if (motor != null) {
bMotorServoOnOffStatus = motor.IsServoOn();
if (motor.IsHomeCompleted()) {
ProgressStatus = 100;
} else {
//nProgressStatus = motor.GetHomeRate(axisindex , ref rate); // 이런 함수가 있다고 하네?
ProgressStatus = 0;
}
} else {
bMotorServoOnOffStatus = false;
ProgressStatus = 0;
}
if (bMotorServoOnOffStatus) {
strMotorServoOnOffStatus = "Servo On";
} else {
strMotorServoOnOffStatus = "Servo Off";
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void PerformCmdMotorHome(object obj)
{
try {
var motor = GetComboSelectedBaseMotor();
if (motor == null) {
return;
}
motor.Home();
// test 임시
ProgressStatus = 0;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void PerformCmdMotorStop(object obj)
{
try {
var motor = GetComboSelectedBaseMotor();
if (motor == null) return;
motor.Stop();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}