[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 |
|
---|
| 10 | using namespace pacpus;
|
---|
| 11 | using namespace std;
|
---|
| 12 |
|
---|
| 13 | DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
|
---|
| 14 |
|
---|
| 15 | /// Construct the factory
|
---|
| 16 | static ComponentFactory<ProducerExample> sFactory("ProducerExample");
|
---|
| 17 |
|
---|
| 18 | static const char * outputFileName = "producer.txt";
|
---|
| 19 |
|
---|
| 20 | ProducerExample::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 |
|
---|
| 32 | ProducerExample::~ProducerExample()
|
---|
| 33 | {
|
---|
| 34 | LOG_TRACE("destructor");
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | ComponentBase::COMPONENT_CONFIGURATION
|
---|
| 38 | ProducerExample::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 |
|
---|
| 46 | void ProducerExample::addInputs()
|
---|
| 47 | {
|
---|
| 48 | // empty: no inputs
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void ProducerExample::addOutputs()
|
---|
| 52 | {
|
---|
| 53 | addOutput<QImage, ProducerExample>("image");
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void 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 |
|
---|
| 68 | void 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 |
|
---|
| 77 | void 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 | }
|
---|