|
-
- using BPA.Message.Enum;
- using BPA.Message.IOT;
- using BPASmartClient.Business;
- using BPASmartClient.Device;
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartDatavDeviceClient.IoT;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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());
- public DataVClient()
- {
- DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
- ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
- Initialize();
- }
- #endregion
-
- #region 公有变量
- /// <summary>
- /// DataV 服务地址
- /// </summary>
- public string DataVApiAddress { set; get; }
- /// <summary>
- /// 客户端ID
- /// </summary>
- public string ClientId { set; get; }
- /// <summary>
- /// MQTT上报集合
- /// </summary>
- public DataVReport DeviceDataV = new DataVReport();
- /// <summary>
- /// 大屏上报Model
- /// </summary>
- public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
- /// <summary>
- /// 广播
- /// </summary>
- public string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + "Transit_SetDevice";
- /// <summary>
- /// key值
- /// </summary>
- public Dictionary<string,string> keyValues = new Dictionary<string, string>();
- #endregion
-
- #region API调用
- /// <summary>
- /// 增加告警信息
- /// </summary>
- /// <param name="alarmTable"></param>
- /// <returns>返回ID</returns>
- public string HttpAddAlarm(AlarmTable alarmTable)
- {
- try
- {
- if (DeviceDataV != null && DeviceDataV.GetIsConnected())
- {
- alarmTable.ClientId = ClientId;
- alarmTable.devicename = DeviceDataV.deviceTable.devicename;
- DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(alarmTable));
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show(ex.Message);
- }
- return alarmTable.KeyID;
- }
-
- /// <summary>
- /// 增加日志信息
- /// </summary>
- /// <param name="alarmTable"></param>
- /// <returns>返回ID</returns>
- public string HttpAddLog(LogTable logTable)
- {
- string id = string.Empty;
- try
- {
- if (DeviceDataV != null && DeviceDataV.GetIsConnected())
- {
- logTable.ClientId = ClientId;
- logTable.devicename = DeviceDataV.deviceTable.devicename;
- DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(logTable));
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show(ex.Message);
- }
- return id;
- }
- #endregion
-
- #region 公用
- /// <summary>
- /// 释放
- /// </summary>
- public void Dispose()
- {
- ThreadManage.GetInstance().StopTask("DataV数据上报");
- }
-
- /// <summary>
- /// 初始化
- /// </summary>
- public void Initialize()
- {
- string message = string.Empty;
- if (DeviceDataV.Initialize(DataVApiAddress, ClientId, "", ref message))
- {
- DeviceDataV.DataVMessageAction += DevIOTActionHandler;
- MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceDataV.deviceTable.devicename}阿里云连接成功");
- }
- else
- {
- MessageLog.GetInstance.ShowEx(message);
- }
- Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
- {
- device.AddErrorAction+= AddErrorAction;
- device.DeleteErrorAction += DeleteErrorAction;
- });
- }
-
- /// <summary>
- /// 启动DataV数据上报
- /// </summary>
- public void Start()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (DeviceDataV != null && DeviceDataV.GetIsConnected())
- {
- List<object> dataVNode = new List<object>();
- Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
- {
- var obj = new
- {
- DeviceId = device.DeviceId.ToString(),
- devicename = DeviceDataV.deviceTable.devicename,
- Name = device.Name,
- DeviceType = device.DeviceType.ToString(),
- IsBusy = device.IsBusy ? "忙碌" : "空闲",
- IsBusyColor = device.IsBusy ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
- IsHealth = device.IsHealth ? "健康" : "故障",
- IsHealthColor = !device.IsHealth ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
- Status = device.Status.GetIOTStatus(),
- gjxx = device.GetError(),
- rzxx = device.GetLog(),
- VariableMonitor = device.GetVariableMonitor(),
- };
- dataVNode.Add(obj);
- });
- if (dataVNode.Count > 0)
- {
- iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(new { data = dataVNode });
- DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson());
- }
- }
- Thread.Sleep(3000);
- }), "DataV数据上报", true);
- }
- #endregion
-
- #region 私有
-
- /// <summary>
- /// 增加告警
- /// </summary>
- /// <param name="obj"></param>
- private void AddErrorAction(object obj)
- {
- string id = Guid.NewGuid().ToString();
- HttpAddAlarm(new AlarmTable
- {
- AlarmTime = GetPropertyValue(obj, "Time").ToString(),
- AlarmType = GetPropertyValue(obj, "Type").ToString(),
- AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
- AlarmVla = "告警",
- KeyID = id,
- });
- keyValues[GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString()] =id ;
- }
-
- /// <summary>
- /// 删除告警
- /// </summary>
- /// <param name="obj"></param>
- private void DeleteErrorAction(object obj)
- {
- string message = GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString();
- if (keyValues.ContainsKey(message))
- {
- HttpAddAlarm(new AlarmTable
- {
- AlarmTime = GetPropertyValue(obj, "Time").ToString(),
- AlarmType = GetPropertyValue(obj, "Type").ToString(),
- AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
- AlarmVla = "告警",
- KeyID = keyValues[message],
- State="n"
- });
- }
- }
-
- /// <summary>
- /// 接收云端消息
- /// </summary>
- /// <param name="topic"></param>
- /// <param name="message"></param>
- private void DevIOTActionHandler(string deviceId, string topic, string message)
- {
- if (DeviceDataV.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
- {
- IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
- if (iOTCommand.deviceName == DeviceDataV.deviceTable.devicename)
- ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
- }
- }
-
- /// <summary>
- /// 获取某个对象中的属性值
- /// </summary>
- /// <param name="info"></param>
- /// <param name="field"></param>
- public object GetPropertyValue(object info, string field)
- {
- if (info == null) return null;
- Type t = info.GetType();
- IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
- return property.First().GetValue(info, null);
- }
- #endregion
-
-
- }
-
- //命令实体类
- 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;
- }
- }
-
-
- }
|