2026-01-26 14:51:28 +09:00

137 lines
4.1 KiB
C#

using Aurora;
using Aurora.Api;
using System.Linq;
using System.Reactive.Linq;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Aurora.Api.Manager;
using System.Windows.Threading;
namespace AuroraACS.Views
{
public class ACSIOViewModel : NotifyPropertyChangedBase
{
public ICommand CMD_VisibleChanged { get; }
private List<AddrInfo> bitInputs = new List<AddrInfo>();
public List<AddrInfo> BitInputs { get => bitInputs; set => SetProperty(ref bitInputs, value); }
private List<AddrInfo> bitOutputs = new List<AddrInfo>();
public List<AddrInfo> BitOutputs { get => bitOutputs; set => SetProperty(ref bitOutputs, value); }
private int port;
public int Port { get => port; set => SetProperty(ref port, value); }
private int bit;
public int Bit { get => bit; set => SetProperty(ref bit, value); }
private int val;
public int Value { get => val; set => SetProperty(ref val, value); }
private DelegateCommand cmdGetValue;
private DelegateCommand cmdSetValue;
public ICommand CmdGetValue {
get {
if (cmdGetValue == null) {
cmdGetValue = new DelegateCommand(PerformCmdGetValue);
}
return cmdGetValue;
}
}
public ICommand CmdSetValue {
get {
if (cmdSetValue == null) {
cmdSetValue = new DelegateCommand(PerformCmdSetValue);
}
return cmdSetValue;
}
}
private void PerformCmdGetValue(object commandParameter)
{
Value = _acsIO.GetOutput(Port, Bit);
}
private void PerformCmdSetValue(object commandParameter)
{
if (Port < 0 || Port > 15 || Bit < 0 || Bit > 32)
return;
_acsIO.SetOutput(Port, Bit, Value);
}
private readonly DispatcherTimer _monitorTimer;
private readonly IDeviceManager _DeviceManager;
ACSIO _acsIO;
IAuroraEnvironment _auroraEnv;
public ACSIOViewModel(string deviceId, IAuroraEnvironment aurora)
{
_DeviceManager = aurora.DeviceManager;
_acsIO = _DeviceManager[deviceId] as ACSIO;
_auroraEnv = aurora;
var config = _DeviceManager.GetDeviceConfig(_acsIO.DeviceId) as ACSIOConfig;
BitInputs = config.InputList;
BitOutputs = config.OutputList;
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
_monitorTimer = new DispatcherTimer();
_monitorTimer.Tick += OnMonitoring;
_monitorTimer.Interval = TimeSpan.FromMilliseconds(300);
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 == deviceId
).ObserveOnDispatcher(
).Subscribe(x => DeviceActivated());
}
private void DeviceActivated()
{
if (_acsIO.IsActivated) {
var config = _DeviceManager.GetDeviceConfig(_acsIO.DeviceId) as ACSIOConfig;
BitInputs = config.InputList;
BitOutputs = config.OutputList;
}
}
private void VisibleChanged(object obj)
{
bool visible = (bool)obj;
if (visible) {
_monitorTimer.Start();
} else {
_monitorTimer.Stop();
}
}
private void OnMonitoring(object sender, EventArgs e)
{
if (!_acsIO.IsActivated) {
return;
}
foreach (AddrInfo item in BitInputs) {
item.Value = _acsIO.GetInput(item.Port, item.Bit);
}
foreach (AddrInfo item in BitOutputs) {
item.Value = _acsIO.GetOutput(item.Port, item.Bit);
}
}
}
}