|
-
- using BPASmartClient.Bus.DataBus;
- using BPASmartClient.Helper;
- using BPASmartClinet.DataBusName;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
-
- /* ***********************************************
- * subject 数据总线,总线入口,后续按类型分发
- * author 张原川
- * date 2019/6/3 9:49:03
- * ***********************************************/
-
- namespace LandStation.Bus.DataBus
- {
- public class DataBus : Singleton<DataBus>, IDisposable
- {
- //原始数据总线
- private IDataBus<byte> _dataBus_rawdata;
- //MAVLINK消息总线
- private IDataBus<object> _dataBus_mavMessage;
- //参数总线
- private IDataBus<object> _dataBus_parameter;
- //调试消息总线
- private IDataBus<string> _dataBus_message;
-
- /// <summary>
- /// 数据总线初始化
- /// </summary>
- public void Initialize()
- {
- _dataBus_rawdata = new DataBus_Byte();
- _dataBus_mavMessage = new DataBus_Currency<object>();
- _dataBus_parameter = new DataBus_Currency<object>();
- _dataBus_message = new DataBus_Currency<string>();
- }
-
- /// <summary>
- /// 总线开启
- /// </summary>
- public void Start()
- {
- Executer.GetInstance().Start(_dataBus_rawdata.StartBus, ActionKey.DataBus_Rawdata);
- Executer.GetInstance().Start(_dataBus_mavMessage.StartBus, ActionKey.DataBus_MAVLinkMessage);
- Executer.GetInstance().Start(_dataBus_message.StartBus, ActionKey.DataBus_MessageData);
- Executer.GetInstance().Start(_dataBus_parameter.StartBus, ActionKey.DataBus_ParameterData);
-
- //Executer.GetInstance().Start(delegate ()
- //{
- // while (true)
- // {
- // //Console.WriteLine("原始数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_rawdata.MultSubscriberCount, _dataBus_rawdata.SingleSubscriberCount);
- // //Console.WriteLine("MAVLink数据=====>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_mavMessage.MultSubscriberCount, _dataBus_mavMessage.SingleSubscriberCount);
- // //Console.WriteLine("状态数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_status.MultSubscriberCount, _dataBus_status.SingleSubscriberCount);
- // //Console.WriteLine("参数数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_parameter.MultSubscriberCount, _dataBus_parameter.SingleSubscriberCount);
- // //Console.WriteLine("消息数据========>批量数据订阅数:{0},单数据订阅数:{1}", _dataBus_message.MultSubscriberCount, _dataBus_message.SingleSubscriberCount);
- // //Console.WriteLine("===============================================================================================");
- // Console.WriteLine("原始数据========>{0}", _dataBus_rawdata.DataCount);
- // Console.WriteLine("MAVLink数据=====>{0}", _dataBus_mavMessage.DataCount);
- // Console.WriteLine("状态数据========>{0}", _dataBus_status.DataCount);
- // Console.WriteLine("参数数据========>{0}", _dataBus_parameter.DataCount);
- // Console.WriteLine("消息数据========>{0}", _dataBus_message.DataCount);
- // Console.WriteLine("===============================================================================================");
- // Thread.Sleep(50);
- // }
- //}, ActionKey.DataBus_Monitor, "数据总线监控");
- }
-
- /// <summary>
- /// 根据数据类型放入数据到对应总线
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="simpleData">数据业务类型</param>
- public void Put<TData>(object data, SimpleDataType simpleData)
- {
- switch (simpleData)
- {
- case SimpleDataType.RAW_DATA:
- if (data is byte)
- _dataBus_rawdata.Put((byte)data);
- if (data is byte[])
- _dataBus_rawdata.Put((byte[])data);
- break;
- case SimpleDataType.MAV_MESSAGE_DATA:
- _dataBus_mavMessage.Put((object)data);
- break;
- case SimpleDataType.PARAMETER:
- _dataBus_parameter.Put((object)data);
- break;
- case SimpleDataType.MESSAGE_DATA:
- _dataBus_message.Put((string)data);
- break;
- }
- }
-
- /// <summary>
- /// 订阅数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="action">接收数据推送回调</param>
- /// <param name="dataType">数据业务类型</param>
- public void Subscribe<TData>(Action<TData> action, SimpleDataType dataType)
- {
- switch (dataType)
- {
- case SimpleDataType.RAW_DATA:
- ((ISimpleDataBus<TData>)_dataBus_rawdata).Subscribe(action);
- break;
- case SimpleDataType.MAV_MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_mavMessage).Subscribe(action);
- break;
- case SimpleDataType.PARAMETER:
- ((ISimpleDataBus<TData>)_dataBus_parameter).Subscribe(action);
- break;
- case SimpleDataType.MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_message).Subscribe(action);
- break;
- }
- }
-
- /// <summary>
- /// 订阅数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="action">接收数据推送回调</param>
- /// <param name="dataType">数据业务类型</param>
- public void UnSubscribe<TData>(Action<TData> action, SimpleDataType dataType)
- {
- switch (dataType)
- {
- case SimpleDataType.RAW_DATA:
- ((ISimpleDataBus<TData>)_dataBus_rawdata).UnSubcribe(action);
- break;
- case SimpleDataType.MAV_MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_mavMessage).UnSubcribe(action);
- break;
- case SimpleDataType.PARAMETER:
- ((ISimpleDataBus<TData>)_dataBus_parameter).UnSubcribe(action);
- break;
- case SimpleDataType.MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_message).UnSubcribe(action);
- break;
- }
- }
-
- /// <summary>
- /// 订阅数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="action">接收数据推送回调</param>
- /// <param name="dataType">数据业务类型</param>
- public void Subscribe<TData>(Action<TData[]> action, SimpleDataType dataType)
- {
- switch (dataType)
- {
- case SimpleDataType.RAW_DATA:
- ((ISimpleDataBus<TData>)_dataBus_rawdata).Subscribe(action);
- break;
- case SimpleDataType.MAV_MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_mavMessage).Subscribe(action);
- break;
- case SimpleDataType.PARAMETER:
- ((ISimpleDataBus<TData>)_dataBus_parameter).Subscribe(action);
- break;
- case SimpleDataType.MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_message).Subscribe(action);
- break;
- }
- }
-
- /// <summary>
- /// 订阅数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="action">接收数据推送回调</param>
- /// <param name="dataType">数据业务类型</param>
- public void UnSubscribe<TData>(Action<TData[]> action, SimpleDataType dataType)
- {
- switch (dataType)
- {
- case SimpleDataType.RAW_DATA:
- ((ISimpleDataBus<TData>)_dataBus_rawdata).UnSubcribe(action);
- break;
- case SimpleDataType.MAV_MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_mavMessage).UnSubcribe(action);
- break;
- case SimpleDataType.PARAMETER:
- ((ISimpleDataBus<TData>)_dataBus_parameter).UnSubcribe(action);
- break;
- case SimpleDataType.MESSAGE_DATA:
- ((ISimpleDataBus<TData>)_dataBus_message).UnSubcribe(action);
- break;
- }
- }
-
- /// <summary>
- /// 获取一个数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="dataType">数据业务类型</param>
- /// <returns>一个数据</returns>
- public byte GetRawdata()
- {
- return ((IGivenDataBus<byte>)_dataBus_rawdata).Get();
- }
-
- /// <summary>
- /// 获取多个数据
- /// </summary>
- /// <typeparam name="TData">数据实际类型</typeparam>
- /// <param name="dataType">数据业务类型</param>
- /// <param name="length">获取数据长度</param>
- /// <returns>一个数据</returns>
- public byte[] GetRawdata(int length)
- {
- return ((IGivenDataBus<byte>)_dataBus_rawdata).Get(length);
- }
-
- /// <summary>
- /// 释放数据总线
- /// </summary>
- public void Dispose()
- {
- _dataBus_rawdata.StopBus();
- _dataBus_mavMessage.StopBus();
- _dataBus_message.StopBus();
- _dataBus_parameter.StopBus();
- }
- }
- }
|