Fix golangci-lint findings and improve code quality

Move example files to separate packages to avoid main redeclaration. Fix cyclomatic complexity and variable shadowing. Address errcheck and wsl linting issues. Update tests to handle capabilities and fix panics. Apply consistent formatting with gofmt.
This commit is contained in:
Tobias Gesellchen
2026-01-11 00:39:58 +01:00
parent 1f47c763dc
commit 369ebc42fe
11 changed files with 388 additions and 161 deletions
@@ -1,3 +1,4 @@
// Package main provides an example of using advanced audio controls.
package main
import (
@@ -19,7 +20,37 @@ func main() {
fmt.Println("=================================================")
// Example 1: Check device capabilities first
checkCapabilities(soundtouchClient)
// Example 2: DSP Audio Controls
demonstrateDSPControls(soundtouchClient)
time.Sleep(2 * time.Second)
// Example 3: Advanced Tone Controls (Bass/Treble)
demonstrateToneControls(soundtouchClient)
time.Sleep(2 * time.Second)
// Example 4: Speaker Level Controls
demonstrateLevelControls(soundtouchClient)
// Example 5: Compare with basic controls
demonstrateBasicControls(soundtouchClient)
// Example 6: Error handling and validation
demonstrateErrorHandling(soundtouchClient)
// Example 7: CLI command equivalents
showCLIEquivalents(deviceIP)
fmt.Println("\n🎉 Advanced audio controls example completed!")
printNotes()
}
func checkCapabilities(soundtouchClient *client.Client) {
fmt.Println("\n1. Checking device capabilities...")
capabilities, err := soundtouchClient.GetCapabilities()
if err != nil {
log.Printf("❌ Failed to get capabilities: %v", err)
@@ -27,7 +58,6 @@ func main() {
}
fmt.Printf("📋 Device: %s\n", capabilities.DeviceID)
fmt.Printf(" Type: %s\n", capabilities.Type)
// Look for advanced audio capabilities in the response
// (Note: Advanced audio controls are only available on professional/high-end devices)
@@ -35,160 +65,174 @@ func main() {
fmt.Println(" - DSP Controls: Check device response for 'audiodspcontrols'")
fmt.Println(" - Tone Controls: Check device response for 'audioproducttonecontrols'")
fmt.Println(" - Level Controls: Check device response for 'audioproductlevelcontrols'")
}
// Example 2: DSP Audio Controls
func demonstrateDSPControls(soundtouchClient *client.Client) {
fmt.Println("\n2. DSP Audio Controls...")
dspControls, err := soundtouchClient.GetAudioDSPControls()
if err != nil {
log.Printf("⚠️ DSP controls not available on this device: %v", err)
fmt.Println(" This is normal for consumer-grade SoundTouch devices")
} else {
fmt.Printf("🎛️ Current DSP Settings: %s\n", dspControls.String())
// Try setting a different audio mode
supportedModes := dspControls.GetSupportedAudioModes()
if len(supportedModes) > 0 {
newMode := supportedModes[0]
if newMode != dspControls.AudioMode && newMode != "" {
fmt.Printf(" Changing audio mode to: %s\n", newMode)
return
}
err = soundtouchClient.SetAudioMode(newMode)
if err != nil {
log.Printf("❌ Failed to set audio mode: %v", err)
} else {
fmt.Printf("✅ Audio mode changed successfully\n")
}
}
}
fmt.Printf("🎛️ Current DSP Settings: %s\n", dspControls.String())
// Demonstrate video sync delay adjustment
if dspControls.VideoSyncAudioDelay != 50 {
fmt.Println(" Setting video sync audio delay to 50ms...")
err = soundtouchClient.SetVideoSyncAudioDelay(50)
// Try setting a different audio mode
supportedModes := dspControls.GetSupportedAudioModes()
if len(supportedModes) > 0 {
newMode := supportedModes[0]
if newMode != dspControls.AudioMode && newMode != "" {
fmt.Printf(" Changing audio mode to: %s\n", newMode)
err = soundtouchClient.SetAudioMode(newMode)
if err != nil {
log.Printf("❌ Failed to set video sync delay: %v", err)
log.Printf("❌ Failed to set audio mode: %v", err)
} else {
fmt.Printf("✅ Video sync delay adjusted\n")
fmt.Printf("✅ Audio mode changed successfully\n")
}
}
// Combined DSP settings update
fmt.Println(" Updating DSP controls (mode + delay)...")
err = soundtouchClient.SetAudioDSPControls("NORMAL", 25)
if err != nil {
log.Printf("❌ Failed to set DSP controls: %v", err)
} else {
fmt.Printf("✅ DSP controls updated\n")
}
}
time.Sleep(2 * time.Second)
// Demonstrate video sync delay adjustment
if dspControls.VideoSyncAudioDelay != 50 {
fmt.Println(" Setting video sync audio delay to 50ms...")
// Example 3: Advanced Tone Controls (Bass/Treble)
err = soundtouchClient.SetVideoSyncAudioDelay(50)
if err != nil {
log.Printf("❌ Failed to set video sync delay: %v", err)
} else {
fmt.Printf("✅ Video sync delay adjusted\n")
}
}
// Combined DSP settings update
fmt.Println(" Updating DSP controls (mode + delay)...")
err = soundtouchClient.SetAudioDSPControls("NORMAL", 25)
if err != nil {
log.Printf("❌ Failed to set DSP controls: %v", err)
} else {
fmt.Printf("✅ DSP controls updated\n")
}
}
func demonstrateToneControls(soundtouchClient *client.Client) {
fmt.Println("\n3. Advanced Tone Controls...")
toneControls, err := soundtouchClient.GetAudioProductToneControls()
if err != nil {
log.Printf("⚠️ Advanced tone controls not available on this device: %v", err)
fmt.Println(" Use the basic bass control instead (soundtouch-cli bass)")
} else {
fmt.Printf("🎚️ Current Tone Settings: %s\n", toneControls.String())
// Adjust bass only
newBassLevel := 3
if toneControls.Bass.Value != newBassLevel {
fmt.Printf(" Setting advanced bass to %d...\n", newBassLevel)
err = soundtouchClient.SetAdvancedBass(newBassLevel)
if err != nil {
log.Printf("❌ Failed to set advanced bass: %v", err)
} else {
fmt.Printf("✅ Advanced bass adjusted\n")
}
}
return
}
time.Sleep(1 * time.Second)
fmt.Printf("🎚️ Current Tone Settings: %s\n", toneControls.String())
// Adjust treble only
newTrebleLevel := -1
if toneControls.Treble.Value != newTrebleLevel {
fmt.Printf(" Setting advanced treble to %d...\n", newTrebleLevel)
err = soundtouchClient.SetAdvancedTreble(newTrebleLevel)
if err != nil {
log.Printf("❌ Failed to set advanced treble: %v", err)
} else {
fmt.Printf("✅ Advanced treble adjusted\n")
}
}
// Adjust bass only
newBassLevel := 3
if toneControls.Bass.Value != newBassLevel {
fmt.Printf(" Setting advanced bass to %d...\n", newBassLevel)
time.Sleep(1 * time.Second)
// Adjust both bass and treble together
combinedBass := 2
combinedTreble := 1
fmt.Printf(" Setting bass to %d and treble to %d together...\n", combinedBass, combinedTreble)
err = soundtouchClient.SetAudioProductToneControls(&combinedBass, &combinedTreble)
err = soundtouchClient.SetAdvancedBass(newBassLevel)
if err != nil {
log.Printf("❌ Failed to set tone controls: %v", err)
log.Printf("❌ Failed to set advanced bass: %v", err)
} else {
fmt.Printf("✅ Both tone controls adjusted\n")
fmt.Printf("✅ Advanced bass adjusted\n")
}
}
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
// Example 4: Speaker Level Controls
// Adjust treble only
newTrebleLevel := -1
if toneControls.Treble.Value != newTrebleLevel {
fmt.Printf(" Setting advanced treble to %d...\n", newTrebleLevel)
err = soundtouchClient.SetAdvancedTreble(newTrebleLevel)
if err != nil {
log.Printf("❌ Failed to set advanced treble: %v", err)
} else {
fmt.Printf("✅ Advanced treble adjusted\n")
}
}
time.Sleep(1 * time.Second)
// Adjust both bass and treble together
combinedBass := 2
combinedTreble := 1
fmt.Printf(" Setting bass to %d and treble to %d together...\n", combinedBass, combinedTreble)
err = soundtouchClient.SetAudioProductToneControls(&combinedBass, &combinedTreble)
if err != nil {
log.Printf("❌ Failed to set tone controls: %v", err)
} else {
fmt.Printf("✅ Both tone controls adjusted\n")
}
}
func demonstrateLevelControls(soundtouchClient *client.Client) {
fmt.Println("\n4. Speaker Level Controls...")
levelControls, err := soundtouchClient.GetAudioProductLevelControls()
if err != nil {
log.Printf("⚠️ Speaker level controls not available on this device: %v", err)
fmt.Println(" This feature is only available on surround sound systems")
} else {
fmt.Printf("🔊 Current Speaker Levels: %s\n", levelControls.String())
// Adjust front-center speaker level
newFrontCenterLevel := 2
if levelControls.FrontCenterSpeakerLevel.Value != newFrontCenterLevel {
fmt.Printf(" Setting front-center speaker level to %d...\n", newFrontCenterLevel)
err = soundtouchClient.SetFrontCenterSpeakerLevel(newFrontCenterLevel)
if err != nil {
log.Printf("❌ Failed to set front-center level: %v", err)
} else {
fmt.Printf("✅ Front-center speaker level adjusted\n")
}
}
return
}
time.Sleep(1 * time.Second)
fmt.Printf("🔊 Current Speaker Levels: %s\n", levelControls.String())
// Adjust rear-surround speakers level
newRearSurroundLevel := -1
if levelControls.RearSurroundSpeakersLevel.Value != newRearSurroundLevel {
fmt.Printf(" Setting rear-surround speakers level to %d...\n", newRearSurroundLevel)
err = soundtouchClient.SetRearSurroundSpeakersLevel(newRearSurroundLevel)
if err != nil {
log.Printf("❌ Failed to set rear-surround level: %v", err)
} else {
fmt.Printf("✅ Rear-surround speakers level adjusted\n")
}
}
// Adjust front-center speaker level
newFrontCenterLevel := 2
if levelControls.FrontCenterSpeakerLevel.Value != newFrontCenterLevel {
fmt.Printf(" Setting front-center speaker level to %d...\n", newFrontCenterLevel)
time.Sleep(1 * time.Second)
// Adjust both speaker levels together
combinedFrontCenter := 1
combinedRearSurround := 0
fmt.Printf(" Setting front-center to %d and rear-surround to %d together...\n",
combinedFrontCenter, combinedRearSurround)
err = soundtouchClient.SetAudioProductLevelControls(&combinedFrontCenter, &combinedRearSurround)
err = soundtouchClient.SetFrontCenterSpeakerLevel(newFrontCenterLevel)
if err != nil {
log.Printf("❌ Failed to set speaker levels: %v", err)
log.Printf("❌ Failed to set front-center level: %v", err)
} else {
fmt.Printf("✅ Both speaker levels adjusted\n")
fmt.Printf("✅ Front-center speaker level adjusted\n")
}
}
// Example 5: Compare with basic controls
time.Sleep(1 * time.Second)
// Adjust rear-surround speakers level
newRearSurroundLevel := -1
if levelControls.RearSurroundSpeakersLevel.Value != newRearSurroundLevel {
fmt.Printf(" Setting rear-surround speakers level to %d...\n", newRearSurroundLevel)
err = soundtouchClient.SetRearSurroundSpeakersLevel(newRearSurroundLevel)
if err != nil {
log.Printf("❌ Failed to set rear-surround level: %v", err)
} else {
fmt.Printf("✅ Rear-surround speakers level adjusted\n")
}
}
time.Sleep(1 * time.Second)
// Adjust both speaker levels together
combinedFrontCenter := 1
combinedRearSurround := 0
fmt.Printf(" Setting front-center to %d and rear-surround to %d together...\n",
combinedFrontCenter, combinedRearSurround)
err = soundtouchClient.SetAudioProductLevelControls(&combinedFrontCenter, &combinedRearSurround)
if err != nil {
log.Printf("❌ Failed to set speaker levels: %v", err)
} else {
fmt.Printf("✅ Both speaker levels adjusted\n")
}
}
func demonstrateBasicControls(soundtouchClient *client.Client) {
fmt.Println("\n5. Comparison with Basic Audio Controls...")
fmt.Println(" Basic controls available on all devices:")
@@ -215,24 +259,28 @@ func main() {
} else {
fmt.Printf(" Balance: %d (range: -50 to +50)\n", balance.TargetBalance)
}
}
// Example 6: Error handling and validation
func demonstrateErrorHandling(soundtouchClient *client.Client) {
fmt.Println("\n6. Error Handling Examples...")
// Try to set invalid DSP controls to demonstrate validation
fmt.Println(" Testing invalid audio mode...")
err = soundtouchClient.SetAudioMode("INVALID_MODE")
err := soundtouchClient.SetAudioMode("INVALID_MODE")
if err != nil {
fmt.Printf("⚠️ Expected error for invalid mode: %v\n", err)
}
fmt.Println(" Testing negative video sync delay...")
err = soundtouchClient.SetVideoSyncAudioDelay(-10)
if err != nil {
fmt.Printf("⚠️ Expected error for negative delay: %v\n", err)
}
}
// Example 7: CLI command equivalents
func showCLIEquivalents(deviceIP string) {
fmt.Println("\n7. CLI Command Equivalents...")
fmt.Println(" You can also use the CLI for these operations:")
fmt.Println(" ")
@@ -250,8 +298,9 @@ func main() {
fmt.Printf(" soundtouch-cli audio level get --host %s\n", deviceIP)
fmt.Printf(" soundtouch-cli audio level set --host %s --front-center 2 --rear-surround -1\n", deviceIP)
fmt.Printf(" soundtouch-cli audio level front-center --host %s --level 3\n", deviceIP)
}
fmt.Println("\n🎉 Advanced audio controls example completed!")
func printNotes() {
fmt.Println("\nNotes:")
fmt.Println("• Advanced audio controls are only available on professional/high-end devices")
fmt.Println("• Consumer SoundTouch devices typically only support basic controls")
@@ -1,3 +1,4 @@
// Package main provides an example of using zone slave operations.
package main
import (