终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

267 строки
10 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using BPA.Helper;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Runtime.CompilerServices;
  11. using System.Windows;
  12. using System.IO.Ports;
  13. using System.IO;
  14. using Newtonsoft.Json;
  15. using BPASmartClient.Model;
  16. namespace BPASmartClient.ViewModel
  17. {
  18. public class ShopDeviceConfigViewModel : NotifyBase
  19. {
  20. private DeviceConfigModelJson configModel = new DeviceConfigModelJson();
  21. string FileName => deviceConfig.Count > 0 ? deviceConfig[0].ShopName : string.Empty;
  22. public static List<string> IDevices = new List<string>();
  23. public static List<string> IPeripherals = new List<string>();
  24. public static Action NewShop { get; set; }
  25. public ShopDeviceConfigViewModel()
  26. {
  27. NewShop = null;
  28. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  29. {
  30. if (o != null && o is string[] par)
  31. {
  32. if (par.Length == 2)
  33. {
  34. configModel.ShopName = par[0];
  35. configModel.ShopId = par[1];
  36. deviceConfig.Clear();
  37. deviceConfig.Add(configModel);
  38. }
  39. }
  40. }), "ShopPar");
  41. NewDeviceCommand = new BPARelayCommand<object>(NewDevice);
  42. RemoveDeviceCommand = new BPARelayCommand<object>(RemoveDevice);
  43. NewCommunicationCommand = new BPARelayCommand<object>(NewCommunication);
  44. RemoveCommunicationCommand = new BPARelayCommand<object>(RemoveCommunication);
  45. NewShopCommand = new BPARelayCommand(() => { NewShop?.Invoke(); });
  46. DataListInit();
  47. SaveData = new BPARelayCommand(() =>
  48. {
  49. if (deviceConfig.Count > 0)
  50. {
  51. for (int i = 0; i < deviceConfig.ElementAt(0).deviceModels.Count; i++)
  52. {
  53. string name = deviceConfig.ElementAt(0).deviceModels.ElementAt(i).DeviceModule;
  54. deviceConfig.ElementAt(0).deviceModels.ElementAt(i).DeviceNamespace = IDevices.FirstOrDefault(p => p.Contains(name));
  55. for (int m = 0; m < deviceConfig.ElementAt(0).deviceModels.ElementAt(i).communicationDevcies.Count; m++)
  56. {
  57. string comName = deviceConfig.ElementAt(0).deviceModels.ElementAt(i).communicationDevcies.ElementAt(m).CommunicationModule;
  58. deviceConfig.ElementAt(0).deviceModels.ElementAt(i).communicationDevcies.ElementAt(m).CommunicationNamespace = IPeripherals.FirstOrDefault(p => p.Contains(comName));
  59. }
  60. }
  61. File.WriteAllText($"{LocaPath.GetDeviceConfigPath}{FileName}.json", JsonConvert.SerializeObject(deviceConfig));
  62. }
  63. });
  64. }
  65. #region 右键菜单按钮操作
  66. private void RemoveCommunication(object? obj)
  67. {
  68. if (obj != null && obj is CommunicationModel com)
  69. {
  70. if (com != null)
  71. {
  72. int index = Array.FindIndex(deviceConfig.ElementAt(0).deviceModels.ToArray(), p => p.Id == com.DeviceModelId);
  73. if (index >= 0 && index < deviceConfig.ElementAt(0).deviceModels.Count)
  74. {
  75. var res = deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.FirstOrDefault(p => p.CommunicationName == com.CommunicationName);
  76. if (res != null)
  77. {
  78. deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.Remove(res);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. private void NewCommunication(object? obj)
  85. {
  86. if (obj != null && obj is DeviceModel dm)
  87. {
  88. if (dm != null)
  89. {
  90. string CommunicationName = string.Empty;
  91. int num = 1;
  92. while (true)
  93. {
  94. int index = Array.FindIndex(deviceConfig.ElementAt(0).deviceModels.ToArray(), p => p.DeviceName == dm.DeviceName);
  95. if (index >= 0 && index < deviceConfig.ElementAt(0).deviceModels.Count)
  96. {
  97. var res = deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.FirstOrDefault(p => p.CommunicationName.Contains($"Communication_{num}"));
  98. if (res == null)
  99. {
  100. deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.Add(new CommunicationModel()
  101. {
  102. CommunicationName = $"Communication_{num}",
  103. DeviceModelId = deviceConfig.ElementAt(0).deviceModels.FirstOrDefault(p => p.DeviceName == dm.DeviceName)?.Id,
  104. communicationPar = new CommunicationPar() { IsNetworkPort = false },
  105. });
  106. break;
  107. }
  108. }
  109. else break;
  110. num++;
  111. }
  112. }
  113. }
  114. }
  115. private void RemoveDevice(object? obj)
  116. {
  117. if (obj != null && deviceConfig.Count == 1)
  118. {
  119. string DeviceName = obj?.GetType().GetProperty("DeviceName")?.GetValue(obj, null)?.ToString();
  120. var res = deviceConfig.ElementAt(0).deviceModels.FirstOrDefault(p => p.DeviceName == DeviceName);
  121. if (res != null)
  122. deviceConfig.ElementAt(0).deviceModels.Remove(res);
  123. }
  124. }
  125. private void NewDevice(object? obj)
  126. {
  127. if (obj != null && deviceConfig.Count == 1)
  128. {
  129. string DeviceName = string.Empty;
  130. int num = 1;
  131. while (true)
  132. {
  133. var res = deviceConfig.ElementAt(0).deviceModels.FirstOrDefault(p => p.DeviceName == $"Device_{num}");
  134. if (res == null)
  135. {
  136. deviceConfig.ElementAt(0).deviceModels.Add(new DeviceModel() { DeviceId = "0", DeviceName = $"Device_{num}", Id = Guid.NewGuid().ToString() });
  137. break;
  138. }
  139. num++;
  140. }
  141. }
  142. }
  143. #endregion
  144. #region Command
  145. /// <summary>
  146. /// 新建设备
  147. /// </summary>
  148. public BPARelayCommand<object> NewDeviceCommand { get; set; }
  149. /// <summary>
  150. /// 删除设备
  151. /// </summary>
  152. public BPARelayCommand<object> RemoveDeviceCommand { get; set; }
  153. /// <summary>
  154. /// 新建通讯
  155. /// </summary>
  156. public BPARelayCommand<object> NewCommunicationCommand { get; set; }
  157. /// <summary>
  158. /// 删除通讯
  159. /// </summary>
  160. public BPARelayCommand<object> RemoveCommunicationCommand { get; set; }
  161. public BPARelayCommand SaveData { get; set; }
  162. public BPARelayCommand NewShopCommand { get; set; }
  163. #endregion
  164. #region 列表集合
  165. /// <summary>
  166. /// 设备信息列表
  167. /// </summary>
  168. public static ObservableCollection<DeviceConfigModelJson> deviceConfig { get; set; } = new ObservableCollection<DeviceConfigModelJson>();
  169. /// <summary>
  170. /// 端口号列表
  171. /// </summary>
  172. public static ObservableCollection<string> Ports { get; set; } = new ObservableCollection<string>();
  173. /// <summary>
  174. /// 波特率列表
  175. /// </summary>
  176. public static ObservableCollection<int> BaudRates { get; set; } = new ObservableCollection<int>();
  177. /// <summary>
  178. /// 奇偶校验列表
  179. /// </summary>
  180. public static ObservableCollection<string> Paritys { get; set; } = new ObservableCollection<string>();
  181. /// <summary>
  182. /// 设备模块
  183. /// </summary>
  184. public static ObservableCollection<string> DeviceModels { get; set; } = new ObservableCollection<string>();
  185. /// <summary>
  186. /// 通讯模块
  187. /// </summary>
  188. public static ObservableCollection<string> CommunicationModel { get; set; } = new ObservableCollection<string>();
  189. /// <summary>
  190. /// 店铺集合
  191. /// </summary>
  192. public static ObservableCollection<string> Shops { get; set; } = new ObservableCollection<string>();
  193. private void DataListInit()
  194. {
  195. Ports.Clear();
  196. System.IO.Ports.SerialPort.GetPortNames().ToList().ForEach((item) => { Ports.Add(item); });
  197. BaudRates.Clear();
  198. int[] rb = new int[] { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 43000, 57600, 76800, 115200 };
  199. rb.ToList().ForEach((item) => { BaudRates.Add(item); });
  200. Paritys.Clear();
  201. Enum.GetNames(typeof(Parity)).ToList().ForEach((item) => { Paritys.Add(item); });
  202. DeviceModels.Clear();
  203. IDevices.ForEach((item) =>
  204. {
  205. var strs = item.Split(".");
  206. if (strs != null && strs.Length == 3)
  207. {
  208. DeviceModels.Add(strs[1]);
  209. }
  210. });
  211. CommunicationModel.Clear();
  212. IPeripherals.ForEach((item) =>
  213. {
  214. var strs = item.Split(".");
  215. if (strs != null && strs.Length == 3)
  216. {
  217. CommunicationModel.Add(strs[1]);
  218. }
  219. });
  220. Shops.Clear();
  221. DirectoryInfo directoryInfo = new DirectoryInfo(LocaPath.GetDeviceConfigPath);
  222. var files = directoryInfo.GetFiles();
  223. foreach (var item in files)
  224. {
  225. var res = Path.GetFileNameWithoutExtension(item.FullName);
  226. if (res != null && res.Length > 0 && item.FullName.Contains("json")) Shops.Add(res);
  227. }
  228. }
  229. #endregion
  230. }
  231. }