|
@@ -11,11 +11,11 @@ namespace BPASmartClient.IoT |
|
|
/// <summary> |
|
|
/// <summary> |
|
|
/// DataV客户端数据中心 |
|
|
/// DataV客户端数据中心 |
|
|
/// </summary> |
|
|
/// </summary> |
|
|
public class DataVClient : IPlugin |
|
|
|
|
|
|
|
|
public class DataVClient |
|
|
{ |
|
|
{ |
|
|
#region 单例模式 |
|
|
#region 单例模式 |
|
|
private volatile static DataVClient _Instance; |
|
|
private volatile static DataVClient _Instance; |
|
|
public static DataVClient GetInstance => _Instance ?? (_Instance = new DataVClient()); |
|
|
|
|
|
|
|
|
public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient()); |
|
|
#endregion |
|
|
#endregion |
|
|
|
|
|
|
|
|
#region 公有变量 |
|
|
#region 公有变量 |
|
@@ -24,13 +24,94 @@ namespace BPASmartClient.IoT |
|
|
//客户端ID |
|
|
//客户端ID |
|
|
public string ClientId { set; get; } |
|
|
public string ClientId { set; get; } |
|
|
//MQTT上报集合 |
|
|
//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 |
|
|
#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() |
|
|
public DataVClient() |
|
|
{ |
|
|
{ |
|
|
DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString(); |
|
|
DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString(); |
|
|
ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString(); |
|
|
ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString(); |
|
|
|
|
|
Initialize(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void Dispose() |
|
|
public void Dispose() |
|
@@ -40,16 +121,11 @@ namespace BPASmartClient.IoT |
|
|
|
|
|
|
|
|
public void Initialize() |
|
|
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> |
|
|
/// <summary> |
|
@@ -57,22 +133,19 @@ namespace BPASmartClient.IoT |
|
|
/// </summary> |
|
|
/// </summary> |
|
|
public void Start() |
|
|
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> |
|
|
/// <summary> |
|
@@ -82,41 +155,39 @@ namespace BPASmartClient.IoT |
|
|
/// <param name="message"></param> |
|
|
/// <param name="message"></param> |
|
|
private void DevIOTActionHandler(string deviceId,string topic, string message) |
|
|
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 |
|
|
public class IOTCommandModel |
|
|
{ |
|
|
{ |
|
|
// |
|
|
|
|
|
// 摘要: |
|
|
|
|
|
// 设备名称 |
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
/// 设备名称 |
|
|
|
|
|
/// </summary> |
|
|
public string deviceName |
|
|
public string deviceName |
|
|
{ |
|
|
{ |
|
|
get; |
|
|
get; |
|
|
set; |
|
|
set; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// |
|
|
|
|
|
// 摘要: |
|
|
|
|
|
// 命令名称:0 控制类 1 设置属性 2 通知信息类 |
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
/// 命令名称:0 控制类 1 设置属性 2 通知信息类 |
|
|
|
|
|
/// </summary> |
|
|
public int CommandName |
|
|
public int CommandName |
|
|
{ |
|
|
{ |
|
|
get; |
|
|
get; |
|
|
set; |
|
|
set; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// |
|
|
|
|
|
// 摘要: |
|
|
|
|
|
// 命令变量:执行变量 key为属性或时间 value为值或者消息 |
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
/// 命令变量:执行变量 key为属性或时间 value为值或者消息 |
|
|
|
|
|
/// </summary> |
|
|
public Dictionary<string, string> CommandValue |
|
|
public Dictionary<string, string> CommandValue |
|
|
{ |
|
|
{ |
|
|
get; |
|
|
get; |
|
|