Browse Source

add fwupdate_admin. not complete.

yuchuli 1 year ago
parent
commit
d18a9c7cf6

+ 18 - 0
src/include/proto/fwlogin.proto

@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+package iv.fw;
+
+message loginreq
+{
+	required string username = 1;
+	required string password = 2;
+};
+
+
+message loginreply
+{
+	required int32 nauth = 1;  //0 not auth    1 auth
+	optional string errstr = 2; //
+}
+
+

+ 11 - 3
src/include/proto3/commonrpc.proto

@@ -28,14 +28,22 @@ service CommonRPC {
   
 }
 
-
+enum CommonRPCType //枚举消息类型
+{
+	LOGIN = 0; 
+}
 
 message queryrpcReq {
-  bytes data = 1; 
+  
+  CommonRPCType nType = 1;
+  int32 ndatasize = 2;
+  bytes data = 3; 
 }
 
 message queryrpcReply {
-    bytes data = 1;
+    CommonRPCType nType = 1;
+    int32 ndatasize = 2;
+    bytes data = 3;
 }
 
 

BIN
src/tool/fwupdate_admin/display.png


+ 64 - 0
src/tool/fwupdate_admin/fwupdate_admin.pro

@@ -0,0 +1,64 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2023-08-29T09:20:14
+#
+#-------------------------------------------------
+
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+TARGET = fwupdate_admin
+TEMPLATE = app
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which has been marked as deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+
+SOURCES += \
+        main.cpp \
+        mainwindow.cpp \
+    mainwindowlogin.cpp \
+    ../../include/msgtype/commonrpc.pb.cc \
+    ../../include/msgtype/fwlogin.pb.cc \
+    ../server_fwupdate/commonrpc.grpc.pb.cc \
+    grpcfwclient.cpp
+
+HEADERS += \
+        mainwindow.h \
+    mainwindowlogin.h \
+    ../../include/msgtype/commonrpc.pb.h \
+    ../../include/msgtype/fwlogin.pb.h \
+    ../server_fwupdate/commonrpc.grpc.pb.h \
+    grpcfwclient.h
+
+FORMS += \
+        mainwindow.ui \
+    mainwindowlogin.ui
+
+RESOURCES += \
+    res.qrc
+
+
+INCLUDEPATH += $$PWD/../server_fwupdate
+
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+!include(../../../include/ivprotobuf.pri ) {
+    error( "Couldn't find the ivprotobuf.pri file!" )
+}
+
+!include(../../../include/ivgrpc.pri ) {
+    error( "Couldn't find the ivgrpc.pri file!" )
+}

+ 108 - 0
src/tool/fwupdate_admin/grpcfwclient.cpp

@@ -0,0 +1,108 @@
+#include "grpcfwclient.h"
+
+grpcfwclient::grpcfwclient()
+{
+    mbRun = true;
+    mpthread = new std::thread(&grpcfwclient::threadrpc,this);
+}
+
+grpcfwclient::~grpcfwclient()
+{
+    mbRun = false;
+    if(mpthread != NULL)
+    {
+        mbRun = false;
+        mpthread->join();
+    }
+}
+
+void grpcfwclient::threadrpc()
+{
+    std::string target_str = mstrserverip+":";
+    target_str = target_str + mstrserverport ;//std::to_string()
+    auto cargs = grpc::ChannelArguments();
+    cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
+    cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
+
+    std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
+             target_str, grpc::InsecureChannelCredentials(),cargs);
+
+    std::unique_ptr<iv::CommonRPC::Stub> stub_ = iv::CommonRPC::NewStub(channel);
+
+    gpr_timespec timespec;
+      timespec.tv_sec = 30;//设置阻塞时间为5秒
+      timespec.tv_nsec = 0;
+      timespec.clock_type = GPR_TIMESPAN;
+
+    int nwaitms = 10;
+    while(mbRun)
+    {
+        std::unique_lock<std::mutex> lk(mmutexcv);
+        if(mcv.wait_for(lk, std::chrono::milliseconds(nwaitms)) == std::cv_status::timeout)
+        {
+            lk.unlock();
+        }
+        else
+        {
+            lk.unlock();
+        }
+
+        iv::queryrpcReq xreq;
+        iv::queryrpcReply xreply;
+        if(mbReplyUpdate)
+        {
+            mmutexReq.lock();
+            xreq.CopyFrom(mReq);
+            mbReplyUpdate = false;
+            mmutexReq.unlock();
+
+
+        }
+
+
+        ClientContext context ;
+        context.set_deadline(timespec);
+
+        Status status = stub_->rpcquery(&context, xreq, &xreply);
+        if (status.ok()) {
+            mmutexReply.lock();
+            mReply.CopyFrom(xreply);
+            mbReplyUpdate = true;
+            mmutexReply.unlock();
+        } else {
+          std::cout << status.error_code() << ": " << status.error_message()
+                    << std::endl;
+          std::cout<<"RPC failed"<<std::endl;
+          mnConnectFail = status.error_code();
+          if(status.error_code() == 4)
+          {
+              std::cout<<" RPC Exceed Time, Create New stub_"<<std::endl;
+              channel = grpc::CreateCustomChannel(
+                       target_str, grpc::InsecureChannelCredentials(),cargs);
+
+              stub_ = iv::CommonRPC::NewStub(channel);
+          }
+          std::this_thread::sleep_for(std::chrono::milliseconds(900));
+
+        }
+
+    }
+
+}
+
+void grpcfwclient::SetReq(iv::queryrpcReq & xreq)
+{
+    mmutexReq.lock();
+    mReq.CopyFrom(xreq);
+    mbReplyUpdate = false;
+    mbReqUpdate = true;
+    mnConnectFail = 0;
+    mmutexReq.unlock();
+    mcv.notify_all();
+}
+
+int grpcfwclient::GetReply(iv::queryrpcReply & xReply)
+{
+
+    return 0;
+}

+ 54 - 0
src/tool/fwupdate_admin/grpcfwclient.h

@@ -0,0 +1,54 @@
+#ifndef GRPCFWCLIENT_H
+#define GRPCFWCLIENT_H
+
+#include <mutex>
+#include <thread>
+#include <condition_variable>
+#include <iostream>
+
+#include <grpcpp/grpcpp.h>
+#include "commonrpc.grpc.pb.h"
+
+#include "commonrpc.pb.h"
+
+using grpc::Channel;
+using grpc::ClientContext;
+using grpc::Status;
+
+class grpcfwclient
+{
+public:
+    grpcfwclient();
+    ~grpcfwclient();
+
+private:
+    void threadrpc();
+
+private:
+    std::thread * mpthread = NULL;
+    bool mbRun = true;
+
+    std::string mstrserverip = "127.0.0.1";
+    std::string mstrserverport = "19111";
+
+    std::condition_variable mcv;
+    std::mutex mmutexcv;
+
+    std::mutex mmutexReq;
+    std::mutex mmutexReply;
+
+    iv::queryrpcReq mReq;
+    bool mbReqUpdate = false;
+
+    int mnConnectFail = 0; //4 14 fail;
+
+    iv::queryrpcReply mReply;
+    bool mbReplyUpdate = false;
+
+public:
+    void SetReq(iv::queryrpcReq & xreq);
+    int GetReply(iv::queryrpcReply & xReply);
+
+};
+
+#endif // GRPCFWCLIENT_H

+ 29 - 0
src/tool/fwupdate_admin/main.cpp

@@ -0,0 +1,29 @@
+#include "mainwindow.h"
+#include <QApplication>
+
+#include "grpcfwclient.h"
+
+#include "mainwindowlogin.h"
+
+MainWindow * gwmain;
+MainWindowLogin * gwlogin;
+
+grpcfwclient * gpgrpcfwclient;
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+
+    grpcfwclient fwclient;
+    gpgrpcfwclient = &fwclient;
+
+    MainWindowLogin wlogin;
+    wlogin.show();
+
+    MainWindow w;
+//    w.show();
+    gwlogin = &wlogin;
+    gwmain = &w;
+
+    return a.exec();
+}

+ 14 - 0
src/tool/fwupdate_admin/mainwindow.cpp

@@ -0,0 +1,14 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+
+MainWindow::MainWindow(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}

+ 22 - 0
src/tool/fwupdate_admin/mainwindow.h

@@ -0,0 +1,22 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+namespace Ui {
+class MainWindow;
+}
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit MainWindow(QWidget *parent = 0);
+    ~MainWindow();
+
+private:
+    Ui::MainWindow *ui;
+};
+
+#endif // MAINWINDOW_H

+ 24 - 0
src/tool/fwupdate_admin/mainwindow.ui

@@ -0,0 +1,24 @@
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>MainWindow</string>
+  </property>
+  <widget class="QMenuBar" name="menuBar" />
+  <widget class="QToolBar" name="mainToolBar" />
+  <widget class="QWidget" name="centralWidget" />
+  <widget class="QStatusBar" name="statusBar" />
+ </widget>
+ <layoutDefault spacing="6" margin="11" />
+ <pixmapfunction></pixmapfunction>
+ <resources/>
+ <connections/>
+</ui>

+ 87 - 0
src/tool/fwupdate_admin/mainwindowlogin.cpp

@@ -0,0 +1,87 @@
+#include "mainwindowlogin.h"
+#include "ui_mainwindowlogin.h"
+
+#include <QMessageBox>
+
+#include "mainwindow.h"
+#include "mainwindowlogin.h"
+
+extern MainWindow * gwmain;
+extern MainWindowLogin * gwlogin;
+
+MainWindowLogin::MainWindowLogin(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindowLogin)
+{
+    ui->setupUi(this);
+
+    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
+
+    ui->pushButton_Login->setDefault(true);  //When Click Enter, click
+
+    mpcheckdisplay = new QCheckBox(ui->lineEdit_password);
+    connect(mpcheckdisplay,SIGNAL(stateChanged(int)),this, SLOT(slot_checkbox_state_changed(int)));
+
+    // 设置样式表(图片为眼睛样式)
+    mpcheckdisplay->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
+    "QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/no_display.png);}"
+    "QCheckBox::indicator:checked {image: url(:/display.png);}");
+
+//    mpcheckdisplay->setGeometry(ui->lineEdit_password->pos().x() + 100,ui->lineEdit_password->pos().y() + 4,20,20);
+
+    mpcheckdisplay->setGeometry(ui->lineEdit_password->size().width() - 30, (ui->lineEdit_password->size().height() -20)/2,20,20);
+
+    connect(this,SIGNAL(loginstate(int,char *)),this,SLOT(onloginstate(int,char *)));
+
+    setWindowTitle("Login");
+}
+
+MainWindowLogin::~MainWindowLogin()
+{
+    delete ui;
+}
+
+void MainWindowLogin::on_pushButton_Login_clicked()
+{
+    ui->pushButton_Login->setText("Loging....");
+    ui->pushButton_Login->setEnabled(false);
+
+    mpthread = new std::thread(&MainWindowLogin::threadlogin,this);
+
+//    this->hide();
+//    gwmain->show();
+}
+
+void MainWindowLogin::slot_checkbox_state_changed(int state)
+{
+    if(state){
+       ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
+    }else{
+       ui->lineEdit_password->setEchoMode(QLineEdit::Password);
+    }
+
+}
+
+void MainWindowLogin::onloginstate(int nstate, char * strerr)
+{
+    if(nstate == 1)
+    {
+        this->hide();
+        gwmain->show();
+    }
+    else
+    {
+        if(nstate == 0)
+        {
+            QMessageBox::warning(this,"Warning",strerr,QMessageBox::YesAll);
+            ui->pushButton_Login->setText("Login");
+            ui->pushButton_Login->setEnabled(true);
+        }
+    }
+}
+
+void MainWindowLogin::threadlogin()
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
+    emit loginstate(0,"password error");
+}

+ 41 - 0
src/tool/fwupdate_admin/mainwindowlogin.h

@@ -0,0 +1,41 @@
+#ifndef MAINWINDOWLOGIN_H
+#define MAINWINDOWLOGIN_H
+
+#include <QMainWindow>
+#include <QCheckBox>
+#include <thread>
+
+namespace Ui {
+class MainWindowLogin;
+}
+
+class MainWindowLogin : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit MainWindowLogin(QWidget *parent = 0);
+    ~MainWindowLogin();
+
+signals:
+    void loginstate(int nstate,char * strerr);
+
+private slots:
+    void on_pushButton_Login_clicked();
+
+    void slot_checkbox_state_changed(int state);
+
+    void onloginstate(int nstate, char * strerr);
+
+private:
+    void threadlogin();
+
+private:
+    Ui::MainWindowLogin *ui;
+
+    QCheckBox * mpcheckdisplay;
+
+    std::thread * mpthread;
+};
+
+#endif // MAINWINDOWLOGIN_H

+ 99 - 0
src/tool/fwupdate_admin/mainwindowlogin.ui

@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindowLogin</class>
+ <widget class="QMainWindow" name="MainWindowLogin">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>375</width>
+    <height>269</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget">
+   <widget class="QPushButton" name="pushButton_Login">
+    <property name="geometry">
+     <rect>
+      <x>110</x>
+      <y>150</y>
+      <width>121</width>
+      <height>41</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>Login</string>
+    </property>
+   </widget>
+   <widget class="QLabel" name="label">
+    <property name="geometry">
+     <rect>
+      <x>40</x>
+      <y>30</y>
+      <width>91</width>
+      <height>31</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>User:</string>
+    </property>
+   </widget>
+   <widget class="QLabel" name="label_2">
+    <property name="geometry">
+     <rect>
+      <x>40</x>
+      <y>84</y>
+      <width>81</width>
+      <height>31</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>PassWord:</string>
+    </property>
+   </widget>
+   <widget class="QLineEdit" name="lineEdit_username">
+    <property name="geometry">
+     <rect>
+      <x>160</x>
+      <y>25</y>
+      <width>191</width>
+      <height>41</height>
+     </rect>
+    </property>
+   </widget>
+   <widget class="QLineEdit" name="lineEdit_password">
+    <property name="geometry">
+     <rect>
+      <x>159</x>
+      <y>78</y>
+      <width>191</width>
+      <height>41</height>
+     </rect>
+    </property>
+    <property name="inputMethodHints">
+     <set>Qt::ImhNone</set>
+    </property>
+   </widget>
+  </widget>
+  <widget class="QMenuBar" name="menubar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>375</width>
+     <height>28</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <tabstops>
+  <tabstop>lineEdit_username</tabstop>
+  <tabstop>lineEdit_password</tabstop>
+  <tabstop>pushButton_Login</tabstop>
+ </tabstops>
+ <resources/>
+ <connections/>
+</ui>

BIN
src/tool/fwupdate_admin/no_display.png


+ 6 - 0
src/tool/fwupdate_admin/res.qrc

@@ -0,0 +1,6 @@
+<RCC>
+    <qresource prefix="/">
+        <file>display.png</file>
+        <file>no_display.png</file>
+    </qresource>
+</RCC>