using Aurora; using Aurora.View; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using static AuroraLaser.Trumpf.TruPulseNano.LaserDeviceTruPulseNano; using System.Windows.Input; namespace AuroraLaser.Trumpf.TruPulseNano.Views { public class UCTruPulseNanoViewModel : NotifyPropertyChangedBase { IAuroraEnvironment _env; private LaserDeviceTruPulseNano _trupulsenano; private string _sent; public string Sent { get => _sent; set => SetProperty(ref _sent, value); } private string _received; public string Received { get => _received; set => SetProperty(ref _received, value); } public PacketInfo LastPacket { get; set; } public ObservableCollection CommandList { get; } public ObservableCollection Arguments { get; } = new ObservableCollection(); public ICommand CMD_SendCommand { get; set; } private TrupulseNanoCommand _selectedCommand; public TrupulseNanoCommand SelectedCommand { get => _selectedCommand; set { if (SetProperty(ref _selectedCommand, value)) { UpdateArguments(); } } } public UCTruPulseNanoViewModel(IAuroraEnvironment env, string deviceId) { _env = env; _trupulsenano = _env.DeviceManager[deviceId] as LaserDeviceTruPulseNano; LastPacket = new PacketInfo(); CMD_SendCommand = new DelegateCommand(SendCommand); CommandList = new ObservableCollection( Enum.GetValues(typeof(TrupulseNanoCommand)).Cast()); // 초기 선택값 및 Arguments 세팅 if (CommandList.Count > 0) SelectedCommand = CommandList[0]; } private void UpdateArguments() { Arguments.Clear(); if (CommandArguments.TryGetValue(SelectedCommand, out var argInfo)) { int argCount = argInfo.ArgumentCount; string[] descs = null; CommandArgumentDescriptions.TryGetValue(SelectedCommand, out descs); for (int i = 0; i < argCount; i++) { string desc = (descs != null && i < descs.Length) ? descs[i] : $"Argument {i + 1}"; Arguments.Add(new ArgumentInfo(desc)); } } } private async void SendCommand(object obj) { var argValues = Arguments.Select(a => a.Value).ToArray(); var bytearray = _trupulsenano.CreateCommandPacket(SelectedCommand, argValues); Sent = string.Join(" ", bytearray.Select(x => $"0x{x:X2}")); var response = await _trupulsenano.SendandReceiveAsync(bytearray); if (response != null) { Received = string.Join(" ", response.Select(x => $"0x{x:X2}")); ParseResponsePacket(response); // 응답 파싱 메서드 호출 } else { Received = "No Response"; LastPacket.Arguments.Clear(); LastPacket.Command = null; } } private void ParseResponsePacket(byte[] response) { try { var (commandNumber, arguments, error) = _trupulsenano.ParseResponse(response); if (error != null) { LastPacket.Command = $" Error: {error}"; LastPacket.Arguments.Clear(); } else if (arguments != null && arguments.Length > 0) { LastPacket.Arguments.Clear(); LastPacket.Command = Enum.GetName(typeof(TrupulseNanoCommand), commandNumber); // 인덱스를 포함한 Arguments 추가 int index = 1; foreach (var arg in _trupulsenano.ParseArguments((TrupulseNanoCommand)commandNumber, arguments)) { LastPacket.Arguments.Add(new IndexedArgument { Index = index++, Value = arg }); } } else { LastPacket.Command = $"[Command: 0x{commandNumber:X2}] OK (No arguments)"; LastPacket.Arguments.Clear(); } } catch (Exception ex) { LastPacket.Command = $"[Parse Error] {ex.Message}"; LastPacket.Arguments.Clear(); } } //For Send Argument public class ArgumentInfo : NotifyPropertyChangedBase { private string _value; public string Value { get => _value; set => SetProperty(ref _value, value); } public string Description { get; set; } public ArgumentInfo(string description) { Description = description; _value = ""; } } //For Received Argument public class PacketInfo : NotifyPropertyChangedBase { private string command; public string Command { get => command; set => SetProperty(ref command, value); } // IndexedArgument로 변경 public ObservableCollection Arguments { get; set; } = new ObservableCollection(); } // IndexedArgument 클래스 추가 public class IndexedArgument : NotifyPropertyChangedBase { public int Index { get; set; } // 행 인덱스 public object Value { get; set; } // 기존 Argument 값 } } }