终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataVViewModel.cs 5.0 KiB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 
  2. using BPASmartClient.Business;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.IoT;
  5. using DataVAPI.Tool.IOT;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace BPASmartClient.ViewModel
  14. {
  15. public class DataVViewModel : ObservableObject
  16. {
  17. #region 单一变量
  18. private volatile static DataVViewModel _Instance;
  19. public static DataVViewModel GetInstance() => _Instance ?? (_Instance = new DataVViewModel());
  20. private DataVViewModel()
  21. {
  22. deviceTable = new ObservableCollection<DeviceTable>();
  23. device = new DeviceTable();
  24. deviceTableSelectedItem = new DeviceTable();
  25. ApiURL = InternetInfo.DataVApiAddress;
  26. Refresh();
  27. }
  28. #endregion
  29. #region 公有变量
  30. /// <summary>
  31. /// 设备表
  32. /// </summary>
  33. public ObservableCollection<DeviceTable> deviceTable { get; set; }
  34. /// <summary>
  35. /// 选中行
  36. /// </summary>
  37. private DeviceTable _deviceTableSelectedItem;
  38. public DeviceTable deviceTableSelectedItem
  39. {
  40. get { return _deviceTableSelectedItem; }
  41. set
  42. {
  43. if (_deviceTableSelectedItem == value)
  44. return;
  45. _deviceTableSelectedItem = value;
  46. OnPropertyChanged("deviceTableSelectedItem");
  47. }
  48. }
  49. /// <summary>
  50. /// 当前设备
  51. /// </summary>
  52. private DeviceTable _device;
  53. public DeviceTable device
  54. {
  55. get { return _device; }
  56. set
  57. {
  58. if (_device == value)
  59. return;
  60. _device = value;
  61. OnPropertyChanged("device");
  62. }
  63. }
  64. /// <summary>
  65. /// API 地址
  66. /// </summary>
  67. public string ApiURL { get; set; }
  68. #endregion
  69. #region 公有函数
  70. /// <summary>
  71. /// 刷新
  72. /// </summary>
  73. public void Refresh()
  74. {
  75. deviceTable.Clear();
  76. Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d =>
  77. {
  78. if (int.Parse(d.ClientId) > 0)
  79. {
  80. deviceTable.Add(d);
  81. }
  82. });
  83. }
  84. /// <summary>
  85. /// 根据客户端iD查询
  86. /// </summary>
  87. /// <param name="clientId"></param>
  88. public List<DeviceTable> Inquire(string clientId = "")
  89. {
  90. try
  91. {
  92. string url = ApiURL + "/api/Device/Query";
  93. if (!string.IsNullOrEmpty(clientId)) url = ApiURL + "/api/Device/Query?clientId=" + clientId;
  94. string json = HttpRequestHelper.HttpGetRequest(url);
  95. JsonMsg<List<DeviceTable>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<DeviceTable>>>(json);
  96. return jsonMsg.obj.data;
  97. }
  98. catch (Exception ex)
  99. {
  100. return null;
  101. }
  102. }
  103. /// <summary>
  104. /// 修改
  105. /// </summary>
  106. /// <returns></returns>
  107. public bool Update()
  108. {
  109. try
  110. {
  111. string url = ApiURL + "/api/Device/Modify";
  112. string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device));
  113. Refresh();
  114. return true;
  115. }
  116. catch (Exception ex)
  117. {
  118. return false;
  119. }
  120. }
  121. /// <summary>
  122. /// 增加
  123. /// </summary>
  124. /// <returns></returns>
  125. public bool Add()
  126. {
  127. try
  128. {
  129. string url = ApiURL + "/api/Device/Create";
  130. string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device));
  131. Refresh();
  132. return true;
  133. }
  134. catch (Exception ex)
  135. {
  136. return false;
  137. }
  138. }
  139. public bool Add(DeviceTable deviceTable)
  140. {
  141. try
  142. {
  143. string url = ApiURL + "/api/Device/Create";
  144. string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(deviceTable));
  145. Refresh();
  146. return true;
  147. }
  148. catch (Exception ex)
  149. {
  150. return false;
  151. }
  152. }
  153. /// <summary>
  154. /// 删除
  155. /// </summary>
  156. /// <returns></returns>
  157. public bool Delete()
  158. {
  159. try
  160. {
  161. string url = ApiURL + "/api/Device/DeleteDate?id=" + device.Id;
  162. string json = HttpRequestHelper.HttpGetRequest(url);
  163. Refresh();
  164. return true;
  165. }
  166. catch (Exception ex)
  167. {
  168. return false;
  169. }
  170. }
  171. #endregion
  172. }
  173. }