using Aurora; using Aurora.Api; using Aurora.Api.Devices; using Aurora.View; using AuroraDeviceCommon.Views; using AuroraPowerMeter; using AuroraUIComponent.Chart; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Threading; namespace AuroraPowerMeter.Gentec { public class UCGentecEoPowerMeterDeviceViewModel : BaseDeviceViewModel { private string _CurrentPowerName; public string CurrentPowerName { get => _CurrentPowerName; set => SetProperty(ref _CurrentPowerName, value); } private string _CurrentPortNumber; public string CurrentPortNumber { get => _CurrentPortNumber; set => SetProperty(ref _CurrentPortNumber, value); } private string _CurrentState; public string CurrentState { get => _CurrentState; set => SetProperty(ref _CurrentState, value); } private double _CurrentPower; public double CurrentPower { get => _CurrentPower; set => SetProperty(ref _CurrentPower, value); } bool _ContinuesMeasure; public bool ContinuesMeasure { get => _ContinuesMeasure; set => SetProperty(ref _ContinuesMeasure, value); } public UCLineChartViewModel PowerMeterChart { get; } public ICommand CMD_Connect { get; } public ICommand CMD_MeasureOneTime { get; } public ICommand CMD_StartMeasurement { get; } public ICommand CMD_StopMeasurement { get; } public ICommand CMD_SetZero { get; } public ICommand CMD_SetWaveLength { get; } public ICommand CMD_MeasurePower { get; } private readonly DispatcherTimer MonitorTimer; public UCGentecEoPowerMeterDeviceViewModel(IAuroraEnvironment aurora, Device powerMeter) : base(aurora, powerMeter) { CMD_Connect = new DelegateCommand(Connect); CMD_StartMeasurement = new DelegateCommand(StartMeasurement); CMD_StopMeasurement = new DelegateCommand(StopMeasurement); CMD_SetZero = new DelegateCommand(SetZero); CMD_SetWaveLength = new DelegateCommand(SetWaveLength); PowerMeterChart = new UCLineChartViewModel(aurora); PowerMeterChart.SetChart("POWER", "", 8, 0); PowerMeterChart.SetChartLine("Power", 6); PowerMeterChart.SetChartLineRange(true, 1, 0, 10); PowerMeterChart.InitChart(); _CurrentState = string.Empty; _CurrentPower = 0.0; _CurrentPowerName = string.Empty; _CurrentPortNumber = string.Empty; _ContinuesMeasure = false; MonitorTimer = new DispatcherTimer(); MonitorTimer.Tick += OnMonitoring; MonitorTimer.Interval = TimeSpan.FromMilliseconds(1000); CurrentPowerName = powerMeter.DeviceId; aurora.EventObservable.AsObservable(PowerMeterEvent.EVENT_ID_MEASURE_RESULT) .Select(msg => (ValueTuple)msg.Param) .Where(msg => msg.Item1 == powerMeter.DeviceId) .Subscribe(power => OnPowerMeasured(power.Item2)); } private void OnPowerMeasured(double _value) { Application.Current.Dispatcher.InvokeAsync(() => PowerMeterChart.AddPoint(_value)); } protected override void VisibleChanged(object obj) { bool visible = (bool)obj; var powerMeter = _Device as PowerMeter; if (visible) { //powerMeter.WaveLengthCorrection(355); MonitorTimer.Start(); } else { ContinuesMeasure = false; MonitorTimer.Stop(); } } private void OnMonitoring(object sender, EventArgs e) { if (!_Device.IsActivated) { CurrentState = "연결안됨"; return; } else { var powerMeter = _Device as PowerMeter; var deviceConfig = Application.Current.AuroraEnv().DeviceManager.GetDeviceConfig(_Device.DeviceId).ConnConfig as SerialConnConfig; CurrentPortNumber = string.Format("{0}", deviceConfig.PortNum); if (ContinuesMeasure) { CurrentState = "측정중"; powerMeter.Measure(out double value); CurrentPower = powerMeter.MeasureResult; PowerMeterChart.AddPoint(CurrentPower); } else { CurrentState = "대기중"; } return; } } private void Connect(object obj) { if (!_Device.IsActivated) { return; } // 연결/해제 메서드 없음 } private void StartMeasurement(object obj) { if (!_Device.IsActivated) { return; } var powerMeter = _Device as PowerMeter; ContinuesMeasure = true; } private void StopMeasurement(object obj) { if (!_Device.IsActivated) { return; } var powerMeter = _Device as PowerMeter; ContinuesMeasure = false; } private void SetZero(object obj) { if (!_Device.IsActivated) { return; } var powerMeter = _Device as PowerMeter; powerMeter.SetZero(); } private void SetWaveLength(object obj) { if (!_Device.IsActivated) { return; } var powerMeter = _Device as PowerMeter; powerMeter.WaveLengthCorrection(355); } } }