[163] | 1 | #include "ProducerExample.h"
|
---|
| 2 |
|
---|
| 3 | #include <Pacpus/kernel/ComponentFactory.h>
|
---|
| 4 | #include <Pacpus/kernel/ComponentManager.h>
|
---|
| 5 | #include <Pacpus/kernel/InputOutputInterface.h>
|
---|
| 6 | #include <Pacpus/kernel/Log.h>
|
---|
| 7 |
|
---|
[291] | 8 | #include <boost/bind.hpp>
|
---|
| 9 | #include <boost/thread.hpp>
|
---|
| 10 |
|
---|
[163] | 11 | using namespace pacpus;
|
---|
| 12 | using namespace std;
|
---|
| 13 |
|
---|
| 14 | DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
|
---|
| 15 |
|
---|
[288] | 16 | PACPUS_REGISTER_COMPONENT(ProducerExample);
|
---|
[163] | 17 |
|
---|
| 18 | static const char * outputFileName = "producer.txt";
|
---|
| 19 |
|
---|
| 20 | ProducerExample::ProducerExample(QString name)
|
---|
| 21 | : ComponentBase(name)
|
---|
[291] | 22 | , mInterval(boost::posix_time::milliseconds(150))
|
---|
| 23 | , mTimer(mIo, mInterval)
|
---|
[163] | 24 | {
|
---|
[291] | 25 | PACPUS_LOG_FUNCTION();
|
---|
[176] | 26 |
|
---|
| 27 | namespace po = boost::program_options;
|
---|
| 28 |
|
---|
| 29 | addParameters()
|
---|
| 30 | ("output-path", po::value<std::string>(&mOutputFileName)->default_value("producer.txt"), "set output file path")
|
---|
| 31 | ;
|
---|
[163] | 32 | }
|
---|
| 33 |
|
---|
| 34 | ProducerExample::~ProducerExample()
|
---|
| 35 | {
|
---|
[291] | 36 | PACPUS_LOG_FUNCTION();
|
---|
[163] | 37 | }
|
---|
| 38 |
|
---|
[291] | 39 | ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/)
|
---|
[163] | 40 | {
|
---|
[176] | 41 | PACPUS_LOG_FUNCTION();
|
---|
[163] | 42 |
|
---|
[176] | 43 | LOG_INFO("component '" << getName() << "' configured");
|
---|
[163] | 44 | return ComponentBase::CONFIGURED_OK;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void ProducerExample::addInputs()
|
---|
| 48 | {
|
---|
| 49 | // empty: no inputs
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void ProducerExample::addOutputs()
|
---|
| 53 | {
|
---|
| 54 | addOutput<QImage, ProducerExample>("image");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | void ProducerExample::startActivity()
|
---|
| 58 | {
|
---|
[291] | 59 | PACPUS_LOG_FUNCTION();
|
---|
[163] | 60 |
|
---|
[291] | 61 | using boost::bind;
|
---|
| 62 | using boost::ref;
|
---|
| 63 |
|
---|
| 64 | std::fstream mFile(outputFileName, std::ios_base::out | std::ios_base::app);
|
---|
| 65 | if (!mFile.is_open()) {
|
---|
| 66 | LOG_ERROR("file '" << outputFileName << "' cannot be opened");
|
---|
| 67 | return;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | mImageOutput = getTypedOutput<QImage, ProducerExample>("image");
|
---|
| 71 |
|
---|
[163] | 72 | setActive(true);
|
---|
| 73 | setState(MONITOR_OK);
|
---|
[176] | 74 | LOG_INFO("started component '" << getName() << "'");
|
---|
[291] | 75 |
|
---|
| 76 | int counter = 0;
|
---|
| 77 | mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter)));
|
---|
| 78 | boost::thread t(bind(&boost::asio::io_service::run, ref(mIo)));
|
---|
| 79 | //mIo.run();
|
---|
[163] | 80 | }
|
---|
| 81 |
|
---|
| 82 | void ProducerExample::stopActivity()
|
---|
| 83 | {
|
---|
[291] | 84 | PACPUS_LOG_FUNCTION();
|
---|
[163] | 85 |
|
---|
| 86 | setActive(false);
|
---|
| 87 | setState(STOPPED);
|
---|
[176] | 88 | LOG_INFO("stopped component '" << getName() << "'");
|
---|
[291] | 89 |
|
---|
| 90 | mFile.close();
|
---|
[163] | 91 | }
|
---|
| 92 |
|
---|
[291] | 93 | void ProducerExample::produce(int& counter)
|
---|
[163] | 94 | {
|
---|
[291] | 95 | if (!isActive()) {
|
---|
| 96 | return;
|
---|
[163] | 97 | }
|
---|
[291] | 98 |
|
---|
| 99 | QImage mat(10, 10, QImage::Format_RGB32);
|
---|
[163] | 100 | //mat.fill( qRgb(189, 149, 39));
|
---|
[291] | 101 | //mat.setPixel(0,0,i);
|
---|
| 102 | LOG_INFO("Sending QImage: "
|
---|
| 103 | << "size = " << mat.size().width()<< " x " << mat.size().height()
|
---|
| 104 | );
|
---|
[163] | 105 |
|
---|
[291] | 106 | checkedSend(mImageOutput, mat);
|
---|
| 107 | LOG_INFO("Sent data=" << counter << ", time=" << road_time());
|
---|
| 108 | mFile << counter << " " << road_time() << "\n" << std::flush;
|
---|
| 109 | ++counter;
|
---|
| 110 | setState(MONITOR_OK);
|
---|
| 111 |
|
---|
| 112 | using boost::bind;
|
---|
| 113 | using boost::ref;
|
---|
| 114 | mTimer.expires_at(mTimer.expires_at() + mInterval);
|
---|
| 115 | mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter)));
|
---|
[163] | 116 | }
|
---|