Bladeren bron

更新iot

样式分支
fyf 2 jaren geleden
bovenliggende
commit
49715224e7
7 gewijzigde bestanden met toevoegingen van 173 en 50 verwijderingen
  1. +117
    -46
      BPASmartClient.IoT/DataVClient.cs
  2. +7
    -3
      BPASmartClient.IoT/Model/DataVReport.cs
  3. +4
    -0
      BPASmartClient.Message/BPASmartClient.Message.csproj
  4. +34
    -1
      BPASmartClient.Message/MessageLog.cs
  5. +1
    -0
      BPASmartClient/BPASmartClient.csproj
  6. +8
    -0
      BPASmartClient/Control/LogView.xaml
  7. +2
    -0
      BPASmartClient/MainWindow.xaml.cs

+ 117
- 46
BPASmartClient.IoT/DataVClient.cs Bestand weergeven

@@ -11,11 +11,11 @@ namespace BPASmartClient.IoT
/// <summary>
/// DataV客户端数据中心
/// </summary>
public class DataVClient : IPlugin
public class DataVClient
{
#region 单例模式
private volatile static DataVClient _Instance;
public static DataVClient GetInstance => _Instance ?? (_Instance = new DataVClient());
public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient());
#endregion

#region 公有变量
@@ -24,13 +24,94 @@ namespace BPASmartClient.IoT
//客户端ID
public string ClientId { set; get; }
//MQTT上报集合
public Dictionary<string, DataVReport> DeviceDataVs = new Dictionary<string, DataVReport>();
//public Dictionary<string, DataVReport> DeviceDataVs = new Dictionary<string, DataVReport>();
public DataVReport DeviceDataV=new DataVReport();
#endregion

#region API调用
/// <summary>
/// 增加告警信息
/// </summary>
/// <param name="alarmTable"></param>
/// <returns>返回ID</returns>
public string HttpAddAlarm(AlarmTable alarmTable)
{
string id = string.Empty;
try
{
if (DeviceDataV != null && DeviceDataV.GetIsConnected())
{
string url = DataVApiAddress + "/api/Alarm/Create";
alarmTable.ClientId = ClientId;
alarmTable.devicename = DeviceDataV.deviceTable.devicename;
string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(alarmTable));
if (!string.IsNullOrEmpty(redata))
{
JsonMsg<AlarmTable> msg = Tools.JsonToObjectTools<JsonMsg<AlarmTable>>(redata);
id = msg?.obj?.data?.Id;
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}

/// <summary>
/// 根据ID删除告警信息
/// </summary>
/// <param name="alarm"></param>
public void HttpDeleteAlarm(string id)
{
try
{
if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; }
string url = DataVApiAddress + "/api/Alarm/Delete?id=" + id;
HttpRequestHelper.HttpGetRequest(url);
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
}

/// <summary>
/// 增加日志信息
/// </summary>
/// <param name="alarmTable"></param>
/// <returns>返回ID</returns>
public string HttpAddLog(LogTable logTable)
{
string id = string.Empty;
try
{
if (DeviceDataV != null && DeviceDataV.GetIsConnected())
{
string url = DataVApiAddress + "/api/Log/Create";
logTable.ClientId = ClientId;
logTable.devicename = DeviceDataV.deviceTable.devicename;
string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(logTable));
if (!string.IsNullOrEmpty(redata))
{
JsonMsg<LogTable> msg = Tools.JsonToObjectTools<JsonMsg<LogTable>>(redata);
id = msg?.obj?.data?.Id;
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}
#endregion
public DataVClient()
{
DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
Initialize();
}

public void Dispose()
@@ -40,16 +121,11 @@ namespace BPASmartClient.IoT

public void Initialize()
{
Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()?.ForEach(device =>
{
string message=string.Empty;
DataVReport dataV = new DataVReport();
bool ret= dataV.Initialize(DataVApiAddress, ClientId, device.DeviceId.ToString(),ref message);
dataV.DataVMessageAction += DevIOTActionHandler;
MessageLog.GetInstance.ShowEx(message);
if (!ret)
DeviceDataVs[device.DeviceId.ToString()] = dataV;
});
string message = string.Empty;
DeviceDataV = new DataVReport();
bool ret = DeviceDataV.Initialize(DataVApiAddress, ClientId,"", ref message);
DeviceDataV.DataVMessageAction += DevIOTActionHandler;
MessageLog.GetInstance.ShowEx(message);
}

/// <summary>
@@ -57,22 +133,19 @@ namespace BPASmartClient.IoT
/// </summary>
public void Start()
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()?.ForEach(device =>
{
if (DeviceDataVs.ContainsKey(device.DeviceId.ToString()))
{
Dictionary<string, object> status= device.Status.GetStatus();
if (DeviceDataVs[device.DeviceId.ToString()].GetIsConnected())
{
//DeviceDataVs[device.DeviceId.ToString()].
}
}
});
//ThreadManage.GetInstance().StartLong(new Action(() =>
//{
// //Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()?.ForEach(device =>
// //{
// // Dictionary<string, object> status = device.Status.GetStatus();
// // if (DeviceDataV.GetIsConnected())
// // {
// // //DeviceDataVs[device.DeviceId.ToString()].
// // }
// //});

Thread.Sleep(5000);
}), "DataV数据上报", true);
// Thread.Sleep(5000);
//}), "DataV数据上报", true);
}

/// <summary>
@@ -82,41 +155,39 @@ namespace BPASmartClient.IoT
/// <param name="message"></param>
private void DevIOTActionHandler(string deviceId,string topic, string message)
{
if (DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
{
IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename)
ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
}
//if (DeviceDataVs.ContainsKey() DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
//{
// IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
// if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename)
// ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
//}
}
}

//
// 摘要:
// 命令实体类
//命令实体类
public class IOTCommandModel
{
//
// 摘要:
// 设备名称
/// <summary>
/// 设备名称
/// </summary>
public string deviceName
{
get;
set;
}

//
// 摘要:
// 命令名称:0 控制类 1 设置属性 2 通知信息类
/// <summary>
/// 命令名称:0 控制类 1 设置属性 2 通知信息类
/// </summary>
public int CommandName
{
get;
set;
}

//
// 摘要:
// 命令变量:执行变量 key为属性或时间 value为值或者消息
/// <summary>
/// 命令变量:执行变量 key为属性或时间 value为值或者消息
/// </summary>
public Dictionary<string, string> CommandValue
{
get;


+ 7
- 3
BPASmartClient.IoT/Model/DataVReport.cs Bestand weergeven

@@ -25,7 +25,7 @@ namespace BPASmartDatavDeviceClient.IoT
{
if(string.IsNullOrEmpty(url))return false;
DeviceTable device; deviceId = _deviceId;
if (!CreateLinks(url, _clientId, _deviceId, out device))
if (!CreateLinks(url, _clientId,out device, _deviceId))
{
message += $"客户端{_clientId}设备{_deviceId}阿里云上没有该设备。";
return false;
@@ -118,11 +118,15 @@ namespace BPASmartDatavDeviceClient.IoT
/// <summary>
/// 创建连接
/// </summary>
private bool CreateLinks(string url, string clientId, string deviceId, out DeviceTable device)
private bool CreateLinks(string url, string clientId, out DeviceTable device ,string deviceId = "")
{
try
{
string json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}&deviceId={deviceId}");
string json = string.Empty;
if (string.IsNullOrEmpty(deviceId))
json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}");
else
json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}&deviceId={deviceId}");
JsonMsg<List<DeviceTable>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<DeviceTable>>>(json);
if (jsonMsg.obj != null && jsonMsg.obj.data != null)
{


+ 4
- 0
BPASmartClient.Message/BPASmartClient.Message.csproj Bestand weergeven

@@ -4,4 +4,8 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
</ItemGroup>

</Project>

+ 34
- 1
BPASmartClient.Message/MessageLog.cs Bestand weergeven

@@ -1,7 +1,9 @@
using System;
using BPASmartClient.Peripheral;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

@@ -59,7 +61,38 @@ namespace BPASmartClient.Message
}
#endregion

#region 查找设备ID
/// <summary>
/// 查询设备ID
/// </summary>
/// <returns></returns>
public object ReturnDeviceID()
{
object DeviceId = null;
StackTrace trace = new StackTrace();
for (int i = 1; i < 10; i++)
{
Type type = trace.GetFrame(i).GetMethod().ReflectedType;
try
{
object obj = Activator.CreateInstance(type);
if (obj is IPeripheral)
{
IPeripheral peripheral = obj as IPeripheral;
DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
}
if (DeviceId != null) break;
}
catch (Exception ex)
{

}
}
return DeviceId;
}
#endregion





+ 1
- 0
BPASmartClient/BPASmartClient.csproj Bestand weergeven

@@ -21,6 +21,7 @@
<ProjectReference Include="..\BPASmartClient.Business\BPASmartClient.Business.csproj" />
<ProjectReference Include="..\BPASmartClient.DRCoffee\BPASmartClient.DRCoffee.csproj" />
<ProjectReference Include="..\BPASmartClient.GSIceCream\BPASmartClient.GSIceCream.csproj" />
<ProjectReference Include="..\BPASmartClient.IoT\BPASmartClient.IoT.csproj" />
<ProjectReference Include="..\BPASmartClient.KLMCoffee\BPASmartClient.KLMCoffee.csproj" />
<ProjectReference Include="..\BPASmartClient.Lebai\BPASmartClient.Lebai.csproj" />
<ProjectReference Include="..\BPASmartClient.MorkD\BPASmartClient.MorkD.csproj" />


+ 8
- 0
BPASmartClient/Control/LogView.xaml Bestand weergeven

@@ -42,6 +42,14 @@
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<!--<DataGridTemplateColumn Header="设备ID" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding deviceId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->

<DataGridTemplateColumn Header="日志类型" Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>


+ 2
- 0
BPASmartClient/MainWindow.xaml.cs Bestand weergeven

@@ -5,6 +5,7 @@ using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.Device;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.IoT;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.冰淇淋.Enum;
@@ -60,6 +61,7 @@ namespace BPASmartClient

ThreadManage.GetInstance().Start(new Action(() =>
{
DataVClient.GetInstance().Start();
new MainConsole().Start();
}), "启动主控制台");
}


Laden…
Annuleren
Opslaan