Files
ProtoTess b4e4982876 Refactor XML response handling in device extended and security tests
- Adjusted formatting in XML response strings for consistency in device_extended_test.go and device_security_test.go.
- Improved readability by aligning XML declaration and body content.
- Updated mock server responses to ensure proper handling of various ONVIF operations.

Enhance device security and storage handling

- Refactored struct field declarations in device_security.go and device_storage_test.go for improved clarity.
- Ensured consistent formatting across struct definitions and XML tags.

Standardize whitespace and formatting across multiple files

- Removed unnecessary blank lines and adjusted indentation in discovery, imaging, media, and PTZ server files.
- Improved overall code readability and maintainability by ensuring consistent formatting.

Update example applications for better readability

- Cleaned up whitespace in example applications to enhance code clarity.
- Ensured consistent formatting in main.go files across various examples.

Refactor server and SOAP handler code for consistency

- Standardized struct field declarations and XML tag formatting in server and SOAP handler files.
- Improved readability by aligning struct fields and ensuring consistent use of whitespace.

General code cleanup and formatting adjustments

- Applied consistent formatting across various files, including types.go and test files.
- Enhanced readability by aligning struct fields and removing unnecessary blank lines.
2025-12-01 00:49:36 +00:00

164 lines
5.0 KiB
Go

package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524a/onvif-go"
)
func main() {
fmt.Println("🧪 Testing ONVIF Server with Client Library")
fmt.Println("===========================================")
fmt.Println()
// Create client
client, err := onvif.NewClient(
"http://localhost:8080/onvif/device_service",
onvif.WithCredentials("admin", "admin"),
onvif.WithTimeout(30*time.Second),
)
if err != nil {
log.Fatalf("❌ Failed to create client: %v", err)
}
ctx := context.Background()
// Test 1: Get device information
fmt.Println("📋 Test 1: Getting Device Information...")
info, err := client.GetDeviceInformation(ctx)
if err != nil {
log.Fatalf("❌ Failed to get device info: %v", err)
}
fmt.Printf("✅ Device: %s %s\n", info.Manufacturer, info.Model)
fmt.Printf(" Firmware: %s\n", info.FirmwareVersion)
fmt.Printf(" Serial: %s\n", info.SerialNumber)
fmt.Println()
// Test 2: Initialize and discover services
fmt.Println("🔍 Test 2: Discovering Services...")
if err := client.Initialize(ctx); err != nil {
log.Fatalf("❌ Failed to initialize: %v", err)
}
fmt.Println("✅ Services discovered successfully")
fmt.Println()
// Test 3: Get capabilities
fmt.Println("🔧 Test 3: Getting Capabilities...")
caps, err := client.GetCapabilities(ctx)
if err != nil {
log.Fatalf("❌ Failed to get capabilities: %v", err)
}
fmt.Println("✅ Capabilities:")
if caps.Media != nil {
fmt.Println(" ✓ Media Service")
}
if caps.PTZ != nil {
fmt.Println(" ✓ PTZ Service")
}
if caps.Imaging != nil {
fmt.Println(" ✓ Imaging Service")
}
fmt.Println()
// Test 4: Get media profiles
fmt.Println("🎬 Test 4: Getting Media Profiles...")
profiles, err := client.GetProfiles(ctx)
if err != nil {
log.Fatalf("❌ Failed to get profiles: %v", err)
}
fmt.Printf("✅ Found %d camera profiles:\n", len(profiles))
for i, profile := range profiles {
fmt.Printf("\n Profile %d: %s\n", i+1, profile.Name)
fmt.Printf(" Token: %s\n", profile.Token)
if profile.VideoEncoderConfiguration != nil {
fmt.Printf(" Video: %dx%d @ %s\n",
profile.VideoEncoderConfiguration.Resolution.Width,
profile.VideoEncoderConfiguration.Resolution.Height,
profile.VideoEncoderConfiguration.Encoding)
}
// Get stream URI
streamURI, err := client.GetStreamURI(ctx, profile.Token)
if err != nil {
fmt.Printf(" ⚠️ Failed to get stream URI: %v\n", err)
} else {
fmt.Printf(" RTSP: %s\n", streamURI.URI)
}
// Get snapshot URI if available
snapshotURI, err := client.GetSnapshotURI(ctx, profile.Token)
if err == nil {
fmt.Printf(" Snapshot: %s\n", snapshotURI.URI)
}
// Test PTZ if available
if profile.PTZConfiguration != nil {
fmt.Println(" PTZ: ✓ Enabled")
// Get PTZ status
status, err := client.GetStatus(ctx, profile.Token)
if err == nil {
fmt.Printf(" Position: Pan=%.1f°, Tilt=%.1f°, Zoom=%.2f\n",
status.Position.PanTilt.X,
status.Position.PanTilt.Y,
status.Position.Zoom.X)
}
// Get presets
presets, err := client.GetPresets(ctx, profile.Token)
if err == nil && len(presets) > 0 {
fmt.Printf(" Presets: %d available\n", len(presets))
}
}
}
fmt.Println()
// Test 5: PTZ control (if available)
if len(profiles) > 0 && profiles[0].PTZConfiguration != nil {
fmt.Println("🎮 Test 5: Testing PTZ Control...")
profileToken := profiles[0].Token
// Absolute move to home position
fmt.Println(" Moving to home position...")
position := &onvif.PTZVector{
PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0},
Zoom: &onvif.Vector1D{X: 0.0},
}
if err := client.AbsoluteMove(ctx, profileToken, position, nil); err != nil {
fmt.Printf(" ⚠️ Failed to move: %v\n", err)
} else {
fmt.Println(" ✅ Moved to home position")
}
// Wait a moment
time.Sleep(500 * time.Millisecond)
// Get status after move
status, err := client.GetStatus(ctx, profileToken)
if err == nil {
fmt.Printf(" New position: Pan=%.1f°, Tilt=%.1f°, Zoom=%.2f\n",
status.Position.PanTilt.X,
status.Position.PanTilt.Y,
status.Position.Zoom.X)
}
fmt.Println()
}
// Summary
fmt.Println("╔════════════════════════════════════════════════════════════╗")
fmt.Println("║ ║")
fmt.Println("║ ✅ All Tests Passed! ✅ ║")
fmt.Println("║ ║")
fmt.Println("╚════════════════════════════════════════════════════════════╝")
fmt.Println()
fmt.Println("🎉 ONVIF Server is working correctly!")
fmt.Println(" • Device Service: ✓")
fmt.Println(" • Media Service: ✓")
fmt.Println(" • PTZ Service: ✓")
fmt.Printf(" • Multi-lens Camera: ✓ (%d profiles)\n", len(profiles))
}