ghettoptt/bus/bus.go

50 lines
1017 B
Go
Raw Permalink Normal View History

2024-03-18 11:19:16 +00:00
package bus
import (
"time"
"git.t-juice.club/torjus/ghettoptt/metrics"
"github.com/godbus/dbus/v5"
)
type MumbleBus struct {
conn *dbus.Conn
obj dbus.BusObject
metrics *metrics.MumbleMetricsCollector
lastTalked time.Time
}
func NewMumbleBus(m *metrics.MumbleMetricsCollector) *MumbleBus {
mb := &MumbleBus{
metrics: m,
}
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
mb.obj = conn.Object("net.sourceforge.mumble.mumble", "/")
mb.conn = conn
return mb
}
func (m *MumbleBus) Close() error {
return m.conn.Close()
}
func (m *MumbleBus) StartTalking() error {
m.obj.Call("net.sourceforge.mumble.Mumble.startTalking", 0)
m.lastTalked = time.Now()
m.metrics.PTTTogglesTotal.Add(1)
return nil
}
func (m *MumbleBus) StopTalking() error {
m.obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
duration := time.Since(m.lastTalked)
m.lastTalked = time.Time{}
m.metrics.TalkSecondsTotal.Add(duration.Seconds())
m.metrics.PTTTogglesTotal.Add(1)
return nil
}