Browse Source

main loop; verbose flag

old
Robin Thoni 9 years ago
parent
commit
54331888ee
4 changed files with 44 additions and 4 deletions
  1. 2
    2
      server/inputbusiness.cpp
  2. 1
    1
      server/inputbusiness.h
  3. 33
    1
      server/mainclass.cpp
  4. 8
    0
      server/mainclass.h

+ 2
- 2
server/inputbusiness.cpp View File

@@ -12,12 +12,12 @@ InputBusiness::~InputBusiness()
12 12
 {
13 13
 }
14 14
 
15
-void InputBusiness::readAndSend()
15
+QList<QVariant> InputBusiness::readAndSend(quint64 timestamp)
16 16
 {
17 17
     QList<QVariant> values;
18
-    quint64 timestamp = time(nullptr);
19 18
     foreach (auto channel, m_channels)
20 19
         values.append(m_input->read(channel));
21 20
     m_server->sendData(values, timestamp);
21
+    return values;
22 22
 }
23 23
 

+ 1
- 1
server/inputbusiness.h View File

@@ -13,7 +13,7 @@ public:
13 13
     virtual ~InputBusiness();
14 14
 
15 15
 public slots:
16
-    void readAndSend();
16
+    QList<QVariant> readAndSend(quint64 timestamp);
17 17
 
18 18
 private:
19 19
     InputManager* m_input;

+ 33
- 1
server/mainclass.cpp View File

@@ -1,7 +1,10 @@
1 1
 #include <QCoreApplication>
2 2
 #include <QStringList>
3
+#include <QVariant>
3 4
 #include <iostream>
4 5
 #include <sysexits.h>
6
+#include <sys/time.h>
7
+#include <unistd.h>
5 8
 #include "qcommandlineparser.h"
6 9
 #include "mainclass.h"
7 10
 #include "gpiomanager.h"
@@ -13,6 +16,8 @@ MainClass::MainClass(QObject *parent)
13 16
     , m_device(Gpio)
14 17
     , m_address(QHostAddress::Any)
15 18
     , m_port(39415)
19
+    , m_verbose(false)
20
+    , m_lastTime(0)
16 21
 {
17 22
 }
18 23
 
@@ -41,7 +46,30 @@ void MainClass::main()
41 46
     }
42 47
 
43 48
     m_input = new InputBusiness(input, m_channels, server);
44
-    m_input->readAndSend();
49
+    m_timer = new QTimer(this);
50
+    m_timer->setSingleShot(false);
51
+    m_timer->setInterval(0);
52
+    connect(m_timer, SIGNAL(timeout()), this, SLOT(maySend()));
53
+    m_timer->start();
54
+}
55
+
56
+void MainClass::maySend()
57
+{
58
+    struct timeval time;
59
+    gettimeofday(&time, NULL);
60
+    if (time.tv_usec - m_lastTime < 100)
61
+        usleep(100 - (time.tv_usec - m_lastTime));
62
+    gettimeofday(&time, NULL);
63
+    m_lastTime = time.tv_usec;
64
+    auto values = m_input->readAndSend(time.tv_usec);
65
+    if (m_verbose)
66
+    {
67
+        std::cout << time.tv_usec << ":";
68
+        foreach (auto value, values) {
69
+            std::cout << " " << value.toInt();
70
+        }
71
+        std::cout << std::endl;
72
+    }
45 73
 }
46 74
 
47 75
 void MainClass::getOpts()
@@ -59,6 +87,8 @@ void MainClass::getOpts()
59 87
     parser.addOption(address);
60 88
     QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
61 89
     parser.addOption(port);
90
+    QCommandLineOption verbose((QStringList() << "verbose"), "enable verbose mode");
91
+    parser.addOption(verbose);
62 92
 
63 93
     parser.process(*qApp);
64 94
 
@@ -95,4 +125,6 @@ void MainClass::getOpts()
95 125
         std::cerr << "Invalid port" << std::endl;
96 126
         parser.showHelp(EX_USAGE);
97 127
     }
128
+
129
+    m_verbose = parser.isSet(verbose);
98 130
 }

+ 8
- 0
server/mainclass.h View File

@@ -21,9 +21,13 @@ signals:
21 21
 public slots:
22 22
     void main();
23 23
 
24
+    void maySend();
25
+
24 26
 private:
25 27
     InputBusiness* m_input;
26 28
 
29
+    QTimer* m_timer;
30
+
27 31
     DeviceType m_device;
28 32
 
29 33
     QHostAddress m_address;
@@ -32,6 +36,10 @@ private:
32 36
 
33 37
     int m_port;
34 38
 
39
+    bool m_verbose;
40
+
41
+    __suseconds_t m_lastTime;
42
+
35 43
     void getOpts();
36 44
 };
37 45
 

Loading…
Cancel
Save