终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

237 righe
10 KiB

  1. using BPASmartClient.Bus.DataBus;
  2. using BPASmartClient.Helper;
  3. using BPASmartClinet.DataBusName;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. /* ***********************************************
  11.  * subject 数据总线,总线入口,后续按类型分发
  12.  * author 张原川
  13.  * date   2019/6/3 9:49:03
  14.  * ***********************************************/
  15. namespace LandStation.Bus.DataBus
  16. {
  17. public class DataBus : Singleton<DataBus>, IDisposable
  18. {
  19. //原始数据总线
  20. private IDataBus<byte> _dataBus_rawdata;
  21. //MAVLINK消息总线
  22. private IDataBus<object> _dataBus_mavMessage;
  23. //参数总线
  24. private IDataBus<object> _dataBus_parameter;
  25. //调试消息总线
  26. private IDataBus<string> _dataBus_message;
  27. /// <summary>
  28. /// 数据总线初始化
  29. /// </summary>
  30. public void Initialize()
  31. {
  32. _dataBus_rawdata = new DataBus_Byte();
  33. _dataBus_mavMessage = new DataBus_Currency<object>();
  34. _dataBus_parameter = new DataBus_Currency<object>();
  35. _dataBus_message = new DataBus_Currency<string>();
  36. }
  37. /// <summary>
  38. /// 总线开启
  39. /// </summary>
  40. public void Start()
  41. {
  42. Executer.GetInstance().Start(_dataBus_rawdata.StartBus, ActionKey.DataBus_Rawdata);
  43. Executer.GetInstance().Start(_dataBus_mavMessage.StartBus, ActionKey.DataBus_MAVLinkMessage);
  44. Executer.GetInstance().Start(_dataBus_message.StartBus, ActionKey.DataBus_MessageData);
  45. Executer.GetInstance().Start(_dataBus_parameter.StartBus, ActionKey.DataBus_ParameterData);
  46. //Executer.GetInstance().Start(delegate ()
  47. //{
  48. // while (true)
  49. // {
  50. // //Console.WriteLine("原始数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_rawdata.MultSubscriberCount, _dataBus_rawdata.SingleSubscriberCount);
  51. // //Console.WriteLine("MAVLink数据=====>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_mavMessage.MultSubscriberCount, _dataBus_mavMessage.SingleSubscriberCount);
  52. // //Console.WriteLine("状态数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_status.MultSubscriberCount, _dataBus_status.SingleSubscriberCount);
  53. // //Console.WriteLine("参数数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_parameter.MultSubscriberCount, _dataBus_parameter.SingleSubscriberCount);
  54. // //Console.WriteLine("消息数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_message.MultSubscriberCount, _dataBus_message.SingleSubscriberCount);
  55. // //Console.WriteLine("===============================================================================================");
  56. // Console.WriteLine("原始数据========>{0}", _dataBus_rawdata.DataCount);
  57. // Console.WriteLine("MAVLink数据=====>{0}", _dataBus_mavMessage.DataCount);
  58. // Console.WriteLine("状态数据========>{0}", _dataBus_status.DataCount);
  59. // Console.WriteLine("参数数据========>{0}", _dataBus_parameter.DataCount);
  60. // Console.WriteLine("消息数据========>{0}", _dataBus_message.DataCount);
  61. // Console.WriteLine("===============================================================================================");
  62. // Thread.Sleep(50);
  63. // }
  64. //}, ActionKey.DataBus_Monitor, "数据总线监控");
  65. }
  66. /// <summary>
  67. /// 根据数据类型放入数据到对应总线
  68. /// </summary>
  69. /// <typeparam name="TData">数据实际类型</typeparam>
  70. /// <param name="data">数据</param>
  71. /// <param name="simpleData">数据业务类型</param>
  72. public void Put<TData>(object data, SimpleDataType simpleData)
  73. {
  74. switch (simpleData)
  75. {
  76. case SimpleDataType.RAW_DATA:
  77. if (data is byte)
  78. _dataBus_rawdata.Put((byte)data);
  79. if (data is byte[])
  80. _dataBus_rawdata.Put((byte[])data);
  81. break;
  82. case SimpleDataType.MAV_MESSAGE_DATA:
  83. _dataBus_mavMessage.Put((object)data);
  84. break;
  85. case SimpleDataType.PARAMETER:
  86. _dataBus_parameter.Put((object)data);
  87. break;
  88. case SimpleDataType.MESSAGE_DATA:
  89. _dataBus_message.Put((string)data);
  90. break;
  91. }
  92. }
  93. /// <summary>
  94. /// 订阅数据
  95. /// </summary>
  96. /// <typeparam name="TData">数据实际类型</typeparam>
  97. /// <param name="action">接收数据推送回调</param>
  98. /// <param name="dataType">数据业务类型</param>
  99. public void Subscribe<TData>(Action<TData> action, SimpleDataType dataType)
  100. {
  101. switch (dataType)
  102. {
  103. case SimpleDataType.RAW_DATA:
  104. ((ISimpleDataBus<TData>)_dataBus_rawdata).Subscribe(action);
  105. break;
  106. case SimpleDataType.MAV_MESSAGE_DATA:
  107. ((ISimpleDataBus<TData>)_dataBus_mavMessage).Subscribe(action);
  108. break;
  109. case SimpleDataType.PARAMETER:
  110. ((ISimpleDataBus<TData>)_dataBus_parameter).Subscribe(action);
  111. break;
  112. case SimpleDataType.MESSAGE_DATA:
  113. ((ISimpleDataBus<TData>)_dataBus_message).Subscribe(action);
  114. break;
  115. }
  116. }
  117. /// <summary>
  118. /// 订阅数据
  119. /// </summary>
  120. /// <typeparam name="TData">数据实际类型</typeparam>
  121. /// <param name="action">接收数据推送回调</param>
  122. /// <param name="dataType">数据业务类型</param>
  123. public void UnSubscribe<TData>(Action<TData> action, SimpleDataType dataType)
  124. {
  125. switch (dataType)
  126. {
  127. case SimpleDataType.RAW_DATA:
  128. ((ISimpleDataBus<TData>)_dataBus_rawdata).UnSubcribe(action);
  129. break;
  130. case SimpleDataType.MAV_MESSAGE_DATA:
  131. ((ISimpleDataBus<TData>)_dataBus_mavMessage).UnSubcribe(action);
  132. break;
  133. case SimpleDataType.PARAMETER:
  134. ((ISimpleDataBus<TData>)_dataBus_parameter).UnSubcribe(action);
  135. break;
  136. case SimpleDataType.MESSAGE_DATA:
  137. ((ISimpleDataBus<TData>)_dataBus_message).UnSubcribe(action);
  138. break;
  139. }
  140. }
  141. /// <summary>
  142. /// 订阅数据
  143. /// </summary>
  144. /// <typeparam name="TData">数据实际类型</typeparam>
  145. /// <param name="action">接收数据推送回调</param>
  146. /// <param name="dataType">数据业务类型</param>
  147. public void Subscribe<TData>(Action<TData[]> action, SimpleDataType dataType)
  148. {
  149. switch (dataType)
  150. {
  151. case SimpleDataType.RAW_DATA:
  152. ((ISimpleDataBus<TData>)_dataBus_rawdata).Subscribe(action);
  153. break;
  154. case SimpleDataType.MAV_MESSAGE_DATA:
  155. ((ISimpleDataBus<TData>)_dataBus_mavMessage).Subscribe(action);
  156. break;
  157. case SimpleDataType.PARAMETER:
  158. ((ISimpleDataBus<TData>)_dataBus_parameter).Subscribe(action);
  159. break;
  160. case SimpleDataType.MESSAGE_DATA:
  161. ((ISimpleDataBus<TData>)_dataBus_message).Subscribe(action);
  162. break;
  163. }
  164. }
  165. /// <summary>
  166. /// 订阅数据
  167. /// </summary>
  168. /// <typeparam name="TData">数据实际类型</typeparam>
  169. /// <param name="action">接收数据推送回调</param>
  170. /// <param name="dataType">数据业务类型</param>
  171. public void UnSubscribe<TData>(Action<TData[]> action, SimpleDataType dataType)
  172. {
  173. switch (dataType)
  174. {
  175. case SimpleDataType.RAW_DATA:
  176. ((ISimpleDataBus<TData>)_dataBus_rawdata).UnSubcribe(action);
  177. break;
  178. case SimpleDataType.MAV_MESSAGE_DATA:
  179. ((ISimpleDataBus<TData>)_dataBus_mavMessage).UnSubcribe(action);
  180. break;
  181. case SimpleDataType.PARAMETER:
  182. ((ISimpleDataBus<TData>)_dataBus_parameter).UnSubcribe(action);
  183. break;
  184. case SimpleDataType.MESSAGE_DATA:
  185. ((ISimpleDataBus<TData>)_dataBus_message).UnSubcribe(action);
  186. break;
  187. }
  188. }
  189. /// <summary>
  190. /// 获取一个数据
  191. /// </summary>
  192. /// <typeparam name="TData">数据实际类型</typeparam>
  193. /// <param name="dataType">数据业务类型</param>
  194. /// <returns>一个数据</returns>
  195. public byte GetRawdata()
  196. {
  197. return ((IGivenDataBus<byte>)_dataBus_rawdata).Get();
  198. }
  199. /// <summary>
  200. /// 获取多个数据
  201. /// </summary>
  202. /// <typeparam name="TData">数据实际类型</typeparam>
  203. /// <param name="dataType">数据业务类型</param>
  204. /// <param name="length">获取数据长度</param>
  205. /// <returns>一个数据</returns>
  206. public byte[] GetRawdata(int length)
  207. {
  208. return ((IGivenDataBus<byte>)_dataBus_rawdata).Get(length);
  209. }
  210. /// <summary>
  211. /// 释放数据总线
  212. /// </summary>
  213. public void Dispose()
  214. {
  215. _dataBus_rawdata.StopBus();
  216. _dataBus_mavMessage.StopBus();
  217. _dataBus_message.StopBus();
  218. _dataBus_parameter.StopBus();
  219. }
  220. }
  221. }