1 Star 0 Fork 1

AmumuI/swupdate_progress

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
swupdate.cpp 3.75 KB
一键复制 编辑 原始数据 按行查看 历史
xiefu 提交于 2024-09-05 11:20 +08:00 . first commit.
#include <QSocketNotifier>
#include <QVariant>
#include <QTimer>
#include <QDebug>
#include <unistd.h>
#include <progress_ipc.h>
#include "swupdate.h"
SWUpdate::SWUpdate(QObject *parent)
: QObject(parent)
{
m_ipcFd = -1;
m_timer = new QTimer(this);
m_timer->setInterval(1000);
m_timer->start();
connect(m_timer, &QTimer::timeout, this, &SWUpdate::onConnected);
}
SWUpdate::~SWUpdate()
{
if (m_ipcFd > 0) {
close(m_ipcFd);
}
delete m_timer;
}
int SWUpdate::getProgressMsgInt(QString key)
{
return m_msg.value(key).toInt();
}
QString SWUpdate::getProgressMsgStr(QString key)
{
return m_msg.value(key).toString();
}
QString SWUpdate::getProgressMsgALL()
{
return m_msg.value("cur_step").toString()+ " of " + m_msg.value("nsteps").toString()+ " "+
m_msg.value("cur_percent").toString() +"% ("+m_msg.value("cur_image").toString()+"), dwl "+
m_msg.value("dwl_percent").toString() +"% of " +m_msg.value("dwl_bytes").toString() +"bytes";
}
void SWUpdate::onConnected(void)
{
if (m_ipcFd < 0) {
m_ipcFd = progress_ipc_connect(false);
qDebug() << "try to connect swupdate";
}
if (m_ipcFd > 0) {
wait_update = true;
QSocketNotifier *ipcConnect = new QSocketNotifier(m_ipcFd, QSocketNotifier::Read, this);
connect(ipcConnect, &QSocketNotifier::activated, this, &SWUpdate::onActivated);
m_msg.clear();
m_timer->stop();
qDebug() << "onConnectIpc m_ipcFd " << m_ipcFd;
qDebug() << "could connect swupdate";
} else {
qDebug() << "could not connect swupdate";
}
}
void SWUpdate::onActivated(void)
{
struct progress_msg msg;
if (progress_ipc_receive(&m_ipcFd, &msg) <= 0)
return;
if (wait_update) {
if (msg.status == START || msg.status == RUN) {
Q_EMIT start();
switch (msg.source) {
case SOURCE_UNKNOWN:
m_msg.insert("interface", "UNKNOWN");
break;
case SOURCE_WEBSERVER:
m_msg.insert("interface", "WEBSERVER");
break;
case SOURCE_SURICATTA:
m_msg.insert("interface", "BACKEND");
break;
case SOURCE_DOWNLOADER:
m_msg.insert("interface", "DOWNLOADER");
break;
// case SOURCE_CHUNKS_DOWNLOADER:
// m_msg.insert("interface", "CHUNKS DOWNLOADER");
// break;
case SOURCE_LOCAL:
m_msg.insert("interface", "LOCAL");
break;
}
m_curstep = 0;
wait_update = false;
}
}
/*
* Be sure that string in message are Null terminated
*/
if (msg.infolen > 0) {
if (msg.infolen >= sizeof(msg.info) - 1) {
msg.infolen = sizeof(msg.info) - 1;
}
msg.info[msg.infolen] = '\0';
m_msg.insert("info", msg.info);
}
msg.cur_image[sizeof(msg.cur_image) - 1] = '\0';
if (!wait_update) {
if (msg.cur_step > 0) {
m_msg.insert("cur_step", msg.cur_step);
m_msg.insert("nsteps", msg.nsteps);
m_msg.insert("cur_percent", msg.cur_percent);
m_msg.insert("cur_image", msg.cur_image);
m_msg.insert("dwl_percent", msg.dwl_percent);
m_msg.insert("dwl_bytes", msg.dwl_bytes);
if (msg.cur_step != m_curstep || msg.cur_percent != m_percent) {
m_curstep = msg.cur_step;
m_percent = msg.cur_percent;
Q_EMIT msgChanged();
}
}
}
switch (msg.status) {
case SUCCESS:
case FAILURE:
Q_EMIT stop();
break;
default:
break;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/AmumuL/swupdate_progress.git
git@gitee.com:AmumuL/swupdate_progress.git
AmumuL
swupdate_progress
swupdate_progress
master

搜索帮助