Kaynağa Gözat

thread for reading input

old
Robin Thoni 10 yıl önce
ebeveyn
işleme
78add840a3

+ 10
- 9
server/inputbusiness.cpp Dosyayı Görüntüle

1
 #include <time.h>
1
 #include <time.h>
2
 #include "inputbusiness.h"
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
 InputBusiness::~InputBusiness()
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
     m_server->sendData(values, timestamp);
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 Dosyayı Görüntüle

4
 #include <QObject>
4
 #include <QObject>
5
 #include "inputmanager.h"
5
 #include "inputmanager.h"
6
 #include "servermanager.h"
6
 #include "servermanager.h"
7
+#include "readthread.h"
7
 
8
 
8
 class InputBusiness : public QObject
9
 class InputBusiness : public QObject
9
 {
10
 {
10
     Q_OBJECT
11
     Q_OBJECT
11
 public:
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
     virtual ~InputBusiness();
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
     ServerManager* m_server;
27
     ServerManager* m_server;
28
+
29
+    ReadThread* m_thread;
24
 };
30
 };
25
 
31
 
26
 #endif // INPUTBUSINESS_H
32
 #endif // INPUTBUSINESS_H

+ 9
- 20
server/mainclass.cpp Dosyayı Görüntüle

3
 #include <QVariant>
3
 #include <QVariant>
4
 #include <iostream>
4
 #include <iostream>
5
 #include <sysexits.h>
5
 #include <sysexits.h>
6
-#include <unistd.h>
7
 #include "qcommandlineparser.h"
6
 #include "qcommandlineparser.h"
8
 #include "mainclass.h"
7
 #include "mainclass.h"
9
 #include "gpiomanager.h"
8
 #include "gpiomanager.h"
45
         return;
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
 void MainClass::getOpts()
62
 void MainClass::getOpts()

+ 2
- 13
server/mainclass.h Dosyayı Görüntüle

2
 #define MAINCLASS_H
2
 #define MAINCLASS_H
3
 
3
 
4
 #include <QObject>
4
 #include <QObject>
5
-#include <sys/time.h>
6
 #include "inputbusiness.h"
5
 #include "inputbusiness.h"
7
 
6
 
8
 class MainClass : public QObject
7
 class MainClass : public QObject
22
 public slots:
21
 public slots:
23
     void main();
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
 private:
27
 private:
30
     InputBusiness* m_input;
28
     InputBusiness* m_input;
31
 
29
 
32
-    QTimer* m_timer;
33
-
34
     DeviceType m_device;
30
     DeviceType m_device;
35
 
31
 
36
     QHostAddress m_address;
32
     QHostAddress m_address;
48
     void getOpts();
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
 #endif // MAINCLASS_H
47
 #endif // MAINCLASS_H

+ 22
- 0
server/readthread.cpp Dosyayı Görüntüle

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 Dosyayı Görüntüle

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 Dosyayı Görüntüle

26
     inputbusiness.cpp \
26
     inputbusiness.cpp \
27
     randommanager.cpp \
27
     randommanager.cpp \
28
     qcommandlineoption.cpp \
28
     qcommandlineoption.cpp \
29
-    qcommandlineparser.cpp
29
+    qcommandlineparser.cpp \
30
+    readthread.cpp
30
 
31
 
31
 HEADERS += \
32
 HEADERS += \
32
     mainclass.h \
33
     mainclass.h \
36
     inputbusiness.h \
37
     inputbusiness.h \
37
     randommanager.h \
38
     randommanager.h \
38
     qcommandlineoption.h \
39
     qcommandlineoption.h \
39
-    qcommandlineparser.h
40
+    qcommandlineparser.h \
41
+    readthread.h

Loading…
İptal
Kaydet