终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataVViewModel.cs 4.9 KiB

2年前
1年前
1年前
2年前
1年前
1年前
1年前
1年前
1年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 
  2. using BPASmartClient.Business;
  3. using BPA.Helper;
  4. using BPASmartClient.IoT;
  5. using DataVAPI.Tool.IOT;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  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. }