refactor: rename project from go-onvif to onvif-go
- Updated project name in documentation and metadata to reflect new branding. - Removed outdated documentation files related to CLI and network interface summary. - Consolidated implementation summaries for network interface discovery and ONVIF server. - Updated sonar project properties to align with new project name. - Ensured backward compatibility and comprehensive testing across all changes.
This commit is contained in:
@@ -1,381 +0,0 @@
|
||||
# CLI Tools & Network Interface Discovery - Complete Implementation Summary
|
||||
|
||||
## 🎯 Project Completion Overview
|
||||
|
||||
Successfully enhanced the onvif-go project with comprehensive network interface discovery support across both the library API and CLI tools. This allows users with multiple active network interfaces to explicitly specify which interface to use for camera discovery.
|
||||
|
||||
## 📦 Deliverables
|
||||
|
||||
### 1. Library Enhancements (Discovery Module)
|
||||
|
||||
**Files Modified/Created**:
|
||||
- `discovery/discovery.go` - Added DiscoverOptions struct and new functions
|
||||
- `discovery/discovery_test.go` - Added 6 unit tests + 2 benchmarks
|
||||
- `discovery/NETWORK_INTERFACE_GUIDE.md` - 400+ line comprehensive guide
|
||||
|
||||
**New API**:
|
||||
```go
|
||||
type DiscoverOptions struct {
|
||||
NetworkInterface string // Interface name or IP address
|
||||
}
|
||||
|
||||
func DiscoverWithOptions(ctx context.Context, timeout time.Duration,
|
||||
opts *DiscoverOptions) ([]*Device, error)
|
||||
|
||||
func ListNetworkInterfaces() ([]NetworkInterface, error)
|
||||
|
||||
type NetworkInterface struct {
|
||||
Name string
|
||||
Addresses []string
|
||||
Up bool
|
||||
Multicast bool
|
||||
}
|
||||
```
|
||||
|
||||
**Test Results**: All tests passing ✅
|
||||
- TestListNetworkInterfaces ✅
|
||||
- TestResolveNetworkInterface (4 subtests) ✅
|
||||
- TestDiscoverWithOptions_* (3 variants) ✅
|
||||
- TestDiscover_BackwardCompatibility ✅
|
||||
- Benchmarks ✅
|
||||
|
||||
### 2. CLI Tool Enhancements
|
||||
|
||||
#### onvif-cli (Full-Featured Interactive Tool)
|
||||
|
||||
**Enhancements**:
|
||||
- New menu option: "List Network Interfaces"
|
||||
- Updated discovery function with interface selection
|
||||
- Interactive interface choice with helpful descriptions
|
||||
- Display interface status (up/down, multicast capability, assigned IPs)
|
||||
|
||||
**New Menu**:
|
||||
```
|
||||
📋 Main Menu:
|
||||
1. Discover Cameras on Network [NEW: with interface selection]
|
||||
2. List Network Interfaces [NEW]
|
||||
3. Connect to Camera
|
||||
4. Device Operations
|
||||
5. Media Operations
|
||||
6. PTZ Operations
|
||||
7. Imaging Operations
|
||||
0. Exit
|
||||
```
|
||||
|
||||
**Usage Flow**:
|
||||
1. Select "2" to list available interfaces
|
||||
2. Select "1" to discover
|
||||
3. Choose "y" for specific interface
|
||||
4. Enter interface name (eth0) or IP (192.168.1.100)
|
||||
|
||||
#### onvif-quick (Fast Demo Tool)
|
||||
|
||||
**Enhancements**:
|
||||
- New menu option: "List Network Interfaces"
|
||||
- Updated discovery with interface selection prompt
|
||||
- Simplified interface list display
|
||||
|
||||
**New Menu**:
|
||||
```
|
||||
1. 🔍 Discover cameras
|
||||
2. 🌐 List network interfaces [NEW]
|
||||
3. 📹 Connect to camera
|
||||
4. 🎮 PTZ demo
|
||||
5. 📡 Get stream URLs
|
||||
0. Exit
|
||||
```
|
||||
|
||||
**Build Instructions**:
|
||||
```bash
|
||||
go build -o onvif-cli ./cmd/onvif-cli/
|
||||
go build -o onvif-quick ./cmd/onvif-quick/
|
||||
```
|
||||
|
||||
### 3. Documentation
|
||||
|
||||
#### Created Files:
|
||||
1. **discovery/NETWORK_INTERFACE_GUIDE.md** (400+ lines)
|
||||
- Comprehensive API guide with 10+ examples
|
||||
- Common scenarios and troubleshooting
|
||||
- Best practices and error handling
|
||||
- Integration patterns
|
||||
|
||||
2. **docs/CLI_NETWORK_INTERFACE_USAGE.md** (600+ lines)
|
||||
- Complete CLI tool guide
|
||||
- Usage workflows and scenarios
|
||||
- Multi-interface environment guide
|
||||
- Troubleshooting section
|
||||
- Scripting examples
|
||||
|
||||
3. **docs/NETWORK_INTERFACE_IMPLEMENTATION.md** (260+ lines)
|
||||
- Implementation summary
|
||||
- API reference
|
||||
- Test results and verification
|
||||
- Benefits and future enhancements
|
||||
|
||||
#### Updated Files:
|
||||
- **QUICKSTART.md** - Added network interface discovery section
|
||||
- **README.md** - Added CLI tools section with examples
|
||||
|
||||
## 🔄 Usage Examples
|
||||
|
||||
### Library API Usage
|
||||
|
||||
**By Interface Name**:
|
||||
```go
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: "eth0",
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
**By IP Address**:
|
||||
```go
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: "192.168.1.100",
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
**List Available Interfaces**:
|
||||
```go
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
for _, iface := range interfaces {
|
||||
fmt.Printf("%s: %v (Multicast: %v)\n",
|
||||
iface.Name, iface.Addresses, iface.Multicast)
|
||||
}
|
||||
```
|
||||
|
||||
**Backward Compatible**:
|
||||
```go
|
||||
// Old code still works
|
||||
devices, err := discovery.Discover(ctx, 5*time.Second)
|
||||
```
|
||||
|
||||
### CLI Usage
|
||||
|
||||
**onvif-cli - Check Interfaces**:
|
||||
```bash
|
||||
./onvif-cli
|
||||
# Select: 2
|
||||
# Output shows all interfaces with IPs and multicast support
|
||||
```
|
||||
|
||||
**onvif-cli - Discover on Specific Interface**:
|
||||
```bash
|
||||
./onvif-cli
|
||||
# Select: 1
|
||||
# Answer: y (use specific interface)
|
||||
# Enter: eth0
|
||||
# Result: Discovers cameras on eth0 only
|
||||
```
|
||||
|
||||
**onvif-quick - Quick Discovery**:
|
||||
```bash
|
||||
./onvif-quick
|
||||
# Select: 1
|
||||
# Answer: y (use specific interface)
|
||||
# Enter: wlan0
|
||||
# Result: Finds cameras on WiFi interface
|
||||
```
|
||||
|
||||
## 📊 Implementation Statistics
|
||||
|
||||
### Code Changes
|
||||
- **discovery/discovery.go**: +145 lines (production code)
|
||||
- **discovery/discovery_test.go**: +200 lines (test coverage)
|
||||
- **cmd/onvif-cli/main.go**: +120 lines modified
|
||||
- **cmd/onvif-quick/main.go**: +90 lines modified
|
||||
- **Documentation**: 1,300+ new lines across 5 files
|
||||
|
||||
### Testing
|
||||
- **Unit Tests**: 6 new tests covering all functionality
|
||||
- **Benchmarks**: 2 performance benchmarks
|
||||
- **Test Coverage**: All code paths tested
|
||||
- **Test Duration**: ~3 seconds for full suite
|
||||
- **Result**: ✅ 100% passing
|
||||
|
||||
### Documentation
|
||||
- **discovery/NETWORK_INTERFACE_GUIDE.md**: 400 lines
|
||||
- **docs/CLI_NETWORK_INTERFACE_USAGE.md**: 600 lines
|
||||
- **docs/NETWORK_INTERFACE_IMPLEMENTATION.md**: 260 lines
|
||||
- **Total Documentation**: 1,260+ lines
|
||||
- **Code Examples**: 20+ working examples included
|
||||
|
||||
## 🔗 Git Commits
|
||||
|
||||
All work on `fix-go-onvif-references` branch:
|
||||
|
||||
1. **c384dca** - `feat: add network interface selection to WS-Discovery`
|
||||
- Core discovery module enhancement
|
||||
- Comprehensive test suite
|
||||
- NETWORK_INTERFACE_GUIDE.md
|
||||
|
||||
2. **d6e5cbd** - `docs: add network interface discovery section to QUICKSTART`
|
||||
- Updated quick start guide
|
||||
- Added usage examples
|
||||
|
||||
3. **dfa113a** - `docs: add network interface implementation summary`
|
||||
- Implementation documentation
|
||||
- API reference
|
||||
- Verification checklist
|
||||
|
||||
4. **46035f4** - `feat: add network interface selection to CLI tools`
|
||||
- Enhanced onvif-cli
|
||||
- Enhanced onvif-quick
|
||||
- CLI_NETWORK_INTERFACE_USAGE.md guide
|
||||
|
||||
5. **ead5558** - `docs: add CLI tools and network interface selection to README`
|
||||
- Updated main README
|
||||
- Added CLI tools section
|
||||
- Cross-references to guides
|
||||
|
||||
## ✅ Verification Checklist
|
||||
|
||||
### Core Functionality
|
||||
- ✅ DiscoverWithOptions() works with interface names
|
||||
- ✅ DiscoverWithOptions() works with IP addresses
|
||||
- ✅ ListNetworkInterfaces() returns all interfaces
|
||||
- ✅ Error handling with helpful messages
|
||||
- ✅ Backward compatibility with Discover()
|
||||
|
||||
### Testing
|
||||
- ✅ All unit tests passing (6 tests)
|
||||
- ✅ All benchmarks passing
|
||||
- ✅ No compilation errors
|
||||
- ✅ No unused variables
|
||||
- ✅ Test coverage comprehensive
|
||||
|
||||
### CLI Tools
|
||||
- ✅ onvif-cli builds successfully
|
||||
- ✅ onvif-cli menus working
|
||||
- ✅ onvif-cli interface listing works
|
||||
- ✅ onvif-cli discovery with interface works
|
||||
- ✅ onvif-quick builds successfully
|
||||
- ✅ onvif-quick features working
|
||||
|
||||
### Documentation
|
||||
- ✅ API documentation complete
|
||||
- ✅ Usage examples correct and tested
|
||||
- ✅ Troubleshooting section helpful
|
||||
- ✅ README updated
|
||||
- ✅ QUICKSTART updated
|
||||
- ✅ Cross-references working
|
||||
|
||||
## 🎁 Benefits
|
||||
|
||||
### For Users
|
||||
- ✅ Solve multi-interface discovery problems
|
||||
- ✅ Easy-to-use CLI tools
|
||||
- ✅ Flexible API supporting multiple input formats
|
||||
- ✅ Clear error messages with available options
|
||||
- ✅ Backward compatible - no breaking changes
|
||||
|
||||
### For Developers
|
||||
- ✅ Well-documented API
|
||||
- ✅ Comprehensive examples
|
||||
- ✅ Full test coverage
|
||||
- ✅ No external dependencies
|
||||
- ✅ Standard Go patterns
|
||||
|
||||
### For Systems
|
||||
- ✅ Support Docker multi-network scenarios
|
||||
- ✅ Support VM multi-adapter scenarios
|
||||
- ✅ Support mixed WiFi/Ethernet setups
|
||||
- ✅ Robust error handling
|
||||
- ✅ Production-ready
|
||||
|
||||
## 📝 Common Use Cases
|
||||
|
||||
### Use Case 1: Multi-Network System
|
||||
```bash
|
||||
# List available networks
|
||||
./onvif-cli
|
||||
# 2 - See eth0, wlan0, docker0
|
||||
|
||||
# Discover on Ethernet
|
||||
./onvif-cli
|
||||
# 1 -> y -> eth0
|
||||
|
||||
# Discover on WiFi
|
||||
./onvif-cli
|
||||
# 1 -> y -> wlan0
|
||||
```
|
||||
|
||||
### Use Case 2: Docker Container
|
||||
```bash
|
||||
# Container has management and camera networks
|
||||
./onvif-quick
|
||||
# 1 -> y -> 172.20.0.10 (camera network)
|
||||
# Discovers cameras on correct network
|
||||
```
|
||||
|
||||
### Use Case 3: Automated Discovery
|
||||
```go
|
||||
// Try each interface until found
|
||||
for _, iface := range interfaces {
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: iface.Name,
|
||||
}
|
||||
devices, _ := discovery.DiscoverWithOptions(ctx, 2*time.Second, opts)
|
||||
if len(devices) > 0 {
|
||||
return devices
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 🚀 Next Steps & Future Enhancements
|
||||
|
||||
### Potential Enhancements
|
||||
- [ ] IPv6-specific discovery option
|
||||
- [ ] Multicast group customization
|
||||
- [ ] Async discovery across multiple interfaces
|
||||
- [ ] Interface event detection
|
||||
- [ ] Performance optimization for large interface counts
|
||||
|
||||
### Integration Opportunities
|
||||
- [ ] Web UI for discovering cameras
|
||||
- [ ] REST API wrapper
|
||||
- [ ] Kubernetes integration
|
||||
- [ ] Cloud native support
|
||||
- [ ] Advanced filtering options
|
||||
|
||||
## 📚 Related Documentation
|
||||
|
||||
- [discovery/NETWORK_INTERFACE_GUIDE.md](../../discovery/NETWORK_INTERFACE_GUIDE.md)
|
||||
- [docs/CLI_NETWORK_INTERFACE_USAGE.md](../CLI_NETWORK_INTERFACE_USAGE.md)
|
||||
- [QUICKSTART.md](../../QUICKSTART.md)
|
||||
- [README.md](../../README.md)
|
||||
- [ARCHITECTURE.md](../ARCHITECTURE.md)
|
||||
|
||||
## 🎯 Project Status
|
||||
|
||||
### Completed ✅
|
||||
- Network interface selection in discovery module
|
||||
- Comprehensive test coverage (6 tests + 2 benchmarks)
|
||||
- CLI tool enhancements (onvif-cli & onvif-quick)
|
||||
- Extensive documentation (1,300+ lines)
|
||||
- All code changes pushed to branch
|
||||
- All tests passing
|
||||
- No breaking changes
|
||||
- Backward compatibility maintained
|
||||
|
||||
### Ready for
|
||||
- Pull Request review
|
||||
- Integration testing
|
||||
- Production deployment
|
||||
- User feedback
|
||||
|
||||
## 📞 Support
|
||||
|
||||
For questions or issues related to the network interface discovery feature:
|
||||
1. Check `discovery/NETWORK_INTERFACE_GUIDE.md` for API usage
|
||||
2. Check `docs/CLI_NETWORK_INTERFACE_USAGE.md` for CLI usage
|
||||
3. Review troubleshooting sections in documentation
|
||||
4. Open an issue on GitHub with details
|
||||
|
||||
## Summary
|
||||
|
||||
The onvif-go project now has comprehensive, production-ready network interface selection support across both the library API and interactive CLI tools. Users can easily specify which network interface to use for ONVIF camera discovery, solving real-world problems with multi-interface systems. All code is thoroughly tested, well-documented, and fully backward compatible.
|
||||
|
||||
**Ready for integration and public use! 🎉**
|
||||
@@ -1,146 +0,0 @@
|
||||
# Go ONVIF Library - Complete Implementation Summary
|
||||
|
||||
## 🎯 Mission Accomplished!
|
||||
|
||||
We have successfully created a **comprehensive, production-ready Go ONVIF library** that completely refactors and modernizes the original implementation. Here's what was delivered:
|
||||
|
||||
## 📦 Complete Library Implementation
|
||||
|
||||
### Core Components
|
||||
- **`client.go`** - Main ONVIF client with functional options pattern
|
||||
- **`types.go`** - Comprehensive ONVIF type definitions (40+ structs)
|
||||
- **`device.go`** - Device service implementation
|
||||
- **`media.go`** - Media service for streaming and profiles
|
||||
- **`ptz.go`** - PTZ control implementation
|
||||
- **`imaging.go`** - Image settings control
|
||||
- **`soap/soap.go`** - SOAP client with WS-Security authentication
|
||||
- **`discovery/discovery.go`** - WS-Discovery multicast implementation
|
||||
|
||||
### Features Delivered
|
||||
✅ **Complete ONVIF Profile S Support**
|
||||
✅ **WS-Discovery for automatic camera detection**
|
||||
✅ **WS-Security authentication with SHA-1 digest**
|
||||
✅ **PTZ control (continuous, absolute, relative movements)**
|
||||
✅ **Media profile management and stream URIs**
|
||||
✅ **Imaging settings control (brightness, contrast, etc.)**
|
||||
✅ **Device information and capabilities discovery**
|
||||
✅ **Context-based timeout and cancellation**
|
||||
✅ **Thread-safe credential management**
|
||||
✅ **Comprehensive error handling with custom ONVIF errors**
|
||||
|
||||
## 🛠️ Interactive CLI Tools
|
||||
|
||||
### 1. Comprehensive CLI (`onvif-cli`)
|
||||
- Full-featured interactive menu system
|
||||
- Camera discovery and connection
|
||||
- All ONVIF operations with guided inputs
|
||||
- Real-time parameter validation
|
||||
- Comprehensive error handling with troubleshooting tips
|
||||
|
||||
### 2. Quick Tool (`onvif-quick`)
|
||||
- Simple, streamlined interface
|
||||
- Essential operations (discovery, connection, PTZ demo)
|
||||
- Fast testing and demos
|
||||
- User-friendly prompts with defaults
|
||||
|
||||
## 🏗️ Development Infrastructure
|
||||
|
||||
### Build System
|
||||
- **Makefile** with comprehensive targets
|
||||
- Multi-platform builds (Linux, Windows, macOS - AMD64/ARM64)
|
||||
- Docker containerization
|
||||
- Development environment setup
|
||||
|
||||
### Testing & Quality
|
||||
- **Comprehensive test suite** with mock ONVIF server
|
||||
- Benchmark tests for performance validation
|
||||
- Coverage reporting
|
||||
- Example programs for different use cases
|
||||
- CI/CD ready structure
|
||||
|
||||
### Documentation
|
||||
- **Extensive README** with usage examples
|
||||
- API documentation with code samples
|
||||
- Contributing guidelines
|
||||
- Docker deployment instructions
|
||||
- Examples for every major feature
|
||||
|
||||
## 🚀 Modern Go Best Practices
|
||||
|
||||
### Architecture
|
||||
- **Go 1.21+** with modern patterns
|
||||
- **Functional options pattern** for client configuration
|
||||
- **Context-first design** for cancellation and timeouts
|
||||
- **Interface-based design** for extensibility
|
||||
- **Comprehensive error types** with detailed context
|
||||
|
||||
### Code Quality
|
||||
- Proper dependency management with Go modules
|
||||
- Thread-safe implementations
|
||||
- Comprehensive logging and debugging support
|
||||
- Production-ready error handling
|
||||
- Performance optimizations
|
||||
|
||||
## 📋 How to Use
|
||||
|
||||
### Basic Library Usage
|
||||
```go
|
||||
import "github.com/0x524a/onvif-go"
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
"http://192.168.1.100/onvif/device_service",
|
||||
onvif.WithCredentials("admin", "password"),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
```
|
||||
|
||||
### CLI Tools
|
||||
```bash
|
||||
# Build tools
|
||||
make build
|
||||
|
||||
# Run interactive CLI
|
||||
./bin/onvif-cli
|
||||
|
||||
# Run quick tool
|
||||
./bin/onvif-quick
|
||||
|
||||
# Run discovery example
|
||||
./bin/examples/discovery
|
||||
```
|
||||
|
||||
### Docker Deployment
|
||||
```bash
|
||||
# Build image
|
||||
make docker
|
||||
|
||||
# Run container
|
||||
docker run -it onvif-go:latest
|
||||
```
|
||||
|
||||
## 🎯 Key Improvements from Original
|
||||
|
||||
1. **Modern Go Architecture** - Updated to Go 1.21+ patterns
|
||||
2. **Better Error Handling** - Comprehensive error types and context
|
||||
3. **Interactive CLI Tools** - User-friendly interfaces for testing
|
||||
4. **Complete Test Coverage** - Mock servers and comprehensive testing
|
||||
5. **Production Ready** - Thread-safe, context-aware, robust
|
||||
6. **Developer Experience** - Easy setup, clear documentation, examples
|
||||
7. **Extensible Design** - Easy to add new ONVIF services
|
||||
8. **Performance Optimized** - Efficient HTTP client management
|
||||
|
||||
## 🏆 Result
|
||||
|
||||
This implementation provides a **modern, comprehensive, production-ready ONVIF library** that:
|
||||
- Works with any ONVIF-compliant camera
|
||||
- Provides both programmatic API and interactive CLI tools
|
||||
- Includes extensive testing and documentation
|
||||
- Follows Go best practices and patterns
|
||||
- Is ready for production deployment
|
||||
|
||||
The library completely fulfills the original request to "create a new innovative and performant library that can connect to any ONVIF supporting camera and help communicating with it" plus adds interactive binary tools for direct camera interaction.
|
||||
|
||||
**🎉 Ready for real-world usage with actual ONVIF cameras!**
|
||||
@@ -1,262 +0,0 @@
|
||||
# Network Interface Discovery Feature - Implementation Summary
|
||||
|
||||
## Overview
|
||||
|
||||
Successfully implemented network interface selection for ONVIF device discovery via WS-Discovery multicast. This feature allows users to explicitly specify which network interface to use when discovering cameras on their network.
|
||||
|
||||
## Problem Statement
|
||||
|
||||
Users with multiple active network interfaces (Ethernet, WiFi, Virtual Adapters, etc.) often encounter situations where the auto-detected network interface isn't the one connected to their cameras. This results in failed discovery despite cameras being present on another network segment.
|
||||
|
||||
## Solution
|
||||
|
||||
Added optional `DiscoverOptions` parameter to discovery functions, allowing users to:
|
||||
- Specify interface by name (e.g., "eth0", "wlan0")
|
||||
- Specify interface by IP address (e.g., "192.168.1.100")
|
||||
- Enumerate all available interfaces with metadata
|
||||
- Get helpful error messages listing available options
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### Files Modified
|
||||
|
||||
**`discovery/discovery.go`**
|
||||
- Added `DiscoverOptions` struct with `NetworkInterface` field
|
||||
- Added `DiscoverWithOptions()` function for interface-specific discovery
|
||||
- Added `ListNetworkInterfaces()` public function
|
||||
- Added `resolveNetworkInterface()` helper function
|
||||
- Maintained backward compatibility with existing `Discover()` function
|
||||
|
||||
**`discovery/discovery_test.go`**
|
||||
- Added comprehensive test suite (6 unit tests + 2 benchmarks)
|
||||
- Tests cover: listing, resolution by name, resolution by IP, error handling
|
||||
- All tests passing (3.009s runtime)
|
||||
|
||||
### Files Created
|
||||
|
||||
**`discovery/NETWORK_INTERFACE_GUIDE.md`**
|
||||
- Comprehensive usage guide with examples
|
||||
- API reference documentation
|
||||
- Common scenarios and troubleshooting
|
||||
- Best practices and error handling patterns
|
||||
- 400+ lines of detailed documentation
|
||||
|
||||
**`QUICKSTART.md` (Updated)**
|
||||
- Added network interface discovery section
|
||||
- Included examples for all three usage patterns
|
||||
- Cross-reference to detailed guide
|
||||
|
||||
## API Reference
|
||||
|
||||
### New Functions
|
||||
|
||||
```go
|
||||
// Discover with custom options
|
||||
func DiscoverWithOptions(ctx context.Context, timeout time.Duration,
|
||||
opts *DiscoverOptions) ([]*Device, error)
|
||||
|
||||
// List all available interfaces
|
||||
func ListNetworkInterfaces() ([]NetworkInterface, error)
|
||||
```
|
||||
|
||||
### New Types
|
||||
|
||||
```go
|
||||
type DiscoverOptions struct {
|
||||
// NetworkInterface specifies which interface to use
|
||||
// Examples: "eth0", "192.168.1.100"
|
||||
// Empty string = system default
|
||||
NetworkInterface string
|
||||
}
|
||||
|
||||
type NetworkInterface struct {
|
||||
Name string // "eth0", "wlan0", etc.
|
||||
Addresses []string // IP addresses
|
||||
Up bool // Is interface up?
|
||||
Multicast bool // Supports multicast?
|
||||
}
|
||||
```
|
||||
|
||||
### Backward Compatibility
|
||||
|
||||
The existing `Discover()` function continues to work unchanged:
|
||||
|
||||
```go
|
||||
// Old code still works
|
||||
devices, err := discovery.Discover(ctx, 5*time.Second)
|
||||
|
||||
// New code with options
|
||||
opts := &discovery.DiscoverOptions{NetworkInterface: "eth0"}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### List Available Interfaces
|
||||
|
||||
```go
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
for _, iface := range interfaces {
|
||||
fmt.Printf("%s: up=%v, multicast=%v, ips=%v\n",
|
||||
iface.Name, iface.Up, iface.Multicast, iface.Addresses)
|
||||
}
|
||||
```
|
||||
|
||||
### Discover on Specific Interface
|
||||
|
||||
```go
|
||||
// By interface name
|
||||
opts := &discovery.DiscoverOptions{NetworkInterface: "eth0"}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
|
||||
// By IP address
|
||||
opts := &discovery.DiscoverOptions{NetworkInterface: "192.168.1.100"}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
```go
|
||||
opts := &discovery.DiscoverOptions{NetworkInterface: "invalid-interface"}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
if err != nil {
|
||||
// Error includes list of available interfaces
|
||||
fmt.Println(err)
|
||||
// Output: network interface "invalid-interface" not found.
|
||||
// Available interfaces: [eth0 [192.168.1.100] wlan0 [192.168.88.50] ...]
|
||||
}
|
||||
```
|
||||
|
||||
## Testing Results
|
||||
|
||||
```
|
||||
=== RUN TestListNetworkInterfaces
|
||||
discovery_test.go:279: Found 3 network interface(s)
|
||||
discovery_test.go:281: - lo: up=true, multicast=false, addresses=[127.0.0.1 ::1]
|
||||
discovery_test.go:281: - eth0: up=true, multicast=true, addresses=[10.0.0.27 fe80::...]
|
||||
discovery_test.go:281: - docker0: up=true, multicast=true, addresses=[172.17.0.1]
|
||||
--- PASS: TestListNetworkInterfaces (0.00s)
|
||||
|
||||
=== RUN TestResolveNetworkInterface
|
||||
=== RUN TestResolveNetworkInterface/loopback_by_name
|
||||
discovery_test.go:328: Resolved lo to interface: lo
|
||||
=== RUN TestResolveNetworkInterface/loopback_by_ip
|
||||
discovery_test.go:328: Resolved 127.0.0.1 to interface: lo
|
||||
=== RUN TestResolveNetworkInterface/invalid_interface
|
||||
--- PASS: TestResolveNetworkInterface (0.00s)
|
||||
|
||||
=== RUN TestDiscoverWithOptions_DefaultOptions
|
||||
--- PASS: TestDiscoverWithOptions_DefaultOptions (1.00s)
|
||||
|
||||
=== RUN TestDiscoverWithOptions_NilOptions
|
||||
--- PASS: TestDiscoverWithOptions_NilOptions (0.50s)
|
||||
|
||||
=== RUN TestDiscoverWithOptions_LoopbackInterface
|
||||
--- PASS: TestDiscoverWithOptions_LoopbackInterface (0.50s)
|
||||
|
||||
=== RUN TestDiscoverWithOptions_InvalidInterface
|
||||
discovery_test.go:407: Got expected error: failed to resolve network interface:...
|
||||
--- PASS: TestDiscoverWithOptions_InvalidInterface (0.00s)
|
||||
|
||||
=== RUN TestDiscover_BackwardCompatibility
|
||||
discovery_test.go:424: Backward compat: found 0 devices
|
||||
--- PASS: TestDiscover_BackwardCompatibility (0.50s)
|
||||
|
||||
PASS
|
||||
ok github.com/0x524a/onvif-go/discovery 3.009s
|
||||
```
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
### Scenario 1: Multiple Network Adapters
|
||||
```go
|
||||
// List all to find the right one
|
||||
interfaces, _ := discovery.ListNetworkInterfaces()
|
||||
for _, iface := range interfaces {
|
||||
opts := &discovery.DiscoverOptions{NetworkInterface: iface.Name}
|
||||
devices, _ := discovery.DiscoverWithOptions(ctx, 2*time.Second, opts)
|
||||
if len(devices) > 0 {
|
||||
fmt.Printf("Found %d devices on %s\n", len(devices), iface.Name)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Scenario 2: Docker Container with Multiple Networks
|
||||
```go
|
||||
// Use specific bridge network IP
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: "172.20.0.10", // Custom bridge network
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
### Scenario 3: CLI Tool with User Selection
|
||||
```go
|
||||
// Command: ./app -interface eth0
|
||||
interfaces, _ := discovery.ListNetworkInterfaces()
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: userInputFlag,
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
## Benefits
|
||||
|
||||
✅ **Solves Real Problem**: Users with multiple interfaces can now find cameras reliably
|
||||
✅ **Backward Compatible**: Existing code continues to work unchanged
|
||||
✅ **Flexible**: Supports interface names and IP addresses
|
||||
✅ **User-Friendly**: Helpful error messages with available options
|
||||
✅ **Well-Documented**: Comprehensive guide with examples
|
||||
✅ **Well-Tested**: 6 unit tests + 2 benchmarks + backward compatibility test
|
||||
✅ **Production-Ready**: No external dependencies, uses standard library only
|
||||
|
||||
## Documentation
|
||||
|
||||
- **Detailed Guide**: `discovery/NETWORK_INTERFACE_GUIDE.md` (400+ lines with examples)
|
||||
- **Quick Start**: `QUICKSTART.md` - Updated with network interface examples
|
||||
- **API Docs**: Inline code comments with examples
|
||||
- **Tests**: `discovery/discovery_test.go` - Serve as additional usage examples
|
||||
|
||||
## Commits
|
||||
|
||||
1. **c384dca**: `feat: add network interface selection to WS-Discovery`
|
||||
- Core implementation of all new functions
|
||||
- Comprehensive test suite
|
||||
- NETWORK_INTERFACE_GUIDE.md created
|
||||
|
||||
2. **d6e5cbd**: `docs: add network interface discovery section to QUICKSTART`
|
||||
- Updated QUICKSTART.md with examples
|
||||
- Cross-references to detailed guide
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Possible future improvements:
|
||||
- Support for interface filtering (up/down, multicast capability)
|
||||
- Async discovery across multiple interfaces
|
||||
- Caching of interface list
|
||||
- Event-based interface change detection
|
||||
- IPv6-only discovery option
|
||||
- Custom multicast group selection
|
||||
|
||||
## Related Issues & PRs
|
||||
|
||||
- Addresses user request: "For the discovery, lets add an option that the user should be able to define the Network Interface on which we can send the Multicast messages"
|
||||
- Part of PR #30: Network Interface Selection for Discovery
|
||||
- Built on top of PR #29: Complete branding consistency
|
||||
|
||||
## Verification Checklist
|
||||
|
||||
✅ Implementation complete
|
||||
✅ All tests passing (3.009s)
|
||||
✅ Backward compatibility verified
|
||||
✅ No unused variables or imports
|
||||
✅ Error handling comprehensive
|
||||
✅ Documentation complete (400+ lines)
|
||||
✅ Examples provided for all features
|
||||
✅ Changes committed and pushed
|
||||
✅ Code follows Go standards
|
||||
✅ No external dependencies added
|
||||
|
||||
## Summary
|
||||
|
||||
Successfully implemented network interface selection for ONVIF device discovery. The feature is production-ready, well-documented, fully backward compatible, and comprehensively tested. Users can now reliably discover cameras when multiple network interfaces are active on their systems.
|
||||
Reference in New Issue
Block a user