package main import ( "context" "errors" "log/slog" "net/http" "os" "os/signal" "git.t-juice.club/torjus/ghettoptt/bus" "git.t-juice.club/torjus/ghettoptt/metrics" "github.com/holoplot/go-evdev" "github.com/prometheus/client_golang/prometheus/promhttp" ) const Version = "v0.1.2" func main() { // Setup logger h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}) logger := slog.New(h) slog.SetDefault(logger) // Setup signal handler ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) defer stop() // Setup metrics m := metrics.NewMumbleMetricsCollector() mbus := bus.NewMumbleBus(m) defer mbus.Close() // Start reading input events input, err := evdev.OpenWithFlags("/dev/input/event1", os.O_RDONLY) if err != nil { panic(err) } defer input.Close() input.NonBlock() var done bool // Start metrics server srvCtx, srvCancel := context.WithCancel(context.Background()) defer srvCancel() go func() { mux := http.NewServeMux() mux.Handle("/metrics", promhttp.Handler()) srv := http.Server{ Handler: mux, Addr: ":8989", } go func() { <-ctx.Done() srv.Shutdown(context.Background()) srvCancel() }() slog.Info("Starting metrics server", "addr", srv.Addr) srv.ListenAndServe() }() // Listen for context cancellation go func() { <-ctx.Done() done = true input.Close() }() // Start listening for PTT key slog.Info("Starting event listener", "version", Version) for !done { ev, err := input.ReadOne() if err != nil { if errors.Is(err, os.ErrClosed) { continue } slog.Error("Error reading from input device", "error", err) os.Exit(1) } if ev.Code == evdev.KEY_F24 && ev.Value == 1 { slog.Info("PTT ON") mbus.StartTalking() } if ev.Code == evdev.KEY_F24 && ev.Value == 0 { slog.Info("PTT OFF") mbus.StopTalking() } } mbus.StopTalking() <-srvCtx.Done() slog.Info("Exiting") }