-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
219 lines (189 loc) · 7.35 KB
/
main.qml
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import QtQuick 2.15
import QtQuick.Layouts 1.15
import org.kde.plasma.components 3.0 as PlasmaComponents
import org.kde.plasma.plasmoid 2.0
import org.kde.kirigami 2.20 as Kirigami
import org.kde.plasma.plasma5support 2.0 as P5Support
PlasmoidItem {
id: root
property string energyText: "-- W"
property color energyTextColor: Kirigami.Theme.textColor
property string batteryPath: ""
property string acAdapterPath: ""
property bool hasPowerNow: false
property bool hasPowerNowChecked: false
preferredRepresentation: fullRepresentation
fullRepresentation: ColumnLayout {
id: fullRep
Layout.minimumWidth: Kirigami.Units.gridUnit * 10
Layout.minimumHeight: Kirigami.Units.gridUnit * 4
spacing: Kirigami.Units.smallSpacing
PlasmaComponents.Label {
id: energyLabel
Layout.alignment: Qt.AlignHCenter
text: root.energyText
font.pointSize: plasmoid.configuration.fontSize
font.bold: plasmoid.configuration.fontBold
font.family: plasmoid.configuration.fontFamily || font.family
color: root.energyTextColor
}
}
P5Support.DataSource {
id: dataSource
engine: "executable"
connectedSources: []
function readFile(path) {
connectSource("cat " + path)
}
onNewData: (sourceName, data) => {
var exitCode = data["exit code"]
var exitStatus = data["exit status"]
var stdout = data["stdout"]
var stderr = data["stderr"]
if (exitCode == 0) {
root.processFileContent(sourceName, stdout.trim())
} else {
console.error("Error reading file:", sourceName, "stderr:", stderr)
root.processFileContent(sourceName, null)
}
disconnectSource(sourceName)
}
}
function processFileContent(source, content) {
if (source.includes("status")) {
var isCharging = content === "Charging"
if (root.hasPowerNow) {
readPowerNow()
} else {
readVoltage()
}
} else if (source.includes("voltage_now")) {
if (content === null) {
root.energyText = "Reading voltage"
root.energyTextColor = Kirigami.Theme.negativeTextColor
return
}
voltageValue = parseFloat(content) / 1000000
readCurrent()
} else if (source.includes("current_now")) {
if (content === null) {
root.energyText = "Reading current"
root.energyTextColor = Kirigami.Theme.negativeTextColor
return
}
currentValue = parseFloat(content) / 1000000
readACOnline()
} else if (source.includes("power_now")) {
if (content === null) {
root.energyText = "Reading power"
root.energyTextColor = Kirigami.Theme.negativeTextColor
return
}
powerValue = parseFloat(content) / 1000000
readACOnline()
} else if (source.includes("online")) {
var acOnline = parseInt(content, 10) === 1
calculateAndDisplayEnergy(acOnline)
}
}
property real voltageValue: 0
property real currentValue: 0
property real powerValue: 0
function readVoltage() {
dataSource.readFile(root.batteryPath + "/voltage_now")
}
function readCurrent() {
dataSource.readFile(root.batteryPath + "/current_now")
}
function readPowerNow() {
dataSource.readFile(root.batteryPath + "/power_now")
}
function readACOnline() {
if (root.acAdapterPath) {
dataSource.readFile(root.acAdapterPath + "/online")
} else {
calculateAndDisplayEnergy(false)
}
}
function calculateAndDisplayEnergy(acOnline) {
var watts = root.hasPowerNow ? powerValue : voltageValue * currentValue
var isFullyCharged = root.hasPowerNow ? (watts < 0.1) : (Math.abs(currentValue) < 0.01)
if (isFullyCharged && acOnline) {
root.energyText = "\u26A1"
root.energyTextColor = Kirigami.Theme.textColor
} else {
var formattedWatts
if (acOnline) {
formattedWatts = watts.toLocaleString(Qt.locale(), 'f', 1) + "W"
root.energyTextColor = Kirigami.Theme.textColor
} else {
formattedWatts = "-" + Math.abs(watts).toLocaleString(Qt.locale(), 'f', 1) + "W"
var threshold = plasmoid.configuration.wattageThreshold
root.energyTextColor = watts >= threshold ? "red" : Kirigami.Theme.textColor
}
root.energyText = formattedWatts + (acOnline ? "\u26A1" : "")
}
}
function findPowerSupplyPaths() {
dataSource.connectSource("ls /sys/class/power_supply")
}
function updateEnergyUsage() {
if (root.batteryPath) {
dataSource.readFile(root.batteryPath + "/status")
} else {
findPowerSupplyPaths()
}
}
Timer {
id: updateTimer
interval: plasmoid.configuration.updateInterval
running: true
repeat: true
onTriggered: updateEnergyUsage()
}
Component.onCompleted: {
findPowerSupplyPaths()
}
Connections {
target: plasmoid.configuration
function onUpdateIntervalChanged() {
updateTimer.interval = plasmoid.configuration.updateInterval
}
}
Connections {
target: dataSource
function onNewData(sourceName, data) {
if (sourceName === "ls /sys/class/power_supply") {
var devices = data.stdout.split('\n')
var batteries = devices.filter(function(item) {
return item.startsWith('BAT')
})
var acAdapters = devices.filter(function(item) {
return item.startsWith('AC') || item.startsWith('ADP') || item.startsWith('USB')
})
if (batteries.length > 0) {
root.batteryPath = "/sys/class/power_supply/" + batteries[0]
console.log("Battery found: " + root.batteryPath)
dataSource.connectSource("ls " + root.batteryPath + "/power_now")
} else {
console.error("No battery found")
root.energyText = "No battery"
root.energyTextColor = Kirigami.Theme.negativeTextColor
}
if (acAdapters.length > 0) {
root.acAdapterPath = "/sys/class/power_supply/" + acAdapters[0]
console.log("AC adapter found: " + root.acAdapterPath)
} else {
console.warn("No AC adapter found")
}
updateEnergyUsage()
dataSource.disconnectSource(sourceName)
} else if (sourceName.includes("/power_now") && !hasPowerNowChecked) {
root.hasPowerNow = (data["exit code"] === 0)
console.log("Device " + (root.hasPowerNow ? "has" : "does not have") + " power_now file")
hasPowerNowChecked = true
dataSource.disconnectSource(sourceName)
}
}
}
}