Просмотр исходного кода

调整IOT数据上报

样式分支
fyf 2 лет назад
Родитель
Сommit
ccdff0828e
5 измененных файлов: 55 добавлений и 106 удалений
  1. +0
    -93
      BPASmartClient.Helper/Tools.cs
  2. +40
    -9
      BPASmartClient.IoT/DataVClient.cs
  3. +3
    -2
      BPASmartClient.IoT/Model/DataVReport.cs
  4. +1
    -0
      BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj
  5. +11
    -2
      BPASmartClient.ViewModel/LogViewModel.cs

+ 0
- 93
BPASmartClient.Helper/Tools.cs Просмотреть файл

@@ -1,93 +0,0 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Helper
{
/// <summary>
/// 工具
/// </summary>
public class Tools
{
/// <summary>
/// 对象Json序列
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象实例</param>
/// <returns></returns>
public static string JsonConvertTools<T>(T obj)
{
string strvalue = JsonConvert.SerializeObject(obj);
return strvalue;
}

/// <summary>
/// 对象反Json序列
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="jsonstring">对象序列化json字符串</param>
/// <returns>返回对象实例</returns>
public static T JsonToObjectTools<T>(string jsonstring)
{
T obj = JsonConvert.DeserializeObject<T>(jsonstring);
return obj;
}

/// <summary>
/// DateTime --> long
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static long ConvertDateTimeToLong(DateTime dt)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan toNow = dt.Subtract(dtStart);
long timeStamp = toNow.Ticks;
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
return timeStamp;
}

/// <summary>
/// long --> DateTime
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static DateTime ConvertLongToDateTime(long d)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(d + "0000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
}

/// <summary>
/// 获取IP 地址
/// </summary>
/// <returns></returns>
public static string GetLocalIp()
{
//得到本机名
string hostname = Dns.GetHostName();
//解析主机名称或IP地址的system.net.iphostentry实例。
IPHostEntry localhost = Dns.GetHostEntry(hostname);
if (localhost != null)
{
foreach (IPAddress item in localhost.AddressList)
{
//判断是否是内网IPv4地址
if (item.AddressFamily == AddressFamily.InterNetwork)
{
return item.MapToIPv4().ToString();
}
}
}
return "192.168.1.124";
}
}
}

+ 40
- 9
BPASmartClient.IoT/DataVClient.cs Просмотреть файл

@@ -1,4 +1,8 @@
using BPASmartClient.Business;

using BPA.Message.Enum;
using BPA.Message.IOT;
using BPASmartClient.Business;
using BPASmartClient.Device;
using BPASmartClient.Helper; using BPASmartClient.Helper;
using BPASmartClient.Message; using BPASmartClient.Message;
using BPASmartDatavDeviceClient.IoT; using BPASmartDatavDeviceClient.IoT;
@@ -11,7 +15,7 @@ namespace BPASmartClient.IoT
/// <summary> /// <summary>
/// DataV客户端数据中心 /// DataV客户端数据中心
/// </summary> /// </summary>
public class DataVClient
public class DataVClient
{ {
#region 单例模式 #region 单例模式
private volatile static DataVClient _Instance; private volatile static DataVClient _Instance;
@@ -36,7 +40,11 @@ namespace BPASmartClient.IoT
/// <summary> /// <summary>
/// MQTT上报集合 /// MQTT上报集合
/// </summary> /// </summary>
public DataVReport DeviceDataV=new DataVReport();
public DataVReport DeviceDataV = new DataVReport();
/// <summary>
/// 大屏上报Model
/// </summary>
public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
#endregion #endregion


#region API调用 #region API调用
@@ -149,15 +157,32 @@ namespace BPASmartClient.IoT
{ {
ThreadManage.GetInstance().StartLong(new Action(() => ThreadManage.GetInstance().StartLong(new Action(() =>
{ {
Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
if (DeviceDataV != null && DeviceDataV.GetIsConnected())
{ {
Dictionary<string, object> status = device.Status.GetStatus();
if (DeviceDataV.GetIsConnected())
DataVNodeStatus dataVNode = new DataVNodeStatus();
dataVNode.data = new List<object>();
Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
{ {
// DeviceDataVs[device.DeviceId.ToString()].
var obj=new
{
DeviceId=device.DeviceId,
Name=device.Name,
DeviceType=device.DeviceType,
IsBusy=device.IsBusy,
IsHealth=device.IsHealth,
Status=device.Status.GetStatus(),
};
dataVNode.data.Add(obj);
});
if (dataVNode.data.Count > 0)
{
iOTDevSXModel.KZSX = Tools.JsonConvertTools<DevSX>(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
iOTDevSXModel.JBSX = Tools.JsonConvertTools<DevSX>(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
iOTDevSXModel.NodeStatus = Tools.JsonConvertTools<DataVNodeStatus>(dataVNode);
DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson());
} }
});
Thread.Sleep(5000);
}
Thread.Sleep(3000);
}), "DataV数据上报", true); }), "DataV数据上报", true);
} }
#endregion #endregion
@@ -210,4 +235,10 @@ namespace BPASmartClient.IoT
set; set;
} }
} }

public class DataVNodeStatus
{
public List<object> data { get; set; }
}

} }

+ 3
- 2
BPASmartClient.IoT/Model/DataVReport.cs Просмотреть файл

@@ -1,4 +1,5 @@
using BPASmartClient.Helper;
using BPA.Message.IOT;
using BPASmartClient.Helper;
using BPASmartClient.IoT; using BPASmartClient.IoT;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -292,7 +293,7 @@ namespace BPASmartDatavDeviceClient.IoT
/// <summary> /// <summary>
/// 属性发布消息主题 /// 属性发布消息主题
/// </summary> /// </summary>
public static string PubTopic = "/" + ProductKey + "/" + DeviceName + "/user/update";
public string PubTopic = "/" + ProductKey + "/" + DeviceName + "/user/update";
/// <summary> /// <summary>
/// 属性接收消息主题 /// 属性接收消息主题
/// </summary> /// </summary>


+ 1
- 0
BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj Просмотреть файл

@@ -13,6 +13,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BPASmartClient.Business\BPASmartClient.Business.csproj" /> <ProjectReference Include="..\BPASmartClient.Business\BPASmartClient.Business.csproj" />
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" /> <ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
<ProjectReference Include="..\BPASmartClient.IoT\BPASmartClient.IoT.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" /> <ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
</ItemGroup> </ItemGroup>




+ 11
- 2
BPASmartClient.ViewModel/LogViewModel.cs Просмотреть файл

@@ -1,4 +1,5 @@
using BPASmartClient.Message;
using BPASmartClient.IoT;
using BPASmartClient.Message;
using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Win32; using Microsoft.Win32;
@@ -22,7 +23,7 @@ namespace BPASmartClient.ViewModel
public class LogViewModel :ObservableObject public class LogViewModel :ObservableObject
{ {
public DispatcherTimer dispatcherTimer; public DispatcherTimer dispatcherTimer;
public string ClientId= System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
private ObservableCollection<LogModel> _LogModels; private ObservableCollection<LogModel> _LogModels;
public ObservableCollection<LogModel> LogDataGrid public ObservableCollection<LogModel> LogDataGrid
{ {
@@ -56,6 +57,14 @@ namespace BPASmartClient.ViewModel
System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
{ {
LogDataGrid.Insert(0,new LogModel { message = s,type = "Error" }); LogDataGrid.Insert(0,new LogModel { message = s,type = "Error" });
DataVClient.GetInstance().HttpAddLog(new LogTable
{
ClientId = ClientId,
LogTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"),
LogType = "严重",
LogMessage = s,
LogVla = "错误日志",
});
})); }));
}); });
ExcelCommand = new RelayCommand(() => ExcelCommand = new RelayCommand(() =>


Загрузка…
Отмена
Сохранить