添加自研批检仪检测命令

This commit is contained in:
ranchuan
2023-11-27 14:31:00 +08:00
parent b3a0d7b57c
commit a0b0f41c39
45 changed files with 3778 additions and 2628 deletions

View File

@@ -4,183 +4,168 @@
if_tcp::if_tcp()
{
this->connect_state=false;
this->timer_reconnect_=nullptr;
this->tcp_socket_=nullptr;
this->reconnect_timeout_ms=10000;
this->cfg_=syscfg();
this->connect_state = false;
this->timer_reconnect_ = nullptr;
this->tcp_socket_ = nullptr;
this->reconnect_timeout_ms = 10000;
this->cfg_ = syscfg();
this->timer_recv_end_=nullptr;
this->recv_end_timeout_ms=100;
qDebug("tcp created.");
this->timer_recv_end_ = nullptr;
this->recv_end_timeout_ms = 100;
qDebug("tcp created.");
}
if_tcp::~if_tcp()
{
}
void if_tcp::init()
{
if(nullptr == timer_reconnect_)
{
timer_reconnect_ = new QTimer(this);
if (nullptr == timer_reconnect_)
{
timer_reconnect_ = new QTimer(this);
connect(timer_reconnect_, &QTimer::timeout, this, &if_tcp::reconnect_cb);
}
timer_reconnect_->start(reconnect_timeout_ms);
connect(timer_reconnect_, &QTimer::timeout, this, &if_tcp::reconnect_cb);
}
timer_reconnect_->start(reconnect_timeout_ms);
if (nullptr == tcp_socket_)
{
tcp_socket_ = new QTcpSocket(this);
connect(tcp_socket_, SIGNAL(connected()), this, SLOT(connected_cb()));
connect(tcp_socket_, SIGNAL(disconnected()), this, SLOT(disconnected_cb()));
connect(tcp_socket_, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(state_changed_cb(QAbstractSocket::SocketState)));
connect(tcp_socket_, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(on_error_cb(QAbstractSocket::SocketError)));
connect(tcp_socket_, SIGNAL(readyRead()), this, SLOT(ready_read_cb()));
if(nullptr == tcp_socket_)
{
tcp_socket_ = new QTcpSocket(this);
connect(tcp_socket_, SIGNAL(connected()), this, SLOT(connected_cb()));
connect(tcp_socket_, SIGNAL(disconnected()), this, SLOT(disconnected_cb()));
connect(tcp_socket_, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(state_changed_cb(QAbstractSocket::SocketState)));
connect(tcp_socket_, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(on_error_cb(QAbstractSocket::SocketError)));
connect(tcp_socket_, SIGNAL(readyRead()), this, SLOT(ready_read_cb()));
tcp_socket_->setSocketOption(QAbstractSocket::LowDelayOption, 1);// 禁用Nagle算法.
tcp_socket_->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
if(cfg_->tcp_enable)
tcp_socket_->connectToHost(QHostAddress(cfg_->server_ip), cfg_->server_port);
}
tcp_socket_->setSocketOption(QAbstractSocket::LowDelayOption, 1); // 禁用Nagle算法.
tcp_socket_->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
if (cfg_->tcp_enable)
tcp_socket_->connectToHost(QHostAddress(cfg_->server_ip), cfg_->server_port);
}
}
// 获取本机地址
QString if_tcp::get_local_ip()
{
QList<QNetworkInterface> network =QNetworkInterface::allInterfaces();
foreach (QNetworkInterface net, network)
QList<QNetworkInterface> network = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface net, network)
{
QString netName = net.humanReadableName();
if (netName == "eth0")
{
QString netName=net.humanReadableName();
if(netName=="eth0")
QList<QNetworkAddressEntry> list = net.addressEntries(); // 获取IP地址与子掩码等
foreach (QNetworkAddressEntry address, list)
{
if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) // 获取IPv4的地址
{
QList<QNetworkAddressEntry> list=net.addressEntries(); // 获取IP地址与子掩码等
foreach(QNetworkAddressEntry address,list)
{
if(address.ip().protocol()==QAbstractSocket::IPv4Protocol) // 获取IPv4的地址
{
return address.ip().toString();
}
}
return address.ip().toString();
}
}
}
return QString();
}
return QString();
}
void if_tcp::reconnect_cb()
{
if(connect_state!=true)
if (connect_state != true)
{
QNetworkConfigurationManager mgr;
if (mgr.isOnline() == true)
{
QNetworkConfigurationManager mgr;
if(mgr.isOnline()==true)
{
if(cfg_->tcp_enable){
qDebug("network is online.");
tcp_socket_->connectToHost(QHostAddress(cfg_->server_ip), cfg_->server_port);
}
}
else
{
if(cfg_->tcp_enable){
qDebug("network is not online.");
}
}
// 设置为配置的ip地址
//if(QHostAddress(get_local_ip())!=QHostAddress(local_ip))
if(get_local_ip().isEmpty())
{
qDebug("modify local ip addr.");
QString str="ifconfig eth0 %1";
system(str.arg(cfg_->local_ip).toLocal8Bit().data());
str="route add default gw %1";
system(str.arg(cfg_->gateway_ip).toLocal8Bit().data());
}
if (cfg_->tcp_enable)
{
qDebug("network is online.");
tcp_socket_->connectToHost(QHostAddress(cfg_->server_ip), cfg_->server_port);
}
}
else
{
if (cfg_->tcp_enable)
{
qDebug("network is not online.");
}
}
// 设置为配置的ip地址
// if(QHostAddress(get_local_ip())!=QHostAddress(local_ip))
if (get_local_ip().isEmpty())
{
qDebug("modify local ip addr.");
QString str = "ifconfig eth0 %1";
system(str.arg(cfg_->local_ip).toLocal8Bit().data());
str = "route add default gw %1";
system(str.arg(cfg_->gateway_ip).toLocal8Bit().data());
}
}
}
void if_tcp::connected_cb()
{
timer_reconnect_->start();
connect_state=true;
emit tcp_connected_signal();
qInfo("connected");
timer_reconnect_->start();
connect_state = true;
emit tcp_connected_signal();
qInfo("connected");
}
void if_tcp::disconnected_cb()
{
connect_state=false;
qInfo("disconnected");
connect_state = false;
qInfo("disconnected");
}
void if_tcp::state_changed_cb(QAbstractSocket::SocketState nSocketState)
{
qInfo("state changed:%d",nSocketState);
qInfo("state changed:%d", nSocketState);
}
void if_tcp::on_error_cb(QAbstractSocket::SocketError nErrCode)
{
connect_state=false;
qWarning("on error:%d",nErrCode);
connect_state = false;
qWarning("on error:%d", nErrCode);
}
void if_tcp::ready_read_cb()
{
QByteArray data = tcp_socket_->readAll();
timer_reconnect_->start();
if(irq_fun){
irq_fun(myarray(data));
}
// qInfo()<< "recv data from tcp:"<<crc::byte_array_to_string(recv)<<endl;
QByteArray data = tcp_socket_->readAll();
timer_reconnect_->start();
if (irq_fun)
{
irq_fun(myarray(data));
}
// qInfo()<< "recv data from tcp:"<<crc::byte_array_to_string(recv)<<endl;
}
int if_tcp::write(myarray data)
{
if(nullptr == tcp_socket_ || false == connect_state)
{
//qWarning() << "Cant send data , TcpClient socket not connect.";
return 0;
}
if (nullptr == tcp_socket_ || false == connect_state)
{
// qWarning() << "Cant send data , TcpClient socket not connect.";
return 0;
}
int wb = tcp_socket_->write(data);
if(!tcp_socket_->flush())
{
//connect_state=false;
qWarning("write fial:[%s]",data.toHex(' ').data());
}
else
{
timer_reconnect_->start();
}
return wb;
int wb = tcp_socket_->write(data);
if (!tcp_socket_->flush())
{
// connect_state=false;
qWarning("write fial:[%s]", data.toHex(' ').data());
}
else
{
timer_reconnect_->start();
}
return wb;
}
InterFace *if_tcp_get()
{
static InterFace *if_=nullptr;
if(if_==nullptr){
if_=new if_tcp();
QTimer::singleShot(0,if_,&if_->init);
}
return if_;
static InterFace *if_ = nullptr;
if (if_ == nullptr)
{
if_ = new if_tcp();
QTimer::singleShot(0, if_, &InterFace::init);
}
return if_;
}
if_export(tcp,if_tcp_get);
if_export(tcp, if_tcp_get);