using BPASmartClient.Business;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartDatavDeviceClient.IoT;
using System;
using System.Collections.Generic;
using System.Threading;
namespace BPASmartClient.IoT
{
///
/// DataV客户端数据中心
///
public class DataVClient
{
#region 单例模式
private volatile static DataVClient _Instance;
public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient());
#endregion
#region 公有变量
//DataV 服务地址
public string DataVApiAddress { set; get; }
//客户端ID
public string ClientId { set; get; }
//MQTT上报集合
//public Dictionary DeviceDataVs = new Dictionary();
public DataVReport DeviceDataV=new DataVReport();
#endregion
#region API调用
///
/// 增加告警信息
///
///
/// 返回ID
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 msg = Tools.JsonToObjectTools>(redata);
id = msg?.obj?.data?.Id;
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}
///
/// 根据ID删除告警信息
///
///
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);
}
}
///
/// 增加日志信息
///
///
/// 返回ID
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 msg = Tools.JsonToObjectTools>(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()
{
ThreadManage.GetInstance().StopTask("DataV数据上报");
}
public void Initialize()
{
string message = string.Empty;
DeviceDataV = new DataVReport();
bool ret = DeviceDataV.Initialize(DataVApiAddress, ClientId,"", ref message);
DeviceDataV.DataVMessageAction += DevIOTActionHandler;
MessageLog.GetInstance.ShowEx(message);
}
///
/// 启动DataV数据上报
///
public void Start()
{
//ThreadManage.GetInstance().StartLong(new Action(() =>
//{
// //Plugin.GetInstance().GetPlugin().GetDevices()?.ForEach(device =>
// //{
// // Dictionary status = device.Status.GetStatus();
// // if (DeviceDataV.GetIsConnected())
// // {
// // //DeviceDataVs[device.DeviceId.ToString()].
// // }
// //});
// Thread.Sleep(5000);
//}), "DataV数据上报", true);
}
///
/// 接收云端消息
///
///
///
private void DevIOTActionHandler(string deviceId,string topic, string message)
{
//if (DeviceDataVs.ContainsKey() DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
//{
// IOTCommandModel iOTCommand = Tools.JsonToObjectTools(message);
// if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename)
// ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
//}
}
}
//命令实体类
public class IOTCommandModel
{
///
/// 设备名称
///
public string deviceName
{
get;
set;
}
///
/// 命令名称:0 控制类 1 设置属性 2 通知信息类
///
public int CommandName
{
get;
set;
}
///
/// 命令变量:执行变量 key为属性或时间 value为值或者消息
///
public Dictionary CommandValue
{
get;
set;
}
}
}