source: pacpusframework/trunk/examples/ProducerConsumerExample/ConsumerExample.cpp@ 363

Last change on this file since 363 was 312, checked in by Marek Kurdej, 10 years ago

ComponentBase: added addParameter.

File size: 2.4 KB
Line 
1#include "ConsumerExample.h"
2
3#include <Pacpus/kernel/ComponentFactory.h>
4#include <Pacpus/kernel/Log.h>
5#include <Pacpus/kernel/InputOutputInterface.h>
6
7using namespace pacpus;
8using namespace std;
9
10DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ConsumerExample");
11
12PACPUS_REGISTER_COMPONENT(ConsumerExample);
13
14ConsumerExample::ConsumerExample(QString name)
15 : ComponentBase(name)
16{
17 LOG_TRACE("constructor(" << name << ")");
18
19 addParameter("output-path", value<std::string>(&mOutputFileName)->default_value("consumer.txt"), "set output file path");
20}
21
22ConsumerExample::~ConsumerExample()
23{
24 LOG_TRACE("destructor");
25}
26
27void ConsumerExample::addInputs()
28{
29 addInput<QImage, ConsumerExample>("image", &ConsumerExample::process);
30}
31
32void ConsumerExample::addOutputs()
33{
34 // empty: no outputs
35}
36
37ComponentBase::COMPONENT_CONFIGURATION
38ConsumerExample::configureComponent(XmlComponentConfig /*config*/)
39{
40 PACPUS_LOG_FUNCTION();
41
42 // load XML parameters -- NOT NEEDED - loaded by boost::program_options (used addParameters())
43
44 LOG_INFO("component '" << getName() << "' configured");
45 return ComponentBase::CONFIGURED_OK;
46}
47
48void ConsumerExample::startActivity()
49{
50 LOG_TRACE(Q_FUNC_INFO);
51 m_counter = 0;
52
53 m_file.open(mOutputFileName.c_str(), std::ios_base::out | std::ios_base::app);
54 if (!m_file.is_open()) {
55 LOG_ERROR("file '" << mOutputFileName << "' cannot be opened");
56 }
57
58 setState(MONITOR_OK);
59 LOG_INFO("started component '" << getName() << "'");
60
61 moveToThread(&mThread);
62 mThread.start();
63}
64
65void ConsumerExample::stopActivity()
66{
67 LOG_TRACE(Q_FUNC_INFO);
68
69 m_file.close();
70 setState(STOPPED);
71 LOG_INFO("stopped component '" << getName() << "'");
72 QMetaObject::invokeMethod(&mThread, "quit");
73}
74
75void ConsumerExample::process(const QImage& matrix)
76{
77 m_file << ++m_counter << " " << road_time() << "\n" << std::flush;
78
79 LOG_INFO("Received QIMage: "
80 << "size = " << matrix.size().width()<< " x " << matrix.size().height()
81 );
82
83 // DO PROCESSING
84
85 setState(MONITOR_OK);
86}
87
88void testPacpusTypedEventConverts()
89{
90 PacpusTypedEvent<float> floatEvent(TYPED_EVENT, 1.0f);
91 PacpusTypedEvent<double> doubleEvent = floatEvent;
92
93 PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2);
94 //PacpusTypedEvent<QImage> imageEvent = intEvent; // should not compile
95}
Note: See TracBrowser for help on using the repository browser.