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