207 lines
6.9 KiB
C#
207 lines
6.9 KiB
C#
using Aurora;
|
|
using Aurora.Api;
|
|
using Aurora.Api.Devices;
|
|
using Aurora.Api.Manager;
|
|
using Aurora.View;
|
|
using AuroraACS;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reactive.Linq;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
|
|
namespace AuroraACS.Views
|
|
{
|
|
public class DeviceList : NotifyPropertyChangedBase
|
|
{
|
|
public string DeviceId { get; set; }
|
|
public IDeviceConfig Config { get; set; }
|
|
|
|
public Device Motor { get; set; }
|
|
private bool isInit = false;
|
|
public bool IsInit { get => isInit; set => SetProperty(ref isInit, value); }
|
|
}
|
|
|
|
public class UCACSDeviceViewModel : NotifyPropertyChangedBase
|
|
{
|
|
public ICommand CMD_InitAll { get; }
|
|
public ICommand CMD_VisibleChanged { get; }
|
|
public ICommand CMD_Save { get; }
|
|
public ICommand CMD_SaveConfigs { get; }
|
|
|
|
private bool isActivated = false;
|
|
public bool IsActivated {
|
|
get => isActivated;
|
|
set {
|
|
if (isActivated != value) {
|
|
isActivated = value;
|
|
HandleActivationChange(value);
|
|
NotifyPropertyChanged("IsActivated");
|
|
}
|
|
}
|
|
}
|
|
|
|
private UCMotorMovingViewModel aCSMotionViewModel;
|
|
public UCMotorMovingViewModel ACSMotionViewModel { get => aCSMotionViewModel; set => SetProperty(ref aCSMotionViewModel, value); }
|
|
|
|
public IDeviceConfig SelectedDeviceConfig
|
|
=> ACS == null ? null : DeviceManager.GetDeviceConfig(ACS.DeviceId);
|
|
|
|
public List<DeviceList> MotorList { get; set; } = new List<DeviceList>();
|
|
|
|
private readonly IDeviceManager DeviceManager;
|
|
private readonly ACSDevice ACS;
|
|
private readonly DispatcherTimer MonitorTimer;
|
|
private List<string> _MotorIds = new List<string>();
|
|
public UCACSDeviceViewModel(IAuroraEnvironment aurora, string deviceId, List<string> motorIDs)
|
|
{
|
|
motorIDs.ForEach(x => _MotorIds.Add(x));
|
|
|
|
DeviceManager = aurora.DeviceManager;
|
|
ACSMotionViewModel = new UCMotorMovingViewModel(aurora.DeviceManager, aurora.EventObservable
|
|
, deviceId, motorIDs);
|
|
|
|
ACS = aurora.DeviceManager[deviceId] as ACSDevice;
|
|
|
|
isActivated = ACS.IsActivated;
|
|
|
|
MakeMotorList();
|
|
|
|
CMD_InitAll = new DelegateCommand(InitAllAxses);
|
|
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
|
|
CMD_Save = new DelegateCommand(SaveConfigs);
|
|
CMD_SaveConfigs = new DelegateCommand(SaveConfigs);
|
|
|
|
MonitorTimer = new DispatcherTimer();
|
|
MonitorTimer.Tick += OnMonitoring;
|
|
MonitorTimer.Interval = TimeSpan.FromMilliseconds(100);
|
|
|
|
Observable.Merge(
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED),
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_DEACTIVATED)
|
|
).Select(msg => msg.Param as string
|
|
).Where(x => x == ACS?.DeviceId
|
|
).ObserveOnDispatcher(
|
|
).Subscribe(
|
|
x => OnDeviceActiveChanged()
|
|
);
|
|
|
|
Observable.Merge(
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED),
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_DEACTIVATED)
|
|
).Select(msg => msg.Param as string
|
|
).Where(x => x == ACS?.DeviceId
|
|
).ObserveOnDispatcher(
|
|
).Subscribe(
|
|
x => OnDeviceActiveChanged()
|
|
);
|
|
|
|
Observable.Merge(
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED),
|
|
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_DEACTIVATED))
|
|
.Select(msg => msg.Param as string)
|
|
.Where(x => x == ACS?.DeviceId)
|
|
.ObserveOnDispatcher().Subscribe(x => OnDeviceActiveChanged());
|
|
|
|
|
|
}
|
|
|
|
private void OnDeviceActiveChanged()
|
|
{
|
|
IsActivated = ACS.IsActivated;
|
|
if (ACS.IsActivated) {
|
|
// InitAllAxses(null);
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
private void SaveConfigs(object obj)
|
|
{
|
|
MotorList.ForEach(x => DeviceManager.SaveDeviceConfig(x.DeviceId));
|
|
}
|
|
|
|
private void OnMonitoring(object sender, EventArgs e)
|
|
{
|
|
if (!ACS.IsActivated) {
|
|
return;
|
|
}
|
|
#warning 임시. isInit 호출도 하면 안될듯?
|
|
foreach (var motor in MotorList) {
|
|
var acsmotor = motor.Motor as ACSMotor;
|
|
if (!motor.IsInit) {
|
|
//motor.IsInit = acsmotor.IsInitiallized();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void VisibleChanged(object obj)
|
|
{
|
|
bool visible = (bool)obj;
|
|
if (visible) {
|
|
MonitorTimer.Start();
|
|
ACSMotionViewModel.StartMonitoring();
|
|
} else {
|
|
MonitorTimer.Stop();
|
|
ACSMotionViewModel.StopMonitoring();
|
|
}
|
|
}
|
|
|
|
private void InitAllAxses(object obj)
|
|
{
|
|
if (!ACS.IsActivated) {
|
|
return;
|
|
}
|
|
foreach (var motor in MotorList) {
|
|
var acsmotor = motor.Motor as ACSMotor;
|
|
if (!acsmotor.IsInit) {
|
|
acsmotor.Init();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MakeMotorList()
|
|
{
|
|
foreach (var motorId in _MotorIds) {
|
|
MotorList.Add(new DeviceList() {
|
|
Config = DeviceManager.GetDeviceConfig(motorId),
|
|
DeviceId = motorId,
|
|
Motor = DeviceManager[motorId],
|
|
});
|
|
}
|
|
|
|
foreach (var motor in MotorList) {
|
|
var acsmotor = motor.Motor as ACSMotor;
|
|
motor.IsInit = acsmotor.IsInit;
|
|
}
|
|
}
|
|
|
|
private void HandleActivationChange(bool value)
|
|
{
|
|
if (value) {
|
|
int res = 0;
|
|
if (!DeviceManager[ACS.DeviceId].IsActivated) {
|
|
res |= DeviceManager[ACS.DeviceId].Activate();
|
|
}
|
|
foreach (var motorId in _MotorIds) {
|
|
if (!DeviceManager[motorId].IsActivated) {
|
|
res |= DeviceManager[motorId].Activate();
|
|
}
|
|
}
|
|
} else {
|
|
int res = 0;
|
|
foreach (var motorId in _MotorIds) {
|
|
if (DeviceManager[motorId].IsActivated) {
|
|
res |= DeviceManager[motorId].Deactivate();
|
|
}
|
|
}
|
|
if (DeviceManager[ACS.DeviceId].IsActivated) {
|
|
res |= DeviceManager[ACS.DeviceId].Deactivate();
|
|
}
|
|
}
|
|
|
|
isActivated = ACS.IsActivated;
|
|
}
|
|
}
|
|
}
|