Go to the documentation of this file.00001 #ifndef DATA_STREAMER_TEMPLATE_H
00002 #define DATA_STREAMER_TEMPLATE_H
00003
00004 #include <mutex>
00005 #include <unordered_set>
00006 #include "PlotJuggler/plotdata.h"
00007 #include "PlotJuggler/pj_plugin.h"
00008
00017 class DataStreamer: public PlotJugglerPlugin
00018 {
00019 Q_OBJECT
00020 public:
00021
00022 DataStreamer():
00023 PlotJugglerPlugin()
00024 {
00025
00026 }
00027
00028 virtual bool start(QStringList*) = 0;
00029
00030 virtual void shutdown() = 0;
00031
00032 virtual bool isRunning() const = 0;
00033
00034 virtual ~DataStreamer() {}
00035
00036 std::mutex& mutex()
00037 {
00038 return _mutex;
00039 }
00040
00041 void setMaximumRange(double range);
00042
00043 virtual std::vector<QString> appendData(PlotDataMapRef& destination);
00044
00045 PlotDataMapRef& dataMap()
00046 {
00047 return _data_map;
00048 }
00049
00050 const PlotDataMapRef& dataMap() const
00051 {
00052 return _data_map;
00053 }
00054
00055 signals:
00056
00057 void clearBuffers();
00058
00059 void dataUpdated();
00060
00061 void connectionClosed();
00062
00063 private:
00064 std::mutex _mutex;
00065 PlotDataMapRef _data_map;
00066 QAction* _start_streamer;
00067 };
00068
00069 QT_BEGIN_NAMESPACE
00070
00071 #define DataStream_iid "com.icarustechnology.PlotJuggler.DataStreamer"
00072
00073
00074
00075 Q_DECLARE_INTERFACE(DataStreamer, DataStream_iid)
00076
00077 QT_END_NAMESPACE
00078
00079
00080 inline
00081 void DataStreamer::setMaximumRange(double range)
00082 {
00083 std::lock_guard<std::mutex> lock( mutex() );
00084 for (auto& it : dataMap().numeric ) {
00085 it.second.setMaximumRangeX( range );
00086 }
00087 for (auto& it: dataMap().user_defined) {
00088 it.second.setMaximumRangeX( range );
00089 }
00090 }
00091
00092 inline
00093 std::vector<QString> DataStreamer::appendData(PlotDataMapRef &destination)
00094 {
00095 PlotDataMapRef &source = _data_map;
00096
00097 std::vector<QString> added_curves;
00098 for (auto& it: _data_map.numeric)
00099 {
00100 const std::string& name = it.first;
00101 if( it.second.size()>0 && destination.numeric.count(name) == 0)
00102 {
00103 added_curves.push_back( QString::fromStdString( name ) );
00104 }
00105 }
00106
00107 for (auto& it: source.numeric)
00108 {
00109 const std::string& name = it.first;
00110 auto& source_plot = it.second;
00111 auto plot_with_same_name = destination.numeric.find(name);
00112
00113
00114 if( plot_with_same_name == destination.numeric.end() )
00115 {
00116 plot_with_same_name = destination.numeric.emplace(
00117 std::piecewise_construct,
00118 std::forward_as_tuple(name),
00119 std::forward_as_tuple(name)
00120 ).first;
00121 }
00122 auto& destination_plot = plot_with_same_name->second;
00123 for (size_t i=0; i< source_plot.size(); i++)
00124 {
00125 destination_plot.pushBack( source_plot.at(i) );
00126 }
00127 source_plot.clear();
00128 }
00129
00130 for (auto& it: source.user_defined)
00131 {
00132 const std::string& name = it.first;
00133 auto& source_plot = it.second;
00134 auto plot_with_same_name = destination.user_defined.find(name);
00135
00136
00137 if( plot_with_same_name == destination.user_defined.end() )
00138 {
00139 plot_with_same_name = destination.user_defined.emplace(
00140 std::piecewise_construct,
00141 std::forward_as_tuple(name),
00142 std::forward_as_tuple(name)
00143 ).first;
00144 }
00145 auto& destination_plot = plot_with_same_name->second;
00146 for (size_t i=0; i< source_plot.size(); i++)
00147 {
00148 destination_plot.pushBack( source_plot.at(i) );
00149 }
00150 source_plot.clear();
00151 }
00152 return added_curves;
00153 }
00154
00155 #endif
00156