终端一体化运控平台
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.
 
 
 

190 rindas
5.1 KiB

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