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, IDisposable { //原始数据总线 private IDataBus _dataBus_rawdata; //MAVLINK消息总线 private IDataBus _dataBus_mavMessage; //参数总线 private IDataBus _dataBus_parameter; //调试消息总线 private IDataBus _dataBus_message; /// /// 数据总线初始化 /// public void Initialize() { _dataBus_rawdata = new DataBus_Byte(); _dataBus_mavMessage = new DataBus_Currency(); _dataBus_parameter = new DataBus_Currency(); _dataBus_message = new DataBus_Currency(); } /// /// 总线开启 /// 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, "数据总线监控"); } /// /// 根据数据类型放入数据到对应总线 /// /// 数据实际类型 /// 数据 /// 数据业务类型 public void Put(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; } } /// /// 订阅数据 /// /// 数据实际类型 /// 接收数据推送回调 /// 数据业务类型 public void Subscribe(Action action, SimpleDataType dataType) { switch (dataType) { case SimpleDataType.RAW_DATA: ((ISimpleDataBus)_dataBus_rawdata).Subscribe(action); break; case SimpleDataType.MAV_MESSAGE_DATA: ((ISimpleDataBus)_dataBus_mavMessage).Subscribe(action); break; case SimpleDataType.PARAMETER: ((ISimpleDataBus)_dataBus_parameter).Subscribe(action); break; case SimpleDataType.MESSAGE_DATA: ((ISimpleDataBus)_dataBus_message).Subscribe(action); break; } } /// /// 订阅数据 /// /// 数据实际类型 /// 接收数据推送回调 /// 数据业务类型 public void UnSubscribe(Action action, SimpleDataType dataType) { switch (dataType) { case SimpleDataType.RAW_DATA: ((ISimpleDataBus)_dataBus_rawdata).UnSubcribe(action); break; case SimpleDataType.MAV_MESSAGE_DATA: ((ISimpleDataBus)_dataBus_mavMessage).UnSubcribe(action); break; case SimpleDataType.PARAMETER: ((ISimpleDataBus)_dataBus_parameter).UnSubcribe(action); break; case SimpleDataType.MESSAGE_DATA: ((ISimpleDataBus)_dataBus_message).UnSubcribe(action); break; } } /// /// 订阅数据 /// /// 数据实际类型 /// 接收数据推送回调 /// 数据业务类型 public void Subscribe(Action action, SimpleDataType dataType) { switch (dataType) { case SimpleDataType.RAW_DATA: ((ISimpleDataBus)_dataBus_rawdata).Subscribe(action); break; case SimpleDataType.MAV_MESSAGE_DATA: ((ISimpleDataBus)_dataBus_mavMessage).Subscribe(action); break; case SimpleDataType.PARAMETER: ((ISimpleDataBus)_dataBus_parameter).Subscribe(action); break; case SimpleDataType.MESSAGE_DATA: ((ISimpleDataBus)_dataBus_message).Subscribe(action); break; } } /// /// 订阅数据 /// /// 数据实际类型 /// 接收数据推送回调 /// 数据业务类型 public void UnSubscribe(Action action, SimpleDataType dataType) { switch (dataType) { case SimpleDataType.RAW_DATA: ((ISimpleDataBus)_dataBus_rawdata).UnSubcribe(action); break; case SimpleDataType.MAV_MESSAGE_DATA: ((ISimpleDataBus)_dataBus_mavMessage).UnSubcribe(action); break; case SimpleDataType.PARAMETER: ((ISimpleDataBus)_dataBus_parameter).UnSubcribe(action); break; case SimpleDataType.MESSAGE_DATA: ((ISimpleDataBus)_dataBus_message).UnSubcribe(action); break; } } /// /// 获取一个数据 /// /// 数据实际类型 /// 数据业务类型 /// 一个数据 public byte GetRawdata() { return ((IGivenDataBus)_dataBus_rawdata).Get(); } /// /// 获取多个数据 /// /// 数据实际类型 /// 数据业务类型 /// 获取数据长度 /// 一个数据 public byte[] GetRawdata(int length) { return ((IGivenDataBus)_dataBus_rawdata).Get(length); } /// /// 释放数据总线 /// public void Dispose() { _dataBus_rawdata.StopBus(); _dataBus_mavMessage.StopBus(); _dataBus_message.StopBus(); _dataBus_parameter.StopBus(); } } }