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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <QCoreApplication>
  2. #include <QStringList>
  3. #include <QVariant>
  4. #include <iostream>
  5. #include <sysexits.h>
  6. #include <unistd.h>
  7. #include "qcommandlineparser.h"
  8. #include "mainclass.h"
  9. #include "gpiomanager.h"
  10. #include "randommanager.h"
  11. MainClass::MainClass(QObject *parent)
  12. : QObject(parent)
  13. , m_input(nullptr)
  14. , m_device(Gpio)
  15. , m_address(QHostAddress::Any)
  16. , m_port(39415)
  17. , m_verbose(false)
  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, server);
  43. m_timer = new QTimer(this);
  44. m_timer->setSingleShot(false);
  45. m_timer->setInterval(0);
  46. connect(m_timer, SIGNAL(timeout()), this, SLOT(maySend()));
  47. m_timer->start();
  48. }
  49. void MainClass::maySend()
  50. {
  51. auto time = getTime();
  52. if (time - m_lastTime < 100)
  53. usleep(100 - (time - m_lastTime));
  54. m_lastTime = getTime();
  55. auto values = m_input->readAndSend(time);
  56. if (m_verbose)
  57. {
  58. std::cout << time << ":";
  59. foreach (auto value, values) {
  60. std::cout << " " << value .first.toInt() << ":" << value.second.toInt();
  61. }
  62. std::cout << std::endl;
  63. }
  64. }
  65. void MainClass::getOpts()
  66. {
  67. QCommandLineParser parser;
  68. parser.setApplicationDescription("Server for GPIO monitoring");
  69. parser.addHelpOption();
  70. parser.addVersionOption();
  71. QCommandLineOption device((QStringList() << "d" << "device"), "device to use [gpio|rand]", "DEVICE", "gpio");
  72. parser.addOption(device);
  73. QCommandLineOption channel((QStringList() << "c" << "channel"), "channel to monitor", "CHANNEL");
  74. parser.addOption(channel);
  75. QCommandLineOption address((QStringList() << "a" << "address"), "address to bind socket", "ADDRESS", m_address.toString());
  76. parser.addOption(address);
  77. QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
  78. parser.addOption(port);
  79. QCommandLineOption verbose((QStringList() << "verbose"), "enable verbose mode");
  80. parser.addOption(verbose);
  81. parser.process(*qApp);
  82. auto dev = parser.value(device);
  83. if (dev == "gpio")
  84. m_device = Gpio;
  85. else if (dev == "rand" || dev == "random")
  86. m_device = Random;
  87. else
  88. {
  89. std::cerr << "Invalid device" << std::endl;
  90. parser.showHelp(EX_USAGE);
  91. }
  92. foreach (auto channel, parser.values(channel))
  93. m_channels.append(channel);
  94. if (m_channels.empty())
  95. {
  96. std::cerr << "At least one channel is required" << std::endl;
  97. parser.showHelp(EX_USAGE);
  98. }
  99. m_address = parser.value(address);
  100. if (m_address.isNull())
  101. {
  102. std::cerr << "Invalid address" << std::endl;
  103. parser.showHelp(EX_USAGE);
  104. }
  105. bool ok = false;
  106. m_port = parser.value(port).toInt(&ok);
  107. if (!ok || m_port <= 0 || m_port > 653535)
  108. {
  109. std::cerr << "Invalid port" << std::endl;
  110. parser.showHelp(EX_USAGE);
  111. }
  112. m_verbose = parser.isSet(verbose);
  113. }