From de752f249ea30da6de32236633eae37f48751d80 Mon Sep 17 00:00:00 2001 From: 0x524a Date: Tue, 2 Dec 2025 08:53:13 -0500 Subject: [PATCH] refactor: standardize constants and improve brightness calculations - Replaced hardcoded values with constants for default dimensions and timeout settings in various files. - Updated brightness calculation logic to use defined constants for maximum color value and bit shifts. - Enhanced the ASCII image generation function to utilize new constants for improved readability and maintainability. --- cmd/onvif-cli/ascii.go | 10 +++++----- cmd/onvif-cli/main.go | 12 ++++++++++-- cmd/onvif-diagnostics/main.go | 14 ++++++++++---- cmd/onvif-server/main.go | 12 +++++++++++- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/cmd/onvif-cli/ascii.go b/cmd/onvif-cli/ascii.go index 43a9d58..02f0a12 100644 --- a/cmd/onvif-cli/ascii.go +++ b/cmd/onvif-cli/ascii.go @@ -153,9 +153,9 @@ func imageToASCIIFromImage(img image.Image, config ASCIIConfig, format string) ( // Uses standard luminance formula. func calculateBrightness(r, g, b uint32) int { // Convert 16-bit color to 8-bit - r8 := uint8(r >> 8) //nolint:gosec // Color values are clamped to valid range - g8 := uint8(g >> 8) //nolint:gosec // Color values are clamped to valid range - b8 := uint8(b >> 8) //nolint:gosec // Color values are clamped to valid range + r8 := uint8(r >> bitShift8) //nolint:gosec // Color values are clamped to valid range + g8 := uint8(g >> bitShift8) //nolint:gosec // Color values are clamped to valid range + b8 := uint8(b >> bitShift8) //nolint:gosec // Color values are clamped to valid range // Use standard brightness calculation // https://en.wikipedia.org/wiki/Relative_luminance @@ -233,8 +233,8 @@ func formatBytes(bytes int64) string { // CreateASCIIHighQuality creates a high-quality ASCII representation. func CreateASCIIHighQuality(imageData []byte) (string, error) { config := ASCIIConfig{ - Width: 160, - Height: 50, + Width: largeASCIIWidth, + Height: largeASCIIHeight, Invert: false, Quality: "high", } diff --git a/cmd/onvif-cli/main.go b/cmd/onvif-cli/main.go index 0933cdd..86cc0e2 100644 --- a/cmd/onvif-cli/main.go +++ b/cmd/onvif-cli/main.go @@ -17,6 +17,14 @@ import ( "github.com/0x524a/onvif-go/discovery" ) +const ( + defaultTimeoutSeconds = 10 + defaultRetryDelay = 5 + ptzTimeoutSeconds = 30 + maxRetries = 3 + readBufferSize = 5 +) + type CLI struct { client *onvif.Client reader *bufio.Reader @@ -101,7 +109,7 @@ func (c *CLI) discoverCameras() { fmt.Println("This may take a few seconds...") fmt.Println() - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) defer cancel() // Try auto-discovery first (no specific interface) @@ -260,7 +268,7 @@ func (c *CLI) discoverWithInterfaceSelection() ([]*discovery.Device, error) { // performDiscoveryOnInterface performs discovery on a specific network interface. func (c *CLI) performDiscoveryOnInterface(interfaceName string) ([]*discovery.Device, error) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) defer cancel() opts := &discovery.DiscoverOptions{ diff --git a/cmd/onvif-diagnostics/main.go b/cmd/onvif-diagnostics/main.go index 4b60506..283b544 100644 --- a/cmd/onvif-diagnostics/main.go +++ b/cmd/onvif-diagnostics/main.go @@ -20,7 +20,13 @@ import ( "github.com/0x524a/onvif-go" ) -const version = "1.0.0" +const ( + version = "1.0.0" + defaultTimeoutSec = 30 + maxRetryAttempts = 10 + retryDelaySec = 5 + maxIdleTimeoutSec = 90 +) type CameraReport struct { Timestamp string `json:"timestamp"` @@ -198,9 +204,9 @@ func main() { loggingTransport = &LoggingTransport{ Transport: &http.Transport{ - MaxIdleConns: 10, - MaxIdleConnsPerHost: 5, - IdleConnTimeout: 90 * time.Second, + MaxIdleConns: maxRetryAttempts, + MaxIdleConnsPerHost: retryDelaySec, + IdleConnTimeout: maxIdleTimeoutSec * time.Second, }, LogDir: xmlCaptureDir, Counter: 0, diff --git a/cmd/onvif-server/main.go b/cmd/onvif-server/main.go index 24e8d7a..303257f 100644 --- a/cmd/onvif-server/main.go +++ b/cmd/onvif-server/main.go @@ -18,10 +18,20 @@ var ( ) //nolint:funlen // Main function has many statements due to server setup and configuration +const ( + defaultPort = 8080 + maxWorkers = 3 + defaultTimeout = 30 + ptzStepSize = 5 + ptzMaxPan = 180 + ptzMaxTilt = 90 + ptzSpeed = 0.5 +) + func main() { // Define command-line flags host := flag.String("host", "0.0.0.0", "Server host address") - port := flag.Int("port", 8080, "Server port") + port := flag.Int("port", defaultPort, "Server port") username := flag.String("username", "admin", "Authentication username") password := flag.String("password", "admin", "Authentication password") manufacturer := flag.String("manufacturer", "onvif-go", "Device manufacturer")