using BPASmartClient.Helper; using BPASmartClient.IoT; using DataVAPI.Tool.IOT; using Microsoft.Toolkit.Mvvm.ComponentModel; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.ViewModel { public class DataVViewModel : ObservableObject { #region 单一变量 private volatile static DataVViewModel _Instance; public static DataVViewModel GetInstance() => _Instance ?? (_Instance = new DataVViewModel()); private DataVViewModel() { deviceTable = new ObservableCollection(); device = new DeviceTable(); deviceTableSelectedItem = new DeviceTable(); ApiURL = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString(); Refresh(); } #endregion #region 公有变量 /// /// 设备表 /// public ObservableCollection deviceTable { get; set; } /// /// 选中行 /// private DeviceTable _deviceTableSelectedItem; public DeviceTable deviceTableSelectedItem { get { return _deviceTableSelectedItem; } set { if (_deviceTableSelectedItem == value) return; _deviceTableSelectedItem = value; OnPropertyChanged("deviceTableSelectedItem"); } } /// /// 当前设备 /// private DeviceTable _device; public DeviceTable device { get { return _device; } set { if (_device == value) return; _device = value; OnPropertyChanged("device"); } } /// /// API 地址 /// public string ApiURL { get; set; } #endregion #region 公有函数 /// /// 刷新 /// public void Refresh() { deviceTable.Clear(); Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d => { if (int.Parse(d.ClientId) > 0) { deviceTable.Add(d); } }); } /// /// 根据客户端iD查询 /// /// public List Inquire(string clientId = "") { try { string url = ApiURL + "/api/Device/Query"; if (!string.IsNullOrEmpty(clientId)) url = ApiURL + "/api/Device/Query?clientId=" + clientId; string json = HttpRequestHelper.HttpGetRequest(url); JsonMsg> jsonMsg = Tools.JsonToObjectTools>>(json); return jsonMsg.obj.data; } catch (Exception ex) { return null; } } /// /// 修改 /// /// public bool Update() { try { string url = ApiURL + "/api/Device/Modify"; string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device)); Refresh(); return true; } catch (Exception ex) { return false; } } /// /// 增加 /// /// public bool Add() { try { string url = ApiURL + "/api/Device/Create"; string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device)); Refresh(); return true; } catch (Exception ex) { return false; } } public bool Add(DeviceTable deviceTable) { try { string url = ApiURL + "/api/Device/Create"; string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(deviceTable)); Refresh(); return true; } catch (Exception ex) { return false; } } /// /// 删除 /// /// public bool Delete() { try { string url = ApiURL + "/api/Device/DeleteDate?id=" + device.Id; string json = HttpRequestHelper.HttpGetRequest(url); Refresh(); return true; } catch (Exception ex) { return false; } } #endregion } }