Skip to content

Commit

Permalink
Use new calaos API for network/info
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Aug 11, 2023
1 parent 1719df2 commit e1d7171
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 399 deletions.
38 changes: 31 additions & 7 deletions qml/desktop/ConfigTabs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,36 @@ BorderImage {
ConfigTabLabelValue {
id: uptimeTxt
labelText: qsTr("System started since:")
valueText: "-"

Component.onCompleted: {
var days = calaosApp.getUptimeDays()
if (days == 1) {
uptimeTxt.valueText = qsTr("%1 day").arg(days)
} else if (days > 1) {
uptimeTxt.valueText = qsTr("%1 days").arg(days)
property int uptime: calaosApp.uptime
onUptimeChanged: uptimeTxt.valueText = formatTime(uptime)

function formatTime(seconds) {
if (seconds < 60) {
return seconds + ' sec';
} else if (seconds < 3600) {
const minutes = Math.floor(seconds / 60);
return minutes + ' min';
} else if (seconds < 86400) {
const hours = Math.floor(seconds / 3600);
if (hours > 1) {
return qsTr("%1 hours").arg(hours)
} else {
return qsTr("%1 hour").arg(hours)
}
} else {
const days = Math.floor(seconds / 86400);
if (days > 1) {
return qsTr("%1 days").arg(days)
} else {
return qsTr("%1 day").arg(days)
}
}
}

Component.onCompleted: {
calaosApp.updateSystemInfo()
}
}

Item { width: 1; height: Units.dp(20) }
Expand All @@ -85,6 +105,10 @@ BorderImage {

ConfigTabLabelValue {
labelText: qsTr("Network:")

Component.onCompleted: {
calaosApp.updateNetworkInfo()
}
}

Repeater {
Expand Down
68 changes: 50 additions & 18 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void Application::createQmlApp()

update_isSnapshotBoot(false);
CalaosOsAPI::Instance()->getFsStatus(
[this](bool success, const QJsonObject &o)
[this](bool success, const QJsonValue &o)
{
if (!success)
{
Expand Down Expand Up @@ -165,6 +165,11 @@ void Application::createQmlApp()

update_isSnapshotBoot(false);
});

update_uptime(0);
update_cpuUsage(0);
update_memoryUsage(0);
updateSystemInfo();
#else
update_isSnapshotBoot(false);
#endif
Expand Down Expand Up @@ -244,8 +249,6 @@ void Application::createQmlApp()
controlPanelModel = new ControlPanelModel(this);
engine.rootContext()->setContextProperty("controlPanelModel", controlPanelModel);

update_machineName(Machine::getHostname());

//network info timer
updateNetworkInfo();
auto netTimer = new QTimer(this);
Expand Down Expand Up @@ -627,15 +630,9 @@ void Application::rollbackSnapshot()
#endif
}

quint32 Application::getUptimeDays()
{
return Machine::getMachineUptime() / 60 / 60 / 24;
}

void Application::sysInfoTimerSlot()
{
update_cpuUsage(Machine::getCpuUsage());
update_memoryUsage(Machine::getMemoryUsage());
updateSystemInfo();
}

void Application::setupLanguage()
Expand Down Expand Up @@ -698,12 +695,47 @@ void Application::pushNotificationReceived(const QString &uuid)

void Application::updateNetworkInfo()
{
m_netAddresses->clear();
QList<NetworkInfo *> nets = Machine::getNetworkInfo();
for (int i = 0;i < nets.count();i++)
{
if (nets.at(i)->get_isLoopback())
continue;
m_netAddresses->append(nets.at(i));
}
CalaosOsAPI::Instance()->getNetworkInterfaces(
[this](bool success, const QJsonValue &data)
{
m_netAddresses->clear();

if (!success) return;

const QJsonArray &jarr = data.toArray();

for (int i = 0;i < jarr.size();i++)
{
const QJsonObject &o = jarr[i].toObject();

if (o["is_loopback"].toBool())
continue;

NetworkInfo *net = new NetworkInfo();

net->update_netinterface(o["name"].toString());
net->update_ipv4(o["ipv4"].toString());
net->update_ipv6(o["ipv6"].toString());
net->update_mac(o["mac"].toString());
net->update_isLoopback(o["is_loopback"].toBool());

m_netAddresses->append(net);
}
}
);
}

void Application::updateSystemInfo()
{
CalaosOsAPI::Instance()->getSystemInfo(
[this](bool success, const QJsonValue &data)
{
if (!success) return;

update_cpuUsage(data["cpu_usage"].toInt());
update_memoryUsage(data["mem_usage"].toInt());
update_machineName(data["hostname"].toString());
update_uptime(data["uptime"].toInt());
}
);
}
7 changes: 4 additions & 3 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Application : public QAPP
QML_OBJMODEL_PROPERTY(NetworkInfo, netAddresses)
QML_READONLY_PROPERTY(int, cpuUsage)
QML_READONLY_PROPERTY(int, memoryUsage)
QML_READONLY_PROPERTY(int, uptime)
QML_READONLY_PROPERTY(bool, hasWebEngine)
QML_READONLY_PROPERTY(bool, hasInstall) //If running on live install enable installation mode
QML_READONLY_PROPERTY(bool, isSnapshotBoot) //if system has been booted read only from a snapshot
Expand Down Expand Up @@ -86,20 +87,20 @@ class Application : public QAPP
Q_INVOKABLE void restartApp();
Q_INVOKABLE void rollbackSnapshot();

Q_INVOKABLE quint32 getUptimeDays();

QQmlApplicationEngine *getEngine() { return &engine; }

Q_INVOKABLE void setLanguage(QString code);

Q_INVOKABLE void updateNetworkInfo();
Q_INVOKABLE void updateSystemInfo();

private slots:
void homeLoaded(const QVariantMap &homeData);
void loginFailed();
void networkStatusChanged();
void calaosServerDetected();
void sysInfoTimerSlot();
void pushNotificationReceived(const QString &uuid);
void updateNetworkInfo();

private:
QQmlApplicationEngine engine;
Expand Down
35 changes: 16 additions & 19 deletions src/CalaosOsAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void CalaosOsAPI::restartApp(std::function<void (bool)> callback)
doPost("/api/system/restart", {}, callback);
}

void CalaosOsAPI::getFsStatus(std::function<void (bool, const QJsonObject &)> callback)
void CalaosOsAPI::getFsStatus(std::function<void (bool, const QJsonValue &)> callback)
{
doGet("/api/system/fs_status", callback);
}
Expand All @@ -47,7 +47,7 @@ void CalaosOsAPI::rollbackSnapshot(std::function<void (bool)> callback)
doPost("/api/system/rollback_snapshot", {}, callback);
}

void CalaosOsAPI::listInstallDevices(std::function<void (bool, const QJsonObject &)> callback)
void CalaosOsAPI::listInstallDevices(std::function<void (bool, const QJsonValue &)> callback)
{
doGet("/api/system/install/list_devices", callback);
}
Expand Down Expand Up @@ -162,6 +162,16 @@ void CalaosOsAPI::startInstallation(QString device, std::function<void (bool)> c
jobs->start();
}

void CalaosOsAPI::getNetworkInterfaces(std::function<void (bool, const QJsonValue &)> callback)
{
doGet("/api/network/list", callback);
}

void CalaosOsAPI::getSystemInfo(std::function<void (bool, const QJsonValue &)> callback)
{
doGet("/api/system/info", callback);
}

void CalaosOsAPI::checkErrors(const QJsonDocument &jdoc, NetworkRequest *n)
{
QJsonObject jobj = jdoc.object();
Expand Down Expand Up @@ -226,7 +236,7 @@ void CalaosOsAPI::doPost(QString apiPath, const QByteArray &postData, std::funct
jobs->start();
}

void CalaosOsAPI::doGet(QString apiPath, std::function<void (bool, const QJsonObject &)> callback)
void CalaosOsAPI::doGet(QString apiPath, std::function<void (bool, const QJsonValue &)> callback)
{
AsyncJobs *jobs = new AsyncJobs(this);

Expand Down Expand Up @@ -254,21 +264,8 @@ void CalaosOsAPI::doGet(QString apiPath, std::function<void (bool, const QJsonOb
}
else
{
QJsonParseError err;
QJsonDocument doc;

doc = QJsonDocument::fromJson(jobj["output"].toString().toUtf8(), &err);
auto o = doc.object();
if (err.error != QJsonParseError::NoError)
{
auto e = "JSON parse error " + err.errorString() + " at offset: " + QString::number(err.offset);
lastError.append(e);
job->emitFailed();
}
else
{
job->emitSuccess(o);
}
QJsonValue v = jobj["output"];
job->emitSuccess(v);
}
}
else
Expand All @@ -293,7 +290,7 @@ void CalaosOsAPI::doGet(QString apiPath, std::function<void (bool, const QJsonOb

connect(jobs, &AsyncJobs::finished, this, [callback](const QVariant &data)
{
callback(true, data.toJsonObject());
callback(true, data.toJsonValue());
});

jobs->start();
Expand Down
9 changes: 6 additions & 3 deletions src/CalaosOsAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ class CalaosOsAPI: public QObject

void rebootMachine(std::function<void(bool success)> callback);
void restartApp(std::function<void(bool success)> callback);
void getFsStatus(std::function<void(bool success, const QJsonObject &res)> callback);
void getFsStatus(std::function<void(bool success, const QJsonValue &res)> callback);

void rollbackSnapshot(std::function<void(bool success)> callback);

void listInstallDevices(std::function<void(bool success, const QJsonObject &res)> callback);
void listInstallDevices(std::function<void(bool success, const QJsonValue &res)> callback);
void startInstallation(QString device,
std::function<void(bool success)> callbackFinished,
std::function<void(QString out)> callbackStdout);

void getNetworkInterfaces(std::function<void(bool success, const QJsonValue &res)> callback);
void getSystemInfo(std::function<void(bool success, const QJsonValue &res)> callback);

private:
void checkErrors(const QJsonDocument &jdoc, NetworkRequest *n = nullptr);
void doPost(QString apiPath, const QByteArray &postData, std::function<void(bool success)> callback);
void doGet(QString apiPath, std::function<void(bool success, const QJsonObject &res)> callback);
void doGet(QString apiPath, std::function<void(bool success, const QJsonValue &res)> callback);

QNetworkAccessManager *netManager;

Expand Down
Loading

0 comments on commit e1d7171

Please sign in to comment.