proto-debuger/protoDebuger/sharedata.cpp

220 lines
5.8 KiB
C++
Raw Normal View History

2021-04-11 03:28:04 +00:00
#include "sharedata.h"
2021-04-17 17:57:39 +00:00
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QFile>
2021-04-18 15:16:03 +00:00
#include <QThread>
2021-05-01 06:09:18 +00:00
#include <QQuickItem>
#include <QQmlContext>
2021-04-11 03:28:04 +00:00
2021-07-19 15:52:38 +00:00
QmlShareData::QmlShareData(QObject *parent) : QObject(parent),
2021-04-29 14:26:45 +00:00
m_qml_view_(nullptr),
2021-08-29 08:13:50 +00:00
m_luavm_status_(false),
2021-09-09 16:22:33 +00:00
m_network_(nullptr),
m_tcp_server(nullptr)
2021-04-12 15:39:06 +00:00
{
2021-04-29 14:26:45 +00:00
m_serial_controller_ = new SerialController(nullptr);
2021-04-17 17:57:39 +00:00
QFile file("Test.lua");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
2021-04-29 14:26:45 +00:00
m_lua_string = in.readAll();
int ret = m_lua_.DoString(m_lua_string);
2021-04-18 15:16:03 +00:00
if(ret < 0){
qDebug()<<"默认lua脚本加载错误";
2021-04-20 15:35:18 +00:00
}else{
2021-04-29 14:26:45 +00:00
m_luavm_status_ = true;
2021-04-18 15:16:03 +00:00
}
2021-04-17 17:57:39 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::SetQuickView(QQuickWidget *view)
2021-04-17 17:57:39 +00:00
{
QStringList comList;
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
comList<<info.portName();
}
2021-05-01 06:09:18 +00:00
// 设置qml基本属性
2021-04-29 14:26:45 +00:00
this->m_qml_view_ = view;
2021-05-01 06:09:18 +00:00
QObject *qmlObject = view->rootObject()->findChild<QObject*>("SerialSelect",
2021-04-17 17:57:39 +00:00
Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != qmlObject)
qmlObject->setProperty("comlist",comList);
2021-05-01 06:09:18 +00:00
m_qml_protodebug_ = view->rootObject()->findChild<QObject*>("ProtoDebug",
2021-04-17 17:57:39 +00:00
Qt::FindChildOption::FindChildrenRecursively);
2021-05-01 06:09:18 +00:00
2021-04-29 14:26:45 +00:00
if(nullptr != m_qml_protodebug_)
m_qml_protodebug_->setProperty("lua_script_text",m_lua_string);
2021-04-17 17:57:39 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::OnDataRecv(QByteArray arr)
2021-04-17 17:57:39 +00:00
{
2021-04-29 14:26:45 +00:00
if(m_luavm_status_)
2021-05-03 08:49:33 +00:00
m_lua_.OnSerialData(QString(arr));
2021-04-17 17:57:39 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::ShowDataInQML(QString x)
2021-04-17 17:57:39 +00:00
{
2021-04-29 14:26:45 +00:00
if(nullptr != m_qml_protodebug_){
QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, x));
2021-04-17 17:57:39 +00:00
}
2021-04-12 15:39:06 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::SendUartData(const char *data)
2021-04-18 15:16:03 +00:00
{
if(nullptr != data){
2021-05-03 16:05:38 +00:00
this->m_serial_controller_->SendData((char *)data,strlen(data));
2021-04-18 15:16:03 +00:00
return 0;
}
return -1;
}
2021-07-19 15:52:38 +00:00
int QmlShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
2021-04-12 15:39:06 +00:00
{
2021-04-29 14:26:45 +00:00
if(m_serial_controller_->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){
m_serial_controller_->SetListener(this);
2021-04-12 15:39:06 +00:00
return 0;
}
qDebug()<<"openserial failed";
return -1;
}
2021-07-19 15:52:38 +00:00
int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type)
2021-04-29 14:26:45 +00:00
{
if(type == NetworkController::TYPE_TCP_CLIENT){
2021-08-29 08:13:50 +00:00
qDebug()<<"connect to server";
if(m_network_ == nullptr)
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
else{
delete m_network_;
m_network_ = nullptr;
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
}
2021-08-29 15:50:41 +00:00
connect(m_network_,SIGNAL(on_connected()),this,SLOT(on_network_conected()));
connect(m_network_,SIGNAL(on_disconnectd()),this,SLOT(on_network_disconected()));
2021-05-03 08:49:33 +00:00
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
2021-08-29 15:50:41 +00:00
return 0;
2021-04-29 14:26:45 +00:00
}
2021-05-03 06:07:04 +00:00
if((type == NetworkController::TYPE_UDP_CLIENT) ||
(type == NetworkController::TYPE_UDP_SERVER)){
2021-05-03 08:49:33 +00:00
m_network_ = new NetworkController((NetworkController::NetworkType)type,ip,port);
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
2021-08-30 14:56:38 +00:00
emit(sendToQml(11));
2021-05-03 08:49:33 +00:00
2021-05-03 06:07:04 +00:00
return 0;
2021-04-29 14:26:45 +00:00
}
2021-05-02 18:02:25 +00:00
if(type == NetworkController::TYPE_TCP_SERVER){
2021-09-09 16:22:33 +00:00
m_tcp_server = new TcpServerLibevent(port,ip.toStdString());
m_tcp_server->StartServerAsync();
2021-05-03 08:49:33 +00:00
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
2021-09-01 16:16:47 +00:00
connect(m_network_,SIGNAL(on_connected()),this,SLOT(on_network_conected()));
2021-05-03 08:49:33 +00:00
2021-05-02 18:02:25 +00:00
return 0;
}
2021-05-01 15:41:40 +00:00
return -1;
2021-04-29 14:26:45 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::closeNetwork()
2021-05-03 05:50:07 +00:00
{
m_network_->Close();
}
2021-07-19 15:52:38 +00:00
int QmlShareData::closeSerial()
2021-04-11 03:28:04 +00:00
{
2021-04-29 14:26:45 +00:00
if(m_serial_controller_->CloseSerial() == 0){
2021-04-13 17:28:19 +00:00
qDebug()<<"close serial ok";
return 0;
}
qDebug()<<"cloase serial failed";
return -1;
2021-04-11 03:28:04 +00:00
}
2021-04-14 16:52:05 +00:00
2021-07-19 15:52:38 +00:00
int QmlShareData::TestLua()
2021-04-14 16:52:05 +00:00
{
2021-07-19 15:52:38 +00:00
m_lua_.OnSerialData("ceshi");
return 0;
2021-04-14 16:52:05 +00:00
}
2021-04-16 18:27:32 +00:00
2021-07-19 15:52:38 +00:00
int QmlShareData::TestShowData()
2021-04-16 18:27:32 +00:00
{
2021-04-29 14:26:45 +00:00
if(nullptr != m_qml_protodebug_){
QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, QString("test\r\n")));
2021-04-16 18:27:32 +00:00
}
return 0;
}
2021-07-19 15:52:38 +00:00
int QmlShareData::updateLuaScript(QString str)
2021-04-18 15:16:03 +00:00
{
qDebug()<<QThread::currentThreadId();
2021-04-29 14:26:45 +00:00
m_lua_string = str;
2021-04-18 15:16:03 +00:00
qDebug()<<str;
2021-09-01 16:16:47 +00:00
int ret = m_lua_.UpdateScript(m_lua_string);
2021-04-18 15:16:03 +00:00
if(ret < 0){
2021-04-20 15:35:18 +00:00
qDebug()<<"更新lua脚本失败";
2021-04-29 14:26:45 +00:00
m_luavm_status_ = false;
2021-04-18 15:16:03 +00:00
return -1;
}
2021-04-29 14:26:45 +00:00
m_luavm_status_ = true;
2021-04-27 13:02:32 +00:00
return 0;
2021-04-18 15:16:03 +00:00
}
2021-07-19 15:52:38 +00:00
int QmlShareData::saveLuaScript(QString s)
2021-04-19 16:52:06 +00:00
{
QFile file("Test.lua"); //---打开文件
if (file.open(QIODevice::WriteOnly) )
{
//-----将数据写入文件
qint64 LineLen = file.write(s.toStdString().c_str(), s.size());
2021-04-27 13:02:32 +00:00
if (LineLen <= 0)
2021-04-19 16:52:06 +00:00
{
return -1;
}
}
file.close();
2021-04-20 15:35:18 +00:00
return 0;
}
2021-07-19 15:52:38 +00:00
bool QmlShareData::luaStatus()
2021-04-20 15:35:18 +00:00
{
2021-04-29 14:26:45 +00:00
return m_luavm_status_;
2021-04-19 16:52:06 +00:00
}
2021-09-01 16:16:47 +00:00
bool QmlShareData::DoLuaCmd(QString)
{
}
2021-07-19 15:52:38 +00:00
void QmlShareData::on_network_data_recv(){
2021-05-03 08:49:33 +00:00
qDebug()<<"recv data";
2021-08-31 14:23:45 +00:00
char dat[4096] = {0};
auto ret = this->m_network_->ReadData((int8_t * )dat);
qDebug()<<ret.len<<ret.addr;
if(ret.len > 0){
m_lua_.OnNetworkData((char*)ret.addr.toStdString().data(),dat,ret.port.toUInt());
}
}
void QmlShareData::on_udp_data_recv(){
2021-05-03 08:49:33 +00:00
char dat[4096] = {0};
auto ret = this->m_network_->ReadData((int8_t * )dat);
qDebug()<<dat;
if(ret.len > 0){
m_lua_.OnNetworkData((char*)ret.addr.toStdString().data(),dat,ret.port.toUInt());
}
}
2021-08-29 15:50:41 +00:00
void QmlShareData::on_network_conected()
{
emit(sendToQml(11));
}
void QmlShareData::on_network_disconected()
{
emit(disConnected());
}
2021-04-17 17:57:39 +00:00