Browse Source

thread for reading input

old
Robin Thoni 9 years ago
parent
commit
78add840a3
7 changed files with 100 additions and 50 deletions
  1. 10
    9
      server/inputbusiness.cpp
  2. 12
    6
      server/inputbusiness.h
  3. 9
    20
      server/mainclass.cpp
  4. 2
    13
      server/mainclass.h
  5. 22
    0
      server/readthread.cpp
  6. 41
    0
      server/readthread.h
  7. 4
    2
      server/server.pro

+ 10
- 9
server/inputbusiness.cpp View File

@@ -1,23 +1,24 @@
1 1
 #include <time.h>
2 2
 #include "inputbusiness.h"
3 3
 
4
-InputBusiness::InputBusiness(InputManager *input, const QList<QVariant> &channels, ServerManager *server)
5
-    : m_input(input)
6
-    , m_channels(channels)
7
-    , m_server(server)
4
+InputBusiness::InputBusiness(InputManager *input, const QList<QVariant> &channels, quint64 interval, ServerManager *server)
5
+    : m_server(server)
6
+    , m_thread(new ReadThread(input, channels, interval, this))
8 7
 {
8
+    connect(m_thread, SIGNAL(newValues(InputValues,quint64)), this, SLOT(m_newValues(InputValues,quint64)));
9 9
 }
10 10
 
11 11
 InputBusiness::~InputBusiness()
12 12
 {
13 13
 }
14 14
 
15
-QList<QPair<QVariant, QVariant>> InputBusiness::readAndSend(quint64 timestamp)
15
+void InputBusiness::m_newValues(InputValues values, quint64 timestamp)
16 16
 {
17
-    QList<QPair<QVariant, QVariant>> values;
18
-    foreach (auto channel, m_channels)
19
-        values.append(QPair<QVariant, QVariant>(channel, m_input->read(channel)));
20 17
     m_server->sendData(values, timestamp);
21
-    return values;
18
+    emit newValues(values, timestamp);
22 19
 }
23 20
 
21
+void InputBusiness::start()
22
+{
23
+    m_thread->start();
24
+}

+ 12
- 6
server/inputbusiness.h View File

@@ -4,23 +4,29 @@
4 4
 #include <QObject>
5 5
 #include "inputmanager.h"
6 6
 #include "servermanager.h"
7
+#include "readthread.h"
7 8
 
8 9
 class InputBusiness : public QObject
9 10
 {
10 11
     Q_OBJECT
11 12
 public:
12
-    InputBusiness(InputManager* input, const QList<QVariant>& channels, ServerManager* server);
13
+    InputBusiness(InputManager* input, const QList<QVariant>& channels, quint64 interval, ServerManager* server);
14
+
13 15
     virtual ~InputBusiness();
14 16
 
15
-public slots:
16
-    QList<QPair<QVariant, QVariant> > readAndSend(quint64 timestamp);
17
+signals:
18
+    void newValues(InputValues values, quint64 timestamp);
17 19
 
18
-private:
19
-    InputManager* m_input;
20
+public slots:
21
+    void start();
20 22
 
21
-    QList<QVariant> m_channels;
23
+private slots:
24
+    void m_newValues(InputValues values, quint64 timestamp);
22 25
 
26
+private:
23 27
     ServerManager* m_server;
28
+
29
+    ReadThread* m_thread;
24 30
 };
25 31
 
26 32
 #endif // INPUTBUSINESS_H

+ 9
- 20
server/mainclass.cpp View File

@@ -3,7 +3,6 @@
3 3
 #include <QVariant>
4 4
 #include <iostream>
5 5
 #include <sysexits.h>
6
-#include <unistd.h>
7 6
 #include "qcommandlineparser.h"
8 7
 #include "mainclass.h"
9 8
 #include "gpiomanager.h"
@@ -45,29 +44,19 @@ void MainClass::main()
45 44
         return;
46 45
     }
47 46
 
48
-    m_input = new InputBusiness(input, m_channels, server);
49
-    m_timer = new QTimer(this);
50
-    m_timer->setSingleShot(false);
51
-    m_timer->setInterval(m_interval / 1000);
52
-    connect(m_timer, SIGNAL(timeout()), this, SLOT(maySend()));
53
-    m_timer->start();
47
+    m_input = new InputBusiness(input, m_channels, m_interval, server);
48
+    if (m_verbose)
49
+        connect(m_input, SIGNAL(newValues(InputValues,quint64)), this, SLOT(newValues(InputValues,quint64)));
50
+    m_input->start();
54 51
 }
55 52
 
56
-void MainClass::maySend()
53
+void MainClass::newValues(InputValues values, quint64 timestamp)
57 54
 {
58
-    auto time = getTime();
59
-    if (time - m_lastTime < m_interval)
60
-        usleep(m_interval - (time - m_lastTime));
61
-    m_lastTime = getTime();
62
-    auto values = m_input->readAndSend(time);
63
-    if (m_verbose)
64
-    {
65
-        std::cout << time << ":";
66
-        foreach (auto value, values) {
67
-            std::cout << " " << value .first.toLongLong() << ":" << value.second.toInt();
68
-        }
69
-        std::cout << std::endl;
55
+    std::cout << timestamp << ":";
56
+    foreach (auto value, values) {
57
+        std::cout << " " << value .first.toLongLong() << ":" << value.second.toInt();
70 58
     }
59
+    std::cout << "\n";
71 60
 }
72 61
 
73 62
 void MainClass::getOpts()

+ 2
- 13
server/mainclass.h View File

@@ -2,7 +2,6 @@
2 2
 #define MAINCLASS_H
3 3
 
4 4
 #include <QObject>
5
-#include <sys/time.h>
6 5
 #include "inputbusiness.h"
7 6
 
8 7
 class MainClass : public QObject
@@ -22,15 +21,12 @@ signals:
22 21
 public slots:
23 22
     void main();
24 23
 
25
-    void maySend();
26
-
27
-    inline __suseconds_t getTime() const;
24
+private slots:
25
+    void newValues(InputValues values, quint64 timestamp);
28 26
 
29 27
 private:
30 28
     InputBusiness* m_input;
31 29
 
32
-    QTimer* m_timer;
33
-
34 30
     DeviceType m_device;
35 31
 
36 32
     QHostAddress m_address;
@@ -48,11 +44,4 @@ private:
48 44
     void getOpts();
49 45
 };
50 46
 
51
-__suseconds_t MainClass::getTime() const
52
-{
53
-    struct timeval time;
54
-    gettimeofday(&time, NULL);
55
-    return 1000000 * time.tv_sec + time.tv_usec;
56
-}
57
-
58 47
 #endif // MAINCLASS_H

+ 22
- 0
server/readthread.cpp View File

@@ -0,0 +1,22 @@
1
+#include "readthread.h"
2
+
3
+ReadThread::ReadThread(InputManager* input, const QList<QVariant> &channels, quint64 interval, QObject *parent)
4
+    : QThread(parent)
5
+    , m_input(input)
6
+    , m_channels(channels)
7
+    , m_interval(interval)
8
+{
9
+    qRegisterMetaType<InputValues>("InputValues");
10
+}
11
+
12
+void ReadThread::run()
13
+{
14
+    while (true)
15
+    {
16
+        InputValues values;
17
+        foreach (auto channel, m_channels)
18
+            values.append(QPair<QVariant, QVariant>(channel, m_input->read(channel)));
19
+        emit newValues(values, getTime());
20
+        usleep(m_interval);
21
+    }
22
+}

+ 41
- 0
server/readthread.h View File

@@ -0,0 +1,41 @@
1
+#ifndef READTHREAD_H
2
+#define READTHREAD_H
3
+
4
+#include <QThread>
5
+#include <sys/time.h>
6
+#include "inputmanager.h"
7
+
8
+typedef QList<QPair<QVariant, QVariant>> InputValues;
9
+
10
+class ReadThread : public QThread
11
+{
12
+    Q_OBJECT
13
+public:
14
+    explicit ReadThread(InputManager* input, const QList<QVariant> &channels, quint64 interval, QObject *parent = 0);
15
+
16
+    inline __suseconds_t getTime() const;
17
+
18
+protected:
19
+    void run();
20
+
21
+signals:
22
+    void newValues(InputValues values, quint64 timestamp);
23
+
24
+private:
25
+    InputManager* m_input;
26
+
27
+    QList<QVariant> m_channels;
28
+
29
+    quint64 m_interval;
30
+};
31
+
32
+__suseconds_t ReadThread::getTime() const
33
+{
34
+    struct timeval time;
35
+    gettimeofday(&time, NULL);
36
+    return 1000000 * time.tv_sec + time.tv_usec;
37
+}
38
+
39
+Q_DECLARE_METATYPE(InputValues)
40
+
41
+#endif // READTHREAD_H

+ 4
- 2
server/server.pro View File

@@ -26,7 +26,8 @@ SOURCES += main.cpp \
26 26
     inputbusiness.cpp \
27 27
     randommanager.cpp \
28 28
     qcommandlineoption.cpp \
29
-    qcommandlineparser.cpp
29
+    qcommandlineparser.cpp \
30
+    readthread.cpp
30 31
 
31 32
 HEADERS += \
32 33
     mainclass.h \
@@ -36,4 +37,5 @@ HEADERS += \
36 37
     inputbusiness.h \
37 38
     randommanager.h \
38 39
     qcommandlineoption.h \
39
-    qcommandlineparser.h
40
+    qcommandlineparser.h \
41
+    readthread.h

Loading…
Cancel
Save