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