Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mainclass.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <QCoreApplication>
  2. #include <QStringList>
  3. #include <iostream>
  4. #include <sysexits.h>
  5. #include "qcommandlineparser.h"
  6. #include "mainclass.h"
  7. #include "gpiomanager.h"
  8. #include "randommanager.h"
  9. MainClass::MainClass(QObject *parent)
  10. : QObject(parent)
  11. , m_input(nullptr)
  12. , m_device(Gpio)
  13. , m_address(QHostAddress::Any)
  14. , m_port(39415)
  15. {
  16. }
  17. void MainClass::main()
  18. {
  19. getOpts();
  20. InputManager* input = nullptr;
  21. if (m_device == Gpio)
  22. input = new GpioManager(this);
  23. else if (m_device == Random)
  24. input = new RandomManager(this);
  25. if (!input->init(m_channels))
  26. {
  27. std::cerr << "Failed to initialize device" << std::endl;
  28. exit(1);
  29. return;
  30. }
  31. auto server = new ServerManager(this);
  32. if (!server->init(m_address, m_port))
  33. {
  34. std::cerr << "Failed to bind socket" << std::endl;
  35. exit(2);
  36. return;
  37. }
  38. m_input = new InputBusiness(input, m_channels, server);
  39. m_input->readAndSend();
  40. }
  41. void MainClass::getOpts()
  42. {
  43. QCommandLineParser parser;
  44. parser.setApplicationDescription("Server for GPIO monitoring");
  45. parser.addHelpOption();
  46. parser.addVersionOption();
  47. QCommandLineOption device((QStringList() << "d" << "device"), "device to use [gpio|rand]", "DEVICE", "gpio");
  48. parser.addOption(device);
  49. QCommandLineOption channel((QStringList() << "c" << "channel"), "channel to monitor", "CHANNEL");
  50. parser.addOption(channel);
  51. QCommandLineOption address((QStringList() << "a" << "address"), "address to bind socket", "ADDRESS", m_address.toString());
  52. parser.addOption(address);
  53. QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
  54. parser.addOption(port);
  55. parser.process(*qApp);
  56. auto dev = parser.value(device);
  57. if (dev == "gpio")
  58. m_device = Gpio;
  59. else if (dev == "rand" || dev == "random")
  60. m_device = Random;
  61. else
  62. {
  63. std::cerr << "Invalid device" << std::endl;
  64. parser.showHelp(EX_USAGE);
  65. }
  66. foreach (auto channel, parser.values(channel))
  67. m_channels.append(channel);
  68. if (m_channels.empty())
  69. {
  70. std::cerr << "At least one channel is required" << std::endl;
  71. parser.showHelp(EX_USAGE);
  72. }
  73. m_address = parser.value(address);
  74. if (m_address.isNull())
  75. {
  76. std::cerr << "Invalid address" << std::endl;
  77. parser.showHelp(EX_USAGE);
  78. }
  79. bool ok = false;
  80. m_port = parser.value(port).toInt(&ok);
  81. if (!ok || m_port <= 0 || m_port > 653535)
  82. {
  83. std::cerr << "Invalid port" << std::endl;
  84. parser.showHelp(EX_USAGE);
  85. }
  86. }