refactor: enhance code clarity and maintainability across multiple files

- Updated comments to improve clarity and adhere to best practices in ascii.go, main.go, and diagnostics.
- Removed unnecessary linter directives for improved readability in imaging.go and ptz.go.
- Reformatted function signatures and added helper calls in tests for consistency and clarity.
- Enhanced error handling and logging consistency in various server files, ensuring better maintainability.
This commit is contained in:
0x524a
2025-12-02 22:21:20 -05:00
parent c1daba5be6
commit 02f79ea7a7
12 changed files with 70 additions and 56 deletions
+2 -2
View File
@@ -347,8 +347,8 @@ func (s *Server) HandleSetImagingSettings(body interface{}) (interface{}, error)
// HandleGetOptions handles GetOptions request.
func (s *Server) HandleGetOptions(body interface{}) (interface{}, error) {
// Return available imaging options/capabilities
const maxImagingValue = 100 //nolint:mnd // Maximum imaging parameter value
const maxExposureTime = 10000 //nolint:mnd // Maximum exposure time in microseconds
const maxImagingValue = 100 // Maximum imaging parameter value
const maxExposureTime = 10000 // Maximum exposure time in microseconds
options := &ImagingOptions{
Brightness: &FloatRange{Min: 0, Max: maxImagingValue},
ColorSaturation: &FloatRange{Min: 0, Max: maxImagingValue},
+9 -8
View File
@@ -11,7 +11,7 @@ const (
)
func TestHandleGetImagingSettings(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -47,7 +47,7 @@ func TestHandleGetImagingSettings(t *testing.T) {
}
func TestHandleSetImagingSettings(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -90,7 +90,7 @@ func TestHandleSetImagingSettings(t *testing.T) {
}
func TestHandleGetOptions(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -128,9 +128,10 @@ func TestHandleGetOptions(t *testing.T) {
// TestHandleMove - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleMove(t *testing.T) {
config := createTestConfig(t)
t.Helper()
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -470,7 +471,7 @@ func TestGetImagingSettingsResponseXML(t *testing.T) {
}
func TestHandleGetOptionsDetails(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -506,7 +507,7 @@ func TestImagingSettingsEdgeCases(t *testing.T) {
}
func TestSetImagingSettingsEdgeCases(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -525,7 +526,7 @@ func TestSetImagingSettingsEdgeCases(t *testing.T) {
}
func TestGetImagingSettingsEdgeCases(t *testing.T) {
config := createTestConfig(t)
config := createTestConfig()
server, _ := New(config)
// Test with invalid token
+2 -2
View File
@@ -306,8 +306,8 @@ func (s *Server) HandleRelativeMove(body interface{}) (interface{}, error) {
}
// Clamp values to valid ranges (simplified)
const maxPan = 180 //nolint:mnd // PTZ pan range
const maxTilt = 90 //nolint:mnd // PTZ tilt range
const maxPan = 180 // PTZ pan range
const maxTilt = 90 // PTZ tilt range
state.Position.Pan = clamp(state.Position.Pan, -maxPan, maxPan)
state.Position.Tilt = clamp(state.Position.Tilt, -maxTilt, maxTilt)
state.Position.Zoom = clamp(state.Position.Zoom, 0, 1)
+12 -6
View File
@@ -8,8 +8,9 @@ import (
// These handlers are better tested through the SOAP handler in integration tests.
//
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleGetPresets(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
@@ -78,8 +79,9 @@ func TestHandleGotoPreset(t *testing.T) {
// TestHandleGetStatus - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleGetStatus(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
@@ -116,8 +118,9 @@ func _DisabledTestHandleGetStatus(t *testing.T) {
// TestHandleAbsoluteMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleAbsoluteMove(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
@@ -159,8 +162,9 @@ func _DisabledTestHandleAbsoluteMove(t *testing.T) {
// TestHandleRelativeMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleRelativeMove(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
@@ -202,8 +206,9 @@ func _DisabledTestHandleRelativeMove(t *testing.T) {
// TestHandleContinuousMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleContinuousMove(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
@@ -244,8 +249,9 @@ func _DisabledTestHandleContinuousMove(t *testing.T) {
// TestHandleStop - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
//nolint:unused,thelper // Disabled test function kept for reference
func _DisabledTestHandleStop(t *testing.T) {
t.Helper()
config := createTestConfig()
server, _ := New(config)
profileToken := config.Profiles[0].Token
+1 -1
View File
@@ -160,7 +160,7 @@ func (s *Server) Start(ctx context.Context) error {
select {
case <-ctx.Done():
fmt.Println("\n🛑 Shutting down server...")
const shutdownTimeout = 5 //nolint:mnd // Server shutdown timeout in seconds
const shutdownTimeout = 5 // Server shutdown timeout in seconds
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout*time.Second)
defer cancel()
+3 -2
View File
@@ -12,6 +12,7 @@ import (
"testing"
)
const testXMLHeader = `<?xml version="1.0"?>`
func TestNewHandler(t *testing.T) {
handler := NewHandler("admin", "password")
@@ -68,7 +69,7 @@ func TestServeHTTPValidSOAPRequest(t *testing.T) {
})
// Create SOAP request
soapBody := `<?xml version="1.0"?>
soapBody := testXMLHeader + `
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TestAction/>
@@ -323,7 +324,7 @@ func TestAuthenticateFailsWithWrongPassword(t *testing.T) {
func TestHandlerWithoutAuthentication(t *testing.T) {
handler := NewHandler("", "") // No authentication
soapBody := `<?xml version="1.0"?>
soapBody := testXMLHeader + `
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TestAction/>
+21 -20
View File
@@ -250,14 +250,12 @@ type WDRSettings struct {
}
// DefaultConfig returns a default server configuration with a multi-lens camera setup.
//
//nolint:funlen // DefaultConfig has many statements due to comprehensive default configuration
func DefaultConfig() *Config {
func DefaultConfig() *Config { //nolint:funlen // DefaultConfig has many statements due to comprehensive default configuration
return &Config{
Host: "0.0.0.0",
Port: 8080, //nolint:mnd // Default HTTP port
BasePath: "/onvif",
Timeout: defaultTimeoutSec * time.Second, //nolint:mnd // Default timeout
Timeout: defaultTimeoutSec * time.Second,
DeviceInfo: DeviceInfo{
Manufacturer: "onvif-go",
Model: "Virtual Multi-Lens Camera",
@@ -277,25 +275,25 @@ func DefaultConfig() *Config {
VideoSource: VideoSourceConfig{
Token: "video_source_0",
Name: "Main Camera",
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bounds: Bounds{X: 0, Y: 0, Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default bounds
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight},
Framerate: defaultFramerate,
Bounds: Bounds{X: 0, Y: 0, Width: defaultWidth, Height: defaultHeight},
},
VideoEncoder: VideoEncoderConfig{
Encoding: "H264",
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Quality: defaultQuality, //nolint:mnd // Default quality
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bitrate: defaultBitrate, //nolint:mnd // Default bitrate
GovLength: defaultFramerate, //nolint:mnd // Default gov length
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight},
Quality: defaultQuality,
Framerate: defaultFramerate,
Bitrate: defaultBitrate,
GovLength: defaultFramerate,
},
PTZ: &PTZConfig{
NodeToken: "ptz_node_0",
PanRange: Range{Min: -maxPan, Max: maxPan}, //nolint:mnd // PTZ pan range
TiltRange: Range{Min: -maxTilt, Max: maxTilt}, //nolint:mnd // PTZ tilt range
PanRange: Range{Min: -maxPan, Max: maxPan},
TiltRange: Range{Min: -maxTilt, Max: maxTilt},
ZoomRange: Range{Min: 0, Max: 1},
DefaultSpeed: PTZSpeed{
Pan: defaultPTZSpeed, Tilt: defaultPTZSpeed, Zoom: defaultPTZSpeed, //nolint:mnd // Default PTZ speed
Pan: defaultPTZSpeed, Tilt: defaultPTZSpeed, Zoom: defaultPTZSpeed,
},
SupportsContinuous: true,
SupportsAbsolute: true,
@@ -357,10 +355,10 @@ func DefaultConfig() *Config {
GovLength: lowFramerate, //nolint:mnd // Low framerate
},
PTZ: &PTZConfig{
NodeToken: "ptz_node_2",
PanRange: Range{Min: -maxPan, Max: maxPan}, //nolint:mnd // PTZ pan range
TiltRange: Range{Min: -maxTilt, Max: maxTilt}, //nolint:mnd // PTZ tilt range
ZoomRange: Range{Min: 0, Max: maxZoom}, //nolint:mnd // Max zoom
NodeToken: "ptz_node_2",
PanRange: Range{Min: -maxPan, Max: maxPan},
TiltRange: Range{Min: -maxTilt, Max: maxTilt},
ZoomRange: Range{Min: 0, Max: maxZoom}, //nolint:mnd // Max zoom
DefaultSpeed: PTZSpeed{
Pan: lowPTZSpeed, Tilt: lowPTZSpeed, Zoom: lowPTZSpeed, //nolint:mnd // Low PTZ speed
},
@@ -369,7 +367,10 @@ func DefaultConfig() *Config {
SupportsRelative: true,
Presets: []Preset{
{Token: "preset_2_0", Name: "Home", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: 0}},
{Token: "preset_2_1", Name: "Zoom In", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: presetZoom}}, //nolint:mnd // Preset zoom
{
Token: "preset_2_1", Name: "Zoom In",
Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: presetZoom}, //nolint:mnd // Preset zoom
},
},
},
Snapshot: SnapshotConfig{