|
-
- 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<DeviceTable>();
- device = new DeviceTable();
- deviceTableSelectedItem = new DeviceTable();
- ApiURL = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
- Refresh();
- }
- #endregion
-
- #region 公有变量
- /// <summary>
- /// 设备表
- /// </summary>
- public ObservableCollection<DeviceTable> deviceTable { get; set; }
- /// <summary>
- /// 选中行
- /// </summary>
- private DeviceTable _deviceTableSelectedItem;
-
- public DeviceTable deviceTableSelectedItem
- {
- get { return _deviceTableSelectedItem; }
- set
- {
- if (_deviceTableSelectedItem == value)
- return;
- _deviceTableSelectedItem = value;
- OnPropertyChanged("deviceTableSelectedItem");
- }
- }
- /// <summary>
- /// 当前设备
- /// </summary>
- private DeviceTable _device;
-
- public DeviceTable device
- {
- get { return _device; }
- set
- {
- if (_device == value)
- return;
- _device = value;
- OnPropertyChanged("device");
- }
- }
- /// <summary>
- /// API 地址
- /// </summary>
- public string ApiURL { get; set; }
- #endregion
-
- #region 公有函数
-
-
- /// <summary>
- /// 刷新
- /// </summary>
- public void Refresh()
- {
- deviceTable.Clear();
- Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d =>
- {
- if (int.Parse(d.ClientId) > 0)
- {
- deviceTable.Add(d);
- }
- });
- }
-
- /// <summary>
- /// 根据客户端iD查询
- /// </summary>
- /// <param name="clientId"></param>
- public List<DeviceTable> 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<List<DeviceTable>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<DeviceTable>>>(json);
- return jsonMsg.obj.data;
- }
- catch (Exception ex)
- {
- return null;
- }
-
- }
-
- /// <summary>
- /// 修改
- /// </summary>
- /// <returns></returns>
- 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;
- }
-
- }
-
- /// <summary>
- /// 增加
- /// </summary>
- /// <returns></returns>
- 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;
- }
-
- }
-
- /// <summary>
- /// 删除
- /// </summary>
- /// <returns></returns>
- 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
- }
- }
|