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?
This commit is contained in:
Philip Cohn-Cort 2020-04-13 05:54:11 +00:00
parent b3d78d6d07
commit c315170be5
6 changed files with 91 additions and 6 deletions

View file

@ -44,7 +44,7 @@ else()
find_package(Qca-qt5 ${QCA_MIN_VERSION} REQUIRED) find_package(Qca-qt5 ${QCA_MIN_VERSION} REQUIRED)
set(Qca_LIBRARY qca-qt5) 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(KF5_OPTIONAL_COMPONENTS DocTools)
set_package_properties(KF5Kirigami2 PROPERTIES set_package_properties(KF5Kirigami2 PROPERTIES

View file

@ -8,5 +8,6 @@ kdeconnect_add_plugin(kdeconnect_battery JSON kdeconnect_battery.json SOURCES ${
target_link_libraries(kdeconnect_battery target_link_libraries(kdeconnect_battery
kdeconnectcore kdeconnectcore
Qt5::DBus Qt5::DBus
KF5::Solid
KF5::I18n KF5::I18n
) )

View file

@ -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. This is an int so in the future we'll be able to subscribe to more events.
(see BatteryPlugin.ThresholdBatteryEvent) (see BatteryPlugin.ThresholdBatteryEvent)
<TODO>
Symmetrically, it sends its own battery information in packages with the same 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> </TODO>
It also sends packages with type kdeconnect.battery and a field "request": true, It also sends packages with type kdeconnect.battery and a field "request": true,

View file

@ -23,6 +23,10 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <KPluginFactory> #include <KPluginFactory>
#include <Solid/Device>
#include <Solid/Battery>
#include <Solid/Predicate>
#include <core/daemon.h> #include <core/daemon.h>
#include "batterydbusinterface.h" #include "batterydbusinterface.h"
@ -36,8 +40,8 @@ BatteryPlugin::BatteryPlugin(QObject* parent, const QVariantList& args)
, batteryDbusInterface(new BatteryDbusInterface(device())) , batteryDbusInterface(new BatteryDbusInterface(device()))
{ {
//TODO: Add battery reporting, could be based on: //TODO: Our protocol should support a dynamic number of batteries.
// https://www.linux-apps.com/content/show.php/battery+plasmoid+with+remaining+time?content=120309 //Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
} }
@ -45,6 +49,76 @@ void BatteryPlugin::connected()
{ {
NetworkPacket np(PACKET_TYPE_BATTERY_REQUEST, {{QStringLiteral("request"),true}}); NetworkPacket np(PACKET_TYPE_BATTERY_REQUEST, {{QStringLiteral("request"),true}});
sendPacket(np); 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() BatteryPlugin::~BatteryPlugin()

View file

@ -24,6 +24,7 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include <core/kdeconnectplugin.h> #include <core/kdeconnectplugin.h>
#define PACKET_TYPE_BATTERY QStringLiteral("kdeconnect.battery")
#define PACKET_TYPE_BATTERY_REQUEST QStringLiteral("kdeconnect.battery.request") #define PACKET_TYPE_BATTERY_REQUEST QStringLiteral("kdeconnect.battery.request")
Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_BATTERY) Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_BATTERY)
@ -41,6 +42,11 @@ public:
bool receivePacket(const NetworkPacket& np) override; bool receivePacket(const NetworkPacket& np) override;
void connected() 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: private:
// Keep these values in sync with THRESHOLD* constants in // Keep these values in sync with THRESHOLD* constants in
// kdeconnect-android:BatteryPlugin.java // kdeconnect-android:BatteryPlugin.java

View file

@ -131,9 +131,11 @@
"Website": "https://albertvaka.wordpress.com" "Website": "https://albertvaka.wordpress.com"
}, },
"X-KdeConnect-OutgoingPacketType": [ "X-KdeConnect-OutgoingPacketType": [
"kdeconnect.battery",
"kdeconnect.battery.request" "kdeconnect.battery.request"
], ],
"X-KdeConnect-SupportedPacketType": [ "X-KdeConnect-SupportedPacketType": [
"kdeconnect.battery" "kdeconnect.battery",
"kdeconnect.battery.request"
] ]
} }