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.
This commit is contained in:
0x524a
2025-12-02 08:53:13 -05:00
parent 96ac509c24
commit de752f249e
4 changed files with 36 additions and 12 deletions
+5 -5
View File
@@ -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",
}
+10 -2
View File
@@ -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{
+10 -4
View File
@@ -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,
+11 -1
View File
@@ -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")