Prechádzať zdrojové kódy

read parameter data from fifo

xuqiang 4 mesiacov pred
rodič
commit
7792d63c1f
4 zmenil súbory, kde vykonal 115 pridanie a 0 odobranie
  1. 3 0
      include/appcontext.h
  2. 27 0
      include/datafifo.h
  3. 5 0
      src/appcontext.cpp
  4. 80 0
      src/datafifo.cpp

+ 3 - 0
include/appcontext.h

@@ -7,6 +7,7 @@
 #include "network.h"
 #include "serialport.h"
 #include "httpserver.h"
+#include "datafifo.h"
 
 class AppContext : public QObject
 {
@@ -22,6 +23,7 @@ public:
     Network &network();
     SerialPort &serialPort();
     HttpServer &httpServer();
+    DataFifo &dataFifo();
 
 private:
     PluginManager m_pluginMgr;
@@ -29,6 +31,7 @@ private:
     Network m_network;
     SerialPort m_serialPort;
     HttpServer m_httpServer;
+    DataFifo m_dataFifo;
 
 };
 

+ 27 - 0
include/datafifo.h

@@ -0,0 +1,27 @@
+#ifndef __DATAFIFO_H__
+#define __DATAFIFO_H__
+
+#include <QObject>
+#include <QThread>
+
+class DataFifo : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit DataFifo(QObject *parent = nullptr);
+
+    void start();
+    void stop();
+
+private:
+    QThread m_thread;
+    bool m_isRunning;
+    
+private slots:
+    void doWork();
+
+};
+
+
+#endif  // DATAFIFO_H

+ 5 - 0
src/appcontext.cpp

@@ -43,3 +43,8 @@ HttpServer &AppContext::httpServer()
 {
     return m_httpServer;
 }
+
+DataFifo &AppContext::dataFifo()
+{
+    return m_dataFifo;
+}

+ 80 - 0
src/datafifo.cpp

@@ -0,0 +1,80 @@
+#include "datafifo.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "logger.h"
+
+#define FIFO_PATH           "/tmp/netparse"
+
+DataFifo::DataFifo(QObject *parent)
+    : QObject(parent)
+    , m_isRunning(false)
+{
+    connect(&m_thread, &QThread::started, this, &DataFifo::doWork);
+    moveToThread(&m_thread);
+    start();
+}
+
+void DataFifo::start()
+{
+    m_isRunning = true;
+    m_thread.start();
+}
+
+void DataFifo::stop()
+{
+    m_isRunning = false;
+
+    // 打开写端写一个换行,唤醒阻塞的 read
+    int fd = open(FIFO_PATH, O_WRONLY | O_NONBLOCK);
+    if (fd != -1) {
+        write(fd, "\n", 1);
+        close(fd);
+    }
+
+    m_thread.exit();
+    m_thread.wait();
+}
+
+void DataFifo::doWork()
+{
+    // 1. 创建 FIFO(如果已存在忽略错误)
+    if (mkfifo(FIFO_PATH, 0666) == -1) {
+        if (errno != EEXIST) {
+            LOG_ERROR("mkfifo error");
+            return;
+        }
+    }
+
+    // 2. 打开 FIFO 读端
+    int fd = open(FIFO_PATH, O_RDONLY);
+    if (fd == -1) {
+        LOG_ERROR("open fifo error");
+        return;
+    }
+
+    FILE *stream = fdopen(fd, "r");
+
+    ssize_t n;
+    char *line = NULL;
+    size_t len = 0;
+
+    // 3. 循环读取
+    while (m_isRunning) {
+        n = getline(&line, &len, stream);
+        if(n > 0) {
+            LOG_INFO("read size: {}, buffer size: {}, data: {}", n, len, line);
+        } else if (n == 0) {
+            // FIFO 写端关闭时 read 返回 0
+            LOG_INFO("writer closed. exiting.");
+            break;
+        } else {
+            LOG_ERROR("read error");
+            break;
+        }
+    }
+    free(line);
+    fclose(stream); // 会同时关闭文件描述符
+    // close(fd);
+}