Browse Source

fixed sleep; added channels in data

old
Robin Thoni 8 years ago
parent
commit
4684551968
6 changed files with 25 additions and 17 deletions
  1. 3
    3
      server/inputbusiness.cpp
  2. 1
    1
      server/inputbusiness.h
  3. 7
    10
      server/mainclass.cpp
  4. 10
    0
      server/mainclass.h
  5. 2
    2
      server/servermanager.cpp
  6. 2
    1
      server/servermanager.h

+ 3
- 3
server/inputbusiness.cpp View File

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

+ 1
- 1
server/inputbusiness.h View File

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

+ 7
- 10
server/mainclass.cpp View File

@@ -3,7 +3,6 @@
3 3
 #include <QVariant>
4 4
 #include <iostream>
5 5
 #include <sysexits.h>
6
-#include <sys/time.h>
7 6
 #include <unistd.h>
8 7
 #include "qcommandlineparser.h"
9 8
 #include "mainclass.h"
@@ -55,18 +54,16 @@ void MainClass::main()
55 54
 
56 55
 void MainClass::maySend()
57 56
 {
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);
57
+    auto time = getTime();
58
+    if (time - m_lastTime < 100)
59
+        usleep(100 - (time - m_lastTime));
60
+    m_lastTime = getTime();
61
+    auto values = m_input->readAndSend(time);
65 62
     if (m_verbose)
66 63
     {
67
-        std::cout << time.tv_usec << ":";
64
+        std::cout << time << ":";
68 65
         foreach (auto value, values) {
69
-            std::cout << " " << value.toInt();
66
+            std::cout << " " << value .first.toInt() << ":" << value.second.toInt();
70 67
         }
71 68
         std::cout << std::endl;
72 69
     }

+ 10
- 0
server/mainclass.h View File

@@ -2,6 +2,7 @@
2 2
 #define MAINCLASS_H
3 3
 
4 4
 #include <QObject>
5
+#include <sys/time.h>
5 6
 #include "inputbusiness.h"
6 7
 
7 8
 class MainClass : public QObject
@@ -23,6 +24,8 @@ public slots:
23 24
 
24 25
     void maySend();
25 26
 
27
+    inline __suseconds_t getTime() const;
28
+
26 29
 private:
27 30
     InputBusiness* m_input;
28 31
 
@@ -43,4 +46,11 @@ private:
43 46
     void getOpts();
44 47
 };
45 48
 
49
+__suseconds_t MainClass::getTime() const
50
+{
51
+    struct timeval time;
52
+    gettimeofday(&time, NULL);
53
+    return 1000000 * time.tv_sec + time.tv_usec;
54
+}
55
+
46 56
 #endif // MAINCLASS_H

+ 2
- 2
server/servermanager.cpp View File

@@ -12,7 +12,7 @@ bool ServerManager::init(const QHostAddress &addr, const int &port)
12 12
     return m_server->listen(addr, port);
13 13
 }
14 14
 
15
-void ServerManager::sendData(const QList<QVariant> &values, const qint64 &timestamp)
15
+void ServerManager::sendData(const QList<QPair<QVariant, QVariant>> &values, const qint64 &timestamp)
16 16
 {
17 17
     auto clients = m_server->getClients();
18 18
     if (!clients.count())
@@ -22,7 +22,7 @@ void ServerManager::sendData(const QList<QVariant> &values, const qint64 &timest
22 22
 
23 23
     stream << timestamp << values.count();
24 24
     foreach (auto value, values)
25
-        stream << value;
25
+        stream << value.first << value.second;
26 26
     foreach (auto client, clients)
27 27
         client->send(1, data);
28 28
 }

+ 2
- 1
server/servermanager.h View File

@@ -2,6 +2,7 @@
2 2
 #define SERVERMANAGER_H
3 3
 
4 4
 #include <QObject>
5
+#include <QPair>
5 6
 #include <ptsocket/ptserver.h>
6 7
 
7 8
 class ServerManager : public QObject
@@ -15,7 +16,7 @@ signals:
15 16
 public slots:
16 17
     bool init(const QHostAddress& addr, const int& port);
17 18
 
18
-    void sendData(const QList<QVariant> &values, const qint64& timestamp);
19
+    void sendData(const QList<QPair<QVariant, QVariant> > &values, const qint64& timestamp);
19 20
 
20 21
 private:
21 22
     PTServer* m_server;

Loading…
Cancel
Save