|
- 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 BPASmartClient.Helper;
- using BPASmart.Model;
- //using BPASmartClient.Model;
- using System.IO.Ports;
- using System.IO;
- using System.Reflection;
-
- namespace BPASmart.VariableManager.ViewModels
- {
- public class CommunicationSetViewModel : ObservableObject
- {
- public CommunicationSetViewModel()
- {
- Json<CommunicationPar>.Read();
- communicationDevices = Json<CommunicationPar>.Data.CommunicationDevices;
- DataListInit();
- NewConnectCommand = new RelayCommand(() => { NewConnect(); });
- RemoveDeviceCommand = new RelayCommand<object>(RemoveDevice);
- SaveConnectSetCommand = new RelayCommand(() => { Json<CommunicationPar>.Save(); });
-
- 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
-
- }
- }
|