irgendwas mit json

This commit is contained in:
Flummi 2024-06-07 01:56:43 +02:00
parent 6290a78666
commit b2e7b948fe
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
3 changed files with 35 additions and 32 deletions

View File

@ -1,7 +1,6 @@
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.kirigami as Kirigami
import "./lib/helper.js" as Helper
@ -14,7 +13,7 @@ Item {
anchors.topMargin: 10 - batteryheight
anchors.bottomMargin: 10 - batteryheight
color: "transparent"
border.color: PlasmaCore.Theme.textColor
border.color: Kirigami.Theme.textColor
radius: 4
Item {
@ -33,8 +32,8 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 1
anchors.bottomMargin: 1
source: Helper.batteryIcon(batteryPercent)
color: batteryPercent == -1 ? colorFull : PlasmaCore.Theme.textColor
source: Helper.batteryIcon(batteryStatus)
color: batteryPercent == -1 ? colorFull : Kirigami.Theme.textColor
}
}
}
@ -46,7 +45,7 @@ Item {
radius: 4
height: parent.height / 3
width: 2
color: PlasmaCore.Theme.textColor
color: Kirigami.Theme.textColor
}
MouseArea {

View File

@ -1,12 +1,13 @@
const batteryLevel = batteryPercent => ({
"-2": "not connected",
"-1": "charging"
})[batteryPercent] ?? batteryPercent + "%";
const batteryLevel = (batteryStatus, batteryPercent) => ({
"BATTERY_UNAVAILABLE": "not connected",
"BATTERY_CHARGING": "charging (" + batteryPercent + "%)"
})[batteryStatus] ?? batteryPercent + "%";
const batteryIcon = batteryPercent => ({
"-2": "action-unavailable",
"-1": "reload"
})[batteryPercent] ?? "audio-headphones-symbolic";
const batteryIcon = batteryStatus => ({
"BATTERY_UNAVAILABLE": "action-unavailable",
"BATTERY_AVAILABLE": "audio-headphones-symbolic",
"BATTERY_CHARGING": "reload"
})[batteryStatus] ?? "audio-headphones-symbolic";
const hex2rgb = (hex, h = hex.replace('#', '0x')) => ({ r: h >> 16 & 255, g: h >> 8 & 255, b: h & 255 });
const rgb2Hex = rgb => "#" + ((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);

View File

@ -11,33 +11,36 @@ import "./lib/helper.js" as Helper
PlasmoidItem {
id: main
property int pollingrate: Plasmoid.configuration.pollingrate
property int batteryheight: Plasmoid.configuration.batteryheight
property string colorEmpty: Plasmoid.configuration.colorEmpty
property string colorHalf: Plasmoid.configuration.colorHalf
property string colorFull: Plasmoid.configuration.colorFull
readonly property int pollingrate: Plasmoid.configuration.pollingrate
readonly property int batteryheight: Plasmoid.configuration.batteryheight
readonly property string colorEmpty: Plasmoid.configuration.colorEmpty
readonly property string colorHalf: Plasmoid.configuration.colorHalf
readonly property string colorFull: Plasmoid.configuration.colorFull
property bool notifications: Plasmoid.configuration.notifications
property int batteryThreshold: Plasmoid.configuration.batteryThreshold
readonly property bool notifications: Plasmoid.configuration.notifications
readonly property int batteryThreshold: Plasmoid.configuration.batteryThreshold
property bool debug_active: Plasmoid.configuration.debug_active
property int debug_charge: Plasmoid.configuration.debug_charge
readonly property bool debug_active: Plasmoid.configuration.debug_active
readonly property int debug_charge: Plasmoid.configuration.debug_charge
property int batteryPercent: -2
property bool notification_sent: false;
property string batteryStatus: "BATTERY_UNAVAILABLE"
property int batteryPercent: 0
property bool notification_sent: false
Plasma5Support.DataSource {
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -bc"]
connectedSources: ["headsetcontrol -o json"]
interval: pollingrate * 1e3
onNewData: {
if(debug_active)
onNewData: (_, data) => {
const res = JSON.parse(data.stdout);
batteryStatus = res.devices[0].battery.status;
batteryPercent = res.devices[0].battery.level;
if(debug_active) {
batteryStatus = "BATTERY_AVAILABLE";
batteryPercent = debug_charge;
else
batteryPercent = data['exit code'] > 0
? -2 // not connected
: +data.stdout;
}
// send notification if necessary
if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) {
@ -56,7 +59,7 @@ PlasmoidItem {
}
}
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryPercent)
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryStatus, batteryPercent)
toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation