using BPA.Message.Enum;
using BPA.Message.IOT;
using BPASmartClient.Business;
using BPASmartClient.Device;
using BPASmartClient.Helper;
using BPASmartClient.IoT.Model;
using BPASmartClient.Message;
using BPASmartDatavDeviceClient.IoT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace BPASmartClient.IoT
{
///
/// DataV客户端数据中心
///
public class DataVClient
{
#region 单例模式
public static DataVClient init = null;
public static DataVClient GetInstance()
{
if (init == null)
{
init = new DataVClient();
}
return init;
}
///
/// 显示定义构造函数为私有,表示只允许自己实例化自己
///
public DataVClient()
{
DataVApiAddress = InternetInfo.DataVApiAddress;
//DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
DeviceName = System.Configuration.ConfigurationManager.AppSettings["DeviceName"].ToString();
ProductKey = System.Configuration.ConfigurationManager.AppSettings["ProductKey"].ToString();
DeviceSecret = System.Configuration.ConfigurationManager.AppSettings["DeviceSecret"].ToString();
StartupMode = System.Configuration.ConfigurationManager.AppSettings["StartupMode"].ToString();
BroadcastPubTopic = InternetInfo.BroadcastPubTopic;
//BroadcastPubTopic = System.Configuration.ConfigurationManager.AppSettings["BroadcastPubTopic"].ToString();
}
#endregion
#region 公有变量
public string StartupMode { set; get; }
public string DeviceName { set; get; }
public string ProductKey { set; get; }
public string DeviceSecret { set; get; }
public string BroadcastPubTopic { set; get; }
///
/// DataV 服务地址
///
public string DataVApiAddress { set; get; }
///
/// 客户端ID
///
public string ClientId { set; get; }
///
/// MQTT上报集合
///
public DataVReport DeviceDataV = new DataVReport();
///
/// 大屏上报Model
///
public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
///
/// key值
///
public Dictionary keyValues = new Dictionary();
#endregion
#region API调用
///
/// 增加告警信息
///
///
/// 返回ID
public string HttpAddAlarm(AlarmTable alarmTable)
{
try
{
if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
{
alarmTable.ClientId = ClientId;
alarmTable.devicename = DeviceDataV.deviceTable.devicename;
DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(alarmTable));
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return alarmTable.KeyID;
}
///
/// 增加日志信息
///
///
/// 返回ID
public string HttpAddLog(LogTable logTable)
{
string id = string.Empty;
try
{
if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
{
logTable.ClientId = ClientId;
logTable.devicename = DeviceDataV.deviceTable.devicename;
DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(logTable));
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}
#endregion
#region 公用
///
/// 释放
///
public void Dispose()
{
ThreadManage.GetInstance().StopTask("DataV数据上报");
}
///
/// 初始化
///
public void Initialize()
{
string message = string.Empty;
if (StartupMode == "API")
{
if (DeviceDataV.Initialize(DataVApiAddress, ClientId, "", ref message))
{
ProductKey = DeviceDataV.deviceTable.productkey;
DeviceName = DeviceDataV.deviceTable.devicename;
DeviceSecret = DeviceDataV.deviceTable.devicesecret;
DeviceDataV.DataVMessageAction += DevIOTActionHandler;
MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
UpDataFile();
}
else
{
MessageLog.GetInstance.ShowEx(message);
}
}
else
{
if (DeviceDataV.InitializeNo(ProductKey, DeviceName, DeviceSecret, ref message))
{
MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
DeviceDataV.DataVMessageAction += DevIOTActionHandler;
UpDataFile();
}
else
MessageLog.GetInstance.ShowEx(message);
}
Plugin.GetInstance()?.GetPlugin()?.GetDevices()?.ForEach(device =>
{
device.AddErrorAction += AddErrorAction;
device.DeleteErrorAction += DeleteErrorAction;
});
}
///
/// 启动DataV数据上报
///
public void Start()
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
{
iOTDevSXModel.Device1 = String.Empty; iOTDevSXModel.Device2 = String.Empty; iOTDevSXModel.Device3 = String.Empty;
List