zaphyra's git: airpodsctl

Control and monitor your AirPods

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 
package main

import (
	"fmt"
	"os"

	"git.zaphyra.eu/airpodsctl/types"
)

func (state State) toggleAncModeCommand(ancMode types.ANCMode) {
	device, err := state.Devices.Get(state.SelectedDevice)
	if err != nil {
		fmt.Fprintf(os.Stderr, "[setAncModeCommand] Error: Device '%s' is not connected!\n", state.SelectedDevice)
		os.Exit(1)
	}

	if device.ANCMode.Valid {
		if ancMode == types.ANCModeUnknown {
			switch device.ANCMode.Value {
			case types.ANCModeOff:
				ancMode = types.ANCModeTransparency
			case types.ANCModeTransparency:
				ancMode = types.ANCModeAdaptive
			case types.ANCModeAdaptive:
				ancMode = types.ANCModeOn
			case types.ANCModeOn:
				ancMode = types.ANCModeOff
			}
		}

		if err := state.SetANCMode(device, ancMode); err != nil {
			fmt.Fprintln(os.Stderr, "[setAncModeCommand] Error:", err)
			os.Exit(1)
		}

		if err := state.UpdateDevices(); err != nil {
			fmt.Fprintln(os.Stderr, "[setAncModeCommand] Error:", err)
			os.Exit(1)
		}

		device, _ = state.Devices.Get(state.SelectedDevice)
		response := fmt.Sprintf("Switched to ANC-Mode: %s", device.ANCMode.Value)

		state.SendNotify(response, 2, device.Name)
		fmt.Fprintf(os.Stdout, "[%s]: %s\n", device.Name, response)
	} else {
		fmt.Fprintf(os.Stderr, "Device '%s' doesn't support ANC!\n", device.Name)
	}

	os.Exit(0)
}