| 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 << ")");
|
|---|
| 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 | ;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | ProducerExample::~ProducerExample()
|
|---|
| 33 | {
|
|---|
| 34 | LOG_TRACE("destructor");
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | ComponentBase::COMPONENT_CONFIGURATION
|
|---|
| 38 | ProducerExample::configureComponent(XmlComponentConfig /*config*/)
|
|---|
| 39 | {
|
|---|
| 40 | PACPUS_LOG_FUNCTION();
|
|---|
| 41 |
|
|---|
| 42 | LOG_INFO("component '" << getName() << "' configured");
|
|---|
| 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);
|
|---|
| 65 | LOG_INFO("started component '" << getName() << "'");
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | void ProducerExample::stopActivity()
|
|---|
| 69 | {
|
|---|
| 70 | LOG_TRACE(Q_FUNC_INFO);
|
|---|
| 71 |
|
|---|
| 72 | setActive(false);
|
|---|
| 73 | setState(STOPPED);
|
|---|
| 74 | LOG_INFO("stopped component '" << getName() << "'");
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void ProducerExample::run()
|
|---|
| 78 | {
|
|---|
| 79 | unsigned int counter = 1;
|
|---|
| 80 | int waitTimeMicros = 150 * 1000;
|
|---|
| 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);
|
|---|
| 95 | LOG_INFO("Sent QImage: "
|
|---|
| 96 | << "size = " << mat.size().width()<< " x " << mat.size().height()
|
|---|
| 97 | );
|
|---|
| 98 |
|
|---|
| 99 | if (imageOutput && imageOutput->hasConnection()) {
|
|---|
| 100 | imageOutput->send(mat);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | LOG_INFO("Sent data=" << counter << ", time=" << road_time());
|
|---|
| 104 | file << counter << " " << road_time() << "\n" << std::flush;
|
|---|
| 105 |
|
|---|
| 106 | usleep(waitTimeMicros);
|
|---|
| 107 | ++counter;
|
|---|
| 108 | setState(MONITOR_OK);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | file.close();
|
|---|
| 112 | }
|
|---|