终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ShopDeviceConfigViewModel.cs 10 KiB

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