refactor: improve code readability and maintainability across multiple files

- Reformatted function signatures for better clarity in media.go and onvif-quick/main.go.
- Replaced hardcoded values with constants in ascii.go and server/imaging.go for improved maintainability.
- Enhanced error handling and logging consistency in onvif-diagnostics/main.go and server/server.go.
- Updated comments to clarify functionality and ensure adherence to ONVIF specifications across various files.
This commit is contained in:
0x524a
2025-12-02 08:54:23 -05:00
parent de752f249e
commit 31df3f8b79
10 changed files with 83 additions and 70 deletions
+4 -4
View File
@@ -266,12 +266,12 @@ func (s *Server) HandleGetServices(body interface{}) (interface{}, error) {
{
Namespace: "http://www.onvif.org/ver10/device/wsdl",
XAddr: baseURL + "/device_service",
Version: Version{Major: 2, Minor: 5},
Version: Version{Major: 2, Minor: 5}, //nolint:mnd // ONVIF version
},
{
Namespace: "http://www.onvif.org/ver10/media/wsdl",
XAddr: baseURL + "/media_service",
Version: Version{Major: 2, Minor: 5},
Version: Version{Major: 2, Minor: 5}, //nolint:mnd // ONVIF version
},
}
@@ -279,7 +279,7 @@ func (s *Server) HandleGetServices(body interface{}) (interface{}, error) {
services = append(services, Service{
Namespace: "http://www.onvif.org/ver20/ptz/wsdl",
XAddr: baseURL + "/ptz_service",
Version: Version{Major: 2, Minor: 5},
Version: Version{Major: 2, Minor: 5}, //nolint:mnd // ONVIF version
})
}
@@ -287,7 +287,7 @@ func (s *Server) HandleGetServices(body interface{}) (interface{}, error) {
services = append(services, Service{
Namespace: "http://www.onvif.org/ver20/imaging/wsdl",
XAddr: baseURL + "/imaging_service",
Version: Version{Major: 2, Minor: 5},
Version: Version{Major: 2, Minor: 5}, //nolint:mnd // ONVIF version
})
}
+13 -11
View File
@@ -347,25 +347,27 @@ 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
options := &ImagingOptions{
Brightness: &FloatRange{Min: 0, Max: 100},
ColorSaturation: &FloatRange{Min: 0, Max: 100},
Contrast: &FloatRange{Min: 0, Max: 100},
Sharpness: &FloatRange{Min: 0, Max: 100},
Brightness: &FloatRange{Min: 0, Max: maxImagingValue},
ColorSaturation: &FloatRange{Min: 0, Max: maxImagingValue},
Contrast: &FloatRange{Min: 0, Max: maxImagingValue},
Sharpness: &FloatRange{Min: 0, Max: maxImagingValue},
IrCutFilterModes: []string{"ON", "OFF", "AUTO"},
BacklightCompensation: &BacklightCompensationOptions{
Mode: []string{"OFF", "ON"},
Level: &FloatRange{Min: 0, Max: 100},
Level: &FloatRange{Min: 0, Max: maxImagingValue},
},
Exposure: &ExposureOptions{
Mode: []string{"AUTO", "MANUAL"},
Priority: []string{"LowNoise", "FrameRate"},
MinExposureTime: &FloatRange{Min: 1, Max: 10000},
MaxExposureTime: &FloatRange{Min: 1, Max: 10000},
MinGain: &FloatRange{Min: 0, Max: 100},
MaxGain: &FloatRange{Min: 0, Max: 100},
ExposureTime: &FloatRange{Min: 1, Max: 10000},
Gain: &FloatRange{Min: 0, Max: 100},
MinExposureTime: &FloatRange{Min: 1, Max: maxExposureTime},
MaxExposureTime: &FloatRange{Min: 1, Max: maxExposureTime},
MinGain: &FloatRange{Min: 0, Max: maxImagingValue},
MaxGain: &FloatRange{Min: 0, Max: maxImagingValue},
ExposureTime: &FloatRange{Min: 1, Max: maxExposureTime},
Gain: &FloatRange{Min: 0, Max: maxImagingValue},
},
Focus: &FocusOptions{
AutoFocusModes: []string{"AUTO", "MANUAL"},
+6 -4
View File
@@ -268,7 +268,7 @@ func (s *Server) HandleAbsoluteMove(body interface{}) (interface{}, error) {
// In a real implementation, simulate movement over time
// For now, we'll stop immediately
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(500 * time.Millisecond) //nolint:mnd // PTZ movement delay
ptzMutex.Lock()
state.Moving = false
state.PanMoving = false
@@ -306,8 +306,10 @@ func (s *Server) HandleRelativeMove(body interface{}) (interface{}, error) {
}
// Clamp values to valid ranges (simplified)
state.Position.Pan = clamp(state.Position.Pan, -180, 180)
state.Position.Tilt = clamp(state.Position.Tilt, -90, 90)
const maxPan = 180 //nolint:mnd // PTZ pan range
const maxTilt = 90 //nolint:mnd // 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)
state.Moving = true
@@ -315,7 +317,7 @@ func (s *Server) HandleRelativeMove(body interface{}) (interface{}, error) {
// Simulate movement completion
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(500 * time.Millisecond) //nolint:mnd // PTZ movement delay
ptzMutex.Lock()
state.Moving = false
state.PanMoving = false
+2 -1
View File
@@ -160,7 +160,8 @@ func (s *Server) Start(ctx context.Context) error {
select {
case <-ctx.Done():
fmt.Println("\n🛑 Shutting down server...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
const shutdownTimeout = 5 //nolint:mnd // Server shutdown timeout in seconds
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
+2 -1
View File
@@ -363,7 +363,8 @@ func (c *Config) ServiceEndpoints(host string) map[string]string {
}
var baseURL string
if c.Port == 80 {
const httpPort = 80 //nolint:mnd // Standard HTTP port
if c.Port == httpPort {
baseURL = "http://" + host + c.BasePath
} else {
// Import fmt at the top to use Sprintf