Browse Source

change tool/tool_ntripbroadcast. complete this project. have run in xiali computer. Need test use hcp2.

yuchuli 3 years ago
parent
commit
965a6530a0

+ 238 - 2
src/tool/tool_ntripbroadcast/mainwindow.cpp

@@ -1,6 +1,8 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
+
+
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
     , ui(new Ui::MainWindow)
@@ -16,6 +18,37 @@ MainWindow::MainWindow(QWidget *parent)
     ui->lineEdit_Lon->setText(QString::number(mfLon,'f',7));
     ui->lineEdit_Lat->setText(QString::number(mfLat,'f',7));
 
+    QStandardItemModel* model = new QStandardItemModel();
+    mmodel = model;
+
+    /* 设置列数 */
+    model->setColumnCount(3);
+    model->setHeaderData(0, Qt::Horizontal, "ip");
+    model->setHeaderData(1, Qt::Horizontal, "port");
+
+    model->setHeaderData(2, Qt::Horizontal, "state");
+
+    /* 设置行数 */
+    model->setRowCount(100);
+
+    ui->tableView->setModel(model);
+
+    ui->tableView->horizontalHeader()->resizeSection(0,360);
+    ui->tableView->horizontalHeader()->resizeSection(1,100);
+    ui->tableView->horizontalHeader()->resizeSection(2,100);
+//    /* 设置列宽在可视界面自适应宽度 */
+//    ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+    /* 行颜色交替显示 */
+    ui->tableView->setAlternatingRowColors(true);
+    /* 不允许在图形界面修改内容 */
+    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
+
+    /* 在表格内加入内容 */
+
+
+    /* 显示表 */
+    ui->tableView->show();
+
     mpntripclient = new ntrip_client(ui->lineEdit_serverip->text().toStdString(),
                                ui->lineEdit_serverport->text().toStdString(),
                                ui->lineEdit_servermountpoint->text().toStdString(),
@@ -30,6 +63,26 @@ MainWindow::MainWindow(QWidget *parent)
     connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));
     timer->start(100);
 
+    m_tcpserverNtrip = new QTcpServer(this);
+    m_tcpserverNtrip->listen(QHostAddress::Any,8002);//监听的端口号
+    m_tcpserverNtrip->setMaxPendingConnections(2000);
+    connect(m_tcpserverNtrip,SIGNAL(newConnection()), this,SLOT(newNtripConnect()));
+
+    ui->pushButton_Start->setText("Stop");
+    mbStart = true;
+    mpntripclient->setserverinfo(ui->lineEdit_serverip->text().toStdString(),
+                               ui->lineEdit_serverport->text().toStdString(),
+                               ui->lineEdit_servermountpoint->text().toStdString(),
+                               ui->lineEdit_username->text().toStdString(),
+                               ui->lineEdit_password->text().toStdString());
+
+    double flon = ui->lineEdit_Lon->text().toDouble();
+    double flat = ui->lineEdit_Lat->text().toDouble();
+
+
+    mpntripclient->SetLatLon(flon,flat);
+    mpntripclient->StartConnect();
+
 }
 
 MainWindow::~MainWindow()
@@ -92,18 +145,69 @@ void MainWindow::onTimer()
         }
         setWindowTitle(strtitle);
     }
+
+    mMutex.lock();
+    int nNCSize = mvectorNC.size();
+    int i;
+    std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[nNCSize*1000+100]);
+    snprintf(pstr_ptr.get(),1000,"Ntrip Client:\n");
+
+
+    for(i=0;i<std::min(mnModelSize,nNCSize);i++)
+    {
+
+        mmodel->setItem(i,0,new QStandardItem(mvectorNC[i]->mpSocket->peerAddress().toString().toLatin1().data()));
+        mmodel->setItem(i,1,new QStandardItem(QString::number(mvectorNC[i]->mpSocket->peerPort()).toLatin1().data()));
+        mmodel->setItem(i,2,new QStandardItem(QString::number(mvectorNC[i]->mnState).toLatin1().data()));
+        char strout[1000];
+        snprintf(strout,1000,"IP:%s Port:%d  State:%d\n",
+                 mvectorNC[i]->mpSocket->peerAddress().toString().toLatin1().data(),
+                 mvectorNC[i]->mpSocket->peerPort(),
+                 mvectorNC[i]->mnState);
+        strncat(pstr_ptr.get(),strout,1000);
+    }
+
+    for(i=nNCSize;i<mnModelSize;i++)
+    {
+        mmodel->setItem(i,0,new QStandardItem(""));
+        mmodel->setItem(i,1,new QStandardItem(""));
+        mmodel->setItem(i,2,new QStandardItem(""));
+    }
+
+
+    mMutex.unlock();
+//    if(nNCSize > 0)
+//    {
+//        ui->plainTextEdit_Client->setPlainText(pstr_ptr.get());
+//    }
+//    else
+//    {
+//        ui->plainTextEdit_Client->setPlainText("No Connected.");
+//    }
 }
 
 void MainWindow::onupdatentripmsg(QByteArray ba)
 {
     if(ba.size()<1)return;
+
+    int i;
+    mMutex.lock();
+    int nNCSize = mvectorNC.size();
+    for(i=0;i<nNCSize;i++)
+    {
+        mvectorNC[i]->mpSocket->write(ba);
+        mvectorNC[i]->mpSocket->flush();
+    }
+    mMutex.unlock();
+
+
     char * strout = 0;
     std::shared_ptr<char> str_ptr = std::shared_ptr<char>(new char[ba.size()*3 + 100]);
     strout = str_ptr.get();
-    snprintf(strout,3000,"Ntrip msg len is %d info is: ",ba.length());
+    snprintf(strout,3000,"Ntrip msg len is %d \ninfo : ",ba.length());
     char strtem[10];
     int nlen = ba.length();
-    int i;
+
     for(i=0;i<nlen;i++)
     {
         snprintf(strtem,10,"%02X ",(unsigned char )ba.at(i));
@@ -112,3 +216,135 @@ void MainWindow::onupdatentripmsg(QByteArray ba)
 
     ui->plainTextEdit_NTIPMSG->setPlainText(strout);
 }
+
+void MainWindow::newNtripConnect()
+{
+    qDebug("new connect");
+    QTcpSocket * pSocket = m_tcpserverNtrip->nextPendingConnection();
+    if(pSocket != NULL)
+    {
+        pSocket->setSocketOption(QAbstractSocket::LowDelayOption,1);
+        connect(pSocket,SIGNAL(readyRead()),this,SLOT(readNtripMessage()));
+        connect(pSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(errorNtrip()));
+        connect(pSocket,SIGNAL(disconnected()),this,SLOT(disconnetNtrip()));
+        std::shared_ptr<NtripClient> p = std::shared_ptr<NtripClient>( new NtripClient);
+        p->mnState = 0;
+        p->mpSocket = pSocket;
+
+//        p->mLat = 0;
+//        p->mLon = 0;
+        mMutex.lock();
+        mvectorNC.push_back(p);
+        mMutex.unlock();
+//        mnNCCount++;
+    }
+    else
+    {
+    }
+}
+
+void MainWindow::readNtripMessage()
+{
+    QTcpSocket * pSocket = static_cast<QTcpSocket *>(this->sender());
+    QByteArray ba = pSocket->readAll();
+    int i;
+    bool bNeedClose = false;
+    mMutex.lock();
+    for(i=0;i<mvectorNC.size();i++)
+    {
+        if(pSocket == mvectorNC.at(i)->mpSocket)
+        {
+            //Send Auth.
+
+            char * str = new char[ba.length() +1];
+            str[ba.length()] = 0;
+            memcpy(str,ba.data(),ba.length());
+//            qDebug(str);
+            if(strstr(str,"GET / HTTP") >0)
+            {
+                QDateTime dt = QDateTime::currentDateTime();
+
+                char strSend[1000];
+                char strLine[200];
+                sprintf(strSend,"SOURCETABLE 200 OK\r\n");
+                sprintf(strLine,"Server: NTRIP Trimble NTRIP Caster\r\n");strcat(strSend,strLine);
+                sprintf(strLine,"Content-Type: text/plain");strcat(strSend,strLine);
+                sprintf(strLine,"Content-Length: 111");strcat(strSend,strLine);
+                sprintf(strLine,"Date: %s\r\n",dt.toString(Qt::ISODate).toLatin1().data());strcat(strSend,strLine);
+                qDebug(strLine);
+    //            sprintf(strLine,"STR;RTCM23;RTCM23;RTCM 2.3;1(1),3(10),18(1),19(1);2;GPS;SGNET;CHN;31;121;1;1;SGCAN;None;B;N;0;;\r\n");strcat(strSend,strLine);
+    //            sprintf(strLine,"STR;RTCM32_GGB;RTCM3X;RTCM       3.X;1004(1),1005/1007(5),PBS(10);2;GPS;SGNET;CHN;31;121;1;1;SGCAN;None;B;N;0;;\r\n");strcat(strSend,strLine);
+                sprintf(strLine,"STR;RTCM32_GGB;RTCM32_GGB;RTCM 3.2;RTCM(1);2;GPS+GLONASS;SGNET;CHN;39.14;117.08;1;1;Trimble BD982;none;B;N;0;\r\n");strcat(strSend,strLine);
+      //          sprintf(strLine,"");strcat(strSend,strLine);
+                sprintf(strLine,"ENDSOURCETABLE\r\n");strcat(strSend,strLine);
+                pSocket->write(strSend,strlen(strSend));
+                pSocket->flush();
+                qDebug("send ggb");
+
+                bNeedClose = true;
+
+            }
+            else
+            {
+            if(strstr(str,"NTRIP") > 0)
+            {
+                qDebug(str);
+                qDebug("send cors ok");
+                char * strSend = new char[1000];
+                snprintf(strSend,1000,"ICY 200 OK\r\n");
+                pSocket->write(strSend,strlen(strSend));
+                pSocket->flush();
+                delete strSend;
+                mvectorNC.at(i)->mnState = 1;
+
+            }
+            if(strstr(str,"$GPGGA") > 0)
+            {
+                qDebug(str);
+ //               SetNCLatLon(str,mListNC.at(i));
+            }
+            }
+            delete str;
+
+        }
+    }
+    mMutex.unlock();
+    if(bNeedClose )
+    {
+        pSocket->close();
+    }
+}
+
+void MainWindow::errorNtrip()
+{
+  QTcpSocket * pSocket = static_cast<QTcpSocket *>(this->sender());
+  mMutex.lock();
+  int i;
+  for(i=0;i<mvectorNC.size();i++)
+  {
+      if(pSocket == mvectorNC.at(i)->mpSocket)
+      {
+          mvectorNC.erase(mvectorNC.begin()+i);
+//            mnNCCount--;
+      }
+  }
+  mMutex.unlock();
+}
+
+
+void MainWindow::disconnetNtrip()
+{
+    qDebug("disconnect");
+    QTcpSocket * pSocket = static_cast<QTcpSocket *>(this->sender());
+    mMutex.lock();
+    int i;
+    for(i=0;i<mvectorNC.size();i++)
+    {
+        if(pSocket == mvectorNC.at(i)->mpSocket)
+        {
+             mvectorNC.erase(mvectorNC.begin()+i);
+        }
+    }
+    mMutex.unlock();
+}
+

+ 25 - 1
src/tool/tool_ntripbroadcast/mainwindow.h

@@ -2,6 +2,12 @@
 #define MAINWINDOW_H
 
 #include <QMainWindow>
+#include <QTcpServer>
+#include <vector>
+#include <memory>
+
+#include <QTableView>
+#include <QStandardItemModel>
 
 #include "ntrip_client.h"
 
@@ -9,7 +15,12 @@ QT_BEGIN_NAMESPACE
 namespace Ui { class MainWindow; }
 QT_END_NAMESPACE
 
-
+class NtripClient
+{
+public:
+    int mnState;
+    QTcpSocket * mpSocket;
+};
 
 class MainWindow : public QMainWindow
 {
@@ -26,6 +37,11 @@ private slots:
     void onTimer();
     void onupdatentripmsg(QByteArray ba);
 
+    void newNtripConnect();
+    void readNtripMessage();
+    void errorNtrip();
+    void disconnetNtrip();
+
 private:
     Ui::MainWindow *ui;
 
@@ -44,5 +60,13 @@ private:
 
     QString mstrAppTitle = "NTRIP BroadCast";
 
+    QTcpServer * m_tcpserverNtrip;
+
+    std::vector<std::shared_ptr<NtripClient >> mvectorNC;
+    QMutex mMutex;
+
+    QStandardItemModel * mmodel;
+    int mnModelSize = 100;
+
 };
 #endif // MAINWINDOW_H

+ 14 - 14
src/tool/tool_ntripbroadcast/mainwindow.ui

@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>1038</width>
-    <height>674</height>
+    <width>1360</width>
+    <height>686</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -146,22 +146,12 @@
      <string>PassWord</string>
     </property>
    </widget>
-   <widget class="QPlainTextEdit" name="plainTextEdit_Client">
-    <property name="geometry">
-     <rect>
-      <x>660</x>
-      <y>30</y>
-      <width>291</width>
-      <height>331</height>
-     </rect>
-    </property>
-   </widget>
    <widget class="QPlainTextEdit" name="plainTextEdit_NTIPMSG">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>390</y>
-      <width>871</width>
+      <width>1201</width>
       <height>221</height>
      </rect>
     </property>
@@ -212,13 +202,23 @@
      </rect>
     </property>
    </widget>
+   <widget class="QTableView" name="tableView">
+    <property name="geometry">
+     <rect>
+      <x>650</x>
+      <y>30</y>
+      <width>631</width>
+      <height>351</height>
+     </rect>
+    </property>
+   </widget>
   </widget>
   <widget class="QMenuBar" name="menubar">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>1038</width>
+     <width>1360</width>
      <height>28</height>
     </rect>
    </property>

+ 1 - 1
src/tool/tool_ntripbroadcast/ntrip_client.cpp

@@ -96,7 +96,7 @@ void ntrip_client::readyReadSlot()
 #ifdef  DEBUG_NTRIP_MSG
         ShowNtripMsg(message);
 #else
-        qDebug("ntrip msg len is %d",message.length());
+ //       qDebug("ntrip msg len is %d",message.length());
 #endif
 
 

+ 3 - 0
src/tool/tool_ntripbroadcast/ntrip_client.h

@@ -77,6 +77,9 @@ public:
     void SetGGA(char * strgga);
     void SetLatLon(double flon,double flat);
 
+
+
+
 };
 
 #endif // NTRIP_CLIENT_H

+ 2 - 0
src/tool/tool_ntripbroadcast/tool_ntripbroadcast.pro

@@ -4,6 +4,8 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
 CONFIG += c++11
 
+QMAKE_LFLAGS += -no-pie
+
 # The following define makes your compiler emit warnings if you use
 # any Qt feature that has been marked deprecated (the exact warnings
 # depend on your compiler). Please consult the documentation of the