using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using System.Collections.ObjectModel; using Microsoft.Toolkit.Mvvm.Input; using BPASmart.VariableManager.Views; using System.Windows; using BPA.Helper; using BPASmart.Model; using System.IO.Ports; using System.IO; using System.Reflection; namespace BPASmart.VariableManager.ViewModels { public class CommunicationSetViewModel : ObservableObject { public CommunicationSetViewModel() { Json.Read(FileConfigModel.VarConfigPath); communicationDevices = Json.Data.CommunicationDevices; DataListInit(); NewConnectCommand = new RelayCommand(() => { NewConnect(); }); RemoveDeviceCommand = new RelayCommand(RemoveDevice); SaveConnectSetCommand = new RelayCommand(() => { Json.Save(FileConfigModel.VarConfigPath); }); DelegationNotifi.GetInstance.AddDeviceVerify = new Func((p) => { return Json.Data.CommunicationDevices.FirstOrDefault(s => s.DeviceName == p.DeviceName) == null; }); } public ObservableCollection communicationDevices { get; set; } //= new ObservableCollection(); public RelayCommand NewConnectCommand { get; set; } public RelayCommand SaveConnectSetCommand { get; set; } public RelayCommand RemoveDeviceCommand { get; set; } /// /// 创建新连接 /// private void NewConnect() { NewDeviceView deviceManagermentSetView = new NewDeviceView(); var result = deviceManagermentSetView.ShowDialog(); var ResultTag = (DeviceManagermentResult)deviceManagermentSetView.Tag; if (ResultTag != null) { if ((bool)result) { var obj = communicationDevices.FirstOrDefault(p => p.DeviceName == ResultTag.DeviceName); if (obj == null) { CommunicationModel communicationObj = new CommunicationModel(); Type type = Assembly.Load("BPASmart.Model").GetType($"BPASmart.Model.{ResultTag.DeviceType}"); //Type type = Assembly.Load("BPASmart.VariableManager").GetType($"BPASmart.VariableManager.Models.{ResultTag.DeviceType}"); var res = Activator.CreateInstance(type) as ICommunicationDevice; communicationObj.DeviceName = ResultTag.DeviceName; communicationObj.CommDevice = res; communicationObj.ModelName = type.Name; Json.Data.CommunicationDevices.Add(communicationObj); ActionManage.GetInstance.Send("AddCommunicationDevice", ResultTag.DeviceName); } } } deviceManagermentSetView = null; } private void RemoveDevice(object o) { var result = Json.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == o.ToString()); if (result != null) { Json.Data.CommunicationDevices.Remove(result); ActionManage.GetInstance.Send("RemoveCommunicationDevice", result); } } #region 列表集合 /// /// 端口号列表 /// public static ObservableCollection Ports { get; set; } = new ObservableCollection(); /// /// 波特率列表 /// public static ObservableCollection BaudRates { get; set; } = new ObservableCollection(); /// /// 奇偶校验列表 /// public static ObservableCollection Paritys { get; set; } = new ObservableCollection(); /// /// 西门子 PLC 设备类型 /// public ObservableCollection PlcTypes { get; set; } = new ObservableCollection(); private void DataListInit() { Ports.Clear(); System.IO.Ports.SerialPort.GetPortNames().ToList().ForEach((item) => { Ports.Add(item); }); BaudRates.Clear(); int[] rb = new int[] { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 43000, 57600, 76800, 115200 }; rb.ToList().ForEach((item) => { BaudRates.Add(item); }); Paritys.Clear(); Enum.GetNames(typeof(Parity)).ToList().ForEach((item) => { Paritys.Add(item); }); PlcTypes.Clear(); foreach (var item in Enum.GetNames(typeof(ESiemensPlcType))) { PlcTypes.Add(item.Substring(1)); } } #endregion } }