|
- using DataVAPI.Model;
- using DataVAPI.Tool.API请求;
- using DataVAPI.Tool.IOT;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
-
- namespace DataVIOT.Help.ViewModel
- {
- public class MainViewModel : ObservableObject
- {
- #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 = "https://bpa.black-pa.com:21527/datav";
- #endregion
-
- #region 不需要监听
- private static MainViewModel _instance;
- public static MainViewModel Instance
- {
- get
- {
- if (_instance == null)
- _instance = new MainViewModel();
- return _instance;
- }
- }
- public MainViewModel()
- {
- deviceTable = new ObservableCollection<DeviceTable>();
- device = new DeviceTable();
- deviceTableSelectedItem = new DeviceTable();
- //Refresh();
- }
- #endregion
-
- #region 公有函数
-
-
- /// <summary>
- /// 刷新
- /// </summary>
- public void Refresh()
- {
- deviceTable.Clear();
- Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d =>
- {
- 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
-
- }
- }
|