|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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<CommunicationPar>.Read(FileConfigModel.VarConfigPath);
- communicationDevices = Json<CommunicationPar>.Data.CommunicationDevices;
- DataListInit();
- NewConnectCommand = new RelayCommand(() => { NewConnect(); });
- RemoveDeviceCommand = new RelayCommand<object>(RemoveDevice);
- SaveConnectSetCommand = new RelayCommand(() => { Json<CommunicationPar>.Save(FileConfigModel.VarConfigPath); });
-
- DelegationNotifi.GetInstance.AddDeviceVerify = new Func<DeviceManagermentResult, bool>((p) =>
- {
- return Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(s => s.DeviceName == p.DeviceName) == null;
- });
- }
-
- public ObservableCollection<CommunicationModel> communicationDevices { get; set; } //= new ObservableCollection<CommunicationModel>();
-
- public RelayCommand NewConnectCommand { get; set; }
-
- public RelayCommand SaveConnectSetCommand { get; set; }
-
- public RelayCommand<object> RemoveDeviceCommand { get; set; }
-
- /// <summary>
- /// 创建新连接
- /// </summary>
- 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<CommunicationPar>.Data.CommunicationDevices.Add(communicationObj);
- ActionManage.GetInstance.Send("AddCommunicationDevice", ResultTag.DeviceName);
- }
- }
- }
- deviceManagermentSetView = null;
- }
-
- private void RemoveDevice(object o)
- {
- var result = Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == o.ToString());
- if (result != null)
- {
- Json<CommunicationPar>.Data.CommunicationDevices.Remove(result);
- ActionManage.GetInstance.Send("RemoveCommunicationDevice", result);
- }
- }
-
- #region 列表集合
-
- /// <summary>
- /// 端口号列表
- /// </summary>
- public static ObservableCollection<string> Ports { get; set; } = new ObservableCollection<string>();
-
- /// <summary>
- /// 波特率列表
- /// </summary>
- public static ObservableCollection<int> BaudRates { get; set; } = new ObservableCollection<int>();
-
- /// <summary>
- /// 奇偶校验列表
- /// </summary>
- public static ObservableCollection<string> Paritys { get; set; } = new ObservableCollection<string>();
-
- /// <summary>
- /// 西门子 PLC 设备类型
- /// </summary>
- public ObservableCollection<string> PlcTypes { get; set; } = new ObservableCollection<string>();
-
- 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
-
- }
- }
|