tpingd.go: Add laptop battery query via dbus
This commit is contained in:
98
tpingd.go
98
tpingd.go
@@ -5,22 +5,97 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var result = false
|
var pingres = false
|
||||||
|
var sleeptime time.Duration = 5
|
||||||
|
var charge = "[??% ? 00:00]"
|
||||||
|
|
||||||
func mycheck() {
|
func secondsToClock(inSeconds int64) string {
|
||||||
|
hours := (inSeconds / 60) / 60
|
||||||
|
minutes := inSeconds % 60
|
||||||
|
|
||||||
|
str := fmt.Sprintf("%02d:%02d", hours, minutes)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func mycharge () {
|
||||||
|
for true {
|
||||||
|
conn, err := dbus.SystemBus()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
obj := conn.Object("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/battery_BAT0")
|
||||||
|
percentage, err := obj.GetProperty("org.freedesktop.UPower.Percentage")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var pcharge int = int(percentage.Value().(float64))
|
||||||
|
|
||||||
|
state, err := obj.GetProperty("org.freedesktop.UPower.State")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var status string = "?"
|
||||||
|
var chtime string = "00:00"
|
||||||
|
|
||||||
|
if state.Value().(uint32) == 1 {
|
||||||
|
mytime, err := obj.GetProperty("org.freedesktop.UPower.TimeToFull")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
chtime = "00:00"
|
||||||
|
} else {
|
||||||
|
var seconds = mytime.Value().(int64)
|
||||||
|
chtime = secondsToClock(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
status = "▲"
|
||||||
|
} else if state.Value().(uint32) == 2 {
|
||||||
|
mytime, err := obj.GetProperty("org.freedesktop.UPower.TimeToEmpty")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
chtime = "00:00"
|
||||||
|
} else {
|
||||||
|
var seconds = mytime.Value().(int64)
|
||||||
|
chtime = secondsToClock(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
status = "▼"
|
||||||
|
} else if state.Value().(uint32) == 4 {
|
||||||
|
status = "•"
|
||||||
|
}
|
||||||
|
|
||||||
|
charge = fmt.Sprintf("[%02d%% %s %s]\n", pcharge, status, chtime)
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func myping() {
|
||||||
for true {
|
for true {
|
||||||
conn, e := net.DialTimeout("tcp", "jenkins:443", 3000 * time.Millisecond)
|
conn, e := net.DialTimeout("tcp", "jenkins:443", 3000 * time.Millisecond)
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
result = false
|
pingres = false
|
||||||
} else {
|
} else {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
result = true
|
pingres = true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(sleeptime * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,10 +108,12 @@ func handleConnection(conn net.Conn) {
|
|||||||
|
|
||||||
// ah, fuck, yea. if you check buffer with \n\r\n\r against "\n\r\n\r" you'll get always FALSE, but if check against hex values, it works
|
// ah, fuck, yea. if you check buffer with \n\r\n\r against "\n\r\n\r" you'll get always FALSE, but if check against hex values, it works
|
||||||
if bufLen > 4 && fmt.Sprintf("%x", tail) == "0d0a0d0a" {
|
if bufLen > 4 && fmt.Sprintf("%x", tail) == "0d0a0d0a" {
|
||||||
if result {
|
if pingres {
|
||||||
conn.Write([]byte("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nReachable\n"))
|
var answer = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nReachable %s\n", charge)
|
||||||
|
conn.Write([]byte(answer))
|
||||||
} else {
|
} else {
|
||||||
conn.Write([]byte("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nUnreachable\n"))
|
var answer = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nUnreachable %s\n", charge)
|
||||||
|
conn.Write([]byte(answer))
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@@ -55,10 +132,11 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// spawn check goroutine
|
go myping()
|
||||||
go mycheck()
|
go mycharge()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
// infinitely handle incomming connections
|
||||||
conn, e := ln.Accept()
|
conn, e := ln.Accept()
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user