|
- using BPASmartClient.Business;
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartDatavDeviceClient.IoT;
- using System;
- using System.Collections.Generic;
- using System.Threading;
-
- namespace BPASmartClient.IoT
- {
- /// <summary>
- /// DataV客户端数据中心
- /// </summary>
- 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<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()
- {
- 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);
- }
-
- /// <summary>
- /// 启动DataV数据上报
- /// </summary>
- public void Start()
- {
- //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);
- }
-
- /// <summary>
- /// 接收云端消息
- /// </summary>
- /// <param name="topic"></param>
- /// <param name="message"></param>
- private void DevIOTActionHandler(string deviceId,string topic, string message)
- {
- //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;
- }
-
- /// <summary>
- /// 命令名称:0 控制类 1 设置属性 2 通知信息类
- /// </summary>
- public int CommandName
- {
- get;
- set;
- }
-
- /// <summary>
- /// 命令变量:执行变量 key为属性或时间 value为值或者消息
- /// </summary>
- public Dictionary<string, string> CommandValue
- {
- get;
- set;
- }
- }
- }
|