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

191 rader
5.0 KiB

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