Initial Commit

This commit is contained in:
ProtoTess
2025-10-30 00:50:27 +00:00
parent 2e3156df5d
commit 1319e1ea3f
25 changed files with 5216 additions and 16 deletions
+275
View File
@@ -0,0 +1,275 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524A/go-onvif"
"github.com/0x524A/go-onvif/discovery"
)
// This is a comprehensive demonstration of all go-onvif features
func main() {
// Step 1: Discover cameras on the network
fmt.Println("=== Step 1: Discovering ONVIF Cameras ===")
discoverCameras()
// Step 2: Connect to a specific camera
fmt.Println("\n=== Step 2: Connecting to Camera ===")
client := connectToCamera()
// Step 3: Get device information
fmt.Println("\n=== Step 3: Getting Device Information ===")
getDeviceInfo(client)
// Step 4: Get media profiles and streams
fmt.Println("\n=== Step 4: Getting Media Profiles ===")
profiles := getMediaProfiles(client)
// Step 5: Control PTZ
if len(profiles) > 0 {
fmt.Println("\n=== Step 5: PTZ Control ===")
controlPTZ(client, profiles[0].Token)
}
// Step 6: Adjust imaging settings
if len(profiles) > 0 && profiles[0].VideoSourceConfiguration != nil {
fmt.Println("\n=== Step 6: Adjusting Imaging Settings ===")
adjustImaging(client, profiles[0].VideoSourceConfiguration.SourceToken)
}
fmt.Println("\n=== All operations completed successfully! ===")
}
// discoverCameras demonstrates network discovery
func discoverCameras() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
devices, err := discovery.Discover(ctx, 5*time.Second)
if err != nil {
log.Printf("Discovery error: %v", err)
return
}
fmt.Printf("Found %d device(s):\n", len(devices))
for i, device := range devices {
fmt.Printf(" [%d] %s at %s\n", i+1, device.GetName(), device.GetDeviceEndpoint())
}
}
// connectToCamera creates and initializes a client
func connectToCamera() *onvif.Client {
// Replace with your camera's details
endpoint := "http://192.168.1.100/onvif/device_service"
username := "admin"
password := "password"
client, err := onvif.NewClient(
endpoint,
onvif.WithCredentials(username, password),
onvif.WithTimeout(30*time.Second),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Initialize to discover service endpoints
ctx := context.Background()
if err := client.Initialize(ctx); err != nil {
log.Fatalf("Failed to initialize: %v", err)
}
fmt.Printf("Connected to: %s\n", endpoint)
return client
}
// getDeviceInfo retrieves and displays device information
func getDeviceInfo(client *onvif.Client) {
ctx := context.Background()
info, err := client.GetDeviceInformation(ctx)
if err != nil {
log.Printf("Failed to get device info: %v", err)
return
}
fmt.Printf("Manufacturer: %s\n", info.Manufacturer)
fmt.Printf("Model: %s\n", info.Model)
fmt.Printf("Firmware: %s\n", info.FirmwareVersion)
fmt.Printf("Serial: %s\n", info.SerialNumber)
// Get capabilities
caps, err := client.GetCapabilities(ctx)
if err != nil {
log.Printf("Failed to get capabilities: %v", err)
return
}
fmt.Println("\nSupported Services:")
if caps.Media != nil {
fmt.Printf(" ✓ Media (Streaming)\n")
}
if caps.PTZ != nil {
fmt.Printf(" ✓ PTZ (Pan/Tilt/Zoom)\n")
}
if caps.Imaging != nil {
fmt.Printf(" ✓ Imaging (Image Settings)\n")
}
if caps.Events != nil {
fmt.Printf(" ✓ Events\n")
}
}
// getMediaProfiles retrieves media profiles and stream URIs
func getMediaProfiles(client *onvif.Client) []*onvif.Profile {
ctx := context.Background()
profiles, err := client.GetProfiles(ctx)
if err != nil {
log.Printf("Failed to get profiles: %v", err)
return nil
}
fmt.Printf("Found %d profile(s):\n", len(profiles))
for i, profile := range profiles {
fmt.Printf("\nProfile [%d]: %s\n", i+1, profile.Name)
// Video configuration
if profile.VideoEncoderConfiguration != nil {
fmt.Printf(" Encoding: %s\n", profile.VideoEncoderConfiguration.Encoding)
if profile.VideoEncoderConfiguration.Resolution != nil {
fmt.Printf(" Resolution: %dx%d\n",
profile.VideoEncoderConfiguration.Resolution.Width,
profile.VideoEncoderConfiguration.Resolution.Height)
}
}
// Get stream URI
streamURI, err := client.GetStreamURI(ctx, profile.Token)
if err != nil {
fmt.Printf(" Stream URI: Error - %v\n", err)
} else {
fmt.Printf(" Stream URI: %s\n", streamURI.URI)
}
// Get snapshot URI
snapshotURI, err := client.GetSnapshotURI(ctx, profile.Token)
if err != nil {
fmt.Printf(" Snapshot URI: Error - %v\n", err)
} else {
fmt.Printf(" Snapshot URI: %s\n", snapshotURI.URI)
}
}
return profiles
}
// controlPTZ demonstrates PTZ operations
func controlPTZ(client *onvif.Client, profileToken string) {
ctx := context.Background()
// Get current status
status, err := client.GetStatus(ctx, profileToken)
if err != nil {
log.Printf("PTZ not supported: %v", err)
return
}
fmt.Println("PTZ is supported!")
if status.Position != nil && status.Position.PanTilt != nil {
fmt.Printf("Current Position: Pan=%.2f, Tilt=%.2f\n",
status.Position.PanTilt.X,
status.Position.PanTilt.Y)
}
// Get presets
presets, err := client.GetPresets(ctx, profileToken)
if err != nil {
log.Printf("Failed to get presets: %v", err)
} else {
fmt.Printf("Available Presets: %d\n", len(presets))
for _, preset := range presets {
fmt.Printf(" - %s\n", preset.Name)
}
}
// Demonstrate movement (commented out to avoid camera movement)
/*
// Move right
velocity := &onvif.PTZSpeed{
PanTilt: &onvif.Vector2D{X: 0.3, Y: 0.0},
}
timeout := "PT1S"
if err := client.ContinuousMove(ctx, profileToken, velocity, &timeout); err != nil {
log.Printf("Move failed: %v", err)
}
time.Sleep(1 * time.Second)
client.Stop(ctx, profileToken, true, false)
// Return to home
home := &onvif.PTZVector{
PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0},
}
client.AbsoluteMove(ctx, profileToken, home, nil)
*/
fmt.Println("PTZ operations available (commented out in demo)")
}
// adjustImaging demonstrates imaging settings
func adjustImaging(client *onvif.Client, videoSourceToken string) {
ctx := context.Background()
// Get current settings
settings, err := client.GetImagingSettings(ctx, videoSourceToken)
if err != nil {
log.Printf("Failed to get imaging settings: %v", err)
return
}
fmt.Println("Current Imaging Settings:")
if settings.Brightness != nil {
fmt.Printf(" Brightness: %.1f\n", *settings.Brightness)
}
if settings.Contrast != nil {
fmt.Printf(" Contrast: %.1f\n", *settings.Contrast)
}
if settings.ColorSaturation != nil {
fmt.Printf(" Saturation: %.1f\n", *settings.ColorSaturation)
}
if settings.Sharpness != nil {
fmt.Printf(" Sharpness: %.1f\n", *settings.Sharpness)
}
if settings.Exposure != nil {
fmt.Printf(" Exposure Mode: %s\n", settings.Exposure.Mode)
}
if settings.Focus != nil {
fmt.Printf(" Focus Mode: %s\n", settings.Focus.AutoFocusMode)
}
if settings.WhiteBalance != nil {
fmt.Printf(" White Balance: %s\n", settings.WhiteBalance.Mode)
}
// Demonstrate setting adjustment (commented out to avoid changes)
/*
// Adjust brightness
newBrightness := 55.0
settings.Brightness = &newBrightness
if err := client.SetImagingSettings(ctx, videoSourceToken, settings, true); err != nil {
log.Printf("Failed to set imaging settings: %v", err)
} else {
fmt.Println("\nImaging settings updated!")
}
*/
fmt.Println("Imaging adjustment available (commented out in demo)")
}
+93
View File
@@ -0,0 +1,93 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524A/go-onvif"
)
func main() {
// Camera connection details
endpoint := "http://192.168.1.100/onvif/device_service"
username := "admin"
password := "password"
fmt.Println("Connecting to ONVIF camera...")
// Create a new ONVIF client
client, err := onvif.NewClient(
endpoint,
onvif.WithCredentials(username, password),
onvif.WithTimeout(30*time.Second),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Get device information
fmt.Println("\nRetrieving device information...")
info, err := client.GetDeviceInformation(ctx)
if err != nil {
log.Fatalf("Failed to get device information: %v", err)
}
fmt.Printf("\nDevice Information:\n")
fmt.Printf(" Manufacturer: %s\n", info.Manufacturer)
fmt.Printf(" Model: %s\n", info.Model)
fmt.Printf(" Firmware: %s\n", info.FirmwareVersion)
fmt.Printf(" Serial Number: %s\n", info.SerialNumber)
fmt.Printf(" Hardware ID: %s\n", info.HardwareID)
// Initialize client (discover service endpoints)
fmt.Println("\nInitializing client and discovering services...")
if err := client.Initialize(ctx); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
}
// Get media profiles
fmt.Println("\nRetrieving media profiles...")
profiles, err := client.GetProfiles(ctx)
if err != nil {
log.Fatalf("Failed to get profiles: %v", err)
}
fmt.Printf("\nFound %d profile(s):\n", len(profiles))
for i, profile := range profiles {
fmt.Printf("\nProfile #%d:\n", i+1)
fmt.Printf(" Token: %s\n", profile.Token)
fmt.Printf(" Name: %s\n", profile.Name)
if profile.VideoEncoderConfiguration != nil {
fmt.Printf(" Video Encoding: %s\n", profile.VideoEncoderConfiguration.Encoding)
if profile.VideoEncoderConfiguration.Resolution != nil {
fmt.Printf(" Resolution: %dx%d\n",
profile.VideoEncoderConfiguration.Resolution.Width,
profile.VideoEncoderConfiguration.Resolution.Height)
}
fmt.Printf(" Quality: %.1f\n", profile.VideoEncoderConfiguration.Quality)
}
// Get stream URI
streamURI, err := client.GetStreamURI(ctx, profile.Token)
if err != nil {
fmt.Printf(" Stream URI: Error - %v\n", err)
} else {
fmt.Printf(" Stream URI: %s\n", streamURI.URI)
}
// Get snapshot URI
snapshotURI, err := client.GetSnapshotURI(ctx, profile.Token)
if err != nil {
fmt.Printf(" Snapshot URI: Error - %v\n", err)
} else {
fmt.Printf(" Snapshot URI: %s\n", snapshotURI.URI)
}
}
fmt.Println("\nDone!")
}
+42
View File
@@ -0,0 +1,42 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524A/go-onvif/discovery"
)
func main() {
fmt.Println("Discovering ONVIF devices on the network...")
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Discover devices
devices, err := discovery.Discover(ctx, 5*time.Second)
if err != nil {
log.Fatalf("Discovery failed: %v", err)
}
if len(devices) == 0 {
fmt.Println("No ONVIF devices found on the network")
return
}
fmt.Printf("\nFound %d device(s):\n\n", len(devices))
for i, device := range devices {
fmt.Printf("Device #%d:\n", i+1)
fmt.Printf(" Endpoint: %s\n", device.GetDeviceEndpoint())
fmt.Printf(" Name: %s\n", device.GetName())
fmt.Printf(" Location: %s\n", device.GetLocation())
fmt.Printf(" Types: %v\n", device.Types)
fmt.Printf(" Scopes: %v\n", device.Scopes)
fmt.Printf(" XAddrs: %v\n", device.XAddrs)
fmt.Println()
}
}
+143
View File
@@ -0,0 +1,143 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524A/go-onvif"
)
func main() {
// Camera connection details
endpoint := "http://192.168.1.100/onvif/device_service"
username := "admin"
password := "password"
fmt.Println("Connecting to ONVIF camera...")
// Create a new ONVIF client
client, err := onvif.NewClient(
endpoint,
onvif.WithCredentials(username, password),
onvif.WithTimeout(30*time.Second),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Initialize client
if err := client.Initialize(ctx); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
}
// Get profiles
profiles, err := client.GetProfiles(ctx)
if err != nil {
log.Fatalf("Failed to get profiles: %v", err)
}
if len(profiles) == 0 {
log.Fatal("No profiles found")
}
// Get video source token from profile
profile := profiles[0]
if profile.VideoSourceConfiguration == nil {
log.Fatal("No video source configuration found")
}
videoSourceToken := profile.VideoSourceConfiguration.SourceToken
fmt.Printf("Using video source: %s\n\n", videoSourceToken)
// Get current imaging settings
fmt.Println("Getting current imaging settings...")
settings, err := client.GetImagingSettings(ctx, videoSourceToken)
if err != nil {
log.Fatalf("Failed to get imaging settings: %v", err)
}
fmt.Println("\nCurrent Imaging Settings:")
if settings.Brightness != nil {
fmt.Printf(" Brightness: %.2f\n", *settings.Brightness)
}
if settings.Contrast != nil {
fmt.Printf(" Contrast: %.2f\n", *settings.Contrast)
}
if settings.ColorSaturation != nil {
fmt.Printf(" Saturation: %.2f\n", *settings.ColorSaturation)
}
if settings.Sharpness != nil {
fmt.Printf(" Sharpness: %.2f\n", *settings.Sharpness)
}
if settings.IrCutFilter != nil {
fmt.Printf(" IR Cut Filter: %s\n", *settings.IrCutFilter)
}
if settings.Exposure != nil {
fmt.Printf(" Exposure Mode: %s\n", settings.Exposure.Mode)
if settings.Exposure.Mode == "MANUAL" {
fmt.Printf(" Exposure Time: %.2f\n", settings.Exposure.ExposureTime)
fmt.Printf(" Gain: %.2f\n", settings.Exposure.Gain)
}
}
if settings.Focus != nil {
fmt.Printf(" Focus Mode: %s\n", settings.Focus.AutoFocusMode)
}
if settings.WhiteBalance != nil {
fmt.Printf(" White Balance Mode: %s\n", settings.WhiteBalance.Mode)
}
if settings.WideDynamicRange != nil {
fmt.Printf(" WDR Mode: %s\n", settings.WideDynamicRange.Mode)
fmt.Printf(" WDR Level: %.2f\n", settings.WideDynamicRange.Level)
}
// Modify some settings
fmt.Println("\n\nModifying imaging settings...")
// Increase brightness
newBrightness := 60.0
settings.Brightness = &newBrightness
// Increase contrast
newContrast := 55.0
settings.Contrast = &newContrast
// Set to auto exposure
if settings.Exposure != nil {
settings.Exposure.Mode = "AUTO"
}
// Apply new settings
if err := client.SetImagingSettings(ctx, videoSourceToken, settings, true); err != nil {
log.Fatalf("Failed to set imaging settings: %v", err)
}
fmt.Println("Imaging settings updated successfully!")
// Verify changes
fmt.Println("\nVerifying new settings...")
updatedSettings, err := client.GetImagingSettings(ctx, videoSourceToken)
if err != nil {
log.Fatalf("Failed to get updated imaging settings: %v", err)
}
fmt.Println("\nUpdated Imaging Settings:")
if updatedSettings.Brightness != nil {
fmt.Printf(" Brightness: %.2f\n", *updatedSettings.Brightness)
}
if updatedSettings.Contrast != nil {
fmt.Printf(" Contrast: %.2f\n", *updatedSettings.Contrast)
}
if updatedSettings.Exposure != nil {
fmt.Printf(" Exposure Mode: %s\n", updatedSettings.Exposure.Mode)
}
fmt.Println("\nImaging settings demonstration complete!")
}
+154
View File
@@ -0,0 +1,154 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/0x524A/go-onvif"
)
func main() {
// Camera connection details
endpoint := "http://192.168.1.100/onvif/device_service"
username := "admin"
password := "password"
fmt.Println("Connecting to ONVIF camera...")
// Create a new ONVIF client
client, err := onvif.NewClient(
endpoint,
onvif.WithCredentials(username, password),
onvif.WithTimeout(30*time.Second),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Initialize client
if err := client.Initialize(ctx); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
}
// Get profiles
profiles, err := client.GetProfiles(ctx)
if err != nil {
log.Fatalf("Failed to get profiles: %v", err)
}
if len(profiles) == 0 {
log.Fatal("No profiles found")
}
profileToken := profiles[0].Token
fmt.Printf("Using profile: %s\n\n", profiles[0].Name)
// Demonstrate PTZ controls
demonstratePTZ(ctx, client, profileToken)
}
func demonstratePTZ(ctx context.Context, client *onvif.Client, profileToken string) {
// Get current PTZ status
fmt.Println("Getting current PTZ status...")
status, err := client.GetStatus(ctx, profileToken)
if err != nil {
log.Printf("Warning: Failed to get PTZ status: %v\n", err)
} else {
fmt.Printf("Current Position:\n")
if status.Position != nil {
if status.Position.PanTilt != nil {
fmt.Printf(" Pan/Tilt: X=%.2f, Y=%.2f\n",
status.Position.PanTilt.X,
status.Position.PanTilt.Y)
}
if status.Position.Zoom != nil {
fmt.Printf(" Zoom: %.2f\n", status.Position.Zoom.X)
}
}
fmt.Println()
}
// Get presets
fmt.Println("Getting PTZ presets...")
presets, err := client.GetPresets(ctx, profileToken)
if err != nil {
log.Printf("Warning: Failed to get presets: %v\n", err)
} else {
fmt.Printf("Found %d preset(s):\n", len(presets))
for _, preset := range presets {
fmt.Printf(" - %s (Token: %s)\n", preset.Name, preset.Token)
}
fmt.Println()
}
// Continuous move right for 2 seconds
fmt.Println("Moving camera right...")
velocity := &onvif.PTZSpeed{
PanTilt: &onvif.Vector2D{
X: 0.5, // Move right
Y: 0.0,
},
}
timeout := "PT2S" // 2 seconds
if err := client.ContinuousMove(ctx, profileToken, velocity, &timeout); err != nil {
log.Printf("Failed to move: %v\n", err)
} else {
time.Sleep(2 * time.Second)
}
// Stop movement
fmt.Println("Stopping camera movement...")
if err := client.Stop(ctx, profileToken, true, false); err != nil {
log.Printf("Failed to stop: %v\n", err)
}
// Relative move
fmt.Println("\nPerforming relative move (up and zoom in)...")
translation := &onvif.PTZVector{
PanTilt: &onvif.Vector2D{
X: 0.0,
Y: 0.1, // Move up
},
Zoom: &onvif.Vector1D{
X: 0.1, // Zoom in
},
}
if err := client.RelativeMove(ctx, profileToken, translation, nil); err != nil {
log.Printf("Failed to relative move: %v\n", err)
} else {
time.Sleep(2 * time.Second)
}
// Absolute move to home position
fmt.Println("\nMoving to home position...")
homePosition := &onvif.PTZVector{
PanTilt: &onvif.Vector2D{
X: 0.0,
Y: 0.0,
},
Zoom: &onvif.Vector1D{
X: 0.0,
},
}
if err := client.AbsoluteMove(ctx, profileToken, homePosition, nil); err != nil {
log.Printf("Failed to absolute move: %v\n", err)
} else {
time.Sleep(2 * time.Second)
}
// Go to preset if available
if len(presets) > 0 {
fmt.Printf("\nGoing to preset: %s\n", presets[0].Name)
if err := client.GotoPreset(ctx, profileToken, presets[0].Token, nil); err != nil {
log.Printf("Failed to go to preset: %v\n", err)
} else {
time.Sleep(2 * time.Second)
}
}
fmt.Println("\nPTZ demonstration complete!")
}