Robin Thoni 10 лет назад
Родитель
Сommit
54331888ee
4 измененных файлов: 44 добавлений и 4 удалений
  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 Просмотреть файл

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

+ 1
- 1
server/inputbusiness.h Просмотреть файл

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

+ 33
- 1
server/mainclass.cpp Просмотреть файл

1
 #include <QCoreApplication>
1
 #include <QCoreApplication>
2
 #include <QStringList>
2
 #include <QStringList>
3
+#include <QVariant>
3
 #include <iostream>
4
 #include <iostream>
4
 #include <sysexits.h>
5
 #include <sysexits.h>
6
+#include <sys/time.h>
7
+#include <unistd.h>
5
 #include "qcommandlineparser.h"
8
 #include "qcommandlineparser.h"
6
 #include "mainclass.h"
9
 #include "mainclass.h"
7
 #include "gpiomanager.h"
10
 #include "gpiomanager.h"
13
     , m_device(Gpio)
16
     , m_device(Gpio)
14
     , m_address(QHostAddress::Any)
17
     , m_address(QHostAddress::Any)
15
     , m_port(39415)
18
     , m_port(39415)
19
+    , m_verbose(false)
20
+    , m_lastTime(0)
16
 {
21
 {
17
 }
22
 }
18
 
23
 
41
     }
46
     }
42
 
47
 
43
     m_input = new InputBusiness(input, m_channels, server);
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
 void MainClass::getOpts()
75
 void MainClass::getOpts()
59
     parser.addOption(address);
87
     parser.addOption(address);
60
     QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
88
     QCommandLineOption port((QStringList() << "p" << "port"), "port to bind socket [1-65535]", "PORT", QString::number(m_port));
61
     parser.addOption(port);
89
     parser.addOption(port);
90
+    QCommandLineOption verbose((QStringList() << "verbose"), "enable verbose mode");
91
+    parser.addOption(verbose);
62
 
92
 
63
     parser.process(*qApp);
93
     parser.process(*qApp);
64
 
94
 
95
         std::cerr << "Invalid port" << std::endl;
125
         std::cerr << "Invalid port" << std::endl;
96
         parser.showHelp(EX_USAGE);
126
         parser.showHelp(EX_USAGE);
97
     }
127
     }
128
+
129
+    m_verbose = parser.isSet(verbose);
98
 }
130
 }

+ 8
- 0
server/mainclass.h Просмотреть файл

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

Загрузка…
Отмена
Сохранить