Skip to main content

kdeconnect_proto/packet/
battery.rs

1//! The Battery plugin allows a device to expose the status of its battery.
2use serde::{Deserialize, Serialize};
3
4use crate::packet::{NetworkPacket, NetworkPacketBody};
5
6/// This packet is a battery status update.
7///
8/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectbattery>
9#[derive(Serialize, Deserialize, Debug, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct BatteryPacket {
12    /// The current battery percentage, typically between `0` and `100`. If the value is `-1`,
13    /// the device should be treated as though it has no battery.
14    pub current_charge: i8,
15
16    /// A boolean value indicating whether the battery is currently charging.
17    pub is_charging: bool,
18
19    /// Either `1` if the battery is below the threshold level, `0` otherwise.
20    /// The threshold is chosen arbitrarily by the reporting device.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    #[serde(default)]
23    pub threshold_event: Option<u8>,
24}
25
26impl NetworkPacket {
27    /// Make a [`NetworkPacket`] with a [`BatteryPacket`] payload.
28    pub fn battery(current_charge: i8, is_charging: bool) -> Self {
29        NetworkPacket::new(NetworkPacketBody::Battery(BatteryPacket {
30            current_charge,
31            is_charging,
32            threshold_event: None,
33        }))
34    }
35
36    /// Make a [`NetworkPacket`] with a [`BatteryPacket`] payload and a threshold event.
37    pub fn battery_with_threshold(
38        current_charge: i8,
39        is_charging: bool,
40        threshold_event: u8,
41    ) -> Self {
42        NetworkPacket::new(NetworkPacketBody::Battery(BatteryPacket {
43            current_charge,
44            is_charging,
45            threshold_event: Some(threshold_event),
46        }))
47    }
48}