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

View File

@ -1,12 +1,13 @@
const batteryLevel = batteryPercent => ({ const batteryLevel = (batteryStatus, batteryPercent) => ({
"-2": "not connected", "BATTERY_UNAVAILABLE": "not connected",
"-1": "charging" "BATTERY_CHARGING": "charging (" + batteryPercent + "%)"
})[batteryPercent] ?? batteryPercent + "%"; })[batteryStatus] ?? batteryPercent + "%";
const batteryIcon = batteryPercent => ({ const batteryIcon = batteryStatus => ({
"-2": "action-unavailable", "BATTERY_UNAVAILABLE": "action-unavailable",
"-1": "reload" "BATTERY_AVAILABLE": "audio-headphones-symbolic",
})[batteryPercent] ?? "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 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); 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 { PlasmoidItem {
id: main id: main
property int pollingrate: Plasmoid.configuration.pollingrate readonly property int pollingrate: Plasmoid.configuration.pollingrate
property int batteryheight: Plasmoid.configuration.batteryheight readonly property int batteryheight: Plasmoid.configuration.batteryheight
property string colorEmpty: Plasmoid.configuration.colorEmpty readonly property string colorEmpty: Plasmoid.configuration.colorEmpty
property string colorHalf: Plasmoid.configuration.colorHalf readonly property string colorHalf: Plasmoid.configuration.colorHalf
property string colorFull: Plasmoid.configuration.colorFull readonly property string colorFull: Plasmoid.configuration.colorFull
property bool notifications: Plasmoid.configuration.notifications readonly property bool notifications: Plasmoid.configuration.notifications
property int batteryThreshold: Plasmoid.configuration.batteryThreshold readonly property int batteryThreshold: Plasmoid.configuration.batteryThreshold
property bool debug_active: Plasmoid.configuration.debug_active readonly property bool debug_active: Plasmoid.configuration.debug_active
property int debug_charge: Plasmoid.configuration.debug_charge readonly property int debug_charge: Plasmoid.configuration.debug_charge
property int batteryPercent: -2 property string batteryStatus: "BATTERY_UNAVAILABLE"
property bool notification_sent: false; property int batteryPercent: 0
property bool notification_sent: false
Plasma5Support.DataSource { Plasma5Support.DataSource {
id: hsSource id: hsSource
engine: "executable" engine: "executable"
connectedSources: ["headsetcontrol -bc"] connectedSources: ["headsetcontrol -o json"]
interval: pollingrate * 1e3 interval: pollingrate * 1e3
onNewData: { onNewData: (_, data) => {
if(debug_active) 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; batteryPercent = debug_charge;
else }
batteryPercent = data['exit code'] > 0
? -2 // not connected
: +data.stdout;
// send notification if necessary // send notification if necessary
if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) { 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" toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation preferredRepresentation: compactRepresentation