终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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