using HandyC.HW.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace HandyC.HW.Data
{
///
/// 鉴权数据处理类
///
public class Authentication:IAuthentication
{
private readonly string accessTokenName = "Authentication";
private ICacheAdapter cache;
public Authentication(ICacheAdapter adapter)
{
cache = adapter;
}
///
/// 获取鉴权的数据实例
///
/// 异常时执行回调
/// 权限信息
public ApiAuthInfo GetApiAuth(Action
获取设备信息代码如下:
using HandyC.HW.Tools;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace HandyC.HW.Data
{
///
/// 数据采集操作实体
///
public class DataCollection: IDataCollection
{
private IAuthentication _authentication;
public DataCollection(IAuthentication authentication)
{
_authentication = authentication;
}
//查询单个设备信息
public QuerySingleDeviceInfoOutDTO QuerySingleDeviceInfo(string deviceId, Action ErrorCallBack)
{
if (string.IsNullOrEmpty(deviceId))
{
throw new NullReferenceException("deviceId 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary dataDic = new Dictionary();
string localPath = ApiUrls.queryDeviceInfo.Replace("{deviceId}", deviceId);
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", dataDic, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.PostResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//批量查询设备信息列表
public QueryBatchDevicesInfoOutDTO QueryBatchDevicesInfo(QueryBatchDevicesInfoInDTO filter, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
//处理异常信息
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary pathDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
var customAttribute = property.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute));
if (customAttribute != null)
{
//获取到符合条件的参数信息
var namedArgument = customAttribute.NamedArguments.FirstOrDefault(x => x.MemberName == "PropertyName");
//判定参数名称参数对应的别名名称不为空
if (namedArgument != null && namedArgument.TypedValue.Value != null)
{
pathDic.Add(namedArgument.TypedValue.Value.ToString(), value.ToString());
}
}
else
{
pathDic.Add(property.Name, value.ToString());
}
}
}
string strPath = pathDic.DictionaryToString();
string localPath = ApiUrls.queryDevices.Replace("?", $"?{strPath}");
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", new Dictionary(), headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//requestMessage.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Postman/6.5.2 Chrome/59.0.3071.115 Electron/1.8.4 Safari/537.36");
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询设备历史信息
public DeviceDataSource QueryDeviceDataHistory(QueryDeviceDataHistoryInDTO filter, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary pathDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
var customAttribute = property.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute));
if (customAttribute != null)
{
//获取到符合条件的参数信息
var namedArgument = customAttribute.NamedArguments.FirstOrDefault(x => x.MemberName == "PropertyName");
//判定参数名称参数对应的别名名称不为空
if (namedArgument != null && namedArgument.TypedValue.Value != null)
{
pathDic.Add(namedArgument.TypedValue.Value.ToString(), value.ToString());
}
}
else
{
pathDic.Add(property.Name, value.ToString());
}
}
}
string strPath = pathDic.DictionaryToString();
string localPath = ApiUrls.queryHistoryDevice.Replace("?", $"?{strPath}");
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", new Dictionary(), headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询影子历史数据信息
public DeviceDesiredSource QueryDeviceDesiredHistory(QueryDeviceDesiredHistoryInDTO filter, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary pathDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
var customAttribute = property.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute));
if (customAttribute != null)
{
//获取到符合条件的参数信息
var namedArgument = customAttribute.NamedArguments.FirstOrDefault(x => x.MemberName == "PropertyName");
//判定参数名称参数对应的别名名称不为空
if (namedArgument != null && namedArgument.TypedValue.Value != null)
{
pathDic.Add(namedArgument.TypedValue.Value.ToString(), value.ToString());
}
}
else
{
pathDic.Add(property.Name, value.ToString());
}
}
}
string strPath = pathDic.DictionaryToString();
string localPath = ApiUrls.queryHistoryShadowDevice.Replace("?", $"?{strPath}");
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", new Dictionary(), headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询设备服务信息
public DeviceCapabilitySource QueryDeviceCapability(QueryDeviceCapabilitiesInDTO filter, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary pathDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
var customAttribute = property.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute));
if (customAttribute != null)
{
//获取到符合条件的参数信息
var namedArgument = customAttribute.NamedArguments.FirstOrDefault(x => x.MemberName == "PropertyName");
//判定参数名称参数对应的别名名称不为空
if (namedArgument != null && namedArgument.TypedValue.Value != null)
{
pathDic.Add(namedArgument.TypedValue.Value.ToString(), value.ToString());
}
}
else
{
pathDic.Add(property.Name, value.ToString());
}
}
}
string strPath = pathDic.DictionaryToString();
string localPath = ApiUrls.queryDeviceServices.Replace("?", $"?{strPath}");
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", new Dictionary(), headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
}
}
下发命令代码如下:
using HandyC.HW.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace HandyC.HW.Data
{
///
/// 设备命令数据处理类
///
public class DeviceCommand: IDeviceCommand
{
private IAuthentication _authentication;
public DeviceCommand(IAuthentication authentication)
{
_authentication = authentication;
}
//创建设备命令
public PostDeviceCommandOutDTO2 CreateDeviceCommand(PostDeviceCommandInDTO2 dataComand, Action ErrorCallBack)
{
if (dataComand is null)
{
throw new NullReferenceException("dataComand 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
string strJson = dataComand.ObjectToJson();
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, ApiUrls.createDeviceCmd, "POST", strJson, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询设备命令
public QueryDeviceCommandOutDTO2 QueryDeviceCommand(QueryDeviceCommandInDTO2 filter, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary dataDic = new Dictionary();
//Queries
Dictionary pathDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
var customAttribute = property.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute));
if (customAttribute != null)
{
//获取到符合条件的参数信息
var namedArgument = customAttribute.NamedArguments.FirstOrDefault(x => x.MemberName == "PropertyName");
//判定参数名称参数对应的别名名称不为空
if (namedArgument != null && namedArgument.TypedValue.Value != null)
{
pathDic.Add(namedArgument.TypedValue.Value.ToString(), value.ToString());
}
}
else
{
pathDic.Add(property.Name, value.ToString());
}
}
}
string queryStr = pathDic.DictionaryToString();
string localPath = ApiUrls.queryDeviceCmd.Replace("?", $"?{queryStr}");
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", dataDic, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//修改设备命令
public ModifyDeviceCommandOutDTO ModifyDeviceCommand(ModifyDeviceCommandInDTO filter,string deviceCommandId, Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
string strJson = filter.ObjectToJson();
string localPath = ApiUrls.modifyDeviceCmd.Replace("{deviceCommandId}", deviceCommandId);
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "PUT", strJson, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询特定设备命令状态
public DeviceCommandRespV4 QueryDeviceCommandStatus(string deviceId, string commandId, Action ErrorCallBack)
{
if (string.IsNullOrEmpty(deviceId))
{
throw new NullReferenceException("deviceId 不能为Null");
}
if (string.IsNullOrEmpty(commandId))
{
throw new NullReferenceException("commandId 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary dataDic = new Dictionary();
//Path
string localPath = ApiUrls.queryDeviceCmdStatus.Replace("{deviceId}", deviceId).Replace("{commandId}",commandId);
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, localPath, "GET", dataDic, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//批量创建设备命令
//创建设备命令撤销任务
public CreateDeviceCmdCancelTaskOutDTO CreateDeviceCmdCancelTask(CreateDeviceCmdCancelTaskInDTO dataComand, Action ErrorCallBack)
{
if (dataComand is null)
{
throw new NullReferenceException("dataComand 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
string strJson = dataComand.ObjectToJson();
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, ApiUrls.createDeviceCancleTasks, "POST", strJson, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//查询设备命令撤销任务
public QueryDeviceCmdCancelTaskOutDTO2 QueryDeviceCommandTask(QueryDeviceCmdCancelTaskInDTO2 filter,Action ErrorCallBack)
{
if (filter is null)
{
throw new NullReferenceException("filter 不能为Null");
}
//获取accessToken信息
ApiAuthInfo apiAuthInfo = _authentication.GetApiAuth(ErrorCallBack);
if (apiAuthInfo == null)
{
return null;
}
//构建请求需要的header和body
//Headers
Dictionary headerDic = new Dictionary
{
{ "app_key",AuthContext.ClientInfo.AppId},
{ "Authorization",$"{apiAuthInfo.TokenType} {apiAuthInfo.AccessToken}"}
};
//Body
Dictionary dataDic = new Dictionary();
//遍历获取对应实体的所有当前类自身属性
Type type = filter.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var value = property.GetValue(filter);
if (!(value is null))
{
dataDic.Add(property.Name, value.ToString());
}
}
HttpRequestMessage requestMessage = HttpHelper.SetRequestMessage(AuthContext.ClientInfo.Host, ApiUrls.queryDeviceCancleTasks, "GET", dataDic, headerDic);
//此处为智障代码切记小心
if (requestMessage.Content != null)
{
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
//获取证书
var cer = AuthContext.SSLConfig.X509Certificate2;
var result = HttpHelper.GetResponse(requestMessage, cer);
if (result == null)
{
//处理异常信息
ErrorCallBack(HttpHelper.ApiException);
return null;
}
return result;
}
//设备服务调用
}
}
设备管理代码如下:
using GalaSoft.MvvmLight.Messaging;
using HandyC.HW.Datas;
using HandyC.HW.ViewModels;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HandyC.HW.Views
{
///
/// DeviceManage.xaml 的交互逻辑
///
public partial class DeviceManage
{
public DeviceManage()
{
InitializeComponent();
InitialMessager();
}
///
/// 初始化事件注册
///
private void InitialMessager()
{
//避免token值重名使用
this.Loaded += (e, arg) => {
Messenger.Default.Register(this, MessageToken.NewDeviceDialog, NewDeviceDialog);
Messenger.Default.Register(this, MessageToken.QueryLastestDeviceData, LastestDeviceData);
};
this.Unloaded += (e, arg) =>
{
Messenger.Default.Unregister(this, MessageToken.NewDeviceDialog);
Messenger.Default.Unregister(this, MessageToken.QueryLastestDeviceData);
};
}
///
/// 构建设备最新数据监听窗体
///
/// 设备数据信息
private void LastestDeviceData(DeviceInfoViewModel obj)
{
if (obj != null)
{
var deviceDynamicData = new DeviceDynamicData(obj);
deviceDynamicData.Show();
}
}
private void NewDeviceDialog(DeviceInfoViewModel model)
{
if (model != null)
{
//消息明细展示框
Dialog.Show(new DeviceDetials(model), MessageToken.MainToken);
//加载框
//Dialog.Show(new CommonLoading());
//HandyControl.Controls.MessageBox.Show($"查看设备:{model.Name}信息");
}
}
}
}
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HandyC.HW.Data;
using HandyC.HW.Datas;
using HandyC.HW.Service;
using HandyC.HW.Tools;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommonServiceLocator;
namespace HandyC.HW.ViewModels
{
///
/// 设备管理视图实体
///
public class DeviceManageViewModel : ViewModelBase
{
private readonly IDeviceInfoService _deviceInfoService;
public DeviceManageViewModel(IDeviceInfoService deviceInfo)
{
_deviceInfoService = deviceInfo;
//获取初始化构造数据
InitialData();
}
///
/// 初始化设定页面数据
///
private void InitialData()
{
var filter = new QueryBatchDevicesInfoInDTO
{
PageNo = 0,
PageSize = 20
};
//开启加载
Messenger.Default.Send(true, MessageToken.LoadingToken);
Task.Factory.StartNew(() =>
{
//实例化
var datas = _deviceInfoService.QueryBatchDevicesInfo(filter, ErrorCellbackMethod);
if (datas != null)
{
//数据转化
DeviceInfos = AutoMapper.Mapper.Map, List>(datas.Devices);
DeviceSummary.GetSummaryData(DeviceInfos);
}
//关闭加载
Messenger.Default.Send(false, MessageToken.LoadingToken);
});
ContextMenus = new List
{
new ContextMenuViewModel(new RelayCommand(QueryDeviceInfo))
{
Header="查看",
IsEnable = true
},
//new ContextMenuViewModel(new RelayCommand(QueryDeviceInfo))
//{
// Header="编辑",
// IsEnable = true
//},
new ContextMenuViewModel(new RelayCommand(QueryLastest))
{
Header="动态",
IsEnable = true
}
};
}
///
/// 查询设备对应的最新数据
///
///
private void QueryLastest(DeviceInfoViewModel obj)
{
if (obj != null)
{
DeviceDynamicViewModel dyVm = (DeviceDynamicViewModel)ServiceLocator.Current.GetInstance(typeof(DeviceDynamicViewModel));
if (dyVm != null)
{
dyVm.InitialDeviceId(obj);
}
Messenger.Default.Send(obj, MessageToken.QueryLastestDeviceData);
}
}
///
/// 网络请求服务异常结果处理
///
///
private void ErrorCellbackMethod(object obj)
{
string result = $"{this.GetType().Name} InitialData:{obj.ToString()}";
OperationResult operation = new OperationResult(result);
Messenger.Default.Send(operation, MessageToken.MsgToken);
}
private List deviceInfos;
///
/// 设备列表
///
public List DeviceInfos
{
get
{
if (deviceInfos == null)
{
deviceInfos = new List();
}
return deviceInfos;
}
set
{
deviceInfos = value;
RaisePropertyChanged();
}
}
private DeviceSummaryViewModel deviceSummary;
///
/// 设备信息汇总
///
public DeviceSummaryViewModel DeviceSummary
{
get
{
if (deviceSummary == null)
{
deviceSummary = new DeviceSummaryViewModel();
}
return deviceSummary;
}
set
{
deviceSummary = value;
RaisePropertyChanged();
}
}
private List contextMenus;
///
/// 设备信息菜单实体
///
public List ContextMenus
{
get
{
if (contextMenus == null)
{
contextMenus = new List();
}
return contextMenus;
}
set
{
contextMenus = value;
RaisePropertyChanged();
}
}
///
/// 查询设备详信息
///
/// 设备实体
private void QueryDeviceInfo(DeviceInfoViewModel entity)
{
if (entity != null)
{
Messenger.Default.Send(entity, MessageToken.NewDeviceDialog);
}
}
}
}
设备历史信息代码如下:
using GalaSoft.MvvmLight.Messaging;
using HandyC.HW.Datas;
using HandyC.HW.ViewModels;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HandyC.HW.Views
{
///
/// DataHistory.xaml 的交互逻辑
///
public partial class DataHistory
{
public DataHistory()
{
InitializeComponent();
InitialMsg();
}
private void InitialMsg()
{
//避免token值重名使用
this.Loaded += (e, arg) => {
Messenger.Default.Register(this, MessageToken.NewDeviceDataDetails, NewDeviceDataDialog);
};
this.Unloaded += (e, arg) => Messenger.Default.Unregister(this, MessageToken.NewDeviceDataDetails);
}
///
/// 显示数据信息明细
///
///
private void NewDeviceDataDialog(DeviceDataViewModel obj)
{
if (obj != null)
{
Dialog.Show(new DeviceDataInfo(obj), MessageToken.MainToken);
}
}
}
}
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HandyC.HW.Data;
using HandyC.HW.Datas;
using HandyC.HW.Service;
using HandyC.HW.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
namespace HandyC.HW.ViewModels
{
///
/// 历史数据实体实体
///
public class DataHistoryViewModel : ViewModelBase
{
private readonly IDataCollectionService _collectionService;
public DataHistoryViewModel(IDataCollectionService collectionService)
{
_collectionService = collectionService;
InitialData();
}
///
/// 初始化数据
///
private void InitialData()
{
Task.Factory.StartNew(() =>
{
//获取设备信息
Devices = GetDevices();
//默认获取第一个设备的历史数据
if (Devices.Count > 0)
{
SelectedDevice = Devices.FirstOrDefault();
//获取设备服务列表
DeviceServices = GetDeviceServices(SelectedDevice);
pageSize = 10;//默认设定为10
PageNo = 1;//设定默认起始页码
}
});
//设定当前默认数据列表配置值
}
///
/// 获取设备服务列表
///
/// 当前设备
///
private List GetDeviceServices(DeviceInfoViewModel selectedDevice)
{
List services = new List();
List deviceCapabilitys;
var filter = new QueryDeviceCapabilitiesInDTO
{
DeviceId = selectedDevice.DeviceId,
GatewayId = selectedDevice.GatewayId
};
var datas = _collectionService.QueryDeviceCapability(filter, ErrorCellbackMethod);
if (datas != null)
{
deviceCapabilitys = AutoMapper.Mapper.Map, List>(datas.DeviceCapabilities);
}
else
{
deviceCapabilitys = new List();
}
if (deviceCapabilitys == null || deviceCapabilitys.Count < 0)
{
return services;
}
DeviceCapabilityViewModel deviceCapability = deviceCapabilitys.FirstOrDefault(x => x.DeviceId == selectedDevice.DeviceId);
if (deviceCapability != null)
{
services = deviceCapability.ServiceCapabilities;
}
return services;
}
///
/// 获取当前设备列表信息
///
///
private List GetDevices()
{
List result;
//获取当前默认设备对应的数据信息
var filter = new QueryBatchDevicesInfoInDTO
{
PageNo = 0,
PageSize = 1000//默认获取0-1000范围内的设备记录信息
};
//实例化
var datas = _collectionService.QueryBatchDevicesInfo(filter, ErrorCellbackMethod);
if (datas != null)
{
//数据转化
result = AutoMapper.Mapper.Map, List>(datas.Devices);
}
else
{
result = new List();
}
return result;
}
///
/// 网络请求服务异常结果处理
///
///
private void ErrorCellbackMethod(object obj)
{
string result = $"{this.GetType().Name} InitialData:{obj.ToString()}";
OperationResult operation = new OperationResult(result);
Messenger.Default.Send(operation, MessageToken.MsgToken);
}
#region 页面数据
private List devices;
///
/// 设备集合
///
public List Devices
{
get
{
if (devices == null)
{
devices = new List();
}
return devices;
}
set
{
devices = value;
RaisePropertyChanged();
}
}
#endregion
//Cmds
private ICommand pageChangedCmd;
///
/// 当前页变更命令
///
public ICommand PageChangedCmd
{
get
{
if (pageChangedCmd == null)
{
pageChangedCmd = new RelayCommand(PageChanged);
}
return pageChangedCmd;
}
set => pageChangedCmd = value;
}
private ICommand searchCmd;
public ICommand SearchCmd
{
get
{
if (searchCmd == null)
{
searchCmd = new RelayCommand(SearchData);
}
return searchCmd;
}
}
///
/// 查询设备函数
///
private void SearchData()
{
if (PageNo != 1)
{
PageNo = 1;
}
else
{
//获取当前默认设备对应的数据信息
var filter = new QueryDeviceDataHistoryInDTO
{
PageNo = 0,
PageSize = PageSize,
DeviceId = SelectedDevice.DeviceId,
GatewayId = SelectedDevice.GatewayId,
ServiceId = SelectedService.ServiceId
};
Refresh(filter);
}
}
#region 分页控件相关属性和函数
///
/// 当前页变更触发事件
///
///
private void PageChanged(object obj)
{
//获取当前默认设备对应的数据信息
var filter = new QueryDeviceDataHistoryInDTO
{
PageNo = PageNo - 1,
PageSize = PageSize,
DeviceId = SelectedDevice.DeviceId,
GatewayId = SelectedDevice.GatewayId,
ServiceId = SelectedService.ServiceId
};
Refresh(filter);
}
///
/// 刷新当前数据列表
///
public void Refresh(QueryDeviceDataHistoryInDTO filter)
{
Task.Factory.StartNew(() =>
{
//实例化
var datas = _collectionService.QueryDeviceDataHistory(filter, ErrorCellbackMethod);
if (datas != null)
{
TotalCount = datas.TotalCount;
MaxPageCount = GetMaxPageCount();
DeviceDataSource = AutoMapper.Mapper.Map, List>(datas.DeviceDataHistoryDTOs).ToList();
}
});
}
///
/// 获取当前最大记录条数
///
private long GetMaxPageCount()
{
long pageCount;
pageCount = TotalCount / PageSize;
int result = Convert.ToInt32(TotalCount % PageSize);
if (result != 0)
{
pageCount += 1;
}
return pageCount;
}
///
/// 查询的记录数量
///
private long totalCount;
private long pageNo;
private long pageSize;
///
/// 最大显示记录数量
///
public long MaxPageCount
{
get => maxPageCount;
set
{
maxPageCount = value;
RaisePropertyChanged();
}
}
///
/// 查询的记录数量
///
public long TotalCount
{
get { return totalCount; }
set
{
totalCount = value;
RaisePropertyChanged();
}
}
///
/// 查询的页码
///
public long PageNo
{
get => pageNo;
set
{
pageNo = value;
RaisePropertyChanged();
}
}
///
/// 查询每页信息的数量
///
public long PageSize
{
get => pageSize;
set
{
pageSize = value;
RaisePropertyChanged();
}
}
#endregion
#region 设备列表
private List deviceDataSource;
private DeviceInfoViewModel selectedDevice;
private long maxPageCount;
///
/// 设备数据信息集合
///
public List DeviceDataSource
{
get
{
if (deviceDataSource == null)
{
deviceDataSource = new List();
}
return deviceDataSource;
}
set
{
deviceDataSource = value;
RaisePropertyChanged();
}
}
//选中项设备记录
public DeviceInfoViewModel SelectedDevice
{
get => selectedDevice;
set
{
selectedDevice = value;
RaisePropertyChanged();
}
}
#endregion
#region 设备服务列表
private List deviceServices;
///
/// 设备服务能力列表
///
public List DeviceServices
{
get
{
if (deviceServices == null)
{
deviceServices = new List();
}
return deviceServices;
}
set
{
deviceServices = value;
RaisePropertyChanged();
}
}
private ServiceCapabilityDTO selectedService;
///
/// 选中设备服务
///
public ServiceCapabilityDTO SelectedService
{
get
{
if (selectedService == null)
{
selectedService = new ServiceCapabilityDTO();
}
return selectedService;
}
set
{
selectedService = value;
RaisePropertyChanged();
}
}
#endregion
}
}
利用JavaScript进行对象排序,根据用户的年龄排序展示
<script>
var bob={
name;bob,
age:30
}
var peter={
name;peter,
age:30
}
var amy={
name;amy,
age:24
}
var mike={
name;mike,
age:29
}
var john={
FLP
One famous theory in distributed computing, known as FLP after the authors Fischer, Lynch, and Patterson, proved that in a distributed system with asynchronous communication and process crashes,
每一行命令都是用分号(;)作为结束
对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:
mysql_query("INSERT INTO tablename(first_name,last_name)VALUES('$first_name',$last_name')");
题目链接:zoj 3820 Building Fire Stations
题目大意:给定一棵树,选取两个建立加油站,问说所有点距离加油站距离的最大值的最小值是多少,并且任意输出一种建立加油站的方式。
解题思路:二分距离判断,判断函数的复杂度是o(n),这样的复杂度应该是o(nlogn),即使常数系数偏大,但是居然跑了4.5s,也是醉了。 判断函数里面做了3次bfs,但是每次bfs节点最多