|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
-
- using BPASmartClient.Business;
- using BPA.Helper;
- using BPASmartClient.IoT;
- using DataVAPI.Tool.IOT;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BPA.Message.IOT;
-
- namespace BPASmartClient.ViewModel
- {
- public class DataVViewModel : NotifyBase
- {
- #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 = InternetInfo.DataVApiAddress;
- 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 = APIHelper.GetInstance.GetRequest(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 = APIHelper.GetInstance.HttpRequest(url, 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 = APIHelper.GetInstance.HttpRequest(url, device);
- Refresh();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
- }
-
- public bool Add(DeviceTable deviceTable)
- {
- try
- {
- string url = ApiURL + "/api/Device/Create";
- string json = APIHelper.GetInstance.HttpRequest(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 = APIHelper.GetInstance.GetRequest(url);
- Refresh();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
- }
- #endregion
- }
- }
|