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

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