终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataBus.cs 10 KiB

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