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.

MainViewModel.cs 5.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using DataVAPI.Model;
  2. using DataVAPI.Tool.API请求;
  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. namespace DataVIOT.Help.ViewModel
  10. {
  11. public class MainViewModel : ObservableObject
  12. {
  13. #region 公有变量
  14. /// <summary>
  15. /// 设备表
  16. /// </summary>
  17. public ObservableCollection<DeviceTable> deviceTable { get; set; }
  18. /// <summary>
  19. /// 选中行
  20. /// </summary>
  21. private DeviceTable _deviceTableSelectedItem;
  22. public DeviceTable deviceTableSelectedItem
  23. {
  24. get { return _deviceTableSelectedItem; }
  25. set
  26. {
  27. if (_deviceTableSelectedItem == value)
  28. return;
  29. _deviceTableSelectedItem = value;
  30. OnPropertyChanged("deviceTableSelectedItem");
  31. }
  32. }
  33. /// <summary>
  34. /// 当前设备
  35. /// </summary>
  36. private DeviceTable _device;
  37. public DeviceTable device
  38. {
  39. get { return _device; }
  40. set
  41. {
  42. if (_device == value)
  43. return;
  44. _device = value;
  45. OnPropertyChanged("device");
  46. }
  47. }
  48. /// <summary>
  49. /// API 地址
  50. /// </summary>
  51. public string ApiURL = "https://bpa.black-pa.com:21527/datav";
  52. #endregion
  53. #region 不需要监听
  54. private static MainViewModel _instance;
  55. public static MainViewModel Instance
  56. {
  57. get
  58. {
  59. if (_instance == null)
  60. _instance = new MainViewModel();
  61. return _instance;
  62. }
  63. }
  64. public MainViewModel()
  65. {
  66. deviceTable = new ObservableCollection<DeviceTable>();
  67. device = new DeviceTable();
  68. deviceTableSelectedItem = new DeviceTable();
  69. //Refresh();
  70. }
  71. #endregion
  72. #region 公有函数
  73. /// <summary>
  74. /// 刷新
  75. /// </summary>
  76. public void Refresh()
  77. {
  78. deviceTable.Clear();
  79. Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d =>
  80. {
  81. deviceTable.Add(d);
  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. }