package main import ( "encoding/json" "fmt" "os" "git.zaphyra.eu/airpodsctl/types" "github.com/godbus/dbus/v5" ) func (state State) monitorCommand() { var lines int device, err := state.Devices.Get(state.SelectedDevice) if err == nil { switch state.OutputFormat { case types.FormatWaybar: fmt.Fprint(os.Stdout, device.FormatWaybarString()) case types.FormatCLI: var output string output, lines = device.FormatCLIString() fmt.Fprintf(os.Stdout, "\033[?25l%s\033[%dA", output, lines) } } state.RegisterSignals() for signal := range state.Signals { if signal == nil { return } switch signal.Name { case "org.kairpods.manager.DeviceConnected": macAddr := signal.Body[0].(string) device, _ := state.Devices.Get(macAddr) batteryStateBuds := types.StringSlice{} if device.Battery.LeftBud.Valid || device.Battery.RightBud.Valid || device.BatteryTTLEstimate.Valid { batteryStateBuds.Add("\n 󰁹") if device.Battery.LeftBud.Valid { var chargeSymbol string if device.Battery.LeftBud.Value.Charging { chargeSymbol = " " } batteryStateBuds.Add(fmt.Sprintf("L: %d%%%#s", device.Battery.LeftBud.Value.Level, chargeSymbol)) } if device.Battery.RightBud.Valid { var chargeSymbol string if device.Battery.RightBud.Value.Charging { chargeSymbol = " " } batteryStateBuds.Add(fmt.Sprintf("R: %d%%%#s", device.Battery.RightBud.Value.Level, chargeSymbol)) } if device.BatteryTTLEstimate.Valid { batteryStateBuds.Add(fmt.Sprintf("(󱧦 ~%s remaining)", device.BatteryTTLEstimate)) } } state.SendNotify(fmt.Sprintf("Device '%s' connected!%s", device.Name, batteryStateBuds.ConcatString(" ")), 4, "") case "org.kairpods.manager.DeviceDisconnected": macAddr := signal.Body[0].(string) device, _ := state.Devices.Get(macAddr) state.SendNotify(fmt.Sprintf("Device '%s' disconnected!", device.Name), 2, "") // currently unusable, because there is no event triggered for anc-mode 'adaptive' // case "org.kairpods.manager.NoiseControlChanged": // macAddr := signal.Body[0].(string) // ancMode := signal.Body[1].(string) // fmt.Println(ancMode) // state.SendNotify(fmt.Sprintf("Device '%s' switched to ANC-Mode: %s", state.Devices.Get(macAddr).Name, parseANCMode(ancMode).FormatString()), 2) case "org.freedesktop.DBus.Properties.PropertiesChanged": changedProperties := signal.Body[1].(map[string]dbus.Variant) if value, hasKey := changedProperties["Devices"]; hasKey { var devicesStr string var devices types.Devices value.Store(&devicesStr) if err := json.Unmarshal([]byte(devicesStr), &devices); err != nil { state.PrintDebug(fmt.Sprintf("[monitorCommand] Faild to unmarshal devices-JSON: %s\n", err)) return } oldDeviceState, oldDeviceErr := state.Devices.Get(state.SelectedDevice) newDeviceState, newDeviceErr := devices.Get(state.SelectedDevice) state.Devices = devices if newDeviceErr == nil { switch state.OutputFormat { case types.FormatWaybar: fmt.Fprint(os.Stdout, newDeviceState.FormatWaybarString()) case types.FormatCLI: var output string output, lines = newDeviceState.FormatCLIString() fmt.Fprintf(os.Stdout, "\033[?25l\033[0J%s\033[%dA", output, lines) } if oldDeviceErr == nil { if oldDeviceState.ANCMode != newDeviceState.ANCMode { state.SendNotify(fmt.Sprintf("Switched to ANC-Mode: %s", newDeviceState.ANCMode.Value), 2, newDeviceState.Name) } } } } default: state.PrintDebug(fmt.Sprintf("[monitorCommand] Received unknown signal '%s': %+v\n", signal.Name, signal.Body)) } } os.Exit(0) }