终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataVClient.cs 12 KiB

2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. 
  2. using BPA.Message.Enum;
  3. using BPA.Message.IOT;
  4. using BPASmartClient.Business;
  5. using BPASmartClient.Device;
  6. using BPASmartClient.Helper;
  7. using BPASmartClient.Message;
  8. using BPASmartDatavDeviceClient.IoT;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  13. namespace BPASmartClient.IoT
  14. {
  15. /// <summary>
  16. /// DataV客户端数据中心
  17. /// </summary>
  18. public class DataVClient
  19. {
  20. #region 单例模式
  21. public static DataVClient init = null;
  22. public static DataVClient GetInstance()
  23. {
  24. if (init == null)
  25. {
  26. init = new DataVClient();
  27. }
  28. return init;
  29. }
  30. /// <summary>
  31. /// 显示定义构造函数为私有,表示只允许自己实例化自己
  32. /// </summary>
  33. public DataVClient()
  34. {
  35. DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
  36. ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  37. DeviceName = System.Configuration.ConfigurationManager.AppSettings["DeviceName"].ToString();
  38. ProductKey = System.Configuration.ConfigurationManager.AppSettings["ProductKey"].ToString();
  39. DeviceSecret = System.Configuration.ConfigurationManager.AppSettings["DeviceSecret"].ToString();
  40. StartupMode = System.Configuration.ConfigurationManager.AppSettings["StartupMode"].ToString();
  41. BroadcastPubTopic= System.Configuration.ConfigurationManager.AppSettings["BroadcastPubTopic"].ToString();
  42. }
  43. #endregion
  44. #region 公有变量
  45. public string StartupMode { set; get; }
  46. public string DeviceName { set; get; }
  47. public string ProductKey { set; get; }
  48. public string DeviceSecret { set; get; }
  49. public string BroadcastPubTopic { set; get; }
  50. /// <summary>
  51. /// DataV 服务地址
  52. /// </summary>
  53. public string DataVApiAddress { set; get; }
  54. /// <summary>
  55. /// 客户端ID
  56. /// </summary>
  57. public string ClientId { set; get; }
  58. /// <summary>
  59. /// MQTT上报集合
  60. /// </summary>
  61. public DataVReport DeviceDataV = new DataVReport();
  62. /// <summary>
  63. /// 大屏上报Model
  64. /// </summary>
  65. public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
  66. /// <summary>
  67. /// key值
  68. /// </summary>
  69. public Dictionary<string,string> keyValues = new Dictionary<string, string>();
  70. #endregion
  71. #region API调用
  72. /// <summary>
  73. /// 增加告警信息
  74. /// </summary>
  75. /// <param name="alarmTable"></param>
  76. /// <returns>返回ID</returns>
  77. public string HttpAddAlarm(AlarmTable alarmTable)
  78. {
  79. try
  80. {
  81. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  82. {
  83. alarmTable.ClientId = ClientId;
  84. alarmTable.devicename = DeviceDataV.deviceTable.devicename;
  85. DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(alarmTable));
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageLog.GetInstance.Show(ex.Message);
  91. }
  92. return alarmTable.KeyID;
  93. }
  94. /// <summary>
  95. /// 增加日志信息
  96. /// </summary>
  97. /// <param name="alarmTable"></param>
  98. /// <returns>返回ID</returns>
  99. public string HttpAddLog(LogTable logTable)
  100. {
  101. string id = string.Empty;
  102. try
  103. {
  104. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable!=null)
  105. {
  106. logTable.ClientId = ClientId;
  107. logTable.devicename = DeviceDataV.deviceTable.devicename;
  108. DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(logTable));
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. MessageLog.GetInstance.Show(ex.Message);
  114. }
  115. return id;
  116. }
  117. #endregion
  118. #region 公用
  119. /// <summary>
  120. /// 释放
  121. /// </summary>
  122. public void Dispose()
  123. {
  124. ThreadManage.GetInstance().StopTask("DataV数据上报");
  125. }
  126. /// <summary>
  127. /// 初始化
  128. /// </summary>
  129. public void Initialize()
  130. {
  131. string message = string.Empty;
  132. if (StartupMode == "API")
  133. {
  134. if (DeviceDataV.Initialize(DataVApiAddress, ClientId, "", ref message))
  135. {
  136. ProductKey = DeviceDataV.deviceTable.productkey;
  137. DeviceName = DeviceDataV.deviceTable.devicename;
  138. DeviceSecret = DeviceDataV.deviceTable.devicesecret;
  139. DeviceDataV.DataVMessageAction += DevIOTActionHandler;
  140. MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
  141. }
  142. else
  143. {
  144. MessageLog.GetInstance.ShowEx(message);
  145. }
  146. }
  147. else
  148. {
  149. if (DeviceDataV.InitializeNo(ProductKey, DeviceName, DeviceSecret, ref message))
  150. {
  151. MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
  152. }
  153. else
  154. MessageLog.GetInstance.ShowEx(message);
  155. }
  156. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  157. {
  158. device.AddErrorAction+= AddErrorAction;
  159. device.DeleteErrorAction += DeleteErrorAction;
  160. });
  161. }
  162. /// <summary>
  163. /// 启动DataV数据上报
  164. /// </summary>
  165. public void Start()
  166. {
  167. ThreadManage.GetInstance().StartLong(new Action(() =>
  168. {
  169. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  170. {
  171. List<object> dataVNode = new List<object>();
  172. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  173. {
  174. var obj = new
  175. {
  176. DeviceId = device.DeviceId.ToString(),
  177. devicename = DeviceDataV.deviceTable.devicename,
  178. Name = device.Name,
  179. DeviceType = device.DeviceType.ToString(),
  180. IsBusy = device.IsBusy ? "忙碌" : "空闲",
  181. IsBusyColor = device.IsBusy ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  182. IsHealth = device.IsHealth ? "健康" : "故障",
  183. IsHealthColor = !device.IsHealth ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  184. Status = device.Status.GetIOTStatus(),
  185. gjxx = device.GetError(),
  186. rzxx = device.GetLog(),
  187. VariableMonitor = device.GetVariableMonitor(),
  188. };
  189. dataVNode.Add(obj);
  190. });
  191. if (dataVNode.Count > 0)
  192. {
  193. iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(new { data = dataVNode });
  194. DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson());
  195. }
  196. }
  197. Thread.Sleep(3000);
  198. }), "DataV数据上报", true);
  199. }
  200. #endregion
  201. #region 私有
  202. /// <summary>
  203. /// 增加告警
  204. /// </summary>
  205. /// <param name="obj"></param>
  206. private void AddErrorAction(int Devid, object obj)
  207. {
  208. string id = Guid.NewGuid().ToString();
  209. HttpAddAlarm(new AlarmTable
  210. {
  211. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  212. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  213. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  214. AlarmVla = "告警",
  215. DeviceId = Devid.ToString(),
  216. KeyID = id,
  217. });
  218. keyValues[GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString()] =id ;
  219. MessageLog.GetInstance.AddDeviceAlarmLogShow(GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString(),id);
  220. }
  221. /// <summary>
  222. /// 删除告警
  223. /// </summary>
  224. /// <param name="obj"></param>
  225. private void DeleteErrorAction(int Devid, object obj)
  226. {
  227. string message = GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString();
  228. if (keyValues.ContainsKey(message))
  229. {
  230. HttpAddAlarm(new AlarmTable
  231. {
  232. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  233. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  234. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  235. AlarmVla = "告警",
  236. DeviceId= Devid.ToString(),
  237. KeyID = keyValues[message],
  238. State="n"
  239. });
  240. MessageLog.GetInstance.DeleteDeviceAlarmLogShow(message, keyValues[message]);
  241. }
  242. }
  243. /// <summary>
  244. /// 接收云端消息
  245. /// </summary>
  246. /// <param name="topic"></param>
  247. /// <param name="message"></param>
  248. private void DevIOTActionHandler(string deviceId, string topic, string message)
  249. {
  250. if (DeviceDataV.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  251. {
  252. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  253. if (iOTCommand.deviceName == DeviceDataV.deviceTable.devicename)
  254. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  255. }
  256. }
  257. /// <summary>
  258. /// 获取某个对象中的属性值
  259. /// </summary>
  260. /// <param name="info"></param>
  261. /// <param name="field"></param>
  262. public object GetPropertyValue(object info, string field)
  263. {
  264. if (info == null) return null;
  265. Type t = info.GetType();
  266. IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
  267. return property.First().GetValue(info, null);
  268. }
  269. #endregion
  270. }
  271. //命令实体类
  272. public class IOTCommandModel
  273. {
  274. /// <summary>
  275. /// 设备名称
  276. /// </summary>
  277. public string deviceName
  278. {
  279. get;
  280. set;
  281. }
  282. /// <summary>
  283. /// 命令名称:0 控制类 1 设置属性 2 通知信息类
  284. /// </summary>
  285. public int CommandName
  286. {
  287. get;
  288. set;
  289. }
  290. /// <summary>
  291. /// 命令变量:执行变量 key为属性或时间 value为值或者消息
  292. /// </summary>
  293. public Dictionary<string, string> CommandValue
  294. {
  295. get;
  296. set;
  297. }
  298. }
  299. }