source: pacpusframework/trunk/examples/ProducerConsumerExample/ProducerExample.cpp@ 176

Last change on this file since 176 was 176, checked in by Marek Kurdej, 11 years ago

Added: addParameters() method in ComponentBase using Boost.Program_Options.
Each component can declare its parameters and they will be read automatically before configureComponent() method.
See example ProducerConsumerExample constructors.

File size: 2.6 KB
RevLine 
[163]1#include "ProducerExample.h"
2
3#include <fstream>
4#include <Pacpus/kernel/ComponentFactory.h>
5#include <Pacpus/kernel/ComponentManager.h>
6#include <Pacpus/kernel/InputOutputInterface.h>
7#include <Pacpus/kernel/Log.h>
8#include <QColor>
9
10using namespace pacpus;
11using namespace std;
12
13DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
14
15/// Construct the factory
16static ComponentFactory<ProducerExample> sFactory("ProducerExample");
17
18static const char * outputFileName = "producer.txt";
19
20ProducerExample::ProducerExample(QString name)
21 : ComponentBase(name)
22{
23 LOG_TRACE("constructor(" << name << ")");
[176]24
25 namespace po = boost::program_options;
26
27 addParameters()
28 ("output-path", po::value<std::string>(&mOutputFileName)->default_value("producer.txt"), "set output file path")
29 ;
[163]30}
31
32ProducerExample::~ProducerExample()
33{
34 LOG_TRACE("destructor");
35}
36
37ComponentBase::COMPONENT_CONFIGURATION
38ProducerExample::configureComponent(XmlComponentConfig /*config*/)
39{
[176]40 PACPUS_LOG_FUNCTION();
[163]41
[176]42 LOG_INFO("component '" << getName() << "' configured");
[163]43 return ComponentBase::CONFIGURED_OK;
44}
45
46void ProducerExample::addInputs()
47{
48 // empty: no inputs
49}
50
51void ProducerExample::addOutputs()
52{
53 addOutput<QImage, ProducerExample>("image");
54}
55
56void ProducerExample::startActivity()
57{
58 LOG_TRACE(Q_FUNC_INFO);
59
60 //Q_ASSERT(input);
61
62 start();
63 setActive(true);
64 setState(MONITOR_OK);
[176]65 LOG_INFO("started component '" << getName() << "'");
[163]66}
67
68void ProducerExample::stopActivity()
69{
70 LOG_TRACE(Q_FUNC_INFO);
71
72 setActive(false);
73 setState(STOPPED);
[176]74 LOG_INFO("stopped component '" << getName() << "'");
[163]75}
76
77void ProducerExample::run()
78{
[165]79 unsigned int counter = 1;
80 int waitTimeMicros = 150 * 1000;
[163]81
82 std::fstream file(outputFileName, std::ios_base::out | std::ios_base::app);
83 if (!file.is_open()) {
84 LOG_ERROR("file '" << outputFileName << "'cannot be opened");
85 }
86
87 QImage mat(10000, 1000, QImage::Format_RGB32);
88 //mat.fill( qRgb(189, 149, 39));
89
90 OutputInterface<QImage, ProducerExample> * imageOutput =
91 getTypedOutput<QImage, ProducerExample>("image");
92
93 while (isActive()) {
94 //mat.setPixel(0,0,i);
[165]95 LOG_INFO("Sent QImage: "
96 << "size = " << mat.size().width()<< " x " << mat.size().height()
97 );
[163]98
99 if (imageOutput && imageOutput->hasConnection()) {
100 imageOutput->send(mat);
101 }
102
[165]103 LOG_INFO("Sent data=" << counter << ", time=" << road_time());
104 file << counter << " " << road_time() << "\n" << std::flush;
[163]105
[165]106 usleep(waitTimeMicros);
[163]107 ++counter;
108 setState(MONITOR_OK);
109 }
110
111 file.close();
112}
Note: See TracBrowser for help on using the repository browser.