Commit c315170b authored by Philip Cohn-Cort's avatar Philip Cohn-Cort
Browse files

Finally, we have support for sending out Battery information.

## Summary

The core idea is as follows:

1. When a Link loads the BatteryPlugin, we query Solid for a list of batteries.
    1. If the list is empty, we print a warning message and return quickly
    2. Otherwise, we connect *two signals* to every object in that list
2. We send out a single new NetworkPacket as soon as we've processed that list
3. When either of those two signals emits, we send another new NetworkPacket

### Multi-battery Support

BUG: 357193

To handle devices with multiple batteries (requested in that bug), we average
together the battery percentages. This also includes a new field in the packet for
'number of batteries' called `batteryQuantity`. For backwards compatibility, we can
assume it has a default value of one.

This should ensure we support
- devices with no batteries at all (like many desktop machines)
- devices with hot-pluggable batteries (like those laptops with detachable screens)

### Concerns

Note that the implementation isn't perfect.
We'll need some new localizable text to make it clear that we now support sending
battery status information.

Then there's a rather significant question: maybe we should have two battery plugins
on each client, like we do for the `findmyphone`/`findthisdevice` plugins?

## Test Plan

We need to ensure that other clients (including those using the Android codebase)
will respond correctly. The main things to look at are
1. are these new packets sent when the plugin is enabled, and not sent when it's disabled?
2. is the charge percentage accurate?
3. is the charge state (charging, discharging, or full) accurate?
and
4. do we see the correct number of warnings for low-battery?
parent b3d78d6d
Pipeline #18272 failed with stage
in 5 minutes and 5 seconds
......@@ -44,7 +44,7 @@ else()
find_package(Qca-qt5 ${QCA_MIN_VERSION} REQUIRED)
set(Qca_LIBRARY qca-qt5)
set(KF5_REQUIRED_COMPONENTS I18n ConfigWidgets DBusAddons IconThemes Notifications KIO KCMUtils Service Kirigami2 People)
set(KF5_REQUIRED_COMPONENTS I18n ConfigWidgets DBusAddons IconThemes Notifications KIO KCMUtils Service Solid Kirigami2 People)
set(KF5_OPTIONAL_COMPONENTS DocTools)
set_package_properties(KF5Kirigami2 PROPERTIES
......
......@@ -8,5 +8,6 @@ kdeconnect_add_plugin(kdeconnect_battery JSON kdeconnect_battery.json SOURCES ${
target_link_libraries(kdeconnect_battery
kdeconnectcore
Qt5::DBus
KF5::Solid
KF5::I18n
)
......@@ -11,9 +11,11 @@ thresholdEvent (int) [optional when = 0, see below]:
This is an int so in the future we'll be able to subscribe to more events.
(see BatteryPlugin.ThresholdBatteryEvent)
<TODO>
Symmetrically, it sends its own battery information in packages with the same
format.
format. We only look for the 'primary' battery of the system, if one is present.
<TODO>
Support for devices with multiple batteries isn't there yet.
</TODO>
It also sends packages with type kdeconnect.battery and a field "request": true,
......
......@@ -23,6 +23,10 @@
#include <KLocalizedString>
#include <KPluginFactory>
#include <Solid/Device>
#include <Solid/Battery>
#include <Solid/Predicate>
#include <core/daemon.h>
#include "batterydbusinterface.h"
......@@ -36,8 +40,8 @@ BatteryPlugin::BatteryPlugin(QObject* parent, const QVariantList& args)
, batteryDbusInterface(new BatteryDbusInterface(device()))
{
//TODO: Add battery reporting, could be based on:
// https://www.linux-apps.com/content/show.php/battery+plasmoid+with+remaining+time?content=120309
//TODO: Our protocol should support a dynamic number of batteries.
//Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
}
......@@ -45,6 +49,76 @@ void BatteryPlugin::connected()
{
NetworkPacket np(PACKET_TYPE_BATTERY_REQUEST, {{QStringLiteral("request"),true}});
sendPacket(np);
const auto batteryDevice = Solid::DeviceInterface::Type::Battery;
const auto primary = Solid::Battery::BatteryType::PrimaryBattery;
QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary));
if (batteries.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "No Primary Battery detected on this system. This may be a bug.";
QList<Solid::Device> allBatteries = Solid::Device::listFromType(batteryDevice);
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Total quantity of batteries found: " << allBatteries.size();
return;
}
const Solid::Battery* chosen = batteries.first().as<Solid::Battery>();
connect(chosen, &Solid::Battery::chargeStateChanged, this, &BatteryPlugin::chargeChanged);
connect(chosen, &Solid::Battery::chargePercentChanged, this, &BatteryPlugin::chargeChanged);
// Explicitly send the current charge
chargeChanged();
}
void BatteryPlugin::chargeChanged()
{
bool isAnyBatteryCharging = false;
int batteryQuantity = 0;
int cumulativeCharge = 0;
const auto batteryDevice = Solid::DeviceInterface::Type::Battery;
const auto primary = Solid::Battery::BatteryType::PrimaryBattery;
QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary));
for (auto device : batteries) {
const Solid::Battery* battery = device.as<Solid::Battery>();
// Don't look at batteries that have been detached
if (battery->isPresent()) {
batteryQuantity++;
cumulativeCharge += battery->chargePercent();
if (battery->chargeState() == Solid::Battery::ChargeState::Charging) {
isAnyBatteryCharging = true;
}
}
}
if (batteryQuantity == 0) {
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Primary Battery seems to have been removed. Suspending packets until it is reconnected.";
return;
}
// Load a new Battery object to represent the first device in the list
Solid::Battery* chosen = batteries.first().as<Solid::Battery>();
// Prepare an outgoing network packet
NetworkPacket status(PACKET_TYPE_BATTERY, {{}});
status.set(QStringLiteral("isCharging"), isAnyBatteryCharging);
status.set(QStringLiteral("currentCharge"), cumulativeCharge / batteryQuantity);
// FIXME: In future, we should consider sending an array of battery objects
status.set(QStringLiteral("batteryQuantity"), batteryQuantity);
// We consider primary battery to be low if it will only last for 5 minutes or
// less. This doesn't necessarily work if (for example) Solid finds multiple
// batteries.
if (chosen->remainingTime() < 600 && chosen->chargeState() == Solid::Battery::ChargeState::Discharging) {
status.set(QStringLiteral("thresholdEvent"), (int)ThresholdBatteryLow);
} else {
status.set(QStringLiteral("thresholdEvent"), (int)ThresholdNone);
}
sendPacket(status);
}
BatteryPlugin::~BatteryPlugin()
......
......@@ -24,6 +24,7 @@
#include <QLoggingCategory>
#include <core/kdeconnectplugin.h>
#define PACKET_TYPE_BATTERY QStringLiteral("kdeconnect.battery")
#define PACKET_TYPE_BATTERY_REQUEST QStringLiteral("kdeconnect.battery.request")
Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_BATTERY)
......@@ -41,6 +42,11 @@ public:
bool receivePacket(const NetworkPacket& np) override;
void connected() override;
// NB: This may be connected to zero or two signals in Solid::Battery -
// chargePercentageChanged and chargeStatusChanged.
// See inline comments for further details
void chargeChanged();
private:
// Keep these values in sync with THRESHOLD* constants in
// kdeconnect-android:BatteryPlugin.java
......
......@@ -131,9 +131,11 @@
"Website": "https://albertvaka.wordpress.com"
},
"X-KdeConnect-OutgoingPacketType": [
"kdeconnect.battery",
"kdeconnect.battery.request"
],
"X-KdeConnect-SupportedPacketType": [
"kdeconnect.battery"
"kdeconnect.battery",
"kdeconnect.battery.request"
]
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment