You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainclass.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <QCoreApplication>
  2. #include <QStringList>
  3. #include <QVariant>
  4. #include <iostream>
  5. #include <sysexits.h>
  6. #include "qcommandlineparser.h"
  7. #include "mainclass.h"
  8. #include "gpiomanager.h"
  9. #include "randommanager.h"
  10. MainClass::MainClass(QObject *parent)
  11. : QObject(parent)
  12. , m_input(nullptr)
  13. , m_device(Gpio)
  14. , m_address(QHostAddress::Any)
  15. , m_port(39415)
  16. , m_verbose(false)
  17. , m_interval(100)
  18. , m_lastTime(0)
  19. {
  20. }
  21. void MainClass::main()
  22. {
  23. getOpts();
  24. InputManager* input = nullptr;
  25. if (m_device == Gpio)
  26. input = new GpioManager(this);
  27. else if (m_device == Random)
  28. input = new RandomManager(this);
  29. if (!input->init(m_channels))
  30. {
  31. std::cerr << "Failed to initialize device" << std::endl;
  32. exit(1);
  33. return;
  34. }
  35. auto server = new ServerManager(this);
  36. if (!server->init(m_address, m_port))
  37. {
  38. std::cerr << "Failed to bind socket" << std::endl;
  39. exit(2);
  40. return;
  41. }
  42. m_input = new InputBusiness(input, m_channels, m_interval, server);
  43. if (m_verbose)
  44. connect(m_input, SIGNAL(newValues(InputValues,quint64)), this, SLOT(newValues(InputValues,quint64)));
  45. m_input->start();
  46. }
  47. void MainClass::newValues(InputValues values, quint64 timestamp)
  48. {
  49. std::cout << timestamp << ":";
  50. foreach (auto value, values) {
  51. std::cout << " " << value .first.toLongLong() << ":" << value.second.toInt();
  52. }
  53. std::cout << "\n";
  54. }
  55. void MainClass::getOpts()
  56. {
  57. QCommandLineParser parser;
  58. parser.setApplicationDescription("Server for GPIO monitoring");
  59. parser.addHelpOption();
  60. parser.addVersionOption();
  61. QCommandLineOption device((QStringList() << "d" << "device"), "device to use [gpio|rand]", "DEVICE", "gpio");
  62. parser.addOption(device);
  63. QCommandLineOption channel((QStringList() << "c" << "channel"), "channel to monitor", "CHANNEL");
  64. parser.addOption(channel);
  65. QCommandLineOption address((QStringList() << "a" << "address"), "address to bind socket", "ADDRESS", m_address.toString());
  66. parser.addOption(address);
  67. QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
  68. parser.addOption(port);
  69. QCommandLineOption verbose((QStringList() << "verbose"), "enable verbose mode");
  70. parser.addOption(verbose);
  71. QCommandLineOption interval((QStringList() << "i" << "interval"), "interval between measures in usec", "INTERVAL", QString::number(m_interval));
  72. parser.addOption(interval);
  73. parser.process(*qApp);
  74. auto dev = parser.value(device);
  75. if (dev == "gpio")
  76. m_device = Gpio;
  77. else if (dev == "rand" || dev == "random")
  78. m_device = Random;
  79. else
  80. {
  81. std::cerr << "Invalid device" << std::endl;
  82. parser.showHelp(EX_USAGE);
  83. }
  84. foreach (auto channel, parser.values(channel))
  85. m_channels.append(channel);
  86. if (m_channels.empty())
  87. {
  88. std::cerr << "At least one channel is required" << std::endl;
  89. parser.showHelp(EX_USAGE);
  90. }
  91. m_address = parser.value(address);
  92. if (m_address.isNull())
  93. {
  94. std::cerr << "Invalid address" << std::endl;
  95. parser.showHelp(EX_USAGE);
  96. }
  97. bool ok = false;
  98. m_port = parser.value(port).toInt(&ok);
  99. if (!ok || m_port <= 0 || m_port > 653535)
  100. {
  101. std::cerr << "Invalid port" << std::endl;
  102. parser.showHelp(EX_USAGE);
  103. }
  104. m_interval = parser.value(interval).toInt(&ok);
  105. if (!ok || m_interval < 0)
  106. {
  107. std::cerr << "Invalid interval" << std::endl;
  108. parser.showHelp(EX_USAGE);
  109. }
  110. m_verbose = parser.isSet(verbose);
  111. }