cleanup
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(wc:*)",
|
||||
"Bash(grep:*)",
|
||||
"Bash(ls:*)",
|
||||
"Bash(find:*)",
|
||||
"Bash(go build:*)",
|
||||
"Bash(go test:*)",
|
||||
"Bash(GOPROXY=direct go build:*)",
|
||||
"Bash(GOPROXY=https://proxy.golang.org,direct go mod download:*)",
|
||||
"Bash(go mod download:*)",
|
||||
"Bash(./bin/onvif-cli discover:*)",
|
||||
"Bash(./bin/onvif-cli:*)",
|
||||
"Bash(./bin/onvif-diagnostics:*)",
|
||||
"Bash(./bin/discover:*)",
|
||||
"Bash(tee:*)",
|
||||
"Bash(nc:*)",
|
||||
"Bash(tree:*)",
|
||||
"Bash(du -sh:*)",
|
||||
"Bash(xargs:*)",
|
||||
"Bash(gofmt:*)",
|
||||
"Bash(make lint:*)",
|
||||
"Bash(go install:*)",
|
||||
"Bash(go vet:*)",
|
||||
"Bash(~/go/bin/govulncheck ./...)",
|
||||
"Bash(go version:*)",
|
||||
"Bash(~/go/bin/staticcheck:*)",
|
||||
"Bash(make build:*)",
|
||||
"Bash(make clean:*)"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(wc:*)",
|
||||
"Bash(grep:*)",
|
||||
"Bash(ls:*)",
|
||||
"Bash(find:*)",
|
||||
"Bash(go build:*)",
|
||||
"Bash(go test:*)",
|
||||
"Bash(GOPROXY=direct go build:*)",
|
||||
"Bash(GOPROXY=https://proxy.golang.org,direct go mod download:*)",
|
||||
"Bash(go mod download:*)",
|
||||
"Bash(./bin/onvif-cli discover:*)",
|
||||
"Bash(./bin/onvif-cli:*)",
|
||||
"Bash(./bin/onvif-diagnostics:*)",
|
||||
"Bash(./bin/discover:*)",
|
||||
"Bash(tee:*)",
|
||||
"Bash(nc:*)",
|
||||
"Bash(tree:*)",
|
||||
"Bash(du -sh:*)",
|
||||
"Bash(xargs:*)"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
codecov:
|
||||
require_ci_to_pass: yes
|
||||
notify:
|
||||
wait_for_ci: yes
|
||||
|
||||
coverage:
|
||||
precision: 2
|
||||
round: down
|
||||
range: "70...100"
|
||||
status:
|
||||
project:
|
||||
default:
|
||||
target: 45%
|
||||
threshold: 1%
|
||||
base: auto
|
||||
patch:
|
||||
default:
|
||||
target: 80%
|
||||
threshold: 5%
|
||||
|
||||
comment:
|
||||
layout: "reach,diff,flags,tree,footer"
|
||||
behavior: default
|
||||
require_changes: no
|
||||
require_base: no
|
||||
require_head: yes
|
||||
|
||||
ignore:
|
||||
- "cmd/**/*"
|
||||
- "examples/**/*"
|
||||
- "server/**/*"
|
||||
- "testing/**/*"
|
||||
- "**/*_test.go"
|
||||
- "*.md"
|
||||
@@ -1,275 +0,0 @@
|
||||
# Contributing to onvif-go
|
||||
|
||||
Thank you for your interest in contributing to onvif-go! 🎉
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
This project adheres to a code of conduct. By participating, you are expected to uphold this code. Please be respectful and considerate in all interactions.
|
||||
|
||||
## How Can I Contribute?
|
||||
|
||||
### Reporting Bugs
|
||||
|
||||
Before creating bug reports, please check existing issues to avoid duplicates. When creating a bug report, include:
|
||||
|
||||
- Clear, descriptive title
|
||||
- Steps to reproduce the issue
|
||||
- Expected vs actual behavior
|
||||
- Code samples
|
||||
- Your environment (Go version, OS, camera model)
|
||||
- Error messages or logs
|
||||
|
||||
### Suggesting Features
|
||||
|
||||
Feature requests are welcome! Please:
|
||||
|
||||
- Use a clear, descriptive title
|
||||
- Provide detailed description of the proposed feature
|
||||
- Explain the use case and benefits
|
||||
- Consider if the feature fits the project scope
|
||||
|
||||
### Camera Compatibility Reports
|
||||
|
||||
Help us maintain compatibility information:
|
||||
|
||||
- Report both working and non-working cameras
|
||||
- Include manufacturer, model, and firmware version
|
||||
- Run `onvif-diagnostics` and share the output
|
||||
- Note any special configuration needed
|
||||
|
||||
### Pull Requests
|
||||
|
||||
#### Before Submitting
|
||||
|
||||
1. Check if there's an existing PR for the same change
|
||||
2. For major changes, open an issue first to discuss
|
||||
3. Ensure your code follows the project style
|
||||
4. Add tests for new functionality
|
||||
5. Update documentation as needed
|
||||
|
||||
#### Submission Process
|
||||
|
||||
1. **Fork** the repository
|
||||
2. **Create** a feature branch from `main`:
|
||||
```bash
|
||||
git checkout -b feature/amazing-feature
|
||||
```
|
||||
|
||||
3. **Make** your changes:
|
||||
- Write clear, descriptive commit messages
|
||||
- Follow Go best practices and idioms
|
||||
- Add comments for complex logic
|
||||
- Include tests
|
||||
|
||||
4. **Test** your changes:
|
||||
```bash
|
||||
make test
|
||||
make lint
|
||||
```
|
||||
|
||||
5. **Commit** using conventional commits:
|
||||
```bash
|
||||
git commit -m "feat: add GetAnalyticsConfigurations support"
|
||||
git commit -m "fix: correct PTZ coordinate calculation"
|
||||
git commit -m "docs: update README with new examples"
|
||||
```
|
||||
|
||||
6. **Push** to your fork:
|
||||
```bash
|
||||
git push origin feature/amazing-feature
|
||||
```
|
||||
|
||||
7. **Open** a Pull Request with:
|
||||
- Clear title and description
|
||||
- Reference related issues
|
||||
- List of changes made
|
||||
- Testing performed
|
||||
|
||||
## Development Setup
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Go 1.21 or later
|
||||
- Make (optional, for Makefile targets)
|
||||
- golangci-lint for linting
|
||||
|
||||
### Clone and Build
|
||||
|
||||
```bash
|
||||
git clone https://github.com/0x524a/onvif-go.git
|
||||
cd onvif-go
|
||||
go build ./...
|
||||
```
|
||||
|
||||
### Running Tests
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
make test
|
||||
|
||||
# Run tests with coverage
|
||||
make test-coverage
|
||||
|
||||
# Run tests with race detection
|
||||
go test -race ./...
|
||||
|
||||
# Run specific package tests
|
||||
go test ./discovery/...
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
```bash
|
||||
make lint
|
||||
```
|
||||
|
||||
## Coding Standards
|
||||
|
||||
### Go Style
|
||||
|
||||
- Follow [Effective Go](https://golang.org/doc/effective_go)
|
||||
- Use `gofmt` for formatting
|
||||
- Keep functions focused and small
|
||||
- Write self-documenting code
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
- Use descriptive variable names
|
||||
- Follow Go naming conventions (camelCase for private, PascalCase for public)
|
||||
- Avoid abbreviations unless widely understood
|
||||
|
||||
### Error Handling
|
||||
|
||||
- Always check errors
|
||||
- Provide context in error messages
|
||||
- Use `fmt.Errorf` with `%w` for error wrapping
|
||||
|
||||
### Documentation
|
||||
|
||||
- Add GoDoc comments for all exported types and functions
|
||||
- Include usage examples for complex features
|
||||
- Update README.md when adding new features
|
||||
|
||||
### Testing
|
||||
|
||||
- Write table-driven tests when applicable
|
||||
- Test both success and failure cases
|
||||
- Mock external dependencies
|
||||
- Aim for >80% coverage for new code
|
||||
|
||||
### Example Test
|
||||
|
||||
```go
|
||||
func TestGetDeviceInformation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(*testing.T) *Client
|
||||
want *DeviceInformation
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
setup: func(t *testing.T) *Client {
|
||||
// Setup mock
|
||||
},
|
||||
want: &DeviceInformation{
|
||||
Manufacturer: "Test",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := tt.setup(t)
|
||||
got, err := client.GetDeviceInformation(context.Background())
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("got %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Commit Message Guidelines
|
||||
|
||||
We use [Conventional Commits](https://www.conventionalcommits.org/):
|
||||
|
||||
- `feat:` - New feature
|
||||
- `fix:` - Bug fix
|
||||
- `docs:` - Documentation changes
|
||||
- `test:` - Test additions or modifications
|
||||
- `refactor:` - Code refactoring
|
||||
- `perf:` - Performance improvements
|
||||
- `chore:` - Maintenance tasks
|
||||
|
||||
Examples:
|
||||
```
|
||||
feat: add support for Event service
|
||||
fix: correct PTZ velocity calculation in ContinuousMove
|
||||
docs: add examples for imaging settings
|
||||
test: add integration tests for Hikvision cameras
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── client.go # Main ONVIF client
|
||||
├── types.go # ONVIF type definitions
|
||||
├── device.go # Device service
|
||||
├── media.go # Media service
|
||||
├── ptz.go # PTZ service
|
||||
├── imaging.go # Imaging service
|
||||
├── soap/ # SOAP client
|
||||
├── discovery/ # WS-Discovery
|
||||
├── server/ # ONVIF server implementation
|
||||
├── testing/ # Test utilities
|
||||
├── testdata/ # Test fixtures
|
||||
├── cmd/ # Command-line tools
|
||||
└── examples/ # Usage examples
|
||||
```
|
||||
|
||||
## Adding New Features
|
||||
|
||||
### Client Features
|
||||
|
||||
1. Add method to appropriate service file (device.go, media.go, etc.)
|
||||
2. Define request/response types in types.go
|
||||
3. Add tests
|
||||
4. Update documentation
|
||||
5. Add example if useful
|
||||
|
||||
### Server Features
|
||||
|
||||
1. Add handler to server service file
|
||||
2. Define request/response types
|
||||
3. Register handler in server.go
|
||||
4. Add tests
|
||||
5. Update server documentation
|
||||
|
||||
## Review Process
|
||||
|
||||
1. Automated checks run on all PRs (tests, linting)
|
||||
2. Maintainers review code and provide feedback
|
||||
3. Address review comments
|
||||
4. Once approved, PR will be merged
|
||||
|
||||
## Getting Help
|
||||
|
||||
- 💬 [GitHub Discussions](https://github.com/0x524a/onvif-go/discussions) - Ask questions
|
||||
- 🐛 [GitHub Issues](https://github.com/0x524a/onvif-go/issues) - Report bugs
|
||||
- 📖 [Documentation](https://pkg.go.dev/github.com/0x524a/onvif-go) - Read the docs
|
||||
|
||||
## License
|
||||
|
||||
By contributing, you agree that your contributions will be licensed under the MIT License.
|
||||
|
||||
---
|
||||
|
||||
Thank you for contributing to onvif-go! Your efforts help make ONVIF integration better for everyone. 🚀
|
||||
@@ -1,102 +0,0 @@
|
||||
name: 🐛 Bug Report
|
||||
description: Report a bug or unexpected behavior
|
||||
title: "[BUG] "
|
||||
labels: ["bug"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thanks for taking the time to report this bug! Please fill out the information below.
|
||||
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: A clear and concise description of what the bug is
|
||||
placeholder: Describe the bug...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: reproduce
|
||||
attributes:
|
||||
label: Steps to Reproduce
|
||||
description: Steps to reproduce the behavior
|
||||
placeholder: |
|
||||
1. Connect to camera at...
|
||||
2. Call method...
|
||||
3. See error...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: Expected Behavior
|
||||
description: What you expected to happen
|
||||
placeholder: I expected...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: code
|
||||
attributes:
|
||||
label: Code Sample
|
||||
description: Minimal code sample to reproduce the issue
|
||||
render: go
|
||||
placeholder: |
|
||||
package main
|
||||
|
||||
import "github.com/0x524a/onvif-go"
|
||||
|
||||
func main() {
|
||||
// Your code here
|
||||
}
|
||||
|
||||
- type: input
|
||||
id: go-version
|
||||
attributes:
|
||||
label: Go Version
|
||||
description: Output of `go version`
|
||||
placeholder: go version go1.21.0 linux/amd64
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: lib-version
|
||||
attributes:
|
||||
label: Library Version
|
||||
description: Git commit hash or release version
|
||||
placeholder: v1.0.0 or commit abc123
|
||||
|
||||
- type: input
|
||||
id: camera
|
||||
attributes:
|
||||
label: Camera Model/Brand
|
||||
description: If applicable
|
||||
placeholder: Hikvision DS-2CD2xx, Axis M1065-L, etc.
|
||||
|
||||
- type: dropdown
|
||||
id: os
|
||||
attributes:
|
||||
label: Operating System
|
||||
options:
|
||||
- Linux
|
||||
- macOS
|
||||
- Windows
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: logs
|
||||
attributes:
|
||||
label: Error Output/Logs
|
||||
description: Paste any error messages or logs
|
||||
render: shell
|
||||
|
||||
- type: textarea
|
||||
id: context
|
||||
attributes:
|
||||
label: Additional Context
|
||||
description: Any other context about the problem
|
||||
@@ -1,86 +0,0 @@
|
||||
name: 📷 Camera Compatibility Report
|
||||
description: Report compatibility with a specific camera model
|
||||
title: "[CAMERA] "
|
||||
labels: ["camera-compatibility"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Help us track camera compatibility! Share your experience with a specific camera model.
|
||||
|
||||
- type: input
|
||||
id: manufacturer
|
||||
attributes:
|
||||
label: Camera Manufacturer
|
||||
placeholder: Hikvision, Axis, Dahua, Bosch, etc.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: model
|
||||
attributes:
|
||||
label: Camera Model
|
||||
placeholder: DS-2CD2xx, M1065-L, IPC-HDW2xxx, etc.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: firmware
|
||||
attributes:
|
||||
label: Firmware Version
|
||||
placeholder: V5.7.3 build 220727
|
||||
|
||||
- type: dropdown
|
||||
id: status
|
||||
attributes:
|
||||
label: Compatibility Status
|
||||
options:
|
||||
- ✅ Fully Working
|
||||
- ⚠️ Partially Working
|
||||
- ❌ Not Working
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: checkboxes
|
||||
id: features
|
||||
attributes:
|
||||
label: Working Features
|
||||
description: Which features work with this camera?
|
||||
options:
|
||||
- label: Device Information
|
||||
- label: Media Profiles
|
||||
- label: Stream URIs (RTSP)
|
||||
- label: Snapshots
|
||||
- label: PTZ Control
|
||||
- label: Imaging Settings
|
||||
- label: Discovery
|
||||
|
||||
- type: textarea
|
||||
id: issues
|
||||
attributes:
|
||||
label: Known Issues
|
||||
description: Describe any issues or limitations
|
||||
placeholder: PTZ presets don't work, imaging settings return error, etc.
|
||||
|
||||
- type: textarea
|
||||
id: notes
|
||||
attributes:
|
||||
label: Additional Notes
|
||||
description: Any special configuration or workarounds needed
|
||||
placeholder: Requires authentication, needs specific settings, etc.
|
||||
|
||||
- type: checkboxes
|
||||
id: test-results
|
||||
attributes:
|
||||
label: Test Results
|
||||
description: Have you run the diagnostic tool?
|
||||
options:
|
||||
- label: I have run onvif-diagnostics and can attach the output
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: diagnostic-output
|
||||
attributes:
|
||||
label: Diagnostic Output
|
||||
description: Paste the output from onvif-diagnostics if available
|
||||
render: json
|
||||
@@ -1,11 +0,0 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: 💬 Discussions
|
||||
url: https://github.com/0x524a/onvif-go/discussions
|
||||
about: Ask questions and discuss ideas with the community
|
||||
- name: 📖 Documentation
|
||||
url: https://pkg.go.dev/github.com/0x524a/onvif-go
|
||||
about: Read the API documentation
|
||||
- name: 📚 Examples
|
||||
url: https://github.com/0x524a/onvif-go/tree/main/examples
|
||||
about: Browse code examples
|
||||
@@ -1,75 +0,0 @@
|
||||
name: ✨ Feature Request
|
||||
description: Suggest a new feature or enhancement
|
||||
title: "[FEATURE] "
|
||||
labels: ["enhancement"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for suggesting a new feature! Please provide details below.
|
||||
|
||||
- type: textarea
|
||||
id: problem
|
||||
attributes:
|
||||
label: Problem Statement
|
||||
description: Is your feature request related to a problem? Please describe.
|
||||
placeholder: I'm always frustrated when...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: solution
|
||||
attributes:
|
||||
label: Proposed Solution
|
||||
description: Describe the solution you'd like
|
||||
placeholder: I would like to see...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: alternatives
|
||||
attributes:
|
||||
label: Alternatives Considered
|
||||
description: Describe any alternative solutions or features you've considered
|
||||
placeholder: I also considered...
|
||||
|
||||
- type: dropdown
|
||||
id: category
|
||||
attributes:
|
||||
label: Feature Category
|
||||
description: Which area does this feature relate to?
|
||||
options:
|
||||
- Client - Device Service
|
||||
- Client - Media Service
|
||||
- Client - PTZ Service
|
||||
- Client - Imaging Service
|
||||
- Client - Discovery
|
||||
- Server Implementation
|
||||
- Documentation
|
||||
- Testing/Examples
|
||||
- Performance
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: use-case
|
||||
attributes:
|
||||
label: Use Case
|
||||
description: Describe your use case for this feature
|
||||
placeholder: This would help with...
|
||||
|
||||
- type: checkboxes
|
||||
id: contribution
|
||||
attributes:
|
||||
label: Contribution
|
||||
description: Would you be willing to contribute this feature?
|
||||
options:
|
||||
- label: I would be willing to submit a PR for this feature
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: context
|
||||
attributes:
|
||||
label: Additional Context
|
||||
description: Add any other context, screenshots, or examples
|
||||
@@ -1,79 +0,0 @@
|
||||
## Description
|
||||
<!-- Provide a clear and concise description of your changes -->
|
||||
|
||||
## Type of Change
|
||||
<!-- Mark the relevant option with an "x" -->
|
||||
|
||||
- [ ] 🐛 Bug fix (non-breaking change which fixes an issue)
|
||||
- [ ] ✨ New feature (non-breaking change which adds functionality)
|
||||
- [ ] 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||
- [ ] 📝 Documentation update
|
||||
- [ ] 🧪 Test improvements
|
||||
- [ ] ♻️ Code refactoring
|
||||
- [ ] ⚡ Performance improvement
|
||||
|
||||
## Related Issues
|
||||
<!-- Link to related issues using #issue_number -->
|
||||
|
||||
Fixes #
|
||||
Relates to #
|
||||
|
||||
## Changes Made
|
||||
<!-- List the main changes in this PR -->
|
||||
|
||||
-
|
||||
-
|
||||
-
|
||||
|
||||
## Testing Performed
|
||||
<!-- Describe the tests you ran to verify your changes -->
|
||||
|
||||
- [ ] Unit tests pass locally
|
||||
- [ ] Added new tests for new functionality
|
||||
- [ ] Tested with real ONVIF camera(s)
|
||||
- [ ] Ran `make lint` with no errors
|
||||
- [ ] Ran `make test` with all tests passing
|
||||
|
||||
### Camera Testing (if applicable)
|
||||
<!-- If you tested with physical cameras, provide details -->
|
||||
|
||||
- **Camera Model**:
|
||||
- **Firmware Version**:
|
||||
- **Test Results**:
|
||||
|
||||
## Documentation
|
||||
<!-- Mark what documentation was updated -->
|
||||
|
||||
- [ ] Code comments added/updated
|
||||
- [ ] README.md updated
|
||||
- [ ] Examples added/updated
|
||||
- [ ] API documentation (GoDoc) updated
|
||||
- [ ] CHANGELOG.md updated
|
||||
|
||||
## Checklist
|
||||
<!-- Ensure all items are complete before submitting -->
|
||||
|
||||
- [ ] My code follows the project's style guidelines
|
||||
- [ ] I have performed a self-review of my code
|
||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
||||
- [ ] I have made corresponding changes to the documentation
|
||||
- [ ] My changes generate no new warnings
|
||||
- [ ] I have added tests that prove my fix is effective or that my feature works
|
||||
- [ ] New and existing unit tests pass locally with my changes
|
||||
- [ ] Any dependent changes have been merged and published
|
||||
|
||||
## Breaking Changes
|
||||
<!-- If this introduces breaking changes, describe them and migration path -->
|
||||
|
||||
## Screenshots/Examples
|
||||
<!-- If applicable, add screenshots or example code -->
|
||||
|
||||
```go
|
||||
// Example usage
|
||||
```
|
||||
|
||||
## Additional Context
|
||||
<!-- Add any other context about the PR here -->
|
||||
|
||||
## Reviewer Notes
|
||||
<!-- Any specific areas you'd like reviewers to focus on -->
|
||||
@@ -1,180 +0,0 @@
|
||||
# GitHub Actions Workflows
|
||||
|
||||
This directory contains all CI/CD workflows for the ONVIF Go library.
|
||||
|
||||
## Workflows
|
||||
|
||||
### 🔄 CI (`ci.yml`) - Main Pipeline
|
||||
**Unified continuous integration workflow with fail-fast behavior.**
|
||||
|
||||
The CI pipeline runs sequentially - if any stage fails, subsequent stages are skipped:
|
||||
|
||||
```
|
||||
fmt → lint → test → sonarcloud
|
||||
↘ build
|
||||
```
|
||||
|
||||
**Stages:**
|
||||
|
||||
| Stage | Description | Depends On |
|
||||
|-------|-------------|------------|
|
||||
| **fmt** | Format check using `gofmt -s` | - |
|
||||
| **lint** | Static analysis with `go vet` and `golangci-lint` | fmt |
|
||||
| **test** | Unit tests with race detector + coverage | lint |
|
||||
| **sonarcloud** | Code quality & security analysis (push to master only) | test |
|
||||
| **build** | Build verification for all packages | test |
|
||||
| **ci-success** | Final status check | all |
|
||||
|
||||
**Features:**
|
||||
- ✅ Fail-fast: stops immediately if any check fails
|
||||
- ✅ Codecov integration for coverage reporting
|
||||
- ✅ SonarCloud integration for code quality
|
||||
- ✅ Go module caching for faster builds
|
||||
- ✅ Concurrency control (cancels in-progress runs)
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`, `main`
|
||||
- All pull requests targeting `master`, `main`
|
||||
|
||||
**Required for PR Merge:**
|
||||
All stages must pass before a PR can be merged. Configure branch protection rules in GitHub:
|
||||
1. Go to **Settings → Branches → Branch protection rules**
|
||||
2. Add rule for `master`
|
||||
3. Enable **Require status checks to pass before merging**
|
||||
4. Select these required checks:
|
||||
- `Format Check`
|
||||
- `Lint`
|
||||
- `Test & Coverage`
|
||||
- `SonarCloud Analysis`
|
||||
- `Build Verification`
|
||||
- `CI Success`
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Extended Tests (`test.yml`)
|
||||
Extended testing workflow for comprehensive test coverage.
|
||||
|
||||
**Jobs:**
|
||||
- **test-older-versions** - Test on older Go versions (1.19, 1.20)
|
||||
- **benchmark** - Run benchmark tests
|
||||
- **race-detector** - Extended race detector tests
|
||||
|
||||
**Triggers:**
|
||||
- Manual dispatch
|
||||
- Weekly schedule (Sunday 2 AM UTC)
|
||||
- Push to `master`/`main` when Go files change
|
||||
|
||||
---
|
||||
|
||||
### 🚀 Release (`release.yml`)
|
||||
Automated release workflow for creating GitHub releases.
|
||||
|
||||
**Jobs:**
|
||||
- **build** - Build binaries for all platforms (Linux, Windows, macOS, multiple architectures)
|
||||
- **release** - Create GitHub release with artifacts
|
||||
- **docker** - Build and push Docker images to GHCR
|
||||
|
||||
**Triggers:**
|
||||
- Push tags matching `v*.*.*`
|
||||
- Manual dispatch with version input
|
||||
|
||||
---
|
||||
|
||||
### 🔒 Security (`security.yml`)
|
||||
Security scanning workflow.
|
||||
|
||||
**Jobs:**
|
||||
- **gosec** - Security scanner
|
||||
- **govulncheck** - Vulnerability checker
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`/`main`
|
||||
- Pull requests
|
||||
- Weekly schedule
|
||||
|
||||
---
|
||||
|
||||
### 📚 Documentation (`docs.yml`)
|
||||
Documentation validation workflow.
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`/`main` when docs change
|
||||
- Manual dispatch
|
||||
|
||||
---
|
||||
|
||||
### 🔐 Dependency Review (`dependency-review.yml`)
|
||||
Dependency vulnerability review.
|
||||
|
||||
**Triggers:**
|
||||
- Pull requests
|
||||
|
||||
---
|
||||
|
||||
## CI Pipeline Flow
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ CI PIPELINE │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────────────────────┐ │
|
||||
│ │ FMT │────▶│ LINT │────▶│ TEST + COVERAGE │ │
|
||||
│ └─────────┘ └─────────┘ └───────────┬─────────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────┴─────────┐ │
|
||||
│ ▼ ▼ │
|
||||
│ ┌────────────┐ ┌───────────┐ │
|
||||
│ │ SONARCLOUD │ │ BUILD │ │
|
||||
│ │ (push only)│ └───────────┘ │
|
||||
│ └────────────┘ │ │
|
||||
│ │ │ │
|
||||
│ └─────────┬─────────┘ │
|
||||
│ ▼ │
|
||||
│ ┌─────────────────┐ │
|
||||
│ │ CI SUCCESS │ │
|
||||
│ └─────────────────┘ │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
|
||||
❌ If any stage fails, the pipeline stops immediately (fail-fast)
|
||||
ℹ️ SonarCloud only runs on push to master/main (skipped for PRs)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## SonarCloud Configuration
|
||||
|
||||
Security Hotspot analysis excludes:
|
||||
- Test files (`**/*_test.go`)
|
||||
- CI configuration (`**/.github/**`)
|
||||
- Test utilities (`**/testing/**`, `**/testdata/**`)
|
||||
- Example code (`**/examples/**`)
|
||||
- CLI tools (`**/cmd/**`)
|
||||
|
||||
This ensures security analysis focuses on production library code.
|
||||
|
||||
---
|
||||
|
||||
## Required Secrets
|
||||
|
||||
| Secret | Required | Description |
|
||||
|--------|----------|-------------|
|
||||
| `CODECOV_TOKEN` | Yes | Coverage reporting to Codecov |
|
||||
| `SONAR_TOKEN` | Yes | SonarCloud code analysis |
|
||||
| `DOCKERHUB_USERNAME` | No | Docker Hub releases |
|
||||
| `DOCKERHUB_TOKEN` | No | Docker Hub releases |
|
||||
|
||||
---
|
||||
|
||||
## Workflow Status
|
||||
|
||||
- ✅ Go 1.24 as primary version
|
||||
- ✅ Unified fail-fast CI pipeline
|
||||
- ✅ Go module caching for faster builds
|
||||
- ✅ Artifact uploads for coverage and releases
|
||||
- ✅ Concurrency control
|
||||
|
||||
---
|
||||
|
||||
*Last Updated: December 3, 2025*
|
||||
@@ -1,255 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, main]
|
||||
pull_request:
|
||||
branches: [master, main]
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
checks: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GO_VERSION: '1.24.x'
|
||||
|
||||
jobs:
|
||||
# Stage 1: Format Check (fastest - fail immediately if code isn't formatted)
|
||||
fmt:
|
||||
name: Format Check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Check formatting
|
||||
run: |
|
||||
unformatted=$(gofmt -s -l . | grep -v vendor || true)
|
||||
if [ -n "$unformatted" ]; then
|
||||
echo "❌ The following files are not properly formatted:"
|
||||
echo "$unformatted"
|
||||
echo ""
|
||||
echo "Run 'gofmt -s -w .' to fix formatting issues"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ All files are properly formatted"
|
||||
|
||||
# Stage 2: Lint (depends on fmt)
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
needs: fmt
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run go vet
|
||||
run: go vet ./...
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v6.5.0
|
||||
with:
|
||||
version: v2.1.6
|
||||
args: --timeout=5m
|
||||
|
||||
# Stage 3: Test with Coverage (depends on lint)
|
||||
test:
|
||||
name: Test & Coverage
|
||||
runs-on: ubuntu-latest
|
||||
needs: lint
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0 # Full history for SonarCloud
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests with coverage
|
||||
run: |
|
||||
go test -v -race -covermode=atomic -coverprofile=coverage.out -json ./... > test-report.json 2>&1 || true
|
||||
# Ensure coverage file exists even if tests fail
|
||||
if [ ! -f coverage.out ]; then
|
||||
echo "mode: atomic" > coverage.out
|
||||
fi
|
||||
|
||||
- name: Display coverage summary
|
||||
run: |
|
||||
echo "📊 Coverage Summary:"
|
||||
go tool cover -func=coverage.out | tail -20
|
||||
|
||||
- name: Upload coverage artifact
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: coverage-reports
|
||||
path: |
|
||||
coverage.out
|
||||
test-report.json
|
||||
retention-days: 7
|
||||
|
||||
- name: Upload to Codecov
|
||||
uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v4.6.0
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
files: ./coverage.out
|
||||
flags: unittests
|
||||
name: codecov-onvif-go
|
||||
# Don't fail on PRs from forks where token may not be available
|
||||
fail_ci_if_error: ${{ github.event_name == 'push' }}
|
||||
verbose: true
|
||||
|
||||
# Stage 4: SonarCloud Analysis (depends on test)
|
||||
# Only runs on push to master/main when SONAR_TOKEN is available
|
||||
# Skipped for PRs from forks where secrets are not accessible
|
||||
sonarcloud:
|
||||
name: SonarCloud Analysis
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && github.repository == '0x524a/onvif-go'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0 # Full history for accurate blame information
|
||||
|
||||
- name: Download coverage reports
|
||||
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
||||
with:
|
||||
name: coverage-reports
|
||||
|
||||
- name: Verify coverage file
|
||||
run: |
|
||||
echo "📁 Downloaded files:"
|
||||
ls -la
|
||||
if [ -f coverage.out ]; then
|
||||
echo "✅ Coverage file found"
|
||||
head -5 coverage.out
|
||||
else
|
||||
echo "⚠️ Coverage file not found, creating empty one"
|
||||
echo "mode: atomic" > coverage.out
|
||||
fi
|
||||
|
||||
- name: SonarCloud Scan
|
||||
uses: SonarSource/sonarcloud-github-action@4006f663ecaf1f8093e8e4abb9227f6041f52216 # v3.1.0
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
|
||||
# Stage 5: Build Verification (depends on test, runs in parallel with sonarcloud)
|
||||
build:
|
||||
name: Build Verification
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Build library
|
||||
run: go build -v ./...
|
||||
|
||||
- name: Build CLI tools
|
||||
run: |
|
||||
echo "🔨 Building CLI tools..."
|
||||
go build -v -o bin/onvif-cli ./cmd/onvif-cli
|
||||
go build -v -o bin/onvif-quick ./cmd/onvif-quick
|
||||
go build -v -o bin/onvif-server ./cmd/onvif-server
|
||||
go build -v -o bin/onvif-diagnostics ./cmd/onvif-diagnostics
|
||||
echo "✅ All CLI tools built successfully"
|
||||
|
||||
# Final status check
|
||||
ci-success:
|
||||
name: CI Success
|
||||
runs-on: ubuntu-latest
|
||||
needs: [fmt, lint, test, sonarcloud, build]
|
||||
if: always()
|
||||
steps:
|
||||
- name: Check all jobs status
|
||||
run: |
|
||||
if [[ "${{ needs.fmt.result }}" != "success" ]]; then
|
||||
echo "❌ Format check failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.lint.result }}" != "success" ]]; then
|
||||
echo "❌ Lint check failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.test.result }}" != "success" ]]; then
|
||||
echo "❌ Tests failed"
|
||||
exit 1
|
||||
fi
|
||||
# SonarCloud is optional - only fails if it ran and failed (not if skipped)
|
||||
if [[ "${{ needs.sonarcloud.result }}" == "failure" ]]; then
|
||||
echo "❌ SonarCloud analysis failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.sonarcloud.result }}" == "skipped" ]]; then
|
||||
echo "ℹ️ SonarCloud analysis skipped (only runs on push to master/main)"
|
||||
fi
|
||||
if [[ "${{ needs.build.result }}" != "success" ]]; then
|
||||
echo "❌ Build verification failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ All CI checks passed successfully!"
|
||||
@@ -1,22 +0,0 @@
|
||||
name: Dependency Review
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ master, main, develop ]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
dependency-review:
|
||||
name: Review Dependencies
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Dependency Review
|
||||
uses: actions/dependency-review-action@da24556b548a50705dd671f47852072ea4c105d9 # v4.7.1
|
||||
with:
|
||||
fail-on-severity: moderate
|
||||
@@ -1,33 +0,0 @@
|
||||
name: Documentation
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
paths:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
docs-check:
|
||||
name: Documentation Check
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Check for broken links
|
||||
uses: lycheeverse/lychee-action@f81112d0d2814ded911bd23e3beaa9dda9093915 # v2.3.0
|
||||
with:
|
||||
args: --verbose --no-progress docs/ *.md
|
||||
continue-on-error: true
|
||||
|
||||
- name: Validate markdown
|
||||
uses: DavidAnson/markdownlint-cli2-action@05f32210e84442804257b2c8a4c84aa7067b5e06 # v19.0.0
|
||||
with:
|
||||
globs: 'docs/**/*.md'
|
||||
continue-on-error: true
|
||||
@@ -1,286 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'Release version (e.g., v1.2.3)'
|
||||
required: true
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build Release Binaries
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
# Linux
|
||||
- goos: linux
|
||||
goarch: amd64
|
||||
- goos: linux
|
||||
goarch: arm64
|
||||
- goos: linux
|
||||
goarch: arm
|
||||
goarm: 7
|
||||
|
||||
# Windows
|
||||
- goos: windows
|
||||
goarch: amd64
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
|
||||
# macOS
|
||||
- goos: darwin
|
||||
goarch: amd64
|
||||
- goos: darwin
|
||||
goarch: arm64
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Get version
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
echo "Version: ${VERSION}"
|
||||
|
||||
- name: Build binaries
|
||||
env:
|
||||
GOOS: ${{ matrix.goos }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
GOARM: ${{ matrix.goarm }}
|
||||
CGO_ENABLED: 0
|
||||
run: |
|
||||
VERSION=${{ steps.version.outputs.VERSION }}
|
||||
SHORT_SHA=${{ steps.version.outputs.SHORT_SHA }}
|
||||
LDFLAGS="-s -w -X main.Version=${VERSION} -X main.Commit=${SHORT_SHA}"
|
||||
|
||||
# Set file extension for Windows
|
||||
EXT=""
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
EXT=".exe"
|
||||
fi
|
||||
|
||||
# Build all CLI tools
|
||||
mkdir -p dist
|
||||
|
||||
echo "🔨 Building onvif-cli..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-cli
|
||||
|
||||
echo "🔨 Building onvif-quick..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-quick
|
||||
|
||||
echo "🔨 Building onvif-server..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-server
|
||||
|
||||
echo "🔨 Building onvif-diagnostics..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-diagnostics
|
||||
|
||||
- name: Create archive
|
||||
run: |
|
||||
VERSION=${{ steps.version.outputs.VERSION }}
|
||||
PLATFORM="${{ matrix.goos }}-${{ matrix.goarch }}"
|
||||
ARCHIVE_NAME="onvif-go-${VERSION}-${PLATFORM}"
|
||||
|
||||
mkdir -p releases staging
|
||||
|
||||
# Copy binaries with clean names (without platform suffix)
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
cp dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-cli.exe
|
||||
cp dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-quick.exe
|
||||
cp dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-server.exe
|
||||
cp dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-diagnostics.exe
|
||||
else
|
||||
cp dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-cli
|
||||
cp dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-quick
|
||||
cp dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-server
|
||||
cp dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-diagnostics
|
||||
fi
|
||||
|
||||
# Copy documentation
|
||||
cp README.md LICENSE staging/ 2>/dev/null || true
|
||||
|
||||
# Create archive from staging directory
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
cd staging
|
||||
zip -r "../releases/${ARCHIVE_NAME}.zip" .
|
||||
cd ..
|
||||
else
|
||||
cd staging
|
||||
tar czf "../releases/${ARCHIVE_NAME}.tar.gz" .
|
||||
cd ..
|
||||
fi
|
||||
|
||||
echo "✅ Created ${ARCHIVE_NAME}.tar.gz"
|
||||
|
||||
- name: Generate checksums
|
||||
run: |
|
||||
cd releases
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum * > checksums-${{ matrix.goos }}-${{ matrix.goarch }}.txt
|
||||
else
|
||||
shasum -a 256 * > checksums-${{ matrix.goos }}-${{ matrix.goarch }}.txt
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: release-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
path: releases/*
|
||||
retention-days: 7
|
||||
|
||||
release:
|
||||
name: Create GitHub Release
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
||||
with:
|
||||
path: all-releases
|
||||
pattern: release-*
|
||||
merge-multiple: true
|
||||
|
||||
- name: Generate combined checksums
|
||||
run: |
|
||||
cd all-releases
|
||||
# Combine all checksum files
|
||||
cat checksums-*.txt > checksums.txt 2>/dev/null || true
|
||||
# Remove individual checksum files
|
||||
rm -f checksums-*.txt
|
||||
|
||||
- name: Get version and changelog
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
|
||||
# Generate changelog from commits since last tag
|
||||
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
|
||||
if [ -n "$PREV_TAG" ]; then
|
||||
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT
|
||||
git log --pretty=format:"- %s (%h)" ${PREV_TAG}..HEAD >> $GITHUB_OUTPUT
|
||||
echo "" >> $GITHUB_OUTPUT
|
||||
echo "EOF" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "CHANGELOG=Initial release" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Create Release
|
||||
uses: softprops/action-gh-release@7b4da11513bf3f43f9999e90eabced41ab8bb048 # v2.2.2
|
||||
with:
|
||||
files: all-releases/*
|
||||
draft: false
|
||||
prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
|
||||
generate_release_notes: true
|
||||
make_latest: true
|
||||
body: |
|
||||
## Release ${{ steps.version.outputs.VERSION }}
|
||||
|
||||
### 📦 Installation
|
||||
|
||||
Download the appropriate binary for your platform below.
|
||||
|
||||
#### Linux/macOS
|
||||
```bash
|
||||
# Download and extract
|
||||
wget https://github.com/${{ github.repository }}/releases/download/${{ steps.version.outputs.VERSION }}/onvif-go-${{ steps.version.outputs.VERSION }}-linux-amd64.tar.gz
|
||||
tar xzf onvif-go-${{ steps.version.outputs.VERSION }}-linux-amd64.tar.gz
|
||||
|
||||
# Make executable and move to PATH
|
||||
chmod +x onvif-cli
|
||||
sudo mv onvif-cli /usr/local/bin/onvif-cli
|
||||
```
|
||||
|
||||
#### Windows
|
||||
Download the `.zip` file for your architecture and extract it.
|
||||
|
||||
#### Go Library
|
||||
```bash
|
||||
go get github.com/${{ github.repository }}@${{ steps.version.outputs.VERSION }}
|
||||
```
|
||||
|
||||
### 🔐 Checksums
|
||||
|
||||
SHA256 checksums are available in `checksums.txt`
|
||||
|
||||
### 📝 Changes
|
||||
|
||||
${{ steps.version.outputs.CHANGELOG }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
docker:
|
||||
name: Build and Push Docker Image
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
if: (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || github.event_name == 'workflow_dispatch'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515637cff71ef929a # v3.6.0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Get version
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
# Remove 'v' prefix if present
|
||||
VERSION=${VERSION#v}
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/v}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v5.5.0
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||
push: true
|
||||
tags: |
|
||||
ghcr.io/${{ github.repository }}:latest
|
||||
ghcr.io/${{ github.repository }}:${{ steps.version.outputs.VERSION }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
@@ -1,69 +0,0 @@
|
||||
name: Security Scan
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
pull_request:
|
||||
branches: [ master, main ]
|
||||
schedule:
|
||||
- cron: '0 0 * * 0' # Weekly on Sunday
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
jobs:
|
||||
gosec:
|
||||
name: Security Scan (gosec)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Install and run gosec
|
||||
run: |
|
||||
go install github.com/securego/gosec/v2/cmd/gosec@latest
|
||||
gosec -no-fail -fmt json -out gosec-report.json ./... || true
|
||||
|
||||
- name: Upload gosec report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: gosec-report
|
||||
path: gosec-report.json
|
||||
retention-days: 30
|
||||
|
||||
- name: Display gosec results
|
||||
if: always()
|
||||
run: |
|
||||
if [ -f gosec-report.json ]; then
|
||||
echo "📊 Gosec Security Scan Results:"
|
||||
cat gosec-report.json | jq -r '.Stats // empty' || echo "No stats available"
|
||||
echo ""
|
||||
echo "Issues found:"
|
||||
cat gosec-report.json | jq -r '.Issues[]? | "\(.severity | ascii_upcase): \(.rule_id) - \(.details)"' || echo "No issues found"
|
||||
fi
|
||||
|
||||
govulncheck:
|
||||
name: Vulnerability Check (govulncheck)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Run govulncheck
|
||||
run: |
|
||||
go install golang.org/x/vuln/cmd/govulncheck@latest
|
||||
govulncheck ./... || true
|
||||
@@ -1,108 +0,0 @@
|
||||
name: Extended Tests
|
||||
|
||||
on:
|
||||
workflow_dispatch: # Manual trigger
|
||||
schedule:
|
||||
- cron: '0 2 * * 0' # Weekly on Sunday at 2 AM UTC
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
paths:
|
||||
- '**.go'
|
||||
- 'go.mod'
|
||||
- 'go.sum'
|
||||
|
||||
jobs:
|
||||
# Run tests on older Go versions
|
||||
test-older-versions:
|
||||
name: Test on Go ${{ matrix.go-version }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
go-version: ['1.20', '1.19']
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests
|
||||
run: go test -v -race ./...
|
||||
|
||||
# Run benchmarks
|
||||
benchmark:
|
||||
name: Benchmark Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-1.24.x-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-1.24.x-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run benchmarks
|
||||
run: go test -bench=. -benchmem ./... -run=^$ || echo "⚠️ No benchmarks found"
|
||||
|
||||
# Test with race detector
|
||||
race-detector:
|
||||
name: Race Detector Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-1.24.x-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-1.24.x-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests with race detector
|
||||
run: go test -race -timeout=10m ./...
|
||||
Vendored
-275
@@ -1,275 +0,0 @@
|
||||
# Contributing to onvif-go
|
||||
|
||||
Thank you for your interest in contributing to onvif-go! 🎉
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
This project adheres to a code of conduct. By participating, you are expected to uphold this code. Please be respectful and considerate in all interactions.
|
||||
|
||||
## How Can I Contribute?
|
||||
|
||||
### Reporting Bugs
|
||||
|
||||
Before creating bug reports, please check existing issues to avoid duplicates. When creating a bug report, include:
|
||||
|
||||
- Clear, descriptive title
|
||||
- Steps to reproduce the issue
|
||||
- Expected vs actual behavior
|
||||
- Code samples
|
||||
- Your environment (Go version, OS, camera model)
|
||||
- Error messages or logs
|
||||
|
||||
### Suggesting Features
|
||||
|
||||
Feature requests are welcome! Please:
|
||||
|
||||
- Use a clear, descriptive title
|
||||
- Provide detailed description of the proposed feature
|
||||
- Explain the use case and benefits
|
||||
- Consider if the feature fits the project scope
|
||||
|
||||
### Camera Compatibility Reports
|
||||
|
||||
Help us maintain compatibility information:
|
||||
|
||||
- Report both working and non-working cameras
|
||||
- Include manufacturer, model, and firmware version
|
||||
- Run `onvif-diagnostics` and share the output
|
||||
- Note any special configuration needed
|
||||
|
||||
### Pull Requests
|
||||
|
||||
#### Before Submitting
|
||||
|
||||
1. Check if there's an existing PR for the same change
|
||||
2. For major changes, open an issue first to discuss
|
||||
3. Ensure your code follows the project style
|
||||
4. Add tests for new functionality
|
||||
5. Update documentation as needed
|
||||
|
||||
#### Submission Process
|
||||
|
||||
1. **Fork** the repository
|
||||
2. **Create** a feature branch from `main`:
|
||||
```bash
|
||||
git checkout -b feature/amazing-feature
|
||||
```
|
||||
|
||||
3. **Make** your changes:
|
||||
- Write clear, descriptive commit messages
|
||||
- Follow Go best practices and idioms
|
||||
- Add comments for complex logic
|
||||
- Include tests
|
||||
|
||||
4. **Test** your changes:
|
||||
```bash
|
||||
make test
|
||||
make lint
|
||||
```
|
||||
|
||||
5. **Commit** using conventional commits:
|
||||
```bash
|
||||
git commit -m "feat: add GetAnalyticsConfigurations support"
|
||||
git commit -m "fix: correct PTZ coordinate calculation"
|
||||
git commit -m "docs: update README with new examples"
|
||||
```
|
||||
|
||||
6. **Push** to your fork:
|
||||
```bash
|
||||
git push origin feature/amazing-feature
|
||||
```
|
||||
|
||||
7. **Open** a Pull Request with:
|
||||
- Clear title and description
|
||||
- Reference related issues
|
||||
- List of changes made
|
||||
- Testing performed
|
||||
|
||||
## Development Setup
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Go 1.21 or later
|
||||
- Make (optional, for Makefile targets)
|
||||
- golangci-lint for linting
|
||||
|
||||
### Clone and Build
|
||||
|
||||
```bash
|
||||
git clone https://github.com/0x524a/onvif-go.git
|
||||
cd onvif-go
|
||||
go build ./...
|
||||
```
|
||||
|
||||
### Running Tests
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
make test
|
||||
|
||||
# Run tests with coverage
|
||||
make test-coverage
|
||||
|
||||
# Run tests with race detection
|
||||
go test -race ./...
|
||||
|
||||
# Run specific package tests
|
||||
go test ./discovery/...
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
```bash
|
||||
make lint
|
||||
```
|
||||
|
||||
## Coding Standards
|
||||
|
||||
### Go Style
|
||||
|
||||
- Follow [Effective Go](https://golang.org/doc/effective_go)
|
||||
- Use `gofmt` for formatting
|
||||
- Keep functions focused and small
|
||||
- Write self-documenting code
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
- Use descriptive variable names
|
||||
- Follow Go naming conventions (camelCase for private, PascalCase for public)
|
||||
- Avoid abbreviations unless widely understood
|
||||
|
||||
### Error Handling
|
||||
|
||||
- Always check errors
|
||||
- Provide context in error messages
|
||||
- Use `fmt.Errorf` with `%w` for error wrapping
|
||||
|
||||
### Documentation
|
||||
|
||||
- Add GoDoc comments for all exported types and functions
|
||||
- Include usage examples for complex features
|
||||
- Update README.md when adding new features
|
||||
|
||||
### Testing
|
||||
|
||||
- Write table-driven tests when applicable
|
||||
- Test both success and failure cases
|
||||
- Mock external dependencies
|
||||
- Aim for >80% coverage for new code
|
||||
|
||||
### Example Test
|
||||
|
||||
```go
|
||||
func TestGetDeviceInformation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(*testing.T) *Client
|
||||
want *DeviceInformation
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
setup: func(t *testing.T) *Client {
|
||||
// Setup mock
|
||||
},
|
||||
want: &DeviceInformation{
|
||||
Manufacturer: "Test",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := tt.setup(t)
|
||||
got, err := client.GetDeviceInformation(context.Background())
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("got %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Commit Message Guidelines
|
||||
|
||||
We use [Conventional Commits](https://www.conventionalcommits.org/):
|
||||
|
||||
- `feat:` - New feature
|
||||
- `fix:` - Bug fix
|
||||
- `docs:` - Documentation changes
|
||||
- `test:` - Test additions or modifications
|
||||
- `refactor:` - Code refactoring
|
||||
- `perf:` - Performance improvements
|
||||
- `chore:` - Maintenance tasks
|
||||
|
||||
Examples:
|
||||
```
|
||||
feat: add support for Event service
|
||||
fix: correct PTZ velocity calculation in ContinuousMove
|
||||
docs: add examples for imaging settings
|
||||
test: add integration tests for Hikvision cameras
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── client.go # Main ONVIF client
|
||||
├── types.go # ONVIF type definitions
|
||||
├── device.go # Device service
|
||||
├── media.go # Media service
|
||||
├── ptz.go # PTZ service
|
||||
├── imaging.go # Imaging service
|
||||
├── soap/ # SOAP client
|
||||
├── discovery/ # WS-Discovery
|
||||
├── server/ # ONVIF server implementation
|
||||
├── testing/ # Test utilities
|
||||
├── testdata/ # Test fixtures
|
||||
├── cmd/ # Command-line tools
|
||||
└── examples/ # Usage examples
|
||||
```
|
||||
|
||||
## Adding New Features
|
||||
|
||||
### Client Features
|
||||
|
||||
1. Add method to appropriate service file (device.go, media.go, etc.)
|
||||
2. Define request/response types in types.go
|
||||
3. Add tests
|
||||
4. Update documentation
|
||||
5. Add example if useful
|
||||
|
||||
### Server Features
|
||||
|
||||
1. Add handler to server service file
|
||||
2. Define request/response types
|
||||
3. Register handler in server.go
|
||||
4. Add tests
|
||||
5. Update server documentation
|
||||
|
||||
## Review Process
|
||||
|
||||
1. Automated checks run on all PRs (tests, linting)
|
||||
2. Maintainers review code and provide feedback
|
||||
3. Address review comments
|
||||
4. Once approved, PR will be merged
|
||||
|
||||
## Getting Help
|
||||
|
||||
- 💬 [GitHub Discussions](https://github.com/0x524a/onvif-go/discussions) - Ask questions
|
||||
- 🐛 [GitHub Issues](https://github.com/0x524a/onvif-go/issues) - Report bugs
|
||||
- 📖 [Documentation](https://pkg.go.dev/github.com/0x524a/onvif-go) - Read the docs
|
||||
|
||||
## License
|
||||
|
||||
By contributing, you agree that your contributions will be licensed under the MIT License.
|
||||
|
||||
---
|
||||
|
||||
Thank you for contributing to onvif-go! Your efforts help make ONVIF integration better for everyone. 🚀
|
||||
-102
@@ -1,102 +0,0 @@
|
||||
name: 🐛 Bug Report
|
||||
description: Report a bug or unexpected behavior
|
||||
title: "[BUG] "
|
||||
labels: ["bug"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thanks for taking the time to report this bug! Please fill out the information below.
|
||||
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: A clear and concise description of what the bug is
|
||||
placeholder: Describe the bug...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: reproduce
|
||||
attributes:
|
||||
label: Steps to Reproduce
|
||||
description: Steps to reproduce the behavior
|
||||
placeholder: |
|
||||
1. Connect to camera at...
|
||||
2. Call method...
|
||||
3. See error...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: Expected Behavior
|
||||
description: What you expected to happen
|
||||
placeholder: I expected...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: code
|
||||
attributes:
|
||||
label: Code Sample
|
||||
description: Minimal code sample to reproduce the issue
|
||||
render: go
|
||||
placeholder: |
|
||||
package main
|
||||
|
||||
import "github.com/0x524a/onvif-go"
|
||||
|
||||
func main() {
|
||||
// Your code here
|
||||
}
|
||||
|
||||
- type: input
|
||||
id: go-version
|
||||
attributes:
|
||||
label: Go Version
|
||||
description: Output of `go version`
|
||||
placeholder: go version go1.21.0 linux/amd64
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: lib-version
|
||||
attributes:
|
||||
label: Library Version
|
||||
description: Git commit hash or release version
|
||||
placeholder: v1.0.0 or commit abc123
|
||||
|
||||
- type: input
|
||||
id: camera
|
||||
attributes:
|
||||
label: Camera Model/Brand
|
||||
description: If applicable
|
||||
placeholder: Hikvision DS-2CD2xx, Axis M1065-L, etc.
|
||||
|
||||
- type: dropdown
|
||||
id: os
|
||||
attributes:
|
||||
label: Operating System
|
||||
options:
|
||||
- Linux
|
||||
- macOS
|
||||
- Windows
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: logs
|
||||
attributes:
|
||||
label: Error Output/Logs
|
||||
description: Paste any error messages or logs
|
||||
render: shell
|
||||
|
||||
- type: textarea
|
||||
id: context
|
||||
attributes:
|
||||
label: Additional Context
|
||||
description: Any other context about the problem
|
||||
@@ -1,86 +0,0 @@
|
||||
name: 📷 Camera Compatibility Report
|
||||
description: Report compatibility with a specific camera model
|
||||
title: "[CAMERA] "
|
||||
labels: ["camera-compatibility"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Help us track camera compatibility! Share your experience with a specific camera model.
|
||||
|
||||
- type: input
|
||||
id: manufacturer
|
||||
attributes:
|
||||
label: Camera Manufacturer
|
||||
placeholder: Hikvision, Axis, Dahua, Bosch, etc.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: model
|
||||
attributes:
|
||||
label: Camera Model
|
||||
placeholder: DS-2CD2xx, M1065-L, IPC-HDW2xxx, etc.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: firmware
|
||||
attributes:
|
||||
label: Firmware Version
|
||||
placeholder: V5.7.3 build 220727
|
||||
|
||||
- type: dropdown
|
||||
id: status
|
||||
attributes:
|
||||
label: Compatibility Status
|
||||
options:
|
||||
- ✅ Fully Working
|
||||
- ⚠️ Partially Working
|
||||
- ❌ Not Working
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: checkboxes
|
||||
id: features
|
||||
attributes:
|
||||
label: Working Features
|
||||
description: Which features work with this camera?
|
||||
options:
|
||||
- label: Device Information
|
||||
- label: Media Profiles
|
||||
- label: Stream URIs (RTSP)
|
||||
- label: Snapshots
|
||||
- label: PTZ Control
|
||||
- label: Imaging Settings
|
||||
- label: Discovery
|
||||
|
||||
- type: textarea
|
||||
id: issues
|
||||
attributes:
|
||||
label: Known Issues
|
||||
description: Describe any issues or limitations
|
||||
placeholder: PTZ presets don't work, imaging settings return error, etc.
|
||||
|
||||
- type: textarea
|
||||
id: notes
|
||||
attributes:
|
||||
label: Additional Notes
|
||||
description: Any special configuration or workarounds needed
|
||||
placeholder: Requires authentication, needs specific settings, etc.
|
||||
|
||||
- type: checkboxes
|
||||
id: test-results
|
||||
attributes:
|
||||
label: Test Results
|
||||
description: Have you run the diagnostic tool?
|
||||
options:
|
||||
- label: I have run onvif-diagnostics and can attach the output
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: diagnostic-output
|
||||
attributes:
|
||||
label: Diagnostic Output
|
||||
description: Paste the output from onvif-diagnostics if available
|
||||
render: json
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: 💬 Discussions
|
||||
url: https://github.com/0x524a/onvif-go/discussions
|
||||
about: Ask questions and discuss ideas with the community
|
||||
- name: 📖 Documentation
|
||||
url: https://pkg.go.dev/github.com/0x524a/onvif-go
|
||||
about: Read the API documentation
|
||||
- name: 📚 Examples
|
||||
url: https://github.com/0x524a/onvif-go/tree/main/examples
|
||||
about: Browse code examples
|
||||
@@ -1,75 +0,0 @@
|
||||
name: ✨ Feature Request
|
||||
description: Suggest a new feature or enhancement
|
||||
title: "[FEATURE] "
|
||||
labels: ["enhancement"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for suggesting a new feature! Please provide details below.
|
||||
|
||||
- type: textarea
|
||||
id: problem
|
||||
attributes:
|
||||
label: Problem Statement
|
||||
description: Is your feature request related to a problem? Please describe.
|
||||
placeholder: I'm always frustrated when...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: solution
|
||||
attributes:
|
||||
label: Proposed Solution
|
||||
description: Describe the solution you'd like
|
||||
placeholder: I would like to see...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: alternatives
|
||||
attributes:
|
||||
label: Alternatives Considered
|
||||
description: Describe any alternative solutions or features you've considered
|
||||
placeholder: I also considered...
|
||||
|
||||
- type: dropdown
|
||||
id: category
|
||||
attributes:
|
||||
label: Feature Category
|
||||
description: Which area does this feature relate to?
|
||||
options:
|
||||
- Client - Device Service
|
||||
- Client - Media Service
|
||||
- Client - PTZ Service
|
||||
- Client - Imaging Service
|
||||
- Client - Discovery
|
||||
- Server Implementation
|
||||
- Documentation
|
||||
- Testing/Examples
|
||||
- Performance
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: use-case
|
||||
attributes:
|
||||
label: Use Case
|
||||
description: Describe your use case for this feature
|
||||
placeholder: This would help with...
|
||||
|
||||
- type: checkboxes
|
||||
id: contribution
|
||||
attributes:
|
||||
label: Contribution
|
||||
description: Would you be willing to contribute this feature?
|
||||
options:
|
||||
- label: I would be willing to submit a PR for this feature
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: context
|
||||
attributes:
|
||||
label: Additional Context
|
||||
description: Add any other context, screenshots, or examples
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
## Description
|
||||
<!-- Provide a clear and concise description of your changes -->
|
||||
|
||||
## Type of Change
|
||||
<!-- Mark the relevant option with an "x" -->
|
||||
|
||||
- [ ] 🐛 Bug fix (non-breaking change which fixes an issue)
|
||||
- [ ] ✨ New feature (non-breaking change which adds functionality)
|
||||
- [ ] 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||
- [ ] 📝 Documentation update
|
||||
- [ ] 🧪 Test improvements
|
||||
- [ ] ♻️ Code refactoring
|
||||
- [ ] ⚡ Performance improvement
|
||||
|
||||
## Related Issues
|
||||
<!-- Link to related issues using #issue_number -->
|
||||
|
||||
Fixes #
|
||||
Relates to #
|
||||
|
||||
## Changes Made
|
||||
<!-- List the main changes in this PR -->
|
||||
|
||||
-
|
||||
-
|
||||
-
|
||||
|
||||
## Testing Performed
|
||||
<!-- Describe the tests you ran to verify your changes -->
|
||||
|
||||
- [ ] Unit tests pass locally
|
||||
- [ ] Added new tests for new functionality
|
||||
- [ ] Tested with real ONVIF camera(s)
|
||||
- [ ] Ran `make lint` with no errors
|
||||
- [ ] Ran `make test` with all tests passing
|
||||
|
||||
### Camera Testing (if applicable)
|
||||
<!-- If you tested with physical cameras, provide details -->
|
||||
|
||||
- **Camera Model**:
|
||||
- **Firmware Version**:
|
||||
- **Test Results**:
|
||||
|
||||
## Documentation
|
||||
<!-- Mark what documentation was updated -->
|
||||
|
||||
- [ ] Code comments added/updated
|
||||
- [ ] README.md updated
|
||||
- [ ] Examples added/updated
|
||||
- [ ] API documentation (GoDoc) updated
|
||||
- [ ] CHANGELOG.md updated
|
||||
|
||||
## Checklist
|
||||
<!-- Ensure all items are complete before submitting -->
|
||||
|
||||
- [ ] My code follows the project's style guidelines
|
||||
- [ ] I have performed a self-review of my code
|
||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
||||
- [ ] I have made corresponding changes to the documentation
|
||||
- [ ] My changes generate no new warnings
|
||||
- [ ] I have added tests that prove my fix is effective or that my feature works
|
||||
- [ ] New and existing unit tests pass locally with my changes
|
||||
- [ ] Any dependent changes have been merged and published
|
||||
|
||||
## Breaking Changes
|
||||
<!-- If this introduces breaking changes, describe them and migration path -->
|
||||
|
||||
## Screenshots/Examples
|
||||
<!-- If applicable, add screenshots or example code -->
|
||||
|
||||
```go
|
||||
// Example usage
|
||||
```
|
||||
|
||||
## Additional Context
|
||||
<!-- Add any other context about the PR here -->
|
||||
|
||||
## Reviewer Notes
|
||||
<!-- Any specific areas you'd like reviewers to focus on -->
|
||||
Vendored
-180
@@ -1,180 +0,0 @@
|
||||
# GitHub Actions Workflows
|
||||
|
||||
This directory contains all CI/CD workflows for the ONVIF Go library.
|
||||
|
||||
## Workflows
|
||||
|
||||
### 🔄 CI (`ci.yml`) - Main Pipeline
|
||||
**Unified continuous integration workflow with fail-fast behavior.**
|
||||
|
||||
The CI pipeline runs sequentially - if any stage fails, subsequent stages are skipped:
|
||||
|
||||
```
|
||||
fmt → lint → test → sonarcloud
|
||||
↘ build
|
||||
```
|
||||
|
||||
**Stages:**
|
||||
|
||||
| Stage | Description | Depends On |
|
||||
|-------|-------------|------------|
|
||||
| **fmt** | Format check using `gofmt -s` | - |
|
||||
| **lint** | Static analysis with `go vet` and `golangci-lint` | fmt |
|
||||
| **test** | Unit tests with race detector + coverage | lint |
|
||||
| **sonarcloud** | Code quality & security analysis (push to master only) | test |
|
||||
| **build** | Build verification for all packages | test |
|
||||
| **ci-success** | Final status check | all |
|
||||
|
||||
**Features:**
|
||||
- ✅ Fail-fast: stops immediately if any check fails
|
||||
- ✅ Codecov integration for coverage reporting
|
||||
- ✅ SonarCloud integration for code quality
|
||||
- ✅ Go module caching for faster builds
|
||||
- ✅ Concurrency control (cancels in-progress runs)
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`, `main`
|
||||
- All pull requests targeting `master`, `main`
|
||||
|
||||
**Required for PR Merge:**
|
||||
All stages must pass before a PR can be merged. Configure branch protection rules in GitHub:
|
||||
1. Go to **Settings → Branches → Branch protection rules**
|
||||
2. Add rule for `master`
|
||||
3. Enable **Require status checks to pass before merging**
|
||||
4. Select these required checks:
|
||||
- `Format Check`
|
||||
- `Lint`
|
||||
- `Test & Coverage`
|
||||
- `SonarCloud Analysis`
|
||||
- `Build Verification`
|
||||
- `CI Success`
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Extended Tests (`test.yml`)
|
||||
Extended testing workflow for comprehensive test coverage.
|
||||
|
||||
**Jobs:**
|
||||
- **test-older-versions** - Test on older Go versions (1.19, 1.20)
|
||||
- **benchmark** - Run benchmark tests
|
||||
- **race-detector** - Extended race detector tests
|
||||
|
||||
**Triggers:**
|
||||
- Manual dispatch
|
||||
- Weekly schedule (Sunday 2 AM UTC)
|
||||
- Push to `master`/`main` when Go files change
|
||||
|
||||
---
|
||||
|
||||
### 🚀 Release (`release.yml`)
|
||||
Automated release workflow for creating GitHub releases.
|
||||
|
||||
**Jobs:**
|
||||
- **build** - Build binaries for all platforms (Linux, Windows, macOS, multiple architectures)
|
||||
- **release** - Create GitHub release with artifacts
|
||||
- **docker** - Build and push Docker images to GHCR
|
||||
|
||||
**Triggers:**
|
||||
- Push tags matching `v*.*.*`
|
||||
- Manual dispatch with version input
|
||||
|
||||
---
|
||||
|
||||
### 🔒 Security (`security.yml`)
|
||||
Security scanning workflow.
|
||||
|
||||
**Jobs:**
|
||||
- **gosec** - Security scanner
|
||||
- **govulncheck** - Vulnerability checker
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`/`main`
|
||||
- Pull requests
|
||||
- Weekly schedule
|
||||
|
||||
---
|
||||
|
||||
### 📚 Documentation (`docs.yml`)
|
||||
Documentation validation workflow.
|
||||
|
||||
**Triggers:**
|
||||
- Push to `master`/`main` when docs change
|
||||
- Manual dispatch
|
||||
|
||||
---
|
||||
|
||||
### 🔐 Dependency Review (`dependency-review.yml`)
|
||||
Dependency vulnerability review.
|
||||
|
||||
**Triggers:**
|
||||
- Pull requests
|
||||
|
||||
---
|
||||
|
||||
## CI Pipeline Flow
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ CI PIPELINE │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────────────────────┐ │
|
||||
│ │ FMT │────▶│ LINT │────▶│ TEST + COVERAGE │ │
|
||||
│ └─────────┘ └─────────┘ └───────────┬─────────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────┴─────────┐ │
|
||||
│ ▼ ▼ │
|
||||
│ ┌────────────┐ ┌───────────┐ │
|
||||
│ │ SONARCLOUD │ │ BUILD │ │
|
||||
│ │ (push only)│ └───────────┘ │
|
||||
│ └────────────┘ │ │
|
||||
│ │ │ │
|
||||
│ └─────────┬─────────┘ │
|
||||
│ ▼ │
|
||||
│ ┌─────────────────┐ │
|
||||
│ │ CI SUCCESS │ │
|
||||
│ └─────────────────┘ │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
|
||||
❌ If any stage fails, the pipeline stops immediately (fail-fast)
|
||||
ℹ️ SonarCloud only runs on push to master/main (skipped for PRs)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## SonarCloud Configuration
|
||||
|
||||
Security Hotspot analysis excludes:
|
||||
- Test files (`**/*_test.go`)
|
||||
- CI configuration (`**/.github/**`)
|
||||
- Test utilities (`**/testing/**`, `**/testdata/**`)
|
||||
- Example code (`**/examples/**`)
|
||||
- CLI tools (`**/cmd/**`)
|
||||
|
||||
This ensures security analysis focuses on production library code.
|
||||
|
||||
---
|
||||
|
||||
## Required Secrets
|
||||
|
||||
| Secret | Required | Description |
|
||||
|--------|----------|-------------|
|
||||
| `CODECOV_TOKEN` | Yes | Coverage reporting to Codecov |
|
||||
| `SONAR_TOKEN` | Yes | SonarCloud code analysis |
|
||||
| `DOCKERHUB_USERNAME` | No | Docker Hub releases |
|
||||
| `DOCKERHUB_TOKEN` | No | Docker Hub releases |
|
||||
|
||||
---
|
||||
|
||||
## Workflow Status
|
||||
|
||||
- ✅ Go 1.24 as primary version
|
||||
- ✅ Unified fail-fast CI pipeline
|
||||
- ✅ Go module caching for faster builds
|
||||
- ✅ Artifact uploads for coverage and releases
|
||||
- ✅ Concurrency control
|
||||
|
||||
---
|
||||
|
||||
*Last Updated: December 3, 2025*
|
||||
Vendored
-255
@@ -1,255 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, main]
|
||||
pull_request:
|
||||
branches: [master, main]
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
checks: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GO_VERSION: '1.24.x'
|
||||
|
||||
jobs:
|
||||
# Stage 1: Format Check (fastest - fail immediately if code isn't formatted)
|
||||
fmt:
|
||||
name: Format Check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Check formatting
|
||||
run: |
|
||||
unformatted=$(gofmt -s -l . | grep -v vendor || true)
|
||||
if [ -n "$unformatted" ]; then
|
||||
echo "❌ The following files are not properly formatted:"
|
||||
echo "$unformatted"
|
||||
echo ""
|
||||
echo "Run 'gofmt -s -w .' to fix formatting issues"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ All files are properly formatted"
|
||||
|
||||
# Stage 2: Lint (depends on fmt)
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
needs: fmt
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run go vet
|
||||
run: go vet ./...
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v6.5.0
|
||||
with:
|
||||
version: v2.1.6
|
||||
args: --timeout=5m
|
||||
|
||||
# Stage 3: Test with Coverage (depends on lint)
|
||||
test:
|
||||
name: Test & Coverage
|
||||
runs-on: ubuntu-latest
|
||||
needs: lint
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0 # Full history for SonarCloud
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests with coverage
|
||||
run: |
|
||||
go test -v -race -covermode=atomic -coverprofile=coverage.out -json ./... > test-report.json 2>&1 || true
|
||||
# Ensure coverage file exists even if tests fail
|
||||
if [ ! -f coverage.out ]; then
|
||||
echo "mode: atomic" > coverage.out
|
||||
fi
|
||||
|
||||
- name: Display coverage summary
|
||||
run: |
|
||||
echo "📊 Coverage Summary:"
|
||||
go tool cover -func=coverage.out | tail -20
|
||||
|
||||
- name: Upload coverage artifact
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: coverage-reports
|
||||
path: |
|
||||
coverage.out
|
||||
test-report.json
|
||||
retention-days: 7
|
||||
|
||||
- name: Upload to Codecov
|
||||
uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v4.6.0
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
files: ./coverage.out
|
||||
flags: unittests
|
||||
name: codecov-onvif-go
|
||||
# Don't fail on PRs from forks where token may not be available
|
||||
fail_ci_if_error: ${{ github.event_name == 'push' }}
|
||||
verbose: true
|
||||
|
||||
# Stage 4: SonarCloud Analysis (depends on test)
|
||||
# Only runs on push to master/main when SONAR_TOKEN is available
|
||||
# Skipped for PRs from forks where secrets are not accessible
|
||||
sonarcloud:
|
||||
name: SonarCloud Analysis
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && github.repository == '0x524a/onvif-go'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0 # Full history for accurate blame information
|
||||
|
||||
- name: Download coverage reports
|
||||
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
||||
with:
|
||||
name: coverage-reports
|
||||
|
||||
- name: Verify coverage file
|
||||
run: |
|
||||
echo "📁 Downloaded files:"
|
||||
ls -la
|
||||
if [ -f coverage.out ]; then
|
||||
echo "✅ Coverage file found"
|
||||
head -5 coverage.out
|
||||
else
|
||||
echo "⚠️ Coverage file not found, creating empty one"
|
||||
echo "mode: atomic" > coverage.out
|
||||
fi
|
||||
|
||||
- name: SonarCloud Scan
|
||||
uses: SonarSource/sonarcloud-github-action@4006f663ecaf1f8093e8e4abb9227f6041f52216 # v3.1.0
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
|
||||
# Stage 5: Build Verification (depends on test, runs in parallel with sonarcloud)
|
||||
build:
|
||||
name: Build Verification
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ env.GO_VERSION }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Build library
|
||||
run: go build -v ./...
|
||||
|
||||
- name: Build CLI tools
|
||||
run: |
|
||||
echo "🔨 Building CLI tools..."
|
||||
go build -v -o bin/onvif-cli ./cmd/onvif-cli
|
||||
go build -v -o bin/onvif-quick ./cmd/onvif-quick
|
||||
go build -v -o bin/onvif-server ./cmd/onvif-server
|
||||
go build -v -o bin/onvif-diagnostics ./cmd/onvif-diagnostics
|
||||
echo "✅ All CLI tools built successfully"
|
||||
|
||||
# Final status check
|
||||
ci-success:
|
||||
name: CI Success
|
||||
runs-on: ubuntu-latest
|
||||
needs: [fmt, lint, test, sonarcloud, build]
|
||||
if: always()
|
||||
steps:
|
||||
- name: Check all jobs status
|
||||
run: |
|
||||
if [[ "${{ needs.fmt.result }}" != "success" ]]; then
|
||||
echo "❌ Format check failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.lint.result }}" != "success" ]]; then
|
||||
echo "❌ Lint check failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.test.result }}" != "success" ]]; then
|
||||
echo "❌ Tests failed"
|
||||
exit 1
|
||||
fi
|
||||
# SonarCloud is optional - only fails if it ran and failed (not if skipped)
|
||||
if [[ "${{ needs.sonarcloud.result }}" == "failure" ]]; then
|
||||
echo "❌ SonarCloud analysis failed"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${{ needs.sonarcloud.result }}" == "skipped" ]]; then
|
||||
echo "ℹ️ SonarCloud analysis skipped (only runs on push to master/main)"
|
||||
fi
|
||||
if [[ "${{ needs.build.result }}" != "success" ]]; then
|
||||
echo "❌ Build verification failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ All CI checks passed successfully!"
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
name: Dependency Review
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ master, main, develop ]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
dependency-review:
|
||||
name: Review Dependencies
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Dependency Review
|
||||
uses: actions/dependency-review-action@da24556b548a50705dd671f47852072ea4c105d9 # v4.7.1
|
||||
with:
|
||||
fail-on-severity: moderate
|
||||
Vendored
-33
@@ -1,33 +0,0 @@
|
||||
name: Documentation
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
paths:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
docs-check:
|
||||
name: Documentation Check
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Check for broken links
|
||||
uses: lycheeverse/lychee-action@f81112d0d2814ded911bd23e3beaa9dda9093915 # v2.3.0
|
||||
with:
|
||||
args: --verbose --no-progress docs/ *.md
|
||||
continue-on-error: true
|
||||
|
||||
- name: Validate markdown
|
||||
uses: DavidAnson/markdownlint-cli2-action@05f32210e84442804257b2c8a4c84aa7067b5e06 # v19.0.0
|
||||
with:
|
||||
globs: 'docs/**/*.md'
|
||||
continue-on-error: true
|
||||
-286
@@ -1,286 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'Release version (e.g., v1.2.3)'
|
||||
required: true
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build Release Binaries
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
# Linux
|
||||
- goos: linux
|
||||
goarch: amd64
|
||||
- goos: linux
|
||||
goarch: arm64
|
||||
- goos: linux
|
||||
goarch: arm
|
||||
goarm: 7
|
||||
|
||||
# Windows
|
||||
- goos: windows
|
||||
goarch: amd64
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
|
||||
# macOS
|
||||
- goos: darwin
|
||||
goarch: amd64
|
||||
- goos: darwin
|
||||
goarch: arm64
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Get version
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
echo "Version: ${VERSION}"
|
||||
|
||||
- name: Build binaries
|
||||
env:
|
||||
GOOS: ${{ matrix.goos }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
GOARM: ${{ matrix.goarm }}
|
||||
CGO_ENABLED: 0
|
||||
run: |
|
||||
VERSION=${{ steps.version.outputs.VERSION }}
|
||||
SHORT_SHA=${{ steps.version.outputs.SHORT_SHA }}
|
||||
LDFLAGS="-s -w -X main.Version=${VERSION} -X main.Commit=${SHORT_SHA}"
|
||||
|
||||
# Set file extension for Windows
|
||||
EXT=""
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
EXT=".exe"
|
||||
fi
|
||||
|
||||
# Build all CLI tools
|
||||
mkdir -p dist
|
||||
|
||||
echo "🔨 Building onvif-cli..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-cli
|
||||
|
||||
echo "🔨 Building onvif-quick..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-quick
|
||||
|
||||
echo "🔨 Building onvif-server..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-server
|
||||
|
||||
echo "🔨 Building onvif-diagnostics..."
|
||||
go build -ldflags="${LDFLAGS}" -o "dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" ./cmd/onvif-diagnostics
|
||||
|
||||
- name: Create archive
|
||||
run: |
|
||||
VERSION=${{ steps.version.outputs.VERSION }}
|
||||
PLATFORM="${{ matrix.goos }}-${{ matrix.goarch }}"
|
||||
ARCHIVE_NAME="onvif-go-${VERSION}-${PLATFORM}"
|
||||
|
||||
mkdir -p releases staging
|
||||
|
||||
# Copy binaries with clean names (without platform suffix)
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
cp dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-cli.exe
|
||||
cp dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-quick.exe
|
||||
cp dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-server.exe
|
||||
cp dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }}.exe staging/onvif-diagnostics.exe
|
||||
else
|
||||
cp dist/onvif-cli-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-cli
|
||||
cp dist/onvif-quick-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-quick
|
||||
cp dist/onvif-server-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-server
|
||||
cp dist/onvif-diagnostics-${{ matrix.goos }}-${{ matrix.goarch }} staging/onvif-diagnostics
|
||||
fi
|
||||
|
||||
# Copy documentation
|
||||
cp README.md LICENSE staging/ 2>/dev/null || true
|
||||
|
||||
# Create archive from staging directory
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
cd staging
|
||||
zip -r "../releases/${ARCHIVE_NAME}.zip" .
|
||||
cd ..
|
||||
else
|
||||
cd staging
|
||||
tar czf "../releases/${ARCHIVE_NAME}.tar.gz" .
|
||||
cd ..
|
||||
fi
|
||||
|
||||
echo "✅ Created ${ARCHIVE_NAME}.tar.gz"
|
||||
|
||||
- name: Generate checksums
|
||||
run: |
|
||||
cd releases
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum * > checksums-${{ matrix.goos }}-${{ matrix.goarch }}.txt
|
||||
else
|
||||
shasum -a 256 * > checksums-${{ matrix.goos }}-${{ matrix.goarch }}.txt
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: release-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
path: releases/*
|
||||
retention-days: 7
|
||||
|
||||
release:
|
||||
name: Create GitHub Release
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
||||
with:
|
||||
path: all-releases
|
||||
pattern: release-*
|
||||
merge-multiple: true
|
||||
|
||||
- name: Generate combined checksums
|
||||
run: |
|
||||
cd all-releases
|
||||
# Combine all checksum files
|
||||
cat checksums-*.txt > checksums.txt 2>/dev/null || true
|
||||
# Remove individual checksum files
|
||||
rm -f checksums-*.txt
|
||||
|
||||
- name: Get version and changelog
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
|
||||
# Generate changelog from commits since last tag
|
||||
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
|
||||
if [ -n "$PREV_TAG" ]; then
|
||||
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT
|
||||
git log --pretty=format:"- %s (%h)" ${PREV_TAG}..HEAD >> $GITHUB_OUTPUT
|
||||
echo "" >> $GITHUB_OUTPUT
|
||||
echo "EOF" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "CHANGELOG=Initial release" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Create Release
|
||||
uses: softprops/action-gh-release@7b4da11513bf3f43f9999e90eabced41ab8bb048 # v2.2.2
|
||||
with:
|
||||
files: all-releases/*
|
||||
draft: false
|
||||
prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
|
||||
generate_release_notes: true
|
||||
make_latest: true
|
||||
body: |
|
||||
## Release ${{ steps.version.outputs.VERSION }}
|
||||
|
||||
### 📦 Installation
|
||||
|
||||
Download the appropriate binary for your platform below.
|
||||
|
||||
#### Linux/macOS
|
||||
```bash
|
||||
# Download and extract
|
||||
wget https://github.com/${{ github.repository }}/releases/download/${{ steps.version.outputs.VERSION }}/onvif-go-${{ steps.version.outputs.VERSION }}-linux-amd64.tar.gz
|
||||
tar xzf onvif-go-${{ steps.version.outputs.VERSION }}-linux-amd64.tar.gz
|
||||
|
||||
# Make executable and move to PATH
|
||||
chmod +x onvif-cli
|
||||
sudo mv onvif-cli /usr/local/bin/onvif-cli
|
||||
```
|
||||
|
||||
#### Windows
|
||||
Download the `.zip` file for your architecture and extract it.
|
||||
|
||||
#### Go Library
|
||||
```bash
|
||||
go get github.com/${{ github.repository }}@${{ steps.version.outputs.VERSION }}
|
||||
```
|
||||
|
||||
### 🔐 Checksums
|
||||
|
||||
SHA256 checksums are available in `checksums.txt`
|
||||
|
||||
### 📝 Changes
|
||||
|
||||
${{ steps.version.outputs.CHANGELOG }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
docker:
|
||||
name: Build and Push Docker Image
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
if: (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || github.event_name == 'workflow_dispatch'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515637cff71ef929a # v3.6.0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Get version
|
||||
id: version
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
# Remove 'v' prefix if present
|
||||
VERSION=${VERSION#v}
|
||||
else
|
||||
VERSION=${GITHUB_REF#refs/tags/v}
|
||||
fi
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v5.5.0
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||
push: true
|
||||
tags: |
|
||||
ghcr.io/${{ github.repository }}:latest
|
||||
ghcr.io/${{ github.repository }}:${{ steps.version.outputs.VERSION }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
name: Security Scan
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
pull_request:
|
||||
branches: [ master, main ]
|
||||
schedule:
|
||||
- cron: '0 0 * * 0' # Weekly on Sunday
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
jobs:
|
||||
gosec:
|
||||
name: Security Scan (gosec)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Install and run gosec
|
||||
run: |
|
||||
go install github.com/securego/gosec/v2/cmd/gosec@latest
|
||||
gosec -no-fail -fmt json -out gosec-report.json ./... || true
|
||||
|
||||
- name: Upload gosec report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: gosec-report
|
||||
path: gosec-report.json
|
||||
retention-days: 30
|
||||
|
||||
- name: Display gosec results
|
||||
if: always()
|
||||
run: |
|
||||
if [ -f gosec-report.json ]; then
|
||||
echo "📊 Gosec Security Scan Results:"
|
||||
cat gosec-report.json | jq -r '.Stats // empty' || echo "No stats available"
|
||||
echo ""
|
||||
echo "Issues found:"
|
||||
cat gosec-report.json | jq -r '.Issues[]? | "\(.severity | ascii_upcase): \(.rule_id) - \(.details)"' || echo "No issues found"
|
||||
fi
|
||||
|
||||
govulncheck:
|
||||
name: Vulnerability Check (govulncheck)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Run govulncheck
|
||||
run: |
|
||||
go install golang.org/x/vuln/cmd/govulncheck@latest
|
||||
govulncheck ./... || true
|
||||
Vendored
-108
@@ -1,108 +0,0 @@
|
||||
name: Extended Tests
|
||||
|
||||
on:
|
||||
workflow_dispatch: # Manual trigger
|
||||
schedule:
|
||||
- cron: '0 2 * * 0' # Weekly on Sunday at 2 AM UTC
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
paths:
|
||||
- '**.go'
|
||||
- 'go.mod'
|
||||
- 'go.sum'
|
||||
|
||||
jobs:
|
||||
# Run tests on older Go versions
|
||||
test-older-versions:
|
||||
name: Test on Go ${{ matrix.go-version }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
go-version: ['1.20', '1.19']
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests
|
||||
run: go test -v -race ./...
|
||||
|
||||
# Run benchmarks
|
||||
benchmark:
|
||||
name: Benchmark Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-1.24.x-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-1.24.x-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run benchmarks
|
||||
run: go test -bench=. -benchmem ./... -run=^$ || echo "⚠️ No benchmarks found"
|
||||
|
||||
# Test with race detector
|
||||
race-detector:
|
||||
name: Race Detector Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
|
||||
with:
|
||||
go-version: '1.24.x'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-1.24.x-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-1.24.x-
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run tests with race detector
|
||||
run: go test -race -timeout=10m ./...
|
||||
@@ -1,65 +0,0 @@
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool
|
||||
*.out
|
||||
coverage.html
|
||||
coverage.txt
|
||||
|
||||
# Dependency directories
|
||||
vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# IDEs
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
|
||||
# Binaries (in root, bin, or dist directories)
|
||||
bin/
|
||||
dist/
|
||||
releases/
|
||||
/onvif-diagnostics
|
||||
/onvif-server
|
||||
/onvif-server-example
|
||||
/generate-tests
|
||||
/onvif-cli
|
||||
/onvif-quick
|
||||
|
||||
# Temporary files
|
||||
tmp/
|
||||
temp/
|
||||
*.tmp
|
||||
|
||||
# Camera logs and captures (keep directory structure but ignore content)
|
||||
camera-logs/*.json
|
||||
camera-logs/*.tar.gz
|
||||
xml-captures/
|
||||
|
||||
# Camera data collection artifacts
|
||||
camera-data-batch-*/
|
||||
camera-discovery-*.log
|
||||
|
||||
# Extracted test captures
|
||||
capture_*.xml
|
||||
capture_*.json
|
||||
|
||||
# Environment files
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Debug files
|
||||
debug
|
||||
__debug_bin
|
||||
@@ -1,65 +0,0 @@
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool
|
||||
*.out
|
||||
coverage.html
|
||||
coverage.txt
|
||||
|
||||
# Dependency directories
|
||||
vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# IDEs
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
|
||||
# Binaries (in root, bin, or dist directories)
|
||||
bin/
|
||||
dist/
|
||||
releases/
|
||||
/onvif-diagnostics
|
||||
/onvif-server
|
||||
/onvif-server-example
|
||||
/generate-tests
|
||||
/onvif-cli
|
||||
/onvif-quick
|
||||
|
||||
# Temporary files
|
||||
tmp/
|
||||
temp/
|
||||
*.tmp
|
||||
|
||||
# Camera logs and captures (keep directory structure but ignore content)
|
||||
camera-logs/*.json
|
||||
camera-logs/*.tar.gz
|
||||
xml-captures/
|
||||
|
||||
# Camera data collection artifacts
|
||||
camera-data-batch-*/
|
||||
camera-discovery-*.log
|
||||
|
||||
# Extracted test captures
|
||||
capture_*.xml
|
||||
capture_*.json
|
||||
|
||||
# Environment files
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Debug files
|
||||
debug
|
||||
__debug_bin
|
||||
@@ -1,131 +0,0 @@
|
||||
version: "2"
|
||||
|
||||
run:
|
||||
timeout: 5m
|
||||
tests: true
|
||||
|
||||
linters:
|
||||
default: none
|
||||
enable:
|
||||
- errcheck
|
||||
- govet
|
||||
- staticcheck
|
||||
- unused
|
||||
- ineffassign
|
||||
- misspell
|
||||
- unconvert
|
||||
- unparam
|
||||
- gocritic
|
||||
- gosec
|
||||
- copyloopvar
|
||||
- goconst
|
||||
- gocyclo
|
||||
- dupl
|
||||
- funlen
|
||||
- gocognit
|
||||
- nakedret
|
||||
- prealloc
|
||||
- whitespace
|
||||
- wrapcheck
|
||||
- errname
|
||||
- errorlint
|
||||
- exhaustive
|
||||
- godot
|
||||
- err113
|
||||
- mnd
|
||||
- goprintffuncname
|
||||
- nlreturn
|
||||
- noctx
|
||||
- nolintlint
|
||||
- thelper
|
||||
- tparallel
|
||||
- wastedassign
|
||||
|
||||
settings:
|
||||
errcheck:
|
||||
check-type-assertions: true
|
||||
check-blank: true
|
||||
|
||||
gocyclo:
|
||||
min-complexity: 15
|
||||
|
||||
funlen:
|
||||
lines: 120
|
||||
statements: 60
|
||||
|
||||
gocritic:
|
||||
enabled-tags:
|
||||
- diagnostic
|
||||
- experimental
|
||||
- opinionated
|
||||
- performance
|
||||
- style
|
||||
disabled-checks:
|
||||
- dupImport
|
||||
- ifElseChain
|
||||
- octalLiteral
|
||||
- whyNoLint
|
||||
- wrapperFunc
|
||||
|
||||
godot:
|
||||
scope: declarations
|
||||
exclude:
|
||||
- "^TODO:"
|
||||
- "^FIXME:"
|
||||
|
||||
misspell:
|
||||
locale: US
|
||||
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- std-error-handling
|
||||
rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- errcheck
|
||||
- gosec
|
||||
- funlen
|
||||
- gocyclo
|
||||
- gocognit
|
||||
- dupl
|
||||
|
||||
- path: (media|device|ptz|imaging|device_security|device_additional)\.go
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: cmd/
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: deviceio\.go
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: event\.go
|
||||
linters:
|
||||
- dupl
|
||||
- gocritic
|
||||
- staticcheck
|
||||
|
||||
- path: examples/
|
||||
linters:
|
||||
- errcheck
|
||||
- err113
|
||||
- funlen
|
||||
- gocognit
|
||||
- gocritic
|
||||
- gocyclo
|
||||
- godot
|
||||
- gosec
|
||||
- mnd
|
||||
- nlreturn
|
||||
- noctx
|
||||
- unused
|
||||
- wrapcheck
|
||||
|
||||
output:
|
||||
formats:
|
||||
text:
|
||||
path: stdout
|
||||
@@ -1,131 +0,0 @@
|
||||
version: "2"
|
||||
|
||||
run:
|
||||
timeout: 5m
|
||||
tests: true
|
||||
|
||||
linters:
|
||||
default: none
|
||||
enable:
|
||||
- errcheck
|
||||
- govet
|
||||
- staticcheck
|
||||
- unused
|
||||
- ineffassign
|
||||
- misspell
|
||||
- unconvert
|
||||
- unparam
|
||||
- gocritic
|
||||
- gosec
|
||||
- copyloopvar
|
||||
- goconst
|
||||
- gocyclo
|
||||
- dupl
|
||||
- funlen
|
||||
- gocognit
|
||||
- nakedret
|
||||
- prealloc
|
||||
- whitespace
|
||||
- wrapcheck
|
||||
- errname
|
||||
- errorlint
|
||||
- exhaustive
|
||||
- godot
|
||||
- err113
|
||||
- mnd
|
||||
- goprintffuncname
|
||||
- nlreturn
|
||||
- noctx
|
||||
- nolintlint
|
||||
- thelper
|
||||
- tparallel
|
||||
- wastedassign
|
||||
|
||||
settings:
|
||||
errcheck:
|
||||
check-type-assertions: true
|
||||
check-blank: true
|
||||
|
||||
gocyclo:
|
||||
min-complexity: 15
|
||||
|
||||
funlen:
|
||||
lines: 120
|
||||
statements: 60
|
||||
|
||||
gocritic:
|
||||
enabled-tags:
|
||||
- diagnostic
|
||||
- experimental
|
||||
- opinionated
|
||||
- performance
|
||||
- style
|
||||
disabled-checks:
|
||||
- dupImport
|
||||
- ifElseChain
|
||||
- octalLiteral
|
||||
- whyNoLint
|
||||
- wrapperFunc
|
||||
|
||||
godot:
|
||||
scope: declarations
|
||||
exclude:
|
||||
- "^TODO:"
|
||||
- "^FIXME:"
|
||||
|
||||
misspell:
|
||||
locale: US
|
||||
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- std-error-handling
|
||||
rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- errcheck
|
||||
- gosec
|
||||
- funlen
|
||||
- gocyclo
|
||||
- gocognit
|
||||
- dupl
|
||||
|
||||
- path: (media|device|ptz|imaging|device_security|device_additional)\.go
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: cmd/
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: deviceio\.go
|
||||
linters:
|
||||
- dupl
|
||||
|
||||
- path: event\.go
|
||||
linters:
|
||||
- dupl
|
||||
- gocritic
|
||||
- staticcheck
|
||||
|
||||
- path: examples/
|
||||
linters:
|
||||
- errcheck
|
||||
- err113
|
||||
- funlen
|
||||
- gocognit
|
||||
- gocritic
|
||||
- gocyclo
|
||||
- godot
|
||||
- gosec
|
||||
- mnd
|
||||
- nlreturn
|
||||
- noctx
|
||||
- unused
|
||||
- wrapcheck
|
||||
|
||||
output:
|
||||
formats:
|
||||
text:
|
||||
path: stdout
|
||||
@@ -1,226 +0,0 @@
|
||||
# Building and Releasing onvif-go
|
||||
|
||||
This document describes how to build binaries for multiple platforms and create releases.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Build for Your Current Platform
|
||||
|
||||
```bash
|
||||
make build-cli
|
||||
```
|
||||
|
||||
This builds all CLI tools for your current OS/architecture in the `bin/` directory.
|
||||
|
||||
### Build for All Platforms
|
||||
|
||||
```bash
|
||||
make build-all
|
||||
```
|
||||
|
||||
This creates binaries for:
|
||||
- **Linux**: amd64, arm64, arm (32-bit)
|
||||
- **Windows**: amd64, arm64
|
||||
- **macOS**: amd64 (Intel), arm64 (Apple Silicon)
|
||||
|
||||
Binaries are output to `bin/` directory.
|
||||
|
||||
### Create Release Archives
|
||||
|
||||
```bash
|
||||
make release
|
||||
```
|
||||
|
||||
This:
|
||||
1. Builds for all platforms
|
||||
2. Creates `.tar.gz` archives (Linux/macOS) and `.zip` files (Windows)
|
||||
3. Generates SHA256 checksums
|
||||
4. Places everything in `releases/` directory
|
||||
|
||||
## Manual Building
|
||||
|
||||
### Using the Build Script
|
||||
|
||||
```bash
|
||||
# Build with automatic version detection
|
||||
./build-release.sh
|
||||
|
||||
# Build with specific version
|
||||
./build-release.sh v1.0.1
|
||||
```
|
||||
|
||||
### Using Go Directly
|
||||
|
||||
```bash
|
||||
# Set platform and architecture
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
|
||||
# Build a specific tool
|
||||
go build -o bin/onvif-cli-linux-amd64 ./cmd/onvif-cli
|
||||
```
|
||||
|
||||
## Supported Platforms
|
||||
|
||||
| OS | Architecture | Binary Suffix | Notes |
|
||||
|---------|-------------|------------------------|----------------------------|
|
||||
| Linux | amd64 | `linux-amd64` | 64-bit Intel/AMD |
|
||||
| Linux | arm64 | `linux-arm64` | 64-bit ARM (Raspberry Pi 4)|
|
||||
| Linux | arm | `linux-arm` | 32-bit ARM (Raspberry Pi 3)|
|
||||
| Windows | amd64 | `windows-amd64.exe` | 64-bit Windows |
|
||||
| Windows | arm64 | `windows-arm64.exe` | ARM Windows (Surface Pro X)|
|
||||
| macOS | amd64 | `darwin-amd64` | Intel Macs |
|
||||
| macOS | arm64 | `darwin-arm64` | Apple Silicon (M1/M2/M3) |
|
||||
|
||||
## CLI Tools
|
||||
|
||||
The following binaries are built:
|
||||
|
||||
1. **onvif-cli** - Comprehensive ONVIF client with full feature set
|
||||
2. **onvif-quick** - Quick tool for common operations
|
||||
3. **onvif-server** - ONVIF mock server for testing
|
||||
4. **onvif-diagnostics** - Diagnostic and debugging tools
|
||||
|
||||
## Automated Releases via GitHub Actions
|
||||
|
||||
Releases are automatically created when you push a tag:
|
||||
|
||||
```bash
|
||||
# Create and push a new version tag
|
||||
git tag -a v1.0.1 -m "Release version 1.0.1"
|
||||
git push origin v1.0.1
|
||||
```
|
||||
|
||||
The GitHub Actions workflow will:
|
||||
1. Build binaries for all platforms
|
||||
2. Create release archives
|
||||
3. Generate checksums
|
||||
4. Create a GitHub release with all artifacts
|
||||
5. Build and push Docker images (multi-arch)
|
||||
|
||||
### Release Workflow Features
|
||||
|
||||
- ✅ Builds for 7 platform/architecture combinations
|
||||
- ✅ Creates compressed archives (`.tar.gz` and `.zip`)
|
||||
- ✅ Generates SHA256 checksums for verification
|
||||
- ✅ Auto-generates release notes from commits
|
||||
- ✅ Supports pre-releases (tags with `-rc`, `-beta`, `-alpha`)
|
||||
- ✅ Builds multi-architecture Docker images
|
||||
- ✅ Pushes to GitHub Container Registry
|
||||
|
||||
## Docker Images
|
||||
|
||||
Docker images are automatically built for:
|
||||
- `linux/amd64`
|
||||
- `linux/arm64`
|
||||
- `linux/arm/v7`
|
||||
|
||||
Available at:
|
||||
```
|
||||
ghcr.io/0x524a/onvif-go:latest
|
||||
ghcr.io/0x524a/onvif-go:v1.0.0
|
||||
```
|
||||
|
||||
## Manual GitHub Release
|
||||
|
||||
If you prefer to create releases manually:
|
||||
|
||||
```bash
|
||||
# Build release archives
|
||||
make release
|
||||
|
||||
# Create GitHub release using gh CLI
|
||||
gh release create v1.0.1 releases/* \
|
||||
--title "Release v1.0.1" \
|
||||
--notes "Release notes here"
|
||||
```
|
||||
|
||||
## Version Numbering
|
||||
|
||||
Follow [Semantic Versioning](https://semver.org/):
|
||||
|
||||
- `v1.0.0` - Major release (breaking changes)
|
||||
- `v1.1.0` - Minor release (new features, backward compatible)
|
||||
- `v1.1.1` - Patch release (bug fixes)
|
||||
- `v1.0.0-rc1` - Release candidate
|
||||
- `v1.0.0-beta1` - Beta release
|
||||
- `v1.0.0-alpha1` - Alpha release
|
||||
|
||||
## Build Flags
|
||||
|
||||
The build process uses the following flags:
|
||||
|
||||
```bash
|
||||
-ldflags="-s -w -X main.Version=<version> -X main.Commit=<sha>"
|
||||
```
|
||||
|
||||
- `-s` - Omit symbol table (smaller binary)
|
||||
- `-w` - Omit DWARF debug info (smaller binary)
|
||||
- `-X main.Version` - Inject version string
|
||||
- `-X main.Commit` - Inject git commit SHA
|
||||
|
||||
## Size Optimization
|
||||
|
||||
Binaries are built with `CGO_ENABLED=0` and stripped flags, resulting in:
|
||||
- Smaller binary sizes
|
||||
- No external dependencies
|
||||
- Portable across systems
|
||||
|
||||
Typical sizes:
|
||||
- onvif-cli: ~10-15 MB
|
||||
- onvif-quick: ~8-12 MB
|
||||
- onvif-server: ~10-14 MB
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Build Fails for Specific Platform
|
||||
|
||||
Some platforms may not be supported by all dependencies. Check:
|
||||
```bash
|
||||
go tool dist list # List all supported platforms
|
||||
```
|
||||
|
||||
### Large Binary Sizes
|
||||
|
||||
Ensure you're using the build flags:
|
||||
```bash
|
||||
go build -ldflags="-s -w" -o binary ./cmd/tool
|
||||
```
|
||||
|
||||
### Missing Dependencies
|
||||
|
||||
```bash
|
||||
go mod download
|
||||
go mod tidy
|
||||
```
|
||||
|
||||
## Distribution
|
||||
|
||||
Once built, binaries can be distributed via:
|
||||
|
||||
1. **GitHub Releases** (automatic)
|
||||
2. **Package managers** (homebrew, apt, etc.)
|
||||
3. **Container registries** (Docker Hub, GHCR)
|
||||
4. **Direct download** from your server
|
||||
|
||||
## Verification
|
||||
|
||||
Users can verify downloads using checksums:
|
||||
|
||||
```bash
|
||||
# Download binary and checksum
|
||||
wget https://github.com/0x524a/onvif-go/releases/download/v1.0.0/onvif-go-v1.0.0-linux-amd64.tar.gz
|
||||
wget https://github.com/0x524a/onvif-go/releases/download/v1.0.0/checksums.txt
|
||||
|
||||
# Verify
|
||||
sha256sum -c checksums.txt --ignore-missing
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
After building:
|
||||
1. Test binaries on target platforms
|
||||
2. Update CHANGELOG.md with release notes
|
||||
3. Create GitHub release
|
||||
4. Announce on relevant channels
|
||||
5. Update documentation with new features
|
||||
@@ -1,226 +0,0 @@
|
||||
# Building and Releasing onvif-go
|
||||
|
||||
This document describes how to build binaries for multiple platforms and create releases.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Build for Your Current Platform
|
||||
|
||||
```bash
|
||||
make build-cli
|
||||
```
|
||||
|
||||
This builds all CLI tools for your current OS/architecture in the `bin/` directory.
|
||||
|
||||
### Build for All Platforms
|
||||
|
||||
```bash
|
||||
make build-all
|
||||
```
|
||||
|
||||
This creates binaries for:
|
||||
- **Linux**: amd64, arm64, arm (32-bit)
|
||||
- **Windows**: amd64, arm64
|
||||
- **macOS**: amd64 (Intel), arm64 (Apple Silicon)
|
||||
|
||||
Binaries are output to `bin/` directory.
|
||||
|
||||
### Create Release Archives
|
||||
|
||||
```bash
|
||||
make release
|
||||
```
|
||||
|
||||
This:
|
||||
1. Builds for all platforms
|
||||
2. Creates `.tar.gz` archives (Linux/macOS) and `.zip` files (Windows)
|
||||
3. Generates SHA256 checksums
|
||||
4. Places everything in `releases/` directory
|
||||
|
||||
## Manual Building
|
||||
|
||||
### Using the Build Script
|
||||
|
||||
```bash
|
||||
# Build with automatic version detection
|
||||
./build-release.sh
|
||||
|
||||
# Build with specific version
|
||||
./build-release.sh v1.0.1
|
||||
```
|
||||
|
||||
### Using Go Directly
|
||||
|
||||
```bash
|
||||
# Set platform and architecture
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
|
||||
# Build a specific tool
|
||||
go build -o bin/onvif-cli-linux-amd64 ./cmd/onvif-cli
|
||||
```
|
||||
|
||||
## Supported Platforms
|
||||
|
||||
| OS | Architecture | Binary Suffix | Notes |
|
||||
|---------|-------------|------------------------|----------------------------|
|
||||
| Linux | amd64 | `linux-amd64` | 64-bit Intel/AMD |
|
||||
| Linux | arm64 | `linux-arm64` | 64-bit ARM (Raspberry Pi 4)|
|
||||
| Linux | arm | `linux-arm` | 32-bit ARM (Raspberry Pi 3)|
|
||||
| Windows | amd64 | `windows-amd64.exe` | 64-bit Windows |
|
||||
| Windows | arm64 | `windows-arm64.exe` | ARM Windows (Surface Pro X)|
|
||||
| macOS | amd64 | `darwin-amd64` | Intel Macs |
|
||||
| macOS | arm64 | `darwin-arm64` | Apple Silicon (M1/M2/M3) |
|
||||
|
||||
## CLI Tools
|
||||
|
||||
The following binaries are built:
|
||||
|
||||
1. **onvif-cli** - Comprehensive ONVIF client with full feature set
|
||||
2. **onvif-quick** - Quick tool for common operations
|
||||
3. **onvif-server** - ONVIF mock server for testing
|
||||
4. **onvif-diagnostics** - Diagnostic and debugging tools
|
||||
|
||||
## Automated Releases via GitHub Actions
|
||||
|
||||
Releases are automatically created when you push a tag:
|
||||
|
||||
```bash
|
||||
# Create and push a new version tag
|
||||
git tag -a v1.0.1 -m "Release version 1.0.1"
|
||||
git push origin v1.0.1
|
||||
```
|
||||
|
||||
The GitHub Actions workflow will:
|
||||
1. Build binaries for all platforms
|
||||
2. Create release archives
|
||||
3. Generate checksums
|
||||
4. Create a GitHub release with all artifacts
|
||||
5. Build and push Docker images (multi-arch)
|
||||
|
||||
### Release Workflow Features
|
||||
|
||||
- ✅ Builds for 7 platform/architecture combinations
|
||||
- ✅ Creates compressed archives (`.tar.gz` and `.zip`)
|
||||
- ✅ Generates SHA256 checksums for verification
|
||||
- ✅ Auto-generates release notes from commits
|
||||
- ✅ Supports pre-releases (tags with `-rc`, `-beta`, `-alpha`)
|
||||
- ✅ Builds multi-architecture Docker images
|
||||
- ✅ Pushes to GitHub Container Registry
|
||||
|
||||
## Docker Images
|
||||
|
||||
Docker images are automatically built for:
|
||||
- `linux/amd64`
|
||||
- `linux/arm64`
|
||||
- `linux/arm/v7`
|
||||
|
||||
Available at:
|
||||
```
|
||||
ghcr.io/0x524a/onvif-go:latest
|
||||
ghcr.io/0x524a/onvif-go:v1.0.0
|
||||
```
|
||||
|
||||
## Manual GitHub Release
|
||||
|
||||
If you prefer to create releases manually:
|
||||
|
||||
```bash
|
||||
# Build release archives
|
||||
make release
|
||||
|
||||
# Create GitHub release using gh CLI
|
||||
gh release create v1.0.1 releases/* \
|
||||
--title "Release v1.0.1" \
|
||||
--notes "Release notes here"
|
||||
```
|
||||
|
||||
## Version Numbering
|
||||
|
||||
Follow [Semantic Versioning](https://semver.org/):
|
||||
|
||||
- `v1.0.0` - Major release (breaking changes)
|
||||
- `v1.1.0` - Minor release (new features, backward compatible)
|
||||
- `v1.1.1` - Patch release (bug fixes)
|
||||
- `v1.0.0-rc1` - Release candidate
|
||||
- `v1.0.0-beta1` - Beta release
|
||||
- `v1.0.0-alpha1` - Alpha release
|
||||
|
||||
## Build Flags
|
||||
|
||||
The build process uses the following flags:
|
||||
|
||||
```bash
|
||||
-ldflags="-s -w -X main.Version=<version> -X main.Commit=<sha>"
|
||||
```
|
||||
|
||||
- `-s` - Omit symbol table (smaller binary)
|
||||
- `-w` - Omit DWARF debug info (smaller binary)
|
||||
- `-X main.Version` - Inject version string
|
||||
- `-X main.Commit` - Inject git commit SHA
|
||||
|
||||
## Size Optimization
|
||||
|
||||
Binaries are built with `CGO_ENABLED=0` and stripped flags, resulting in:
|
||||
- Smaller binary sizes
|
||||
- No external dependencies
|
||||
- Portable across systems
|
||||
|
||||
Typical sizes:
|
||||
- onvif-cli: ~10-15 MB
|
||||
- onvif-quick: ~8-12 MB
|
||||
- onvif-server: ~10-14 MB
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Build Fails for Specific Platform
|
||||
|
||||
Some platforms may not be supported by all dependencies. Check:
|
||||
```bash
|
||||
go tool dist list # List all supported platforms
|
||||
```
|
||||
|
||||
### Large Binary Sizes
|
||||
|
||||
Ensure you're using the build flags:
|
||||
```bash
|
||||
go build -ldflags="-s -w" -o binary ./cmd/tool
|
||||
```
|
||||
|
||||
### Missing Dependencies
|
||||
|
||||
```bash
|
||||
go mod download
|
||||
go mod tidy
|
||||
```
|
||||
|
||||
## Distribution
|
||||
|
||||
Once built, binaries can be distributed via:
|
||||
|
||||
1. **GitHub Releases** (automatic)
|
||||
2. **Package managers** (homebrew, apt, etc.)
|
||||
3. **Container registries** (Docker Hub, GHCR)
|
||||
4. **Direct download** from your server
|
||||
|
||||
## Verification
|
||||
|
||||
Users can verify downloads using checksums:
|
||||
|
||||
```bash
|
||||
# Download binary and checksum
|
||||
wget https://github.com/0x524a/onvif-go/releases/download/v1.0.0/onvif-go-v1.0.0-linux-amd64.tar.gz
|
||||
wget https://github.com/0x524a/onvif-go/releases/download/v1.0.0/checksums.txt
|
||||
|
||||
# Verify
|
||||
sha256sum -c checksums.txt --ignore-missing
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
After building:
|
||||
1. Test binaries on target platforms
|
||||
2. Update CHANGELOG.md with release notes
|
||||
3. Create GitHub release
|
||||
4. Announce on relevant channels
|
||||
5. Update documentation with new features
|
||||
@@ -1,122 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.1.3] - 2025-11-18
|
||||
|
||||
### Changed
|
||||
- **Release Workflow**: Create releases as draft initially
|
||||
- Fixes "Cannot upload assets to an immutable release" error
|
||||
- Releases must be manually published after assets upload
|
||||
- Prevents race condition where release publishes before all assets finish uploading
|
||||
|
||||
## [1.1.2] - 2025-11-18
|
||||
|
||||
### Changed
|
||||
- **Release Workflow**: Upgraded to `softprops/action-gh-release@v2`
|
||||
- Fixes asset upload race condition in v1
|
||||
- Better handling of concurrent file uploads
|
||||
- Added `fail_on_unmatched_files` and `make_latest` flags
|
||||
|
||||
## [1.1.1] - 2025-11-18
|
||||
|
||||
### Added
|
||||
- **RTSPeek Library Integration**: RTSP stream inspection using `github.com/0x524A/rtspeek`
|
||||
- Replaced command-line `ffprobe` execution with library-based approach
|
||||
- Enhanced stream inspection with codec, resolution, and framerate detection
|
||||
- 5-second timeout for stream DESCRIBE operations
|
||||
- TCP fallback for basic connectivity checks
|
||||
- See `cmd/onvif-cli/main.go` for implementation
|
||||
|
||||
### Changed
|
||||
- **Code Quality Improvements**: Fixed all linting errors
|
||||
- Removed unused `generateDemoASCII()` function
|
||||
- Fixed dynamic format strings (SA1006 errors)
|
||||
- Added proper error handling for Close() operations
|
||||
- Migrated to golangci-lint v2 configuration
|
||||
- CI/CD pipeline excludes utility tools and examples from linting
|
||||
- **golangci-lint v2**: Updated configuration and GitHub Actions workflow
|
||||
- Created `.golangci.yml` with v2 schema
|
||||
- Updated CI to use golangci-lint-action@v8 with v2.2
|
||||
- Scoped linting to main packages only
|
||||
|
||||
## [1.1.0] - 2025-11-18
|
||||
|
||||
### Added
|
||||
- **Simplified Endpoint API**: `NewClient()` now accepts multiple endpoint formats
|
||||
- Simple IP address: `"192.168.1.100"`
|
||||
- IP with port: `"192.168.1.100:8080"`
|
||||
- Full URL: `"http://192.168.1.100/onvif/device_service"` (backward compatible)
|
||||
- Automatically adds `http://` scheme and `/onvif/device_service` path when needed
|
||||
- See `docs/SIMPLIFIED_ENDPOINT.md` for details
|
||||
- **Localhost URL Fix**: Automatic handling of cameras that report localhost addresses
|
||||
- Detects and fixes localhost/127.0.0.1/0.0.0.0/::1 in GetCapabilities response
|
||||
- Replaces with actual camera IP address
|
||||
- Preserves service-specific ports when specified
|
||||
- Handles common camera firmware bugs transparently
|
||||
- Comprehensive test coverage for endpoint normalization (12 test cases)
|
||||
- Comprehensive test coverage for localhost URL handling (10 test cases)
|
||||
- New example: `examples/simplified-endpoint/` demonstrating all endpoint formats
|
||||
- Documentation: `docs/PROJECT_STRUCTURE.md` explaining project organization
|
||||
- Initial release of onvif-go library
|
||||
|
||||
### Changed
|
||||
- **Project Structure**: Implemented ideal Go project layout
|
||||
- Moved `soap/` to `internal/soap/` (private implementation)
|
||||
- Moved `test/test-server.go` to `examples/test-server/` for clarity
|
||||
- Removed empty `test/` directory
|
||||
- Public API remains at root level for clean imports
|
||||
- Follows Standard Go Project Layout for libraries
|
||||
- Updated all imports throughout codebase
|
||||
- See `docs/PROJECT_STRUCTURE.md` and `docs/ARCHITECTURE.md` for details
|
||||
- Updated `docs/ARCHITECTURE.md` to reflect new project structure
|
||||
- Updated module path from `github.com/0x524A/onvif-go` to `github.com/0x524a/onvif-go` (lowercase)
|
||||
- ONVIF Client with context support
|
||||
- Device service implementation
|
||||
- GetDeviceInformation
|
||||
- GetCapabilities
|
||||
- GetSystemDateAndTime
|
||||
- SystemReboot
|
||||
- Media service implementation
|
||||
- GetProfiles
|
||||
- GetStreamURI (RTSP/HTTP)
|
||||
- GetSnapshotURI
|
||||
- GetVideoEncoderConfiguration
|
||||
- PTZ service implementation
|
||||
- ContinuousMove
|
||||
- AbsoluteMove
|
||||
- RelativeMove
|
||||
- Stop
|
||||
- GetStatus
|
||||
- GetPresets
|
||||
- GotoPreset
|
||||
- Imaging service implementation
|
||||
- GetImagingSettings
|
||||
- SetImagingSettings
|
||||
- Move (focus control)
|
||||
- WS-Discovery implementation
|
||||
- Automatic device discovery via multicast
|
||||
- SOAP client with WS-Security
|
||||
- UsernameToken authentication
|
||||
- Password digest (SHA-1)
|
||||
- Comprehensive type definitions
|
||||
- Error handling with typed errors
|
||||
- Connection pooling for performance
|
||||
- Complete examples
|
||||
- Discovery
|
||||
- Device information
|
||||
- PTZ control
|
||||
- Imaging settings
|
||||
- Comprehensive documentation
|
||||
- README with usage guide
|
||||
|
||||
[Unreleased]: https://github.com/0x524a/onvif-go/compare/v1.1.3...HEAD
|
||||
[1.1.3]: https://github.com/0x524a/onvif-go/compare/v1.1.2...v1.1.3
|
||||
[1.1.2]: https://github.com/0x524a/onvif-go/compare/v1.1.1...v1.1.2
|
||||
[1.1.1]: https://github.com/0x524a/onvif-go/compare/v1.1.0...v1.1.1
|
||||
[1.1.0]: https://github.com/0x524a/onvif-go/compare/v1.0.3...v1.1.0
|
||||
@@ -1,122 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.1.3] - 2025-11-18
|
||||
|
||||
### Changed
|
||||
- **Release Workflow**: Create releases as draft initially
|
||||
- Fixes "Cannot upload assets to an immutable release" error
|
||||
- Releases must be manually published after assets upload
|
||||
- Prevents race condition where release publishes before all assets finish uploading
|
||||
|
||||
## [1.1.2] - 2025-11-18
|
||||
|
||||
### Changed
|
||||
- **Release Workflow**: Upgraded to `softprops/action-gh-release@v2`
|
||||
- Fixes asset upload race condition in v1
|
||||
- Better handling of concurrent file uploads
|
||||
- Added `fail_on_unmatched_files` and `make_latest` flags
|
||||
|
||||
## [1.1.1] - 2025-11-18
|
||||
|
||||
### Added
|
||||
- **RTSPeek Library Integration**: RTSP stream inspection using `github.com/0x524A/rtspeek`
|
||||
- Replaced command-line `ffprobe` execution with library-based approach
|
||||
- Enhanced stream inspection with codec, resolution, and framerate detection
|
||||
- 5-second timeout for stream DESCRIBE operations
|
||||
- TCP fallback for basic connectivity checks
|
||||
- See `cmd/onvif-cli/main.go` for implementation
|
||||
|
||||
### Changed
|
||||
- **Code Quality Improvements**: Fixed all linting errors
|
||||
- Removed unused `generateDemoASCII()` function
|
||||
- Fixed dynamic format strings (SA1006 errors)
|
||||
- Added proper error handling for Close() operations
|
||||
- Migrated to golangci-lint v2 configuration
|
||||
- CI/CD pipeline excludes utility tools and examples from linting
|
||||
- **golangci-lint v2**: Updated configuration and GitHub Actions workflow
|
||||
- Created `.golangci.yml` with v2 schema
|
||||
- Updated CI to use golangci-lint-action@v8 with v2.2
|
||||
- Scoped linting to main packages only
|
||||
|
||||
## [1.1.0] - 2025-11-18
|
||||
|
||||
### Added
|
||||
- **Simplified Endpoint API**: `NewClient()` now accepts multiple endpoint formats
|
||||
- Simple IP address: `"192.168.1.100"`
|
||||
- IP with port: `"192.168.1.100:8080"`
|
||||
- Full URL: `"http://192.168.1.100/onvif/device_service"` (backward compatible)
|
||||
- Automatically adds `http://` scheme and `/onvif/device_service` path when needed
|
||||
- See `docs/SIMPLIFIED_ENDPOINT.md` for details
|
||||
- **Localhost URL Fix**: Automatic handling of cameras that report localhost addresses
|
||||
- Detects and fixes localhost/127.0.0.1/0.0.0.0/::1 in GetCapabilities response
|
||||
- Replaces with actual camera IP address
|
||||
- Preserves service-specific ports when specified
|
||||
- Handles common camera firmware bugs transparently
|
||||
- Comprehensive test coverage for endpoint normalization (12 test cases)
|
||||
- Comprehensive test coverage for localhost URL handling (10 test cases)
|
||||
- New example: `examples/simplified-endpoint/` demonstrating all endpoint formats
|
||||
- Documentation: `docs/PROJECT_STRUCTURE.md` explaining project organization
|
||||
- Initial release of onvif-go library
|
||||
|
||||
### Changed
|
||||
- **Project Structure**: Implemented ideal Go project layout
|
||||
- Moved `soap/` to `internal/soap/` (private implementation)
|
||||
- Moved `test/test-server.go` to `examples/test-server/` for clarity
|
||||
- Removed empty `test/` directory
|
||||
- Public API remains at root level for clean imports
|
||||
- Follows Standard Go Project Layout for libraries
|
||||
- Updated all imports throughout codebase
|
||||
- See `docs/PROJECT_STRUCTURE.md` and `docs/ARCHITECTURE.md` for details
|
||||
- Updated `docs/ARCHITECTURE.md` to reflect new project structure
|
||||
- Updated module path from `github.com/0x524A/onvif-go` to `github.com/0x524a/onvif-go` (lowercase)
|
||||
- ONVIF Client with context support
|
||||
- Device service implementation
|
||||
- GetDeviceInformation
|
||||
- GetCapabilities
|
||||
- GetSystemDateAndTime
|
||||
- SystemReboot
|
||||
- Media service implementation
|
||||
- GetProfiles
|
||||
- GetStreamURI (RTSP/HTTP)
|
||||
- GetSnapshotURI
|
||||
- GetVideoEncoderConfiguration
|
||||
- PTZ service implementation
|
||||
- ContinuousMove
|
||||
- AbsoluteMove
|
||||
- RelativeMove
|
||||
- Stop
|
||||
- GetStatus
|
||||
- GetPresets
|
||||
- GotoPreset
|
||||
- Imaging service implementation
|
||||
- GetImagingSettings
|
||||
- SetImagingSettings
|
||||
- Move (focus control)
|
||||
- WS-Discovery implementation
|
||||
- Automatic device discovery via multicast
|
||||
- SOAP client with WS-Security
|
||||
- UsernameToken authentication
|
||||
- Password digest (SHA-1)
|
||||
- Comprehensive type definitions
|
||||
- Error handling with typed errors
|
||||
- Connection pooling for performance
|
||||
- Complete examples
|
||||
- Discovery
|
||||
- Device information
|
||||
- PTZ control
|
||||
- Imaging settings
|
||||
- Comprehensive documentation
|
||||
- README with usage guide
|
||||
|
||||
[Unreleased]: https://github.com/0x524a/onvif-go/compare/v1.1.3...HEAD
|
||||
[1.1.3]: https://github.com/0x524a/onvif-go/compare/v1.1.2...v1.1.3
|
||||
[1.1.2]: https://github.com/0x524a/onvif-go/compare/v1.1.1...v1.1.2
|
||||
[1.1.1]: https://github.com/0x524a/onvif-go/compare/v1.1.0...v1.1.1
|
||||
[1.1.0]: https://github.com/0x524a/onvif-go/compare/v1.0.3...v1.1.0
|
||||
@@ -1,323 +0,0 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
onvif-go is a production-ready Go library for communicating with ONVIF-compliant IP cameras. It provides both a client library for camera control and a server implementation for camera simulation/testing.
|
||||
|
||||
**Key Features:**
|
||||
- ONVIF client with 200+ APIs across Device, Media, PTZ, and Imaging services
|
||||
- ONVIF server for virtual camera simulation
|
||||
- WS-Discovery for network camera detection
|
||||
- WS-Security authentication with digest passwords
|
||||
- Multiple CLI tools for camera interaction and diagnostics
|
||||
|
||||
## Essential Commands
|
||||
|
||||
### Build
|
||||
```bash
|
||||
# Build all CLI tools for current platform
|
||||
make build
|
||||
|
||||
# Build for multiple platforms (Linux, Windows, macOS)
|
||||
make build-all
|
||||
|
||||
# Build specific CLI tool
|
||||
go build -o bin/onvif-cli ./cmd/onvif-cli
|
||||
```
|
||||
|
||||
### Test
|
||||
```bash
|
||||
# Run all tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -v -race -coverprofile=coverage.out ./...
|
||||
make test-coverage
|
||||
|
||||
# Run benchmarks
|
||||
make bench
|
||||
go test -bench=. -benchmem ./...
|
||||
|
||||
# Run specific package tests
|
||||
go test -v ./discovery
|
||||
go test -v ./server
|
||||
```
|
||||
|
||||
### Lint and Format
|
||||
```bash
|
||||
# Run all checks (fmt, vet, lint)
|
||||
make check
|
||||
|
||||
# Format code
|
||||
make fmt
|
||||
|
||||
# Run linter
|
||||
make lint # Requires golangci-lint
|
||||
```
|
||||
|
||||
### Development
|
||||
```bash
|
||||
# Install dependencies
|
||||
make deps
|
||||
|
||||
# Clean build artifacts
|
||||
make clean
|
||||
|
||||
# Build examples
|
||||
make examples
|
||||
|
||||
# Run CLI tools
|
||||
./bin/onvif-cli
|
||||
./bin/onvif-quick
|
||||
```
|
||||
|
||||
### CLI Tools
|
||||
|
||||
**onvif-cli**: Comprehensive ONVIF client with interactive and non-interactive modes
|
||||
```bash
|
||||
# Interactive menu
|
||||
./bin/onvif-cli
|
||||
|
||||
# Discover cameras
|
||||
./bin/onvif-cli discover -interface eth0 -timeout 5
|
||||
|
||||
# Get device info
|
||||
./bin/onvif-cli -op info -endpoint http://camera-ip/onvif/device_service -username admin -password pass
|
||||
```
|
||||
|
||||
**onvif-diagnostics**: Camera testing and XML capture for debugging
|
||||
```bash
|
||||
./bin/onvif-diagnostics -endpoint http://camera-ip/onvif/device_service -username admin -password pass -verbose
|
||||
|
||||
# Capture raw SOAP XML
|
||||
./bin/onvif-diagnostics ... -capture-xml
|
||||
```
|
||||
|
||||
**onvif-server**: Virtual camera server for testing
|
||||
```bash
|
||||
./bin/onvif-server -profiles 5 -username admin -password mypass -port 9000
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
### Package Structure
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── *.go # Core client library (client.go, device.go, media.go, ptz.go, imaging.go, etc.)
|
||||
├── types.go # ONVIF type definitions (all SOAP XML structures)
|
||||
├── internal/soap/ # SOAP client with WS-Security (NOT exported)
|
||||
├── discovery/ # WS-Discovery implementation (exported package)
|
||||
├── server/ # ONVIF server implementation (exported package)
|
||||
├── cmd/ # CLI tools
|
||||
│ ├── onvif-cli/ # Full-featured client
|
||||
│ ├── onvif-quick/ # Lightweight tool
|
||||
│ ├── onvif-diagnostics/ # Debugging and XML capture
|
||||
│ ├── onvif-server/ # Server CLI
|
||||
│ └── generate-tests/ # Test generation from XML captures
|
||||
├── testing/ # Test utilities (mock_server.go)
|
||||
├── testdata/captures/ # Real camera SOAP response captures
|
||||
└── examples/ # Usage examples
|
||||
```
|
||||
|
||||
### Key Components
|
||||
|
||||
**Client Layer** (`client.go`):
|
||||
- Main `Client` struct with HTTP connection pooling
|
||||
- Functional options pattern for configuration (WithCredentials, WithTimeout, WithHTTPClient)
|
||||
- Context-aware operations throughout
|
||||
- Thread-safe credential management with sync.RWMutex
|
||||
|
||||
**Service Implementations**:
|
||||
- `device.go` + `device_*.go`: 98 Device Management APIs (configuration, users, network, certificates, WiFi, storage)
|
||||
- `media.go`: Media profiles, stream URIs (RTSP/HTTP), snapshots, encoder configuration
|
||||
- `ptz.go`: PTZ control (continuous, absolute, relative movement, presets)
|
||||
- `imaging.go`: Image settings (brightness, contrast, exposure, focus, white balance)
|
||||
- `event.go`: Event service (subscriptions, pull-point)
|
||||
- `deviceio.go`: Device I/O and relay control
|
||||
|
||||
**SOAP Layer** (`internal/soap/`):
|
||||
- WS-Security UsernameToken authentication with password digest (SHA-1)
|
||||
- XML marshaling/unmarshaling for ONVIF SOAP messages
|
||||
- Error handling with ONVIFError type
|
||||
- NOT exported - internal implementation detail
|
||||
|
||||
**Discovery** (`discovery/`):
|
||||
- WS-Discovery multicast probe on 239.255.255.250:3702
|
||||
- Network interface selection support
|
||||
- Device deduplication by endpoint reference
|
||||
|
||||
**Server** (`server/`):
|
||||
- Virtual multi-lens camera simulator
|
||||
- Implements Device, Media, PTZ, and Imaging services
|
||||
- Configurable number of camera profiles (up to 10)
|
||||
- WS-Security authentication support
|
||||
|
||||
### Type System
|
||||
|
||||
All ONVIF types are defined in `types.go` (~30,000+ lines). Key patterns:
|
||||
|
||||
- XML struct tags for SOAP serialization
|
||||
- Pointer fields for optional values (ONVIF convention)
|
||||
- Namespace-aware XML marshaling
|
||||
- Comprehensive coverage of ONVIF Core, Device, Media, PTZ, Imaging specs
|
||||
|
||||
## Development Patterns
|
||||
|
||||
### Client Usage Pattern
|
||||
```go
|
||||
// 1. Create client with options
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
)
|
||||
|
||||
// 2. Initialize to discover service endpoints
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. Use service methods
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
```
|
||||
|
||||
### Context Usage
|
||||
All network operations require `context.Context` as first parameter:
|
||||
- Enables timeouts: `context.WithTimeout()`
|
||||
- Enables cancellation: `context.WithCancel()`
|
||||
- No blocking indefinitely
|
||||
|
||||
### Error Handling
|
||||
- Sentinel errors: `ErrServiceNotSupported`, `ErrAuthenticationFailed`
|
||||
- Typed errors: `ONVIFError` for SOAP faults
|
||||
- Use `errors.Is()` and `errors.As()` for error checking
|
||||
- Always wrap errors with context: `fmt.Errorf("operation failed: %w", err)`
|
||||
|
||||
### Testing Strategy
|
||||
- Unit tests alongside implementation files (`*_test.go`)
|
||||
- Real camera tests in `*_real_camera_test.go` (skipped without `-tags=real_camera`)
|
||||
- Mock server in `testing/mock_server.go` for integration tests
|
||||
- XML captures in `testdata/captures/` for regression testing
|
||||
- Comprehensive test coverage tracked in `docs/testing/`
|
||||
|
||||
### Authentication Implementation
|
||||
WS-Security digest authentication requires:
|
||||
1. Generate 16-byte random nonce
|
||||
2. Get UTC timestamp
|
||||
3. Calculate: `Base64(SHA1(nonce + timestamp + password))`
|
||||
4. Include Username, Password (digest), Nonce, Created in SOAP header
|
||||
|
||||
## Critical Implementation Details
|
||||
|
||||
### SOAP Message Structure
|
||||
All ONVIF operations use SOAP 1.2 over HTTP POST:
|
||||
- Envelope with WS-Security header (if authenticated)
|
||||
- Body contains operation-specific request
|
||||
- Response parsed from SOAP envelope body
|
||||
- SOAP faults mapped to Go errors
|
||||
|
||||
### Service Endpoint Discovery
|
||||
The `Initialize()` method discovers service endpoints:
|
||||
1. Calls `GetCapabilities()` to get service URLs
|
||||
2. Caches endpoints (media, PTZ, imaging, event)
|
||||
3. Falls back to device service endpoint if not found
|
||||
4. Subsequent operations use cached endpoints
|
||||
|
||||
### Connection Pooling
|
||||
HTTP client configured for optimal performance:
|
||||
- Idle connection timeout: 90s
|
||||
- Max idle connections: 10
|
||||
- Max idle per host: 5
|
||||
- Custom transport for TLS control
|
||||
|
||||
### Network Interface Selection (Discovery)
|
||||
Discovery supports binding to specific interfaces:
|
||||
- By interface name: `"eth0"`, `"en0"`
|
||||
- By IP address: `"192.168.1.100"`
|
||||
- Auto-detection tries all active interfaces if not specified
|
||||
- Uses `golang.org/x/net/ipv4` for multicast control
|
||||
|
||||
## File Organization
|
||||
|
||||
- **Root `*.go`**: Public API and implementation
|
||||
- **`*_test.go`**: Unit tests (run with `go test`)
|
||||
- **`*_real_camera_test.go`**: Integration tests requiring real cameras
|
||||
- **`docs/`**: Comprehensive documentation organized by category
|
||||
- **`test-reports/`**: JSON reports from real camera testing
|
||||
- **`examples/`**: Standalone example programs
|
||||
|
||||
## Build System
|
||||
|
||||
**Makefile targets**:
|
||||
- `make all`: deps + check + test + build
|
||||
- `make build`: Build CLI tools for current platform
|
||||
- `make build-all`: Cross-compile for all platforms (Linux, Windows, macOS - amd64, arm64, arm)
|
||||
- `make release`: Build + create archives + checksums
|
||||
- `make test`: Run tests with race detection
|
||||
- `make bench`: Run benchmarks
|
||||
- `make check`: fmt + vet + lint
|
||||
- `make clean`: Remove build artifacts
|
||||
|
||||
**Build flags**:
|
||||
- `CGO_ENABLED=0`: Static binaries
|
||||
- `-ldflags="-s -w"`: Strip symbols for smaller size
|
||||
- Version injection: `-X main.Version=$(VERSION)`
|
||||
|
||||
## Testing Without Real Cameras
|
||||
|
||||
Use the diagnostic tool to capture real camera responses:
|
||||
```bash
|
||||
# 1. Capture XML from real camera
|
||||
./onvif-diagnostics -endpoint http://camera/onvif/device_service -username user -password pass -capture-xml
|
||||
|
||||
# 2. Generate test from capture
|
||||
./generate-tests -capture camera-logs/*_xmlcapture_*.tar.gz -output testdata/captures/
|
||||
|
||||
# 3. Run generated tests
|
||||
go test -v ./testdata/captures/
|
||||
```
|
||||
|
||||
This allows testing library changes against real camera behavior without physical hardware.
|
||||
|
||||
## Important Notes
|
||||
|
||||
- **ONVIF specification compliance**: Follows ONVIF Core, Device, Media, PTZ, Imaging specs
|
||||
- **WS-Security**: Digest authentication (SHA-1) per ONVIF requirements
|
||||
- **Concurrency**: All operations are thread-safe
|
||||
- **XML namespaces**: Critical for ONVIF - handled in types.go struct tags
|
||||
- **Pointer semantics**: Optional fields use pointers (ONVIF convention)
|
||||
- **Service support detection**: Always check capabilities before calling service-specific methods
|
||||
- **Endpoint flexibility**: Accepts full URLs, IP:port, or bare IPs (auto-adds http:// and /onvif/device_service)
|
||||
|
||||
## Common Development Tasks
|
||||
|
||||
**Adding a new ONVIF operation**:
|
||||
1. Define request/response types in `types.go` with XML tags
|
||||
2. Implement method in appropriate service file (`device.go`, `media.go`, etc.)
|
||||
3. Use `callMethod()` helper for SOAP invocation
|
||||
4. Add unit test in corresponding `*_test.go`
|
||||
5. Update documentation in `docs/api/`
|
||||
|
||||
**Adding a new CLI command**:
|
||||
1. Add command/flags in `cmd/onvif-cli/main.go`
|
||||
2. Implement handler function
|
||||
3. Update CLI help text
|
||||
4. Add example to `docs/CLI_*.md`
|
||||
|
||||
**Adding server functionality**:
|
||||
1. Implement handler in `server/*.go`
|
||||
2. Register handler in SOAP router
|
||||
3. Add test in `server/*_test.go`
|
||||
4. Update `server/README.md`
|
||||
|
||||
## Dependencies
|
||||
|
||||
Minimal dependencies (see `go.mod`):
|
||||
- `golang.org/x/net`: HTTP/2 and IDNA support
|
||||
- `github.com/0x524A/rtspeek`: RTSP stream validation (diagnostics tool)
|
||||
- Standard library for everything else
|
||||
|
||||
Go version: 1.21+ (currently 1.24)
|
||||
@@ -1,323 +0,0 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
onvif-go is a production-ready Go library for communicating with ONVIF-compliant IP cameras. It provides both a client library for camera control and a server implementation for camera simulation/testing.
|
||||
|
||||
**Key Features:**
|
||||
- ONVIF client with 200+ APIs across Device, Media, PTZ, and Imaging services
|
||||
- ONVIF server for virtual camera simulation
|
||||
- WS-Discovery for network camera detection
|
||||
- WS-Security authentication with digest passwords
|
||||
- Multiple CLI tools for camera interaction and diagnostics
|
||||
|
||||
## Essential Commands
|
||||
|
||||
### Build
|
||||
```bash
|
||||
# Build all CLI tools for current platform
|
||||
make build
|
||||
|
||||
# Build for multiple platforms (Linux, Windows, macOS)
|
||||
make build-all
|
||||
|
||||
# Build specific CLI tool
|
||||
go build -o bin/onvif-cli ./cmd/onvif-cli
|
||||
```
|
||||
|
||||
### Test
|
||||
```bash
|
||||
# Run all tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -v -race -coverprofile=coverage.out ./...
|
||||
make test-coverage
|
||||
|
||||
# Run benchmarks
|
||||
make bench
|
||||
go test -bench=. -benchmem ./...
|
||||
|
||||
# Run specific package tests
|
||||
go test -v ./discovery
|
||||
go test -v ./server
|
||||
```
|
||||
|
||||
### Lint and Format
|
||||
```bash
|
||||
# Run all checks (fmt, vet, lint)
|
||||
make check
|
||||
|
||||
# Format code
|
||||
make fmt
|
||||
|
||||
# Run linter
|
||||
make lint # Requires golangci-lint
|
||||
```
|
||||
|
||||
### Development
|
||||
```bash
|
||||
# Install dependencies
|
||||
make deps
|
||||
|
||||
# Clean build artifacts
|
||||
make clean
|
||||
|
||||
# Build examples
|
||||
make examples
|
||||
|
||||
# Run CLI tools
|
||||
./bin/onvif-cli
|
||||
./bin/onvif-quick
|
||||
```
|
||||
|
||||
### CLI Tools
|
||||
|
||||
**onvif-cli**: Comprehensive ONVIF client with interactive and non-interactive modes
|
||||
```bash
|
||||
# Interactive menu
|
||||
./bin/onvif-cli
|
||||
|
||||
# Discover cameras
|
||||
./bin/onvif-cli discover -interface eth0 -timeout 5
|
||||
|
||||
# Get device info
|
||||
./bin/onvif-cli -op info -endpoint http://camera-ip/onvif/device_service -username admin -password pass
|
||||
```
|
||||
|
||||
**onvif-diagnostics**: Camera testing and XML capture for debugging
|
||||
```bash
|
||||
./bin/onvif-diagnostics -endpoint http://camera-ip/onvif/device_service -username admin -password pass -verbose
|
||||
|
||||
# Capture raw SOAP XML
|
||||
./bin/onvif-diagnostics ... -capture-xml
|
||||
```
|
||||
|
||||
**onvif-server**: Virtual camera server for testing
|
||||
```bash
|
||||
./bin/onvif-server -profiles 5 -username admin -password mypass -port 9000
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
### Package Structure
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── *.go # Core client library (client.go, device.go, media.go, ptz.go, imaging.go, etc.)
|
||||
├── types.go # ONVIF type definitions (all SOAP XML structures)
|
||||
├── internal/soap/ # SOAP client with WS-Security (NOT exported)
|
||||
├── discovery/ # WS-Discovery implementation (exported package)
|
||||
├── server/ # ONVIF server implementation (exported package)
|
||||
├── cmd/ # CLI tools
|
||||
│ ├── onvif-cli/ # Full-featured client
|
||||
│ ├── onvif-quick/ # Lightweight tool
|
||||
│ ├── onvif-diagnostics/ # Debugging and XML capture
|
||||
│ ├── onvif-server/ # Server CLI
|
||||
│ └── generate-tests/ # Test generation from XML captures
|
||||
├── testing/ # Test utilities (mock_server.go)
|
||||
├── testdata/captures/ # Real camera SOAP response captures
|
||||
└── examples/ # Usage examples
|
||||
```
|
||||
|
||||
### Key Components
|
||||
|
||||
**Client Layer** (`client.go`):
|
||||
- Main `Client` struct with HTTP connection pooling
|
||||
- Functional options pattern for configuration (WithCredentials, WithTimeout, WithHTTPClient)
|
||||
- Context-aware operations throughout
|
||||
- Thread-safe credential management with sync.RWMutex
|
||||
|
||||
**Service Implementations**:
|
||||
- `device.go` + `device_*.go`: 98 Device Management APIs (configuration, users, network, certificates, WiFi, storage)
|
||||
- `media.go`: Media profiles, stream URIs (RTSP/HTTP), snapshots, encoder configuration
|
||||
- `ptz.go`: PTZ control (continuous, absolute, relative movement, presets)
|
||||
- `imaging.go`: Image settings (brightness, contrast, exposure, focus, white balance)
|
||||
- `event.go`: Event service (subscriptions, pull-point)
|
||||
- `deviceio.go`: Device I/O and relay control
|
||||
|
||||
**SOAP Layer** (`internal/soap/`):
|
||||
- WS-Security UsernameToken authentication with password digest (SHA-1)
|
||||
- XML marshaling/unmarshaling for ONVIF SOAP messages
|
||||
- Error handling with ONVIFError type
|
||||
- NOT exported - internal implementation detail
|
||||
|
||||
**Discovery** (`discovery/`):
|
||||
- WS-Discovery multicast probe on 239.255.255.250:3702
|
||||
- Network interface selection support
|
||||
- Device deduplication by endpoint reference
|
||||
|
||||
**Server** (`server/`):
|
||||
- Virtual multi-lens camera simulator
|
||||
- Implements Device, Media, PTZ, and Imaging services
|
||||
- Configurable number of camera profiles (up to 10)
|
||||
- WS-Security authentication support
|
||||
|
||||
### Type System
|
||||
|
||||
All ONVIF types are defined in `types.go` (~30,000+ lines). Key patterns:
|
||||
|
||||
- XML struct tags for SOAP serialization
|
||||
- Pointer fields for optional values (ONVIF convention)
|
||||
- Namespace-aware XML marshaling
|
||||
- Comprehensive coverage of ONVIF Core, Device, Media, PTZ, Imaging specs
|
||||
|
||||
## Development Patterns
|
||||
|
||||
### Client Usage Pattern
|
||||
```go
|
||||
// 1. Create client with options
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
)
|
||||
|
||||
// 2. Initialize to discover service endpoints
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. Use service methods
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
```
|
||||
|
||||
### Context Usage
|
||||
All network operations require `context.Context` as first parameter:
|
||||
- Enables timeouts: `context.WithTimeout()`
|
||||
- Enables cancellation: `context.WithCancel()`
|
||||
- No blocking indefinitely
|
||||
|
||||
### Error Handling
|
||||
- Sentinel errors: `ErrServiceNotSupported`, `ErrAuthenticationFailed`
|
||||
- Typed errors: `ONVIFError` for SOAP faults
|
||||
- Use `errors.Is()` and `errors.As()` for error checking
|
||||
- Always wrap errors with context: `fmt.Errorf("operation failed: %w", err)`
|
||||
|
||||
### Testing Strategy
|
||||
- Unit tests alongside implementation files (`*_test.go`)
|
||||
- Real camera tests in `*_real_camera_test.go` (skipped without `-tags=real_camera`)
|
||||
- Mock server in `testing/mock_server.go` for integration tests
|
||||
- XML captures in `testdata/captures/` for regression testing
|
||||
- Comprehensive test coverage tracked in `docs/testing/`
|
||||
|
||||
### Authentication Implementation
|
||||
WS-Security digest authentication requires:
|
||||
1. Generate 16-byte random nonce
|
||||
2. Get UTC timestamp
|
||||
3. Calculate: `Base64(SHA1(nonce + timestamp + password))`
|
||||
4. Include Username, Password (digest), Nonce, Created in SOAP header
|
||||
|
||||
## Critical Implementation Details
|
||||
|
||||
### SOAP Message Structure
|
||||
All ONVIF operations use SOAP 1.2 over HTTP POST:
|
||||
- Envelope with WS-Security header (if authenticated)
|
||||
- Body contains operation-specific request
|
||||
- Response parsed from SOAP envelope body
|
||||
- SOAP faults mapped to Go errors
|
||||
|
||||
### Service Endpoint Discovery
|
||||
The `Initialize()` method discovers service endpoints:
|
||||
1. Calls `GetCapabilities()` to get service URLs
|
||||
2. Caches endpoints (media, PTZ, imaging, event)
|
||||
3. Falls back to device service endpoint if not found
|
||||
4. Subsequent operations use cached endpoints
|
||||
|
||||
### Connection Pooling
|
||||
HTTP client configured for optimal performance:
|
||||
- Idle connection timeout: 90s
|
||||
- Max idle connections: 10
|
||||
- Max idle per host: 5
|
||||
- Custom transport for TLS control
|
||||
|
||||
### Network Interface Selection (Discovery)
|
||||
Discovery supports binding to specific interfaces:
|
||||
- By interface name: `"eth0"`, `"en0"`
|
||||
- By IP address: `"192.168.1.100"`
|
||||
- Auto-detection tries all active interfaces if not specified
|
||||
- Uses `golang.org/x/net/ipv4` for multicast control
|
||||
|
||||
## File Organization
|
||||
|
||||
- **Root `*.go`**: Public API and implementation
|
||||
- **`*_test.go`**: Unit tests (run with `go test`)
|
||||
- **`*_real_camera_test.go`**: Integration tests requiring real cameras
|
||||
- **`docs/`**: Comprehensive documentation organized by category
|
||||
- **`test-reports/`**: JSON reports from real camera testing
|
||||
- **`examples/`**: Standalone example programs
|
||||
|
||||
## Build System
|
||||
|
||||
**Makefile targets**:
|
||||
- `make all`: deps + check + test + build
|
||||
- `make build`: Build CLI tools for current platform
|
||||
- `make build-all`: Cross-compile for all platforms (Linux, Windows, macOS - amd64, arm64, arm)
|
||||
- `make release`: Build + create archives + checksums
|
||||
- `make test`: Run tests with race detection
|
||||
- `make bench`: Run benchmarks
|
||||
- `make check`: fmt + vet + lint
|
||||
- `make clean`: Remove build artifacts
|
||||
|
||||
**Build flags**:
|
||||
- `CGO_ENABLED=0`: Static binaries
|
||||
- `-ldflags="-s -w"`: Strip symbols for smaller size
|
||||
- Version injection: `-X main.Version=$(VERSION)`
|
||||
|
||||
## Testing Without Real Cameras
|
||||
|
||||
Use the diagnostic tool to capture real camera responses:
|
||||
```bash
|
||||
# 1. Capture XML from real camera
|
||||
./onvif-diagnostics -endpoint http://camera/onvif/device_service -username user -password pass -capture-xml
|
||||
|
||||
# 2. Generate test from capture
|
||||
./generate-tests -capture camera-logs/*_xmlcapture_*.tar.gz -output testdata/captures/
|
||||
|
||||
# 3. Run generated tests
|
||||
go test -v ./testdata/captures/
|
||||
```
|
||||
|
||||
This allows testing library changes against real camera behavior without physical hardware.
|
||||
|
||||
## Important Notes
|
||||
|
||||
- **ONVIF specification compliance**: Follows ONVIF Core, Device, Media, PTZ, Imaging specs
|
||||
- **WS-Security**: Digest authentication (SHA-1) per ONVIF requirements
|
||||
- **Concurrency**: All operations are thread-safe
|
||||
- **XML namespaces**: Critical for ONVIF - handled in types.go struct tags
|
||||
- **Pointer semantics**: Optional fields use pointers (ONVIF convention)
|
||||
- **Service support detection**: Always check capabilities before calling service-specific methods
|
||||
- **Endpoint flexibility**: Accepts full URLs, IP:port, or bare IPs (auto-adds http:// and /onvif/device_service)
|
||||
|
||||
## Common Development Tasks
|
||||
|
||||
**Adding a new ONVIF operation**:
|
||||
1. Define request/response types in `types.go` with XML tags
|
||||
2. Implement method in appropriate service file (`device.go`, `media.go`, etc.)
|
||||
3. Use `callMethod()` helper for SOAP invocation
|
||||
4. Add unit test in corresponding `*_test.go`
|
||||
5. Update documentation in `docs/api/`
|
||||
|
||||
**Adding a new CLI command**:
|
||||
1. Add command/flags in `cmd/onvif-cli/main.go`
|
||||
2. Implement handler function
|
||||
3. Update CLI help text
|
||||
4. Add example to `docs/CLI_*.md`
|
||||
|
||||
**Adding server functionality**:
|
||||
1. Implement handler in `server/*.go`
|
||||
2. Register handler in SOAP router
|
||||
3. Add test in `server/*_test.go`
|
||||
4. Update `server/README.md`
|
||||
|
||||
## Dependencies
|
||||
|
||||
Minimal dependencies (see `go.mod`):
|
||||
- `golang.org/x/net`: HTTP/2 and IDNA support
|
||||
- `github.com/0x524A/rtspeek`: RTSP stream validation (diagnostics tool)
|
||||
- Standard library for everything else
|
||||
|
||||
Go version: 1.21+ (currently 1.24)
|
||||
@@ -1,125 +0,0 @@
|
||||
# Contributing to onvif-go
|
||||
|
||||
First off, thank you for considering contributing to onvif-go! It's people like you that make onvif-go such a great tool.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
This project and everyone participating in it is governed by our Code of Conduct. By participating, you are expected to uphold this code.
|
||||
|
||||
## How Can I Contribute?
|
||||
|
||||
### Reporting Bugs
|
||||
|
||||
Before creating bug reports, please check the existing issues as you might find out that you don't need to create one. When you are creating a bug report, please include as many details as possible:
|
||||
|
||||
* **Use a clear and descriptive title**
|
||||
* **Describe the exact steps to reproduce the problem**
|
||||
* **Provide specific examples to demonstrate the steps**
|
||||
* **Describe the behavior you observed and what behavior you expected**
|
||||
* **Include camera model and firmware version if relevant**
|
||||
* **Include Go version and OS information**
|
||||
|
||||
### Suggesting Enhancements
|
||||
|
||||
Enhancement suggestions are tracked as GitHub issues. When creating an enhancement suggestion, please include:
|
||||
|
||||
* **Use a clear and descriptive title**
|
||||
* **Provide a detailed description of the suggested enhancement**
|
||||
* **Provide specific examples to demonstrate the enhancement**
|
||||
* **Explain why this enhancement would be useful**
|
||||
|
||||
### Pull Requests
|
||||
|
||||
1. Fork the repo and create your branch from `main`
|
||||
2. If you've added code that should be tested, add tests
|
||||
3. If you've changed APIs, update the documentation
|
||||
4. Ensure the test suite passes
|
||||
5. Make sure your code follows the existing style
|
||||
6. Issue that pull request!
|
||||
|
||||
## Development Setup
|
||||
|
||||
```bash
|
||||
# Clone your fork
|
||||
git clone https://github.com/YOUR_USERNAME/onvif-go.git
|
||||
cd onvif-go
|
||||
|
||||
# Add upstream remote
|
||||
git remote add upstream https://github.com/0x524a/onvif-go.git
|
||||
|
||||
# Create a branch
|
||||
git checkout -b feature/my-new-feature
|
||||
|
||||
# Install dependencies
|
||||
go mod download
|
||||
|
||||
# Run tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run linter (if installed)
|
||||
golangci-lint run
|
||||
```
|
||||
|
||||
## Coding Standards
|
||||
|
||||
* Follow standard Go conventions and idioms
|
||||
* Use `gofmt` to format your code
|
||||
* Write clear, self-documenting code with comments where necessary
|
||||
* Add tests for new functionality
|
||||
* Keep functions focused and modular
|
||||
* Use meaningful variable and function names
|
||||
|
||||
## Commit Messages
|
||||
|
||||
* Use the present tense ("Add feature" not "Added feature")
|
||||
* Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
|
||||
* Limit the first line to 72 characters or less
|
||||
* Reference issues and pull requests liberally after the first line
|
||||
|
||||
Example:
|
||||
```
|
||||
Add support for Analytics service
|
||||
|
||||
- Implement GetAnalyticsConfiguration
|
||||
- Add rule engine support
|
||||
- Update documentation
|
||||
|
||||
Closes #123
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
* Write unit tests for new functionality
|
||||
* Ensure all tests pass before submitting PR
|
||||
* Add integration tests for new ONVIF services
|
||||
* Test with real cameras when possible
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
go test ./...
|
||||
|
||||
# Run with race detector
|
||||
go test -race ./...
|
||||
|
||||
# Run with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run specific test
|
||||
go test -run TestGetDeviceInformation
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
* Update README.md for user-facing changes
|
||||
* Add godoc comments for exported types and functions
|
||||
* Update examples if API changes
|
||||
* Add changelog entry for significant changes
|
||||
|
||||
## Questions?
|
||||
|
||||
Feel free to open an issue with your question or reach out to the maintainers.
|
||||
|
||||
Thank you for contributing! 🎉
|
||||
@@ -1,125 +0,0 @@
|
||||
# Contributing to onvif-go
|
||||
|
||||
First off, thank you for considering contributing to onvif-go! It's people like you that make onvif-go such a great tool.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
This project and everyone participating in it is governed by our Code of Conduct. By participating, you are expected to uphold this code.
|
||||
|
||||
## How Can I Contribute?
|
||||
|
||||
### Reporting Bugs
|
||||
|
||||
Before creating bug reports, please check the existing issues as you might find out that you don't need to create one. When you are creating a bug report, please include as many details as possible:
|
||||
|
||||
* **Use a clear and descriptive title**
|
||||
* **Describe the exact steps to reproduce the problem**
|
||||
* **Provide specific examples to demonstrate the steps**
|
||||
* **Describe the behavior you observed and what behavior you expected**
|
||||
* **Include camera model and firmware version if relevant**
|
||||
* **Include Go version and OS information**
|
||||
|
||||
### Suggesting Enhancements
|
||||
|
||||
Enhancement suggestions are tracked as GitHub issues. When creating an enhancement suggestion, please include:
|
||||
|
||||
* **Use a clear and descriptive title**
|
||||
* **Provide a detailed description of the suggested enhancement**
|
||||
* **Provide specific examples to demonstrate the enhancement**
|
||||
* **Explain why this enhancement would be useful**
|
||||
|
||||
### Pull Requests
|
||||
|
||||
1. Fork the repo and create your branch from `main`
|
||||
2. If you've added code that should be tested, add tests
|
||||
3. If you've changed APIs, update the documentation
|
||||
4. Ensure the test suite passes
|
||||
5. Make sure your code follows the existing style
|
||||
6. Issue that pull request!
|
||||
|
||||
## Development Setup
|
||||
|
||||
```bash
|
||||
# Clone your fork
|
||||
git clone https://github.com/YOUR_USERNAME/onvif-go.git
|
||||
cd onvif-go
|
||||
|
||||
# Add upstream remote
|
||||
git remote add upstream https://github.com/0x524a/onvif-go.git
|
||||
|
||||
# Create a branch
|
||||
git checkout -b feature/my-new-feature
|
||||
|
||||
# Install dependencies
|
||||
go mod download
|
||||
|
||||
# Run tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run linter (if installed)
|
||||
golangci-lint run
|
||||
```
|
||||
|
||||
## Coding Standards
|
||||
|
||||
* Follow standard Go conventions and idioms
|
||||
* Use `gofmt` to format your code
|
||||
* Write clear, self-documenting code with comments where necessary
|
||||
* Add tests for new functionality
|
||||
* Keep functions focused and modular
|
||||
* Use meaningful variable and function names
|
||||
|
||||
## Commit Messages
|
||||
|
||||
* Use the present tense ("Add feature" not "Added feature")
|
||||
* Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
|
||||
* Limit the first line to 72 characters or less
|
||||
* Reference issues and pull requests liberally after the first line
|
||||
|
||||
Example:
|
||||
```
|
||||
Add support for Analytics service
|
||||
|
||||
- Implement GetAnalyticsConfiguration
|
||||
- Add rule engine support
|
||||
- Update documentation
|
||||
|
||||
Closes #123
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
* Write unit tests for new functionality
|
||||
* Ensure all tests pass before submitting PR
|
||||
* Add integration tests for new ONVIF services
|
||||
* Test with real cameras when possible
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
go test ./...
|
||||
|
||||
# Run with race detector
|
||||
go test -race ./...
|
||||
|
||||
# Run with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run specific test
|
||||
go test -run TestGetDeviceInformation
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
* Update README.md for user-facing changes
|
||||
* Add godoc comments for exported types and functions
|
||||
* Update examples if API changes
|
||||
* Add changelog entry for significant changes
|
||||
|
||||
## Questions?
|
||||
|
||||
Feel free to open an issue with your question or reach out to the maintainers.
|
||||
|
||||
Thank you for contributing! 🎉
|
||||
@@ -1,55 +0,0 @@
|
||||
# Multi-stage build for Go ONVIF library
|
||||
FROM golang:1.21-alpine AS builder
|
||||
|
||||
# Install build dependencies
|
||||
RUN apk add --no-cache git ca-certificates tzdata
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /src
|
||||
|
||||
# Copy go mod files
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# Download dependencies
|
||||
RUN go mod download
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build the applications
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/onvif-cli ./cmd/onvif-cli
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/onvif-quick ./cmd/onvif-quick
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
|
||||
# Install runtime dependencies
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
|
||||
# Create non-root user
|
||||
RUN addgroup -g 1001 -S onvif && \
|
||||
adduser -u 1001 -S onvif -G onvif
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy binaries from builder
|
||||
COPY --from=builder /bin/onvif-cli /usr/local/bin/
|
||||
COPY --from=builder /bin/onvif-quick /usr/local/bin/
|
||||
|
||||
# Copy examples (optional)
|
||||
COPY --from=builder /src/examples ./examples/
|
||||
|
||||
# Set ownership
|
||||
RUN chown -R onvif:onvif /app
|
||||
|
||||
# Switch to non-root user
|
||||
USER onvif
|
||||
|
||||
# Default command (run the quick tool)
|
||||
CMD ["onvif-quick"]
|
||||
|
||||
# Labels
|
||||
LABEL maintainer="ONVIF Library Team"
|
||||
LABEL description="Go ONVIF library with CLI tools"
|
||||
LABEL version="1.0.0"
|
||||
@@ -1,55 +0,0 @@
|
||||
# Multi-stage build for Go ONVIF library
|
||||
FROM golang:1.21-alpine AS builder
|
||||
|
||||
# Install build dependencies
|
||||
RUN apk add --no-cache git ca-certificates tzdata
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /src
|
||||
|
||||
# Copy go mod files
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# Download dependencies
|
||||
RUN go mod download
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build the applications
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/onvif-cli ./cmd/onvif-cli
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/onvif-quick ./cmd/onvif-quick
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
|
||||
# Install runtime dependencies
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
|
||||
# Create non-root user
|
||||
RUN addgroup -g 1001 -S onvif && \
|
||||
adduser -u 1001 -S onvif -G onvif
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy binaries from builder
|
||||
COPY --from=builder /bin/onvif-cli /usr/local/bin/
|
||||
COPY --from=builder /bin/onvif-quick /usr/local/bin/
|
||||
|
||||
# Copy examples (optional)
|
||||
COPY --from=builder /src/examples ./examples/
|
||||
|
||||
# Set ownership
|
||||
RUN chown -R onvif:onvif /app
|
||||
|
||||
# Switch to non-root user
|
||||
USER onvif
|
||||
|
||||
# Default command (run the quick tool)
|
||||
CMD ["onvif-quick"]
|
||||
|
||||
# Labels
|
||||
LABEL maintainer="ONVIF Library Team"
|
||||
LABEL description="Go ONVIF library with CLI tools"
|
||||
LABEL version="1.0.0"
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 ProtoTess
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 ProtoTess
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,220 +0,0 @@
|
||||
# ONVIF GO Library Makefile
|
||||
|
||||
.PHONY: all build test clean install deps lint fmt vet check examples cli docker
|
||||
|
||||
# Configuration
|
||||
BINARY_DIR := bin
|
||||
GOPATH := $(shell go env GOPATH)
|
||||
GOOS := $(shell go env GOOS)
|
||||
GOARCH := $(shell go env GOARCH)
|
||||
|
||||
# Binaries
|
||||
CLI_BINARY := $(BINARY_DIR)/onvif-cli
|
||||
QUICK_BINARY := $(BINARY_DIR)/onvif-quick
|
||||
|
||||
# Build all targets
|
||||
all: deps check test build
|
||||
|
||||
# Build all binaries
|
||||
build: $(CLI_BINARY) $(QUICK_BINARY)
|
||||
|
||||
# Build CLI tool (comprehensive)
|
||||
$(CLI_BINARY):
|
||||
@echo "🔨 Building ONVIF CLI..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
CGO_ENABLED=0 go build -o $(CLI_BINARY) ./cmd/onvif-cli
|
||||
|
||||
# Build quick tool (simple)
|
||||
$(QUICK_BINARY):
|
||||
@echo "🔨 Building ONVIF Quick Tool..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
CGO_ENABLED=0 go build -o $(QUICK_BINARY) ./cmd/onvif-quick
|
||||
|
||||
# Install binaries to GOPATH
|
||||
install: build
|
||||
@echo "📦 Installing binaries..."
|
||||
cp $(CLI_BINARY) $(GOPATH)/bin/
|
||||
cp $(QUICK_BINARY) $(GOPATH)/bin/
|
||||
|
||||
# Download dependencies
|
||||
deps:
|
||||
@echo "📥 Downloading dependencies..."
|
||||
go mod download
|
||||
go mod tidy
|
||||
|
||||
# Run tests
|
||||
test:
|
||||
@echo "🧪 Running tests..."
|
||||
go test -v -race -coverprofile=coverage.out ./...
|
||||
|
||||
# Run tests with coverage report
|
||||
test-coverage: test
|
||||
@echo "📊 Generating coverage report..."
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
@echo "Coverage report: coverage.html"
|
||||
|
||||
# Run benchmarks
|
||||
bench:
|
||||
@echo "⚡ Running benchmarks..."
|
||||
go test -bench=. -benchmem ./...
|
||||
|
||||
# Lint code
|
||||
lint:
|
||||
@echo "🔍 Linting code..."
|
||||
@if command -v golangci-lint >/dev/null 2>&1; then \
|
||||
golangci-lint run ./...; \
|
||||
else \
|
||||
echo "⚠️ golangci-lint not installed. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
|
||||
fi
|
||||
|
||||
# Format code
|
||||
fmt:
|
||||
@echo "🎨 Formatting code..."
|
||||
go fmt ./...
|
||||
|
||||
# Vet code
|
||||
vet:
|
||||
@echo "🔬 Vetting code..."
|
||||
go vet ./...
|
||||
|
||||
# Run all checks
|
||||
check: fmt vet lint
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
@echo "🧹 Cleaning..."
|
||||
rm -rf $(BINARY_DIR)
|
||||
rm -f coverage.out coverage.html
|
||||
|
||||
# Build examples
|
||||
examples:
|
||||
@echo "📚 Building examples..."
|
||||
@mkdir -p $(BINARY_DIR)/examples
|
||||
go build -o $(BINARY_DIR)/examples/discovery ./examples/discovery
|
||||
go build -o $(BINARY_DIR)/examples/device_info ./examples/device_info
|
||||
go build -o $(BINARY_DIR)/examples/media ./examples/media
|
||||
go build -o $(BINARY_DIR)/examples/ptz ./examples/ptz
|
||||
|
||||
# Build for multiple platforms
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
||||
LDFLAGS := -ldflags "-s -w -X main.Version=$(VERSION)"
|
||||
|
||||
build-all:
|
||||
@echo "🌍 Building for multiple platforms (version: $(VERSION))..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
|
||||
# Linux AMD64
|
||||
@echo "Building Linux AMD64..."
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-amd64 ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-amd64 ./cmd/onvif-quick
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-linux-amd64 ./cmd/onvif-server
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-linux-amd64 ./cmd/onvif-diagnostics
|
||||
|
||||
# Linux ARM64
|
||||
@echo "Building Linux ARM64..."
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-arm64 ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-arm64 ./cmd/onvif-quick
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-linux-arm64 ./cmd/onvif-server
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-linux-arm64 ./cmd/onvif-diagnostics
|
||||
|
||||
# Linux ARM (32-bit)
|
||||
@echo "Building Linux ARM..."
|
||||
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-arm ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-arm ./cmd/onvif-quick
|
||||
|
||||
# Windows AMD64
|
||||
@echo "Building Windows AMD64..."
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-windows-amd64.exe ./cmd/onvif-cli
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-windows-amd64.exe ./cmd/onvif-quick
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-windows-amd64.exe ./cmd/onvif-server
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-windows-amd64.exe ./cmd/onvif-diagnostics
|
||||
|
||||
# Windows ARM64
|
||||
@echo "Building Windows ARM64..."
|
||||
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-windows-arm64.exe ./cmd/onvif-cli
|
||||
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-windows-arm64.exe ./cmd/onvif-quick
|
||||
|
||||
# macOS AMD64 (Intel)
|
||||
@echo "Building macOS AMD64..."
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-darwin-amd64 ./cmd/onvif-cli
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-darwin-amd64 ./cmd/onvif-quick
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-darwin-amd64 ./cmd/onvif-server
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-darwin-amd64 ./cmd/onvif-diagnostics
|
||||
|
||||
# macOS ARM64 (Apple Silicon)
|
||||
@echo "Building macOS ARM64..."
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-darwin-arm64 ./cmd/onvif-cli
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-darwin-arm64 ./cmd/onvif-quick
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-darwin-arm64 ./cmd/onvif-server
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-darwin-arm64 ./cmd/onvif-diagnostics
|
||||
|
||||
@echo "✅ All binaries built successfully in $(BINARY_DIR)/"
|
||||
@echo ""
|
||||
@ls -lh $(BINARY_DIR)/
|
||||
|
||||
# Create release archives with checksums
|
||||
release: build-all
|
||||
@echo "📦 Creating release archives..."
|
||||
@mkdir -p releases
|
||||
|
||||
# Create archives for each platform
|
||||
@cd $(BINARY_DIR) && \
|
||||
for os in linux darwin windows; do \
|
||||
for arch in amd64 arm64 arm; do \
|
||||
if [ "$$os" = "windows" ] && [ "$$arch" != "arm" ]; then \
|
||||
if [ -f onvif-cli-$$os-$$arch.exe ]; then \
|
||||
zip -j ../releases/onvif-go-$(VERSION)-$$os-$$arch.zip onvif-*-$$os-$$arch.exe ../README.md ../LICENSE 2>/dev/null || true; \
|
||||
fi; \
|
||||
elif [ "$$os" != "windows" ]; then \
|
||||
if [ -f onvif-cli-$$os-$$arch ]; then \
|
||||
tar czf ../releases/onvif-go-$(VERSION)-$$os-$$arch.tar.gz onvif-*-$$os-$$arch ../README.md ../LICENSE 2>/dev/null || true; \
|
||||
fi; \
|
||||
fi; \
|
||||
done; \
|
||||
done
|
||||
|
||||
# Generate checksums
|
||||
@cd releases && sha256sum * > checksums.txt 2>/dev/null || shasum -a 256 * > checksums.txt
|
||||
@echo "✅ Release archives created in releases/"
|
||||
@ls -lh releases/
|
||||
|
||||
# Create Docker image
|
||||
docker:
|
||||
@echo "🐳 Building Docker image..."
|
||||
docker build -t onvif-go:latest .
|
||||
|
||||
# Development setup
|
||||
dev-setup:
|
||||
@echo "🛠️ Setting up development environment..."
|
||||
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
go install golang.org/x/tools/cmd/goimports@latest
|
||||
go mod download
|
||||
|
||||
# Run quick tool
|
||||
run-quick:
|
||||
@if [ ! -f $(QUICK_BINARY) ]; then $(MAKE) $(QUICK_BINARY); fi
|
||||
$(QUICK_BINARY)
|
||||
|
||||
# Run CLI tool
|
||||
run-cli:
|
||||
@if [ ! -f $(CLI_BINARY) ]; then $(MAKE) $(CLI_BINARY); fi
|
||||
$(CLI_BINARY)
|
||||
|
||||
# Show help
|
||||
help:
|
||||
@echo "📖 Available targets:"
|
||||
@echo " all - Build, test, and check everything"
|
||||
@echo " build - Build both CLI tools"
|
||||
@echo " test - Run tests"
|
||||
@echo " test-coverage- Run tests with coverage report"
|
||||
@echo " bench - Run benchmarks"
|
||||
@echo " check - Run fmt, vet, and lint"
|
||||
@echo " clean - Clean build artifacts"
|
||||
@echo " install - Install binaries to GOPATH"
|
||||
@echo " examples - Build example programs"
|
||||
@echo " build-all - Build for multiple platforms"
|
||||
@echo " docker - Build Docker image"
|
||||
@echo " dev-setup - Set up development environment"
|
||||
@echo " run-quick - Run the quick tool"
|
||||
@echo " run-cli - Run the comprehensive CLI"
|
||||
@echo " help - Show this help"
|
||||
@@ -1,220 +0,0 @@
|
||||
# ONVIF GO Library Makefile
|
||||
|
||||
.PHONY: all build test clean install deps lint fmt vet check examples cli docker
|
||||
|
||||
# Configuration
|
||||
BINARY_DIR := bin
|
||||
GOPATH := $(shell go env GOPATH)
|
||||
GOOS := $(shell go env GOOS)
|
||||
GOARCH := $(shell go env GOARCH)
|
||||
|
||||
# Binaries
|
||||
CLI_BINARY := $(BINARY_DIR)/onvif-cli
|
||||
QUICK_BINARY := $(BINARY_DIR)/onvif-quick
|
||||
|
||||
# Build all targets
|
||||
all: deps check test build
|
||||
|
||||
# Build all binaries
|
||||
build: $(CLI_BINARY) $(QUICK_BINARY)
|
||||
|
||||
# Build CLI tool (comprehensive)
|
||||
$(CLI_BINARY):
|
||||
@echo "🔨 Building ONVIF CLI..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
CGO_ENABLED=0 go build -o $(CLI_BINARY) ./cmd/onvif-cli
|
||||
|
||||
# Build quick tool (simple)
|
||||
$(QUICK_BINARY):
|
||||
@echo "🔨 Building ONVIF Quick Tool..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
CGO_ENABLED=0 go build -o $(QUICK_BINARY) ./cmd/onvif-quick
|
||||
|
||||
# Install binaries to GOPATH
|
||||
install: build
|
||||
@echo "📦 Installing binaries..."
|
||||
cp $(CLI_BINARY) $(GOPATH)/bin/
|
||||
cp $(QUICK_BINARY) $(GOPATH)/bin/
|
||||
|
||||
# Download dependencies
|
||||
deps:
|
||||
@echo "📥 Downloading dependencies..."
|
||||
go mod download
|
||||
go mod tidy
|
||||
|
||||
# Run tests
|
||||
test:
|
||||
@echo "🧪 Running tests..."
|
||||
go test -v -race -coverprofile=coverage.out ./...
|
||||
|
||||
# Run tests with coverage report
|
||||
test-coverage: test
|
||||
@echo "📊 Generating coverage report..."
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
@echo "Coverage report: coverage.html"
|
||||
|
||||
# Run benchmarks
|
||||
bench:
|
||||
@echo "⚡ Running benchmarks..."
|
||||
go test -bench=. -benchmem ./...
|
||||
|
||||
# Lint code
|
||||
lint:
|
||||
@echo "🔍 Linting code..."
|
||||
@if command -v golangci-lint >/dev/null 2>&1; then \
|
||||
golangci-lint run ./...; \
|
||||
else \
|
||||
echo "⚠️ golangci-lint not installed. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
|
||||
fi
|
||||
|
||||
# Format code
|
||||
fmt:
|
||||
@echo "🎨 Formatting code..."
|
||||
go fmt ./...
|
||||
|
||||
# Vet code
|
||||
vet:
|
||||
@echo "🔬 Vetting code..."
|
||||
go vet ./...
|
||||
|
||||
# Run all checks
|
||||
check: fmt vet lint
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
@echo "🧹 Cleaning..."
|
||||
rm -rf $(BINARY_DIR)
|
||||
rm -f coverage.out coverage.html
|
||||
|
||||
# Build examples
|
||||
examples:
|
||||
@echo "📚 Building examples..."
|
||||
@mkdir -p $(BINARY_DIR)/examples
|
||||
go build -o $(BINARY_DIR)/examples/discovery ./examples/discovery
|
||||
go build -o $(BINARY_DIR)/examples/device_info ./examples/device_info
|
||||
go build -o $(BINARY_DIR)/examples/media ./examples/media
|
||||
go build -o $(BINARY_DIR)/examples/ptz ./examples/ptz
|
||||
|
||||
# Build for multiple platforms
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
||||
LDFLAGS := -ldflags "-s -w -X main.Version=$(VERSION)"
|
||||
|
||||
build-all:
|
||||
@echo "🌍 Building for multiple platforms (version: $(VERSION))..."
|
||||
@mkdir -p $(BINARY_DIR)
|
||||
|
||||
# Linux AMD64
|
||||
@echo "Building Linux AMD64..."
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-amd64 ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-amd64 ./cmd/onvif-quick
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-linux-amd64 ./cmd/onvif-server
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-linux-amd64 ./cmd/onvif-diagnostics
|
||||
|
||||
# Linux ARM64
|
||||
@echo "Building Linux ARM64..."
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-arm64 ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-arm64 ./cmd/onvif-quick
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-linux-arm64 ./cmd/onvif-server
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-linux-arm64 ./cmd/onvif-diagnostics
|
||||
|
||||
# Linux ARM (32-bit)
|
||||
@echo "Building Linux ARM..."
|
||||
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-linux-arm ./cmd/onvif-cli
|
||||
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-linux-arm ./cmd/onvif-quick
|
||||
|
||||
# Windows AMD64
|
||||
@echo "Building Windows AMD64..."
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-windows-amd64.exe ./cmd/onvif-cli
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-windows-amd64.exe ./cmd/onvif-quick
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-windows-amd64.exe ./cmd/onvif-server
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-windows-amd64.exe ./cmd/onvif-diagnostics
|
||||
|
||||
# Windows ARM64
|
||||
@echo "Building Windows ARM64..."
|
||||
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-windows-arm64.exe ./cmd/onvif-cli
|
||||
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-windows-arm64.exe ./cmd/onvif-quick
|
||||
|
||||
# macOS AMD64 (Intel)
|
||||
@echo "Building macOS AMD64..."
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-darwin-amd64 ./cmd/onvif-cli
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-darwin-amd64 ./cmd/onvif-quick
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-darwin-amd64 ./cmd/onvif-server
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-darwin-amd64 ./cmd/onvif-diagnostics
|
||||
|
||||
# macOS ARM64 (Apple Silicon)
|
||||
@echo "Building macOS ARM64..."
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-cli-darwin-arm64 ./cmd/onvif-cli
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-quick-darwin-arm64 ./cmd/onvif-quick
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-server-darwin-arm64 ./cmd/onvif-server
|
||||
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BINARY_DIR)/onvif-diagnostics-darwin-arm64 ./cmd/onvif-diagnostics
|
||||
|
||||
@echo "✅ All binaries built successfully in $(BINARY_DIR)/"
|
||||
@echo ""
|
||||
@ls -lh $(BINARY_DIR)/
|
||||
|
||||
# Create release archives with checksums
|
||||
release: build-all
|
||||
@echo "📦 Creating release archives..."
|
||||
@mkdir -p releases
|
||||
|
||||
# Create archives for each platform
|
||||
@cd $(BINARY_DIR) && \
|
||||
for os in linux darwin windows; do \
|
||||
for arch in amd64 arm64 arm; do \
|
||||
if [ "$$os" = "windows" ] && [ "$$arch" != "arm" ]; then \
|
||||
if [ -f onvif-cli-$$os-$$arch.exe ]; then \
|
||||
zip -j ../releases/onvif-go-$(VERSION)-$$os-$$arch.zip onvif-*-$$os-$$arch.exe ../README.md ../LICENSE 2>/dev/null || true; \
|
||||
fi; \
|
||||
elif [ "$$os" != "windows" ]; then \
|
||||
if [ -f onvif-cli-$$os-$$arch ]; then \
|
||||
tar czf ../releases/onvif-go-$(VERSION)-$$os-$$arch.tar.gz onvif-*-$$os-$$arch ../README.md ../LICENSE 2>/dev/null || true; \
|
||||
fi; \
|
||||
fi; \
|
||||
done; \
|
||||
done
|
||||
|
||||
# Generate checksums
|
||||
@cd releases && sha256sum * > checksums.txt 2>/dev/null || shasum -a 256 * > checksums.txt
|
||||
@echo "✅ Release archives created in releases/"
|
||||
@ls -lh releases/
|
||||
|
||||
# Create Docker image
|
||||
docker:
|
||||
@echo "🐳 Building Docker image..."
|
||||
docker build -t onvif-go:latest .
|
||||
|
||||
# Development setup
|
||||
dev-setup:
|
||||
@echo "🛠️ Setting up development environment..."
|
||||
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
go install golang.org/x/tools/cmd/goimports@latest
|
||||
go mod download
|
||||
|
||||
# Run quick tool
|
||||
run-quick:
|
||||
@if [ ! -f $(QUICK_BINARY) ]; then $(MAKE) $(QUICK_BINARY); fi
|
||||
$(QUICK_BINARY)
|
||||
|
||||
# Run CLI tool
|
||||
run-cli:
|
||||
@if [ ! -f $(CLI_BINARY) ]; then $(MAKE) $(CLI_BINARY); fi
|
||||
$(CLI_BINARY)
|
||||
|
||||
# Show help
|
||||
help:
|
||||
@echo "📖 Available targets:"
|
||||
@echo " all - Build, test, and check everything"
|
||||
@echo " build - Build both CLI tools"
|
||||
@echo " test - Run tests"
|
||||
@echo " test-coverage- Run tests with coverage report"
|
||||
@echo " bench - Run benchmarks"
|
||||
@echo " check - Run fmt, vet, and lint"
|
||||
@echo " clean - Clean build artifacts"
|
||||
@echo " install - Install binaries to GOPATH"
|
||||
@echo " examples - Build example programs"
|
||||
@echo " build-all - Build for multiple platforms"
|
||||
@echo " docker - Build Docker image"
|
||||
@echo " dev-setup - Set up development environment"
|
||||
@echo " run-quick - Run the quick tool"
|
||||
@echo " run-cli - Run the comprehensive CLI"
|
||||
@echo " help - Show this help"
|
||||
@@ -1,944 +0,0 @@
|
||||
# onvif-go - ONVIF Client and Server Library for Go
|
||||
|
||||
[](https://pkg.go.dev/github.com/0x524a/onvif-go)
|
||||
[](https://goreportcard.com/report/github.com/0x524a/onvif-go)
|
||||
[](https://codecov.io/gh/0x524a/onvif-go)
|
||||
[](https://sonarcloud.io/summary/new_code?id=0x524a_onvif-go)
|
||||
[](LICENSE)
|
||||
[](https://github.com/0x524a/onvif-go/stargazers)
|
||||
[](https://github.com/0x524a/onvif-go/issues)
|
||||
|
||||
> **Modern, high-performance Go library for ONVIF IP camera integration** - Control surveillance cameras, NVRs, and video devices with comprehensive ONVIF Profile S/T/G support. Includes both client and server implementations for complete ONVIF camera simulation and testing.
|
||||
|
||||
A production-ready, feature-rich Go (Golang) library for communicating with ONVIF-compliant IP cameras, network video recorders (NVR), and surveillance devices. Perfect for building video management systems (VMS), security camera applications, IoT projects, and camera testing frameworks.
|
||||
|
||||
## 🎯 Key Features at a Glance
|
||||
|
||||
- ✅ **ONVIF Client & Server** - Both client library and virtual camera server
|
||||
- ✅ **Production Ready** - Battle-tested with multiple camera brands
|
||||
- ✅ **Full Protocol Support** - Device, Media, PTZ, Imaging, Discovery services
|
||||
- ✅ **Type Safe** - Comprehensive Go types for all ONVIF operations
|
||||
- ✅ **Well Documented** - Extensive examples and API documentation
|
||||
- ✅ **Camera Tested** - Verified with Hikvision, Axis, Dahua, Bosch cameras
|
||||
- ✅ **Testing Framework** - Built-in mock server and testing utilities
|
||||
|
||||
## 🔑 What is ONVIF?
|
||||
|
||||
ONVIF (Open Network Video Interface Forum) is an open industry standard for IP-based security products. This library allows you to:
|
||||
|
||||
- 🎥 Control IP cameras from any manufacturer (Bosch, Hikvision, Axis, Dahua, etc.)
|
||||
- 📹 Get RTSP video streams and snapshots
|
||||
- 🎮 Pan, tilt, and zoom cameras remotely
|
||||
- 🔧 Configure camera settings (exposure, focus, white balance)
|
||||
- 🔍 Discover cameras on your network automatically
|
||||
- 🧪 Test ONVIF implementations without physical hardware
|
||||
|
||||
## Features
|
||||
|
||||
### 📡 ONVIF Client
|
||||
|
||||
✨ **Modern Go Design**
|
||||
- Context support for cancellation and timeouts
|
||||
- Concurrent-safe operations
|
||||
- Type-safe API with comprehensive error handling
|
||||
- Connection pooling for optimal performance
|
||||
|
||||
🎥 **Comprehensive ONVIF Support**
|
||||
- **Device Management**: Get device info, capabilities, system date/time, reboot
|
||||
- **Media Services**: Profiles, stream URIs (RTSP/HTTP), snapshot URIs, encoder configuration
|
||||
- **PTZ Control**: Continuous, absolute, and relative movement, presets, status
|
||||
- **Imaging**: Get/set brightness, contrast, exposure, focus, white balance, WDR
|
||||
- **Discovery**: Automatic camera detection via WS-Discovery multicast
|
||||
|
||||
### 🎬 ONVIF Server (NEW!)
|
||||
|
||||
🎥 **Virtual IP Camera Simulator**
|
||||
- **Multi-Lens Camera Support**: Simulate up to 10 independent camera profiles
|
||||
- **Complete ONVIF Implementation**: Device, Media, PTZ, and Imaging services
|
||||
- **Flexible Configuration**: CLI and library interfaces for easy setup
|
||||
- **PTZ Simulation**: Full pan-tilt-zoom control with preset positions
|
||||
- **Imaging Control**: Brightness, contrast, exposure, focus, and more
|
||||
- **Testing & Development**: Perfect for testing ONVIF clients without physical cameras
|
||||
|
||||
🔐 **Security**
|
||||
- WS-Security with UsernameToken authentication
|
||||
- Password digest (SHA-1) support
|
||||
- Configurable timeout and HTTP client options
|
||||
|
||||
📦 **Easy Integration**
|
||||
- Simple, intuitive API
|
||||
- Well-documented with examples
|
||||
- No external dependencies beyond Go standard library and golang.org/x/net
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
go get github.com/0x524a/onvif-go
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Discover Cameras on Network
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go/discovery"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
devices, err := discovery.Discover(ctx, 5*time.Second)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, device := range devices {
|
||||
fmt.Printf("Found: %s at %s\n",
|
||||
device.GetName(),
|
||||
device.GetDeviceEndpoint())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Connect to a Camera
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create client - endpoint can be:
|
||||
// - Full URL: "http://192.168.1.100/onvif/device_service"
|
||||
// - IP with port: "192.168.1.100:8080"
|
||||
// - IP only: "192.168.1.100" (automatically adds http:// and path)
|
||||
client, err := onvif.NewClient(
|
||||
"192.168.1.100", // Simple IP address
|
||||
onvif.WithCredentials("admin", "password"),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Get device information
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Camera: %s %s\n", info.Manufacturer, info.Model)
|
||||
fmt.Printf("Firmware: %s\n", info.FirmwareVersion)
|
||||
|
||||
// Initialize and discover service endpoints
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get media profiles
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get stream URI
|
||||
if len(profiles) > 0 {
|
||||
streamURI, err := client.GetStreamURI(ctx, profiles[0].Token)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Stream URI: %s\n", streamURI.URI)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### PTZ Control
|
||||
|
||||
```go
|
||||
// Continuous movement
|
||||
velocity := &onvif.PTZSpeed{
|
||||
PanTilt: &onvif.Vector2D{X: 0.5, Y: 0.0}, // Move right
|
||||
}
|
||||
timeout := "PT2S" // 2 seconds
|
||||
err := client.ContinuousMove(ctx, profileToken, velocity, &timeout)
|
||||
|
||||
// Stop movement
|
||||
err = client.Stop(ctx, profileToken, true, true)
|
||||
|
||||
// Absolute positioning
|
||||
position := &onvif.PTZVector{
|
||||
PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0}, // Center
|
||||
Zoom: &onvif.Vector1D{X: 0.5}, // 50% zoom
|
||||
}
|
||||
err = client.AbsoluteMove(ctx, profileToken, position, nil)
|
||||
|
||||
// Go to preset
|
||||
presets, err := client.GetPresets(ctx, profileToken)
|
||||
if len(presets) > 0 {
|
||||
err = client.GotoPreset(ctx, profileToken, presets[0].Token, nil)
|
||||
}
|
||||
```
|
||||
|
||||
### Imaging Settings
|
||||
|
||||
```go
|
||||
// Get current settings
|
||||
settings, err := client.GetImagingSettings(ctx, videoSourceToken)
|
||||
|
||||
// Modify settings
|
||||
brightness := 60.0
|
||||
settings.Brightness = &brightness
|
||||
|
||||
contrast := 55.0
|
||||
settings.Contrast = &contrast
|
||||
|
||||
// Apply settings
|
||||
err = client.SetImagingSettings(ctx, videoSourceToken, settings, true)
|
||||
```
|
||||
|
||||
## API Overview
|
||||
|
||||
### API Coverage Summary
|
||||
|
||||
The onvif-go library provides comprehensive ONVIF protocol support with **200+ implemented APIs** across all major ONVIF services:
|
||||
|
||||
- **Device Management**: 98 APIs (100% complete) ✅
|
||||
- **Media Service**: 14+ APIs (profiles, streams, encoding) ✅
|
||||
- **PTZ Service**: 13 APIs (movement, presets, status) ✅
|
||||
- **Imaging Service**: 7 APIs (brightness, contrast, focus control) ✅
|
||||
- **Discovery Service**: WS-Discovery network scanning ✅
|
||||
|
||||
### Client Creation
|
||||
|
||||
```go
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
onvif.WithHTTPClient(customHTTPClient),
|
||||
)
|
||||
```
|
||||
|
||||
### Device Service (98 APIs) - 100% Complete ✅
|
||||
|
||||
The Device Service provides comprehensive device management capabilities with **98 fully implemented APIs**:
|
||||
|
||||
#### Core Device Information
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDeviceInformation()` | Get manufacturer, model, firmware version, serial number, hardware ID |
|
||||
| `GetCapabilities()` | Get device capabilities and service endpoints (device, media, imaging, PTZ, events, etc.) |
|
||||
| `GetServices()` | Get list of services with optional capabilities |
|
||||
| `GetServiceCapabilities()` | Get device service-specific capabilities |
|
||||
| `GetEndpointReference()` | Get device's WS-Addressing endpoint reference |
|
||||
| `SystemReboot()` | Reboot the device |
|
||||
| `Initialize()` | Discover and cache service endpoints |
|
||||
|
||||
#### Hostname & Network Discovery
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetHostname()` | Get device hostname configuration |
|
||||
| `SetHostname()` | Set device hostname |
|
||||
| `SetHostnameFromDHCP()` | Enable/disable hostname from DHCP |
|
||||
| `GetScopes()` | Get configured WS-Discovery scopes |
|
||||
| `SetScopes()` | Set WS-Discovery scopes |
|
||||
| `AddScopes()` | Add WS-Discovery scopes |
|
||||
| `RemoveScopes()` | Remove WS-Discovery scopes |
|
||||
|
||||
#### DNS Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDNS()` | Get DNS configuration (DHCP and manual DNS servers) |
|
||||
| `SetDNS()` | Set DNS configuration (from DHCP, search domains, DNS servers) |
|
||||
|
||||
#### NTP Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetNTP()` | Get NTP configuration (DHCP and manual NTP servers) |
|
||||
| `SetNTP()` | Set NTP configuration (from DHCP, NTP servers) |
|
||||
|
||||
#### Dynamic DNS
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDynamicDNS()` | Get Dynamic DNS configuration |
|
||||
| `SetDynamicDNS()` | Set Dynamic DNS with type and name |
|
||||
|
||||
#### System Date & Time
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetSystemDateAndTime()` | Get device system date and time (interface{}) |
|
||||
| `FixedGetSystemDateAndTime()` | Get properly typed system date and time with timezone support |
|
||||
| `SetSystemDateAndTime()` | Set device system date and time with manual/NTP mode |
|
||||
|
||||
#### Network Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetNetworkInterfaces()` | Get all network interface configurations |
|
||||
| `GetNetworkProtocols()` | Get network protocol settings (HTTP, HTTPS, RTSP, RTMP, SSH, etc.) |
|
||||
| `SetNetworkProtocols()` | Set network protocol settings |
|
||||
| `GetNetworkDefaultGateway()` | Get default gateway configuration (IPv4 and IPv6) |
|
||||
| `SetNetworkDefaultGateway()` | Set default gateway configuration |
|
||||
| `GetZeroConfiguration()` | Get Zero Configuration (zeroconf/Bonjour) status |
|
||||
| `SetZeroConfiguration()` | Enable/disable Zero Configuration per interface |
|
||||
|
||||
#### User Management
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetUsers()` | Get list of user accounts and credentials |
|
||||
| `CreateUsers()` | Create new user accounts |
|
||||
| `SetUser()` | Modify existing user account |
|
||||
| `DeleteUsers()` | Delete user accounts |
|
||||
| `GetRemoteUser()` | Get remote user connection status |
|
||||
| `SetRemoteUser()` | Set remote user connection settings |
|
||||
|
||||
#### Security & Access Control
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetIPAddressFilter()` | Get IP address filter (allow/deny lists) |
|
||||
| `SetIPAddressFilter()` | Set IP address filtering rules |
|
||||
| `AddIPAddressFilter()` | Add IP addresses to filter list |
|
||||
| `RemoveIPAddressFilter()` | Remove IP addresses from filter list |
|
||||
| `GetPasswordComplexityConfiguration()` | Get password policy settings |
|
||||
| `SetPasswordComplexityConfiguration()` | Set password policy (length, uppercase, numbers, special chars) |
|
||||
| `GetPasswordHistoryConfiguration()` | Get password history requirements |
|
||||
| `SetPasswordHistoryConfiguration()` | Set password history and re-use prevention |
|
||||
| `GetAuthFailureWarningConfiguration()` | Get failed authentication warning settings |
|
||||
| `SetAuthFailureWarningConfiguration()` | Set failed authentication thresholds |
|
||||
|
||||
#### Discovery Modes
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDiscoveryMode()` | Get discovery mode (Discoverable/NonDiscoverable) |
|
||||
| `SetDiscoveryMode()` | Set discovery mode |
|
||||
| `GetRemoteDiscoveryMode()` | Get remote discovery mode |
|
||||
| `SetRemoteDiscoveryMode()` | Set remote discovery mode |
|
||||
|
||||
#### Certificate Management
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetCertificates()` | Get installed certificates |
|
||||
| `GetCACertificates()` | Get Certificate Authority certificates |
|
||||
| `LoadCertificates()` | Load/install certificates |
|
||||
| `LoadCACertificates()` | Load/install CA certificates |
|
||||
| `CreateCertificate()` | Create self-signed certificate |
|
||||
| `DeleteCertificates()` | Delete certificates |
|
||||
| `GetCertificateInformation()` | Get certificate details and validity |
|
||||
| `GetCertificatesStatus()` | Get certificate usage status |
|
||||
| `SetCertificatesStatus()` | Set certificate usage (enabled/disabled) |
|
||||
| `GetPkcs10Request()` | Generate PKCS#10 certificate signing request |
|
||||
| `LoadCertificateWithPrivateKey()` | Load certificate with private key |
|
||||
| `GetClientCertificateMode()` | Check if client certificate authentication enabled |
|
||||
| `SetClientCertificateMode()` | Enable/disable client certificate authentication |
|
||||
|
||||
#### WiFi/802.11 Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDot11Capabilities()` | Get WiFi capabilities (cipher suites, auth modes) |
|
||||
| `GetDot11Status()` | Get WiFi status (SSID, signal strength, link quality) |
|
||||
| `GetDot1XConfiguration()` | Get 802.1X EAP configuration |
|
||||
| `GetDot1XConfigurations()` | Get all 802.1X configurations |
|
||||
| `SetDot1XConfiguration()` | Set 802.1X configuration |
|
||||
| `CreateDot1XConfiguration()` | Create new 802.1X configuration |
|
||||
| `DeleteDot1XConfiguration()` | Delete 802.1X configuration |
|
||||
| `ScanAvailableDot11Networks()` | Scan for available WiFi networks |
|
||||
|
||||
#### Storage Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetStorageConfigurations()` | Get all storage configurations |
|
||||
| `GetStorageConfiguration()` | Get specific storage configuration |
|
||||
| `CreateStorageConfiguration()` | Create new storage configuration |
|
||||
| `SetStorageConfiguration()` | Update storage configuration |
|
||||
| `DeleteStorageConfiguration()` | Delete storage configuration |
|
||||
| `SetHashingAlgorithm()` | Set password hashing algorithm |
|
||||
|
||||
#### System Maintenance & Logs
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetSystemLog()` | Get system logs (boot, security, etc.) |
|
||||
| `GetSystemBackup()` | Get available system backups |
|
||||
| `RestoreSystem()` | Restore from backup file |
|
||||
| `GetSystemUris()` | Get system log and backup URIs |
|
||||
| `GetSystemSupportInformation()` | Get support information and system details |
|
||||
| `SetSystemFactoryDefault()` | Reset device to factory defaults |
|
||||
| `StartFirmwareUpgrade()` | Initiate firmware upgrade |
|
||||
| `StartSystemRestore()` | Initiate system restore |
|
||||
|
||||
#### Relay & Auxiliary I/O
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetRelayOutputs()` | Get relay outputs and their current state |
|
||||
| `SetRelayOutputSettings()` | Configure relay output behavior |
|
||||
| `SetRelayOutputState()` | Set relay output state (active/inactive) |
|
||||
| `SendAuxiliaryCommand()` | Send auxiliary commands (e.g., IR control) |
|
||||
|
||||
#### Additional Features
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetGeoLocation()` | Get device geographic location |
|
||||
| `SetGeoLocation()` | Set device geographic location |
|
||||
| `DeleteGeoLocation()` | Delete geographic location |
|
||||
| `GetDPAddresses()` | Get WS-Discovery multicast addresses |
|
||||
| `SetDPAddresses()` | Set WS-Discovery multicast addresses |
|
||||
| `GetAccessPolicy()` | Get device access policy |
|
||||
| `SetAccessPolicy()` | Set device access policy |
|
||||
| `GetWsdlUrl()` | Get device WSDL URL (deprecated) |
|
||||
|
||||
## 🔧 Device Management Features
|
||||
|
||||
The onvif-go library provides **98 fully-implemented Device Management APIs** for complete device configuration and control. See [DEVICE_API_STATUS.md](DEVICE_API_STATUS.md) for the complete API reference.
|
||||
|
||||
### Common Device Management Use Cases
|
||||
|
||||
#### Query Device Information
|
||||
```go
|
||||
// Get device info (manufacturer, model, firmware)
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Camera: %s %s (FW: %s)\n", info.Manufacturer, info.Model, info.FirmwareVersion)
|
||||
|
||||
// Get capabilities
|
||||
caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
|
||||
#### Network Configuration
|
||||
```go
|
||||
// Get all network interfaces
|
||||
interfaces, err := client.GetNetworkInterfaces(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get DNS and NTP settings
|
||||
dns, err := client.GetDNS(ctx)
|
||||
ntp, err := client.GetNTP(ctx)
|
||||
|
||||
// Configure DNS
|
||||
err = client.SetDNS(ctx, false, []string{"example.com"}, []onvif.IPAddress{
|
||||
{Type: "IPv4", IPv4Address: "8.8.8.8"},
|
||||
})
|
||||
|
||||
// Get/Set hostname
|
||||
hostname, err := client.GetHostname(ctx)
|
||||
err = client.SetHostname(ctx, "new-camera-name")
|
||||
```
|
||||
|
||||
#### User & Security Management
|
||||
```go
|
||||
// Get users
|
||||
users, err := client.GetUsers(ctx)
|
||||
|
||||
// Create new user
|
||||
err = client.CreateUsers(ctx, []*onvif.User{
|
||||
{Username: "operator", Password: "pass123"},
|
||||
})
|
||||
|
||||
// Configure security
|
||||
err = client.SetPasswordComplexityConfiguration(ctx, &onvif.PasswordComplexityConfiguration{
|
||||
MinLen: 8,
|
||||
Uppercase: 1,
|
||||
Number: 1,
|
||||
SpecialChars: 1,
|
||||
})
|
||||
|
||||
// IP address filtering
|
||||
filter := &onvif.IPAddressFilter{
|
||||
Type: onvif.IPAddressFilterAllow,
|
||||
}
|
||||
err = client.SetIPAddressFilter(ctx, filter)
|
||||
```
|
||||
|
||||
#### Certificate Management
|
||||
```go
|
||||
// Get installed certificates
|
||||
certs, err := client.GetCertificates(ctx)
|
||||
|
||||
// Create self-signed certificate
|
||||
cert, err := client.CreateCertificate(ctx,
|
||||
"cert1",
|
||||
"CN=camera.example.com",
|
||||
"2024-01-01T00:00:00Z",
|
||||
"2025-01-01T00:00:00Z",
|
||||
)
|
||||
|
||||
// Check certificate status
|
||||
status, err := client.GetCertificatesStatus(ctx)
|
||||
|
||||
// Enable client certificate authentication
|
||||
err = client.SetClientCertificateMode(ctx, true)
|
||||
```
|
||||
|
||||
#### System Maintenance
|
||||
```go
|
||||
// Get system logs
|
||||
log, err := client.GetSystemLog(ctx, onvif.SystemLogTypeBoot)
|
||||
|
||||
// Get system backup
|
||||
backups, err := client.GetSystemBackup(ctx)
|
||||
|
||||
// Reboot device
|
||||
rebootToken, err := client.SystemReboot(ctx)
|
||||
|
||||
// Set factory defaults
|
||||
err = client.SetSystemFactoryDefault(ctx, onvif.FactoryDefaultTypeSoft)
|
||||
|
||||
// Firmware upgrade
|
||||
upgradeToken, err := client.StartFirmwareUpgrade(ctx)
|
||||
```
|
||||
|
||||
#### WiFi Configuration (802.11/802.1X)
|
||||
```go
|
||||
// Get WiFi capabilities
|
||||
caps, err := client.GetDot11Capabilities(ctx)
|
||||
|
||||
// Scan available networks
|
||||
networks, err := client.ScanAvailableDot11Networks(ctx, "interface1")
|
||||
|
||||
// Get 802.1X configuration
|
||||
config, err := client.GetDot1XConfiguration(ctx, "config1")
|
||||
|
||||
// Set 802.1X
|
||||
err = client.SetDot1XConfiguration(ctx, config)
|
||||
```
|
||||
|
||||
#### Relay & I/O Control
|
||||
```go
|
||||
// Get relay outputs
|
||||
relays, err := client.GetRelayOutputs(ctx)
|
||||
|
||||
// Control relay state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", onvif.RelayLogicalStateActive)
|
||||
err = client.SetRelayOutputState(ctx, "relay1", onvif.RelayLogicalStateInactive)
|
||||
|
||||
// Send auxiliary commands (e.g., IR control)
|
||||
response, err := client.SendAuxiliaryCommand(ctx, "tt:IRLamp|On")
|
||||
```
|
||||
|
||||
### Full API Reference
|
||||
|
||||
For complete documentation of all 98 Device Management APIs with detailed descriptions, parameters, and return types, see:
|
||||
- **[DEVICE_API_STATUS.md](DEVICE_API_STATUS.md)** - Complete API listing with categories and examples
|
||||
|
||||
### Media Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetProfiles()` | Get all media profiles |
|
||||
| `GetStreamURI()` | Get RTSP/HTTP stream URI |
|
||||
| `GetSnapshotURI()` | Get snapshot image URI |
|
||||
| `GetVideoEncoderConfiguration()` | Get video encoder settings |
|
||||
| `GetVideoSources()` | Get all video sources |
|
||||
| `GetAudioSources()` | Get all audio sources |
|
||||
| `GetAudioOutputs()` | Get all audio outputs |
|
||||
| `CreateProfile()` | Create new media profile |
|
||||
| `DeleteProfile()` | Delete media profile |
|
||||
| `SetVideoEncoderConfiguration()` | Set video encoder configuration |
|
||||
|
||||
### PTZ Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `ContinuousMove()` | Start continuous PTZ movement |
|
||||
| `AbsoluteMove()` | Move to absolute position |
|
||||
| `RelativeMove()` | Move relative to current position |
|
||||
| `Stop()` | Stop PTZ movement |
|
||||
| `GetStatus()` | Get current PTZ status and position |
|
||||
| `GetPresets()` | Get list of PTZ presets |
|
||||
| `GotoPreset()` | Move to a preset position |
|
||||
| `SetPreset()` | Save current position as preset |
|
||||
| `RemovePreset()` | Delete a preset |
|
||||
| `GotoHomePosition()` | Move to home position |
|
||||
| `SetHomePosition()` | Set current position as home |
|
||||
| `GetConfiguration()` | Get PTZ configuration |
|
||||
| `GetConfigurations()` | Get all PTZ configurations |
|
||||
|
||||
### Imaging Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetImagingSettings()` | Get imaging settings (brightness, contrast, etc.) |
|
||||
| `SetImagingSettings()` | Set imaging settings |
|
||||
| `Move()` | Perform focus move operations |
|
||||
| `GetOptions()` | Get available imaging options and ranges |
|
||||
| `GetMoveOptions()` | Get available focus move options |
|
||||
| `StopFocus()` | Stop focus movement |
|
||||
| `GetImagingStatus()` | Get current imaging/focus status |
|
||||
|
||||
### Discovery Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `Discover()` | Discover ONVIF devices on network |
|
||||
|
||||
## ONVIF Server
|
||||
|
||||
The library now includes a complete ONVIF server implementation that simulates multi-lens IP cameras!
|
||||
|
||||
### Quick Start
|
||||
|
||||
```bash
|
||||
# Install the server CLI
|
||||
go install ./cmd/onvif-server
|
||||
|
||||
# Run with default settings (3 camera profiles)
|
||||
onvif-server
|
||||
|
||||
# Or customize
|
||||
onvif-server -profiles 5 -username admin -password mypass -port 9000
|
||||
```
|
||||
|
||||
### Using the Server Library
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/0x524a/onvif-go/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create server with default multi-lens camera configuration
|
||||
srv, err := server.New(server.DefaultConfig())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Start server
|
||||
ctx := context.Background()
|
||||
if err := srv.Start(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Server Features
|
||||
|
||||
- 🎥 **Multi-Lens Simulation**: Support for up to 10 independent camera profiles
|
||||
- 🎮 **Full PTZ Control**: Pan, tilt, zoom with preset positions
|
||||
- 📷 **Imaging Settings**: Brightness, contrast, exposure, focus, white balance
|
||||
- 🌐 **Complete ONVIF Services**: Device, Media, PTZ, and Imaging services
|
||||
- 🔐 **WS-Security**: Digest authentication support
|
||||
- ⚙️ **Flexible Configuration**: CLI and library interfaces
|
||||
|
||||
### Use Cases
|
||||
|
||||
- Testing ONVIF client implementations
|
||||
- Developing video management systems
|
||||
- CI/CD integration testing
|
||||
- Demonstrations without physical cameras
|
||||
- Learning ONVIF protocol
|
||||
|
||||
For complete documentation, see [server/README.md](server/README.md).
|
||||
|
||||
## Examples
|
||||
|
||||
The [examples](examples/) directory contains complete working examples:
|
||||
|
||||
### Client Examples
|
||||
- **[discovery](examples/discovery/)**: Discover cameras on the network
|
||||
- **[device-info](examples/device-info/)**: Get device information and media profiles
|
||||
- **[ptz-control](examples/ptz-control/)**: Control camera PTZ (pan, tilt, zoom)
|
||||
- **[imaging-settings](examples/imaging-settings/)**: Adjust imaging settings
|
||||
|
||||
### Server Examples
|
||||
- **[onvif-server](examples/onvif-server/)**: Multi-lens camera server with custom configuration
|
||||
|
||||
To run an example:
|
||||
|
||||
```bash
|
||||
cd examples/discovery
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── client.go # Main ONVIF client
|
||||
├── types.go # ONVIF data types
|
||||
├── errors.go # Error definitions
|
||||
├── device.go # Device service implementation
|
||||
├── media.go # Media service implementation
|
||||
├── ptz.go # PTZ service implementation
|
||||
├── imaging.go # Imaging service implementation
|
||||
├── soap/ # SOAP client with WS-Security
|
||||
│ └── soap.go
|
||||
├── discovery/ # WS-Discovery implementation
|
||||
│ └── discovery.go
|
||||
├── server/ # ONVIF server implementation
|
||||
│ ├── server.go # Main server
|
||||
│ ├── types.go # Server types and configuration
|
||||
│ ├── device.go # Device service handlers
|
||||
│ ├── media.go # Media service handlers
|
||||
│ ├── ptz.go # PTZ service handlers
|
||||
│ ├── imaging.go # Imaging service handlers
|
||||
│ └── soap/ # SOAP server handler
|
||||
│ └── handler.go
|
||||
├── cmd/
|
||||
│ ├── onvif-cli/ # Client CLI tool
|
||||
│ └── onvif-server/ # Server CLI tool
|
||||
└── examples/ # Usage examples
|
||||
├── discovery/
|
||||
├── device-info/
|
||||
├── ptz-control/
|
||||
├── imaging-settings/
|
||||
└── onvif-server/ # Multi-lens camera server example
|
||||
```
|
||||
|
||||
## Design Principles
|
||||
|
||||
1. **Context-Aware**: All network operations accept `context.Context` for cancellation and timeouts
|
||||
2. **Type Safety**: Strong typing with comprehensive struct definitions
|
||||
3. **Error Handling**: Typed errors with clear error messages
|
||||
4. **Concurrency Safe**: Thread-safe operations with proper locking
|
||||
5. **Performance**: Connection pooling and efficient HTTP client reuse
|
||||
6. **Standards Compliant**: Follows ONVIF specifications for SOAP/XML messaging
|
||||
|
||||
## Compatibility
|
||||
|
||||
- **Go Version**: 1.21+
|
||||
- **ONVIF Versions**: Compatible with ONVIF Profile S, Profile T, Profile G
|
||||
- **Tested Cameras**: Works with most ONVIF-compliant IP cameras including:
|
||||
- Axis
|
||||
- Hikvision
|
||||
- Dahua
|
||||
- Bosch
|
||||
- Hanwha (Samsung)
|
||||
- And many others
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
# Run tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run tests with race detection
|
||||
go test -race ./...
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
||||
|
||||
1. Fork the repository
|
||||
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
||||
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
||||
4. Push to the branch (`git push origin feature/amazing-feature`)
|
||||
5. Open a Pull Request
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [ ] Event service implementation
|
||||
- [ ] Analytics service implementation
|
||||
- [ ] Recording service implementation
|
||||
- [ ] Replay service implementation
|
||||
- [ ] Advanced security features (TLS, X.509 certificates)
|
||||
- [ ] Comprehensive test suite with mock cameras
|
||||
- [ ] Performance benchmarks
|
||||
- [ ] CLI tool for camera management
|
||||
|
||||
## Debugging Tools
|
||||
|
||||
### 🔍 Diagnostic Utility
|
||||
|
||||
Comprehensive camera testing and analysis with optional XML capture:
|
||||
|
||||
```bash
|
||||
go build -o onvif-diagnostics ./cmd/onvif-diagnostics/
|
||||
|
||||
# Standard diagnostic report
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera-ip/onvif/device_service" \
|
||||
-username "admin" \
|
||||
-password "pass" \
|
||||
-verbose
|
||||
|
||||
# With raw SOAP XML capture for debugging
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera-ip/onvif/device_service" \
|
||||
-username "admin" \
|
||||
-password "pass" \
|
||||
-capture-xml \
|
||||
-verbose
|
||||
```
|
||||
|
||||
**Generates**:
|
||||
- `camera-logs/Manufacturer_Model_Firmware_timestamp.json` - Diagnostic report
|
||||
- `camera-logs/Manufacturer_Model_Firmware_xmlcapture_timestamp.tar.gz` - Raw XML (with `-capture-xml`)
|
||||
|
||||
**See**: `XML_DEBUGGING_SOLUTION.md` for complete debugging workflow
|
||||
|
||||
### 🧪 Camera Test Framework
|
||||
|
||||
Automated regression testing using captured camera responses:
|
||||
|
||||
```bash
|
||||
# 1. Capture from camera
|
||||
./onvif-diagnostics -endpoint "http://camera/onvif/device_service" \
|
||||
-username "user" -password "pass" -capture-xml
|
||||
|
||||
# 2. Generate test
|
||||
go build -o generate-tests ./cmd/generate-tests/
|
||||
./generate-tests -capture camera-logs/*_xmlcapture_*.tar.gz -output testdata/captures/
|
||||
|
||||
# 3. Run tests
|
||||
go test -v ./testdata/captures/
|
||||
```
|
||||
|
||||
**Benefits**:
|
||||
- Test without physical cameras
|
||||
- Prevent regressions across camera models
|
||||
- Fast CI/CD integration
|
||||
- Real camera response validation
|
||||
|
||||
**See**: `testdata/captures/README.md` for complete testing guide
|
||||
|
||||
## 🖥️ CLI Tools
|
||||
|
||||
### Interactive CLI Tool
|
||||
|
||||
Feature-rich command-line interface for camera management and testing:
|
||||
|
||||
```bash
|
||||
go build -o onvif-cli ./cmd/onvif-cli/
|
||||
|
||||
# Start interactive menu
|
||||
./onvif-cli
|
||||
```
|
||||
|
||||
**Features**:
|
||||
- 🔍 Discover cameras on network with interface selection
|
||||
- 🌐 View all network interfaces and their capabilities
|
||||
- 🔗 Connect to cameras with authentication
|
||||
- 📱 Get device info, capabilities, and system settings
|
||||
- 📹 Retrieve media profiles and stream URLs
|
||||
- 🎮 PTZ control (pan, tilt, zoom, presets)
|
||||
- 🎨 Imaging settings (brightness, contrast, exposure, etc.)
|
||||
- 📞 Network interface selection for multi-interface systems
|
||||
|
||||
**Usage**:
|
||||
```
|
||||
📋 Main Menu:
|
||||
1. Discover Cameras on Network
|
||||
2. Connect to Camera
|
||||
3. Device Operations
|
||||
4. Media Operations
|
||||
5. PTZ Operations
|
||||
6. Imaging Operations
|
||||
0. Exit
|
||||
```
|
||||
|
||||
Note: The discovery function now intelligently detects multiple interfaces and shows options only when needed - no separate "List Network Interfaces" menu required.
|
||||
|
||||
### Quick Demo Tool
|
||||
|
||||
Lightweight tool for quick testing and demonstration:
|
||||
|
||||
```bash
|
||||
go build -o onvif-quick ./cmd/onvif-quick/
|
||||
|
||||
# Start interactive menu
|
||||
./onvif-quick
|
||||
```
|
||||
|
||||
**Features**:
|
||||
- ⚡ Quick camera discovery
|
||||
- 🌐 List available network interfaces
|
||||
- 🔗 Quick connection and camera info
|
||||
- 🎮 PTZ demo with movement examples
|
||||
- 📡 Stream URL retrieval
|
||||
|
||||
### Network Interface Selection
|
||||
|
||||
The CLI intelligently handles network interface selection automatically:
|
||||
- **Single interface**: Auto-discovery works seamlessly
|
||||
- **Multiple interfaces**: Shows interfaces only if auto-discovery fails
|
||||
- **Multiple active interfaces**: Tries each one and aggregates results
|
||||
|
||||
For programmatic usage:
|
||||
|
||||
```go
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: "eth0", // By interface name
|
||||
// or
|
||||
// NetworkInterface: "192.168.1.100", // By IP address
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
**See**:
|
||||
- `docs/CLI_NETWORK_INTERFACE_USAGE.md` - Detailed CLI guide
|
||||
- `discovery/NETWORK_INTERFACE_GUIDE.md` - API usage examples
|
||||
- `DESIGN_REFACTOR.md` - How smart interface detection works
|
||||
|
||||
## 🌟 Star History
|
||||
|
||||
If you find this project useful, please consider giving it a star! ⭐
|
||||
|
||||
[](https://star-history.com/#0x524a/onvif-go&Date)
|
||||
|
||||
## 📊 Project Stats
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
- Inspired by the original [use-go/onvif](https://github.com/use-go/onvif) library
|
||||
- ONVIF specifications from [ONVIF.org](https://www.onvif.org)
|
||||
- Thanks to all contributors and the Go community
|
||||
|
||||
## Support
|
||||
|
||||
- 📖 [Documentation](https://pkg.go.dev/github.com/0x524a/onvif-go)
|
||||
- 🐛 [Issue Tracker](https://github.com/0x524a/onvif-go/issues)
|
||||
- 💬 [Discussions](https://github.com/0x524a/onvif-go/discussions)
|
||||
- 🔒 [Security Policy](.github/SECURITY.md)
|
||||
|
||||
## Keywords
|
||||
|
||||
`onvif` `ip-camera` `surveillance` `golang` `rtsp` `ptz` `camera-control` `video-streaming` `security-camera` `nvr` `vms` `iot` `cctv` `hikvision` `axis` `dahua` `bosch` `camera-sdk` `golang-library` `soap` `ws-discovery`
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [ONVIF Device Manager](https://sourceforge.net/projects/onvifdm/) - GUI tool for testing ONVIF devices
|
||||
- [ONVIF Device Tool](https://www.onvif.org/tools/) - Official ONVIF test tool
|
||||
|
||||
---
|
||||
|
||||
Made with ❤️ for the Go and IoT community
|
||||
@@ -1,944 +0,0 @@
|
||||
# onvif-go - ONVIF Client and Server Library for Go
|
||||
|
||||
[](https://pkg.go.dev/github.com/0x524a/onvif-go)
|
||||
[](https://goreportcard.com/report/github.com/0x524a/onvif-go)
|
||||
[](https://codecov.io/gh/0x524a/onvif-go)
|
||||
[](https://sonarcloud.io/summary/new_code?id=0x524a_onvif-go)
|
||||
[](LICENSE)
|
||||
[](https://github.com/0x524a/onvif-go/stargazers)
|
||||
[](https://github.com/0x524a/onvif-go/issues)
|
||||
|
||||
> **Modern, high-performance Go library for ONVIF IP camera integration** - Control surveillance cameras, NVRs, and video devices with comprehensive ONVIF Profile S/T/G support. Includes both client and server implementations for complete ONVIF camera simulation and testing.
|
||||
|
||||
A production-ready, feature-rich Go (Golang) library for communicating with ONVIF-compliant IP cameras, network video recorders (NVR), and surveillance devices. Perfect for building video management systems (VMS), security camera applications, IoT projects, and camera testing frameworks.
|
||||
|
||||
## 🎯 Key Features at a Glance
|
||||
|
||||
- ✅ **ONVIF Client & Server** - Both client library and virtual camera server
|
||||
- ✅ **Production Ready** - Battle-tested with multiple camera brands
|
||||
- ✅ **Full Protocol Support** - Device, Media, PTZ, Imaging, Discovery services
|
||||
- ✅ **Type Safe** - Comprehensive Go types for all ONVIF operations
|
||||
- ✅ **Well Documented** - Extensive examples and API documentation
|
||||
- ✅ **Camera Tested** - Verified with Hikvision, Axis, Dahua, Bosch cameras
|
||||
- ✅ **Testing Framework** - Built-in mock server and testing utilities
|
||||
|
||||
## 🔑 What is ONVIF?
|
||||
|
||||
ONVIF (Open Network Video Interface Forum) is an open industry standard for IP-based security products. This library allows you to:
|
||||
|
||||
- 🎥 Control IP cameras from any manufacturer (Bosch, Hikvision, Axis, Dahua, etc.)
|
||||
- 📹 Get RTSP video streams and snapshots
|
||||
- 🎮 Pan, tilt, and zoom cameras remotely
|
||||
- 🔧 Configure camera settings (exposure, focus, white balance)
|
||||
- 🔍 Discover cameras on your network automatically
|
||||
- 🧪 Test ONVIF implementations without physical hardware
|
||||
|
||||
## Features
|
||||
|
||||
### 📡 ONVIF Client
|
||||
|
||||
✨ **Modern Go Design**
|
||||
- Context support for cancellation and timeouts
|
||||
- Concurrent-safe operations
|
||||
- Type-safe API with comprehensive error handling
|
||||
- Connection pooling for optimal performance
|
||||
|
||||
🎥 **Comprehensive ONVIF Support**
|
||||
- **Device Management**: Get device info, capabilities, system date/time, reboot
|
||||
- **Media Services**: Profiles, stream URIs (RTSP/HTTP), snapshot URIs, encoder configuration
|
||||
- **PTZ Control**: Continuous, absolute, and relative movement, presets, status
|
||||
- **Imaging**: Get/set brightness, contrast, exposure, focus, white balance, WDR
|
||||
- **Discovery**: Automatic camera detection via WS-Discovery multicast
|
||||
|
||||
### 🎬 ONVIF Server (NEW!)
|
||||
|
||||
🎥 **Virtual IP Camera Simulator**
|
||||
- **Multi-Lens Camera Support**: Simulate up to 10 independent camera profiles
|
||||
- **Complete ONVIF Implementation**: Device, Media, PTZ, and Imaging services
|
||||
- **Flexible Configuration**: CLI and library interfaces for easy setup
|
||||
- **PTZ Simulation**: Full pan-tilt-zoom control with preset positions
|
||||
- **Imaging Control**: Brightness, contrast, exposure, focus, and more
|
||||
- **Testing & Development**: Perfect for testing ONVIF clients without physical cameras
|
||||
|
||||
🔐 **Security**
|
||||
- WS-Security with UsernameToken authentication
|
||||
- Password digest (SHA-1) support
|
||||
- Configurable timeout and HTTP client options
|
||||
|
||||
📦 **Easy Integration**
|
||||
- Simple, intuitive API
|
||||
- Well-documented with examples
|
||||
- No external dependencies beyond Go standard library and golang.org/x/net
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
go get github.com/0x524a/onvif-go
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Discover Cameras on Network
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go/discovery"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
devices, err := discovery.Discover(ctx, 5*time.Second)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, device := range devices {
|
||||
fmt.Printf("Found: %s at %s\n",
|
||||
device.GetName(),
|
||||
device.GetDeviceEndpoint())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Connect to a Camera
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create client - endpoint can be:
|
||||
// - Full URL: "http://192.168.1.100/onvif/device_service"
|
||||
// - IP with port: "192.168.1.100:8080"
|
||||
// - IP only: "192.168.1.100" (automatically adds http:// and path)
|
||||
client, err := onvif.NewClient(
|
||||
"192.168.1.100", // Simple IP address
|
||||
onvif.WithCredentials("admin", "password"),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Get device information
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Camera: %s %s\n", info.Manufacturer, info.Model)
|
||||
fmt.Printf("Firmware: %s\n", info.FirmwareVersion)
|
||||
|
||||
// Initialize and discover service endpoints
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get media profiles
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get stream URI
|
||||
if len(profiles) > 0 {
|
||||
streamURI, err := client.GetStreamURI(ctx, profiles[0].Token)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Stream URI: %s\n", streamURI.URI)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### PTZ Control
|
||||
|
||||
```go
|
||||
// Continuous movement
|
||||
velocity := &onvif.PTZSpeed{
|
||||
PanTilt: &onvif.Vector2D{X: 0.5, Y: 0.0}, // Move right
|
||||
}
|
||||
timeout := "PT2S" // 2 seconds
|
||||
err := client.ContinuousMove(ctx, profileToken, velocity, &timeout)
|
||||
|
||||
// Stop movement
|
||||
err = client.Stop(ctx, profileToken, true, true)
|
||||
|
||||
// Absolute positioning
|
||||
position := &onvif.PTZVector{
|
||||
PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0}, // Center
|
||||
Zoom: &onvif.Vector1D{X: 0.5}, // 50% zoom
|
||||
}
|
||||
err = client.AbsoluteMove(ctx, profileToken, position, nil)
|
||||
|
||||
// Go to preset
|
||||
presets, err := client.GetPresets(ctx, profileToken)
|
||||
if len(presets) > 0 {
|
||||
err = client.GotoPreset(ctx, profileToken, presets[0].Token, nil)
|
||||
}
|
||||
```
|
||||
|
||||
### Imaging Settings
|
||||
|
||||
```go
|
||||
// Get current settings
|
||||
settings, err := client.GetImagingSettings(ctx, videoSourceToken)
|
||||
|
||||
// Modify settings
|
||||
brightness := 60.0
|
||||
settings.Brightness = &brightness
|
||||
|
||||
contrast := 55.0
|
||||
settings.Contrast = &contrast
|
||||
|
||||
// Apply settings
|
||||
err = client.SetImagingSettings(ctx, videoSourceToken, settings, true)
|
||||
```
|
||||
|
||||
## API Overview
|
||||
|
||||
### API Coverage Summary
|
||||
|
||||
The onvif-go library provides comprehensive ONVIF protocol support with **200+ implemented APIs** across all major ONVIF services:
|
||||
|
||||
- **Device Management**: 98 APIs (100% complete) ✅
|
||||
- **Media Service**: 14+ APIs (profiles, streams, encoding) ✅
|
||||
- **PTZ Service**: 13 APIs (movement, presets, status) ✅
|
||||
- **Imaging Service**: 7 APIs (brightness, contrast, focus control) ✅
|
||||
- **Discovery Service**: WS-Discovery network scanning ✅
|
||||
|
||||
### Client Creation
|
||||
|
||||
```go
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(30*time.Second),
|
||||
onvif.WithHTTPClient(customHTTPClient),
|
||||
)
|
||||
```
|
||||
|
||||
### Device Service (98 APIs) - 100% Complete ✅
|
||||
|
||||
The Device Service provides comprehensive device management capabilities with **98 fully implemented APIs**:
|
||||
|
||||
#### Core Device Information
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDeviceInformation()` | Get manufacturer, model, firmware version, serial number, hardware ID |
|
||||
| `GetCapabilities()` | Get device capabilities and service endpoints (device, media, imaging, PTZ, events, etc.) |
|
||||
| `GetServices()` | Get list of services with optional capabilities |
|
||||
| `GetServiceCapabilities()` | Get device service-specific capabilities |
|
||||
| `GetEndpointReference()` | Get device's WS-Addressing endpoint reference |
|
||||
| `SystemReboot()` | Reboot the device |
|
||||
| `Initialize()` | Discover and cache service endpoints |
|
||||
|
||||
#### Hostname & Network Discovery
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetHostname()` | Get device hostname configuration |
|
||||
| `SetHostname()` | Set device hostname |
|
||||
| `SetHostnameFromDHCP()` | Enable/disable hostname from DHCP |
|
||||
| `GetScopes()` | Get configured WS-Discovery scopes |
|
||||
| `SetScopes()` | Set WS-Discovery scopes |
|
||||
| `AddScopes()` | Add WS-Discovery scopes |
|
||||
| `RemoveScopes()` | Remove WS-Discovery scopes |
|
||||
|
||||
#### DNS Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDNS()` | Get DNS configuration (DHCP and manual DNS servers) |
|
||||
| `SetDNS()` | Set DNS configuration (from DHCP, search domains, DNS servers) |
|
||||
|
||||
#### NTP Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetNTP()` | Get NTP configuration (DHCP and manual NTP servers) |
|
||||
| `SetNTP()` | Set NTP configuration (from DHCP, NTP servers) |
|
||||
|
||||
#### Dynamic DNS
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDynamicDNS()` | Get Dynamic DNS configuration |
|
||||
| `SetDynamicDNS()` | Set Dynamic DNS with type and name |
|
||||
|
||||
#### System Date & Time
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetSystemDateAndTime()` | Get device system date and time (interface{}) |
|
||||
| `FixedGetSystemDateAndTime()` | Get properly typed system date and time with timezone support |
|
||||
| `SetSystemDateAndTime()` | Set device system date and time with manual/NTP mode |
|
||||
|
||||
#### Network Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetNetworkInterfaces()` | Get all network interface configurations |
|
||||
| `GetNetworkProtocols()` | Get network protocol settings (HTTP, HTTPS, RTSP, RTMP, SSH, etc.) |
|
||||
| `SetNetworkProtocols()` | Set network protocol settings |
|
||||
| `GetNetworkDefaultGateway()` | Get default gateway configuration (IPv4 and IPv6) |
|
||||
| `SetNetworkDefaultGateway()` | Set default gateway configuration |
|
||||
| `GetZeroConfiguration()` | Get Zero Configuration (zeroconf/Bonjour) status |
|
||||
| `SetZeroConfiguration()` | Enable/disable Zero Configuration per interface |
|
||||
|
||||
#### User Management
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetUsers()` | Get list of user accounts and credentials |
|
||||
| `CreateUsers()` | Create new user accounts |
|
||||
| `SetUser()` | Modify existing user account |
|
||||
| `DeleteUsers()` | Delete user accounts |
|
||||
| `GetRemoteUser()` | Get remote user connection status |
|
||||
| `SetRemoteUser()` | Set remote user connection settings |
|
||||
|
||||
#### Security & Access Control
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetIPAddressFilter()` | Get IP address filter (allow/deny lists) |
|
||||
| `SetIPAddressFilter()` | Set IP address filtering rules |
|
||||
| `AddIPAddressFilter()` | Add IP addresses to filter list |
|
||||
| `RemoveIPAddressFilter()` | Remove IP addresses from filter list |
|
||||
| `GetPasswordComplexityConfiguration()` | Get password policy settings |
|
||||
| `SetPasswordComplexityConfiguration()` | Set password policy (length, uppercase, numbers, special chars) |
|
||||
| `GetPasswordHistoryConfiguration()` | Get password history requirements |
|
||||
| `SetPasswordHistoryConfiguration()` | Set password history and re-use prevention |
|
||||
| `GetAuthFailureWarningConfiguration()` | Get failed authentication warning settings |
|
||||
| `SetAuthFailureWarningConfiguration()` | Set failed authentication thresholds |
|
||||
|
||||
#### Discovery Modes
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDiscoveryMode()` | Get discovery mode (Discoverable/NonDiscoverable) |
|
||||
| `SetDiscoveryMode()` | Set discovery mode |
|
||||
| `GetRemoteDiscoveryMode()` | Get remote discovery mode |
|
||||
| `SetRemoteDiscoveryMode()` | Set remote discovery mode |
|
||||
|
||||
#### Certificate Management
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetCertificates()` | Get installed certificates |
|
||||
| `GetCACertificates()` | Get Certificate Authority certificates |
|
||||
| `LoadCertificates()` | Load/install certificates |
|
||||
| `LoadCACertificates()` | Load/install CA certificates |
|
||||
| `CreateCertificate()` | Create self-signed certificate |
|
||||
| `DeleteCertificates()` | Delete certificates |
|
||||
| `GetCertificateInformation()` | Get certificate details and validity |
|
||||
| `GetCertificatesStatus()` | Get certificate usage status |
|
||||
| `SetCertificatesStatus()` | Set certificate usage (enabled/disabled) |
|
||||
| `GetPkcs10Request()` | Generate PKCS#10 certificate signing request |
|
||||
| `LoadCertificateWithPrivateKey()` | Load certificate with private key |
|
||||
| `GetClientCertificateMode()` | Check if client certificate authentication enabled |
|
||||
| `SetClientCertificateMode()` | Enable/disable client certificate authentication |
|
||||
|
||||
#### WiFi/802.11 Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetDot11Capabilities()` | Get WiFi capabilities (cipher suites, auth modes) |
|
||||
| `GetDot11Status()` | Get WiFi status (SSID, signal strength, link quality) |
|
||||
| `GetDot1XConfiguration()` | Get 802.1X EAP configuration |
|
||||
| `GetDot1XConfigurations()` | Get all 802.1X configurations |
|
||||
| `SetDot1XConfiguration()` | Set 802.1X configuration |
|
||||
| `CreateDot1XConfiguration()` | Create new 802.1X configuration |
|
||||
| `DeleteDot1XConfiguration()` | Delete 802.1X configuration |
|
||||
| `ScanAvailableDot11Networks()` | Scan for available WiFi networks |
|
||||
|
||||
#### Storage Configuration
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetStorageConfigurations()` | Get all storage configurations |
|
||||
| `GetStorageConfiguration()` | Get specific storage configuration |
|
||||
| `CreateStorageConfiguration()` | Create new storage configuration |
|
||||
| `SetStorageConfiguration()` | Update storage configuration |
|
||||
| `DeleteStorageConfiguration()` | Delete storage configuration |
|
||||
| `SetHashingAlgorithm()` | Set password hashing algorithm |
|
||||
|
||||
#### System Maintenance & Logs
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetSystemLog()` | Get system logs (boot, security, etc.) |
|
||||
| `GetSystemBackup()` | Get available system backups |
|
||||
| `RestoreSystem()` | Restore from backup file |
|
||||
| `GetSystemUris()` | Get system log and backup URIs |
|
||||
| `GetSystemSupportInformation()` | Get support information and system details |
|
||||
| `SetSystemFactoryDefault()` | Reset device to factory defaults |
|
||||
| `StartFirmwareUpgrade()` | Initiate firmware upgrade |
|
||||
| `StartSystemRestore()` | Initiate system restore |
|
||||
|
||||
#### Relay & Auxiliary I/O
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetRelayOutputs()` | Get relay outputs and their current state |
|
||||
| `SetRelayOutputSettings()` | Configure relay output behavior |
|
||||
| `SetRelayOutputState()` | Set relay output state (active/inactive) |
|
||||
| `SendAuxiliaryCommand()` | Send auxiliary commands (e.g., IR control) |
|
||||
|
||||
#### Additional Features
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetGeoLocation()` | Get device geographic location |
|
||||
| `SetGeoLocation()` | Set device geographic location |
|
||||
| `DeleteGeoLocation()` | Delete geographic location |
|
||||
| `GetDPAddresses()` | Get WS-Discovery multicast addresses |
|
||||
| `SetDPAddresses()` | Set WS-Discovery multicast addresses |
|
||||
| `GetAccessPolicy()` | Get device access policy |
|
||||
| `SetAccessPolicy()` | Set device access policy |
|
||||
| `GetWsdlUrl()` | Get device WSDL URL (deprecated) |
|
||||
|
||||
## 🔧 Device Management Features
|
||||
|
||||
The onvif-go library provides **98 fully-implemented Device Management APIs** for complete device configuration and control. See [DEVICE_API_STATUS.md](DEVICE_API_STATUS.md) for the complete API reference.
|
||||
|
||||
### Common Device Management Use Cases
|
||||
|
||||
#### Query Device Information
|
||||
```go
|
||||
// Get device info (manufacturer, model, firmware)
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Camera: %s %s (FW: %s)\n", info.Manufacturer, info.Model, info.FirmwareVersion)
|
||||
|
||||
// Get capabilities
|
||||
caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
|
||||
#### Network Configuration
|
||||
```go
|
||||
// Get all network interfaces
|
||||
interfaces, err := client.GetNetworkInterfaces(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get DNS and NTP settings
|
||||
dns, err := client.GetDNS(ctx)
|
||||
ntp, err := client.GetNTP(ctx)
|
||||
|
||||
// Configure DNS
|
||||
err = client.SetDNS(ctx, false, []string{"example.com"}, []onvif.IPAddress{
|
||||
{Type: "IPv4", IPv4Address: "8.8.8.8"},
|
||||
})
|
||||
|
||||
// Get/Set hostname
|
||||
hostname, err := client.GetHostname(ctx)
|
||||
err = client.SetHostname(ctx, "new-camera-name")
|
||||
```
|
||||
|
||||
#### User & Security Management
|
||||
```go
|
||||
// Get users
|
||||
users, err := client.GetUsers(ctx)
|
||||
|
||||
// Create new user
|
||||
err = client.CreateUsers(ctx, []*onvif.User{
|
||||
{Username: "operator", Password: "pass123"},
|
||||
})
|
||||
|
||||
// Configure security
|
||||
err = client.SetPasswordComplexityConfiguration(ctx, &onvif.PasswordComplexityConfiguration{
|
||||
MinLen: 8,
|
||||
Uppercase: 1,
|
||||
Number: 1,
|
||||
SpecialChars: 1,
|
||||
})
|
||||
|
||||
// IP address filtering
|
||||
filter := &onvif.IPAddressFilter{
|
||||
Type: onvif.IPAddressFilterAllow,
|
||||
}
|
||||
err = client.SetIPAddressFilter(ctx, filter)
|
||||
```
|
||||
|
||||
#### Certificate Management
|
||||
```go
|
||||
// Get installed certificates
|
||||
certs, err := client.GetCertificates(ctx)
|
||||
|
||||
// Create self-signed certificate
|
||||
cert, err := client.CreateCertificate(ctx,
|
||||
"cert1",
|
||||
"CN=camera.example.com",
|
||||
"2024-01-01T00:00:00Z",
|
||||
"2025-01-01T00:00:00Z",
|
||||
)
|
||||
|
||||
// Check certificate status
|
||||
status, err := client.GetCertificatesStatus(ctx)
|
||||
|
||||
// Enable client certificate authentication
|
||||
err = client.SetClientCertificateMode(ctx, true)
|
||||
```
|
||||
|
||||
#### System Maintenance
|
||||
```go
|
||||
// Get system logs
|
||||
log, err := client.GetSystemLog(ctx, onvif.SystemLogTypeBoot)
|
||||
|
||||
// Get system backup
|
||||
backups, err := client.GetSystemBackup(ctx)
|
||||
|
||||
// Reboot device
|
||||
rebootToken, err := client.SystemReboot(ctx)
|
||||
|
||||
// Set factory defaults
|
||||
err = client.SetSystemFactoryDefault(ctx, onvif.FactoryDefaultTypeSoft)
|
||||
|
||||
// Firmware upgrade
|
||||
upgradeToken, err := client.StartFirmwareUpgrade(ctx)
|
||||
```
|
||||
|
||||
#### WiFi Configuration (802.11/802.1X)
|
||||
```go
|
||||
// Get WiFi capabilities
|
||||
caps, err := client.GetDot11Capabilities(ctx)
|
||||
|
||||
// Scan available networks
|
||||
networks, err := client.ScanAvailableDot11Networks(ctx, "interface1")
|
||||
|
||||
// Get 802.1X configuration
|
||||
config, err := client.GetDot1XConfiguration(ctx, "config1")
|
||||
|
||||
// Set 802.1X
|
||||
err = client.SetDot1XConfiguration(ctx, config)
|
||||
```
|
||||
|
||||
#### Relay & I/O Control
|
||||
```go
|
||||
// Get relay outputs
|
||||
relays, err := client.GetRelayOutputs(ctx)
|
||||
|
||||
// Control relay state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", onvif.RelayLogicalStateActive)
|
||||
err = client.SetRelayOutputState(ctx, "relay1", onvif.RelayLogicalStateInactive)
|
||||
|
||||
// Send auxiliary commands (e.g., IR control)
|
||||
response, err := client.SendAuxiliaryCommand(ctx, "tt:IRLamp|On")
|
||||
```
|
||||
|
||||
### Full API Reference
|
||||
|
||||
For complete documentation of all 98 Device Management APIs with detailed descriptions, parameters, and return types, see:
|
||||
- **[DEVICE_API_STATUS.md](DEVICE_API_STATUS.md)** - Complete API listing with categories and examples
|
||||
|
||||
### Media Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetProfiles()` | Get all media profiles |
|
||||
| `GetStreamURI()` | Get RTSP/HTTP stream URI |
|
||||
| `GetSnapshotURI()` | Get snapshot image URI |
|
||||
| `GetVideoEncoderConfiguration()` | Get video encoder settings |
|
||||
| `GetVideoSources()` | Get all video sources |
|
||||
| `GetAudioSources()` | Get all audio sources |
|
||||
| `GetAudioOutputs()` | Get all audio outputs |
|
||||
| `CreateProfile()` | Create new media profile |
|
||||
| `DeleteProfile()` | Delete media profile |
|
||||
| `SetVideoEncoderConfiguration()` | Set video encoder configuration |
|
||||
|
||||
### PTZ Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `ContinuousMove()` | Start continuous PTZ movement |
|
||||
| `AbsoluteMove()` | Move to absolute position |
|
||||
| `RelativeMove()` | Move relative to current position |
|
||||
| `Stop()` | Stop PTZ movement |
|
||||
| `GetStatus()` | Get current PTZ status and position |
|
||||
| `GetPresets()` | Get list of PTZ presets |
|
||||
| `GotoPreset()` | Move to a preset position |
|
||||
| `SetPreset()` | Save current position as preset |
|
||||
| `RemovePreset()` | Delete a preset |
|
||||
| `GotoHomePosition()` | Move to home position |
|
||||
| `SetHomePosition()` | Set current position as home |
|
||||
| `GetConfiguration()` | Get PTZ configuration |
|
||||
| `GetConfigurations()` | Get all PTZ configurations |
|
||||
|
||||
### Imaging Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetImagingSettings()` | Get imaging settings (brightness, contrast, etc.) |
|
||||
| `SetImagingSettings()` | Set imaging settings |
|
||||
| `Move()` | Perform focus move operations |
|
||||
| `GetOptions()` | Get available imaging options and ranges |
|
||||
| `GetMoveOptions()` | Get available focus move options |
|
||||
| `StopFocus()` | Stop focus movement |
|
||||
| `GetImagingStatus()` | Get current imaging/focus status |
|
||||
|
||||
### Discovery Service
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `Discover()` | Discover ONVIF devices on network |
|
||||
|
||||
## ONVIF Server
|
||||
|
||||
The library now includes a complete ONVIF server implementation that simulates multi-lens IP cameras!
|
||||
|
||||
### Quick Start
|
||||
|
||||
```bash
|
||||
# Install the server CLI
|
||||
go install ./cmd/onvif-server
|
||||
|
||||
# Run with default settings (3 camera profiles)
|
||||
onvif-server
|
||||
|
||||
# Or customize
|
||||
onvif-server -profiles 5 -username admin -password mypass -port 9000
|
||||
```
|
||||
|
||||
### Using the Server Library
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/0x524a/onvif-go/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create server with default multi-lens camera configuration
|
||||
srv, err := server.New(server.DefaultConfig())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Start server
|
||||
ctx := context.Background()
|
||||
if err := srv.Start(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Server Features
|
||||
|
||||
- 🎥 **Multi-Lens Simulation**: Support for up to 10 independent camera profiles
|
||||
- 🎮 **Full PTZ Control**: Pan, tilt, zoom with preset positions
|
||||
- 📷 **Imaging Settings**: Brightness, contrast, exposure, focus, white balance
|
||||
- 🌐 **Complete ONVIF Services**: Device, Media, PTZ, and Imaging services
|
||||
- 🔐 **WS-Security**: Digest authentication support
|
||||
- ⚙️ **Flexible Configuration**: CLI and library interfaces
|
||||
|
||||
### Use Cases
|
||||
|
||||
- Testing ONVIF client implementations
|
||||
- Developing video management systems
|
||||
- CI/CD integration testing
|
||||
- Demonstrations without physical cameras
|
||||
- Learning ONVIF protocol
|
||||
|
||||
For complete documentation, see [server/README.md](server/README.md).
|
||||
|
||||
## Examples
|
||||
|
||||
The [examples](examples/) directory contains complete working examples:
|
||||
|
||||
### Client Examples
|
||||
- **[discovery](examples/discovery/)**: Discover cameras on the network
|
||||
- **[device-info](examples/device-info/)**: Get device information and media profiles
|
||||
- **[ptz-control](examples/ptz-control/)**: Control camera PTZ (pan, tilt, zoom)
|
||||
- **[imaging-settings](examples/imaging-settings/)**: Adjust imaging settings
|
||||
|
||||
### Server Examples
|
||||
- **[onvif-server](examples/onvif-server/)**: Multi-lens camera server with custom configuration
|
||||
|
||||
To run an example:
|
||||
|
||||
```bash
|
||||
cd examples/discovery
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
onvif-go/
|
||||
├── client.go # Main ONVIF client
|
||||
├── types.go # ONVIF data types
|
||||
├── errors.go # Error definitions
|
||||
├── device.go # Device service implementation
|
||||
├── media.go # Media service implementation
|
||||
├── ptz.go # PTZ service implementation
|
||||
├── imaging.go # Imaging service implementation
|
||||
├── soap/ # SOAP client with WS-Security
|
||||
│ └── soap.go
|
||||
├── discovery/ # WS-Discovery implementation
|
||||
│ └── discovery.go
|
||||
├── server/ # ONVIF server implementation
|
||||
│ ├── server.go # Main server
|
||||
│ ├── types.go # Server types and configuration
|
||||
│ ├── device.go # Device service handlers
|
||||
│ ├── media.go # Media service handlers
|
||||
│ ├── ptz.go # PTZ service handlers
|
||||
│ ├── imaging.go # Imaging service handlers
|
||||
│ └── soap/ # SOAP server handler
|
||||
│ └── handler.go
|
||||
├── cmd/
|
||||
│ ├── onvif-cli/ # Client CLI tool
|
||||
│ └── onvif-server/ # Server CLI tool
|
||||
└── examples/ # Usage examples
|
||||
├── discovery/
|
||||
├── device-info/
|
||||
├── ptz-control/
|
||||
├── imaging-settings/
|
||||
└── onvif-server/ # Multi-lens camera server example
|
||||
```
|
||||
|
||||
## Design Principles
|
||||
|
||||
1. **Context-Aware**: All network operations accept `context.Context` for cancellation and timeouts
|
||||
2. **Type Safety**: Strong typing with comprehensive struct definitions
|
||||
3. **Error Handling**: Typed errors with clear error messages
|
||||
4. **Concurrency Safe**: Thread-safe operations with proper locking
|
||||
5. **Performance**: Connection pooling and efficient HTTP client reuse
|
||||
6. **Standards Compliant**: Follows ONVIF specifications for SOAP/XML messaging
|
||||
|
||||
## Compatibility
|
||||
|
||||
- **Go Version**: 1.21+
|
||||
- **ONVIF Versions**: Compatible with ONVIF Profile S, Profile T, Profile G
|
||||
- **Tested Cameras**: Works with most ONVIF-compliant IP cameras including:
|
||||
- Axis
|
||||
- Hikvision
|
||||
- Dahua
|
||||
- Bosch
|
||||
- Hanwha (Samsung)
|
||||
- And many others
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
# Run tests
|
||||
go test ./...
|
||||
|
||||
# Run tests with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run tests with race detection
|
||||
go test -race ./...
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
||||
|
||||
1. Fork the repository
|
||||
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
||||
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
||||
4. Push to the branch (`git push origin feature/amazing-feature`)
|
||||
5. Open a Pull Request
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [ ] Event service implementation
|
||||
- [ ] Analytics service implementation
|
||||
- [ ] Recording service implementation
|
||||
- [ ] Replay service implementation
|
||||
- [ ] Advanced security features (TLS, X.509 certificates)
|
||||
- [ ] Comprehensive test suite with mock cameras
|
||||
- [ ] Performance benchmarks
|
||||
- [ ] CLI tool for camera management
|
||||
|
||||
## Debugging Tools
|
||||
|
||||
### 🔍 Diagnostic Utility
|
||||
|
||||
Comprehensive camera testing and analysis with optional XML capture:
|
||||
|
||||
```bash
|
||||
go build -o onvif-diagnostics ./cmd/onvif-diagnostics/
|
||||
|
||||
# Standard diagnostic report
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera-ip/onvif/device_service" \
|
||||
-username "admin" \
|
||||
-password "pass" \
|
||||
-verbose
|
||||
|
||||
# With raw SOAP XML capture for debugging
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera-ip/onvif/device_service" \
|
||||
-username "admin" \
|
||||
-password "pass" \
|
||||
-capture-xml \
|
||||
-verbose
|
||||
```
|
||||
|
||||
**Generates**:
|
||||
- `camera-logs/Manufacturer_Model_Firmware_timestamp.json` - Diagnostic report
|
||||
- `camera-logs/Manufacturer_Model_Firmware_xmlcapture_timestamp.tar.gz` - Raw XML (with `-capture-xml`)
|
||||
|
||||
**See**: `XML_DEBUGGING_SOLUTION.md` for complete debugging workflow
|
||||
|
||||
### 🧪 Camera Test Framework
|
||||
|
||||
Automated regression testing using captured camera responses:
|
||||
|
||||
```bash
|
||||
# 1. Capture from camera
|
||||
./onvif-diagnostics -endpoint "http://camera/onvif/device_service" \
|
||||
-username "user" -password "pass" -capture-xml
|
||||
|
||||
# 2. Generate test
|
||||
go build -o generate-tests ./cmd/generate-tests/
|
||||
./generate-tests -capture camera-logs/*_xmlcapture_*.tar.gz -output testdata/captures/
|
||||
|
||||
# 3. Run tests
|
||||
go test -v ./testdata/captures/
|
||||
```
|
||||
|
||||
**Benefits**:
|
||||
- Test without physical cameras
|
||||
- Prevent regressions across camera models
|
||||
- Fast CI/CD integration
|
||||
- Real camera response validation
|
||||
|
||||
**See**: `testdata/captures/README.md` for complete testing guide
|
||||
|
||||
## 🖥️ CLI Tools
|
||||
|
||||
### Interactive CLI Tool
|
||||
|
||||
Feature-rich command-line interface for camera management and testing:
|
||||
|
||||
```bash
|
||||
go build -o onvif-cli ./cmd/onvif-cli/
|
||||
|
||||
# Start interactive menu
|
||||
./onvif-cli
|
||||
```
|
||||
|
||||
**Features**:
|
||||
- 🔍 Discover cameras on network with interface selection
|
||||
- 🌐 View all network interfaces and their capabilities
|
||||
- 🔗 Connect to cameras with authentication
|
||||
- 📱 Get device info, capabilities, and system settings
|
||||
- 📹 Retrieve media profiles and stream URLs
|
||||
- 🎮 PTZ control (pan, tilt, zoom, presets)
|
||||
- 🎨 Imaging settings (brightness, contrast, exposure, etc.)
|
||||
- 📞 Network interface selection for multi-interface systems
|
||||
|
||||
**Usage**:
|
||||
```
|
||||
📋 Main Menu:
|
||||
1. Discover Cameras on Network
|
||||
2. Connect to Camera
|
||||
3. Device Operations
|
||||
4. Media Operations
|
||||
5. PTZ Operations
|
||||
6. Imaging Operations
|
||||
0. Exit
|
||||
```
|
||||
|
||||
Note: The discovery function now intelligently detects multiple interfaces and shows options only when needed - no separate "List Network Interfaces" menu required.
|
||||
|
||||
### Quick Demo Tool
|
||||
|
||||
Lightweight tool for quick testing and demonstration:
|
||||
|
||||
```bash
|
||||
go build -o onvif-quick ./cmd/onvif-quick/
|
||||
|
||||
# Start interactive menu
|
||||
./onvif-quick
|
||||
```
|
||||
|
||||
**Features**:
|
||||
- ⚡ Quick camera discovery
|
||||
- 🌐 List available network interfaces
|
||||
- 🔗 Quick connection and camera info
|
||||
- 🎮 PTZ demo with movement examples
|
||||
- 📡 Stream URL retrieval
|
||||
|
||||
### Network Interface Selection
|
||||
|
||||
The CLI intelligently handles network interface selection automatically:
|
||||
- **Single interface**: Auto-discovery works seamlessly
|
||||
- **Multiple interfaces**: Shows interfaces only if auto-discovery fails
|
||||
- **Multiple active interfaces**: Tries each one and aggregates results
|
||||
|
||||
For programmatic usage:
|
||||
|
||||
```go
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: "eth0", // By interface name
|
||||
// or
|
||||
// NetworkInterface: "192.168.1.100", // By IP address
|
||||
}
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, 5*time.Second, opts)
|
||||
```
|
||||
|
||||
**See**:
|
||||
- `docs/CLI_NETWORK_INTERFACE_USAGE.md` - Detailed CLI guide
|
||||
- `discovery/NETWORK_INTERFACE_GUIDE.md` - API usage examples
|
||||
- `DESIGN_REFACTOR.md` - How smart interface detection works
|
||||
|
||||
## 🌟 Star History
|
||||
|
||||
If you find this project useful, please consider giving it a star! ⭐
|
||||
|
||||
[](https://star-history.com/#0x524a/onvif-go&Date)
|
||||
|
||||
## 📊 Project Stats
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
- Inspired by the original [use-go/onvif](https://github.com/use-go/onvif) library
|
||||
- ONVIF specifications from [ONVIF.org](https://www.onvif.org)
|
||||
- Thanks to all contributors and the Go community
|
||||
|
||||
## Support
|
||||
|
||||
- 📖 [Documentation](https://pkg.go.dev/github.com/0x524a/onvif-go)
|
||||
- 🐛 [Issue Tracker](https://github.com/0x524a/onvif-go/issues)
|
||||
- 💬 [Discussions](https://github.com/0x524a/onvif-go/discussions)
|
||||
- 🔒 [Security Policy](.github/SECURITY.md)
|
||||
|
||||
## Keywords
|
||||
|
||||
`onvif` `ip-camera` `surveillance` `golang` `rtsp` `ptz` `camera-control` `video-streaming` `security-camera` `nvr` `vms` `iot` `cctv` `hikvision` `axis` `dahua` `bosch` `camera-sdk` `golang-library` `soap` `ws-discovery`
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [ONVIF Device Manager](https://sourceforge.net/projects/onvifdm/) - GUI tool for testing ONVIF devices
|
||||
- [ONVIF Device Tool](https://www.onvif.org/tools/) - Official ONVIF test tool
|
||||
|
||||
---
|
||||
|
||||
Made with ❤️ for the Go and IoT community
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,112 +0,0 @@
|
||||
#!/bin/bash
|
||||
# build-release.sh - Build release binaries locally
|
||||
|
||||
set -e
|
||||
|
||||
VERSION=${1:-$(git describe --tags --always --dirty 2>/dev/null || echo "dev")}
|
||||
echo "Building release binaries for version: $VERSION"
|
||||
|
||||
# Clean previous builds
|
||||
rm -rf bin releases
|
||||
mkdir -p bin releases
|
||||
|
||||
# Platforms to build
|
||||
PLATFORMS=(
|
||||
"linux/amd64"
|
||||
"linux/arm64"
|
||||
"linux/arm"
|
||||
"windows/amd64"
|
||||
"windows/arm64"
|
||||
"darwin/amd64"
|
||||
"darwin/arm64"
|
||||
)
|
||||
|
||||
# Binaries to build
|
||||
BINARIES=(
|
||||
"onvif-cli"
|
||||
"onvif-quick"
|
||||
"onvif-server"
|
||||
"onvif-diagnostics"
|
||||
)
|
||||
|
||||
LDFLAGS="-s -w -X main.Version=${VERSION} -X main.Commit=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
|
||||
|
||||
echo "Building binaries..."
|
||||
for platform in "${PLATFORMS[@]}"; do
|
||||
OS="${platform%/*}"
|
||||
ARCH="${platform#*/}"
|
||||
|
||||
echo ""
|
||||
echo "Building for $OS/$ARCH..."
|
||||
|
||||
for binary in "${BINARIES[@]}"; do
|
||||
OUTPUT="bin/${binary}-${OS}-${ARCH}"
|
||||
|
||||
if [ "$OS" = "windows" ]; then
|
||||
OUTPUT="${OUTPUT}.exe"
|
||||
fi
|
||||
|
||||
echo " - ${binary}"
|
||||
GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build -ldflags="${LDFLAGS}" -o "$OUTPUT" "./cmd/${binary}" 2>/dev/null || {
|
||||
echo " ⚠️ Skipped (build failed)"
|
||||
continue
|
||||
}
|
||||
done
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Creating release archives..."
|
||||
|
||||
cd bin
|
||||
|
||||
for platform in "${PLATFORMS[@]}"; do
|
||||
OS="${platform%/*}"
|
||||
ARCH="${platform#*/}"
|
||||
ARCHIVE_NAME="onvif-go-${VERSION}-${OS}-${ARCH}"
|
||||
|
||||
# Check if any binary exists for this platform
|
||||
if [ "$OS" = "windows" ]; then
|
||||
FILES=(*-${OS}-${ARCH}.exe)
|
||||
else
|
||||
FILES=(*-${OS}-${ARCH})
|
||||
fi
|
||||
|
||||
# Skip if no files found
|
||||
if [ "${FILES[0]}" = "*-${OS}-${ARCH}" ] || [ "${FILES[0]}" = "*-${OS}-${ARCH}.exe" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo " Creating archive for ${OS}/${ARCH}..."
|
||||
|
||||
if [ "$OS" = "windows" ]; then
|
||||
# ZIP for Windows
|
||||
zip -q "../releases/${ARCHIVE_NAME}.zip" *-${OS}-${ARCH}.exe ../README.md ../LICENSE
|
||||
else
|
||||
# tar.gz for Unix-like
|
||||
tar czf "../releases/${ARCHIVE_NAME}.tar.gz" *-${OS}-${ARCH} -C .. README.md LICENSE
|
||||
fi
|
||||
done
|
||||
|
||||
cd ..
|
||||
|
||||
echo ""
|
||||
echo "Generating checksums..."
|
||||
cd releases
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum * > checksums.txt
|
||||
else
|
||||
shasum -a 256 * > checksums.txt
|
||||
fi
|
||||
cd ..
|
||||
|
||||
echo ""
|
||||
echo "✅ Build complete!"
|
||||
echo ""
|
||||
echo "Binaries in: $(pwd)/bin/"
|
||||
echo "Archives in: $(pwd)/releases/"
|
||||
echo ""
|
||||
ls -lh releases/
|
||||
|
||||
echo ""
|
||||
echo "To create a GitHub release, run:"
|
||||
echo " gh release create ${VERSION} releases/* --title \"Release ${VERSION}\" --notes \"Release notes here\""
|
||||
@@ -1,112 +0,0 @@
|
||||
#!/bin/bash
|
||||
# build-release.sh - Build release binaries locally
|
||||
|
||||
set -e
|
||||
|
||||
VERSION=${1:-$(git describe --tags --always --dirty 2>/dev/null || echo "dev")}
|
||||
echo "Building release binaries for version: $VERSION"
|
||||
|
||||
# Clean previous builds
|
||||
rm -rf bin releases
|
||||
mkdir -p bin releases
|
||||
|
||||
# Platforms to build
|
||||
PLATFORMS=(
|
||||
"linux/amd64"
|
||||
"linux/arm64"
|
||||
"linux/arm"
|
||||
"windows/amd64"
|
||||
"windows/arm64"
|
||||
"darwin/amd64"
|
||||
"darwin/arm64"
|
||||
)
|
||||
|
||||
# Binaries to build
|
||||
BINARIES=(
|
||||
"onvif-cli"
|
||||
"onvif-quick"
|
||||
"onvif-server"
|
||||
"onvif-diagnostics"
|
||||
)
|
||||
|
||||
LDFLAGS="-s -w -X main.Version=${VERSION} -X main.Commit=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
|
||||
|
||||
echo "Building binaries..."
|
||||
for platform in "${PLATFORMS[@]}"; do
|
||||
OS="${platform%/*}"
|
||||
ARCH="${platform#*/}"
|
||||
|
||||
echo ""
|
||||
echo "Building for $OS/$ARCH..."
|
||||
|
||||
for binary in "${BINARIES[@]}"; do
|
||||
OUTPUT="bin/${binary}-${OS}-${ARCH}"
|
||||
|
||||
if [ "$OS" = "windows" ]; then
|
||||
OUTPUT="${OUTPUT}.exe"
|
||||
fi
|
||||
|
||||
echo " - ${binary}"
|
||||
GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build -ldflags="${LDFLAGS}" -o "$OUTPUT" "./cmd/${binary}" 2>/dev/null || {
|
||||
echo " ⚠️ Skipped (build failed)"
|
||||
continue
|
||||
}
|
||||
done
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Creating release archives..."
|
||||
|
||||
cd bin
|
||||
|
||||
for platform in "${PLATFORMS[@]}"; do
|
||||
OS="${platform%/*}"
|
||||
ARCH="${platform#*/}"
|
||||
ARCHIVE_NAME="onvif-go-${VERSION}-${OS}-${ARCH}"
|
||||
|
||||
# Check if any binary exists for this platform
|
||||
if [ "$OS" = "windows" ]; then
|
||||
FILES=(*-${OS}-${ARCH}.exe)
|
||||
else
|
||||
FILES=(*-${OS}-${ARCH})
|
||||
fi
|
||||
|
||||
# Skip if no files found
|
||||
if [ "${FILES[0]}" = "*-${OS}-${ARCH}" ] || [ "${FILES[0]}" = "*-${OS}-${ARCH}.exe" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo " Creating archive for ${OS}/${ARCH}..."
|
||||
|
||||
if [ "$OS" = "windows" ]; then
|
||||
# ZIP for Windows
|
||||
zip -q "../releases/${ARCHIVE_NAME}.zip" *-${OS}-${ARCH}.exe ../README.md ../LICENSE
|
||||
else
|
||||
# tar.gz for Unix-like
|
||||
tar czf "../releases/${ARCHIVE_NAME}.tar.gz" *-${OS}-${ARCH} -C .. README.md LICENSE
|
||||
fi
|
||||
done
|
||||
|
||||
cd ..
|
||||
|
||||
echo ""
|
||||
echo "Generating checksums..."
|
||||
cd releases
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum * > checksums.txt
|
||||
else
|
||||
shasum -a 256 * > checksums.txt
|
||||
fi
|
||||
cd ..
|
||||
|
||||
echo ""
|
||||
echo "✅ Build complete!"
|
||||
echo ""
|
||||
echo "Binaries in: $(pwd)/bin/"
|
||||
echo "Archives in: $(pwd)/releases/"
|
||||
echo ""
|
||||
ls -lh releases/
|
||||
|
||||
echo ""
|
||||
echo "To create a GitHub release, run:"
|
||||
echo " gh release create ${VERSION} releases/* --title \"Release ${VERSION}\" --notes \"Release notes here\""
|
||||
@@ -1,524 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5" //nolint:gosec // MD5 used for ONVIF digest authentication
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Default client configuration constants.
|
||||
const (
|
||||
// DefaultTimeout is the default HTTP client timeout.
|
||||
DefaultTimeout = 30 * time.Second
|
||||
// DefaultIdleConnTimeout is the default idle connection timeout.
|
||||
DefaultIdleConnTimeout = 90 * time.Second
|
||||
// DefaultMaxIdleConns is the default maximum idle connections.
|
||||
DefaultMaxIdleConns = 10
|
||||
// DefaultMaxIdleConnsPerHost is the default maximum idle connections per host.
|
||||
DefaultMaxIdleConnsPerHost = 5
|
||||
// NonceSize is the size of the nonce for digest authentication.
|
||||
NonceSize = 16
|
||||
)
|
||||
|
||||
// Client represents an ONVIF client for communicating with IP cameras.
|
||||
type Client struct {
|
||||
endpoint string
|
||||
username string
|
||||
password string
|
||||
httpClient *http.Client
|
||||
mu sync.RWMutex
|
||||
|
||||
// Service endpoints
|
||||
mediaEndpoint string
|
||||
ptzEndpoint string
|
||||
imagingEndpoint string
|
||||
eventEndpoint string
|
||||
}
|
||||
|
||||
// ClientOption is a functional option for configuring the Client.
|
||||
type ClientOption func(*Client)
|
||||
|
||||
// WithTimeout sets the HTTP client timeout.
|
||||
func WithTimeout(timeout time.Duration) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.httpClient.Timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets a custom HTTP client.
|
||||
func WithHTTPClient(httpClient *http.Client) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.httpClient = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// WithInsecureSkipVerify disables TLS certificate verification.
|
||||
// WARNING: Only use this for testing or with trusted cameras on private networks.
|
||||
func WithInsecureSkipVerify() ClientOption {
|
||||
return func(c *Client) {
|
||||
if transport, ok := c.httpClient.Transport.(*http.Transport); ok {
|
||||
if transport.TLSClientConfig == nil {
|
||||
transport.TLSClientConfig = &tls.Config{ //nolint:gosec // InsecureSkipVerify is intentional for testing
|
||||
}
|
||||
}
|
||||
transport.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithCredentials sets the authentication credentials.
|
||||
func WithCredentials(username, password string) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.username = username
|
||||
c.password = password
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient creates a new ONVIF client
|
||||
// The endpoint can be provided in multiple formats:
|
||||
// - Full URL: "http://192.168.1.100/onvif/device_service"
|
||||
// - IP with port: "192.168.1.100:80" (http assumed, /onvif/device_service added)
|
||||
// - IP only: "192.168.1.100" (http://IP:80/onvif/device_service used)
|
||||
func NewClient(endpoint string, opts ...ClientOption) (*Client, error) {
|
||||
// Normalize endpoint to full URL
|
||||
normalizedEndpoint, err := normalizeEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid endpoint: %w", err)
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
endpoint: normalizedEndpoint,
|
||||
httpClient: &http.Client{
|
||||
Timeout: DefaultTimeout,
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: DefaultMaxIdleConns,
|
||||
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: DefaultIdleConnTimeout,
|
||||
},
|
||||
// Don't follow redirects automatically
|
||||
// This prevents http:// from being silently upgraded to https://
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Apply options
|
||||
for _, opt := range opts {
|
||||
opt(client)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// normalizeEndpoint converts various endpoint formats to a full ONVIF URL.
|
||||
func normalizeEndpoint(endpoint string) (string, error) {
|
||||
// Check if endpoint starts with a scheme
|
||||
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
|
||||
// Parse as full URL
|
||||
parsedURL, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse endpoint URL: %w", err)
|
||||
}
|
||||
if parsedURL.Host == "" {
|
||||
return "", fmt.Errorf("%w", ErrURLMissingHost)
|
||||
}
|
||||
// If path is empty or just "/", add default ONVIF path
|
||||
if parsedURL.Path == "" || parsedURL.Path == "/" {
|
||||
parsedURL.Path = "/onvif/device_service"
|
||||
}
|
||||
|
||||
return parsedURL.String(), nil
|
||||
}
|
||||
|
||||
// No scheme - treat as IP, IP:port, hostname, or hostname:port
|
||||
// Add http:// scheme and validate
|
||||
fullURL := "http://" + endpoint + "/onvif/device_service"
|
||||
parsedURL, err := url.Parse(fullURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid IP address or hostname: %w", err)
|
||||
}
|
||||
|
||||
if parsedURL.Host == "" {
|
||||
return "", fmt.Errorf("%w", ErrInvalidEndpointFormat)
|
||||
}
|
||||
|
||||
return fullURL, nil
|
||||
}
|
||||
|
||||
// Some cameras incorrectly report localhost (127.0.0.1, 0.0.0.0, localhost) in their capability URLs.
|
||||
func (c *Client) fixLocalhostURL(serviceURL string) string {
|
||||
if serviceURL == "" {
|
||||
return serviceURL
|
||||
}
|
||||
|
||||
// Parse the service URL
|
||||
parsedService, err := url.Parse(serviceURL)
|
||||
if err != nil {
|
||||
return serviceURL // Return original if parsing fails
|
||||
}
|
||||
|
||||
// Check if the service URL has a localhost/loopback address
|
||||
host := parsedService.Hostname()
|
||||
if host == "localhost" || host == "127.0.0.1" || host == "0.0.0.0" || host == "::1" {
|
||||
// Parse the client's endpoint to get the actual camera address
|
||||
parsedClient, err := url.Parse(c.endpoint)
|
||||
if err != nil {
|
||||
return serviceURL // Return original if parsing fails
|
||||
}
|
||||
|
||||
// Replace the host but keep the port from service URL if specified
|
||||
servicePort := parsedService.Port()
|
||||
if servicePort != "" {
|
||||
parsedService.Host = parsedClient.Hostname() + ":" + servicePort
|
||||
} else {
|
||||
parsedService.Host = parsedClient.Hostname()
|
||||
// Use client's port if service doesn't specify one
|
||||
if clientPort := parsedClient.Port(); clientPort != "" {
|
||||
parsedService.Host = parsedClient.Hostname() + ":" + clientPort
|
||||
}
|
||||
}
|
||||
|
||||
return parsedService.String()
|
||||
}
|
||||
|
||||
return serviceURL
|
||||
}
|
||||
|
||||
// Initialize discovers and initializes service endpoints.
|
||||
func (c *Client) Initialize(ctx context.Context) error {
|
||||
// Get device information and capabilities
|
||||
capabilities, err := c.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get capabilities: %w", err)
|
||||
}
|
||||
|
||||
// Extract service endpoints and fix any localhost addresses
|
||||
// Some cameras incorrectly report localhost instead of their actual IP
|
||||
if capabilities.Media != nil && capabilities.Media.XAddr != "" {
|
||||
c.mediaEndpoint = c.fixLocalhostURL(capabilities.Media.XAddr)
|
||||
}
|
||||
if capabilities.PTZ != nil && capabilities.PTZ.XAddr != "" {
|
||||
c.ptzEndpoint = c.fixLocalhostURL(capabilities.PTZ.XAddr)
|
||||
}
|
||||
if capabilities.Imaging != nil && capabilities.Imaging.XAddr != "" {
|
||||
c.imagingEndpoint = c.fixLocalhostURL(capabilities.Imaging.XAddr)
|
||||
}
|
||||
if capabilities.Events != nil && capabilities.Events.XAddr != "" {
|
||||
c.eventEndpoint = c.fixLocalhostURL(capabilities.Events.XAddr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Endpoint returns the device endpoint.
|
||||
func (c *Client) Endpoint() string {
|
||||
return c.endpoint
|
||||
}
|
||||
|
||||
// SetCredentials updates the authentication credentials.
|
||||
func (c *Client) SetCredentials(username, password string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.username = username
|
||||
c.password = password
|
||||
}
|
||||
|
||||
// GetCredentials returns the current credentials.
|
||||
func (c *Client) GetCredentials() (username, password string) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
return c.username, c.password
|
||||
}
|
||||
|
||||
// DownloadFile downloads a file from the given URL with authentication.
|
||||
// Supports both Basic and Digest authentication (tries basic first, falls back to digest).
|
||||
func (c *Client) DownloadFile(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
// Try basic auth first
|
||||
data, err := c.downloadWithBasicAuth(ctx, downloadURL)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// If basic auth fails with 401, try digest auth
|
||||
if strings.Contains(err.Error(), "401") {
|
||||
digestData, digestErr := c.downloadWithDigestAuth(ctx, downloadURL)
|
||||
if digestErr == nil {
|
||||
return digestData, nil
|
||||
}
|
||||
// If digest auth also fails, return the original error
|
||||
if strings.Contains(digestErr.Error(), "401") {
|
||||
return nil, err // Return original error (both auth methods failed)
|
||||
}
|
||||
|
||||
return nil, digestErr
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// downloadWithBasicAuth performs an HTTP download with Basic authentication.
|
||||
func (c *Client) downloadWithBasicAuth(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
if c.username != "" {
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "onvif-go-client")
|
||||
req.Header.Set("Connection", "close")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download request failed: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyPreview, _ := io.ReadAll(resp.Body) //nolint:errcheck // Error preview - ignore read errors
|
||||
bodyStr := string(bodyPreview)
|
||||
const maxBodyPreview = 200
|
||||
if len(bodyStr) > maxBodyPreview {
|
||||
bodyStr = bodyStr[:maxBodyPreview] + "..."
|
||||
}
|
||||
|
||||
// Base error message for programmatic use
|
||||
errorMsg := fmt.Sprintf("download failed with status code %d", resp.StatusCode)
|
||||
|
||||
// Add structured error details
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized:
|
||||
errorMsg += ": authentication failed (401 Unauthorized); basic auth failed, trying digest auth"
|
||||
case http.StatusForbidden:
|
||||
errorMsg += ": access denied (403 Forbidden); user may not have permission to download snapshots"
|
||||
case http.StatusNotFound:
|
||||
errorMsg += ": snapshot URI not found (404); camera may have revoked the URI, try getting a fresh snapshot URI"
|
||||
}
|
||||
|
||||
if bodyStr != "" {
|
||||
errorMsg += fmt.Sprintf("; response: %s", bodyStr)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %s", ErrDownloadFailed, errorMsg)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// downloadWithDigestAuth performs an HTTP download with Digest authentication.
|
||||
func (c *Client) downloadWithDigestAuth(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
if c.username == "" {
|
||||
return nil, fmt.Errorf("%w", ErrDigestAuthRequiresCredentials)
|
||||
}
|
||||
|
||||
// Create a custom transport with digest auth
|
||||
tr := &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: DefaultTimeout,
|
||||
KeepAlive: DefaultTimeout,
|
||||
}).Dial,
|
||||
MaxIdleConns: DefaultMaxIdleConns,
|
||||
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: DefaultIdleConnTimeout,
|
||||
}
|
||||
|
||||
// Create a custom HTTP client for digest auth
|
||||
digestClient := &http.Client{
|
||||
Transport: &digestAuthTransport{
|
||||
transport: tr,
|
||||
username: c.username,
|
||||
password: c.password,
|
||||
},
|
||||
Timeout: DefaultTimeout,
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "onvif-go-client")
|
||||
req.Header.Set("Connection", "close")
|
||||
|
||||
resp, err := digestClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("digest auth request failed: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyPreview, _ := io.ReadAll(resp.Body) //nolint:errcheck // Error preview - ignore read errors
|
||||
bodyStr := string(bodyPreview)
|
||||
const maxBodyPreview = 200
|
||||
if len(bodyStr) > maxBodyPreview {
|
||||
bodyStr = bodyStr[:maxBodyPreview] + "..."
|
||||
}
|
||||
|
||||
errorMsg := fmt.Sprintf("download failed with status code %d", resp.StatusCode)
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized:
|
||||
errorMsg += ": digest authentication failed (401 Unauthorized); check camera credentials (username/password)"
|
||||
case http.StatusForbidden:
|
||||
errorMsg += ": access denied (403 Forbidden); user may not have permission to download snapshots"
|
||||
case http.StatusNotFound:
|
||||
errorMsg += ": snapshot URI not found (404); try getting a fresh snapshot URI"
|
||||
}
|
||||
|
||||
if bodyStr != "" {
|
||||
errorMsg += fmt.Sprintf("; response: %s", bodyStr)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %s", ErrDownloadFailed, errorMsg)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// digestAuthTransport implements digest authentication for HTTP transport.
|
||||
type digestAuthTransport struct {
|
||||
transport *http.Transport
|
||||
username string
|
||||
password string
|
||||
nc int
|
||||
ncMu sync.Mutex // Protects nc field from concurrent access
|
||||
}
|
||||
|
||||
// RoundTrip implements http.RoundTripper with digest auth support.
|
||||
func (d *digestAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// First request without auth to get the challenge
|
||||
resp, err := d.transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("transport round trip failed: %w", err)
|
||||
}
|
||||
|
||||
// If we get 401, handle digest auth challenge
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
// Read the WWW-Authenticate header
|
||||
authHeader := resp.Header.Get("WWW-Authenticate")
|
||||
if strings.Contains(authHeader, "Digest") {
|
||||
// Parse digest challenge and create auth header
|
||||
authHeaderValue := d.createDigestAuthHeader(req, authHeader)
|
||||
|
||||
// Create new request with auth header
|
||||
newReq := req.Clone(req.Context())
|
||||
newReq.Header.Set("Authorization", authHeaderValue)
|
||||
|
||||
// Retry with auth
|
||||
resp, err = d.transport.RoundTrip(newReq)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("transport round trip with auth failed: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// createDigestAuthHeader creates a digest auth header from the challenge.
|
||||
func (d *digestAuthTransport) createDigestAuthHeader(req *http.Request, authHeader string) string {
|
||||
// Simple digest auth implementation - parse challenge and create response
|
||||
// This is a basic implementation that handles most ONVIF cameras
|
||||
|
||||
// Extract digest parameters from WWW-Authenticate header
|
||||
realm := extractParam(authHeader, "realm")
|
||||
nonce := extractParam(authHeader, "nonce")
|
||||
qop := extractParam(authHeader, "qop")
|
||||
uri := req.URL.Path
|
||||
if req.URL.RawQuery != "" {
|
||||
uri += "?" + req.URL.RawQuery
|
||||
}
|
||||
|
||||
// Generate response hash
|
||||
ha1 := md5Hash(d.username + ":" + realm + ":" + d.password)
|
||||
|
||||
method := req.Method
|
||||
ha2 := md5Hash(method + ":" + uri)
|
||||
|
||||
// Increment nonce count atomically to prevent race conditions
|
||||
// HTTP transports must be safe for concurrent use
|
||||
d.ncMu.Lock()
|
||||
d.nc++
|
||||
nc := d.nc
|
||||
d.ncMu.Unlock()
|
||||
ncStr := fmt.Sprintf("%08x", nc)
|
||||
cnonce := generateNonce()
|
||||
|
||||
var responseStr string
|
||||
if qop == "auth" {
|
||||
responseStr = md5Hash(ha1 + ":" + nonce + ":" + ncStr + ":" + cnonce + ":auth:" + ha2)
|
||||
} else {
|
||||
responseStr = md5Hash(ha1 + ":" + nonce + ":" + ha2)
|
||||
}
|
||||
|
||||
// Build Authorization header
|
||||
authHeaderValue := fmt.Sprintf(`Digest username=%q, realm=%q, nonce=%q, uri=%q, response=%q`,
|
||||
d.username, realm, nonce, uri, responseStr)
|
||||
|
||||
if qop == "auth" {
|
||||
authHeaderValue += fmt.Sprintf(`, opaque=%q, qop=%s, nc=%s, cnonce=%q`,
|
||||
extractParam(authHeader, "opaque"), qop, ncStr, cnonce)
|
||||
}
|
||||
|
||||
return authHeaderValue
|
||||
}
|
||||
|
||||
// Helper functions for digest auth.
|
||||
func extractParam(authHeader, param string) string {
|
||||
prefix := param + `="`
|
||||
idx := strings.Index(authHeader, prefix)
|
||||
if idx == -1 {
|
||||
return ""
|
||||
}
|
||||
start := idx + len(prefix)
|
||||
end := strings.Index(authHeader[start:], `"`)
|
||||
if end == -1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return authHeader[start : start+end]
|
||||
}
|
||||
|
||||
func md5Hash(s string) string {
|
||||
h := md5.New() //nolint:gosec // MD5 required for ONVIF digest auth
|
||||
h.Write([]byte(s))
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// generateNonce generates a cryptographically secure random nonce for digest authentication.
|
||||
func generateNonce() string {
|
||||
bytes := make([]byte, NonceSize)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
// Fallback to time-based nonce if crypto/rand fails (shouldn't happen)
|
||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
@@ -1,524 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5" //nolint:gosec // MD5 used for ONVIF digest authentication
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Default client configuration constants.
|
||||
const (
|
||||
// DefaultTimeout is the default HTTP client timeout.
|
||||
DefaultTimeout = 30 * time.Second
|
||||
// DefaultIdleConnTimeout is the default idle connection timeout.
|
||||
DefaultIdleConnTimeout = 90 * time.Second
|
||||
// DefaultMaxIdleConns is the default maximum idle connections.
|
||||
DefaultMaxIdleConns = 10
|
||||
// DefaultMaxIdleConnsPerHost is the default maximum idle connections per host.
|
||||
DefaultMaxIdleConnsPerHost = 5
|
||||
// NonceSize is the size of the nonce for digest authentication.
|
||||
NonceSize = 16
|
||||
)
|
||||
|
||||
// Client represents an ONVIF client for communicating with IP cameras.
|
||||
type Client struct {
|
||||
endpoint string
|
||||
username string
|
||||
password string
|
||||
httpClient *http.Client
|
||||
mu sync.RWMutex
|
||||
|
||||
// Service endpoints
|
||||
mediaEndpoint string
|
||||
ptzEndpoint string
|
||||
imagingEndpoint string
|
||||
eventEndpoint string
|
||||
}
|
||||
|
||||
// ClientOption is a functional option for configuring the Client.
|
||||
type ClientOption func(*Client)
|
||||
|
||||
// WithTimeout sets the HTTP client timeout.
|
||||
func WithTimeout(timeout time.Duration) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.httpClient.Timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets a custom HTTP client.
|
||||
func WithHTTPClient(httpClient *http.Client) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.httpClient = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// WithInsecureSkipVerify disables TLS certificate verification.
|
||||
// WARNING: Only use this for testing or with trusted cameras on private networks.
|
||||
func WithInsecureSkipVerify() ClientOption {
|
||||
return func(c *Client) {
|
||||
if transport, ok := c.httpClient.Transport.(*http.Transport); ok {
|
||||
if transport.TLSClientConfig == nil {
|
||||
transport.TLSClientConfig = &tls.Config{ //nolint:gosec // InsecureSkipVerify is intentional for testing
|
||||
}
|
||||
}
|
||||
transport.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithCredentials sets the authentication credentials.
|
||||
func WithCredentials(username, password string) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.username = username
|
||||
c.password = password
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient creates a new ONVIF client
|
||||
// The endpoint can be provided in multiple formats:
|
||||
// - Full URL: "http://192.168.1.100/onvif/device_service"
|
||||
// - IP with port: "192.168.1.100:80" (http assumed, /onvif/device_service added)
|
||||
// - IP only: "192.168.1.100" (http://IP:80/onvif/device_service used)
|
||||
func NewClient(endpoint string, opts ...ClientOption) (*Client, error) {
|
||||
// Normalize endpoint to full URL
|
||||
normalizedEndpoint, err := normalizeEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid endpoint: %w", err)
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
endpoint: normalizedEndpoint,
|
||||
httpClient: &http.Client{
|
||||
Timeout: DefaultTimeout,
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: DefaultMaxIdleConns,
|
||||
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: DefaultIdleConnTimeout,
|
||||
},
|
||||
// Don't follow redirects automatically
|
||||
// This prevents http:// from being silently upgraded to https://
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Apply options
|
||||
for _, opt := range opts {
|
||||
opt(client)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// normalizeEndpoint converts various endpoint formats to a full ONVIF URL.
|
||||
func normalizeEndpoint(endpoint string) (string, error) {
|
||||
// Check if endpoint starts with a scheme
|
||||
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
|
||||
// Parse as full URL
|
||||
parsedURL, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse endpoint URL: %w", err)
|
||||
}
|
||||
if parsedURL.Host == "" {
|
||||
return "", fmt.Errorf("%w", ErrURLMissingHost)
|
||||
}
|
||||
// If path is empty or just "/", add default ONVIF path
|
||||
if parsedURL.Path == "" || parsedURL.Path == "/" {
|
||||
parsedURL.Path = "/onvif/device_service"
|
||||
}
|
||||
|
||||
return parsedURL.String(), nil
|
||||
}
|
||||
|
||||
// No scheme - treat as IP, IP:port, hostname, or hostname:port
|
||||
// Add http:// scheme and validate
|
||||
fullURL := "http://" + endpoint + "/onvif/device_service"
|
||||
parsedURL, err := url.Parse(fullURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid IP address or hostname: %w", err)
|
||||
}
|
||||
|
||||
if parsedURL.Host == "" {
|
||||
return "", fmt.Errorf("%w", ErrInvalidEndpointFormat)
|
||||
}
|
||||
|
||||
return fullURL, nil
|
||||
}
|
||||
|
||||
// Some cameras incorrectly report localhost (127.0.0.1, 0.0.0.0, localhost) in their capability URLs.
|
||||
func (c *Client) fixLocalhostURL(serviceURL string) string {
|
||||
if serviceURL == "" {
|
||||
return serviceURL
|
||||
}
|
||||
|
||||
// Parse the service URL
|
||||
parsedService, err := url.Parse(serviceURL)
|
||||
if err != nil {
|
||||
return serviceURL // Return original if parsing fails
|
||||
}
|
||||
|
||||
// Check if the service URL has a localhost/loopback address
|
||||
host := parsedService.Hostname()
|
||||
if host == "localhost" || host == "127.0.0.1" || host == "0.0.0.0" || host == "::1" {
|
||||
// Parse the client's endpoint to get the actual camera address
|
||||
parsedClient, err := url.Parse(c.endpoint)
|
||||
if err != nil {
|
||||
return serviceURL // Return original if parsing fails
|
||||
}
|
||||
|
||||
// Replace the host but keep the port from service URL if specified
|
||||
servicePort := parsedService.Port()
|
||||
if servicePort != "" {
|
||||
parsedService.Host = parsedClient.Hostname() + ":" + servicePort
|
||||
} else {
|
||||
parsedService.Host = parsedClient.Hostname()
|
||||
// Use client's port if service doesn't specify one
|
||||
if clientPort := parsedClient.Port(); clientPort != "" {
|
||||
parsedService.Host = parsedClient.Hostname() + ":" + clientPort
|
||||
}
|
||||
}
|
||||
|
||||
return parsedService.String()
|
||||
}
|
||||
|
||||
return serviceURL
|
||||
}
|
||||
|
||||
// Initialize discovers and initializes service endpoints.
|
||||
func (c *Client) Initialize(ctx context.Context) error {
|
||||
// Get device information and capabilities
|
||||
capabilities, err := c.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get capabilities: %w", err)
|
||||
}
|
||||
|
||||
// Extract service endpoints and fix any localhost addresses
|
||||
// Some cameras incorrectly report localhost instead of their actual IP
|
||||
if capabilities.Media != nil && capabilities.Media.XAddr != "" {
|
||||
c.mediaEndpoint = c.fixLocalhostURL(capabilities.Media.XAddr)
|
||||
}
|
||||
if capabilities.PTZ != nil && capabilities.PTZ.XAddr != "" {
|
||||
c.ptzEndpoint = c.fixLocalhostURL(capabilities.PTZ.XAddr)
|
||||
}
|
||||
if capabilities.Imaging != nil && capabilities.Imaging.XAddr != "" {
|
||||
c.imagingEndpoint = c.fixLocalhostURL(capabilities.Imaging.XAddr)
|
||||
}
|
||||
if capabilities.Events != nil && capabilities.Events.XAddr != "" {
|
||||
c.eventEndpoint = c.fixLocalhostURL(capabilities.Events.XAddr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Endpoint returns the device endpoint.
|
||||
func (c *Client) Endpoint() string {
|
||||
return c.endpoint
|
||||
}
|
||||
|
||||
// SetCredentials updates the authentication credentials.
|
||||
func (c *Client) SetCredentials(username, password string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.username = username
|
||||
c.password = password
|
||||
}
|
||||
|
||||
// GetCredentials returns the current credentials.
|
||||
func (c *Client) GetCredentials() (username, password string) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
return c.username, c.password
|
||||
}
|
||||
|
||||
// DownloadFile downloads a file from the given URL with authentication.
|
||||
// Supports both Basic and Digest authentication (tries basic first, falls back to digest).
|
||||
func (c *Client) DownloadFile(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
// Try basic auth first
|
||||
data, err := c.downloadWithBasicAuth(ctx, downloadURL)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// If basic auth fails with 401, try digest auth
|
||||
if strings.Contains(err.Error(), "401") {
|
||||
digestData, digestErr := c.downloadWithDigestAuth(ctx, downloadURL)
|
||||
if digestErr == nil {
|
||||
return digestData, nil
|
||||
}
|
||||
// If digest auth also fails, return the original error
|
||||
if strings.Contains(digestErr.Error(), "401") {
|
||||
return nil, err // Return original error (both auth methods failed)
|
||||
}
|
||||
|
||||
return nil, digestErr
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// downloadWithBasicAuth performs an HTTP download with Basic authentication.
|
||||
func (c *Client) downloadWithBasicAuth(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
if c.username != "" {
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "onvif-go-client")
|
||||
req.Header.Set("Connection", "close")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download request failed: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyPreview, _ := io.ReadAll(resp.Body) //nolint:errcheck // Error preview - ignore read errors
|
||||
bodyStr := string(bodyPreview)
|
||||
const maxBodyPreview = 200
|
||||
if len(bodyStr) > maxBodyPreview {
|
||||
bodyStr = bodyStr[:maxBodyPreview] + "..."
|
||||
}
|
||||
|
||||
// Base error message for programmatic use
|
||||
errorMsg := fmt.Sprintf("download failed with status code %d", resp.StatusCode)
|
||||
|
||||
// Add structured error details
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized:
|
||||
errorMsg += ": authentication failed (401 Unauthorized); basic auth failed, trying digest auth"
|
||||
case http.StatusForbidden:
|
||||
errorMsg += ": access denied (403 Forbidden); user may not have permission to download snapshots"
|
||||
case http.StatusNotFound:
|
||||
errorMsg += ": snapshot URI not found (404); camera may have revoked the URI, try getting a fresh snapshot URI"
|
||||
}
|
||||
|
||||
if bodyStr != "" {
|
||||
errorMsg += fmt.Sprintf("; response: %s", bodyStr)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %s", ErrDownloadFailed, errorMsg)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// downloadWithDigestAuth performs an HTTP download with Digest authentication.
|
||||
func (c *Client) downloadWithDigestAuth(ctx context.Context, downloadURL string) ([]byte, error) {
|
||||
if c.username == "" {
|
||||
return nil, fmt.Errorf("%w", ErrDigestAuthRequiresCredentials)
|
||||
}
|
||||
|
||||
// Create a custom transport with digest auth
|
||||
tr := &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: DefaultTimeout,
|
||||
KeepAlive: DefaultTimeout,
|
||||
}).Dial,
|
||||
MaxIdleConns: DefaultMaxIdleConns,
|
||||
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: DefaultIdleConnTimeout,
|
||||
}
|
||||
|
||||
// Create a custom HTTP client for digest auth
|
||||
digestClient := &http.Client{
|
||||
Transport: &digestAuthTransport{
|
||||
transport: tr,
|
||||
username: c.username,
|
||||
password: c.password,
|
||||
},
|
||||
Timeout: DefaultTimeout,
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "onvif-go-client")
|
||||
req.Header.Set("Connection", "close")
|
||||
|
||||
resp, err := digestClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("digest auth request failed: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyPreview, _ := io.ReadAll(resp.Body) //nolint:errcheck // Error preview - ignore read errors
|
||||
bodyStr := string(bodyPreview)
|
||||
const maxBodyPreview = 200
|
||||
if len(bodyStr) > maxBodyPreview {
|
||||
bodyStr = bodyStr[:maxBodyPreview] + "..."
|
||||
}
|
||||
|
||||
errorMsg := fmt.Sprintf("download failed with status code %d", resp.StatusCode)
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized:
|
||||
errorMsg += ": digest authentication failed (401 Unauthorized); check camera credentials (username/password)"
|
||||
case http.StatusForbidden:
|
||||
errorMsg += ": access denied (403 Forbidden); user may not have permission to download snapshots"
|
||||
case http.StatusNotFound:
|
||||
errorMsg += ": snapshot URI not found (404); try getting a fresh snapshot URI"
|
||||
}
|
||||
|
||||
if bodyStr != "" {
|
||||
errorMsg += fmt.Sprintf("; response: %s", bodyStr)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %s", ErrDownloadFailed, errorMsg)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// digestAuthTransport implements digest authentication for HTTP transport.
|
||||
type digestAuthTransport struct {
|
||||
transport *http.Transport
|
||||
username string
|
||||
password string
|
||||
nc int
|
||||
ncMu sync.Mutex // Protects nc field from concurrent access
|
||||
}
|
||||
|
||||
// RoundTrip implements http.RoundTripper with digest auth support.
|
||||
func (d *digestAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// First request without auth to get the challenge
|
||||
resp, err := d.transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("transport round trip failed: %w", err)
|
||||
}
|
||||
|
||||
// If we get 401, handle digest auth challenge
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
// Read the WWW-Authenticate header
|
||||
authHeader := resp.Header.Get("WWW-Authenticate")
|
||||
if strings.Contains(authHeader, "Digest") {
|
||||
// Parse digest challenge and create auth header
|
||||
authHeaderValue := d.createDigestAuthHeader(req, authHeader)
|
||||
|
||||
// Create new request with auth header
|
||||
newReq := req.Clone(req.Context())
|
||||
newReq.Header.Set("Authorization", authHeaderValue)
|
||||
|
||||
// Retry with auth
|
||||
resp, err = d.transport.RoundTrip(newReq)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("transport round trip with auth failed: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// createDigestAuthHeader creates a digest auth header from the challenge.
|
||||
func (d *digestAuthTransport) createDigestAuthHeader(req *http.Request, authHeader string) string {
|
||||
// Simple digest auth implementation - parse challenge and create response
|
||||
// This is a basic implementation that handles most ONVIF cameras
|
||||
|
||||
// Extract digest parameters from WWW-Authenticate header
|
||||
realm := extractParam(authHeader, "realm")
|
||||
nonce := extractParam(authHeader, "nonce")
|
||||
qop := extractParam(authHeader, "qop")
|
||||
uri := req.URL.Path
|
||||
if req.URL.RawQuery != "" {
|
||||
uri += "?" + req.URL.RawQuery
|
||||
}
|
||||
|
||||
// Generate response hash
|
||||
ha1 := md5Hash(d.username + ":" + realm + ":" + d.password)
|
||||
|
||||
method := req.Method
|
||||
ha2 := md5Hash(method + ":" + uri)
|
||||
|
||||
// Increment nonce count atomically to prevent race conditions
|
||||
// HTTP transports must be safe for concurrent use
|
||||
d.ncMu.Lock()
|
||||
d.nc++
|
||||
nc := d.nc
|
||||
d.ncMu.Unlock()
|
||||
ncStr := fmt.Sprintf("%08x", nc)
|
||||
cnonce := generateNonce()
|
||||
|
||||
var responseStr string
|
||||
if qop == "auth" {
|
||||
responseStr = md5Hash(ha1 + ":" + nonce + ":" + ncStr + ":" + cnonce + ":auth:" + ha2)
|
||||
} else {
|
||||
responseStr = md5Hash(ha1 + ":" + nonce + ":" + ha2)
|
||||
}
|
||||
|
||||
// Build Authorization header
|
||||
authHeaderValue := fmt.Sprintf(`Digest username=%q, realm=%q, nonce=%q, uri=%q, response=%q`,
|
||||
d.username, realm, nonce, uri, responseStr)
|
||||
|
||||
if qop == "auth" {
|
||||
authHeaderValue += fmt.Sprintf(`, opaque=%q, qop=%s, nc=%s, cnonce=%q`,
|
||||
extractParam(authHeader, "opaque"), qop, ncStr, cnonce)
|
||||
}
|
||||
|
||||
return authHeaderValue
|
||||
}
|
||||
|
||||
// Helper functions for digest auth.
|
||||
func extractParam(authHeader, param string) string {
|
||||
prefix := param + `="`
|
||||
idx := strings.Index(authHeader, prefix)
|
||||
if idx == -1 {
|
||||
return ""
|
||||
}
|
||||
start := idx + len(prefix)
|
||||
end := strings.Index(authHeader[start:], `"`)
|
||||
if end == -1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return authHeader[start : start+end]
|
||||
}
|
||||
|
||||
func md5Hash(s string) string {
|
||||
h := md5.New() //nolint:gosec // MD5 required for ONVIF digest auth
|
||||
h.Write([]byte(s))
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// generateNonce generates a cryptographically secure random nonce for digest authentication.
|
||||
func generateNonce() string {
|
||||
bytes := make([]byte, NonceSize)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
// Fallback to time-based nonce if crypto/rand fails (shouldn't happen)
|
||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,236 +0,0 @@
|
||||
# Test Generator
|
||||
|
||||
Automatically generate Go tests from captured ONVIF camera XML traffic.
|
||||
|
||||
## Overview
|
||||
|
||||
This tool reads XML capture archives (created by `onvif-diagnostics -capture-xml`) and generates complete Go test files that replay the captured SOAP traffic through a mock server.
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```bash
|
||||
./generate-tests \
|
||||
-capture camera-logs/Camera_Model_xmlcapture_timestamp.tar.gz \
|
||||
-output testdata/captures/
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-capture string
|
||||
Path to XML capture archive (.tar.gz) (required)
|
||||
|
||||
-output string
|
||||
Output directory for generated test file (default: "./")
|
||||
|
||||
-package string
|
||||
Package name for generated test (default: "onvif_test")
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```bash
|
||||
# Generate test from Bosch camera capture
|
||||
./generate-tests \
|
||||
-capture camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_xmlcapture_20251110-120000.tar.gz \
|
||||
-output testdata/captures/
|
||||
|
||||
# Output:
|
||||
# ✓ Generated test file: testdata/captures/bosch_flexidome_indoor_5100i_ir_8.71.0066_test.go
|
||||
# Camera: Bosch FLEXIDOME indoor 5100i IR (Firmware: 8.71.0066)
|
||||
# Captured operations: 18
|
||||
```
|
||||
|
||||
## Generated Test Structure
|
||||
|
||||
The tool creates a complete test file with:
|
||||
|
||||
### Test Function
|
||||
|
||||
```go
|
||||
func Test<CameraName>(t *testing.T)
|
||||
```
|
||||
|
||||
Named based on camera manufacturer, model, and firmware.
|
||||
|
||||
### Subtests
|
||||
|
||||
- `GetDeviceInformation` - Validates device info parsing
|
||||
- `GetSystemDateAndTime` - Tests date/time operation
|
||||
- `GetCapabilities` - Verifies capability discovery
|
||||
- `GetProfiles` - Tests media profile enumeration
|
||||
|
||||
### Assertions
|
||||
|
||||
Each subtest includes:
|
||||
- Error checking
|
||||
- Nil validation
|
||||
- Basic field validation
|
||||
- Informative logging
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Load Capture** - Reads all SOAP exchanges from tar.gz archive
|
||||
2. **Extract Metadata** - Gets camera manufacturer, model, firmware from responses
|
||||
3. **Generate Name** - Creates valid Go identifier from camera info
|
||||
4. **Render Template** - Fills in test template with camera-specific data
|
||||
5. **Write File** - Saves test to output directory
|
||||
|
||||
## Template
|
||||
|
||||
The generator uses an embedded Go template that creates:
|
||||
|
||||
```go
|
||||
package onvif_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
func Test<CameraName>(t *testing.T) {
|
||||
captureArchive := "<archive-file>.tar.gz"
|
||||
|
||||
mockServer, err := onviftesting.NewMockSOAPServer(captureArchive)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock server: %v", err)
|
||||
}
|
||||
defer mockServer.Close()
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
mockServer.URL()+"/onvif/device_service",
|
||||
onvif.WithCredentials("testuser", "testpass"),
|
||||
)
|
||||
// ... test operations
|
||||
}
|
||||
```
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Capture from Camera
|
||||
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera/onvif/device_service" \
|
||||
-username "user" \
|
||||
-password "pass" \
|
||||
-capture-xml
|
||||
```
|
||||
|
||||
### 2. Generate Test
|
||||
|
||||
```bash
|
||||
./generate-tests \
|
||||
-capture camera-logs/Camera_*_xmlcapture_*.tar.gz \
|
||||
-output testdata/captures/
|
||||
```
|
||||
|
||||
### 3. Run Test
|
||||
|
||||
```bash
|
||||
go test -v ./testdata/captures/ -run TestCamera
|
||||
```
|
||||
|
||||
## Customization
|
||||
|
||||
After generation, you can customize the test:
|
||||
|
||||
### Add Camera-Specific Tests
|
||||
|
||||
```go
|
||||
t.Run("CustomFeature", func(t *testing.T) {
|
||||
// Add custom test for camera-specific features
|
||||
})
|
||||
```
|
||||
|
||||
### Add Detailed Assertions
|
||||
|
||||
```go
|
||||
t.Run("GetDeviceInformation", func(t *testing.T) {
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetDeviceInformation failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add specific assertions
|
||||
if info.Manufacturer != "ExpectedManufacturer" {
|
||||
t.Errorf("Expected manufacturer X, got %s", info.Manufacturer)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
```bash
|
||||
go build -o generate-tests ./cmd/generate-tests/
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
- `github.com/0x524a/onvif-go/testing` - Mock server and capture loader
|
||||
|
||||
## Output File Naming
|
||||
|
||||
Generated test files are named:
|
||||
|
||||
```
|
||||
<manufacturer>_<model>_<firmware>_test.go
|
||||
```
|
||||
|
||||
Examples:
|
||||
- `bosch_flexidome_indoor_5100i_ir_8.71.0066_test.go`
|
||||
- `axis_q3626-ve_12.6.104_test.go`
|
||||
- `reolink_e1_zoom_v3.1.0.2649_test.go`
|
||||
|
||||
All special characters converted to underscores or removed.
|
||||
|
||||
## Archive Path Handling
|
||||
|
||||
The generator automatically handles archive paths:
|
||||
|
||||
- If archive is in output directory, uses filename only
|
||||
- Otherwise uses relative path from output directory
|
||||
- Tests can find archives when run with `go test ./testdata/captures/`
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Failed to load capture"
|
||||
|
||||
Archive file not found or corrupted.
|
||||
|
||||
**Solution**: Verify archive path and ensure it's a valid tar.gz file.
|
||||
|
||||
### "Failed to extract device info"
|
||||
|
||||
Archive doesn't contain GetDeviceInformation response.
|
||||
|
||||
**Solution**: Re-capture from camera, ensuring diagnostic runs fully.
|
||||
|
||||
### Generated test won't compile
|
||||
|
||||
Usually due to invalid characters in camera names.
|
||||
|
||||
**Solution**: The generator should handle this, but you can manually edit the test function name.
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Potential improvements:
|
||||
|
||||
- [ ] Detect camera-specific operations (PTZ, audio, etc.)
|
||||
- [ ] Generate profile-specific tests
|
||||
- [ ] Add benchmarking subtests
|
||||
- [ ] Support custom test templates
|
||||
- [ ] Batch generation from multiple captures
|
||||
|
||||
## See Also
|
||||
|
||||
- `testdata/captures/README.md` - Using generated tests
|
||||
- `testing/mock_server.go` - Mock server implementation
|
||||
- `cmd/onvif-diagnostics/` - Capturing tool
|
||||
@@ -1,926 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
var (
|
||||
captureArchive = flag.String("capture", "", "Path to XML capture archive (.tar.gz)")
|
||||
outputDir = flag.String("output", "./", "Output directory for generated test file")
|
||||
packageName = flag.String("package", "onvif_test", "Package name for generated test")
|
||||
updateRegistry = flag.Bool("update-registry", true, "Update registry.json with camera info")
|
||||
registryPath = flag.String("registry", "", "Path to registry.json (default: testdata/captures/registry.json)")
|
||||
coverageReport = flag.Bool("coverage-report", false, "Generate coverage report from registry")
|
||||
coverageOutput = flag.String("coverage-output", "", "Output path for coverage report (default: stdout)")
|
||||
)
|
||||
|
||||
const testTemplate = `package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
// Test{{.CameraName}} tests ONVIF client against {{.CameraDescription}} captured responses.
|
||||
// Capture format: V2 with parameter-aware matching
|
||||
// Total captured operations: {{.TotalExchanges}}
|
||||
func Test{{.CameraName}}(t *testing.T) {
|
||||
// Load capture archive (relative to project root)
|
||||
captureArchive := "{{.CaptureArchiveRelPath}}"
|
||||
|
||||
mockServer, err := onviftesting.NewMockSOAPServerV2(captureArchive)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock server: %v", err)
|
||||
}
|
||||
defer mockServer.Close()
|
||||
|
||||
// Create ONVIF client pointing to mock server
|
||||
client, err := onvif.NewClient(
|
||||
mockServer.URL()+"/onvif/device_service",
|
||||
onvif.WithCredentials("testuser", "testpass"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create ONVIF client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// =========================================================================
|
||||
// Device Service Operations
|
||||
// =========================================================================
|
||||
{{range .DeviceTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Media Service Operations
|
||||
// =========================================================================
|
||||
{{if .NeedsInit}}
|
||||
// Initialize to discover service endpoints (required for Media/PTZ/Imaging)
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
t.Fatalf("Failed to initialize client: %v", err)
|
||||
}
|
||||
{{end}}
|
||||
{{range .MediaTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Profile-Dependent Operations
|
||||
// =========================================================================
|
||||
{{range .ProfileTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// PTZ Operations
|
||||
// =========================================================================
|
||||
{{range .PTZTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Imaging Operations
|
||||
// =========================================================================
|
||||
{{range .ImagingTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
}
|
||||
`
|
||||
|
||||
type TestData struct {
|
||||
PackageName string
|
||||
CameraName string
|
||||
CameraDescription string
|
||||
CaptureArchiveRelPath string
|
||||
TotalExchanges int
|
||||
NeedsInit bool
|
||||
DeviceTests []GeneratedTest
|
||||
MediaTests []GeneratedTest
|
||||
ProfileTests []GeneratedTest
|
||||
PTZTests []GeneratedTest
|
||||
ImagingTests []GeneratedTest
|
||||
}
|
||||
|
||||
type GeneratedTest struct {
|
||||
Name string
|
||||
Code string
|
||||
}
|
||||
|
||||
// operationInfo holds info about captured operations
|
||||
type operationInfo struct {
|
||||
OperationName string
|
||||
ServiceType onviftesting.ServiceType
|
||||
Parameters map[string]interface{}
|
||||
Success bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Set default registry path
|
||||
regPath := *registryPath
|
||||
if regPath == "" {
|
||||
regPath = onviftesting.DefaultRegistryPath
|
||||
}
|
||||
|
||||
// Handle coverage report mode
|
||||
if *coverageReport {
|
||||
generateCoverageReport(regPath)
|
||||
return
|
||||
}
|
||||
|
||||
if *captureArchive == "" {
|
||||
fmt.Println("Error: -capture flag is required")
|
||||
fmt.Println()
|
||||
fmt.Println("Usage:")
|
||||
flag.PrintDefaults()
|
||||
fmt.Println()
|
||||
fmt.Println("Example:")
|
||||
fmt.Println(" ./generate-tests -capture camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_xmlcapture_*.tar.gz")
|
||||
fmt.Println()
|
||||
fmt.Println("Coverage report:")
|
||||
fmt.Println(" ./generate-tests -coverage-report")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputFile := generateTests()
|
||||
|
||||
// Update registry if requested
|
||||
if *updateRegistry {
|
||||
updateCameraRegistry(regPath, *captureArchive, outputFile)
|
||||
}
|
||||
}
|
||||
|
||||
func generateTests() string {
|
||||
// Load capture with V2 support
|
||||
capture, metadata, err := onviftesting.LoadCaptureFromArchiveV2(*captureArchive)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load capture: %v", err)
|
||||
}
|
||||
|
||||
// Extract camera name from archive filename
|
||||
baseName := filepath.Base(*captureArchive)
|
||||
parts := strings.Split(baseName, "_xmlcapture_")
|
||||
cameraID := parts[0]
|
||||
|
||||
// Convert to valid Go identifier
|
||||
cameraName := strings.ReplaceAll(cameraID, "-", "")
|
||||
cameraName = strings.ReplaceAll(cameraName, ".", "")
|
||||
cameraName = strings.ReplaceAll(cameraName, " ", "")
|
||||
|
||||
// Get camera description from metadata or extract from captures
|
||||
cameraDesc := cameraID
|
||||
if metadata != nil && metadata.CameraInfo.Manufacturer != "" {
|
||||
cameraDesc = fmt.Sprintf("%s %s (Firmware: %s)",
|
||||
metadata.CameraInfo.Manufacturer,
|
||||
metadata.CameraInfo.Model,
|
||||
metadata.CameraInfo.FirmwareVersion)
|
||||
} else {
|
||||
// Try to extract from GetDeviceInformation response
|
||||
for _, ex := range capture.Exchanges {
|
||||
if ex.OperationName == "GetDeviceInformation" && ex.Success {
|
||||
manufacturer := extractXMLValue(ex.ResponseBody, "Manufacturer")
|
||||
model := extractXMLValue(ex.ResponseBody, "Model")
|
||||
firmware := extractXMLValue(ex.ResponseBody, "FirmwareVersion")
|
||||
if manufacturer != "" && model != "" {
|
||||
cameraDesc = fmt.Sprintf("%s %s (Firmware: %s)", manufacturer, model, firmware)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze captured operations
|
||||
ops := analyzeOperations(capture)
|
||||
|
||||
// Generate tests by service type
|
||||
testData := TestData{
|
||||
PackageName: *packageName,
|
||||
CameraName: cameraName,
|
||||
CameraDescription: cameraDesc,
|
||||
CaptureArchiveRelPath: makeRelativePath(*captureArchive, *outputDir),
|
||||
TotalExchanges: len(capture.Exchanges),
|
||||
NeedsInit: hasNonDeviceOperations(ops),
|
||||
DeviceTests: generateDeviceTests(ops),
|
||||
MediaTests: generateMediaTests(ops),
|
||||
ProfileTests: generateProfileDependentTests(ops),
|
||||
PTZTests: generatePTZTests(ops),
|
||||
ImagingTests: generateImagingTests(ops),
|
||||
}
|
||||
|
||||
// Generate test file
|
||||
tmpl, err := template.New("test").Parse(testTemplate)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse template: %v", err)
|
||||
}
|
||||
|
||||
outputFile := filepath.Join(*outputDir, fmt.Sprintf("%s_test.go", strings.ToLower(cameraID)))
|
||||
f, err := os.Create(outputFile) //nolint:gosec // Filename is generated from test data, safe
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create output file: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
|
||||
if err := tmpl.Execute(f, testData); err != nil {
|
||||
_ = f.Close()
|
||||
log.Fatalf("Failed to execute template: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("✓ Generated test file: %s\n", outputFile)
|
||||
fmt.Printf(" Camera: %s\n", cameraDesc)
|
||||
fmt.Printf(" Captured operations: %d\n", len(capture.Exchanges))
|
||||
fmt.Printf(" Generated subtests: Device=%d, Media=%d, Profile=%d, PTZ=%d, Imaging=%d\n",
|
||||
len(testData.DeviceTests), len(testData.MediaTests), len(testData.ProfileTests),
|
||||
len(testData.PTZTests), len(testData.ImagingTests))
|
||||
fmt.Println()
|
||||
fmt.Println("Run tests with:")
|
||||
fmt.Printf(" go test -v %s\n", outputFile)
|
||||
|
||||
return outputFile
|
||||
}
|
||||
|
||||
func analyzeOperations(capture *onviftesting.CameraCaptureV2) []operationInfo {
|
||||
var ops []operationInfo
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, ex := range capture.Exchanges {
|
||||
// Create unique key for deduplication
|
||||
key := ex.OperationName
|
||||
if token := ex.GetProfileToken(); token != "" {
|
||||
key += "_" + token
|
||||
} else if token := ex.GetConfigurationToken(); token != "" {
|
||||
key += "_" + token
|
||||
} else if token := ex.GetVideoSourceToken(); token != "" {
|
||||
key += "_" + token
|
||||
}
|
||||
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
|
||||
ops = append(ops, operationInfo{
|
||||
OperationName: ex.OperationName,
|
||||
ServiceType: ex.ServiceType,
|
||||
Parameters: ex.Parameters,
|
||||
Success: ex.Success,
|
||||
})
|
||||
}
|
||||
|
||||
return ops
|
||||
}
|
||||
|
||||
func hasNonDeviceOperations(ops []operationInfo) bool {
|
||||
for _, op := range ops {
|
||||
switch op.ServiceType {
|
||||
case onviftesting.ServiceMedia, onviftesting.ServicePTZ, onviftesting.ServiceImaging:
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func generateDeviceTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Standard device tests
|
||||
deviceOps := map[string]string{
|
||||
"GetDeviceInformation": `info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetDeviceInformation failed: %v", err)
|
||||
return
|
||||
}
|
||||
if info.Manufacturer == "" {
|
||||
t.Error("Manufacturer is empty")
|
||||
}
|
||||
if info.Model == "" {
|
||||
t.Error("Model is empty")
|
||||
}
|
||||
t.Logf("Device: %s %s (Firmware: %s)", info.Manufacturer, info.Model, info.FirmwareVersion)`,
|
||||
|
||||
"GetSystemDateAndTime": `_, err := client.GetSystemDateAndTime(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetSystemDateAndTime failed: %v", err)
|
||||
}`,
|
||||
|
||||
"GetCapabilities": `caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetCapabilities failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Capabilities: Device=%v, Media=%v, Imaging=%v, PTZ=%v",
|
||||
caps.Device != nil, caps.Media != nil, caps.Imaging != nil, caps.PTZ != nil)`,
|
||||
|
||||
"GetHostname": `hostname, err := client.GetHostname(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetHostname failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Hostname: %s", hostname)`,
|
||||
|
||||
"GetScopes": `scopes, err := client.GetScopes(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetScopes failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Scopes: %d", len(scopes))`,
|
||||
|
||||
"GetNetworkInterfaces": `interfaces, err := client.GetNetworkInterfaces(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetNetworkInterfaces failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Network interfaces: %d", len(interfaces))`,
|
||||
|
||||
"GetServices": `services, err := client.GetServices(ctx, true)
|
||||
if err != nil {
|
||||
t.Errorf("GetServices failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Services: %d", len(services))`,
|
||||
}
|
||||
|
||||
// Generate tests for captured operations
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceDevice && op.ServiceType != onviftesting.ServiceUnknown {
|
||||
continue
|
||||
}
|
||||
if code, ok := deviceOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(deviceOps, op.OperationName) // Don't duplicate
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by name for consistent output
|
||||
sort.Slice(tests, func(i, j int) bool {
|
||||
return tests[i].Name < tests[j].Name
|
||||
})
|
||||
|
||||
return tests
|
||||
}
|
||||
|
||||
func generateMediaTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
mediaOps := map[string]string{
|
||||
"GetProfiles": `profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetProfiles failed: %v", err)
|
||||
return
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
t.Error("No profiles returned")
|
||||
}
|
||||
t.Logf("Found %d profile(s)", len(profiles))`,
|
||||
|
||||
"GetVideoSources": `sources, err := client.GetVideoSources(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoSources failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video sources: %d", len(sources))`,
|
||||
|
||||
"GetVideoSourceConfigurations": `configs, err := client.GetVideoSourceConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoSourceConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video source configs: %d", len(configs))`,
|
||||
|
||||
"GetVideoEncoderConfigurations": `configs, err := client.GetVideoEncoderConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoEncoderConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video encoder configs: %d", len(configs))`,
|
||||
|
||||
"GetAudioSources": `sources, err := client.GetAudioSources(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetAudioSources failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Audio sources: %d", len(sources))`,
|
||||
|
||||
"GetAudioSourceConfigurations": `configs, err := client.GetAudioSourceConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetAudioSourceConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Audio source configs: %d", len(configs))`,
|
||||
|
||||
"GetMetadataConfigurations": `configs, err := client.GetMetadataConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetMetadataConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Metadata configs: %d", len(configs))`,
|
||||
}
|
||||
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceMedia {
|
||||
continue
|
||||
}
|
||||
if code, ok := mediaOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(mediaOps, op.OperationName)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(tests, func(i, j int) bool {
|
||||
return tests[i].Name < tests[j].Name
|
||||
})
|
||||
|
||||
return tests
|
||||
}
|
||||
|
||||
func generateProfileDependentTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Group operations by profile token
|
||||
profileOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if token, ok := op.Parameters["ProfileToken"].(string); ok && token != "" {
|
||||
profileOps[token] = append(profileOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate GetStreamURI tests for each profile
|
||||
for token, opList := range profileOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetStreamURI":
|
||||
testName := fmt.Sprintf("GetStreamURI_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`uri, err := client.GetStreamURI(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetStreamURI failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if uri.URI == "" {
|
||||
t.Error("Stream URI is empty")
|
||||
}
|
||||
t.Logf("Stream URI: %%s", uri.URI)`, token),
|
||||
})
|
||||
|
||||
case "GetSnapshotURI":
|
||||
testName := fmt.Sprintf("GetSnapshotURI_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`uri, err := client.GetSnapshotURI(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetSnapshotURI failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if uri.URI == "" {
|
||||
t.Error("Snapshot URI is empty")
|
||||
}
|
||||
t.Logf("Snapshot URI: %%s", uri.URI)`, token),
|
||||
})
|
||||
|
||||
case "GetProfile":
|
||||
testName := fmt.Sprintf("GetProfile_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`profile, err := client.GetProfile(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetProfile failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if profile.Token != "%s" {
|
||||
t.Errorf("Expected token %%s, got %%s", "%s", profile.Token)
|
||||
}
|
||||
t.Logf("Profile: %%s", profile.Name)`, token, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate tests
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func generatePTZTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
ptzOps := map[string]string{
|
||||
"GetNodes": `nodes, err := client.GetNodes(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetNodes failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ nodes: %d", len(nodes))`,
|
||||
|
||||
"GetConfigurations": `configs, err := client.GetConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ configs: %d", len(configs))`,
|
||||
}
|
||||
|
||||
// Group by profile token for status and presets
|
||||
profileOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServicePTZ {
|
||||
continue
|
||||
}
|
||||
if code, ok := ptzOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(ptzOps, op.OperationName)
|
||||
continue
|
||||
}
|
||||
if token, ok := op.Parameters["ProfileToken"].(string); ok && token != "" {
|
||||
profileOps[token] = append(profileOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate profile-specific PTZ tests
|
||||
for token, opList := range profileOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetStatus":
|
||||
testName := fmt.Sprintf("PTZ_GetStatus_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`status, err := client.GetStatus(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetStatus failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ Status retrieved for profile %s")
|
||||
_ = status`, token, token),
|
||||
})
|
||||
|
||||
case "GetPresets":
|
||||
testName := fmt.Sprintf("PTZ_GetPresets_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`presets, err := client.GetPresets(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetPresets failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Found %%d preset(s) for profile %s", len(presets))`, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func generateImagingTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Group by video source token
|
||||
sourceOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceImaging {
|
||||
continue
|
||||
}
|
||||
if token, ok := op.Parameters["VideoSourceToken"].(string); ok && token != "" {
|
||||
sourceOps[token] = append(sourceOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
for token, opList := range sourceOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetImagingSettings":
|
||||
testName := fmt.Sprintf("GetImagingSettings_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`settings, err := client.GetImagingSettings(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetImagingSettings failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Imaging settings retrieved for source %s")
|
||||
_ = settings`, token, token),
|
||||
})
|
||||
|
||||
case "GetOptions":
|
||||
testName := fmt.Sprintf("GetImagingOptions_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`options, err := client.GetOptions(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetOptions failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Imaging options retrieved for source %s")
|
||||
_ = options`, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func sanitizeToken(token string) string {
|
||||
// Make token safe for test name
|
||||
token = strings.ReplaceAll(token, "-", "_")
|
||||
token = strings.ReplaceAll(token, ".", "_")
|
||||
token = strings.ReplaceAll(token, " ", "_")
|
||||
// Truncate if too long
|
||||
if len(token) > 20 {
|
||||
token = token[:20]
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func makeRelativePath(archivePath, outputDir string) string {
|
||||
if absOutput, err := filepath.Abs(outputDir); err == nil {
|
||||
if absArchive, err := filepath.Abs(archivePath); err == nil {
|
||||
if rel, err := filepath.Rel(filepath.Dir(absOutput), absArchive); err == nil {
|
||||
return rel
|
||||
}
|
||||
}
|
||||
}
|
||||
return archivePath
|
||||
}
|
||||
|
||||
func extractXMLValue(xmlStr, tagName string) string {
|
||||
start := fmt.Sprintf("<%s>", tagName)
|
||||
end := fmt.Sprintf("</%s>", tagName)
|
||||
|
||||
startIdx := strings.Index(xmlStr, start)
|
||||
if startIdx == -1 {
|
||||
start = fmt.Sprintf(":%s>", tagName)
|
||||
startIdx = strings.Index(xmlStr, start)
|
||||
if startIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
startIdx += len(start)
|
||||
} else {
|
||||
startIdx += len(start)
|
||||
}
|
||||
|
||||
endIdx := strings.Index(xmlStr[startIdx:], end)
|
||||
if endIdx == -1 {
|
||||
end = fmt.Sprintf(":/%s>", tagName)
|
||||
endIdx = strings.Index(xmlStr[startIdx:], end)
|
||||
if endIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
return strings.TrimSpace(xmlStr[startIdx : startIdx+endIdx])
|
||||
}
|
||||
|
||||
// updateCameraRegistry updates the registry with camera information from the capture.
|
||||
func updateCameraRegistry(regPath, archivePath, testFile string) {
|
||||
registry, err := onviftesting.LoadRegistry(regPath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to load registry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := onviftesting.CreateCameraEntryFromCapture(archivePath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to create registry entry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the test file path (relative to registry directory)
|
||||
if testFile != "" {
|
||||
regDir := filepath.Dir(regPath)
|
||||
if absTest, err := filepath.Abs(testFile); err == nil {
|
||||
if absRegDir, err := filepath.Abs(regDir); err == nil {
|
||||
if rel, err := filepath.Rel(absRegDir, absTest); err == nil {
|
||||
entry.TestFile = rel
|
||||
}
|
||||
}
|
||||
}
|
||||
if entry.TestFile == "" {
|
||||
entry.TestFile = filepath.Base(testFile)
|
||||
}
|
||||
}
|
||||
|
||||
// Add or update the camera entry
|
||||
registry.AddCamera(*entry)
|
||||
|
||||
// Update coverage statistics
|
||||
updateRegistryCoverage(registry, archivePath)
|
||||
|
||||
// Save registry
|
||||
if err := onviftesting.SaveRegistry(registry, regPath); err != nil {
|
||||
log.Printf("Warning: Failed to save registry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✓ Registry updated: %s\n", regPath)
|
||||
fmt.Printf(" Camera ID: %s\n", entry.ID)
|
||||
fmt.Printf(" Total cameras in registry: %d\n", len(registry.Cameras))
|
||||
}
|
||||
|
||||
// updateRegistryCoverage calculates coverage from captured operations.
|
||||
func updateRegistryCoverage(registry *onviftesting.Registry, archivePath string) {
|
||||
capture, _, err := onviftesting.LoadCaptureFromArchiveV2(archivePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Count unique operations per service
|
||||
serviceCounts := make(map[string]map[string]bool)
|
||||
for _, ex := range capture.Exchanges {
|
||||
service := string(ex.ServiceType)
|
||||
if service == "" || service == "Unknown" {
|
||||
continue
|
||||
}
|
||||
if serviceCounts[service] == nil {
|
||||
serviceCounts[service] = make(map[string]bool)
|
||||
}
|
||||
serviceCounts[service][ex.OperationName] = true
|
||||
}
|
||||
|
||||
// Get totals from operations registry
|
||||
opCounts := onviftesting.GetOperationCount()
|
||||
|
||||
// Update coverage
|
||||
registry.Coverage = make(map[string]onviftesting.Coverage)
|
||||
for service, ops := range serviceCounts {
|
||||
total := 0
|
||||
switch service {
|
||||
case "Device":
|
||||
total = opCounts.Device
|
||||
case "Media":
|
||||
total = opCounts.Media
|
||||
case "PTZ":
|
||||
total = opCounts.PTZ
|
||||
case "Imaging":
|
||||
total = opCounts.Imaging
|
||||
case "Event":
|
||||
total = opCounts.Event
|
||||
case "DeviceIO":
|
||||
total = opCounts.DeviceIO
|
||||
}
|
||||
|
||||
registry.Coverage[service] = onviftesting.Coverage{
|
||||
Total: total,
|
||||
Captured: len(ops),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// generateCoverageReport generates a coverage report from the registry.
|
||||
func generateCoverageReport(regPath string) {
|
||||
registry, err := onviftesting.LoadRegistry(regPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load registry: %v", err)
|
||||
}
|
||||
|
||||
// Generate markdown report
|
||||
report := generateCoverageMarkdown(registry)
|
||||
|
||||
// Output to file or stdout
|
||||
if *coverageOutput != "" {
|
||||
if err := os.WriteFile(*coverageOutput, []byte(report), 0600); err != nil { //nolint:mnd
|
||||
log.Fatalf("Failed to write coverage report: %v", err)
|
||||
}
|
||||
fmt.Printf("✓ Coverage report written to: %s\n", *coverageOutput)
|
||||
} else {
|
||||
fmt.Println(report)
|
||||
}
|
||||
}
|
||||
|
||||
// generateCoverageMarkdown creates a markdown coverage report.
|
||||
func generateCoverageMarkdown(registry *onviftesting.Registry) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("# ONVIF Operation Coverage Report\n\n")
|
||||
sb.WriteString(fmt.Sprintf("Generated: %s\n\n", time.Now().Format("2006-01-02 15:04:05")))
|
||||
|
||||
// Summary
|
||||
sb.WriteString("## Summary\n\n")
|
||||
sb.WriteString(fmt.Sprintf("- **Total Cameras**: %d\n", len(registry.Cameras)))
|
||||
|
||||
total, captured := registry.GetTotalCoverage()
|
||||
if total > 0 {
|
||||
sb.WriteString(fmt.Sprintf("- **Overall Coverage**: %.1f%% (%d/%d operations)\n\n",
|
||||
float64(captured)/float64(total)*100, captured, total))
|
||||
}
|
||||
|
||||
// Cameras
|
||||
if len(registry.Cameras) > 0 {
|
||||
sb.WriteString("## Registered Cameras\n\n")
|
||||
sb.WriteString("| Manufacturer | Model | Firmware | Operations | Capabilities |\n")
|
||||
sb.WriteString("|--------------|-------|----------|------------|---------------|\n")
|
||||
|
||||
for _, cam := range registry.Cameras {
|
||||
caps := strings.Join(cam.Capabilities, ", ")
|
||||
sb.WriteString(fmt.Sprintf("| %s | %s | %s | %d | %s |\n",
|
||||
cam.Manufacturer, cam.Model, cam.Firmware, cam.OperationsCaptured, caps))
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Coverage by service
|
||||
if len(registry.Coverage) > 0 {
|
||||
sb.WriteString("## Coverage by Service\n\n")
|
||||
sb.WriteString("| Service | Total | Captured | Coverage |\n")
|
||||
sb.WriteString("|---------|-------|----------|----------|\n")
|
||||
|
||||
services := []string{"Device", "Media", "PTZ", "Imaging", "Event", "DeviceIO"}
|
||||
for _, service := range services {
|
||||
if cov, ok := registry.Coverage[service]; ok {
|
||||
pct := 0.0
|
||||
if cov.Total > 0 {
|
||||
pct = float64(cov.Captured) / float64(cov.Total) * 100
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("| %s | %d | %d | %.1f%% |\n",
|
||||
service, cov.Total, cov.Captured, pct))
|
||||
}
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Missing operations
|
||||
sb.WriteString("## Operation Specifications\n\n")
|
||||
opCounts := onviftesting.GetOperationCount()
|
||||
sb.WriteString(fmt.Sprintf("- Device: %d operations defined\n", opCounts.Device))
|
||||
sb.WriteString(fmt.Sprintf("- Media: %d operations defined\n", opCounts.Media))
|
||||
sb.WriteString(fmt.Sprintf("- PTZ: %d operations defined\n", opCounts.PTZ))
|
||||
sb.WriteString(fmt.Sprintf("- Imaging: %d operations defined\n", opCounts.Imaging))
|
||||
sb.WriteString(fmt.Sprintf("- Event: %d operations defined\n", opCounts.Event))
|
||||
sb.WriteString(fmt.Sprintf("- DeviceIO: %d operations defined\n", opCounts.DeviceIO))
|
||||
sb.WriteString(fmt.Sprintf("\n**Total**: %d read-only operations tracked\n", opCounts.Total))
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ASCIIConfig controls ASCII art generation parameters.
|
||||
type ASCIIConfig struct {
|
||||
Width int // Output width in characters
|
||||
Height int // Output height in characters
|
||||
Invert bool // Invert brightness
|
||||
Quality string // "high", "medium", "low"
|
||||
}
|
||||
|
||||
const (
|
||||
defaultASCIIWidth = 120
|
||||
defaultASCIIHeight = 40
|
||||
maxColorValue = 255
|
||||
bitShift8 = 8
|
||||
bufferSize1024 = 1024
|
||||
largeASCIIWidth = 160
|
||||
largeASCIIHeight = 50
|
||||
defaultQuality = "medium"
|
||||
)
|
||||
|
||||
// DefaultASCIIConfig returns a sensible default configuration.
|
||||
func DefaultASCIIConfig() ASCIIConfig {
|
||||
return ASCIIConfig{
|
||||
Width: defaultASCIIWidth,
|
||||
Height: defaultASCIIHeight,
|
||||
Invert: false,
|
||||
Quality: "medium",
|
||||
}
|
||||
}
|
||||
|
||||
// ASCIICharsets define different character options.
|
||||
var (
|
||||
// Full charset with many shades.
|
||||
charsetFull = []rune{' ', '.', ':', '-', '=', '+', '*', '#', '%', '@'}
|
||||
|
||||
// Medium charset - balanced.
|
||||
charsetMedium = []rune{' ', '.', '-', '=', '+', '#', '@'}
|
||||
|
||||
// Simple charset - just a few chars.
|
||||
charsetSimple = []rune{' ', '-', '#', '@'}
|
||||
|
||||
// Block charset - using block characters.
|
||||
charsetBlock = []rune{' ', '░', '▒', '▓', '█'}
|
||||
|
||||
// Detailed charset.
|
||||
charsetDetailed = []rune{' ', '`', '.', ',', ':', ';', '!', 'i', 'l', 'I',
|
||||
'o', 'O', '0', 'e', 'E', 'p', 'P', 'x', 'X', '$', 'D', 'W', 'M', '@', '#'}
|
||||
)
|
||||
|
||||
// ImageToASCII converts image data to ASCII art. Supports JPEG and PNG formats.
|
||||
func ImageToASCII(imageData []byte, config ASCIIConfig) (string, error) {
|
||||
// Decode image from bytes
|
||||
img, _, err := image.Decode(bytes.NewReader(imageData))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
return imageToASCIIFromImage(img, config, "unknown")
|
||||
}
|
||||
|
||||
// imageToASCIIFromImage is the core conversion function.
|
||||
//
|
||||
//nolint:gocyclo // Image to ASCII conversion has high complexity due to multiple pixel processing paths
|
||||
func imageToASCIIFromImage(img image.Image, config ASCIIConfig, format string) (string, error) { //nolint:unparam // format reserved for future use
|
||||
// Validate configuration
|
||||
if config.Width <= 0 {
|
||||
config.Width = 120
|
||||
}
|
||||
if config.Height <= 0 {
|
||||
config.Height = defaultASCIIHeight
|
||||
}
|
||||
if config.Quality == "" {
|
||||
config.Quality = defaultQuality
|
||||
}
|
||||
|
||||
// Select character set based on quality
|
||||
charset := charsetMedium
|
||||
switch strings.ToLower(config.Quality) {
|
||||
case "high", "detailed":
|
||||
charset = charsetDetailed
|
||||
case "medium":
|
||||
charset = charsetMedium
|
||||
case "low", "simple":
|
||||
charset = charsetSimple
|
||||
case "block":
|
||||
charset = charsetBlock
|
||||
case "full":
|
||||
charset = charsetFull
|
||||
}
|
||||
|
||||
// Get image bounds
|
||||
bounds := img.Bounds()
|
||||
width := bounds.Max.X - bounds.Min.X
|
||||
height := bounds.Max.Y - bounds.Min.Y
|
||||
|
||||
// Calculate scaling factors
|
||||
scaleX := float64(width) / float64(config.Width)
|
||||
scaleY := float64(height) / float64(config.Height)
|
||||
|
||||
// Build ASCII representation
|
||||
var result strings.Builder
|
||||
for y := 0; y < config.Height; y++ {
|
||||
for x := 0; x < config.Width; x++ {
|
||||
// Sample pixel from image
|
||||
srcX := int(float64(x) * scaleX)
|
||||
srcY := int(float64(y) * scaleY)
|
||||
|
||||
// Bounds check
|
||||
if srcX >= width {
|
||||
srcX = width - 1
|
||||
}
|
||||
if srcY >= height {
|
||||
srcY = height - 1
|
||||
}
|
||||
|
||||
// Get pixel color
|
||||
r, g, b, _ := img.At(bounds.Min.X+srcX, bounds.Min.Y+srcY).RGBA()
|
||||
|
||||
// Convert to grayscale brightness (0-255)
|
||||
brightness := calculateBrightness(r, g, b)
|
||||
|
||||
// Invert if requested
|
||||
if config.Invert {
|
||||
brightness = maxColorValue - brightness
|
||||
}
|
||||
|
||||
// Map brightness to character
|
||||
charIndex := int(float64(brightness) / float64(maxColorValue) * float64(len(charset)-1))
|
||||
if charIndex >= len(charset) {
|
||||
charIndex = len(charset) - 1
|
||||
}
|
||||
if charIndex < 0 {
|
||||
charIndex = 0
|
||||
}
|
||||
|
||||
result.WriteRune(charset[charIndex])
|
||||
}
|
||||
result.WriteRune('\n')
|
||||
}
|
||||
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
// Uses standard luminance formula.
|
||||
func calculateBrightness(r, g, b uint32) int {
|
||||
// Convert 16-bit color to 8-bit
|
||||
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
|
||||
brightness := int(0.299*float64(r8) + 0.587*float64(g8) + 0.114*float64(b8))
|
||||
|
||||
if brightness > maxColorValue {
|
||||
brightness = maxColorValue
|
||||
}
|
||||
if brightness < 0 {
|
||||
brightness = 0
|
||||
}
|
||||
|
||||
return brightness
|
||||
}
|
||||
|
||||
// FormatASCIIOutput formats ASCII art with header and footer info.
|
||||
func FormatASCIIOutput(ascii string, imageInfo ImageInfo) string {
|
||||
var result strings.Builder
|
||||
|
||||
// Header
|
||||
result.WriteString("\n")
|
||||
result.WriteString("╔════════════════════════════════════════════════════════════════╗\n")
|
||||
result.WriteString("║ 📷 CAMERA SNAPSHOT (ASCII) ║\n")
|
||||
result.WriteString("╚════════════════════════════════════════════════════════════════╝\n")
|
||||
result.WriteString("\n")
|
||||
|
||||
// Image info
|
||||
if imageInfo.Width > 0 && imageInfo.Height > 0 {
|
||||
result.WriteString(fmt.Sprintf("📊 Original: %dx%d pixels\n", imageInfo.Width, imageInfo.Height))
|
||||
}
|
||||
if imageInfo.SizeBytes > 0 {
|
||||
result.WriteString(fmt.Sprintf("💾 Size: %s\n", formatBytes(imageInfo.SizeBytes)))
|
||||
}
|
||||
if imageInfo.CaptureTime != "" {
|
||||
result.WriteString(fmt.Sprintf("⏱️ Captured: %s\n", imageInfo.CaptureTime))
|
||||
}
|
||||
if imageInfo.Format != "" {
|
||||
result.WriteString(fmt.Sprintf("📁 Format: %s\n", imageInfo.Format))
|
||||
}
|
||||
result.WriteString("\n")
|
||||
|
||||
// ASCII art
|
||||
result.WriteString(ascii)
|
||||
|
||||
// Footer
|
||||
result.WriteString("\n")
|
||||
result.WriteString("╔════════════════════════════════════════════════════════════════╗\n")
|
||||
result.WriteString("💡 Tip: Higher resolution snapshots show better detail\n")
|
||||
result.WriteString("╚════════════════════════════════════════════════════════════════╝\n")
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
// ImageInfo holds metadata about the snapshot.
|
||||
type ImageInfo struct {
|
||||
Width int // Original width in pixels
|
||||
Height int // Original height in pixels
|
||||
SizeBytes int64 // File size in bytes
|
||||
Format string // Image format (JPEG, PNG, etc)
|
||||
CaptureTime string // Capture timestamp
|
||||
}
|
||||
|
||||
// formatBytes converts bytes to human-readable format.
|
||||
func formatBytes(byteCount int64) string {
|
||||
if byteCount < bufferSize1024 {
|
||||
return fmt.Sprintf("%d B", byteCount)
|
||||
}
|
||||
const kbSize = 1024
|
||||
const mbSize = 1024 * 1024
|
||||
if byteCount < mbSize {
|
||||
return fmt.Sprintf("%.1f KB", float64(byteCount)/kbSize)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.1f MB", float64(byteCount)/mbSize)
|
||||
}
|
||||
|
||||
// CreateASCIIHighQuality creates a high-quality ASCII representation.
|
||||
func CreateASCIIHighQuality(imageData []byte) (string, error) {
|
||||
config := ASCIIConfig{
|
||||
Width: largeASCIIWidth,
|
||||
Height: largeASCIIHeight,
|
||||
Invert: false,
|
||||
Quality: "high",
|
||||
}
|
||||
|
||||
return ImageToASCII(imageData, config)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrNoNetworkInterfaces is returned when no network interfaces are found.
|
||||
ErrNoNetworkInterfaces = errors.New("no network interfaces found")
|
||||
|
||||
// ErrNoCamerasFound is returned when no cameras are found on any interface.
|
||||
ErrNoCamerasFound = errors.New("no cameras found on any interface")
|
||||
|
||||
// ErrNoActiveInterfaces is returned when no active interfaces are available for discovery.
|
||||
ErrNoActiveInterfaces = errors.New("no active interfaces available for discovery")
|
||||
|
||||
// ErrNoProfilesFound is returned when no profiles are found.
|
||||
ErrNoProfilesFound = errors.New("no profiles found")
|
||||
|
||||
// ErrNoVideoSourceConfiguration is returned when no video source configuration is found.
|
||||
ErrNoVideoSourceConfiguration = errors.New("no video source configuration found")
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,365 +0,0 @@
|
||||
# ONVIF Camera Diagnostic Utility
|
||||
|
||||
A comprehensive diagnostic tool for collecting detailed information from ONVIF cameras. This utility helps analyze camera capabilities, troubleshoot issues, and generate reports for creating camera-specific tests.
|
||||
|
||||
## Features
|
||||
|
||||
✅ **Comprehensive Testing** - Tests all major ONVIF operations:
|
||||
- Device information and capabilities
|
||||
- Media profiles and streaming
|
||||
- Video encoder configurations
|
||||
- Imaging settings
|
||||
- PTZ status and presets (if available)
|
||||
- System date/time
|
||||
|
||||
✅ **Detailed Reporting** - Generates JSON reports with:
|
||||
- All successful operations with response data
|
||||
- Failed operations with error details
|
||||
- Response times for performance analysis
|
||||
- Structured data ready for test generation
|
||||
|
||||
✅ **Easy to Use** - Simple command-line interface with minimal requirements
|
||||
|
||||
✅ **XML Debugging** - For detailed debugging, see the companion `onvif-xml-capture` utility that captures raw SOAP XML
|
||||
|
||||
✅ **Helpful for**:
|
||||
- Creating camera-specific integration tests
|
||||
- Troubleshooting ONVIF compatibility issues
|
||||
- Analyzing camera capabilities
|
||||
- Debugging connection problems
|
||||
- Documenting camera configurations
|
||||
|
||||
## Installation
|
||||
|
||||
### Option 1: Build from source
|
||||
```bash
|
||||
cd /path/to/onvif-go
|
||||
go build -o onvif-diagnostics ./cmd/onvif-diagnostics/
|
||||
```
|
||||
|
||||
### Option 2: Install globally
|
||||
```bash
|
||||
go install ./cmd/onvif-diagnostics
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234"
|
||||
```
|
||||
|
||||
### With XML Capture (for debugging)
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-capture-xml \
|
||||
-verbose
|
||||
```
|
||||
|
||||
This creates two files:
|
||||
- `Manufacturer_Model_Firmware_timestamp.json` - Diagnostic report
|
||||
- `Manufacturer_Model_Firmware_xmlcapture_timestamp.tar.gz` - Raw SOAP XML archive
|
||||
|
||||
### Verbose Output
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-verbose
|
||||
```
|
||||
|
||||
### Capture Raw SOAP XML
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-capture-xml
|
||||
```
|
||||
|
||||
Enables XML traffic capture and creates a compressed tar.gz archive containing all SOAP request/response pairs. Useful for debugging XML parsing issues or analyzing camera behavior.
|
||||
|
||||
The archive contains:
|
||||
- `capture_001_GetDeviceInformation.json` - Request/response metadata with operation name
|
||||
- `capture_001_GetDeviceInformation_request.xml` - Formatted SOAP request
|
||||
- `capture_001_GetDeviceInformation_response.xml` - Formatted SOAP response
|
||||
- `capture_002_GetSystemDateAndTime.json` - Next operation metadata
|
||||
- ... (one set per SOAP operation, named by operation type)
|
||||
|
||||
Each file is named with the SOAP operation (e.g., GetDeviceInformation, GetProfiles) for easy identification.
|
||||
|
||||
Extract the archive:
|
||||
```bash
|
||||
tar -xzf camera-logs/Camera_Model_xmlcapture_timestamp.tar.gz
|
||||
```
|
||||
|
||||
### Custom Output Directory
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-output ./my-camera-reports
|
||||
```
|
||||
|
||||
### All Options
|
||||
```
|
||||
Usage of ./onvif-diagnostics:
|
||||
-endpoint string
|
||||
ONVIF device endpoint (e.g., http://192.168.1.201/onvif/device_service)
|
||||
-username string
|
||||
ONVIF username
|
||||
-password string
|
||||
ONVIF password
|
||||
-output string
|
||||
Output directory for logs (default "./camera-logs")
|
||||
-timeout int
|
||||
Request timeout in seconds (default 30)
|
||||
-verbose
|
||||
Verbose output
|
||||
-include-raw
|
||||
Include raw SOAP responses (increases file size)
|
||||
```
|
||||
|
||||
## Example Output
|
||||
|
||||
```
|
||||
ONVIF Camera Diagnostic Utility v1.0.0
|
||||
========================================
|
||||
|
||||
Starting diagnostic collection...
|
||||
|
||||
→ 1. Getting device information...
|
||||
✓ Manufacturer: Bosch, Model: FLEXIDOME indoor 5100i IR
|
||||
→ 2. Getting system date and time...
|
||||
✓ Retrieved
|
||||
→ 3. Getting capabilities...
|
||||
✓ Services: Device, Media, Imaging, Events, Analytics
|
||||
→ 4. Discovering service endpoints...
|
||||
✓ Service endpoints discovered
|
||||
→ 5. Getting media profiles...
|
||||
✓ Found 4 profile(s)
|
||||
→ 6. Getting stream URIs for all profiles...
|
||||
✓ Retrieved 4/4 stream URIs
|
||||
→ 7. Getting snapshot URIs for all profiles...
|
||||
✓ Retrieved 4/4 snapshot URIs
|
||||
→ 8. Getting video encoder configurations...
|
||||
✓ Retrieved 4/4 video encoder configs
|
||||
→ 9. Getting imaging settings...
|
||||
✓ Retrieved 1/1 imaging settings
|
||||
→ 10. Getting PTZ status...
|
||||
ℹ No PTZ configurations found
|
||||
→ 11. Getting PTZ presets...
|
||||
ℹ No PTZ configurations found
|
||||
→ Saving diagnostic report...
|
||||
|
||||
========================================
|
||||
✓ Diagnostic collection complete!
|
||||
Report saved to: camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_20251107-193656.json
|
||||
Total errors: 0
|
||||
|
||||
Device: Bosch FLEXIDOME indoor 5100i IR
|
||||
Firmware: 8.71.0066
|
||||
Profiles: 4
|
||||
|
||||
Please share this file for analysis and test creation.
|
||||
========================================
|
||||
```
|
||||
|
||||
## Report Structure
|
||||
|
||||
The generated JSON report includes:
|
||||
|
||||
```json
|
||||
{
|
||||
"timestamp": "2025-11-07T19:36:56Z",
|
||||
"utility_version": "1.0.0",
|
||||
"connection_info": {
|
||||
"endpoint": "http://192.168.1.201/onvif/device_service",
|
||||
"username": "service",
|
||||
"test_date": "2025-11-07"
|
||||
},
|
||||
"device_info": {
|
||||
"success": true,
|
||||
"data": {
|
||||
"manufacturer": "Bosch",
|
||||
"model": "FLEXIDOME indoor 5100i IR",
|
||||
"firmware_version": "8.71.0066",
|
||||
"serial_number": "404754734001050102",
|
||||
"hardware_id": "F000B543"
|
||||
},
|
||||
"response_time": "21.5ms"
|
||||
},
|
||||
"profiles": {
|
||||
"success": true,
|
||||
"count": 4,
|
||||
"data": [ /* profile details */ ]
|
||||
},
|
||||
"stream_uris": [ /* stream URI results for each profile */ ],
|
||||
"errors": [ /* any errors encountered */ ]
|
||||
}
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Creating Camera-Specific Tests
|
||||
Run the diagnostic on your camera and share the JSON file. The report contains all the information needed to create comprehensive integration tests.
|
||||
|
||||
### 2. Troubleshooting Connection Issues
|
||||
If your camera isn't working, run diagnostics to see exactly which operations fail and what error messages are returned.
|
||||
|
||||
### 3. Comparing Cameras
|
||||
Run diagnostics on multiple cameras to compare capabilities, response times, and compatibility.
|
||||
|
||||
### 4. Documentation
|
||||
Generate detailed reports of camera configurations for documentation purposes.
|
||||
|
||||
## Interpreting Results
|
||||
|
||||
### Success Indicators
|
||||
- ✓ Green checkmarks indicate successful operations
|
||||
- Response times help identify performance issues
|
||||
- High success rates indicate good compatibility
|
||||
|
||||
### Error Indicators
|
||||
- ✗ Red X marks indicate failed operations
|
||||
- ℹ Info symbols indicate optional features not available
|
||||
- Check the `errors` array in JSON for detailed error messages
|
||||
|
||||
### Common Issues
|
||||
|
||||
**All operations fail:**
|
||||
- Check network connectivity
|
||||
- Verify endpoint URL is correct
|
||||
- Ensure camera is powered on
|
||||
|
||||
**Authentication errors:**
|
||||
- Verify username and password
|
||||
- Check user permissions on camera
|
||||
|
||||
**Some profiles fail:**
|
||||
- Camera may have different capabilities per profile
|
||||
- Some operations may not be supported by all profiles
|
||||
|
||||
**Timeout errors:**
|
||||
- Increase timeout with `-timeout 60`
|
||||
- Check network latency
|
||||
- Verify camera is responding
|
||||
|
||||
## Sharing Reports
|
||||
|
||||
When sharing diagnostic reports:
|
||||
|
||||
1. **Anonymize if needed** - The report includes:
|
||||
- IP addresses (in endpoint)
|
||||
- Usernames (not passwords)
|
||||
- Serial numbers
|
||||
|
||||
2. **What to share**:
|
||||
- The complete JSON file
|
||||
- Any console output showing errors
|
||||
- Camera model and firmware version
|
||||
|
||||
3. **Where to share**:
|
||||
- GitHub Issues
|
||||
- Email for analysis
|
||||
- Pull request descriptions
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Batch Testing Multiple Cameras
|
||||
Create a script to test multiple cameras:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cameras=(
|
||||
"192.168.1.201:service:password1"
|
||||
"192.168.1.202:admin:password2"
|
||||
"192.168.1.203:user:password3"
|
||||
)
|
||||
|
||||
for camera in "${cameras[@]}"; do
|
||||
IFS=':' read -r ip user pass <<< "$camera"
|
||||
echo "Testing camera at $ip..."
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://$ip/onvif/device_service" \
|
||||
-username "$user" \
|
||||
-password "$pass"
|
||||
done
|
||||
```
|
||||
|
||||
### Automated Testing
|
||||
Include in CI/CD pipelines:
|
||||
|
||||
```yaml
|
||||
- name: Run ONVIF Diagnostics
|
||||
run: |
|
||||
./onvif-diagnostics \
|
||||
-endpoint "${{ secrets.CAMERA_ENDPOINT }}" \
|
||||
-username "${{ secrets.CAMERA_USERNAME }}" \
|
||||
-password "${{ secrets.CAMERA_PASSWORD }}" \
|
||||
-output ./reports
|
||||
|
||||
- name: Upload Diagnostic Reports
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: camera-diagnostics
|
||||
path: ./reports/
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### Adding New Tests
|
||||
|
||||
To add new diagnostic tests, edit `cmd/onvif-diagnostics/main.go`:
|
||||
|
||||
1. Create a new test function following the pattern:
|
||||
```go
|
||||
func testNewOperation(ctx context.Context, client *onvif.Client, report *CameraReport) *NewOperationResult {
|
||||
// Implementation
|
||||
}
|
||||
```
|
||||
|
||||
2. Add result struct to store data
|
||||
3. Call the test in main()
|
||||
4. Update report structure
|
||||
|
||||
### Building for Different Platforms
|
||||
|
||||
```bash
|
||||
# Linux
|
||||
GOOS=linux GOARCH=amd64 go build -o onvif-diagnostics-linux ./cmd/onvif-diagnostics/
|
||||
|
||||
# Windows
|
||||
GOOS=windows GOARCH=amd64 go build -o onvif-diagnostics.exe ./cmd/onvif-diagnostics/
|
||||
|
||||
# macOS ARM
|
||||
GOOS=darwin GOARCH=arm64 go build -o onvif-diagnostics-mac-arm ./cmd/onvif-diagnostics/
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Same as parent project.
|
||||
|
||||
## Support
|
||||
|
||||
For issues or questions:
|
||||
1. Run diagnostics with `-verbose` flag
|
||||
2. Share the generated JSON report
|
||||
3. **For XML parsing issues**: Use `onvif-xml-capture` utility to capture raw SOAP XML
|
||||
4. Open a GitHub issue with the report attached
|
||||
|
||||
## Related Tools
|
||||
|
||||
- **onvif-xml-capture** - Captures raw SOAP XML requests/responses for detailed debugging
|
||||
- Location: `cmd/onvif-xml-capture/`
|
||||
- Use when: Diagnostic report shows errors and you need to see raw XML
|
||||
- See: `XML_DEBUGGING_SOLUTION.md` for complete guide
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,442 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
"github.com/0x524a/onvif-go/discovery"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultUsername = "admin"
|
||||
defaultTimeout = 10
|
||||
defaultRetryDelay = 5
|
||||
ptzTimeout = 30
|
||||
ptzStepSize = 2
|
||||
ptzSpeed = 0.5
|
||||
maxBodyPreview = 200
|
||||
)
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("🎥 Quick ONVIF Camera Tool")
|
||||
fmt.Println("==========================")
|
||||
fmt.Println()
|
||||
|
||||
for {
|
||||
fmt.Println("What would you like to do?")
|
||||
fmt.Println("1. 🔍 Discover cameras")
|
||||
fmt.Println("2. 🌐 List network interfaces")
|
||||
fmt.Println("3. 📹 Connect to camera")
|
||||
fmt.Println("4. 🎮 PTZ demo")
|
||||
fmt.Println("5. 📡 Get stream URLs")
|
||||
fmt.Println("0. Exit")
|
||||
fmt.Print("\nChoice: ")
|
||||
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
input, _ := reader.ReadString('\n')
|
||||
choice := strings.TrimSpace(input)
|
||||
|
||||
switch choice {
|
||||
case "1":
|
||||
discoverCameras()
|
||||
case "2":
|
||||
listNetworkInterfaces()
|
||||
case "3":
|
||||
connectAndShowInfo()
|
||||
case "4":
|
||||
ptzDemo()
|
||||
case "5":
|
||||
getStreamURLs()
|
||||
case "0", "q", "quit":
|
||||
fmt.Println("Goodbye! 👋")
|
||||
|
||||
return
|
||||
default:
|
||||
fmt.Println("Invalid choice. Please try again.")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func discoverCameras() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("🔍 Discovering cameras on network...")
|
||||
|
||||
// Ask if user wants to use a specific interface
|
||||
fmt.Print("Use specific network interface? (y/n) [n]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
useInterface, _ := reader.ReadString('\n')
|
||||
useInterface = strings.ToLower(strings.TrimSpace(useInterface))
|
||||
|
||||
var opts *discovery.DiscoverOptions
|
||||
if useInterface == "y" || useInterface == "yes" {
|
||||
// List interfaces
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("\nAvailable interfaces:")
|
||||
for i, iface := range interfaces {
|
||||
fmt.Printf(" %d. %s (%v)\n", i+1, iface.Name, iface.Addresses)
|
||||
}
|
||||
|
||||
fmt.Print("\nEnter interface name or IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ifaceInput, _ := reader.ReadString('\n')
|
||||
ifaceInput = strings.TrimSpace(ifaceInput)
|
||||
|
||||
if ifaceInput != "" {
|
||||
opts = &discovery.DiscoverOptions{
|
||||
NetworkInterface: ifaceInput,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &discovery.DiscoverOptions{}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout*time.Second)
|
||||
defer cancel()
|
||||
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, defaultRetryDelay*time.Second, opts)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(devices) == 0 {
|
||||
fmt.Println("No cameras found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d camera(s):\n", len(devices))
|
||||
for i, device := range devices {
|
||||
fmt.Printf(" %d. %s (%s)\n", i+1, device.GetName(), device.GetDeviceEndpoint())
|
||||
}
|
||||
}
|
||||
|
||||
func listNetworkInterfaces() {
|
||||
fmt.Println("🌐 Network Interfaces")
|
||||
fmt.Println("====================")
|
||||
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(interfaces) == 0 {
|
||||
fmt.Println("No network interfaces found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d interface(s):\n\n", len(interfaces))
|
||||
|
||||
for _, iface := range interfaces {
|
||||
upStr := "Up"
|
||||
if !iface.Up {
|
||||
upStr = "Down"
|
||||
}
|
||||
|
||||
multicastStr := "Yes"
|
||||
if !iface.Multicast {
|
||||
multicastStr = "No"
|
||||
}
|
||||
|
||||
fmt.Printf("📡 %s (%s, Multicast: %s)\n", iface.Name, upStr, multicastStr)
|
||||
|
||||
if len(iface.Addresses) > 0 {
|
||||
for _, addr := range iface.Addresses {
|
||||
fmt.Printf(" └─ %s\n", addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func connectAndShowInfo() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
fmt.Printf("Connecting to %s...\n", endpoint)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(ptzTimeout*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Get device info
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Connection failed: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Connected!\n")
|
||||
fmt.Printf("📹 %s %s\n", info.Manufacturer, info.Model)
|
||||
fmt.Printf("🔧 Firmware: %s\n", info.FirmwareVersion)
|
||||
|
||||
// Initialize and get profiles
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err == nil && len(profiles) > 0 {
|
||||
fmt.Printf("📺 %d profile(s) available\n", len(profiles))
|
||||
|
||||
// Show first stream URL
|
||||
streamURI, err := client.GetStreamURI(ctx, profiles[0].Token)
|
||||
if err == nil {
|
||||
fmt.Printf("📡 Stream: %s\n", streamURI.URI)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ptzDemo() { //nolint:funlen,gocyclo // Many statements and high complexity due to user interaction
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil || len(profiles) == 0 {
|
||||
fmt.Println("❌ No profiles found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
profileToken := profiles[0].Token
|
||||
|
||||
// Check PTZ status
|
||||
status, err := client.GetStatus(ctx, profileToken)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ PTZ not supported: %v\n", 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)
|
||||
}
|
||||
|
||||
fmt.Println("\n🎮 PTZ Demo - Choose movement:")
|
||||
fmt.Println("1. Move right")
|
||||
fmt.Println("2. Move left")
|
||||
fmt.Println("3. Move up")
|
||||
fmt.Println("4. Move down")
|
||||
fmt.Println("5. Go to center")
|
||||
fmt.Print("Choice: ")
|
||||
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
choice, _ := reader.ReadString('\n')
|
||||
choice = strings.TrimSpace(choice)
|
||||
|
||||
var velocity *onvif.PTZSpeed
|
||||
var position *onvif.PTZVector
|
||||
|
||||
switch choice {
|
||||
case "1":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: ptzSpeed, Y: 0.0}}
|
||||
case "2":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: -ptzSpeed, Y: 0.0}}
|
||||
case "3":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: 0.0, Y: ptzSpeed}}
|
||||
case "4":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: 0.0, Y: -ptzSpeed}}
|
||||
case "5":
|
||||
position = &onvif.PTZVector{PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0}}
|
||||
default:
|
||||
fmt.Println("Invalid choice")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if velocity != nil {
|
||||
timeout := fmt.Sprintf("PT%dS", ptzStepSize)
|
||||
err = client.ContinuousMove(ctx, profileToken, velocity, &timeout)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
fmt.Println("✅ Moving for 2 seconds...")
|
||||
time.Sleep(ptzStepSize * time.Second)
|
||||
//nolint:errcheck // Stop error is not critical for demo
|
||||
_ = client.Stop(ctx, profileToken, true, false)
|
||||
} else if position != nil {
|
||||
err = client.AbsoluteMove(ctx, profileToken, position, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
fmt.Println("✅ Moving to center...")
|
||||
}
|
||||
|
||||
fmt.Println("Demo complete!")
|
||||
}
|
||||
|
||||
func getStreamURLs() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(profiles) == 0 {
|
||||
fmt.Println("❌ No profiles found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d profile(s):\n\n", len(profiles))
|
||||
|
||||
for i, profile := range profiles {
|
||||
fmt.Printf("📹 Profile %d: %s\n", i+1, profile.Name)
|
||||
|
||||
// Stream URI
|
||||
streamURI, err := client.GetStreamURI(ctx, profile.Token)
|
||||
if err != nil {
|
||||
fmt.Printf(" Stream: ❌ Error\n")
|
||||
} else {
|
||||
fmt.Printf(" 📡 Stream: %s\n", streamURI.URI)
|
||||
}
|
||||
|
||||
// Snapshot URI
|
||||
snapshotURI, err := client.GetSnapshotURI(ctx, profile.Token)
|
||||
if err != nil {
|
||||
fmt.Printf(" Snapshot: ❌ Error\n")
|
||||
} else {
|
||||
fmt.Printf(" 📸 Snapshot: %s\n", snapshotURI.URI)
|
||||
}
|
||||
|
||||
// Video info
|
||||
if profile.VideoEncoderConfiguration != nil {
|
||||
fmt.Printf(" 🎬 Encoding: %s", profile.VideoEncoderConfiguration.Encoding)
|
||||
if profile.VideoEncoderConfiguration.Resolution != nil {
|
||||
fmt.Printf(" (%dx%d)",
|
||||
profile.VideoEncoderConfiguration.Resolution.Width,
|
||||
profile.VideoEncoderConfiguration.Resolution.Height)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
fmt.Println("💡 Tips:")
|
||||
fmt.Println(" - Use VLC to open RTSP streams")
|
||||
fmt.Println(" - Open snapshot URLs in a web browser")
|
||||
fmt.Println(" - Some cameras may require authentication in the URL")
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go/server"
|
||||
)
|
||||
|
||||
var (
|
||||
version = "1.0.0"
|
||||
)
|
||||
|
||||
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", 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")
|
||||
model := flag.String("model", "Virtual Multi-Lens Camera", "Device model")
|
||||
firmware := flag.String("firmware", "1.0.0", "Firmware version")
|
||||
serial := flag.String("serial", "SN-12345678", "Serial number")
|
||||
profiles := flag.Int(
|
||||
"profiles", maxWorkers, "Number of camera profiles (1-10)",
|
||||
)
|
||||
ptz := flag.Bool("ptz", true, "Enable PTZ support")
|
||||
imaging := flag.Bool("imaging", true, "Enable Imaging support")
|
||||
events := flag.Bool("events", false, "Enable Events support")
|
||||
info := flag.Bool("info", false, "Show server info and exit")
|
||||
showVersion := flag.Bool("version", false, "Show version and exit")
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "ONVIF Server - Virtual IP Camera Simulator\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "Options:\n")
|
||||
flag.PrintDefaults()
|
||||
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
||||
fmt.Fprintf(os.Stderr, " # Start with default settings (3 profiles, PTZ enabled)\n")
|
||||
fmt.Fprintf(os.Stderr, " %s\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Start with custom credentials and 5 profiles\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -username myuser -password mypass -profiles 5\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Start on specific port without PTZ\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -port 9000 -ptz=false\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Show server information\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -info\n\n", os.Args[0])
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Handle version flag
|
||||
if *showVersion {
|
||||
fmt.Printf("onvif-server version %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Validate profiles count
|
||||
if *profiles < 1 || *profiles > 10 {
|
||||
log.Fatal("Number of profiles must be between 1 and 10")
|
||||
}
|
||||
|
||||
// Create server configuration
|
||||
config := buildConfig(*host, *port, *username, *password, *manufacturer, *model,
|
||||
*firmware, *serial, *profiles, *ptz, *imaging, *events)
|
||||
|
||||
// Create server
|
||||
srv, err := server.New(config)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create server: %v", err)
|
||||
}
|
||||
|
||||
// Handle info flag
|
||||
if *info {
|
||||
fmt.Println(srv.ServerInfo())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Print banner
|
||||
printBanner()
|
||||
|
||||
// Create context that listens for interrupt signals
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Setup signal handler
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// Start server in goroutine
|
||||
go func() {
|
||||
if err := srv.Start(ctx); err != nil {
|
||||
log.Printf("Server error: %v", err)
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for interrupt signal
|
||||
<-sigChan
|
||||
fmt.Println("\n🛑 Received interrupt signal, shutting down...")
|
||||
cancel()
|
||||
|
||||
// Give the server a moment to shut down gracefully
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("✅ Server stopped")
|
||||
}
|
||||
|
||||
// buildConfig creates a server configuration from command-line arguments.
|
||||
func buildConfig(host string, port int, username, password, manufacturer, model,
|
||||
firmware, serial string, numProfiles int, ptz, imaging, events bool) *server.Config {
|
||||
config := &server.Config{
|
||||
Host: host,
|
||||
Port: port,
|
||||
BasePath: "/onvif",
|
||||
Timeout: defaultTimeout * time.Second,
|
||||
DeviceInfo: server.DeviceInfo{
|
||||
Manufacturer: manufacturer,
|
||||
Model: model,
|
||||
FirmwareVersion: firmware,
|
||||
SerialNumber: serial,
|
||||
HardwareID: "HW-87654321",
|
||||
},
|
||||
Username: username,
|
||||
Password: password,
|
||||
SupportPTZ: ptz,
|
||||
SupportImaging: imaging,
|
||||
SupportEvents: events,
|
||||
Profiles: make([]server.ProfileConfig, numProfiles),
|
||||
}
|
||||
|
||||
// Define profile templates
|
||||
templates := []struct {
|
||||
name string
|
||||
width int
|
||||
height int
|
||||
framerate int
|
||||
bitrate int
|
||||
quality float64
|
||||
hasPTZ bool
|
||||
ptzZoomMax float64
|
||||
}{
|
||||
{"Main Camera - High Quality", 1920, 1080, 30, 4096, 80, true, 1},
|
||||
{"Wide Angle Camera", 1280, 720, 30, 2048, 75, false, 0},
|
||||
{"Telephoto Camera", 1920, 1080, 25, 6144, 85, true, 3},
|
||||
{"Low Light Camera", 1920, 1080, 30, 4096, 80, false, 0},
|
||||
{"Ultra HD Camera", 3840, 2160, 30, 16384, 90, true, 2},
|
||||
{"Compact Camera", 640, 480, 30, 512, 70, false, 0},
|
||||
{"PTZ Dome Camera", 1920, 1080, 30, 4096, 80, true, 2},
|
||||
{"Fisheye Camera", 1920, 1080, 30, 4096, 80, false, 0},
|
||||
{"Thermal Camera", 640, 480, 30, 1024, 75, true, 1},
|
||||
{"License Plate Camera", 1920, 1080, 60, 8192, 90, true, 5},
|
||||
}
|
||||
|
||||
// Generate profiles
|
||||
for i := 0; i < numProfiles; i++ {
|
||||
template := templates[i%len(templates)]
|
||||
|
||||
profile := server.ProfileConfig{
|
||||
Token: fmt.Sprintf("profile_%d", i),
|
||||
Name: template.name,
|
||||
VideoSource: server.VideoSourceConfig{
|
||||
Token: fmt.Sprintf("video_source_%d", i),
|
||||
Name: template.name,
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Framerate: template.framerate,
|
||||
Bounds: server.Bounds{X: 0, Y: 0, Width: template.width, Height: template.height},
|
||||
},
|
||||
VideoEncoder: server.VideoEncoderConfig{
|
||||
Encoding: "H264",
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Quality: template.quality,
|
||||
Framerate: template.framerate,
|
||||
Bitrate: template.bitrate,
|
||||
GovLength: template.framerate,
|
||||
},
|
||||
Snapshot: server.SnapshotConfig{
|
||||
Enabled: true,
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Quality: template.quality + 5, //nolint:mnd // Quality offset
|
||||
},
|
||||
}
|
||||
|
||||
// Add PTZ if enabled and template supports it
|
||||
if ptz && template.hasPTZ {
|
||||
profile.PTZ = &server.PTZConfig{
|
||||
NodeToken: fmt.Sprintf("ptz_node_%d", i),
|
||||
PanRange: server.Range{Min: -ptzMaxPan, Max: ptzMaxPan},
|
||||
TiltRange: server.Range{Min: -ptzMaxTilt, Max: ptzMaxTilt},
|
||||
ZoomRange: server.Range{Min: 0, Max: template.ptzZoomMax},
|
||||
DefaultSpeed: server.PTZSpeed{Pan: ptzSpeed, Tilt: ptzSpeed, Zoom: ptzSpeed},
|
||||
SupportsContinuous: true,
|
||||
SupportsAbsolute: true,
|
||||
SupportsRelative: true,
|
||||
Presets: []server.Preset{
|
||||
{
|
||||
Token: fmt.Sprintf("preset_%d_0", i),
|
||||
Name: "Home",
|
||||
Position: server.PTZPosition{Pan: 0, Tilt: 0, Zoom: 0},
|
||||
},
|
||||
{
|
||||
Token: fmt.Sprintf("preset_%d_1", i),
|
||||
Name: "Entrance",
|
||||
Position: server.PTZPosition{
|
||||
Pan: -45, Tilt: -10, Zoom: template.ptzZoomMax * ptzSpeed,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
config.Profiles[i] = profile
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// printBanner prints the application banner.
|
||||
func printBanner() {
|
||||
banner := `
|
||||
╔═══════════════════════════════════════════════════════════╗
|
||||
║ ║
|
||||
║ 🎥 ONVIF Virtual Camera Server 🎥 ║
|
||||
║ ║
|
||||
║ Simulate multi-lens IP cameras with ONVIF support ║
|
||||
║ Version: ` + version + ` ║
|
||||
║ ║
|
||||
╚═══════════════════════════════════════════════════════════╝
|
||||
`
|
||||
fmt.Println(banner)
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
// Command discover performs ONVIF camera discovery on the local network.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go/discovery"
|
||||
)
|
||||
|
||||
func main() {
|
||||
iface := flag.String("interface", "", "Network interface to use (e.g., en0, en11)")
|
||||
timeout := flag.Duration("timeout", 10*time.Second, "Discovery timeout")
|
||||
flag.Parse()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
|
||||
opts := &discovery.DiscoverOptions{
|
||||
NetworkInterface: *iface,
|
||||
}
|
||||
|
||||
fmt.Printf("Discovering ONVIF cameras on the network")
|
||||
if *iface != "" {
|
||||
fmt.Printf(" (interface: %s)", *iface)
|
||||
}
|
||||
fmt.Println("...")
|
||||
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, *timeout, opts)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Discovery error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(devices) == 0 {
|
||||
fmt.Println("No cameras found.")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
fmt.Printf("\nFound %d camera(s):\n\n", len(devices))
|
||||
for i, d := range devices {
|
||||
fmt.Printf("Camera %d:\n", i+1)
|
||||
fmt.Printf(" Endpoint: %s\n", d.EndpointRef)
|
||||
for _, addr := range d.XAddrs {
|
||||
fmt.Printf(" XAddr: %s\n", addr)
|
||||
}
|
||||
if len(d.Scopes) > 0 {
|
||||
fmt.Printf(" Scopes:\n")
|
||||
for _, s := range d.Scopes {
|
||||
fmt.Printf(" - %s\n", s)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
@@ -1,236 +0,0 @@
|
||||
# Test Generator
|
||||
|
||||
Automatically generate Go tests from captured ONVIF camera XML traffic.
|
||||
|
||||
## Overview
|
||||
|
||||
This tool reads XML capture archives (created by `onvif-diagnostics -capture-xml`) and generates complete Go test files that replay the captured SOAP traffic through a mock server.
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```bash
|
||||
./generate-tests \
|
||||
-capture camera-logs/Camera_Model_xmlcapture_timestamp.tar.gz \
|
||||
-output testdata/captures/
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-capture string
|
||||
Path to XML capture archive (.tar.gz) (required)
|
||||
|
||||
-output string
|
||||
Output directory for generated test file (default: "./")
|
||||
|
||||
-package string
|
||||
Package name for generated test (default: "onvif_test")
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```bash
|
||||
# Generate test from Bosch camera capture
|
||||
./generate-tests \
|
||||
-capture camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_xmlcapture_20251110-120000.tar.gz \
|
||||
-output testdata/captures/
|
||||
|
||||
# Output:
|
||||
# ✓ Generated test file: testdata/captures/bosch_flexidome_indoor_5100i_ir_8.71.0066_test.go
|
||||
# Camera: Bosch FLEXIDOME indoor 5100i IR (Firmware: 8.71.0066)
|
||||
# Captured operations: 18
|
||||
```
|
||||
|
||||
## Generated Test Structure
|
||||
|
||||
The tool creates a complete test file with:
|
||||
|
||||
### Test Function
|
||||
|
||||
```go
|
||||
func Test<CameraName>(t *testing.T)
|
||||
```
|
||||
|
||||
Named based on camera manufacturer, model, and firmware.
|
||||
|
||||
### Subtests
|
||||
|
||||
- `GetDeviceInformation` - Validates device info parsing
|
||||
- `GetSystemDateAndTime` - Tests date/time operation
|
||||
- `GetCapabilities` - Verifies capability discovery
|
||||
- `GetProfiles` - Tests media profile enumeration
|
||||
|
||||
### Assertions
|
||||
|
||||
Each subtest includes:
|
||||
- Error checking
|
||||
- Nil validation
|
||||
- Basic field validation
|
||||
- Informative logging
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Load Capture** - Reads all SOAP exchanges from tar.gz archive
|
||||
2. **Extract Metadata** - Gets camera manufacturer, model, firmware from responses
|
||||
3. **Generate Name** - Creates valid Go identifier from camera info
|
||||
4. **Render Template** - Fills in test template with camera-specific data
|
||||
5. **Write File** - Saves test to output directory
|
||||
|
||||
## Template
|
||||
|
||||
The generator uses an embedded Go template that creates:
|
||||
|
||||
```go
|
||||
package onvif_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
func Test<CameraName>(t *testing.T) {
|
||||
captureArchive := "<archive-file>.tar.gz"
|
||||
|
||||
mockServer, err := onviftesting.NewMockSOAPServer(captureArchive)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock server: %v", err)
|
||||
}
|
||||
defer mockServer.Close()
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
mockServer.URL()+"/onvif/device_service",
|
||||
onvif.WithCredentials("testuser", "testpass"),
|
||||
)
|
||||
// ... test operations
|
||||
}
|
||||
```
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Capture from Camera
|
||||
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://camera/onvif/device_service" \
|
||||
-username "user" \
|
||||
-password "pass" \
|
||||
-capture-xml
|
||||
```
|
||||
|
||||
### 2. Generate Test
|
||||
|
||||
```bash
|
||||
./generate-tests \
|
||||
-capture camera-logs/Camera_*_xmlcapture_*.tar.gz \
|
||||
-output testdata/captures/
|
||||
```
|
||||
|
||||
### 3. Run Test
|
||||
|
||||
```bash
|
||||
go test -v ./testdata/captures/ -run TestCamera
|
||||
```
|
||||
|
||||
## Customization
|
||||
|
||||
After generation, you can customize the test:
|
||||
|
||||
### Add Camera-Specific Tests
|
||||
|
||||
```go
|
||||
t.Run("CustomFeature", func(t *testing.T) {
|
||||
// Add custom test for camera-specific features
|
||||
})
|
||||
```
|
||||
|
||||
### Add Detailed Assertions
|
||||
|
||||
```go
|
||||
t.Run("GetDeviceInformation", func(t *testing.T) {
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetDeviceInformation failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add specific assertions
|
||||
if info.Manufacturer != "ExpectedManufacturer" {
|
||||
t.Errorf("Expected manufacturer X, got %s", info.Manufacturer)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
```bash
|
||||
go build -o generate-tests ./cmd/generate-tests/
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
- `github.com/0x524a/onvif-go/testing` - Mock server and capture loader
|
||||
|
||||
## Output File Naming
|
||||
|
||||
Generated test files are named:
|
||||
|
||||
```
|
||||
<manufacturer>_<model>_<firmware>_test.go
|
||||
```
|
||||
|
||||
Examples:
|
||||
- `bosch_flexidome_indoor_5100i_ir_8.71.0066_test.go`
|
||||
- `axis_q3626-ve_12.6.104_test.go`
|
||||
- `reolink_e1_zoom_v3.1.0.2649_test.go`
|
||||
|
||||
All special characters converted to underscores or removed.
|
||||
|
||||
## Archive Path Handling
|
||||
|
||||
The generator automatically handles archive paths:
|
||||
|
||||
- If archive is in output directory, uses filename only
|
||||
- Otherwise uses relative path from output directory
|
||||
- Tests can find archives when run with `go test ./testdata/captures/`
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Failed to load capture"
|
||||
|
||||
Archive file not found or corrupted.
|
||||
|
||||
**Solution**: Verify archive path and ensure it's a valid tar.gz file.
|
||||
|
||||
### "Failed to extract device info"
|
||||
|
||||
Archive doesn't contain GetDeviceInformation response.
|
||||
|
||||
**Solution**: Re-capture from camera, ensuring diagnostic runs fully.
|
||||
|
||||
### Generated test won't compile
|
||||
|
||||
Usually due to invalid characters in camera names.
|
||||
|
||||
**Solution**: The generator should handle this, but you can manually edit the test function name.
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Potential improvements:
|
||||
|
||||
- [ ] Detect camera-specific operations (PTZ, audio, etc.)
|
||||
- [ ] Generate profile-specific tests
|
||||
- [ ] Add benchmarking subtests
|
||||
- [ ] Support custom test templates
|
||||
- [ ] Batch generation from multiple captures
|
||||
|
||||
## See Also
|
||||
|
||||
- `testdata/captures/README.md` - Using generated tests
|
||||
- `testing/mock_server.go` - Mock server implementation
|
||||
- `cmd/onvif-diagnostics/` - Capturing tool
|
||||
@@ -1,926 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
var (
|
||||
captureArchive = flag.String("capture", "", "Path to XML capture archive (.tar.gz)")
|
||||
outputDir = flag.String("output", "./", "Output directory for generated test file")
|
||||
packageName = flag.String("package", "onvif_test", "Package name for generated test")
|
||||
updateRegistry = flag.Bool("update-registry", true, "Update registry.json with camera info")
|
||||
registryPath = flag.String("registry", "", "Path to registry.json (default: testdata/captures/registry.json)")
|
||||
coverageReport = flag.Bool("coverage-report", false, "Generate coverage report from registry")
|
||||
coverageOutput = flag.String("coverage-output", "", "Output path for coverage report (default: stdout)")
|
||||
)
|
||||
|
||||
const testTemplate = `package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
// Test{{.CameraName}} tests ONVIF client against {{.CameraDescription}} captured responses.
|
||||
// Capture format: V2 with parameter-aware matching
|
||||
// Total captured operations: {{.TotalExchanges}}
|
||||
func Test{{.CameraName}}(t *testing.T) {
|
||||
// Load capture archive (relative to project root)
|
||||
captureArchive := "{{.CaptureArchiveRelPath}}"
|
||||
|
||||
mockServer, err := onviftesting.NewMockSOAPServerV2(captureArchive)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock server: %v", err)
|
||||
}
|
||||
defer mockServer.Close()
|
||||
|
||||
// Create ONVIF client pointing to mock server
|
||||
client, err := onvif.NewClient(
|
||||
mockServer.URL()+"/onvif/device_service",
|
||||
onvif.WithCredentials("testuser", "testpass"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create ONVIF client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// =========================================================================
|
||||
// Device Service Operations
|
||||
// =========================================================================
|
||||
{{range .DeviceTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Media Service Operations
|
||||
// =========================================================================
|
||||
{{if .NeedsInit}}
|
||||
// Initialize to discover service endpoints (required for Media/PTZ/Imaging)
|
||||
if err := client.Initialize(ctx); err != nil {
|
||||
t.Fatalf("Failed to initialize client: %v", err)
|
||||
}
|
||||
{{end}}
|
||||
{{range .MediaTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Profile-Dependent Operations
|
||||
// =========================================================================
|
||||
{{range .ProfileTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// PTZ Operations
|
||||
// =========================================================================
|
||||
{{range .PTZTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
// =========================================================================
|
||||
// Imaging Operations
|
||||
// =========================================================================
|
||||
{{range .ImagingTests}}
|
||||
t.Run("{{.Name}}", func(t *testing.T) {
|
||||
{{.Code}}
|
||||
})
|
||||
{{end}}
|
||||
}
|
||||
`
|
||||
|
||||
type TestData struct {
|
||||
PackageName string
|
||||
CameraName string
|
||||
CameraDescription string
|
||||
CaptureArchiveRelPath string
|
||||
TotalExchanges int
|
||||
NeedsInit bool
|
||||
DeviceTests []GeneratedTest
|
||||
MediaTests []GeneratedTest
|
||||
ProfileTests []GeneratedTest
|
||||
PTZTests []GeneratedTest
|
||||
ImagingTests []GeneratedTest
|
||||
}
|
||||
|
||||
type GeneratedTest struct {
|
||||
Name string
|
||||
Code string
|
||||
}
|
||||
|
||||
// operationInfo holds info about captured operations
|
||||
type operationInfo struct {
|
||||
OperationName string
|
||||
ServiceType onviftesting.ServiceType
|
||||
Parameters map[string]interface{}
|
||||
Success bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Set default registry path
|
||||
regPath := *registryPath
|
||||
if regPath == "" {
|
||||
regPath = onviftesting.DefaultRegistryPath
|
||||
}
|
||||
|
||||
// Handle coverage report mode
|
||||
if *coverageReport {
|
||||
generateCoverageReport(regPath)
|
||||
return
|
||||
}
|
||||
|
||||
if *captureArchive == "" {
|
||||
fmt.Println("Error: -capture flag is required")
|
||||
fmt.Println()
|
||||
fmt.Println("Usage:")
|
||||
flag.PrintDefaults()
|
||||
fmt.Println()
|
||||
fmt.Println("Example:")
|
||||
fmt.Println(" ./generate-tests -capture camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_xmlcapture_*.tar.gz")
|
||||
fmt.Println()
|
||||
fmt.Println("Coverage report:")
|
||||
fmt.Println(" ./generate-tests -coverage-report")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputFile := generateTests()
|
||||
|
||||
// Update registry if requested
|
||||
if *updateRegistry {
|
||||
updateCameraRegistry(regPath, *captureArchive, outputFile)
|
||||
}
|
||||
}
|
||||
|
||||
func generateTests() string {
|
||||
// Load capture with V2 support
|
||||
capture, metadata, err := onviftesting.LoadCaptureFromArchiveV2(*captureArchive)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load capture: %v", err)
|
||||
}
|
||||
|
||||
// Extract camera name from archive filename
|
||||
baseName := filepath.Base(*captureArchive)
|
||||
parts := strings.Split(baseName, "_xmlcapture_")
|
||||
cameraID := parts[0]
|
||||
|
||||
// Convert to valid Go identifier
|
||||
cameraName := strings.ReplaceAll(cameraID, "-", "")
|
||||
cameraName = strings.ReplaceAll(cameraName, ".", "")
|
||||
cameraName = strings.ReplaceAll(cameraName, " ", "")
|
||||
|
||||
// Get camera description from metadata or extract from captures
|
||||
cameraDesc := cameraID
|
||||
if metadata != nil && metadata.CameraInfo.Manufacturer != "" {
|
||||
cameraDesc = fmt.Sprintf("%s %s (Firmware: %s)",
|
||||
metadata.CameraInfo.Manufacturer,
|
||||
metadata.CameraInfo.Model,
|
||||
metadata.CameraInfo.FirmwareVersion)
|
||||
} else {
|
||||
// Try to extract from GetDeviceInformation response
|
||||
for _, ex := range capture.Exchanges {
|
||||
if ex.OperationName == "GetDeviceInformation" && ex.Success {
|
||||
manufacturer := extractXMLValue(ex.ResponseBody, "Manufacturer")
|
||||
model := extractXMLValue(ex.ResponseBody, "Model")
|
||||
firmware := extractXMLValue(ex.ResponseBody, "FirmwareVersion")
|
||||
if manufacturer != "" && model != "" {
|
||||
cameraDesc = fmt.Sprintf("%s %s (Firmware: %s)", manufacturer, model, firmware)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze captured operations
|
||||
ops := analyzeOperations(capture)
|
||||
|
||||
// Generate tests by service type
|
||||
testData := TestData{
|
||||
PackageName: *packageName,
|
||||
CameraName: cameraName,
|
||||
CameraDescription: cameraDesc,
|
||||
CaptureArchiveRelPath: makeRelativePath(*captureArchive, *outputDir),
|
||||
TotalExchanges: len(capture.Exchanges),
|
||||
NeedsInit: hasNonDeviceOperations(ops),
|
||||
DeviceTests: generateDeviceTests(ops),
|
||||
MediaTests: generateMediaTests(ops),
|
||||
ProfileTests: generateProfileDependentTests(ops),
|
||||
PTZTests: generatePTZTests(ops),
|
||||
ImagingTests: generateImagingTests(ops),
|
||||
}
|
||||
|
||||
// Generate test file
|
||||
tmpl, err := template.New("test").Parse(testTemplate)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse template: %v", err)
|
||||
}
|
||||
|
||||
outputFile := filepath.Join(*outputDir, fmt.Sprintf("%s_test.go", strings.ToLower(cameraID)))
|
||||
f, err := os.Create(outputFile) //nolint:gosec // Filename is generated from test data, safe
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create output file: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
|
||||
if err := tmpl.Execute(f, testData); err != nil {
|
||||
_ = f.Close()
|
||||
log.Fatalf("Failed to execute template: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("✓ Generated test file: %s\n", outputFile)
|
||||
fmt.Printf(" Camera: %s\n", cameraDesc)
|
||||
fmt.Printf(" Captured operations: %d\n", len(capture.Exchanges))
|
||||
fmt.Printf(" Generated subtests: Device=%d, Media=%d, Profile=%d, PTZ=%d, Imaging=%d\n",
|
||||
len(testData.DeviceTests), len(testData.MediaTests), len(testData.ProfileTests),
|
||||
len(testData.PTZTests), len(testData.ImagingTests))
|
||||
fmt.Println()
|
||||
fmt.Println("Run tests with:")
|
||||
fmt.Printf(" go test -v %s\n", outputFile)
|
||||
|
||||
return outputFile
|
||||
}
|
||||
|
||||
func analyzeOperations(capture *onviftesting.CameraCaptureV2) []operationInfo {
|
||||
var ops []operationInfo
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, ex := range capture.Exchanges {
|
||||
// Create unique key for deduplication
|
||||
key := ex.OperationName
|
||||
if token := ex.GetProfileToken(); token != "" {
|
||||
key += "_" + token
|
||||
} else if token := ex.GetConfigurationToken(); token != "" {
|
||||
key += "_" + token
|
||||
} else if token := ex.GetVideoSourceToken(); token != "" {
|
||||
key += "_" + token
|
||||
}
|
||||
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
|
||||
ops = append(ops, operationInfo{
|
||||
OperationName: ex.OperationName,
|
||||
ServiceType: ex.ServiceType,
|
||||
Parameters: ex.Parameters,
|
||||
Success: ex.Success,
|
||||
})
|
||||
}
|
||||
|
||||
return ops
|
||||
}
|
||||
|
||||
func hasNonDeviceOperations(ops []operationInfo) bool {
|
||||
for _, op := range ops {
|
||||
switch op.ServiceType {
|
||||
case onviftesting.ServiceMedia, onviftesting.ServicePTZ, onviftesting.ServiceImaging:
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func generateDeviceTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Standard device tests
|
||||
deviceOps := map[string]string{
|
||||
"GetDeviceInformation": `info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetDeviceInformation failed: %v", err)
|
||||
return
|
||||
}
|
||||
if info.Manufacturer == "" {
|
||||
t.Error("Manufacturer is empty")
|
||||
}
|
||||
if info.Model == "" {
|
||||
t.Error("Model is empty")
|
||||
}
|
||||
t.Logf("Device: %s %s (Firmware: %s)", info.Manufacturer, info.Model, info.FirmwareVersion)`,
|
||||
|
||||
"GetSystemDateAndTime": `_, err := client.GetSystemDateAndTime(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetSystemDateAndTime failed: %v", err)
|
||||
}`,
|
||||
|
||||
"GetCapabilities": `caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetCapabilities failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Capabilities: Device=%v, Media=%v, Imaging=%v, PTZ=%v",
|
||||
caps.Device != nil, caps.Media != nil, caps.Imaging != nil, caps.PTZ != nil)`,
|
||||
|
||||
"GetHostname": `hostname, err := client.GetHostname(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetHostname failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Hostname: %s", hostname)`,
|
||||
|
||||
"GetScopes": `scopes, err := client.GetScopes(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetScopes failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Scopes: %d", len(scopes))`,
|
||||
|
||||
"GetNetworkInterfaces": `interfaces, err := client.GetNetworkInterfaces(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetNetworkInterfaces failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Network interfaces: %d", len(interfaces))`,
|
||||
|
||||
"GetServices": `services, err := client.GetServices(ctx, true)
|
||||
if err != nil {
|
||||
t.Errorf("GetServices failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Services: %d", len(services))`,
|
||||
}
|
||||
|
||||
// Generate tests for captured operations
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceDevice && op.ServiceType != onviftesting.ServiceUnknown {
|
||||
continue
|
||||
}
|
||||
if code, ok := deviceOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(deviceOps, op.OperationName) // Don't duplicate
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by name for consistent output
|
||||
sort.Slice(tests, func(i, j int) bool {
|
||||
return tests[i].Name < tests[j].Name
|
||||
})
|
||||
|
||||
return tests
|
||||
}
|
||||
|
||||
func generateMediaTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
mediaOps := map[string]string{
|
||||
"GetProfiles": `profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetProfiles failed: %v", err)
|
||||
return
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
t.Error("No profiles returned")
|
||||
}
|
||||
t.Logf("Found %d profile(s)", len(profiles))`,
|
||||
|
||||
"GetVideoSources": `sources, err := client.GetVideoSources(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoSources failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video sources: %d", len(sources))`,
|
||||
|
||||
"GetVideoSourceConfigurations": `configs, err := client.GetVideoSourceConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoSourceConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video source configs: %d", len(configs))`,
|
||||
|
||||
"GetVideoEncoderConfigurations": `configs, err := client.GetVideoEncoderConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetVideoEncoderConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Video encoder configs: %d", len(configs))`,
|
||||
|
||||
"GetAudioSources": `sources, err := client.GetAudioSources(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetAudioSources failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Audio sources: %d", len(sources))`,
|
||||
|
||||
"GetAudioSourceConfigurations": `configs, err := client.GetAudioSourceConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetAudioSourceConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Audio source configs: %d", len(configs))`,
|
||||
|
||||
"GetMetadataConfigurations": `configs, err := client.GetMetadataConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetMetadataConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Metadata configs: %d", len(configs))`,
|
||||
}
|
||||
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceMedia {
|
||||
continue
|
||||
}
|
||||
if code, ok := mediaOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(mediaOps, op.OperationName)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(tests, func(i, j int) bool {
|
||||
return tests[i].Name < tests[j].Name
|
||||
})
|
||||
|
||||
return tests
|
||||
}
|
||||
|
||||
func generateProfileDependentTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Group operations by profile token
|
||||
profileOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if token, ok := op.Parameters["ProfileToken"].(string); ok && token != "" {
|
||||
profileOps[token] = append(profileOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate GetStreamURI tests for each profile
|
||||
for token, opList := range profileOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetStreamURI":
|
||||
testName := fmt.Sprintf("GetStreamURI_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`uri, err := client.GetStreamURI(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetStreamURI failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if uri.URI == "" {
|
||||
t.Error("Stream URI is empty")
|
||||
}
|
||||
t.Logf("Stream URI: %%s", uri.URI)`, token),
|
||||
})
|
||||
|
||||
case "GetSnapshotURI":
|
||||
testName := fmt.Sprintf("GetSnapshotURI_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`uri, err := client.GetSnapshotURI(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetSnapshotURI failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if uri.URI == "" {
|
||||
t.Error("Snapshot URI is empty")
|
||||
}
|
||||
t.Logf("Snapshot URI: %%s", uri.URI)`, token),
|
||||
})
|
||||
|
||||
case "GetProfile":
|
||||
testName := fmt.Sprintf("GetProfile_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`profile, err := client.GetProfile(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetProfile failed: %%v", err)
|
||||
return
|
||||
}
|
||||
if profile.Token != "%s" {
|
||||
t.Errorf("Expected token %%s, got %%s", "%s", profile.Token)
|
||||
}
|
||||
t.Logf("Profile: %%s", profile.Name)`, token, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate tests
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func generatePTZTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
ptzOps := map[string]string{
|
||||
"GetNodes": `nodes, err := client.GetNodes(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetNodes failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ nodes: %d", len(nodes))`,
|
||||
|
||||
"GetConfigurations": `configs, err := client.GetConfigurations(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("GetConfigurations failed: %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ configs: %d", len(configs))`,
|
||||
}
|
||||
|
||||
// Group by profile token for status and presets
|
||||
profileOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServicePTZ {
|
||||
continue
|
||||
}
|
||||
if code, ok := ptzOps[op.OperationName]; ok {
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: op.OperationName,
|
||||
Code: code,
|
||||
})
|
||||
delete(ptzOps, op.OperationName)
|
||||
continue
|
||||
}
|
||||
if token, ok := op.Parameters["ProfileToken"].(string); ok && token != "" {
|
||||
profileOps[token] = append(profileOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate profile-specific PTZ tests
|
||||
for token, opList := range profileOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetStatus":
|
||||
testName := fmt.Sprintf("PTZ_GetStatus_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`status, err := client.GetStatus(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetStatus failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("PTZ Status retrieved for profile %s")
|
||||
_ = status`, token, token),
|
||||
})
|
||||
|
||||
case "GetPresets":
|
||||
testName := fmt.Sprintf("PTZ_GetPresets_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`presets, err := client.GetPresets(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetPresets failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Found %%d preset(s) for profile %s", len(presets))`, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func generateImagingTests(ops []operationInfo) []GeneratedTest {
|
||||
var tests []GeneratedTest
|
||||
|
||||
// Group by video source token
|
||||
sourceOps := make(map[string][]operationInfo)
|
||||
for _, op := range ops {
|
||||
if op.ServiceType != onviftesting.ServiceImaging {
|
||||
continue
|
||||
}
|
||||
if token, ok := op.Parameters["VideoSourceToken"].(string); ok && token != "" {
|
||||
sourceOps[token] = append(sourceOps[token], op)
|
||||
}
|
||||
}
|
||||
|
||||
for token, opList := range sourceOps {
|
||||
for _, op := range opList {
|
||||
switch op.OperationName {
|
||||
case "GetImagingSettings":
|
||||
testName := fmt.Sprintf("GetImagingSettings_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`settings, err := client.GetImagingSettings(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetImagingSettings failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Imaging settings retrieved for source %s")
|
||||
_ = settings`, token, token),
|
||||
})
|
||||
|
||||
case "GetOptions":
|
||||
testName := fmt.Sprintf("GetImagingOptions_%s", sanitizeToken(token))
|
||||
tests = append(tests, GeneratedTest{
|
||||
Name: testName,
|
||||
Code: fmt.Sprintf(`options, err := client.GetOptions(ctx, "%s")
|
||||
if err != nil {
|
||||
t.Errorf("GetOptions failed: %%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("Imaging options retrieved for source %s")
|
||||
_ = options`, token, token),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
seen := make(map[string]bool)
|
||||
var uniqueTests []GeneratedTest
|
||||
for _, t := range tests {
|
||||
if !seen[t.Name] {
|
||||
seen[t.Name] = true
|
||||
uniqueTests = append(uniqueTests, t)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uniqueTests, func(i, j int) bool {
|
||||
return uniqueTests[i].Name < uniqueTests[j].Name
|
||||
})
|
||||
|
||||
return uniqueTests
|
||||
}
|
||||
|
||||
func sanitizeToken(token string) string {
|
||||
// Make token safe for test name
|
||||
token = strings.ReplaceAll(token, "-", "_")
|
||||
token = strings.ReplaceAll(token, ".", "_")
|
||||
token = strings.ReplaceAll(token, " ", "_")
|
||||
// Truncate if too long
|
||||
if len(token) > 20 {
|
||||
token = token[:20]
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func makeRelativePath(archivePath, outputDir string) string {
|
||||
if absOutput, err := filepath.Abs(outputDir); err == nil {
|
||||
if absArchive, err := filepath.Abs(archivePath); err == nil {
|
||||
if rel, err := filepath.Rel(filepath.Dir(absOutput), absArchive); err == nil {
|
||||
return rel
|
||||
}
|
||||
}
|
||||
}
|
||||
return archivePath
|
||||
}
|
||||
|
||||
func extractXMLValue(xmlStr, tagName string) string {
|
||||
start := fmt.Sprintf("<%s>", tagName)
|
||||
end := fmt.Sprintf("</%s>", tagName)
|
||||
|
||||
startIdx := strings.Index(xmlStr, start)
|
||||
if startIdx == -1 {
|
||||
start = fmt.Sprintf(":%s>", tagName)
|
||||
startIdx = strings.Index(xmlStr, start)
|
||||
if startIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
startIdx += len(start)
|
||||
} else {
|
||||
startIdx += len(start)
|
||||
}
|
||||
|
||||
endIdx := strings.Index(xmlStr[startIdx:], end)
|
||||
if endIdx == -1 {
|
||||
end = fmt.Sprintf(":/%s>", tagName)
|
||||
endIdx = strings.Index(xmlStr[startIdx:], end)
|
||||
if endIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
return strings.TrimSpace(xmlStr[startIdx : startIdx+endIdx])
|
||||
}
|
||||
|
||||
// updateCameraRegistry updates the registry with camera information from the capture.
|
||||
func updateCameraRegistry(regPath, archivePath, testFile string) {
|
||||
registry, err := onviftesting.LoadRegistry(regPath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to load registry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := onviftesting.CreateCameraEntryFromCapture(archivePath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to create registry entry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the test file path (relative to registry directory)
|
||||
if testFile != "" {
|
||||
regDir := filepath.Dir(regPath)
|
||||
if absTest, err := filepath.Abs(testFile); err == nil {
|
||||
if absRegDir, err := filepath.Abs(regDir); err == nil {
|
||||
if rel, err := filepath.Rel(absRegDir, absTest); err == nil {
|
||||
entry.TestFile = rel
|
||||
}
|
||||
}
|
||||
}
|
||||
if entry.TestFile == "" {
|
||||
entry.TestFile = filepath.Base(testFile)
|
||||
}
|
||||
}
|
||||
|
||||
// Add or update the camera entry
|
||||
registry.AddCamera(*entry)
|
||||
|
||||
// Update coverage statistics
|
||||
updateRegistryCoverage(registry, archivePath)
|
||||
|
||||
// Save registry
|
||||
if err := onviftesting.SaveRegistry(registry, regPath); err != nil {
|
||||
log.Printf("Warning: Failed to save registry: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✓ Registry updated: %s\n", regPath)
|
||||
fmt.Printf(" Camera ID: %s\n", entry.ID)
|
||||
fmt.Printf(" Total cameras in registry: %d\n", len(registry.Cameras))
|
||||
}
|
||||
|
||||
// updateRegistryCoverage calculates coverage from captured operations.
|
||||
func updateRegistryCoverage(registry *onviftesting.Registry, archivePath string) {
|
||||
capture, _, err := onviftesting.LoadCaptureFromArchiveV2(archivePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Count unique operations per service
|
||||
serviceCounts := make(map[string]map[string]bool)
|
||||
for _, ex := range capture.Exchanges {
|
||||
service := string(ex.ServiceType)
|
||||
if service == "" || service == "Unknown" {
|
||||
continue
|
||||
}
|
||||
if serviceCounts[service] == nil {
|
||||
serviceCounts[service] = make(map[string]bool)
|
||||
}
|
||||
serviceCounts[service][ex.OperationName] = true
|
||||
}
|
||||
|
||||
// Get totals from operations registry
|
||||
opCounts := onviftesting.GetOperationCount()
|
||||
|
||||
// Update coverage
|
||||
registry.Coverage = make(map[string]onviftesting.Coverage)
|
||||
for service, ops := range serviceCounts {
|
||||
total := 0
|
||||
switch service {
|
||||
case "Device":
|
||||
total = opCounts.Device
|
||||
case "Media":
|
||||
total = opCounts.Media
|
||||
case "PTZ":
|
||||
total = opCounts.PTZ
|
||||
case "Imaging":
|
||||
total = opCounts.Imaging
|
||||
case "Event":
|
||||
total = opCounts.Event
|
||||
case "DeviceIO":
|
||||
total = opCounts.DeviceIO
|
||||
}
|
||||
|
||||
registry.Coverage[service] = onviftesting.Coverage{
|
||||
Total: total,
|
||||
Captured: len(ops),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// generateCoverageReport generates a coverage report from the registry.
|
||||
func generateCoverageReport(regPath string) {
|
||||
registry, err := onviftesting.LoadRegistry(regPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load registry: %v", err)
|
||||
}
|
||||
|
||||
// Generate markdown report
|
||||
report := generateCoverageMarkdown(registry)
|
||||
|
||||
// Output to file or stdout
|
||||
if *coverageOutput != "" {
|
||||
if err := os.WriteFile(*coverageOutput, []byte(report), 0600); err != nil { //nolint:mnd
|
||||
log.Fatalf("Failed to write coverage report: %v", err)
|
||||
}
|
||||
fmt.Printf("✓ Coverage report written to: %s\n", *coverageOutput)
|
||||
} else {
|
||||
fmt.Println(report)
|
||||
}
|
||||
}
|
||||
|
||||
// generateCoverageMarkdown creates a markdown coverage report.
|
||||
func generateCoverageMarkdown(registry *onviftesting.Registry) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("# ONVIF Operation Coverage Report\n\n")
|
||||
sb.WriteString(fmt.Sprintf("Generated: %s\n\n", time.Now().Format("2006-01-02 15:04:05")))
|
||||
|
||||
// Summary
|
||||
sb.WriteString("## Summary\n\n")
|
||||
sb.WriteString(fmt.Sprintf("- **Total Cameras**: %d\n", len(registry.Cameras)))
|
||||
|
||||
total, captured := registry.GetTotalCoverage()
|
||||
if total > 0 {
|
||||
sb.WriteString(fmt.Sprintf("- **Overall Coverage**: %.1f%% (%d/%d operations)\n\n",
|
||||
float64(captured)/float64(total)*100, captured, total))
|
||||
}
|
||||
|
||||
// Cameras
|
||||
if len(registry.Cameras) > 0 {
|
||||
sb.WriteString("## Registered Cameras\n\n")
|
||||
sb.WriteString("| Manufacturer | Model | Firmware | Operations | Capabilities |\n")
|
||||
sb.WriteString("|--------------|-------|----------|------------|---------------|\n")
|
||||
|
||||
for _, cam := range registry.Cameras {
|
||||
caps := strings.Join(cam.Capabilities, ", ")
|
||||
sb.WriteString(fmt.Sprintf("| %s | %s | %s | %d | %s |\n",
|
||||
cam.Manufacturer, cam.Model, cam.Firmware, cam.OperationsCaptured, caps))
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Coverage by service
|
||||
if len(registry.Coverage) > 0 {
|
||||
sb.WriteString("## Coverage by Service\n\n")
|
||||
sb.WriteString("| Service | Total | Captured | Coverage |\n")
|
||||
sb.WriteString("|---------|-------|----------|----------|\n")
|
||||
|
||||
services := []string{"Device", "Media", "PTZ", "Imaging", "Event", "DeviceIO"}
|
||||
for _, service := range services {
|
||||
if cov, ok := registry.Coverage[service]; ok {
|
||||
pct := 0.0
|
||||
if cov.Total > 0 {
|
||||
pct = float64(cov.Captured) / float64(cov.Total) * 100
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("| %s | %d | %d | %.1f%% |\n",
|
||||
service, cov.Total, cov.Captured, pct))
|
||||
}
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Missing operations
|
||||
sb.WriteString("## Operation Specifications\n\n")
|
||||
opCounts := onviftesting.GetOperationCount()
|
||||
sb.WriteString(fmt.Sprintf("- Device: %d operations defined\n", opCounts.Device))
|
||||
sb.WriteString(fmt.Sprintf("- Media: %d operations defined\n", opCounts.Media))
|
||||
sb.WriteString(fmt.Sprintf("- PTZ: %d operations defined\n", opCounts.PTZ))
|
||||
sb.WriteString(fmt.Sprintf("- Imaging: %d operations defined\n", opCounts.Imaging))
|
||||
sb.WriteString(fmt.Sprintf("- Event: %d operations defined\n", opCounts.Event))
|
||||
sb.WriteString(fmt.Sprintf("- DeviceIO: %d operations defined\n", opCounts.DeviceIO))
|
||||
sb.WriteString(fmt.Sprintf("\n**Total**: %d read-only operations tracked\n", opCounts.Total))
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ASCIIConfig controls ASCII art generation parameters.
|
||||
type ASCIIConfig struct {
|
||||
Width int // Output width in characters
|
||||
Height int // Output height in characters
|
||||
Invert bool // Invert brightness
|
||||
Quality string // "high", "medium", "low"
|
||||
}
|
||||
|
||||
const (
|
||||
defaultASCIIWidth = 120
|
||||
defaultASCIIHeight = 40
|
||||
maxColorValue = 255
|
||||
bitShift8 = 8
|
||||
bufferSize1024 = 1024
|
||||
largeASCIIWidth = 160
|
||||
largeASCIIHeight = 50
|
||||
defaultQuality = "medium"
|
||||
)
|
||||
|
||||
// DefaultASCIIConfig returns a sensible default configuration.
|
||||
func DefaultASCIIConfig() ASCIIConfig {
|
||||
return ASCIIConfig{
|
||||
Width: defaultASCIIWidth,
|
||||
Height: defaultASCIIHeight,
|
||||
Invert: false,
|
||||
Quality: "medium",
|
||||
}
|
||||
}
|
||||
|
||||
// ASCIICharsets define different character options.
|
||||
var (
|
||||
// Full charset with many shades.
|
||||
charsetFull = []rune{' ', '.', ':', '-', '=', '+', '*', '#', '%', '@'}
|
||||
|
||||
// Medium charset - balanced.
|
||||
charsetMedium = []rune{' ', '.', '-', '=', '+', '#', '@'}
|
||||
|
||||
// Simple charset - just a few chars.
|
||||
charsetSimple = []rune{' ', '-', '#', '@'}
|
||||
|
||||
// Block charset - using block characters.
|
||||
charsetBlock = []rune{' ', '░', '▒', '▓', '█'}
|
||||
|
||||
// Detailed charset.
|
||||
charsetDetailed = []rune{' ', '`', '.', ',', ':', ';', '!', 'i', 'l', 'I',
|
||||
'o', 'O', '0', 'e', 'E', 'p', 'P', 'x', 'X', '$', 'D', 'W', 'M', '@', '#'}
|
||||
)
|
||||
|
||||
// ImageToASCII converts image data to ASCII art. Supports JPEG and PNG formats.
|
||||
func ImageToASCII(imageData []byte, config ASCIIConfig) (string, error) {
|
||||
// Decode image from bytes
|
||||
img, _, err := image.Decode(bytes.NewReader(imageData))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
return imageToASCIIFromImage(img, config, "unknown")
|
||||
}
|
||||
|
||||
// imageToASCIIFromImage is the core conversion function.
|
||||
//
|
||||
//nolint:gocyclo // Image to ASCII conversion has high complexity due to multiple pixel processing paths
|
||||
func imageToASCIIFromImage(img image.Image, config ASCIIConfig, format string) (string, error) { //nolint:unparam // format reserved for future use
|
||||
// Validate configuration
|
||||
if config.Width <= 0 {
|
||||
config.Width = 120
|
||||
}
|
||||
if config.Height <= 0 {
|
||||
config.Height = defaultASCIIHeight
|
||||
}
|
||||
if config.Quality == "" {
|
||||
config.Quality = defaultQuality
|
||||
}
|
||||
|
||||
// Select character set based on quality
|
||||
charset := charsetMedium
|
||||
switch strings.ToLower(config.Quality) {
|
||||
case "high", "detailed":
|
||||
charset = charsetDetailed
|
||||
case "medium":
|
||||
charset = charsetMedium
|
||||
case "low", "simple":
|
||||
charset = charsetSimple
|
||||
case "block":
|
||||
charset = charsetBlock
|
||||
case "full":
|
||||
charset = charsetFull
|
||||
}
|
||||
|
||||
// Get image bounds
|
||||
bounds := img.Bounds()
|
||||
width := bounds.Max.X - bounds.Min.X
|
||||
height := bounds.Max.Y - bounds.Min.Y
|
||||
|
||||
// Calculate scaling factors
|
||||
scaleX := float64(width) / float64(config.Width)
|
||||
scaleY := float64(height) / float64(config.Height)
|
||||
|
||||
// Build ASCII representation
|
||||
var result strings.Builder
|
||||
for y := 0; y < config.Height; y++ {
|
||||
for x := 0; x < config.Width; x++ {
|
||||
// Sample pixel from image
|
||||
srcX := int(float64(x) * scaleX)
|
||||
srcY := int(float64(y) * scaleY)
|
||||
|
||||
// Bounds check
|
||||
if srcX >= width {
|
||||
srcX = width - 1
|
||||
}
|
||||
if srcY >= height {
|
||||
srcY = height - 1
|
||||
}
|
||||
|
||||
// Get pixel color
|
||||
r, g, b, _ := img.At(bounds.Min.X+srcX, bounds.Min.Y+srcY).RGBA()
|
||||
|
||||
// Convert to grayscale brightness (0-255)
|
||||
brightness := calculateBrightness(r, g, b)
|
||||
|
||||
// Invert if requested
|
||||
if config.Invert {
|
||||
brightness = maxColorValue - brightness
|
||||
}
|
||||
|
||||
// Map brightness to character
|
||||
charIndex := int(float64(brightness) / float64(maxColorValue) * float64(len(charset)-1))
|
||||
if charIndex >= len(charset) {
|
||||
charIndex = len(charset) - 1
|
||||
}
|
||||
if charIndex < 0 {
|
||||
charIndex = 0
|
||||
}
|
||||
|
||||
result.WriteRune(charset[charIndex])
|
||||
}
|
||||
result.WriteRune('\n')
|
||||
}
|
||||
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
// Uses standard luminance formula.
|
||||
func calculateBrightness(r, g, b uint32) int {
|
||||
// Convert 16-bit color to 8-bit
|
||||
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
|
||||
brightness := int(0.299*float64(r8) + 0.587*float64(g8) + 0.114*float64(b8))
|
||||
|
||||
if brightness > maxColorValue {
|
||||
brightness = maxColorValue
|
||||
}
|
||||
if brightness < 0 {
|
||||
brightness = 0
|
||||
}
|
||||
|
||||
return brightness
|
||||
}
|
||||
|
||||
// FormatASCIIOutput formats ASCII art with header and footer info.
|
||||
func FormatASCIIOutput(ascii string, imageInfo ImageInfo) string {
|
||||
var result strings.Builder
|
||||
|
||||
// Header
|
||||
result.WriteString("\n")
|
||||
result.WriteString("╔════════════════════════════════════════════════════════════════╗\n")
|
||||
result.WriteString("║ 📷 CAMERA SNAPSHOT (ASCII) ║\n")
|
||||
result.WriteString("╚════════════════════════════════════════════════════════════════╝\n")
|
||||
result.WriteString("\n")
|
||||
|
||||
// Image info
|
||||
if imageInfo.Width > 0 && imageInfo.Height > 0 {
|
||||
result.WriteString(fmt.Sprintf("📊 Original: %dx%d pixels\n", imageInfo.Width, imageInfo.Height))
|
||||
}
|
||||
if imageInfo.SizeBytes > 0 {
|
||||
result.WriteString(fmt.Sprintf("💾 Size: %s\n", formatBytes(imageInfo.SizeBytes)))
|
||||
}
|
||||
if imageInfo.CaptureTime != "" {
|
||||
result.WriteString(fmt.Sprintf("⏱️ Captured: %s\n", imageInfo.CaptureTime))
|
||||
}
|
||||
if imageInfo.Format != "" {
|
||||
result.WriteString(fmt.Sprintf("📁 Format: %s\n", imageInfo.Format))
|
||||
}
|
||||
result.WriteString("\n")
|
||||
|
||||
// ASCII art
|
||||
result.WriteString(ascii)
|
||||
|
||||
// Footer
|
||||
result.WriteString("\n")
|
||||
result.WriteString("╔════════════════════════════════════════════════════════════════╗\n")
|
||||
result.WriteString("💡 Tip: Higher resolution snapshots show better detail\n")
|
||||
result.WriteString("╚════════════════════════════════════════════════════════════════╝\n")
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
// ImageInfo holds metadata about the snapshot.
|
||||
type ImageInfo struct {
|
||||
Width int // Original width in pixels
|
||||
Height int // Original height in pixels
|
||||
SizeBytes int64 // File size in bytes
|
||||
Format string // Image format (JPEG, PNG, etc)
|
||||
CaptureTime string // Capture timestamp
|
||||
}
|
||||
|
||||
// formatBytes converts bytes to human-readable format.
|
||||
func formatBytes(byteCount int64) string {
|
||||
if byteCount < bufferSize1024 {
|
||||
return fmt.Sprintf("%d B", byteCount)
|
||||
}
|
||||
const kbSize = 1024
|
||||
const mbSize = 1024 * 1024
|
||||
if byteCount < mbSize {
|
||||
return fmt.Sprintf("%.1f KB", float64(byteCount)/kbSize)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.1f MB", float64(byteCount)/mbSize)
|
||||
}
|
||||
|
||||
// CreateASCIIHighQuality creates a high-quality ASCII representation.
|
||||
func CreateASCIIHighQuality(imageData []byte) (string, error) {
|
||||
config := ASCIIConfig{
|
||||
Width: largeASCIIWidth,
|
||||
Height: largeASCIIHeight,
|
||||
Invert: false,
|
||||
Quality: "high",
|
||||
}
|
||||
|
||||
return ImageToASCII(imageData, config)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrNoNetworkInterfaces is returned when no network interfaces are found.
|
||||
ErrNoNetworkInterfaces = errors.New("no network interfaces found")
|
||||
|
||||
// ErrNoCamerasFound is returned when no cameras are found on any interface.
|
||||
ErrNoCamerasFound = errors.New("no cameras found on any interface")
|
||||
|
||||
// ErrNoActiveInterfaces is returned when no active interfaces are available for discovery.
|
||||
ErrNoActiveInterfaces = errors.New("no active interfaces available for discovery")
|
||||
|
||||
// ErrNoProfilesFound is returned when no profiles are found.
|
||||
ErrNoProfilesFound = errors.New("no profiles found")
|
||||
|
||||
// ErrNoVideoSourceConfiguration is returned when no video source configuration is found.
|
||||
ErrNoVideoSourceConfiguration = errors.New("no video source configuration found")
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,365 +0,0 @@
|
||||
# ONVIF Camera Diagnostic Utility
|
||||
|
||||
A comprehensive diagnostic tool for collecting detailed information from ONVIF cameras. This utility helps analyze camera capabilities, troubleshoot issues, and generate reports for creating camera-specific tests.
|
||||
|
||||
## Features
|
||||
|
||||
✅ **Comprehensive Testing** - Tests all major ONVIF operations:
|
||||
- Device information and capabilities
|
||||
- Media profiles and streaming
|
||||
- Video encoder configurations
|
||||
- Imaging settings
|
||||
- PTZ status and presets (if available)
|
||||
- System date/time
|
||||
|
||||
✅ **Detailed Reporting** - Generates JSON reports with:
|
||||
- All successful operations with response data
|
||||
- Failed operations with error details
|
||||
- Response times for performance analysis
|
||||
- Structured data ready for test generation
|
||||
|
||||
✅ **Easy to Use** - Simple command-line interface with minimal requirements
|
||||
|
||||
✅ **XML Debugging** - For detailed debugging, see the companion `onvif-xml-capture` utility that captures raw SOAP XML
|
||||
|
||||
✅ **Helpful for**:
|
||||
- Creating camera-specific integration tests
|
||||
- Troubleshooting ONVIF compatibility issues
|
||||
- Analyzing camera capabilities
|
||||
- Debugging connection problems
|
||||
- Documenting camera configurations
|
||||
|
||||
## Installation
|
||||
|
||||
### Option 1: Build from source
|
||||
```bash
|
||||
cd /path/to/onvif-go
|
||||
go build -o onvif-diagnostics ./cmd/onvif-diagnostics/
|
||||
```
|
||||
|
||||
### Option 2: Install globally
|
||||
```bash
|
||||
go install ./cmd/onvif-diagnostics
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234"
|
||||
```
|
||||
|
||||
### With XML Capture (for debugging)
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-capture-xml \
|
||||
-verbose
|
||||
```
|
||||
|
||||
This creates two files:
|
||||
- `Manufacturer_Model_Firmware_timestamp.json` - Diagnostic report
|
||||
- `Manufacturer_Model_Firmware_xmlcapture_timestamp.tar.gz` - Raw SOAP XML archive
|
||||
|
||||
### Verbose Output
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-verbose
|
||||
```
|
||||
|
||||
### Capture Raw SOAP XML
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-capture-xml
|
||||
```
|
||||
|
||||
Enables XML traffic capture and creates a compressed tar.gz archive containing all SOAP request/response pairs. Useful for debugging XML parsing issues or analyzing camera behavior.
|
||||
|
||||
The archive contains:
|
||||
- `capture_001_GetDeviceInformation.json` - Request/response metadata with operation name
|
||||
- `capture_001_GetDeviceInformation_request.xml` - Formatted SOAP request
|
||||
- `capture_001_GetDeviceInformation_response.xml` - Formatted SOAP response
|
||||
- `capture_002_GetSystemDateAndTime.json` - Next operation metadata
|
||||
- ... (one set per SOAP operation, named by operation type)
|
||||
|
||||
Each file is named with the SOAP operation (e.g., GetDeviceInformation, GetProfiles) for easy identification.
|
||||
|
||||
Extract the archive:
|
||||
```bash
|
||||
tar -xzf camera-logs/Camera_Model_xmlcapture_timestamp.tar.gz
|
||||
```
|
||||
|
||||
### Custom Output Directory
|
||||
```bash
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://192.168.1.201/onvif/device_service" \
|
||||
-username "service" \
|
||||
-password "Service.1234" \
|
||||
-output ./my-camera-reports
|
||||
```
|
||||
|
||||
### All Options
|
||||
```
|
||||
Usage of ./onvif-diagnostics:
|
||||
-endpoint string
|
||||
ONVIF device endpoint (e.g., http://192.168.1.201/onvif/device_service)
|
||||
-username string
|
||||
ONVIF username
|
||||
-password string
|
||||
ONVIF password
|
||||
-output string
|
||||
Output directory for logs (default "./camera-logs")
|
||||
-timeout int
|
||||
Request timeout in seconds (default 30)
|
||||
-verbose
|
||||
Verbose output
|
||||
-include-raw
|
||||
Include raw SOAP responses (increases file size)
|
||||
```
|
||||
|
||||
## Example Output
|
||||
|
||||
```
|
||||
ONVIF Camera Diagnostic Utility v1.0.0
|
||||
========================================
|
||||
|
||||
Starting diagnostic collection...
|
||||
|
||||
→ 1. Getting device information...
|
||||
✓ Manufacturer: Bosch, Model: FLEXIDOME indoor 5100i IR
|
||||
→ 2. Getting system date and time...
|
||||
✓ Retrieved
|
||||
→ 3. Getting capabilities...
|
||||
✓ Services: Device, Media, Imaging, Events, Analytics
|
||||
→ 4. Discovering service endpoints...
|
||||
✓ Service endpoints discovered
|
||||
→ 5. Getting media profiles...
|
||||
✓ Found 4 profile(s)
|
||||
→ 6. Getting stream URIs for all profiles...
|
||||
✓ Retrieved 4/4 stream URIs
|
||||
→ 7. Getting snapshot URIs for all profiles...
|
||||
✓ Retrieved 4/4 snapshot URIs
|
||||
→ 8. Getting video encoder configurations...
|
||||
✓ Retrieved 4/4 video encoder configs
|
||||
→ 9. Getting imaging settings...
|
||||
✓ Retrieved 1/1 imaging settings
|
||||
→ 10. Getting PTZ status...
|
||||
ℹ No PTZ configurations found
|
||||
→ 11. Getting PTZ presets...
|
||||
ℹ No PTZ configurations found
|
||||
→ Saving diagnostic report...
|
||||
|
||||
========================================
|
||||
✓ Diagnostic collection complete!
|
||||
Report saved to: camera-logs/Bosch_FLEXIDOME_indoor_5100i_IR_8.71.0066_20251107-193656.json
|
||||
Total errors: 0
|
||||
|
||||
Device: Bosch FLEXIDOME indoor 5100i IR
|
||||
Firmware: 8.71.0066
|
||||
Profiles: 4
|
||||
|
||||
Please share this file for analysis and test creation.
|
||||
========================================
|
||||
```
|
||||
|
||||
## Report Structure
|
||||
|
||||
The generated JSON report includes:
|
||||
|
||||
```json
|
||||
{
|
||||
"timestamp": "2025-11-07T19:36:56Z",
|
||||
"utility_version": "1.0.0",
|
||||
"connection_info": {
|
||||
"endpoint": "http://192.168.1.201/onvif/device_service",
|
||||
"username": "service",
|
||||
"test_date": "2025-11-07"
|
||||
},
|
||||
"device_info": {
|
||||
"success": true,
|
||||
"data": {
|
||||
"manufacturer": "Bosch",
|
||||
"model": "FLEXIDOME indoor 5100i IR",
|
||||
"firmware_version": "8.71.0066",
|
||||
"serial_number": "404754734001050102",
|
||||
"hardware_id": "F000B543"
|
||||
},
|
||||
"response_time": "21.5ms"
|
||||
},
|
||||
"profiles": {
|
||||
"success": true,
|
||||
"count": 4,
|
||||
"data": [ /* profile details */ ]
|
||||
},
|
||||
"stream_uris": [ /* stream URI results for each profile */ ],
|
||||
"errors": [ /* any errors encountered */ ]
|
||||
}
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Creating Camera-Specific Tests
|
||||
Run the diagnostic on your camera and share the JSON file. The report contains all the information needed to create comprehensive integration tests.
|
||||
|
||||
### 2. Troubleshooting Connection Issues
|
||||
If your camera isn't working, run diagnostics to see exactly which operations fail and what error messages are returned.
|
||||
|
||||
### 3. Comparing Cameras
|
||||
Run diagnostics on multiple cameras to compare capabilities, response times, and compatibility.
|
||||
|
||||
### 4. Documentation
|
||||
Generate detailed reports of camera configurations for documentation purposes.
|
||||
|
||||
## Interpreting Results
|
||||
|
||||
### Success Indicators
|
||||
- ✓ Green checkmarks indicate successful operations
|
||||
- Response times help identify performance issues
|
||||
- High success rates indicate good compatibility
|
||||
|
||||
### Error Indicators
|
||||
- ✗ Red X marks indicate failed operations
|
||||
- ℹ Info symbols indicate optional features not available
|
||||
- Check the `errors` array in JSON for detailed error messages
|
||||
|
||||
### Common Issues
|
||||
|
||||
**All operations fail:**
|
||||
- Check network connectivity
|
||||
- Verify endpoint URL is correct
|
||||
- Ensure camera is powered on
|
||||
|
||||
**Authentication errors:**
|
||||
- Verify username and password
|
||||
- Check user permissions on camera
|
||||
|
||||
**Some profiles fail:**
|
||||
- Camera may have different capabilities per profile
|
||||
- Some operations may not be supported by all profiles
|
||||
|
||||
**Timeout errors:**
|
||||
- Increase timeout with `-timeout 60`
|
||||
- Check network latency
|
||||
- Verify camera is responding
|
||||
|
||||
## Sharing Reports
|
||||
|
||||
When sharing diagnostic reports:
|
||||
|
||||
1. **Anonymize if needed** - The report includes:
|
||||
- IP addresses (in endpoint)
|
||||
- Usernames (not passwords)
|
||||
- Serial numbers
|
||||
|
||||
2. **What to share**:
|
||||
- The complete JSON file
|
||||
- Any console output showing errors
|
||||
- Camera model and firmware version
|
||||
|
||||
3. **Where to share**:
|
||||
- GitHub Issues
|
||||
- Email for analysis
|
||||
- Pull request descriptions
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Batch Testing Multiple Cameras
|
||||
Create a script to test multiple cameras:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cameras=(
|
||||
"192.168.1.201:service:password1"
|
||||
"192.168.1.202:admin:password2"
|
||||
"192.168.1.203:user:password3"
|
||||
)
|
||||
|
||||
for camera in "${cameras[@]}"; do
|
||||
IFS=':' read -r ip user pass <<< "$camera"
|
||||
echo "Testing camera at $ip..."
|
||||
./onvif-diagnostics \
|
||||
-endpoint "http://$ip/onvif/device_service" \
|
||||
-username "$user" \
|
||||
-password "$pass"
|
||||
done
|
||||
```
|
||||
|
||||
### Automated Testing
|
||||
Include in CI/CD pipelines:
|
||||
|
||||
```yaml
|
||||
- name: Run ONVIF Diagnostics
|
||||
run: |
|
||||
./onvif-diagnostics \
|
||||
-endpoint "${{ secrets.CAMERA_ENDPOINT }}" \
|
||||
-username "${{ secrets.CAMERA_USERNAME }}" \
|
||||
-password "${{ secrets.CAMERA_PASSWORD }}" \
|
||||
-output ./reports
|
||||
|
||||
- name: Upload Diagnostic Reports
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: camera-diagnostics
|
||||
path: ./reports/
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### Adding New Tests
|
||||
|
||||
To add new diagnostic tests, edit `cmd/onvif-diagnostics/main.go`:
|
||||
|
||||
1. Create a new test function following the pattern:
|
||||
```go
|
||||
func testNewOperation(ctx context.Context, client *onvif.Client, report *CameraReport) *NewOperationResult {
|
||||
// Implementation
|
||||
}
|
||||
```
|
||||
|
||||
2. Add result struct to store data
|
||||
3. Call the test in main()
|
||||
4. Update report structure
|
||||
|
||||
### Building for Different Platforms
|
||||
|
||||
```bash
|
||||
# Linux
|
||||
GOOS=linux GOARCH=amd64 go build -o onvif-diagnostics-linux ./cmd/onvif-diagnostics/
|
||||
|
||||
# Windows
|
||||
GOOS=windows GOARCH=amd64 go build -o onvif-diagnostics.exe ./cmd/onvif-diagnostics/
|
||||
|
||||
# macOS ARM
|
||||
GOOS=darwin GOARCH=arm64 go build -o onvif-diagnostics-mac-arm ./cmd/onvif-diagnostics/
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Same as parent project.
|
||||
|
||||
## Support
|
||||
|
||||
For issues or questions:
|
||||
1. Run diagnostics with `-verbose` flag
|
||||
2. Share the generated JSON report
|
||||
3. **For XML parsing issues**: Use `onvif-xml-capture` utility to capture raw SOAP XML
|
||||
4. Open a GitHub issue with the report attached
|
||||
|
||||
## Related Tools
|
||||
|
||||
- **onvif-xml-capture** - Captures raw SOAP XML requests/responses for detailed debugging
|
||||
- Location: `cmd/onvif-xml-capture/`
|
||||
- Use when: Diagnostic report shows errors and you need to see raw XML
|
||||
- See: `XML_DEBUGGING_SOLUTION.md` for complete guide
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,442 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go"
|
||||
"github.com/0x524a/onvif-go/discovery"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultUsername = "admin"
|
||||
defaultTimeout = 10
|
||||
defaultRetryDelay = 5
|
||||
ptzTimeout = 30
|
||||
ptzStepSize = 2
|
||||
ptzSpeed = 0.5
|
||||
maxBodyPreview = 200
|
||||
)
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("🎥 Quick ONVIF Camera Tool")
|
||||
fmt.Println("==========================")
|
||||
fmt.Println()
|
||||
|
||||
for {
|
||||
fmt.Println("What would you like to do?")
|
||||
fmt.Println("1. 🔍 Discover cameras")
|
||||
fmt.Println("2. 🌐 List network interfaces")
|
||||
fmt.Println("3. 📹 Connect to camera")
|
||||
fmt.Println("4. 🎮 PTZ demo")
|
||||
fmt.Println("5. 📡 Get stream URLs")
|
||||
fmt.Println("0. Exit")
|
||||
fmt.Print("\nChoice: ")
|
||||
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
input, _ := reader.ReadString('\n')
|
||||
choice := strings.TrimSpace(input)
|
||||
|
||||
switch choice {
|
||||
case "1":
|
||||
discoverCameras()
|
||||
case "2":
|
||||
listNetworkInterfaces()
|
||||
case "3":
|
||||
connectAndShowInfo()
|
||||
case "4":
|
||||
ptzDemo()
|
||||
case "5":
|
||||
getStreamURLs()
|
||||
case "0", "q", "quit":
|
||||
fmt.Println("Goodbye! 👋")
|
||||
|
||||
return
|
||||
default:
|
||||
fmt.Println("Invalid choice. Please try again.")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func discoverCameras() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("🔍 Discovering cameras on network...")
|
||||
|
||||
// Ask if user wants to use a specific interface
|
||||
fmt.Print("Use specific network interface? (y/n) [n]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
useInterface, _ := reader.ReadString('\n')
|
||||
useInterface = strings.ToLower(strings.TrimSpace(useInterface))
|
||||
|
||||
var opts *discovery.DiscoverOptions
|
||||
if useInterface == "y" || useInterface == "yes" {
|
||||
// List interfaces
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("\nAvailable interfaces:")
|
||||
for i, iface := range interfaces {
|
||||
fmt.Printf(" %d. %s (%v)\n", i+1, iface.Name, iface.Addresses)
|
||||
}
|
||||
|
||||
fmt.Print("\nEnter interface name or IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ifaceInput, _ := reader.ReadString('\n')
|
||||
ifaceInput = strings.TrimSpace(ifaceInput)
|
||||
|
||||
if ifaceInput != "" {
|
||||
opts = &discovery.DiscoverOptions{
|
||||
NetworkInterface: ifaceInput,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &discovery.DiscoverOptions{}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout*time.Second)
|
||||
defer cancel()
|
||||
|
||||
devices, err := discovery.DiscoverWithOptions(ctx, defaultRetryDelay*time.Second, opts)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(devices) == 0 {
|
||||
fmt.Println("No cameras found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d camera(s):\n", len(devices))
|
||||
for i, device := range devices {
|
||||
fmt.Printf(" %d. %s (%s)\n", i+1, device.GetName(), device.GetDeviceEndpoint())
|
||||
}
|
||||
}
|
||||
|
||||
func listNetworkInterfaces() {
|
||||
fmt.Println("🌐 Network Interfaces")
|
||||
fmt.Println("====================")
|
||||
|
||||
interfaces, err := discovery.ListNetworkInterfaces()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(interfaces) == 0 {
|
||||
fmt.Println("No network interfaces found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d interface(s):\n\n", len(interfaces))
|
||||
|
||||
for _, iface := range interfaces {
|
||||
upStr := "Up"
|
||||
if !iface.Up {
|
||||
upStr = "Down"
|
||||
}
|
||||
|
||||
multicastStr := "Yes"
|
||||
if !iface.Multicast {
|
||||
multicastStr = "No"
|
||||
}
|
||||
|
||||
fmt.Printf("📡 %s (%s, Multicast: %s)\n", iface.Name, upStr, multicastStr)
|
||||
|
||||
if len(iface.Addresses) > 0 {
|
||||
for _, addr := range iface.Addresses {
|
||||
fmt.Printf(" └─ %s\n", addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func connectAndShowInfo() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
fmt.Printf("Connecting to %s...\n", endpoint)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
onvif.WithTimeout(ptzTimeout*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Get device info
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Connection failed: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Connected!\n")
|
||||
fmt.Printf("📹 %s %s\n", info.Manufacturer, info.Model)
|
||||
fmt.Printf("🔧 Firmware: %s\n", info.FirmwareVersion)
|
||||
|
||||
// Initialize and get profiles
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err == nil && len(profiles) > 0 {
|
||||
fmt.Printf("📺 %d profile(s) available\n", len(profiles))
|
||||
|
||||
// Show first stream URL
|
||||
streamURI, err := client.GetStreamURI(ctx, profiles[0].Token)
|
||||
if err == nil {
|
||||
fmt.Printf("📡 Stream: %s\n", streamURI.URI)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ptzDemo() { //nolint:funlen,gocyclo // Many statements and high complexity due to user interaction
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil || len(profiles) == 0 {
|
||||
fmt.Println("❌ No profiles found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
profileToken := profiles[0].Token
|
||||
|
||||
// Check PTZ status
|
||||
status, err := client.GetStatus(ctx, profileToken)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ PTZ not supported: %v\n", 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)
|
||||
}
|
||||
|
||||
fmt.Println("\n🎮 PTZ Demo - Choose movement:")
|
||||
fmt.Println("1. Move right")
|
||||
fmt.Println("2. Move left")
|
||||
fmt.Println("3. Move up")
|
||||
fmt.Println("4. Move down")
|
||||
fmt.Println("5. Go to center")
|
||||
fmt.Print("Choice: ")
|
||||
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
choice, _ := reader.ReadString('\n')
|
||||
choice = strings.TrimSpace(choice)
|
||||
|
||||
var velocity *onvif.PTZSpeed
|
||||
var position *onvif.PTZVector
|
||||
|
||||
switch choice {
|
||||
case "1":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: ptzSpeed, Y: 0.0}}
|
||||
case "2":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: -ptzSpeed, Y: 0.0}}
|
||||
case "3":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: 0.0, Y: ptzSpeed}}
|
||||
case "4":
|
||||
velocity = &onvif.PTZSpeed{PanTilt: &onvif.Vector2D{X: 0.0, Y: -ptzSpeed}}
|
||||
case "5":
|
||||
position = &onvif.PTZVector{PanTilt: &onvif.Vector2D{X: 0.0, Y: 0.0}}
|
||||
default:
|
||||
fmt.Println("Invalid choice")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if velocity != nil {
|
||||
timeout := fmt.Sprintf("PT%dS", ptzStepSize)
|
||||
err = client.ContinuousMove(ctx, profileToken, velocity, &timeout)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
fmt.Println("✅ Moving for 2 seconds...")
|
||||
time.Sleep(ptzStepSize * time.Second)
|
||||
//nolint:errcheck // Stop error is not critical for demo
|
||||
_ = client.Stop(ctx, profileToken, true, false)
|
||||
} else if position != nil {
|
||||
err = client.AbsoluteMove(ctx, profileToken, position, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
fmt.Println("✅ Moving to center...")
|
||||
}
|
||||
|
||||
fmt.Println("Demo complete!")
|
||||
}
|
||||
|
||||
func getStreamURLs() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Camera IP: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
ip, _ := reader.ReadString('\n')
|
||||
ip = strings.TrimSpace(ip)
|
||||
|
||||
fmt.Print("Username [admin]: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
username = defaultUsername
|
||||
}
|
||||
|
||||
fmt.Print("Password: ")
|
||||
//nolint:errcheck // ReadString error on stdin is rare and not critical for CLI
|
||||
password, _ := reader.ReadString('\n')
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s/onvif/device_service", ip)
|
||||
|
||||
client, err := onvif.NewClient(
|
||||
endpoint,
|
||||
onvif.WithCredentials(username, password),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
//nolint:errcheck // Ignore initialization errors, we'll catch them on GetProfiles
|
||||
_ = client.Initialize(ctx)
|
||||
|
||||
profiles, err := client.GetProfiles(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(profiles) == 0 {
|
||||
fmt.Println("❌ No profiles found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Found %d profile(s):\n\n", len(profiles))
|
||||
|
||||
for i, profile := range profiles {
|
||||
fmt.Printf("📹 Profile %d: %s\n", i+1, profile.Name)
|
||||
|
||||
// Stream URI
|
||||
streamURI, err := client.GetStreamURI(ctx, profile.Token)
|
||||
if err != nil {
|
||||
fmt.Printf(" Stream: ❌ Error\n")
|
||||
} else {
|
||||
fmt.Printf(" 📡 Stream: %s\n", streamURI.URI)
|
||||
}
|
||||
|
||||
// Snapshot URI
|
||||
snapshotURI, err := client.GetSnapshotURI(ctx, profile.Token)
|
||||
if err != nil {
|
||||
fmt.Printf(" Snapshot: ❌ Error\n")
|
||||
} else {
|
||||
fmt.Printf(" 📸 Snapshot: %s\n", snapshotURI.URI)
|
||||
}
|
||||
|
||||
// Video info
|
||||
if profile.VideoEncoderConfiguration != nil {
|
||||
fmt.Printf(" 🎬 Encoding: %s", profile.VideoEncoderConfiguration.Encoding)
|
||||
if profile.VideoEncoderConfiguration.Resolution != nil {
|
||||
fmt.Printf(" (%dx%d)",
|
||||
profile.VideoEncoderConfiguration.Resolution.Width,
|
||||
profile.VideoEncoderConfiguration.Resolution.Height)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
fmt.Println("💡 Tips:")
|
||||
fmt.Println(" - Use VLC to open RTSP streams")
|
||||
fmt.Println(" - Open snapshot URLs in a web browser")
|
||||
fmt.Println(" - Some cameras may require authentication in the URL")
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/0x524a/onvif-go/server"
|
||||
)
|
||||
|
||||
var (
|
||||
version = "1.0.0"
|
||||
)
|
||||
|
||||
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", 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")
|
||||
model := flag.String("model", "Virtual Multi-Lens Camera", "Device model")
|
||||
firmware := flag.String("firmware", "1.0.0", "Firmware version")
|
||||
serial := flag.String("serial", "SN-12345678", "Serial number")
|
||||
profiles := flag.Int(
|
||||
"profiles", maxWorkers, "Number of camera profiles (1-10)",
|
||||
)
|
||||
ptz := flag.Bool("ptz", true, "Enable PTZ support")
|
||||
imaging := flag.Bool("imaging", true, "Enable Imaging support")
|
||||
events := flag.Bool("events", false, "Enable Events support")
|
||||
info := flag.Bool("info", false, "Show server info and exit")
|
||||
showVersion := flag.Bool("version", false, "Show version and exit")
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "ONVIF Server - Virtual IP Camera Simulator\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "Options:\n")
|
||||
flag.PrintDefaults()
|
||||
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
||||
fmt.Fprintf(os.Stderr, " # Start with default settings (3 profiles, PTZ enabled)\n")
|
||||
fmt.Fprintf(os.Stderr, " %s\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Start with custom credentials and 5 profiles\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -username myuser -password mypass -profiles 5\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Start on specific port without PTZ\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -port 9000 -ptz=false\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " # Show server information\n")
|
||||
fmt.Fprintf(os.Stderr, " %s -info\n\n", os.Args[0])
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Handle version flag
|
||||
if *showVersion {
|
||||
fmt.Printf("onvif-server version %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Validate profiles count
|
||||
if *profiles < 1 || *profiles > 10 {
|
||||
log.Fatal("Number of profiles must be between 1 and 10")
|
||||
}
|
||||
|
||||
// Create server configuration
|
||||
config := buildConfig(*host, *port, *username, *password, *manufacturer, *model,
|
||||
*firmware, *serial, *profiles, *ptz, *imaging, *events)
|
||||
|
||||
// Create server
|
||||
srv, err := server.New(config)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create server: %v", err)
|
||||
}
|
||||
|
||||
// Handle info flag
|
||||
if *info {
|
||||
fmt.Println(srv.ServerInfo())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Print banner
|
||||
printBanner()
|
||||
|
||||
// Create context that listens for interrupt signals
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Setup signal handler
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// Start server in goroutine
|
||||
go func() {
|
||||
if err := srv.Start(ctx); err != nil {
|
||||
log.Printf("Server error: %v", err)
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for interrupt signal
|
||||
<-sigChan
|
||||
fmt.Println("\n🛑 Received interrupt signal, shutting down...")
|
||||
cancel()
|
||||
|
||||
// Give the server a moment to shut down gracefully
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("✅ Server stopped")
|
||||
}
|
||||
|
||||
// buildConfig creates a server configuration from command-line arguments.
|
||||
func buildConfig(host string, port int, username, password, manufacturer, model,
|
||||
firmware, serial string, numProfiles int, ptz, imaging, events bool) *server.Config {
|
||||
config := &server.Config{
|
||||
Host: host,
|
||||
Port: port,
|
||||
BasePath: "/onvif",
|
||||
Timeout: defaultTimeout * time.Second,
|
||||
DeviceInfo: server.DeviceInfo{
|
||||
Manufacturer: manufacturer,
|
||||
Model: model,
|
||||
FirmwareVersion: firmware,
|
||||
SerialNumber: serial,
|
||||
HardwareID: "HW-87654321",
|
||||
},
|
||||
Username: username,
|
||||
Password: password,
|
||||
SupportPTZ: ptz,
|
||||
SupportImaging: imaging,
|
||||
SupportEvents: events,
|
||||
Profiles: make([]server.ProfileConfig, numProfiles),
|
||||
}
|
||||
|
||||
// Define profile templates
|
||||
templates := []struct {
|
||||
name string
|
||||
width int
|
||||
height int
|
||||
framerate int
|
||||
bitrate int
|
||||
quality float64
|
||||
hasPTZ bool
|
||||
ptzZoomMax float64
|
||||
}{
|
||||
{"Main Camera - High Quality", 1920, 1080, 30, 4096, 80, true, 1},
|
||||
{"Wide Angle Camera", 1280, 720, 30, 2048, 75, false, 0},
|
||||
{"Telephoto Camera", 1920, 1080, 25, 6144, 85, true, 3},
|
||||
{"Low Light Camera", 1920, 1080, 30, 4096, 80, false, 0},
|
||||
{"Ultra HD Camera", 3840, 2160, 30, 16384, 90, true, 2},
|
||||
{"Compact Camera", 640, 480, 30, 512, 70, false, 0},
|
||||
{"PTZ Dome Camera", 1920, 1080, 30, 4096, 80, true, 2},
|
||||
{"Fisheye Camera", 1920, 1080, 30, 4096, 80, false, 0},
|
||||
{"Thermal Camera", 640, 480, 30, 1024, 75, true, 1},
|
||||
{"License Plate Camera", 1920, 1080, 60, 8192, 90, true, 5},
|
||||
}
|
||||
|
||||
// Generate profiles
|
||||
for i := 0; i < numProfiles; i++ {
|
||||
template := templates[i%len(templates)]
|
||||
|
||||
profile := server.ProfileConfig{
|
||||
Token: fmt.Sprintf("profile_%d", i),
|
||||
Name: template.name,
|
||||
VideoSource: server.VideoSourceConfig{
|
||||
Token: fmt.Sprintf("video_source_%d", i),
|
||||
Name: template.name,
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Framerate: template.framerate,
|
||||
Bounds: server.Bounds{X: 0, Y: 0, Width: template.width, Height: template.height},
|
||||
},
|
||||
VideoEncoder: server.VideoEncoderConfig{
|
||||
Encoding: "H264",
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Quality: template.quality,
|
||||
Framerate: template.framerate,
|
||||
Bitrate: template.bitrate,
|
||||
GovLength: template.framerate,
|
||||
},
|
||||
Snapshot: server.SnapshotConfig{
|
||||
Enabled: true,
|
||||
Resolution: server.Resolution{Width: template.width, Height: template.height},
|
||||
Quality: template.quality + 5, //nolint:mnd // Quality offset
|
||||
},
|
||||
}
|
||||
|
||||
// Add PTZ if enabled and template supports it
|
||||
if ptz && template.hasPTZ {
|
||||
profile.PTZ = &server.PTZConfig{
|
||||
NodeToken: fmt.Sprintf("ptz_node_%d", i),
|
||||
PanRange: server.Range{Min: -ptzMaxPan, Max: ptzMaxPan},
|
||||
TiltRange: server.Range{Min: -ptzMaxTilt, Max: ptzMaxTilt},
|
||||
ZoomRange: server.Range{Min: 0, Max: template.ptzZoomMax},
|
||||
DefaultSpeed: server.PTZSpeed{Pan: ptzSpeed, Tilt: ptzSpeed, Zoom: ptzSpeed},
|
||||
SupportsContinuous: true,
|
||||
SupportsAbsolute: true,
|
||||
SupportsRelative: true,
|
||||
Presets: []server.Preset{
|
||||
{
|
||||
Token: fmt.Sprintf("preset_%d_0", i),
|
||||
Name: "Home",
|
||||
Position: server.PTZPosition{Pan: 0, Tilt: 0, Zoom: 0},
|
||||
},
|
||||
{
|
||||
Token: fmt.Sprintf("preset_%d_1", i),
|
||||
Name: "Entrance",
|
||||
Position: server.PTZPosition{
|
||||
Pan: -45, Tilt: -10, Zoom: template.ptzZoomMax * ptzSpeed,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
config.Profiles[i] = profile
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// printBanner prints the application banner.
|
||||
func printBanner() {
|
||||
banner := `
|
||||
╔═══════════════════════════════════════════════════════════╗
|
||||
║ ║
|
||||
║ 🎥 ONVIF Virtual Camera Server 🎥 ║
|
||||
║ ║
|
||||
║ Simulate multi-lens IP cameras with ONVIF support ║
|
||||
║ Version: ` + version + ` ║
|
||||
║ ║
|
||||
╚═══════════════════════════════════════════════════════════╝
|
||||
`
|
||||
fmt.Println(banner)
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
#!/bin/bash
|
||||
# collect-camera-data.sh - Collect test data from all discovered cameras
|
||||
|
||||
set -e
|
||||
|
||||
# Color output
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "${GREEN}ONVIF Camera Data Collection${NC}"
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo ""
|
||||
|
||||
# Check if diagnostics tool exists
|
||||
if [ ! -f "./bin/onvif-diagnostics" ]; then
|
||||
echo -e "${RED}Error: onvif-diagnostics not found. Building...${NC}"
|
||||
go build -o bin/onvif-diagnostics ./cmd/onvif-diagnostics
|
||||
echo -e "${GREEN}✓ Built onvif-diagnostics${NC}"
|
||||
fi
|
||||
|
||||
# Prompt for credentials
|
||||
echo -e "${YELLOW}Enter ONVIF credentials for your cameras:${NC}"
|
||||
read -p "Username: " ONVIF_USER
|
||||
read -sp "Password: " ONVIF_PASS
|
||||
echo ""
|
||||
echo ""
|
||||
|
||||
# Cameras discovered
|
||||
declare -a CAMERAS=(
|
||||
"192.168.2.61:8000|Reolink_E1Zoom"
|
||||
"192.168.2.57:80|Bosch_AUTODOME_5000i"
|
||||
"192.168.2.82:80|AXIS_P3818"
|
||||
"192.168.2.236:8000|Reolink_TrackMixWiFi"
|
||||
"192.168.2.200:80|Bosch_FLEXIDOME_8000i"
|
||||
"192.168.2.24:80|Bosch_FLEXIDOME_5100i"
|
||||
"192.168.2.190:80|AXIS_Q3819"
|
||||
"192.168.2.30:80|AXIS_P5655"
|
||||
)
|
||||
|
||||
SUCCESS=0
|
||||
FAILED=0
|
||||
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
|
||||
|
||||
# Create output directory for this batch
|
||||
BATCH_DIR="camera-data-batch-${TIMESTAMP}"
|
||||
mkdir -p "${BATCH_DIR}"
|
||||
|
||||
echo -e "${GREEN}Collecting data from ${#CAMERAS[@]} cameras...${NC}"
|
||||
echo ""
|
||||
|
||||
# Loop through each camera
|
||||
for camera_info in "${CAMERAS[@]}"; do
|
||||
IFS='|' read -r ip_port name <<< "$camera_info"
|
||||
|
||||
# Check if port is specified
|
||||
if [[ $ip_port == *":"* ]]; then
|
||||
ENDPOINT="http://${ip_port}/onvif/device_service"
|
||||
else
|
||||
ENDPOINT="http://${ip_port}:80/onvif/device_service"
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||
echo -e "${YELLOW}Camera: ${name}${NC}"
|
||||
echo -e "${YELLOW}Endpoint: ${ENDPOINT}${NC}"
|
||||
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||
|
||||
# Run COMPREHENSIVE diagnostics with XML capture (captures all operations)
|
||||
if ./bin/onvif-diagnostics \
|
||||
-endpoint "${ENDPOINT}" \
|
||||
-username "${ONVIF_USER}" \
|
||||
-password "${ONVIF_PASS}" \
|
||||
-capture-all \
|
||||
-verbose 2>&1 | tee "${BATCH_DIR}/${name}_log.txt"; then
|
||||
|
||||
echo -e "${GREEN}✓ Successfully captured data from ${name}${NC}"
|
||||
SUCCESS=$((SUCCESS + 1))
|
||||
else
|
||||
echo -e "${RED}✗ Failed to capture data from ${name}${NC}"
|
||||
FAILED=$((FAILED + 1))
|
||||
fi
|
||||
|
||||
echo ""
|
||||
sleep 2 # Brief delay between cameras to avoid network congestion
|
||||
done
|
||||
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "${GREEN}Collection Complete${NC}"
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "Success: ${GREEN}${SUCCESS}${NC} / ${#CAMERAS[@]}"
|
||||
echo -e "Failed: ${RED}${FAILED}${NC} / ${#CAMERAS[@]}"
|
||||
echo ""
|
||||
echo -e "${YELLOW}Results saved to: ${BATCH_DIR}/${NC}"
|
||||
echo ""
|
||||
|
||||
# Move camera-logs to batch directory
|
||||
if [ -d "camera-logs" ]; then
|
||||
echo -e "${YELLOW}Moving camera-logs to batch directory...${NC}"
|
||||
mv camera-logs/* "${BATCH_DIR}/" 2>/dev/null || true
|
||||
echo -e "${GREEN}✓ Logs organized${NC}"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${GREEN}Next steps:${NC}"
|
||||
echo "1. Review the capture files in ${BATCH_DIR}/"
|
||||
echo "2. Copy .tar.gz files to testdata/captures/"
|
||||
echo "3. Run: go build -o bin/generate-tests ./cmd/generate-tests"
|
||||
echo "4. Generate tests for each camera capture"
|
||||
echo ""
|
||||
@@ -1,111 +0,0 @@
|
||||
#!/bin/bash
|
||||
# collect-camera-data.sh - Collect test data from all discovered cameras
|
||||
|
||||
set -e
|
||||
|
||||
# Color output
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "${GREEN}ONVIF Camera Data Collection${NC}"
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo ""
|
||||
|
||||
# Check if diagnostics tool exists
|
||||
if [ ! -f "./bin/onvif-diagnostics" ]; then
|
||||
echo -e "${RED}Error: onvif-diagnostics not found. Building...${NC}"
|
||||
go build -o bin/onvif-diagnostics ./cmd/onvif-diagnostics
|
||||
echo -e "${GREEN}✓ Built onvif-diagnostics${NC}"
|
||||
fi
|
||||
|
||||
# Prompt for credentials
|
||||
echo -e "${YELLOW}Enter ONVIF credentials for your cameras:${NC}"
|
||||
read -p "Username: " ONVIF_USER
|
||||
read -sp "Password: " ONVIF_PASS
|
||||
echo ""
|
||||
echo ""
|
||||
|
||||
# Cameras discovered
|
||||
declare -a CAMERAS=(
|
||||
"192.168.2.61:8000|Reolink_E1Zoom"
|
||||
"192.168.2.57:80|Bosch_AUTODOME_5000i"
|
||||
"192.168.2.82:80|AXIS_P3818"
|
||||
"192.168.2.236:8000|Reolink_TrackMixWiFi"
|
||||
"192.168.2.200:80|Bosch_FLEXIDOME_8000i"
|
||||
"192.168.2.24:80|Bosch_FLEXIDOME_5100i"
|
||||
"192.168.2.190:80|AXIS_Q3819"
|
||||
"192.168.2.30:80|AXIS_P5655"
|
||||
)
|
||||
|
||||
SUCCESS=0
|
||||
FAILED=0
|
||||
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
|
||||
|
||||
# Create output directory for this batch
|
||||
BATCH_DIR="camera-data-batch-${TIMESTAMP}"
|
||||
mkdir -p "${BATCH_DIR}"
|
||||
|
||||
echo -e "${GREEN}Collecting data from ${#CAMERAS[@]} cameras...${NC}"
|
||||
echo ""
|
||||
|
||||
# Loop through each camera
|
||||
for camera_info in "${CAMERAS[@]}"; do
|
||||
IFS='|' read -r ip_port name <<< "$camera_info"
|
||||
|
||||
# Check if port is specified
|
||||
if [[ $ip_port == *":"* ]]; then
|
||||
ENDPOINT="http://${ip_port}/onvif/device_service"
|
||||
else
|
||||
ENDPOINT="http://${ip_port}:80/onvif/device_service"
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||
echo -e "${YELLOW}Camera: ${name}${NC}"
|
||||
echo -e "${YELLOW}Endpoint: ${ENDPOINT}${NC}"
|
||||
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||
|
||||
# Run COMPREHENSIVE diagnostics with XML capture (captures all operations)
|
||||
if ./bin/onvif-diagnostics \
|
||||
-endpoint "${ENDPOINT}" \
|
||||
-username "${ONVIF_USER}" \
|
||||
-password "${ONVIF_PASS}" \
|
||||
-capture-all \
|
||||
-verbose 2>&1 | tee "${BATCH_DIR}/${name}_log.txt"; then
|
||||
|
||||
echo -e "${GREEN}✓ Successfully captured data from ${name}${NC}"
|
||||
SUCCESS=$((SUCCESS + 1))
|
||||
else
|
||||
echo -e "${RED}✗ Failed to capture data from ${name}${NC}"
|
||||
FAILED=$((FAILED + 1))
|
||||
fi
|
||||
|
||||
echo ""
|
||||
sleep 2 # Brief delay between cameras to avoid network congestion
|
||||
done
|
||||
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "${GREEN}Collection Complete${NC}"
|
||||
echo -e "${GREEN}========================================${NC}"
|
||||
echo -e "Success: ${GREEN}${SUCCESS}${NC} / ${#CAMERAS[@]}"
|
||||
echo -e "Failed: ${RED}${FAILED}${NC} / ${#CAMERAS[@]}"
|
||||
echo ""
|
||||
echo -e "${YELLOW}Results saved to: ${BATCH_DIR}/${NC}"
|
||||
echo ""
|
||||
|
||||
# Move camera-logs to batch directory
|
||||
if [ -d "camera-logs" ]; then
|
||||
echo -e "${YELLOW}Moving camera-logs to batch directory...${NC}"
|
||||
mv camera-logs/* "${BATCH_DIR}/" 2>/dev/null || true
|
||||
echo -e "${GREEN}✓ Logs organized${NC}"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${GREEN}Next steps:${NC}"
|
||||
echo "1. Review the capture files in ${BATCH_DIR}/"
|
||||
echo "2. Copy .tar.gz files to testdata/captures/"
|
||||
echo "3. Run: go build -o bin/generate-tests ./cmd/generate-tests"
|
||||
echo "4. Generate tests for each camera capture"
|
||||
echo ""
|
||||
File diff suppressed because it is too large
Load Diff
-1096
File diff suppressed because it is too large
Load Diff
@@ -1,229 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetGeoLocation retrieves geographic location information. ONVIF Specification: GetGeoLocation operation.
|
||||
func (c *Client) GetGeoLocation(ctx context.Context) ([]LocationEntity, error) {
|
||||
type GetGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"GetGeoLocationResponse"`
|
||||
Location []LocationEntity `xml:"Location"`
|
||||
}
|
||||
|
||||
request := GetGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Location, nil
|
||||
}
|
||||
|
||||
// SetGeoLocation sets geographic location information. ONVIF Specification: SetGeoLocation operation.
|
||||
func (c *Client) SetGeoLocation(ctx context.Context, location []LocationEntity) error {
|
||||
type SetGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Location []LocationEntity `xml:"tds:Location"`
|
||||
}
|
||||
|
||||
type SetGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"SetGeoLocationResponse"`
|
||||
}
|
||||
|
||||
request := SetGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Location: location,
|
||||
}
|
||||
var response SetGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteGeoLocation deletes geographic location information. ONVIF Specification: DeleteGeoLocation operation.
|
||||
func (c *Client) DeleteGeoLocation(ctx context.Context, location []LocationEntity) error {
|
||||
type DeleteGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Location []LocationEntity `xml:"tds:Location"`
|
||||
}
|
||||
|
||||
type DeleteGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteGeoLocationResponse"`
|
||||
}
|
||||
|
||||
request := DeleteGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Location: location,
|
||||
}
|
||||
var response DeleteGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDPAddresses retrieves DP (Device Provisioning) addresses. ONVIF Specification: GetDPAddresses operation.
|
||||
func (c *Client) GetDPAddresses(ctx context.Context) ([]NetworkHost, error) {
|
||||
type GetDPAddressesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetDPAddresses"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetDPAddressesResponse struct {
|
||||
XMLName xml.Name `xml:"GetDPAddressesResponse"`
|
||||
DPAddress []NetworkHost `xml:"DPAddress"`
|
||||
}
|
||||
|
||||
request := GetDPAddressesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetDPAddressesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetDPAddresses failed: %w", err)
|
||||
}
|
||||
|
||||
return response.DPAddress, nil
|
||||
}
|
||||
|
||||
// SetDPAddresses sets DP (Device Provisioning) addresses. ONVIF Specification: SetDPAddresses operation.
|
||||
func (c *Client) SetDPAddresses(ctx context.Context, dpAddress []NetworkHost) error {
|
||||
type SetDPAddressesBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetDPAddresses"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
DPAddress []NetworkHost `xml:"tds:DPAddress"`
|
||||
}
|
||||
|
||||
type SetDPAddressesResponse struct {
|
||||
XMLName xml.Name `xml:"SetDPAddressesResponse"`
|
||||
}
|
||||
|
||||
request := SetDPAddressesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
DPAddress: dpAddress,
|
||||
}
|
||||
var response SetDPAddressesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetDPAddresses failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAccessPolicy retrieves access policy information. ONVIF Specification: GetAccessPolicy operation.
|
||||
func (c *Client) GetAccessPolicy(ctx context.Context) (*AccessPolicy, error) {
|
||||
type GetAccessPolicyBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetAccessPolicy"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetAccessPolicyResponse struct {
|
||||
XMLName xml.Name `xml:"GetAccessPolicyResponse"`
|
||||
PolicyFile *BinaryData `xml:"PolicyFile"`
|
||||
}
|
||||
|
||||
request := GetAccessPolicyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetAccessPolicyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetAccessPolicy failed: %w", err)
|
||||
}
|
||||
|
||||
return &AccessPolicy{PolicyFile: response.PolicyFile}, nil
|
||||
}
|
||||
|
||||
// SetAccessPolicy sets access policy information. ONVIF Specification: SetAccessPolicy operation.
|
||||
func (c *Client) SetAccessPolicy(ctx context.Context, policy *AccessPolicy) error {
|
||||
type SetAccessPolicyBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetAccessPolicy"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
PolicyFile *BinaryData `xml:"tds:PolicyFile"`
|
||||
}
|
||||
|
||||
type SetAccessPolicyResponse struct {
|
||||
XMLName xml.Name `xml:"SetAccessPolicyResponse"`
|
||||
}
|
||||
|
||||
request := SetAccessPolicyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
PolicyFile: policy.PolicyFile,
|
||||
}
|
||||
var response SetAccessPolicyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetAccessPolicy failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWsdlURL retrieves the WSDL URL (deprecated). ONVIF Specification: GetWsdlUrl operation.
|
||||
func (c *Client) GetWsdlURL(ctx context.Context) (string, error) {
|
||||
type GetWsdlURLBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetWsdlUrl"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetWsdlURLResponse struct {
|
||||
XMLName xml.Name `xml:"GetWsdlUrlResponse"`
|
||||
WsdlURL string `xml:"WsdlUrl"`
|
||||
}
|
||||
|
||||
request := GetWsdlURLBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetWsdlURLResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return "", fmt.Errorf("GetWsdlURL failed: %w", err)
|
||||
}
|
||||
|
||||
return response.WsdlURL, nil
|
||||
}
|
||||
@@ -1,229 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetGeoLocation retrieves geographic location information. ONVIF Specification: GetGeoLocation operation.
|
||||
func (c *Client) GetGeoLocation(ctx context.Context) ([]LocationEntity, error) {
|
||||
type GetGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"GetGeoLocationResponse"`
|
||||
Location []LocationEntity `xml:"Location"`
|
||||
}
|
||||
|
||||
request := GetGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Location, nil
|
||||
}
|
||||
|
||||
// SetGeoLocation sets geographic location information. ONVIF Specification: SetGeoLocation operation.
|
||||
func (c *Client) SetGeoLocation(ctx context.Context, location []LocationEntity) error {
|
||||
type SetGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Location []LocationEntity `xml:"tds:Location"`
|
||||
}
|
||||
|
||||
type SetGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"SetGeoLocationResponse"`
|
||||
}
|
||||
|
||||
request := SetGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Location: location,
|
||||
}
|
||||
var response SetGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteGeoLocation deletes geographic location information. ONVIF Specification: DeleteGeoLocation operation.
|
||||
func (c *Client) DeleteGeoLocation(ctx context.Context, location []LocationEntity) error {
|
||||
type DeleteGeoLocationBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteGeoLocation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Location []LocationEntity `xml:"tds:Location"`
|
||||
}
|
||||
|
||||
type DeleteGeoLocationResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteGeoLocationResponse"`
|
||||
}
|
||||
|
||||
request := DeleteGeoLocationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Location: location,
|
||||
}
|
||||
var response DeleteGeoLocationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteGeoLocation failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDPAddresses retrieves DP (Device Provisioning) addresses. ONVIF Specification: GetDPAddresses operation.
|
||||
func (c *Client) GetDPAddresses(ctx context.Context) ([]NetworkHost, error) {
|
||||
type GetDPAddressesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetDPAddresses"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetDPAddressesResponse struct {
|
||||
XMLName xml.Name `xml:"GetDPAddressesResponse"`
|
||||
DPAddress []NetworkHost `xml:"DPAddress"`
|
||||
}
|
||||
|
||||
request := GetDPAddressesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetDPAddressesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetDPAddresses failed: %w", err)
|
||||
}
|
||||
|
||||
return response.DPAddress, nil
|
||||
}
|
||||
|
||||
// SetDPAddresses sets DP (Device Provisioning) addresses. ONVIF Specification: SetDPAddresses operation.
|
||||
func (c *Client) SetDPAddresses(ctx context.Context, dpAddress []NetworkHost) error {
|
||||
type SetDPAddressesBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetDPAddresses"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
DPAddress []NetworkHost `xml:"tds:DPAddress"`
|
||||
}
|
||||
|
||||
type SetDPAddressesResponse struct {
|
||||
XMLName xml.Name `xml:"SetDPAddressesResponse"`
|
||||
}
|
||||
|
||||
request := SetDPAddressesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
DPAddress: dpAddress,
|
||||
}
|
||||
var response SetDPAddressesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetDPAddresses failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAccessPolicy retrieves access policy information. ONVIF Specification: GetAccessPolicy operation.
|
||||
func (c *Client) GetAccessPolicy(ctx context.Context) (*AccessPolicy, error) {
|
||||
type GetAccessPolicyBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetAccessPolicy"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetAccessPolicyResponse struct {
|
||||
XMLName xml.Name `xml:"GetAccessPolicyResponse"`
|
||||
PolicyFile *BinaryData `xml:"PolicyFile"`
|
||||
}
|
||||
|
||||
request := GetAccessPolicyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetAccessPolicyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetAccessPolicy failed: %w", err)
|
||||
}
|
||||
|
||||
return &AccessPolicy{PolicyFile: response.PolicyFile}, nil
|
||||
}
|
||||
|
||||
// SetAccessPolicy sets access policy information. ONVIF Specification: SetAccessPolicy operation.
|
||||
func (c *Client) SetAccessPolicy(ctx context.Context, policy *AccessPolicy) error {
|
||||
type SetAccessPolicyBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetAccessPolicy"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
PolicyFile *BinaryData `xml:"tds:PolicyFile"`
|
||||
}
|
||||
|
||||
type SetAccessPolicyResponse struct {
|
||||
XMLName xml.Name `xml:"SetAccessPolicyResponse"`
|
||||
}
|
||||
|
||||
request := SetAccessPolicyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
PolicyFile: policy.PolicyFile,
|
||||
}
|
||||
var response SetAccessPolicyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetAccessPolicy failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWsdlURL retrieves the WSDL URL (deprecated). ONVIF Specification: GetWsdlUrl operation.
|
||||
func (c *Client) GetWsdlURL(ctx context.Context) (string, error) {
|
||||
type GetWsdlURLBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetWsdlUrl"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetWsdlURLResponse struct {
|
||||
XMLName xml.Name `xml:"GetWsdlUrlResponse"`
|
||||
WsdlURL string `xml:"WsdlUrl"`
|
||||
}
|
||||
|
||||
request := GetWsdlURLBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetWsdlURLResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return "", fmt.Errorf("GetWsdlURL failed: %w", err)
|
||||
}
|
||||
|
||||
return response.WsdlURL, nil
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceAdditionalServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "GetGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:tt="http://www.onvif.org/ver10/schema">
|
||||
<s:Body>
|
||||
<tds:GetGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Location Lon="-122.4194" Lat="37.7749" Elevation="10.5">
|
||||
<tt:Entity>Building A</tt:Entity>
|
||||
<tt:Token>location1</tt:Token>
|
||||
<tt:Fixed>true</tt:Fixed>
|
||||
</tds:Location>
|
||||
</tds:GetGeoLocationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "DeleteGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:DeleteGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetDPAddresses"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetDPAddressesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:DPAddress>
|
||||
<tt:Type>IPv4</tt:Type>
|
||||
<tt:IPv4Address>239.255.255.250</tt:IPv4Address>
|
||||
</tds:DPAddress>
|
||||
<tds:DPAddress>
|
||||
<tt:Type>IPv6</tt:Type>
|
||||
<tt:IPv6Address>ff02::c</tt:IPv6Address>
|
||||
</tds:DPAddress>
|
||||
</tds:GetDPAddressesResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetDPAddresses"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetDPAddressesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetAccessPolicy"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetAccessPolicyResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:PolicyFile>
|
||||
<tt:Data>cG9saWN5IGRhdGE=</tt:Data>
|
||||
<tt:ContentType>application/xml</tt:ContentType>
|
||||
</tds:PolicyFile>
|
||||
</tds:GetAccessPolicyResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetAccessPolicy"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetAccessPolicyResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetWsdlUrl"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetWsdlUrlResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:WsdlUrl>http://192.168.1.100/onvif/device.wsdl</tds:WsdlUrl>
|
||||
</tds:GetWsdlUrlResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations, err := client.GetGeoLocation(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetGeoLocation failed: %v", err)
|
||||
}
|
||||
|
||||
if len(locations) != 1 {
|
||||
t.Fatalf("Expected 1 location, got %d", len(locations))
|
||||
}
|
||||
|
||||
loc := locations[0]
|
||||
if loc.Entity != "Building A" {
|
||||
t.Errorf("Expected entity 'Building A', got %s", loc.Entity)
|
||||
}
|
||||
|
||||
if loc.Token != "location1" {
|
||||
t.Errorf("Expected token 'location1', got %s", loc.Token)
|
||||
}
|
||||
|
||||
if !loc.Fixed {
|
||||
t.Error("Expected Fixed to be true")
|
||||
}
|
||||
|
||||
// Check coordinates (approximate comparison due to float precision)
|
||||
if loc.Lon < -122.42 || loc.Lon > -122.41 {
|
||||
t.Errorf("Expected longitude around -122.4194, got %f", loc.Lon)
|
||||
}
|
||||
|
||||
if loc.Lat < 37.77 || loc.Lat > 37.78 {
|
||||
t.Errorf("Expected latitude around 37.7749, got %f", loc.Lat)
|
||||
}
|
||||
|
||||
if loc.Elevation < 10.0 || loc.Elevation > 11.0 {
|
||||
t.Errorf("Expected elevation around 10.5, got %f", loc.Elevation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations := []LocationEntity{
|
||||
{
|
||||
Entity: "Main Office",
|
||||
Token: "loc1",
|
||||
Fixed: true,
|
||||
Lon: -122.4194,
|
||||
Lat: 37.7749,
|
||||
Elevation: 15.0,
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetGeoLocation(ctx, locations)
|
||||
if err != nil {
|
||||
t.Fatalf("SetGeoLocation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations := []LocationEntity{
|
||||
{Token: "location1"},
|
||||
}
|
||||
|
||||
err = client.DeleteGeoLocation(ctx, locations)
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteGeoLocation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDPAddresses(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
addresses, err := client.GetDPAddresses(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDPAddresses failed: %v", err)
|
||||
}
|
||||
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("Expected 2 addresses, got %d", len(addresses))
|
||||
}
|
||||
|
||||
// Check IPv4 address
|
||||
if addresses[0].Type != "IPv4" {
|
||||
t.Errorf("Expected Type 'IPv4', got %s", addresses[0].Type)
|
||||
}
|
||||
if addresses[0].IPv4Address != "239.255.255.250" {
|
||||
t.Errorf("Expected IPv4 address '239.255.255.250', got %s", addresses[0].IPv4Address)
|
||||
}
|
||||
|
||||
// Check IPv6 address
|
||||
if addresses[1].Type != "IPv6" {
|
||||
t.Errorf("Expected Type 'IPv6', got %s", addresses[1].Type)
|
||||
}
|
||||
if addresses[1].IPv6Address != "ff02::c" {
|
||||
t.Errorf("Expected IPv6 address 'ff02::c', got %s", addresses[1].IPv6Address)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDPAddresses(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
addresses := []NetworkHost{
|
||||
{
|
||||
Type: "IPv4",
|
||||
IPv4Address: "239.255.255.250",
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetDPAddresses(ctx, addresses)
|
||||
if err != nil {
|
||||
t.Fatalf("SetDPAddresses failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccessPolicy(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
policy, err := client.GetAccessPolicy(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccessPolicy failed: %v", err)
|
||||
}
|
||||
|
||||
if policy == nil || policy.PolicyFile == nil {
|
||||
t.Fatal("Expected policy file, got nil")
|
||||
}
|
||||
|
||||
if policy.PolicyFile.ContentType != "application/xml" {
|
||||
t.Errorf("Expected content type 'application/xml', got %s", policy.PolicyFile.ContentType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAccessPolicy(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
policy := &AccessPolicy{
|
||||
PolicyFile: &BinaryData{
|
||||
Data: []byte("policy data"),
|
||||
ContentType: "application/xml",
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetAccessPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
t.Fatalf("SetAccessPolicy failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWsdlUrl(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
url, err := client.GetWsdlURL(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetWsdlURL failed: %v", err)
|
||||
}
|
||||
|
||||
expected := "http://192.168.1.100/onvif/device.wsdl"
|
||||
if url != expected {
|
||||
t.Errorf("Expected URL %s, got %s", expected, url)
|
||||
}
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceAdditionalServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "GetGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:tt="http://www.onvif.org/ver10/schema">
|
||||
<s:Body>
|
||||
<tds:GetGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Location Lon="-122.4194" Lat="37.7749" Elevation="10.5">
|
||||
<tt:Entity>Building A</tt:Entity>
|
||||
<tt:Token>location1</tt:Token>
|
||||
<tt:Fixed>true</tt:Fixed>
|
||||
</tds:Location>
|
||||
</tds:GetGeoLocationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "DeleteGeoLocation"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:DeleteGeoLocationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetDPAddresses"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetDPAddressesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:DPAddress>
|
||||
<tt:Type>IPv4</tt:Type>
|
||||
<tt:IPv4Address>239.255.255.250</tt:IPv4Address>
|
||||
</tds:DPAddress>
|
||||
<tds:DPAddress>
|
||||
<tt:Type>IPv6</tt:Type>
|
||||
<tt:IPv6Address>ff02::c</tt:IPv6Address>
|
||||
</tds:DPAddress>
|
||||
</tds:GetDPAddressesResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetDPAddresses"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetDPAddressesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetAccessPolicy"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetAccessPolicyResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:PolicyFile>
|
||||
<tt:Data>cG9saWN5IGRhdGE=</tt:Data>
|
||||
<tt:ContentType>application/xml</tt:ContentType>
|
||||
</tds:PolicyFile>
|
||||
</tds:GetAccessPolicyResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetAccessPolicy"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetAccessPolicyResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetWsdlUrl"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetWsdlUrlResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:WsdlUrl>http://192.168.1.100/onvif/device.wsdl</tds:WsdlUrl>
|
||||
</tds:GetWsdlUrlResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations, err := client.GetGeoLocation(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetGeoLocation failed: %v", err)
|
||||
}
|
||||
|
||||
if len(locations) != 1 {
|
||||
t.Fatalf("Expected 1 location, got %d", len(locations))
|
||||
}
|
||||
|
||||
loc := locations[0]
|
||||
if loc.Entity != "Building A" {
|
||||
t.Errorf("Expected entity 'Building A', got %s", loc.Entity)
|
||||
}
|
||||
|
||||
if loc.Token != "location1" {
|
||||
t.Errorf("Expected token 'location1', got %s", loc.Token)
|
||||
}
|
||||
|
||||
if !loc.Fixed {
|
||||
t.Error("Expected Fixed to be true")
|
||||
}
|
||||
|
||||
// Check coordinates (approximate comparison due to float precision)
|
||||
if loc.Lon < -122.42 || loc.Lon > -122.41 {
|
||||
t.Errorf("Expected longitude around -122.4194, got %f", loc.Lon)
|
||||
}
|
||||
|
||||
if loc.Lat < 37.77 || loc.Lat > 37.78 {
|
||||
t.Errorf("Expected latitude around 37.7749, got %f", loc.Lat)
|
||||
}
|
||||
|
||||
if loc.Elevation < 10.0 || loc.Elevation > 11.0 {
|
||||
t.Errorf("Expected elevation around 10.5, got %f", loc.Elevation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations := []LocationEntity{
|
||||
{
|
||||
Entity: "Main Office",
|
||||
Token: "loc1",
|
||||
Fixed: true,
|
||||
Lon: -122.4194,
|
||||
Lat: 37.7749,
|
||||
Elevation: 15.0,
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetGeoLocation(ctx, locations)
|
||||
if err != nil {
|
||||
t.Fatalf("SetGeoLocation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteGeoLocation(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
locations := []LocationEntity{
|
||||
{Token: "location1"},
|
||||
}
|
||||
|
||||
err = client.DeleteGeoLocation(ctx, locations)
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteGeoLocation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDPAddresses(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
addresses, err := client.GetDPAddresses(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDPAddresses failed: %v", err)
|
||||
}
|
||||
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("Expected 2 addresses, got %d", len(addresses))
|
||||
}
|
||||
|
||||
// Check IPv4 address
|
||||
if addresses[0].Type != "IPv4" {
|
||||
t.Errorf("Expected Type 'IPv4', got %s", addresses[0].Type)
|
||||
}
|
||||
if addresses[0].IPv4Address != "239.255.255.250" {
|
||||
t.Errorf("Expected IPv4 address '239.255.255.250', got %s", addresses[0].IPv4Address)
|
||||
}
|
||||
|
||||
// Check IPv6 address
|
||||
if addresses[1].Type != "IPv6" {
|
||||
t.Errorf("Expected Type 'IPv6', got %s", addresses[1].Type)
|
||||
}
|
||||
if addresses[1].IPv6Address != "ff02::c" {
|
||||
t.Errorf("Expected IPv6 address 'ff02::c', got %s", addresses[1].IPv6Address)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDPAddresses(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
addresses := []NetworkHost{
|
||||
{
|
||||
Type: "IPv4",
|
||||
IPv4Address: "239.255.255.250",
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetDPAddresses(ctx, addresses)
|
||||
if err != nil {
|
||||
t.Fatalf("SetDPAddresses failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccessPolicy(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
policy, err := client.GetAccessPolicy(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccessPolicy failed: %v", err)
|
||||
}
|
||||
|
||||
if policy == nil || policy.PolicyFile == nil {
|
||||
t.Fatal("Expected policy file, got nil")
|
||||
}
|
||||
|
||||
if policy.PolicyFile.ContentType != "application/xml" {
|
||||
t.Errorf("Expected content type 'application/xml', got %s", policy.PolicyFile.ContentType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAccessPolicy(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
policy := &AccessPolicy{
|
||||
PolicyFile: &BinaryData{
|
||||
Data: []byte("policy data"),
|
||||
ContentType: "application/xml",
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetAccessPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
t.Fatalf("SetAccessPolicy failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWsdlUrl(t *testing.T) {
|
||||
server := newMockDeviceAdditionalServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
url, err := client.GetWsdlURL(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetWsdlURL failed: %v", err)
|
||||
}
|
||||
|
||||
expected := "http://192.168.1.100/onvif/device.wsdl"
|
||||
if url != expected {
|
||||
t.Errorf("Expected URL %s, got %s", expected, url)
|
||||
}
|
||||
}
|
||||
@@ -1,417 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetCertificates retrieves certificates. ONVIF Specification: GetCertificates operation.
|
||||
func (c *Client) GetCertificates(ctx context.Context) ([]*Certificate, error) {
|
||||
type GetCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificatesResponse"`
|
||||
Certificates []*Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := GetCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificates, nil
|
||||
}
|
||||
|
||||
// GetCACertificates retrieves CA certificates. ONVIF Specification: GetCACertificates operation.
|
||||
func (c *Client) GetCACertificates(ctx context.Context) ([]*Certificate, error) {
|
||||
type GetCACertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCACertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCACertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"GetCACertificatesResponse"`
|
||||
Certificates []*Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := GetCACertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCACertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCACertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificates, nil
|
||||
}
|
||||
|
||||
// LoadCertificates loads certificates. ONVIF Specification: LoadCertificates operation.
|
||||
func (c *Client) LoadCertificates(ctx context.Context, certificates []*Certificate) error {
|
||||
type LoadCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Certificate []*Certificate `xml:"tds:Certificate"`
|
||||
}
|
||||
|
||||
type LoadCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCertificatesResponse"`
|
||||
}
|
||||
|
||||
request := LoadCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Certificate: certificates,
|
||||
}
|
||||
var response LoadCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadCACertificates loads CA certificates. ONVIF Specification: LoadCACertificates operation.
|
||||
func (c *Client) LoadCACertificates(ctx context.Context, certificates []*Certificate) error {
|
||||
type LoadCACertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCACertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Certificate []*Certificate `xml:"tds:Certificate"`
|
||||
}
|
||||
|
||||
type LoadCACertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCACertificatesResponse"`
|
||||
}
|
||||
|
||||
request := LoadCACertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Certificate: certificates,
|
||||
}
|
||||
var response LoadCACertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCACertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateCertificate creates a certificate. ONVIF Specification: CreateCertificate operation.
|
||||
func (c *Client) CreateCertificate(
|
||||
ctx context.Context,
|
||||
certificateID, subject, validNotBefore, validNotAfter string,
|
||||
) (*Certificate, error) {
|
||||
type CreateCertificateBody struct {
|
||||
XMLName xml.Name `xml:"tds:CreateCertificate"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID,omitempty"`
|
||||
Subject string `xml:"tds:Subject"`
|
||||
ValidNotBefore string `xml:"tds:ValidNotBefore"`
|
||||
ValidNotAfter string `xml:"tds:ValidNotAfter"`
|
||||
}
|
||||
|
||||
type CreateCertificateResponse struct {
|
||||
XMLName xml.Name `xml:"CreateCertificateResponse"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := CreateCertificateBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
Subject: subject,
|
||||
ValidNotBefore: validNotBefore,
|
||||
ValidNotAfter: validNotAfter,
|
||||
}
|
||||
var response CreateCertificateResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("CreateCertificate failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificate, nil
|
||||
}
|
||||
|
||||
// DeleteCertificates deletes certificates. ONVIF Specification: DeleteCertificates operation.
|
||||
func (c *Client) DeleteCertificates(ctx context.Context, certificateIDs []string) error {
|
||||
type DeleteCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID []string `xml:"tds:CertificateID"`
|
||||
}
|
||||
|
||||
type DeleteCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteCertificatesResponse"`
|
||||
}
|
||||
|
||||
request := DeleteCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateIDs,
|
||||
}
|
||||
var response DeleteCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCertificateInformation retrieves certificate information.
|
||||
// ONVIF Specification: GetCertificateInformation operation.
|
||||
func (c *Client) GetCertificateInformation(ctx context.Context, certificateID string) (*CertificateInformation, error) {
|
||||
type GetCertificateInformationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificateInformation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID"`
|
||||
}
|
||||
|
||||
type GetCertificateInformationResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificateInformationResponse"`
|
||||
CertificateInformation *CertificateInformation `xml:"CertificateInformation"`
|
||||
}
|
||||
|
||||
request := GetCertificateInformationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
}
|
||||
var response GetCertificateInformationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificateInformation failed: %w", err)
|
||||
}
|
||||
|
||||
return response.CertificateInformation, nil
|
||||
}
|
||||
|
||||
// GetCertificatesStatus retrieves certificate status. ONVIF Specification: GetCertificatesStatus operation.
|
||||
func (c *Client) GetCertificatesStatus(ctx context.Context) ([]*CertificateStatus, error) {
|
||||
type GetCertificatesStatusBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificatesStatus"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCertificatesStatusResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificatesStatusResponse"`
|
||||
CertificateStatus []*CertificateStatus `xml:"CertificateStatus"`
|
||||
}
|
||||
|
||||
request := GetCertificatesStatusBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCertificatesStatusResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificatesStatus failed: %w", err)
|
||||
}
|
||||
|
||||
return response.CertificateStatus, nil
|
||||
}
|
||||
|
||||
// SetCertificatesStatus sets certificate status. ONVIF Specification: SetCertificatesStatus operation.
|
||||
func (c *Client) SetCertificatesStatus(ctx context.Context, statuses []*CertificateStatus) error {
|
||||
type SetCertificatesStatusBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetCertificatesStatus"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateStatus []*CertificateStatus `xml:"tds:CertificateStatus"`
|
||||
}
|
||||
|
||||
type SetCertificatesStatusResponse struct {
|
||||
XMLName xml.Name `xml:"SetCertificatesStatusResponse"`
|
||||
}
|
||||
|
||||
request := SetCertificatesStatusBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateStatus: statuses,
|
||||
}
|
||||
var response SetCertificatesStatusResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetCertificatesStatus failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPkcs10Request retrieves a PKCS10 certificate request. ONVIF Specification: GetPkcs10Request operation.
|
||||
func (c *Client) GetPkcs10Request(
|
||||
ctx context.Context,
|
||||
certificateID, subject string,
|
||||
attributes *BinaryData,
|
||||
) (*BinaryData, error) {
|
||||
type GetPkcs10RequestBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetPkcs10Request"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID,omitempty"`
|
||||
Subject string `xml:"tds:Subject"`
|
||||
Attributes *BinaryData `xml:"tds:Attributes,omitempty"`
|
||||
}
|
||||
|
||||
type GetPkcs10RequestResponse struct {
|
||||
XMLName xml.Name `xml:"GetPkcs10RequestResponse"`
|
||||
Pkcs10Request *BinaryData `xml:"Pkcs10Request"`
|
||||
}
|
||||
|
||||
request := GetPkcs10RequestBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
Subject: subject,
|
||||
Attributes: attributes,
|
||||
}
|
||||
var response GetPkcs10RequestResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetPkcs10Request failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Pkcs10Request, nil
|
||||
}
|
||||
|
||||
// LoadCertificateWithPrivateKey loads a certificate with its private key.
|
||||
// ONVIF Specification: LoadCertificateWithPrivateKey operation.
|
||||
func (c *Client) LoadCertificateWithPrivateKey(
|
||||
ctx context.Context,
|
||||
certificates []*Certificate,
|
||||
privateKey []*BinaryData,
|
||||
certificateIDs []string,
|
||||
) error {
|
||||
type LoadCertificateWithPrivateKeyBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCertificateWithPrivateKey"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateWithPrivateKey []struct {
|
||||
CertificateID string `xml:"CertificateID"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
PrivateKey *BinaryData `xml:"PrivateKey"`
|
||||
} `xml:"tds:CertificateWithPrivateKey"`
|
||||
}
|
||||
|
||||
type LoadCertificateWithPrivateKeyResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCertificateWithPrivateKeyResponse"`
|
||||
}
|
||||
|
||||
request := LoadCertificateWithPrivateKeyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
// Build certificate with private key array
|
||||
for i := 0; i < len(certificates); i++ {
|
||||
item := struct {
|
||||
CertificateID string `xml:"CertificateID"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
PrivateKey *BinaryData `xml:"PrivateKey"`
|
||||
}{
|
||||
CertificateID: certificateIDs[i],
|
||||
Certificate: certificates[i],
|
||||
}
|
||||
if i < len(privateKey) {
|
||||
item.PrivateKey = privateKey[i]
|
||||
}
|
||||
request.CertificateWithPrivateKey = append(request.CertificateWithPrivateKey, item)
|
||||
}
|
||||
|
||||
var response LoadCertificateWithPrivateKeyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCertificateWithPrivateKey failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClientCertificateMode retrieves the client certificate mode.
|
||||
// ONVIF Specification: GetClientCertificateMode operation.
|
||||
func (c *Client) GetClientCertificateMode(ctx context.Context) (bool, error) {
|
||||
type GetClientCertificateModeBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetClientCertificateMode"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetClientCertificateModeResponse struct {
|
||||
XMLName xml.Name `xml:"GetClientCertificateModeResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
}
|
||||
|
||||
request := GetClientCertificateModeBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetClientCertificateModeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return false, fmt.Errorf("GetClientCertificateMode failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Enabled, nil
|
||||
}
|
||||
|
||||
// SetClientCertificateMode sets the client certificate mode. ONVIF Specification: SetClientCertificateMode operation.
|
||||
func (c *Client) SetClientCertificateMode(ctx context.Context, enabled bool) error {
|
||||
type SetClientCertificateModeBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetClientCertificateMode"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
}
|
||||
|
||||
type SetClientCertificateModeResponse struct {
|
||||
XMLName xml.Name `xml:"SetClientCertificateModeResponse"`
|
||||
}
|
||||
|
||||
request := SetClientCertificateModeBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: enabled,
|
||||
}
|
||||
var response SetClientCertificateModeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetClientCertificateMode failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,417 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetCertificates retrieves certificates. ONVIF Specification: GetCertificates operation.
|
||||
func (c *Client) GetCertificates(ctx context.Context) ([]*Certificate, error) {
|
||||
type GetCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificatesResponse"`
|
||||
Certificates []*Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := GetCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificates, nil
|
||||
}
|
||||
|
||||
// GetCACertificates retrieves CA certificates. ONVIF Specification: GetCACertificates operation.
|
||||
func (c *Client) GetCACertificates(ctx context.Context) ([]*Certificate, error) {
|
||||
type GetCACertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCACertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCACertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"GetCACertificatesResponse"`
|
||||
Certificates []*Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := GetCACertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCACertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCACertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificates, nil
|
||||
}
|
||||
|
||||
// LoadCertificates loads certificates. ONVIF Specification: LoadCertificates operation.
|
||||
func (c *Client) LoadCertificates(ctx context.Context, certificates []*Certificate) error {
|
||||
type LoadCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Certificate []*Certificate `xml:"tds:Certificate"`
|
||||
}
|
||||
|
||||
type LoadCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCertificatesResponse"`
|
||||
}
|
||||
|
||||
request := LoadCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Certificate: certificates,
|
||||
}
|
||||
var response LoadCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadCACertificates loads CA certificates. ONVIF Specification: LoadCACertificates operation.
|
||||
func (c *Client) LoadCACertificates(ctx context.Context, certificates []*Certificate) error {
|
||||
type LoadCACertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCACertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Certificate []*Certificate `xml:"tds:Certificate"`
|
||||
}
|
||||
|
||||
type LoadCACertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCACertificatesResponse"`
|
||||
}
|
||||
|
||||
request := LoadCACertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Certificate: certificates,
|
||||
}
|
||||
var response LoadCACertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCACertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateCertificate creates a certificate. ONVIF Specification: CreateCertificate operation.
|
||||
func (c *Client) CreateCertificate(
|
||||
ctx context.Context,
|
||||
certificateID, subject, validNotBefore, validNotAfter string,
|
||||
) (*Certificate, error) {
|
||||
type CreateCertificateBody struct {
|
||||
XMLName xml.Name `xml:"tds:CreateCertificate"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID,omitempty"`
|
||||
Subject string `xml:"tds:Subject"`
|
||||
ValidNotBefore string `xml:"tds:ValidNotBefore"`
|
||||
ValidNotAfter string `xml:"tds:ValidNotAfter"`
|
||||
}
|
||||
|
||||
type CreateCertificateResponse struct {
|
||||
XMLName xml.Name `xml:"CreateCertificateResponse"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
}
|
||||
|
||||
request := CreateCertificateBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
Subject: subject,
|
||||
ValidNotBefore: validNotBefore,
|
||||
ValidNotAfter: validNotAfter,
|
||||
}
|
||||
var response CreateCertificateResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("CreateCertificate failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Certificate, nil
|
||||
}
|
||||
|
||||
// DeleteCertificates deletes certificates. ONVIF Specification: DeleteCertificates operation.
|
||||
func (c *Client) DeleteCertificates(ctx context.Context, certificateIDs []string) error {
|
||||
type DeleteCertificatesBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteCertificates"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID []string `xml:"tds:CertificateID"`
|
||||
}
|
||||
|
||||
type DeleteCertificatesResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteCertificatesResponse"`
|
||||
}
|
||||
|
||||
request := DeleteCertificatesBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateIDs,
|
||||
}
|
||||
var response DeleteCertificatesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteCertificates failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCertificateInformation retrieves certificate information.
|
||||
// ONVIF Specification: GetCertificateInformation operation.
|
||||
func (c *Client) GetCertificateInformation(ctx context.Context, certificateID string) (*CertificateInformation, error) {
|
||||
type GetCertificateInformationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificateInformation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID"`
|
||||
}
|
||||
|
||||
type GetCertificateInformationResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificateInformationResponse"`
|
||||
CertificateInformation *CertificateInformation `xml:"CertificateInformation"`
|
||||
}
|
||||
|
||||
request := GetCertificateInformationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
}
|
||||
var response GetCertificateInformationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificateInformation failed: %w", err)
|
||||
}
|
||||
|
||||
return response.CertificateInformation, nil
|
||||
}
|
||||
|
||||
// GetCertificatesStatus retrieves certificate status. ONVIF Specification: GetCertificatesStatus operation.
|
||||
func (c *Client) GetCertificatesStatus(ctx context.Context) ([]*CertificateStatus, error) {
|
||||
type GetCertificatesStatusBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetCertificatesStatus"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetCertificatesStatusResponse struct {
|
||||
XMLName xml.Name `xml:"GetCertificatesStatusResponse"`
|
||||
CertificateStatus []*CertificateStatus `xml:"CertificateStatus"`
|
||||
}
|
||||
|
||||
request := GetCertificatesStatusBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetCertificatesStatusResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetCertificatesStatus failed: %w", err)
|
||||
}
|
||||
|
||||
return response.CertificateStatus, nil
|
||||
}
|
||||
|
||||
// SetCertificatesStatus sets certificate status. ONVIF Specification: SetCertificatesStatus operation.
|
||||
func (c *Client) SetCertificatesStatus(ctx context.Context, statuses []*CertificateStatus) error {
|
||||
type SetCertificatesStatusBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetCertificatesStatus"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateStatus []*CertificateStatus `xml:"tds:CertificateStatus"`
|
||||
}
|
||||
|
||||
type SetCertificatesStatusResponse struct {
|
||||
XMLName xml.Name `xml:"SetCertificatesStatusResponse"`
|
||||
}
|
||||
|
||||
request := SetCertificatesStatusBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateStatus: statuses,
|
||||
}
|
||||
var response SetCertificatesStatusResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetCertificatesStatus failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPkcs10Request retrieves a PKCS10 certificate request. ONVIF Specification: GetPkcs10Request operation.
|
||||
func (c *Client) GetPkcs10Request(
|
||||
ctx context.Context,
|
||||
certificateID, subject string,
|
||||
attributes *BinaryData,
|
||||
) (*BinaryData, error) {
|
||||
type GetPkcs10RequestBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetPkcs10Request"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateID string `xml:"tds:CertificateID,omitempty"`
|
||||
Subject string `xml:"tds:Subject"`
|
||||
Attributes *BinaryData `xml:"tds:Attributes,omitempty"`
|
||||
}
|
||||
|
||||
type GetPkcs10RequestResponse struct {
|
||||
XMLName xml.Name `xml:"GetPkcs10RequestResponse"`
|
||||
Pkcs10Request *BinaryData `xml:"Pkcs10Request"`
|
||||
}
|
||||
|
||||
request := GetPkcs10RequestBody{
|
||||
Xmlns: deviceNamespace,
|
||||
CertificateID: certificateID,
|
||||
Subject: subject,
|
||||
Attributes: attributes,
|
||||
}
|
||||
var response GetPkcs10RequestResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetPkcs10Request failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Pkcs10Request, nil
|
||||
}
|
||||
|
||||
// LoadCertificateWithPrivateKey loads a certificate with its private key.
|
||||
// ONVIF Specification: LoadCertificateWithPrivateKey operation.
|
||||
func (c *Client) LoadCertificateWithPrivateKey(
|
||||
ctx context.Context,
|
||||
certificates []*Certificate,
|
||||
privateKey []*BinaryData,
|
||||
certificateIDs []string,
|
||||
) error {
|
||||
type LoadCertificateWithPrivateKeyBody struct {
|
||||
XMLName xml.Name `xml:"tds:LoadCertificateWithPrivateKey"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
CertificateWithPrivateKey []struct {
|
||||
CertificateID string `xml:"CertificateID"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
PrivateKey *BinaryData `xml:"PrivateKey"`
|
||||
} `xml:"tds:CertificateWithPrivateKey"`
|
||||
}
|
||||
|
||||
type LoadCertificateWithPrivateKeyResponse struct {
|
||||
XMLName xml.Name `xml:"LoadCertificateWithPrivateKeyResponse"`
|
||||
}
|
||||
|
||||
request := LoadCertificateWithPrivateKeyBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
// Build certificate with private key array
|
||||
for i := 0; i < len(certificates); i++ {
|
||||
item := struct {
|
||||
CertificateID string `xml:"CertificateID"`
|
||||
Certificate *Certificate `xml:"Certificate"`
|
||||
PrivateKey *BinaryData `xml:"PrivateKey"`
|
||||
}{
|
||||
CertificateID: certificateIDs[i],
|
||||
Certificate: certificates[i],
|
||||
}
|
||||
if i < len(privateKey) {
|
||||
item.PrivateKey = privateKey[i]
|
||||
}
|
||||
request.CertificateWithPrivateKey = append(request.CertificateWithPrivateKey, item)
|
||||
}
|
||||
|
||||
var response LoadCertificateWithPrivateKeyResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("LoadCertificateWithPrivateKey failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClientCertificateMode retrieves the client certificate mode.
|
||||
// ONVIF Specification: GetClientCertificateMode operation.
|
||||
func (c *Client) GetClientCertificateMode(ctx context.Context) (bool, error) {
|
||||
type GetClientCertificateModeBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetClientCertificateMode"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetClientCertificateModeResponse struct {
|
||||
XMLName xml.Name `xml:"GetClientCertificateModeResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
}
|
||||
|
||||
request := GetClientCertificateModeBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetClientCertificateModeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return false, fmt.Errorf("GetClientCertificateMode failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Enabled, nil
|
||||
}
|
||||
|
||||
// SetClientCertificateMode sets the client certificate mode. ONVIF Specification: SetClientCertificateMode operation.
|
||||
func (c *Client) SetClientCertificateMode(ctx context.Context, enabled bool) error {
|
||||
type SetClientCertificateModeBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetClientCertificateMode"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
}
|
||||
|
||||
type SetClientCertificateModeResponse struct {
|
||||
XMLName xml.Name `xml:"SetClientCertificateModeResponse"`
|
||||
}
|
||||
|
||||
request := SetClientCertificateModeBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: enabled,
|
||||
}
|
||||
var response SetClientCertificateModeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetClientCertificateMode failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,495 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
testCertID = "cert-001"
|
||||
testXMLHeader = `<?xml version="1.0" encoding="UTF-8"?>`
|
||||
)
|
||||
|
||||
func newMockDeviceCertificatesServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
// Parse request to determine which operation
|
||||
buf := make([]byte, r.ContentLength)
|
||||
_, _ = r.Body.Read(buf)
|
||||
requestBody := string(buf)
|
||||
|
||||
var response string
|
||||
|
||||
switch {
|
||||
case strings.Contains(requestBody, "GetCertificatesStatus"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificatesStatusResponse>
|
||||
<tds:CertificateStatus>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:Status>true</tt:Status>
|
||||
</tds:CertificateStatus>
|
||||
</tds:GetCertificatesStatusResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "SetCertificatesStatus"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:SetCertificatesStatusResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCertificateInformation"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificateInformationResponse>
|
||||
<tds:CertificateInformation>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:IssuerDN>CN=Test CA</tt:IssuerDN>
|
||||
<tt:SubjectDN>CN=Device Certificate</tt:SubjectDN>
|
||||
<tt:ValidNotBefore>2024-01-01T00:00:00Z</tt:ValidNotBefore>
|
||||
<tt:ValidNotAfter>2025-01-01T00:00:00Z</tt:ValidNotAfter>
|
||||
</tds:CertificateInformation>
|
||||
</tds:GetCertificateInformationResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCertificateWithPrivateKey"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCertificateWithPrivateKeyResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCACertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCACertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCACertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCACertificatesResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>ca-001</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("CA CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:GetCACertificatesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificatesResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:GetCertificatesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "CreateCertificate"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:CreateCertificateResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>cert-new</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("NEW CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:CreateCertificateResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "DeleteCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:DeleteCertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetPkcs10Request"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetPkcs10RequestResponse>
|
||||
<tds:Pkcs10Request>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("PKCS#10 CSR DATA")) + `</tt:Data>
|
||||
</tds:Pkcs10Request>
|
||||
</tds:GetPkcs10RequestResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetClientCertificateMode"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetClientCertificateModeResponse>
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
</tds:GetClientCertificateModeResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "SetClientCertificateMode"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:SetClientCertificateModeResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
default:
|
||||
response = testXMLHeader + `
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<SOAP-ENV:Fault>
|
||||
<SOAP-ENV:Code><SOAP-ENV:Value>SOAP-ENV:Receiver</SOAP-ENV:Value></SOAP-ENV:Code>
|
||||
<SOAP-ENV:Reason><SOAP-ENV:Text>Unknown operation</SOAP-ENV:Text></SOAP-ENV:Reason>
|
||||
</SOAP-ENV:Fault>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
}
|
||||
|
||||
_, _ = w.Write([]byte(response))
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs, err := client.GetCertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificates failed: %v", err)
|
||||
}
|
||||
|
||||
if len(certs) == 0 {
|
||||
t.Error("Expected at least one certificate")
|
||||
}
|
||||
|
||||
if certs[0].CertificateID != testCertID {
|
||||
t.Errorf("Expected certificate ID '%s', got '%s'", testCertID, certs[0].CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCACertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs, err := client.GetCACertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCACertificates failed: %v", err)
|
||||
}
|
||||
|
||||
if len(certs) == 0 {
|
||||
t.Error("Expected at least one CA certificate")
|
||||
}
|
||||
|
||||
if certs[0].CertificateID != "ca-001" {
|
||||
t.Errorf("Expected certificate ID 'ca-001', got '%s'", certs[0].CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "cert-upload",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("UPLOADED CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCertificates(ctx, certs)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCACertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "ca-upload",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("UPLOADED CA CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCACertificates(ctx, certs)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCACertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCertificate(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
cert, err := client.CreateCertificate(ctx, "cert-new", "CN=New Device", "2024-01-01T00:00:00Z", "2025-01-01T00:00:00Z")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if cert.CertificateID != "cert-new" {
|
||||
t.Errorf("Expected certificate ID 'cert-new', got '%s'", cert.CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
err = client.DeleteCertificates(ctx, []string{"cert-001", "cert-002"})
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteCertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCertificateInformation(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
info, err := client.GetCertificateInformation(ctx, "cert-001")
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificateInformation failed: %v", err)
|
||||
}
|
||||
|
||||
if info.CertificateID != "cert-001" {
|
||||
t.Errorf("Expected certificate ID 'cert-001', got '%s'", info.CertificateID)
|
||||
}
|
||||
|
||||
if info.IssuerDN != "CN=Test CA" {
|
||||
t.Errorf("Expected issuer 'CN=Test CA', got '%s'", info.IssuerDN)
|
||||
}
|
||||
|
||||
if info.SubjectDN != "CN=Device Certificate" {
|
||||
t.Errorf("Expected subject 'CN=Device Certificate', got '%s'", info.SubjectDN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCertificatesStatus(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
statuses, err := client.GetCertificatesStatus(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificatesStatus failed: %v", err)
|
||||
}
|
||||
|
||||
if len(statuses) == 0 {
|
||||
t.Error("Expected at least one certificate status")
|
||||
}
|
||||
|
||||
if statuses[0].CertificateID != "cert-001" {
|
||||
t.Errorf("Expected certificate ID 'cert-001', got '%s'", statuses[0].CertificateID)
|
||||
}
|
||||
|
||||
if !statuses[0].Status {
|
||||
t.Error("Expected certificate status to be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCertificatesStatus(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
statuses := []*CertificateStatus{
|
||||
{
|
||||
CertificateID: "cert-001",
|
||||
Status: true,
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetCertificatesStatus(ctx, statuses)
|
||||
if err != nil {
|
||||
t.Fatalf("SetCertificatesStatus failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPkcs10Request(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
csr, err := client.GetPkcs10Request(ctx, "cert-csr", "CN=Device CSR", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPkcs10Request failed: %v", err)
|
||||
}
|
||||
|
||||
if csr == nil || len(csr.Data) == 0 {
|
||||
t.Error("Expected non-empty PKCS#10 CSR data")
|
||||
}
|
||||
|
||||
// Check that data was decoded from base64
|
||||
expectedData := []byte("PKCS#10 CSR DATA")
|
||||
if len(csr.Data) > 0 && !bytes.Equal(csr.Data, expectedData) {
|
||||
t.Logf("CSR data length: %d, expected: %d", len(csr.Data), len(expectedData))
|
||||
t.Logf("CSR data: %q, expected: %q", string(csr.Data), string(expectedData))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCertificateWithPrivateKey(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "cert-with-key",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
privateKeys := []*BinaryData{
|
||||
{
|
||||
Data: []byte("PRIVATE KEY DATA"),
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCertificateWithPrivateKey(ctx, certs, privateKeys, []string{"cert-with-key"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCertificateWithPrivateKey failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientCertificateMode(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
enabled, err := client.GetClientCertificateMode(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetClientCertificateMode failed: %v", err)
|
||||
}
|
||||
|
||||
if !enabled {
|
||||
t.Error("Expected client certificate mode to be enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetClientCertificateMode(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
err = client.SetClientCertificateMode(ctx, true)
|
||||
if err != nil {
|
||||
t.Fatalf("SetClientCertificateMode failed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,495 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
testCertID = "cert-001"
|
||||
testXMLHeader = `<?xml version="1.0" encoding="UTF-8"?>`
|
||||
)
|
||||
|
||||
func newMockDeviceCertificatesServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
// Parse request to determine which operation
|
||||
buf := make([]byte, r.ContentLength)
|
||||
_, _ = r.Body.Read(buf)
|
||||
requestBody := string(buf)
|
||||
|
||||
var response string
|
||||
|
||||
switch {
|
||||
case strings.Contains(requestBody, "GetCertificatesStatus"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificatesStatusResponse>
|
||||
<tds:CertificateStatus>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:Status>true</tt:Status>
|
||||
</tds:CertificateStatus>
|
||||
</tds:GetCertificatesStatusResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "SetCertificatesStatus"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:SetCertificatesStatusResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCertificateInformation"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificateInformationResponse>
|
||||
<tds:CertificateInformation>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:IssuerDN>CN=Test CA</tt:IssuerDN>
|
||||
<tt:SubjectDN>CN=Device Certificate</tt:SubjectDN>
|
||||
<tt:ValidNotBefore>2024-01-01T00:00:00Z</tt:ValidNotBefore>
|
||||
<tt:ValidNotAfter>2025-01-01T00:00:00Z</tt:ValidNotAfter>
|
||||
</tds:CertificateInformation>
|
||||
</tds:GetCertificateInformationResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCertificateWithPrivateKey"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCertificateWithPrivateKeyResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCACertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCACertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "LoadCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:LoadCertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCACertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCACertificatesResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>ca-001</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("CA CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:GetCACertificatesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCertificatesResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>cert-001</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:GetCertificatesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "CreateCertificate"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:CreateCertificateResponse>
|
||||
<tds:Certificate>
|
||||
<tt:CertificateID>cert-new</tt:CertificateID>
|
||||
<tt:Certificate>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("NEW CERTIFICATE DATA")) + `</tt:Data>
|
||||
</tt:Certificate>
|
||||
</tds:Certificate>
|
||||
</tds:CreateCertificateResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "DeleteCertificates"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:DeleteCertificatesResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetPkcs10Request"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetPkcs10RequestResponse>
|
||||
<tds:Pkcs10Request>
|
||||
<tt:Data>` + base64.StdEncoding.EncodeToString([]byte("PKCS#10 CSR DATA")) + `</tt:Data>
|
||||
</tds:Pkcs10Request>
|
||||
</tds:GetPkcs10RequestResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "GetClientCertificateMode"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetClientCertificateModeResponse>
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
</tds:GetClientCertificateModeResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
case strings.Contains(requestBody, "SetClientCertificateMode"):
|
||||
response = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:SetClientCertificateModeResponse/>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
default:
|
||||
response = testXMLHeader + `
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<SOAP-ENV:Fault>
|
||||
<SOAP-ENV:Code><SOAP-ENV:Value>SOAP-ENV:Receiver</SOAP-ENV:Value></SOAP-ENV:Code>
|
||||
<SOAP-ENV:Reason><SOAP-ENV:Text>Unknown operation</SOAP-ENV:Text></SOAP-ENV:Reason>
|
||||
</SOAP-ENV:Fault>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
}
|
||||
|
||||
_, _ = w.Write([]byte(response))
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs, err := client.GetCertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificates failed: %v", err)
|
||||
}
|
||||
|
||||
if len(certs) == 0 {
|
||||
t.Error("Expected at least one certificate")
|
||||
}
|
||||
|
||||
if certs[0].CertificateID != testCertID {
|
||||
t.Errorf("Expected certificate ID '%s', got '%s'", testCertID, certs[0].CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCACertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs, err := client.GetCACertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCACertificates failed: %v", err)
|
||||
}
|
||||
|
||||
if len(certs) == 0 {
|
||||
t.Error("Expected at least one CA certificate")
|
||||
}
|
||||
|
||||
if certs[0].CertificateID != "ca-001" {
|
||||
t.Errorf("Expected certificate ID 'ca-001', got '%s'", certs[0].CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "cert-upload",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("UPLOADED CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCertificates(ctx, certs)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCACertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "ca-upload",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("UPLOADED CA CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCACertificates(ctx, certs)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCACertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCertificate(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
cert, err := client.CreateCertificate(ctx, "cert-new", "CN=New Device", "2024-01-01T00:00:00Z", "2025-01-01T00:00:00Z")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if cert.CertificateID != "cert-new" {
|
||||
t.Errorf("Expected certificate ID 'cert-new', got '%s'", cert.CertificateID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCertificates(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
err = client.DeleteCertificates(ctx, []string{"cert-001", "cert-002"})
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteCertificates failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCertificateInformation(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
info, err := client.GetCertificateInformation(ctx, "cert-001")
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificateInformation failed: %v", err)
|
||||
}
|
||||
|
||||
if info.CertificateID != "cert-001" {
|
||||
t.Errorf("Expected certificate ID 'cert-001', got '%s'", info.CertificateID)
|
||||
}
|
||||
|
||||
if info.IssuerDN != "CN=Test CA" {
|
||||
t.Errorf("Expected issuer 'CN=Test CA', got '%s'", info.IssuerDN)
|
||||
}
|
||||
|
||||
if info.SubjectDN != "CN=Device Certificate" {
|
||||
t.Errorf("Expected subject 'CN=Device Certificate', got '%s'", info.SubjectDN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCertificatesStatus(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
statuses, err := client.GetCertificatesStatus(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCertificatesStatus failed: %v", err)
|
||||
}
|
||||
|
||||
if len(statuses) == 0 {
|
||||
t.Error("Expected at least one certificate status")
|
||||
}
|
||||
|
||||
if statuses[0].CertificateID != "cert-001" {
|
||||
t.Errorf("Expected certificate ID 'cert-001', got '%s'", statuses[0].CertificateID)
|
||||
}
|
||||
|
||||
if !statuses[0].Status {
|
||||
t.Error("Expected certificate status to be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCertificatesStatus(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
statuses := []*CertificateStatus{
|
||||
{
|
||||
CertificateID: "cert-001",
|
||||
Status: true,
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetCertificatesStatus(ctx, statuses)
|
||||
if err != nil {
|
||||
t.Fatalf("SetCertificatesStatus failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPkcs10Request(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
csr, err := client.GetPkcs10Request(ctx, "cert-csr", "CN=Device CSR", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPkcs10Request failed: %v", err)
|
||||
}
|
||||
|
||||
if csr == nil || len(csr.Data) == 0 {
|
||||
t.Error("Expected non-empty PKCS#10 CSR data")
|
||||
}
|
||||
|
||||
// Check that data was decoded from base64
|
||||
expectedData := []byte("PKCS#10 CSR DATA")
|
||||
if len(csr.Data) > 0 && !bytes.Equal(csr.Data, expectedData) {
|
||||
t.Logf("CSR data length: %d, expected: %d", len(csr.Data), len(expectedData))
|
||||
t.Logf("CSR data: %q, expected: %q", string(csr.Data), string(expectedData))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCertificateWithPrivateKey(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
certs := []*Certificate{
|
||||
{
|
||||
CertificateID: "cert-with-key",
|
||||
Certificate: BinaryData{
|
||||
Data: []byte("CERTIFICATE DATA"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
privateKeys := []*BinaryData{
|
||||
{
|
||||
Data: []byte("PRIVATE KEY DATA"),
|
||||
},
|
||||
}
|
||||
|
||||
err = client.LoadCertificateWithPrivateKey(ctx, certs, privateKeys, []string{"cert-with-key"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCertificateWithPrivateKey failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientCertificateMode(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
enabled, err := client.GetClientCertificateMode(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetClientCertificateMode failed: %v", err)
|
||||
}
|
||||
|
||||
if !enabled {
|
||||
t.Error("Expected client certificate mode to be enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetClientCertificateMode(t *testing.T) {
|
||||
server := newMockDeviceCertificatesServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient failed: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
err = client.SetClientCertificateMode(ctx, true)
|
||||
if err != nil {
|
||||
t.Fatalf("SetClientCertificateMode failed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,796 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// SetDNS sets the DNS settings on a device.
|
||||
func (c *Client) SetDNS(ctx context.Context, fromDHCP bool, searchDomain []string, dnsManual []IPAddress) error {
|
||||
type SetDNS struct {
|
||||
XMLName xml.Name `xml:"tds:SetDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
SearchDomain []string `xml:"tds:SearchDomain,omitempty"`
|
||||
DNSManual []struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
} `xml:"tds:DNSManual,omitempty"`
|
||||
}
|
||||
|
||||
req := SetDNS{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
SearchDomain: searchDomain,
|
||||
}
|
||||
|
||||
for _, dns := range dnsManual {
|
||||
req.DNSManual = append(req.DNSManual, struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
}{
|
||||
Type: dns.Type,
|
||||
IPv4Address: dns.IPv4Address,
|
||||
IPv6Address: dns.IPv6Address,
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetNTP sets the NTP settings on a device.
|
||||
func (c *Client) SetNTP(ctx context.Context, fromDHCP bool, ntpManual []NetworkHost) error {
|
||||
type SetNTP struct {
|
||||
XMLName xml.Name `xml:"tds:SetNTP"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
NTPManual []struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
DNSname string `xml:"tds:DNSname,omitempty"`
|
||||
} `xml:"tds:NTPManual,omitempty"`
|
||||
}
|
||||
|
||||
req := SetNTP{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
}
|
||||
|
||||
for _, ntp := range ntpManual {
|
||||
req.NTPManual = append(req.NTPManual, struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
DNSname string `xml:"tds:DNSname,omitempty"`
|
||||
}{
|
||||
Type: ntp.Type,
|
||||
IPv4Address: ntp.IPv4Address,
|
||||
IPv6Address: ntp.IPv6Address,
|
||||
DNSname: ntp.DNSname,
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetNTP failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHostnameFromDHCP controls whether the hostname is set manually or retrieved via DHCP.
|
||||
func (c *Client) SetHostnameFromDHCP(ctx context.Context, fromDHCP bool) (bool, error) {
|
||||
type SetHostnameFromDHCP struct {
|
||||
XMLName xml.Name `xml:"tds:SetHostnameFromDHCP"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
}
|
||||
|
||||
type SetHostnameFromDHCPResponse struct {
|
||||
XMLName xml.Name `xml:"SetHostnameFromDHCPResponse"`
|
||||
RebootNeeded bool `xml:"RebootNeeded"`
|
||||
}
|
||||
|
||||
req := SetHostnameFromDHCP{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
}
|
||||
|
||||
var resp SetHostnameFromDHCPResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return false, fmt.Errorf("SetHostnameFromDHCP failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.RebootNeeded, nil
|
||||
}
|
||||
|
||||
// FixedGetSystemDateAndTime retrieves the device's system date and time with proper typing.
|
||||
func (c *Client) FixedGetSystemDateAndTime(ctx context.Context) (*SystemDateTime, error) {
|
||||
type GetSystemDateAndTime struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemDateAndTime"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemDateAndTimeResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemDateAndTimeResponse"`
|
||||
SystemDateAndTime struct {
|
||||
DateTimeType string `xml:"DateTimeType"`
|
||||
DaylightSavings bool `xml:"DaylightSavings"`
|
||||
TimeZone struct {
|
||||
TZ string `xml:"TZ"`
|
||||
} `xml:"TimeZone"`
|
||||
UTCDateTime struct {
|
||||
Time struct {
|
||||
Hour int `xml:"Hour"`
|
||||
Minute int `xml:"Minute"`
|
||||
Second int `xml:"Second"`
|
||||
} `xml:"Time"`
|
||||
Date struct {
|
||||
Year int `xml:"Year"`
|
||||
Month int `xml:"Month"`
|
||||
Day int `xml:"Day"`
|
||||
} `xml:"Date"`
|
||||
} `xml:"UTCDateTime"`
|
||||
LocalDateTime struct {
|
||||
Time struct {
|
||||
Hour int `xml:"Hour"`
|
||||
Minute int `xml:"Minute"`
|
||||
Second int `xml:"Second"`
|
||||
} `xml:"Time"`
|
||||
Date struct {
|
||||
Year int `xml:"Year"`
|
||||
Month int `xml:"Month"`
|
||||
Day int `xml:"Day"`
|
||||
} `xml:"Date"`
|
||||
} `xml:"LocalDateTime"`
|
||||
} `xml:"SystemDateAndTime"`
|
||||
}
|
||||
|
||||
req := GetSystemDateAndTime{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemDateAndTimeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemDateAndTime failed: %w", err)
|
||||
}
|
||||
|
||||
return &SystemDateTime{
|
||||
DateTimeType: SetDateTimeType(resp.SystemDateAndTime.DateTimeType),
|
||||
DaylightSavings: resp.SystemDateAndTime.DaylightSavings,
|
||||
TimeZone: &TimeZone{
|
||||
TZ: resp.SystemDateAndTime.TimeZone.TZ,
|
||||
},
|
||||
UTCDateTime: &DateTime{
|
||||
Time: Time{
|
||||
Hour: resp.SystemDateAndTime.UTCDateTime.Time.Hour,
|
||||
Minute: resp.SystemDateAndTime.UTCDateTime.Time.Minute,
|
||||
Second: resp.SystemDateAndTime.UTCDateTime.Time.Second,
|
||||
},
|
||||
Date: Date{
|
||||
Year: resp.SystemDateAndTime.UTCDateTime.Date.Year,
|
||||
Month: resp.SystemDateAndTime.UTCDateTime.Date.Month,
|
||||
Day: resp.SystemDateAndTime.UTCDateTime.Date.Day,
|
||||
},
|
||||
},
|
||||
LocalDateTime: &DateTime{
|
||||
Time: Time{
|
||||
Hour: resp.SystemDateAndTime.LocalDateTime.Time.Hour,
|
||||
Minute: resp.SystemDateAndTime.LocalDateTime.Time.Minute,
|
||||
Second: resp.SystemDateAndTime.LocalDateTime.Time.Second,
|
||||
},
|
||||
Date: Date{
|
||||
Year: resp.SystemDateAndTime.LocalDateTime.Date.Year,
|
||||
Month: resp.SystemDateAndTime.LocalDateTime.Date.Month,
|
||||
Day: resp.SystemDateAndTime.LocalDateTime.Date.Day,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetSystemDateAndTime sets the device system date and time.
|
||||
func (c *Client) SetSystemDateAndTime(ctx context.Context, dateTime *SystemDateTime) error {
|
||||
type SetSystemDateAndTime struct {
|
||||
XMLName xml.Name `xml:"tds:SetSystemDateAndTime"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
DateTimeType string `xml:"tds:DateTimeType"`
|
||||
DaylightSavings bool `xml:"tds:DaylightSavings"`
|
||||
TimeZone *struct {
|
||||
TZ string `xml:"tds:TZ"`
|
||||
} `xml:"tds:TimeZone,omitempty"`
|
||||
UTCDateTime *struct {
|
||||
Time struct {
|
||||
Hour int `xml:"tt:Hour"`
|
||||
Minute int `xml:"tt:Minute"`
|
||||
Second int `xml:"tt:Second"`
|
||||
} `xml:"tt:Time"`
|
||||
Date struct {
|
||||
Year int `xml:"tt:Year"`
|
||||
Month int `xml:"tt:Month"`
|
||||
Day int `xml:"tt:Day"`
|
||||
} `xml:"tt:Date"`
|
||||
} `xml:"tds:UTCDateTime,omitempty"`
|
||||
}
|
||||
|
||||
req := SetSystemDateAndTime{
|
||||
Xmlns: deviceNamespace,
|
||||
DateTimeType: string(dateTime.DateTimeType),
|
||||
DaylightSavings: dateTime.DaylightSavings,
|
||||
}
|
||||
|
||||
if dateTime.TimeZone != nil {
|
||||
req.TimeZone = &struct {
|
||||
TZ string `xml:"tds:TZ"`
|
||||
}{
|
||||
TZ: dateTime.TimeZone.TZ,
|
||||
}
|
||||
}
|
||||
|
||||
if dateTime.UTCDateTime != nil {
|
||||
req.UTCDateTime = &struct {
|
||||
Time struct {
|
||||
Hour int `xml:"tt:Hour"`
|
||||
Minute int `xml:"tt:Minute"`
|
||||
Second int `xml:"tt:Second"`
|
||||
} `xml:"tt:Time"`
|
||||
Date struct {
|
||||
Year int `xml:"tt:Year"`
|
||||
Month int `xml:"tt:Month"`
|
||||
Day int `xml:"tt:Day"`
|
||||
} `xml:"tt:Date"`
|
||||
}{}
|
||||
req.UTCDateTime.Time.Hour = dateTime.UTCDateTime.Time.Hour
|
||||
req.UTCDateTime.Time.Minute = dateTime.UTCDateTime.Time.Minute
|
||||
req.UTCDateTime.Time.Second = dateTime.UTCDateTime.Time.Second
|
||||
req.UTCDateTime.Date.Year = dateTime.UTCDateTime.Date.Year
|
||||
req.UTCDateTime.Date.Month = dateTime.UTCDateTime.Date.Month
|
||||
req.UTCDateTime.Date.Day = dateTime.UTCDateTime.Date.Day
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetSystemDateAndTime failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddScopes adds new configurable scope parameters to a device.
|
||||
func (c *Client) AddScopes(ctx context.Context, scopeItems []string) error {
|
||||
type AddScopes struct {
|
||||
XMLName xml.Name `xml:"tds:AddScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
ScopeItem []string `xml:"tds:ScopeItem"`
|
||||
}
|
||||
|
||||
req := AddScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
ScopeItem: scopeItems,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("AddScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveScopes deletes scope-configurable scope parameters from a device.
|
||||
func (c *Client) RemoveScopes(ctx context.Context, scopeItems []string) ([]string, error) {
|
||||
type RemoveScopes struct {
|
||||
XMLName xml.Name `xml:"tds:RemoveScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
ScopeItem []string `xml:"tds:ScopeItem"`
|
||||
}
|
||||
|
||||
type RemoveScopesResponse struct {
|
||||
XMLName xml.Name `xml:"RemoveScopesResponse"`
|
||||
ScopeItem []string `xml:"ScopeItem"`
|
||||
}
|
||||
|
||||
req := RemoveScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
ScopeItem: scopeItems,
|
||||
}
|
||||
|
||||
var resp RemoveScopesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("RemoveScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.ScopeItem, nil
|
||||
}
|
||||
|
||||
// SetScopes sets the scope parameters of a device.
|
||||
func (c *Client) SetScopes(ctx context.Context, scopes []string) error {
|
||||
type SetScopes struct {
|
||||
XMLName xml.Name `xml:"tds:SetScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Scopes []string `xml:"tds:Scopes"`
|
||||
}
|
||||
|
||||
req := SetScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
Scopes: scopes,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRelayOutputs gets a list of all available relay outputs and their settings.
|
||||
func (c *Client) GetRelayOutputs(ctx context.Context) ([]*RelayOutput, error) {
|
||||
type GetRelayOutputs struct {
|
||||
XMLName xml.Name `xml:"tds:GetRelayOutputs"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetRelayOutputsResponse struct {
|
||||
XMLName xml.Name `xml:"GetRelayOutputsResponse"`
|
||||
RelayOutputs []struct {
|
||||
Token string `xml:"token,attr"`
|
||||
Properties struct {
|
||||
Mode string `xml:"Mode"`
|
||||
DelayTime string `xml:"DelayTime"`
|
||||
IdleState string `xml:"IdleState"`
|
||||
} `xml:"Properties"`
|
||||
} `xml:"RelayOutputs"`
|
||||
}
|
||||
|
||||
req := GetRelayOutputs{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetRelayOutputsResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetRelayOutputs failed: %w", err)
|
||||
}
|
||||
|
||||
relays := make([]*RelayOutput, len(resp.RelayOutputs))
|
||||
for i, relay := range resp.RelayOutputs {
|
||||
relays[i] = &RelayOutput{
|
||||
Token: relay.Token,
|
||||
Properties: RelayOutputSettings{
|
||||
Mode: RelayMode(relay.Properties.Mode),
|
||||
IdleState: RelayIdleState(relay.Properties.IdleState),
|
||||
// DelayTime parsing would require duration parsing
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return relays, nil
|
||||
}
|
||||
|
||||
// SetRelayOutputSettings sets the settings of a relay output.
|
||||
func (c *Client) SetRelayOutputSettings(ctx context.Context, token string, settings *RelayOutputSettings) error {
|
||||
type SetRelayOutputSettings struct {
|
||||
XMLName xml.Name `xml:"tds:SetRelayOutputSettings"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RelayOutputToken string `xml:"tds:RelayOutputToken"`
|
||||
Properties struct {
|
||||
Mode string `xml:"tt:Mode"`
|
||||
DelayTime string `xml:"tt:DelayTime"`
|
||||
IdleState string `xml:"tt:IdleState"`
|
||||
} `xml:"tds:Properties"`
|
||||
}
|
||||
|
||||
req := SetRelayOutputSettings{
|
||||
Xmlns: deviceNamespace,
|
||||
RelayOutputToken: token,
|
||||
}
|
||||
req.Properties.Mode = string(settings.Mode)
|
||||
req.Properties.IdleState = string(settings.IdleState)
|
||||
// DelayTime would need duration formatting
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRelayOutputSettings failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRelayOutputState sets the state of a relay output.
|
||||
func (c *Client) SetRelayOutputState(ctx context.Context, token string, state RelayLogicalState) error {
|
||||
type SetRelayOutputState struct {
|
||||
XMLName xml.Name `xml:"tds:SetRelayOutputState"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RelayOutputToken string `xml:"tds:RelayOutputToken"`
|
||||
LogicalState RelayLogicalState `xml:"tds:LogicalState"`
|
||||
}
|
||||
|
||||
req := SetRelayOutputState{
|
||||
Xmlns: deviceNamespace,
|
||||
RelayOutputToken: token,
|
||||
LogicalState: state,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRelayOutputState failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendAuxiliaryCommand sends an auxiliary command to the device.
|
||||
func (c *Client) SendAuxiliaryCommand(ctx context.Context, command AuxiliaryData) (AuxiliaryData, error) {
|
||||
type SendAuxiliaryCommand struct {
|
||||
XMLName xml.Name `xml:"tds:SendAuxiliaryCommand"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
AuxiliaryCommand AuxiliaryData `xml:"tds:AuxiliaryCommand"`
|
||||
}
|
||||
|
||||
type SendAuxiliaryCommandResponse struct {
|
||||
XMLName xml.Name `xml:"SendAuxiliaryCommandResponse"`
|
||||
AuxiliaryCommandResponse AuxiliaryData `xml:"AuxiliaryCommandResponse"`
|
||||
}
|
||||
|
||||
req := SendAuxiliaryCommand{
|
||||
Xmlns: deviceNamespace,
|
||||
AuxiliaryCommand: command,
|
||||
}
|
||||
|
||||
var resp SendAuxiliaryCommandResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", fmt.Errorf("SendAuxiliaryCommand failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.AuxiliaryCommandResponse, nil
|
||||
}
|
||||
|
||||
// GetSystemLog gets a system log from the device.
|
||||
func (c *Client) GetSystemLog(ctx context.Context, logType SystemLogType) (*SystemLog, error) {
|
||||
type GetSystemLog struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemLog"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
LogType SystemLogType `xml:"tds:LogType"`
|
||||
}
|
||||
|
||||
type GetSystemLogResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemLogResponse"`
|
||||
SystemLog struct {
|
||||
Binary *struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Binary"`
|
||||
String string `xml:"String"`
|
||||
} `xml:"SystemLog"`
|
||||
}
|
||||
|
||||
req := GetSystemLog{
|
||||
Xmlns: deviceNamespace,
|
||||
LogType: logType,
|
||||
}
|
||||
|
||||
var resp GetSystemLogResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemLog failed: %w", err)
|
||||
}
|
||||
|
||||
systemLog := &SystemLog{
|
||||
String: resp.SystemLog.String,
|
||||
}
|
||||
|
||||
if resp.SystemLog.Binary != nil {
|
||||
systemLog.Binary = &AttachmentData{
|
||||
ContentType: resp.SystemLog.Binary.ContentType,
|
||||
}
|
||||
}
|
||||
|
||||
return systemLog, nil
|
||||
}
|
||||
|
||||
// GetSystemBackup retrieves system backup configuration files from a device.
|
||||
func (c *Client) GetSystemBackup(ctx context.Context) ([]*BackupFile, error) {
|
||||
type GetSystemBackup struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemBackup"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemBackupResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemBackupResponse"`
|
||||
BackupFiles []struct {
|
||||
Name string `xml:"Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Data"`
|
||||
} `xml:"BackupFiles"`
|
||||
}
|
||||
|
||||
req := GetSystemBackup{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemBackupResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemBackup failed: %w", err)
|
||||
}
|
||||
|
||||
backups := make([]*BackupFile, len(resp.BackupFiles))
|
||||
for i, file := range resp.BackupFiles {
|
||||
backups[i] = &BackupFile{
|
||||
Name: file.Name,
|
||||
Data: AttachmentData{
|
||||
ContentType: file.Data.ContentType,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return backups, nil
|
||||
}
|
||||
|
||||
// RestoreSystem restores the system backup configuration files.
|
||||
func (c *Client) RestoreSystem(ctx context.Context, backupFiles []*BackupFile) error {
|
||||
type RestoreSystem struct {
|
||||
XMLName xml.Name `xml:"tds:RestoreSystem"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
BackupFiles []struct {
|
||||
Name string `xml:"tds:Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"tds:Data"`
|
||||
} `xml:"tds:BackupFiles"`
|
||||
}
|
||||
|
||||
req := RestoreSystem{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
for _, file := range backupFiles {
|
||||
req.BackupFiles = append(req.BackupFiles, struct {
|
||||
Name string `xml:"tds:Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"tds:Data"`
|
||||
}{
|
||||
Name: file.Name,
|
||||
Data: struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
}{
|
||||
ContentType: file.Data.ContentType,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("RestoreSystem failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSystemUris retrieves URIs from which system information may be downloaded.
|
||||
func (c *Client) GetSystemUris(
|
||||
ctx context.Context,
|
||||
) (uriList *SystemLogURIList, systemBackupURI, systemLogURI string, err error) {
|
||||
type GetSystemUris struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemUris"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemUrisResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemUrisResponse"`
|
||||
SystemLogUris *struct {
|
||||
SystemLog []struct {
|
||||
Type string `xml:"Type"`
|
||||
URI string `xml:"Uri"`
|
||||
} `xml:"SystemLog"`
|
||||
} `xml:"SystemLogUris"`
|
||||
SupportInfoURI string `xml:"SupportInfoUri"`
|
||||
SystemBackupURI string `xml:"SystemBackupUri"`
|
||||
}
|
||||
|
||||
req := GetSystemUris{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemUrisResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, "", "", fmt.Errorf("GetSystemUris failed: %w", err)
|
||||
}
|
||||
|
||||
var logUris *SystemLogURIList
|
||||
if resp.SystemLogUris != nil {
|
||||
logUris = &SystemLogURIList{}
|
||||
for _, log := range resp.SystemLogUris.SystemLog {
|
||||
logUris.SystemLog = append(logUris.SystemLog, SystemLogURI{
|
||||
Type: SystemLogType(log.Type),
|
||||
URI: log.URI,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return logUris, resp.SupportInfoURI, resp.SystemBackupURI, nil
|
||||
}
|
||||
|
||||
// GetSystemSupportInformation gets arbitrary device diagnostics information.
|
||||
func (c *Client) GetSystemSupportInformation(ctx context.Context) (*SupportInformation, error) {
|
||||
type GetSystemSupportInformation struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemSupportInformation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemSupportInformationResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemSupportInformationResponse"`
|
||||
SupportInformation struct {
|
||||
Binary *struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Binary"`
|
||||
String string `xml:"String"`
|
||||
} `xml:"SupportInformation"`
|
||||
}
|
||||
|
||||
req := GetSystemSupportInformation{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemSupportInformationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemSupportInformation failed: %w", err)
|
||||
}
|
||||
|
||||
info := &SupportInformation{
|
||||
String: resp.SupportInformation.String,
|
||||
}
|
||||
|
||||
if resp.SupportInformation.Binary != nil {
|
||||
info.Binary = &AttachmentData{
|
||||
ContentType: resp.SupportInformation.Binary.ContentType,
|
||||
}
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// SetSystemFactoryDefault reloads the parameters on the device to their factory default values.
|
||||
func (c *Client) SetSystemFactoryDefault(ctx context.Context, factoryDefault FactoryDefaultType) error {
|
||||
type SetSystemFactoryDefault struct {
|
||||
XMLName xml.Name `xml:"tds:SetSystemFactoryDefault"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FactoryDefault FactoryDefaultType `xml:"tds:FactoryDefault"`
|
||||
}
|
||||
|
||||
req := SetSystemFactoryDefault{
|
||||
Xmlns: deviceNamespace,
|
||||
FactoryDefault: factoryDefault,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetSystemFactoryDefault failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartFirmwareUpgrade initiates a firmware upgrade using the HTTP POST mechanism.
|
||||
func (c *Client) StartFirmwareUpgrade(
|
||||
ctx context.Context,
|
||||
) (uploadURI, uploadDelay, expectedDownTime string, err error) {
|
||||
type StartFirmwareUpgrade struct {
|
||||
XMLName xml.Name `xml:"tds:StartFirmwareUpgrade"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type StartFirmwareUpgradeResponse struct {
|
||||
XMLName xml.Name `xml:"StartFirmwareUpgradeResponse"`
|
||||
UploadURI string `xml:"UploadUri"`
|
||||
UploadDelay string `xml:"UploadDelay"`
|
||||
ExpectedDownTime string `xml:"ExpectedDownTime"`
|
||||
}
|
||||
|
||||
req := StartFirmwareUpgrade{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp StartFirmwareUpgradeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", "", "", fmt.Errorf("StartFirmwareUpgrade failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.UploadURI, resp.UploadDelay, resp.ExpectedDownTime, nil
|
||||
}
|
||||
|
||||
// StartSystemRestore initiates a system restore from backed up configuration data.
|
||||
func (c *Client) StartSystemRestore(ctx context.Context) (uploadURI, expectedDownTime string, err error) {
|
||||
type StartSystemRestore struct {
|
||||
XMLName xml.Name `xml:"tds:StartSystemRestore"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type StartSystemRestoreResponse struct {
|
||||
XMLName xml.Name `xml:"StartSystemRestoreResponse"`
|
||||
UploadURI string `xml:"UploadUri"`
|
||||
ExpectedDownTime string `xml:"ExpectedDownTime"`
|
||||
}
|
||||
|
||||
req := StartSystemRestore{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp StartSystemRestoreResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", "", fmt.Errorf("StartSystemRestore failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.UploadURI, resp.ExpectedDownTime, nil
|
||||
}
|
||||
@@ -1,796 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// SetDNS sets the DNS settings on a device.
|
||||
func (c *Client) SetDNS(ctx context.Context, fromDHCP bool, searchDomain []string, dnsManual []IPAddress) error {
|
||||
type SetDNS struct {
|
||||
XMLName xml.Name `xml:"tds:SetDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
SearchDomain []string `xml:"tds:SearchDomain,omitempty"`
|
||||
DNSManual []struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
} `xml:"tds:DNSManual,omitempty"`
|
||||
}
|
||||
|
||||
req := SetDNS{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
SearchDomain: searchDomain,
|
||||
}
|
||||
|
||||
for _, dns := range dnsManual {
|
||||
req.DNSManual = append(req.DNSManual, struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
}{
|
||||
Type: dns.Type,
|
||||
IPv4Address: dns.IPv4Address,
|
||||
IPv6Address: dns.IPv6Address,
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetNTP sets the NTP settings on a device.
|
||||
func (c *Client) SetNTP(ctx context.Context, fromDHCP bool, ntpManual []NetworkHost) error {
|
||||
type SetNTP struct {
|
||||
XMLName xml.Name `xml:"tds:SetNTP"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
NTPManual []struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
DNSname string `xml:"tds:DNSname,omitempty"`
|
||||
} `xml:"tds:NTPManual,omitempty"`
|
||||
}
|
||||
|
||||
req := SetNTP{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
}
|
||||
|
||||
for _, ntp := range ntpManual {
|
||||
req.NTPManual = append(req.NTPManual, struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address string `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address string `xml:"tds:IPv6Address,omitempty"`
|
||||
DNSname string `xml:"tds:DNSname,omitempty"`
|
||||
}{
|
||||
Type: ntp.Type,
|
||||
IPv4Address: ntp.IPv4Address,
|
||||
IPv6Address: ntp.IPv6Address,
|
||||
DNSname: ntp.DNSname,
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetNTP failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHostnameFromDHCP controls whether the hostname is set manually or retrieved via DHCP.
|
||||
func (c *Client) SetHostnameFromDHCP(ctx context.Context, fromDHCP bool) (bool, error) {
|
||||
type SetHostnameFromDHCP struct {
|
||||
XMLName xml.Name `xml:"tds:SetHostnameFromDHCP"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FromDHCP bool `xml:"tds:FromDHCP"`
|
||||
}
|
||||
|
||||
type SetHostnameFromDHCPResponse struct {
|
||||
XMLName xml.Name `xml:"SetHostnameFromDHCPResponse"`
|
||||
RebootNeeded bool `xml:"RebootNeeded"`
|
||||
}
|
||||
|
||||
req := SetHostnameFromDHCP{
|
||||
Xmlns: deviceNamespace,
|
||||
FromDHCP: fromDHCP,
|
||||
}
|
||||
|
||||
var resp SetHostnameFromDHCPResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return false, fmt.Errorf("SetHostnameFromDHCP failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.RebootNeeded, nil
|
||||
}
|
||||
|
||||
// FixedGetSystemDateAndTime retrieves the device's system date and time with proper typing.
|
||||
func (c *Client) FixedGetSystemDateAndTime(ctx context.Context) (*SystemDateTime, error) {
|
||||
type GetSystemDateAndTime struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemDateAndTime"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemDateAndTimeResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemDateAndTimeResponse"`
|
||||
SystemDateAndTime struct {
|
||||
DateTimeType string `xml:"DateTimeType"`
|
||||
DaylightSavings bool `xml:"DaylightSavings"`
|
||||
TimeZone struct {
|
||||
TZ string `xml:"TZ"`
|
||||
} `xml:"TimeZone"`
|
||||
UTCDateTime struct {
|
||||
Time struct {
|
||||
Hour int `xml:"Hour"`
|
||||
Minute int `xml:"Minute"`
|
||||
Second int `xml:"Second"`
|
||||
} `xml:"Time"`
|
||||
Date struct {
|
||||
Year int `xml:"Year"`
|
||||
Month int `xml:"Month"`
|
||||
Day int `xml:"Day"`
|
||||
} `xml:"Date"`
|
||||
} `xml:"UTCDateTime"`
|
||||
LocalDateTime struct {
|
||||
Time struct {
|
||||
Hour int `xml:"Hour"`
|
||||
Minute int `xml:"Minute"`
|
||||
Second int `xml:"Second"`
|
||||
} `xml:"Time"`
|
||||
Date struct {
|
||||
Year int `xml:"Year"`
|
||||
Month int `xml:"Month"`
|
||||
Day int `xml:"Day"`
|
||||
} `xml:"Date"`
|
||||
} `xml:"LocalDateTime"`
|
||||
} `xml:"SystemDateAndTime"`
|
||||
}
|
||||
|
||||
req := GetSystemDateAndTime{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemDateAndTimeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemDateAndTime failed: %w", err)
|
||||
}
|
||||
|
||||
return &SystemDateTime{
|
||||
DateTimeType: SetDateTimeType(resp.SystemDateAndTime.DateTimeType),
|
||||
DaylightSavings: resp.SystemDateAndTime.DaylightSavings,
|
||||
TimeZone: &TimeZone{
|
||||
TZ: resp.SystemDateAndTime.TimeZone.TZ,
|
||||
},
|
||||
UTCDateTime: &DateTime{
|
||||
Time: Time{
|
||||
Hour: resp.SystemDateAndTime.UTCDateTime.Time.Hour,
|
||||
Minute: resp.SystemDateAndTime.UTCDateTime.Time.Minute,
|
||||
Second: resp.SystemDateAndTime.UTCDateTime.Time.Second,
|
||||
},
|
||||
Date: Date{
|
||||
Year: resp.SystemDateAndTime.UTCDateTime.Date.Year,
|
||||
Month: resp.SystemDateAndTime.UTCDateTime.Date.Month,
|
||||
Day: resp.SystemDateAndTime.UTCDateTime.Date.Day,
|
||||
},
|
||||
},
|
||||
LocalDateTime: &DateTime{
|
||||
Time: Time{
|
||||
Hour: resp.SystemDateAndTime.LocalDateTime.Time.Hour,
|
||||
Minute: resp.SystemDateAndTime.LocalDateTime.Time.Minute,
|
||||
Second: resp.SystemDateAndTime.LocalDateTime.Time.Second,
|
||||
},
|
||||
Date: Date{
|
||||
Year: resp.SystemDateAndTime.LocalDateTime.Date.Year,
|
||||
Month: resp.SystemDateAndTime.LocalDateTime.Date.Month,
|
||||
Day: resp.SystemDateAndTime.LocalDateTime.Date.Day,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetSystemDateAndTime sets the device system date and time.
|
||||
func (c *Client) SetSystemDateAndTime(ctx context.Context, dateTime *SystemDateTime) error {
|
||||
type SetSystemDateAndTime struct {
|
||||
XMLName xml.Name `xml:"tds:SetSystemDateAndTime"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
DateTimeType string `xml:"tds:DateTimeType"`
|
||||
DaylightSavings bool `xml:"tds:DaylightSavings"`
|
||||
TimeZone *struct {
|
||||
TZ string `xml:"tds:TZ"`
|
||||
} `xml:"tds:TimeZone,omitempty"`
|
||||
UTCDateTime *struct {
|
||||
Time struct {
|
||||
Hour int `xml:"tt:Hour"`
|
||||
Minute int `xml:"tt:Minute"`
|
||||
Second int `xml:"tt:Second"`
|
||||
} `xml:"tt:Time"`
|
||||
Date struct {
|
||||
Year int `xml:"tt:Year"`
|
||||
Month int `xml:"tt:Month"`
|
||||
Day int `xml:"tt:Day"`
|
||||
} `xml:"tt:Date"`
|
||||
} `xml:"tds:UTCDateTime,omitempty"`
|
||||
}
|
||||
|
||||
req := SetSystemDateAndTime{
|
||||
Xmlns: deviceNamespace,
|
||||
DateTimeType: string(dateTime.DateTimeType),
|
||||
DaylightSavings: dateTime.DaylightSavings,
|
||||
}
|
||||
|
||||
if dateTime.TimeZone != nil {
|
||||
req.TimeZone = &struct {
|
||||
TZ string `xml:"tds:TZ"`
|
||||
}{
|
||||
TZ: dateTime.TimeZone.TZ,
|
||||
}
|
||||
}
|
||||
|
||||
if dateTime.UTCDateTime != nil {
|
||||
req.UTCDateTime = &struct {
|
||||
Time struct {
|
||||
Hour int `xml:"tt:Hour"`
|
||||
Minute int `xml:"tt:Minute"`
|
||||
Second int `xml:"tt:Second"`
|
||||
} `xml:"tt:Time"`
|
||||
Date struct {
|
||||
Year int `xml:"tt:Year"`
|
||||
Month int `xml:"tt:Month"`
|
||||
Day int `xml:"tt:Day"`
|
||||
} `xml:"tt:Date"`
|
||||
}{}
|
||||
req.UTCDateTime.Time.Hour = dateTime.UTCDateTime.Time.Hour
|
||||
req.UTCDateTime.Time.Minute = dateTime.UTCDateTime.Time.Minute
|
||||
req.UTCDateTime.Time.Second = dateTime.UTCDateTime.Time.Second
|
||||
req.UTCDateTime.Date.Year = dateTime.UTCDateTime.Date.Year
|
||||
req.UTCDateTime.Date.Month = dateTime.UTCDateTime.Date.Month
|
||||
req.UTCDateTime.Date.Day = dateTime.UTCDateTime.Date.Day
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetSystemDateAndTime failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddScopes adds new configurable scope parameters to a device.
|
||||
func (c *Client) AddScopes(ctx context.Context, scopeItems []string) error {
|
||||
type AddScopes struct {
|
||||
XMLName xml.Name `xml:"tds:AddScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
ScopeItem []string `xml:"tds:ScopeItem"`
|
||||
}
|
||||
|
||||
req := AddScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
ScopeItem: scopeItems,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("AddScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveScopes deletes scope-configurable scope parameters from a device.
|
||||
func (c *Client) RemoveScopes(ctx context.Context, scopeItems []string) ([]string, error) {
|
||||
type RemoveScopes struct {
|
||||
XMLName xml.Name `xml:"tds:RemoveScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
ScopeItem []string `xml:"tds:ScopeItem"`
|
||||
}
|
||||
|
||||
type RemoveScopesResponse struct {
|
||||
XMLName xml.Name `xml:"RemoveScopesResponse"`
|
||||
ScopeItem []string `xml:"ScopeItem"`
|
||||
}
|
||||
|
||||
req := RemoveScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
ScopeItem: scopeItems,
|
||||
}
|
||||
|
||||
var resp RemoveScopesResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("RemoveScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.ScopeItem, nil
|
||||
}
|
||||
|
||||
// SetScopes sets the scope parameters of a device.
|
||||
func (c *Client) SetScopes(ctx context.Context, scopes []string) error {
|
||||
type SetScopes struct {
|
||||
XMLName xml.Name `xml:"tds:SetScopes"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Scopes []string `xml:"tds:Scopes"`
|
||||
}
|
||||
|
||||
req := SetScopes{
|
||||
Xmlns: deviceNamespace,
|
||||
Scopes: scopes,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetScopes failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRelayOutputs gets a list of all available relay outputs and their settings.
|
||||
func (c *Client) GetRelayOutputs(ctx context.Context) ([]*RelayOutput, error) {
|
||||
type GetRelayOutputs struct {
|
||||
XMLName xml.Name `xml:"tds:GetRelayOutputs"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetRelayOutputsResponse struct {
|
||||
XMLName xml.Name `xml:"GetRelayOutputsResponse"`
|
||||
RelayOutputs []struct {
|
||||
Token string `xml:"token,attr"`
|
||||
Properties struct {
|
||||
Mode string `xml:"Mode"`
|
||||
DelayTime string `xml:"DelayTime"`
|
||||
IdleState string `xml:"IdleState"`
|
||||
} `xml:"Properties"`
|
||||
} `xml:"RelayOutputs"`
|
||||
}
|
||||
|
||||
req := GetRelayOutputs{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetRelayOutputsResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetRelayOutputs failed: %w", err)
|
||||
}
|
||||
|
||||
relays := make([]*RelayOutput, len(resp.RelayOutputs))
|
||||
for i, relay := range resp.RelayOutputs {
|
||||
relays[i] = &RelayOutput{
|
||||
Token: relay.Token,
|
||||
Properties: RelayOutputSettings{
|
||||
Mode: RelayMode(relay.Properties.Mode),
|
||||
IdleState: RelayIdleState(relay.Properties.IdleState),
|
||||
// DelayTime parsing would require duration parsing
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return relays, nil
|
||||
}
|
||||
|
||||
// SetRelayOutputSettings sets the settings of a relay output.
|
||||
func (c *Client) SetRelayOutputSettings(ctx context.Context, token string, settings *RelayOutputSettings) error {
|
||||
type SetRelayOutputSettings struct {
|
||||
XMLName xml.Name `xml:"tds:SetRelayOutputSettings"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RelayOutputToken string `xml:"tds:RelayOutputToken"`
|
||||
Properties struct {
|
||||
Mode string `xml:"tt:Mode"`
|
||||
DelayTime string `xml:"tt:DelayTime"`
|
||||
IdleState string `xml:"tt:IdleState"`
|
||||
} `xml:"tds:Properties"`
|
||||
}
|
||||
|
||||
req := SetRelayOutputSettings{
|
||||
Xmlns: deviceNamespace,
|
||||
RelayOutputToken: token,
|
||||
}
|
||||
req.Properties.Mode = string(settings.Mode)
|
||||
req.Properties.IdleState = string(settings.IdleState)
|
||||
// DelayTime would need duration formatting
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRelayOutputSettings failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRelayOutputState sets the state of a relay output.
|
||||
func (c *Client) SetRelayOutputState(ctx context.Context, token string, state RelayLogicalState) error {
|
||||
type SetRelayOutputState struct {
|
||||
XMLName xml.Name `xml:"tds:SetRelayOutputState"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RelayOutputToken string `xml:"tds:RelayOutputToken"`
|
||||
LogicalState RelayLogicalState `xml:"tds:LogicalState"`
|
||||
}
|
||||
|
||||
req := SetRelayOutputState{
|
||||
Xmlns: deviceNamespace,
|
||||
RelayOutputToken: token,
|
||||
LogicalState: state,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRelayOutputState failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendAuxiliaryCommand sends an auxiliary command to the device.
|
||||
func (c *Client) SendAuxiliaryCommand(ctx context.Context, command AuxiliaryData) (AuxiliaryData, error) {
|
||||
type SendAuxiliaryCommand struct {
|
||||
XMLName xml.Name `xml:"tds:SendAuxiliaryCommand"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
AuxiliaryCommand AuxiliaryData `xml:"tds:AuxiliaryCommand"`
|
||||
}
|
||||
|
||||
type SendAuxiliaryCommandResponse struct {
|
||||
XMLName xml.Name `xml:"SendAuxiliaryCommandResponse"`
|
||||
AuxiliaryCommandResponse AuxiliaryData `xml:"AuxiliaryCommandResponse"`
|
||||
}
|
||||
|
||||
req := SendAuxiliaryCommand{
|
||||
Xmlns: deviceNamespace,
|
||||
AuxiliaryCommand: command,
|
||||
}
|
||||
|
||||
var resp SendAuxiliaryCommandResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", fmt.Errorf("SendAuxiliaryCommand failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.AuxiliaryCommandResponse, nil
|
||||
}
|
||||
|
||||
// GetSystemLog gets a system log from the device.
|
||||
func (c *Client) GetSystemLog(ctx context.Context, logType SystemLogType) (*SystemLog, error) {
|
||||
type GetSystemLog struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemLog"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
LogType SystemLogType `xml:"tds:LogType"`
|
||||
}
|
||||
|
||||
type GetSystemLogResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemLogResponse"`
|
||||
SystemLog struct {
|
||||
Binary *struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Binary"`
|
||||
String string `xml:"String"`
|
||||
} `xml:"SystemLog"`
|
||||
}
|
||||
|
||||
req := GetSystemLog{
|
||||
Xmlns: deviceNamespace,
|
||||
LogType: logType,
|
||||
}
|
||||
|
||||
var resp GetSystemLogResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemLog failed: %w", err)
|
||||
}
|
||||
|
||||
systemLog := &SystemLog{
|
||||
String: resp.SystemLog.String,
|
||||
}
|
||||
|
||||
if resp.SystemLog.Binary != nil {
|
||||
systemLog.Binary = &AttachmentData{
|
||||
ContentType: resp.SystemLog.Binary.ContentType,
|
||||
}
|
||||
}
|
||||
|
||||
return systemLog, nil
|
||||
}
|
||||
|
||||
// GetSystemBackup retrieves system backup configuration files from a device.
|
||||
func (c *Client) GetSystemBackup(ctx context.Context) ([]*BackupFile, error) {
|
||||
type GetSystemBackup struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemBackup"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemBackupResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemBackupResponse"`
|
||||
BackupFiles []struct {
|
||||
Name string `xml:"Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Data"`
|
||||
} `xml:"BackupFiles"`
|
||||
}
|
||||
|
||||
req := GetSystemBackup{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemBackupResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemBackup failed: %w", err)
|
||||
}
|
||||
|
||||
backups := make([]*BackupFile, len(resp.BackupFiles))
|
||||
for i, file := range resp.BackupFiles {
|
||||
backups[i] = &BackupFile{
|
||||
Name: file.Name,
|
||||
Data: AttachmentData{
|
||||
ContentType: file.Data.ContentType,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return backups, nil
|
||||
}
|
||||
|
||||
// RestoreSystem restores the system backup configuration files.
|
||||
func (c *Client) RestoreSystem(ctx context.Context, backupFiles []*BackupFile) error {
|
||||
type RestoreSystem struct {
|
||||
XMLName xml.Name `xml:"tds:RestoreSystem"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
BackupFiles []struct {
|
||||
Name string `xml:"tds:Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"tds:Data"`
|
||||
} `xml:"tds:BackupFiles"`
|
||||
}
|
||||
|
||||
req := RestoreSystem{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
for _, file := range backupFiles {
|
||||
req.BackupFiles = append(req.BackupFiles, struct {
|
||||
Name string `xml:"tds:Name"`
|
||||
Data struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"tds:Data"`
|
||||
}{
|
||||
Name: file.Name,
|
||||
Data: struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
}{
|
||||
ContentType: file.Data.ContentType,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("RestoreSystem failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSystemUris retrieves URIs from which system information may be downloaded.
|
||||
func (c *Client) GetSystemUris(
|
||||
ctx context.Context,
|
||||
) (uriList *SystemLogURIList, systemBackupURI, systemLogURI string, err error) {
|
||||
type GetSystemUris struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemUris"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemUrisResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemUrisResponse"`
|
||||
SystemLogUris *struct {
|
||||
SystemLog []struct {
|
||||
Type string `xml:"Type"`
|
||||
URI string `xml:"Uri"`
|
||||
} `xml:"SystemLog"`
|
||||
} `xml:"SystemLogUris"`
|
||||
SupportInfoURI string `xml:"SupportInfoUri"`
|
||||
SystemBackupURI string `xml:"SystemBackupUri"`
|
||||
}
|
||||
|
||||
req := GetSystemUris{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemUrisResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, "", "", fmt.Errorf("GetSystemUris failed: %w", err)
|
||||
}
|
||||
|
||||
var logUris *SystemLogURIList
|
||||
if resp.SystemLogUris != nil {
|
||||
logUris = &SystemLogURIList{}
|
||||
for _, log := range resp.SystemLogUris.SystemLog {
|
||||
logUris.SystemLog = append(logUris.SystemLog, SystemLogURI{
|
||||
Type: SystemLogType(log.Type),
|
||||
URI: log.URI,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return logUris, resp.SupportInfoURI, resp.SystemBackupURI, nil
|
||||
}
|
||||
|
||||
// GetSystemSupportInformation gets arbitrary device diagnostics information.
|
||||
func (c *Client) GetSystemSupportInformation(ctx context.Context) (*SupportInformation, error) {
|
||||
type GetSystemSupportInformation struct {
|
||||
XMLName xml.Name `xml:"tds:GetSystemSupportInformation"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetSystemSupportInformationResponse struct {
|
||||
XMLName xml.Name `xml:"GetSystemSupportInformationResponse"`
|
||||
SupportInformation struct {
|
||||
Binary *struct {
|
||||
ContentType string `xml:"contentType,attr"`
|
||||
} `xml:"Binary"`
|
||||
String string `xml:"String"`
|
||||
} `xml:"SupportInformation"`
|
||||
}
|
||||
|
||||
req := GetSystemSupportInformation{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp GetSystemSupportInformationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetSystemSupportInformation failed: %w", err)
|
||||
}
|
||||
|
||||
info := &SupportInformation{
|
||||
String: resp.SupportInformation.String,
|
||||
}
|
||||
|
||||
if resp.SupportInformation.Binary != nil {
|
||||
info.Binary = &AttachmentData{
|
||||
ContentType: resp.SupportInformation.Binary.ContentType,
|
||||
}
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// SetSystemFactoryDefault reloads the parameters on the device to their factory default values.
|
||||
func (c *Client) SetSystemFactoryDefault(ctx context.Context, factoryDefault FactoryDefaultType) error {
|
||||
type SetSystemFactoryDefault struct {
|
||||
XMLName xml.Name `xml:"tds:SetSystemFactoryDefault"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
FactoryDefault FactoryDefaultType `xml:"tds:FactoryDefault"`
|
||||
}
|
||||
|
||||
req := SetSystemFactoryDefault{
|
||||
Xmlns: deviceNamespace,
|
||||
FactoryDefault: factoryDefault,
|
||||
}
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetSystemFactoryDefault failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartFirmwareUpgrade initiates a firmware upgrade using the HTTP POST mechanism.
|
||||
func (c *Client) StartFirmwareUpgrade(
|
||||
ctx context.Context,
|
||||
) (uploadURI, uploadDelay, expectedDownTime string, err error) {
|
||||
type StartFirmwareUpgrade struct {
|
||||
XMLName xml.Name `xml:"tds:StartFirmwareUpgrade"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type StartFirmwareUpgradeResponse struct {
|
||||
XMLName xml.Name `xml:"StartFirmwareUpgradeResponse"`
|
||||
UploadURI string `xml:"UploadUri"`
|
||||
UploadDelay string `xml:"UploadDelay"`
|
||||
ExpectedDownTime string `xml:"ExpectedDownTime"`
|
||||
}
|
||||
|
||||
req := StartFirmwareUpgrade{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp StartFirmwareUpgradeResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", "", "", fmt.Errorf("StartFirmwareUpgrade failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.UploadURI, resp.UploadDelay, resp.ExpectedDownTime, nil
|
||||
}
|
||||
|
||||
// StartSystemRestore initiates a system restore from backed up configuration data.
|
||||
func (c *Client) StartSystemRestore(ctx context.Context) (uploadURI, expectedDownTime string, err error) {
|
||||
type StartSystemRestore struct {
|
||||
XMLName xml.Name `xml:"tds:StartSystemRestore"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type StartSystemRestoreResponse struct {
|
||||
XMLName xml.Name `xml:"StartSystemRestoreResponse"`
|
||||
UploadURI string `xml:"UploadUri"`
|
||||
ExpectedDownTime string `xml:"ExpectedDownTime"`
|
||||
}
|
||||
|
||||
req := StartSystemRestore{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp StartSystemRestoreResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return "", "", fmt.Errorf("StartSystemRestore failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.UploadURI, resp.ExpectedDownTime, nil
|
||||
}
|
||||
@@ -1,414 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceExtendedServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "AddScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:AddScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "RemoveScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:RemoveScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:ScopeItem>onvif://www.onvif.org/location/test</tds:ScopeItem>
|
||||
</tds:RemoveScopesResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetRelayOutputs"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetRelayOutputsResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:RelayOutputs token="relay1">
|
||||
<tt:Properties>
|
||||
<tt:Mode>Bistable</tt:Mode>
|
||||
<tt:DelayTime>PT0S</tt:DelayTime>
|
||||
<tt:IdleState>closed</tt:IdleState>
|
||||
</tt:Properties>
|
||||
</tds:RelayOutputs>
|
||||
</tds:GetRelayOutputsResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRelayOutputSettings"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRelayOutputSettingsResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRelayOutputState"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRelayOutputStateResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SendAuxiliaryCommand"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SendAuxiliaryCommandResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:AuxiliaryCommandResponse>tt:IRLamp|On</tds:AuxiliaryCommandResponse>
|
||||
</tds:SendAuxiliaryCommandResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetSystemLog"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetSystemLogResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:SystemLog>
|
||||
<tt:String>System log content here</tt:String>
|
||||
</tds:SystemLog>
|
||||
</tds:GetSystemLogResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetSystemFactoryDefault"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetSystemFactoryDefaultResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "StartFirmwareUpgrade"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:StartFirmwareUpgradeResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:UploadUri>http://192.168.1.100/upload</tds:UploadUri>
|
||||
<tds:UploadDelay>PT5S</tds:UploadDelay>
|
||||
<tds:ExpectedDownTime>PT60S</tds:ExpectedDownTime>
|
||||
</tds:StartFirmwareUpgradeResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestAddScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{
|
||||
"onvif://www.onvif.org/location/building/floor1",
|
||||
"onvif://www.onvif.org/name/camera-entrance",
|
||||
}
|
||||
|
||||
err = client.AddScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("AddScopes failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{"onvif://www.onvif.org/location/test"}
|
||||
|
||||
removed, err := client.RemoveScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveScopes failed: %v", err)
|
||||
}
|
||||
|
||||
if len(removed) != 1 {
|
||||
t.Fatalf("Expected 1 removed scope, got %d", len(removed))
|
||||
}
|
||||
|
||||
if removed[0] != "onvif://www.onvif.org/location/test" {
|
||||
t.Errorf("Expected removed scope to match, got %s", removed[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{"scope1", "scope2"}
|
||||
|
||||
err = client.SetScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("SetScopes failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRelayOutputs(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
relays, err := client.GetRelayOutputs(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRelayOutputs failed: %v", err)
|
||||
}
|
||||
|
||||
if len(relays) != 1 {
|
||||
t.Fatalf("Expected 1 relay, got %d", len(relays))
|
||||
}
|
||||
|
||||
if relays[0].Token != "relay1" {
|
||||
t.Errorf("Expected relay token 'relay1', got %s", relays[0].Token)
|
||||
}
|
||||
|
||||
if relays[0].Properties.Mode != RelayModeBistable {
|
||||
t.Errorf("Expected Bistable mode, got %s", relays[0].Properties.Mode)
|
||||
}
|
||||
|
||||
if relays[0].Properties.IdleState != RelayIdleStateClosed {
|
||||
t.Errorf("Expected closed idle state, got %s", relays[0].Properties.IdleState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRelayOutputSettings(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
settings := &RelayOutputSettings{
|
||||
Mode: RelayModeBistable,
|
||||
IdleState: RelayIdleStateClosed,
|
||||
}
|
||||
|
||||
err = client.SetRelayOutputSettings(ctx, "relay1", settings)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputSettings failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRelayOutputState(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Test active state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", RelayLogicalStateActive)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputState (active) failed: %v", err)
|
||||
}
|
||||
|
||||
// Test inactive state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", RelayLogicalStateInactive)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputState (inactive) failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendAuxiliaryCommand(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
response, err := client.SendAuxiliaryCommand(ctx, "tt:IRLamp|On")
|
||||
if err != nil {
|
||||
t.Fatalf("SendAuxiliaryCommand failed: %v", err)
|
||||
}
|
||||
|
||||
if response != "tt:IRLamp|On" {
|
||||
t.Errorf("Expected response 'tt:IRLamp|On', got %s", response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSystemLog(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
log, err := client.GetSystemLog(ctx, SystemLogTypeSystem)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemLog failed: %v", err)
|
||||
}
|
||||
|
||||
if log.String != "System log content here" {
|
||||
t.Errorf("Expected system log content, got %s", log.String)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSystemFactoryDefault(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Test soft reset
|
||||
err = client.SetSystemFactoryDefault(ctx, FactoryDefaultSoft)
|
||||
if err != nil {
|
||||
t.Fatalf("SetSystemFactoryDefault (soft) failed: %v", err)
|
||||
}
|
||||
|
||||
// Test hard reset
|
||||
err = client.SetSystemFactoryDefault(ctx, FactoryDefaultHard)
|
||||
if err != nil {
|
||||
t.Fatalf("SetSystemFactoryDefault (hard) failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartFirmwareUpgrade(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
uploadURI, delay, downtime, err := client.StartFirmwareUpgrade(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("StartFirmwareUpgrade failed: %v", err)
|
||||
}
|
||||
|
||||
if uploadURI != "http://192.168.1.100/upload" {
|
||||
t.Errorf("Expected upload URI http://192.168.1.100/upload, got %s", uploadURI)
|
||||
}
|
||||
|
||||
if delay != "PT5S" {
|
||||
t.Errorf("Expected delay PT5S, got %s", delay)
|
||||
}
|
||||
|
||||
if downtime != "PT60S" {
|
||||
t.Errorf("Expected downtime PT60S, got %s", downtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayModeConstants(t *testing.T) {
|
||||
if RelayModeMonostable != "Monostable" {
|
||||
t.Errorf("RelayModeMonostable should be 'Monostable', got %s", RelayModeMonostable)
|
||||
}
|
||||
|
||||
if RelayModeBistable != "Bistable" {
|
||||
t.Errorf("RelayModeBistable should be 'Bistable', got %s", RelayModeBistable)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayIdleStateConstants(t *testing.T) {
|
||||
if RelayIdleStateClosed != "closed" {
|
||||
t.Errorf("RelayIdleStateClosed should be 'closed', got %s", RelayIdleStateClosed)
|
||||
}
|
||||
|
||||
if RelayIdleStateOpen != "open" {
|
||||
t.Errorf("RelayIdleStateOpen should be 'open', got %s", RelayIdleStateOpen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayLogicalStateConstants(t *testing.T) {
|
||||
if RelayLogicalStateActive != "active" {
|
||||
t.Errorf("RelayLogicalStateActive should be 'active', got %s", RelayLogicalStateActive)
|
||||
}
|
||||
|
||||
if RelayLogicalStateInactive != "inactive" {
|
||||
t.Errorf("RelayLogicalStateInactive should be 'inactive', got %s", RelayLogicalStateInactive)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogTypeConstants(t *testing.T) {
|
||||
if SystemLogTypeSystem != "System" {
|
||||
t.Errorf("SystemLogTypeSystem should be 'System', got %s", SystemLogTypeSystem)
|
||||
}
|
||||
|
||||
if SystemLogTypeAccess != "Access" {
|
||||
t.Errorf("SystemLogTypeAccess should be 'Access', got %s", SystemLogTypeAccess)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFactoryDefaultTypeConstants(t *testing.T) {
|
||||
if FactoryDefaultHard != "Hard" {
|
||||
t.Errorf("FactoryDefaultHard should be 'Hard', got %s", FactoryDefaultHard)
|
||||
}
|
||||
|
||||
if FactoryDefaultSoft != "Soft" {
|
||||
t.Errorf("FactoryDefaultSoft should be 'Soft', got %s", FactoryDefaultSoft)
|
||||
}
|
||||
}
|
||||
@@ -1,414 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceExtendedServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "AddScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:AddScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "RemoveScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:RemoveScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:ScopeItem>onvif://www.onvif.org/location/test</tds:ScopeItem>
|
||||
</tds:RemoveScopesResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetScopes"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetRelayOutputs"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetRelayOutputsResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:RelayOutputs token="relay1">
|
||||
<tt:Properties>
|
||||
<tt:Mode>Bistable</tt:Mode>
|
||||
<tt:DelayTime>PT0S</tt:DelayTime>
|
||||
<tt:IdleState>closed</tt:IdleState>
|
||||
</tt:Properties>
|
||||
</tds:RelayOutputs>
|
||||
</tds:GetRelayOutputsResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRelayOutputSettings"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRelayOutputSettingsResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRelayOutputState"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRelayOutputStateResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SendAuxiliaryCommand"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SendAuxiliaryCommandResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:AuxiliaryCommandResponse>tt:IRLamp|On</tds:AuxiliaryCommandResponse>
|
||||
</tds:SendAuxiliaryCommandResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetSystemLog"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetSystemLogResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:SystemLog>
|
||||
<tt:String>System log content here</tt:String>
|
||||
</tds:SystemLog>
|
||||
</tds:GetSystemLogResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetSystemFactoryDefault"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetSystemFactoryDefaultResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "StartFirmwareUpgrade"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:StartFirmwareUpgradeResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:UploadUri>http://192.168.1.100/upload</tds:UploadUri>
|
||||
<tds:UploadDelay>PT5S</tds:UploadDelay>
|
||||
<tds:ExpectedDownTime>PT60S</tds:ExpectedDownTime>
|
||||
</tds:StartFirmwareUpgradeResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestAddScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{
|
||||
"onvif://www.onvif.org/location/building/floor1",
|
||||
"onvif://www.onvif.org/name/camera-entrance",
|
||||
}
|
||||
|
||||
err = client.AddScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("AddScopes failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{"onvif://www.onvif.org/location/test"}
|
||||
|
||||
removed, err := client.RemoveScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveScopes failed: %v", err)
|
||||
}
|
||||
|
||||
if len(removed) != 1 {
|
||||
t.Fatalf("Expected 1 removed scope, got %d", len(removed))
|
||||
}
|
||||
|
||||
if removed[0] != "onvif://www.onvif.org/location/test" {
|
||||
t.Errorf("Expected removed scope to match, got %s", removed[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScopes(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes := []string{"scope1", "scope2"}
|
||||
|
||||
err = client.SetScopes(ctx, scopes)
|
||||
if err != nil {
|
||||
t.Fatalf("SetScopes failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRelayOutputs(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
relays, err := client.GetRelayOutputs(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRelayOutputs failed: %v", err)
|
||||
}
|
||||
|
||||
if len(relays) != 1 {
|
||||
t.Fatalf("Expected 1 relay, got %d", len(relays))
|
||||
}
|
||||
|
||||
if relays[0].Token != "relay1" {
|
||||
t.Errorf("Expected relay token 'relay1', got %s", relays[0].Token)
|
||||
}
|
||||
|
||||
if relays[0].Properties.Mode != RelayModeBistable {
|
||||
t.Errorf("Expected Bistable mode, got %s", relays[0].Properties.Mode)
|
||||
}
|
||||
|
||||
if relays[0].Properties.IdleState != RelayIdleStateClosed {
|
||||
t.Errorf("Expected closed idle state, got %s", relays[0].Properties.IdleState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRelayOutputSettings(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
settings := &RelayOutputSettings{
|
||||
Mode: RelayModeBistable,
|
||||
IdleState: RelayIdleStateClosed,
|
||||
}
|
||||
|
||||
err = client.SetRelayOutputSettings(ctx, "relay1", settings)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputSettings failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRelayOutputState(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Test active state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", RelayLogicalStateActive)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputState (active) failed: %v", err)
|
||||
}
|
||||
|
||||
// Test inactive state
|
||||
err = client.SetRelayOutputState(ctx, "relay1", RelayLogicalStateInactive)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRelayOutputState (inactive) failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendAuxiliaryCommand(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
response, err := client.SendAuxiliaryCommand(ctx, "tt:IRLamp|On")
|
||||
if err != nil {
|
||||
t.Fatalf("SendAuxiliaryCommand failed: %v", err)
|
||||
}
|
||||
|
||||
if response != "tt:IRLamp|On" {
|
||||
t.Errorf("Expected response 'tt:IRLamp|On', got %s", response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSystemLog(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
log, err := client.GetSystemLog(ctx, SystemLogTypeSystem)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemLog failed: %v", err)
|
||||
}
|
||||
|
||||
if log.String != "System log content here" {
|
||||
t.Errorf("Expected system log content, got %s", log.String)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSystemFactoryDefault(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Test soft reset
|
||||
err = client.SetSystemFactoryDefault(ctx, FactoryDefaultSoft)
|
||||
if err != nil {
|
||||
t.Fatalf("SetSystemFactoryDefault (soft) failed: %v", err)
|
||||
}
|
||||
|
||||
// Test hard reset
|
||||
err = client.SetSystemFactoryDefault(ctx, FactoryDefaultHard)
|
||||
if err != nil {
|
||||
t.Fatalf("SetSystemFactoryDefault (hard) failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartFirmwareUpgrade(t *testing.T) {
|
||||
server := newMockDeviceExtendedServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
uploadURI, delay, downtime, err := client.StartFirmwareUpgrade(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("StartFirmwareUpgrade failed: %v", err)
|
||||
}
|
||||
|
||||
if uploadURI != "http://192.168.1.100/upload" {
|
||||
t.Errorf("Expected upload URI http://192.168.1.100/upload, got %s", uploadURI)
|
||||
}
|
||||
|
||||
if delay != "PT5S" {
|
||||
t.Errorf("Expected delay PT5S, got %s", delay)
|
||||
}
|
||||
|
||||
if downtime != "PT60S" {
|
||||
t.Errorf("Expected downtime PT60S, got %s", downtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayModeConstants(t *testing.T) {
|
||||
if RelayModeMonostable != "Monostable" {
|
||||
t.Errorf("RelayModeMonostable should be 'Monostable', got %s", RelayModeMonostable)
|
||||
}
|
||||
|
||||
if RelayModeBistable != "Bistable" {
|
||||
t.Errorf("RelayModeBistable should be 'Bistable', got %s", RelayModeBistable)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayIdleStateConstants(t *testing.T) {
|
||||
if RelayIdleStateClosed != "closed" {
|
||||
t.Errorf("RelayIdleStateClosed should be 'closed', got %s", RelayIdleStateClosed)
|
||||
}
|
||||
|
||||
if RelayIdleStateOpen != "open" {
|
||||
t.Errorf("RelayIdleStateOpen should be 'open', got %s", RelayIdleStateOpen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayLogicalStateConstants(t *testing.T) {
|
||||
if RelayLogicalStateActive != "active" {
|
||||
t.Errorf("RelayLogicalStateActive should be 'active', got %s", RelayLogicalStateActive)
|
||||
}
|
||||
|
||||
if RelayLogicalStateInactive != "inactive" {
|
||||
t.Errorf("RelayLogicalStateInactive should be 'inactive', got %s", RelayLogicalStateInactive)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogTypeConstants(t *testing.T) {
|
||||
if SystemLogTypeSystem != "System" {
|
||||
t.Errorf("SystemLogTypeSystem should be 'System', got %s", SystemLogTypeSystem)
|
||||
}
|
||||
|
||||
if SystemLogTypeAccess != "Access" {
|
||||
t.Errorf("SystemLogTypeAccess should be 'Access', got %s", SystemLogTypeAccess)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFactoryDefaultTypeConstants(t *testing.T) {
|
||||
if FactoryDefaultHard != "Hard" {
|
||||
t.Errorf("FactoryDefaultHard should be 'Hard', got %s", FactoryDefaultHard)
|
||||
}
|
||||
|
||||
if FactoryDefaultSoft != "Soft" {
|
||||
t.Errorf("FactoryDefaultSoft should be 'Soft', got %s", FactoryDefaultSoft)
|
||||
}
|
||||
}
|
||||
@@ -1,597 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test device information from real camera:
|
||||
// Manufacturer: Bosch
|
||||
// Model: FLEXIDOME indoor 5100i IR
|
||||
// Firmware: 8.71.0066
|
||||
// Serial Number: 404754734001050102
|
||||
// Hardware ID: F000B543
|
||||
|
||||
// TestGetDeviceInformation_Bosch tests GetDeviceInformation with real camera response.
|
||||
func TestGetDeviceInformation_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetDeviceInformationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Manufacturer>Bosch</tds:Manufacturer>
|
||||
<tds:Model>FLEXIDOME indoor 5100i IR</tds:Model>
|
||||
<tds:FirmwareVersion>8.71.0066</tds:FirmwareVersion>
|
||||
<tds:SerialNumber>404754734001050102</tds:SerialNumber>
|
||||
<tds:HardwareId>F000B543</tds:HardwareId>
|
||||
</tds:GetDeviceInformationResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetDeviceInformation") {
|
||||
t.Errorf("Request should contain GetDeviceInformation, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDeviceInformation() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if info.Manufacturer != "Bosch" {
|
||||
t.Errorf("Expected Manufacturer=Bosch (Bosch FLEXIDOME), got %s", info.Manufacturer)
|
||||
}
|
||||
if info.Model != "FLEXIDOME indoor 5100i IR" {
|
||||
t.Errorf("Expected Model=FLEXIDOME indoor 5100i IR (Bosch FLEXIDOME), got %s", info.Model)
|
||||
}
|
||||
if info.FirmwareVersion != "8.71.0066" {
|
||||
t.Errorf("Expected FirmwareVersion=8.71.0066 (Bosch FLEXIDOME), got %s", info.FirmwareVersion)
|
||||
}
|
||||
if info.SerialNumber != "404754734001050102" {
|
||||
t.Errorf("Expected SerialNumber=404754734001050102 (Bosch FLEXIDOME), got %s", info.SerialNumber)
|
||||
}
|
||||
if info.HardwareID != "F000B543" {
|
||||
t.Errorf("Expected HardwareID=F000B543 (Bosch FLEXIDOME), got %s", info.HardwareID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetCapabilities_Bosch tests GetCapabilities with real camera response.
|
||||
func TestGetCapabilities_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCapabilitiesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Capabilities>
|
||||
<tds:Device>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/device_service</tds:XAddr>
|
||||
<tds:Network>
|
||||
<tt:IPFilter xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:IPFilter>
|
||||
<tt:ZeroConfiguration xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:ZeroConfiguration>
|
||||
<tt:IPVersion6 xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:IPVersion6>
|
||||
<tt:DynDNS xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DynDNS>
|
||||
</tds:Network>
|
||||
<tds:System>
|
||||
<tt:DiscoveryResolve xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DiscoveryResolve>
|
||||
<tt:DiscoveryBye xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DiscoveryBye>
|
||||
<tt:RemoteDiscovery xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RemoteDiscovery>
|
||||
<tt:SystemBackup xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SystemBackup>
|
||||
<tt:SystemLogging xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SystemLogging>
|
||||
<tt:FirmwareUpgrade xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:FirmwareUpgrade>
|
||||
<tt:SupportedVersions xmlns:tt="http://www.onvif.org/ver10/schema">1 2</tt:SupportedVersions>
|
||||
</tds:System>
|
||||
<tds:IO>
|
||||
<tt:InputConnectors xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:InputConnectors>
|
||||
<tt:RelayOutputs xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:RelayOutputs>
|
||||
</tds:IO>
|
||||
<tds:Security>
|
||||
<tt:TLS1.1 xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:TLS1.1>
|
||||
<tt:TLS1.2 xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:TLS1.2>
|
||||
<tt:OnboardKeyGeneration xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:OnboardKeyGeneration>
|
||||
<tt:AccessPolicyConfig xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:AccessPolicyConfig>
|
||||
<tt:X509Token xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:X509Token>
|
||||
<tt:SAMLToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SAMLToken>
|
||||
<tt:KerberosToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:KerberosToken>
|
||||
<tt:RELToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RELToken>
|
||||
</tds:Security>
|
||||
</tds:Device>
|
||||
<tds:Media>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/media_service</tds:XAddr>
|
||||
<tds:StreamingCapabilities>
|
||||
<tt:RTPMulticast xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:RTPMulticast>
|
||||
<tt:RTP_TCP xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RTP_TCP>
|
||||
<tt:RTP_RTSP_TCP xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:RTP_RTSP_TCP>
|
||||
</tds:StreamingCapabilities>
|
||||
</tds:Media>
|
||||
<tds:Imaging>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/imaging_service</tds:XAddr>
|
||||
</tds:Imaging>
|
||||
<tds:Events>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/event_service</tds:XAddr>
|
||||
<tds:WSSubscriptionPolicySupport>false</tds:WSSubscriptionPolicySupport>
|
||||
<tds:WSPullPointSupport>false</tds:WSPullPointSupport>
|
||||
<tds:WSPausableSubscriptionSupport>false</tds:WSPausableSubscriptionSupport>
|
||||
</tds:Events>
|
||||
<tds:Analytics>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/analytics_service</tds:XAddr>
|
||||
<tds:RuleSupport>true</tds:RuleSupport>
|
||||
<tds:AnalyticsModuleSupport>true</tds:AnalyticsModuleSupport>
|
||||
</tds:Analytics>
|
||||
</tds:Capabilities>
|
||||
</tds:GetCapabilitiesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetCapabilities") {
|
||||
t.Errorf("Request should contain GetCapabilities, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCapabilities() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if caps.Device == nil {
|
||||
t.Fatal("Expected Device capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(caps.Device.XAddr, "device_service") {
|
||||
t.Errorf("Expected device service XAddr from Bosch FLEXIDOME, got %s", caps.Device.XAddr)
|
||||
}
|
||||
if caps.Device.Network == nil {
|
||||
t.Fatal("Expected Network capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Device.Network.ZeroConfiguration {
|
||||
t.Error("Expected ZeroConfiguration=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Device.Security == nil {
|
||||
t.Fatal("Expected Security capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Device.Security.TLS12 {
|
||||
t.Error("Expected TLS12=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Media == nil {
|
||||
t.Fatal("Expected Media capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(caps.Media.XAddr, "media_service") {
|
||||
t.Errorf("Expected media service XAddr from Bosch FLEXIDOME, got %s", caps.Media.XAddr)
|
||||
}
|
||||
if caps.Media.StreamingCapabilities == nil {
|
||||
t.Fatal("Expected StreamingCapabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Media.StreamingCapabilities.RTPMulticast {
|
||||
t.Error("Expected RTPMulticast=true from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetServices_Bosch tests GetServices with real camera response.
|
||||
func TestGetServices_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetServicesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/device/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/device_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">3</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/media/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/media_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">3</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/events/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/event_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">4</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
</tds:GetServicesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetServices") {
|
||||
t.Errorf("Request should contain GetServices, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
services, err := client.GetServices(ctx, false)
|
||||
if err != nil {
|
||||
t.Fatalf("GetServices() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(services) == 0 {
|
||||
t.Fatal("Expected at least one service from Bosch FLEXIDOME")
|
||||
}
|
||||
|
||||
// Check for Device service
|
||||
foundDevice := false
|
||||
for _, svc := range services {
|
||||
if svc.Namespace == "http://www.onvif.org/ver10/device/wsdl" {
|
||||
foundDevice = true
|
||||
if svc.Version.Major != 1 || svc.Version.Minor != 3 {
|
||||
t.Errorf("Expected Device service version 1.3 (Bosch FLEXIDOME), got %d.%d", svc.Version.Major, svc.Version.Minor)
|
||||
}
|
||||
if !strings.Contains(svc.XAddr, "device_service") {
|
||||
t.Errorf("Expected device_service in XAddr (Bosch FLEXIDOME), got %s", svc.XAddr)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundDevice {
|
||||
t.Error("Expected Device service from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetServiceCapabilities_Bosch tests GetServiceCapabilities with real camera response.
|
||||
func TestGetServiceCapabilities_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
// Note: Uses attributes, not child elements
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetServiceCapabilitiesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Capabilities>
|
||||
<tds:Network IPFilter="false" ZeroConfiguration="true" IPVersion6="false" DynDNS="false"/>
|
||||
<tds:System DiscoveryResolve="false" DiscoveryBye="false" RemoteDiscovery="false" SystemBackup="false" SystemLogging="false" FirmwareUpgrade="false"/>
|
||||
<tds:Security TLS1.1="false" TLS1.2="true" OnboardKeyGeneration="false" AccessPolicyConfig="false"/>
|
||||
</tds:Capabilities>
|
||||
</tds:GetServiceCapabilitiesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetServiceCapabilities") {
|
||||
t.Errorf("Request should contain GetServiceCapabilities, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
caps, err := client.GetServiceCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetServiceCapabilities() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if caps.Network == nil {
|
||||
t.Fatal("Expected Network capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Network.ZeroConfiguration {
|
||||
t.Error("Expected ZeroConfiguration=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Security == nil {
|
||||
t.Fatal("Expected Security capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Security.TLS12 {
|
||||
t.Error("Expected TLS12=true from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetSystemDateAndTime_Bosch tests GetSystemDateAndTime with real camera response.
|
||||
func TestGetSystemDateAndTime_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetSystemDateAndTimeResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:SystemDateAndTime>
|
||||
<tt:DateTimeType xmlns:tt="http://www.onvif.org/ver10/schema">Manual</tt:DateTimeType>
|
||||
<tt:DaylightSaving xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DaylightSaving>
|
||||
<tt:TimeZone>
|
||||
<tt:TZ xmlns:tt="http://www.onvif.org/ver10/schema">CST6CDT</tt:TZ>
|
||||
</tt:TimeZone>
|
||||
<tt:UTCDateTime>
|
||||
<tt:Time>
|
||||
<tt:Hour xmlns:tt="http://www.onvif.org/ver10/schema">4</tt:Hour>
|
||||
<tt:Minute xmlns:tt="http://www.onvif.org/ver10/schema">56</tt:Minute>
|
||||
<tt:Second xmlns:tt="http://www.onvif.org/ver10/schema">14</tt:Second>
|
||||
</tt:Time>
|
||||
<tt:Date>
|
||||
<tt:Year xmlns:tt="http://www.onvif.org/ver10/schema">2025</tt:Year>
|
||||
<tt:Month xmlns:tt="http://www.onvif.org/ver10/schema">12</tt:Month>
|
||||
<tt:Day xmlns:tt="http://www.onvif.org/ver10/schema">2</tt:Day>
|
||||
</tt:Date>
|
||||
</tt:UTCDateTime>
|
||||
</tds:SystemDateAndTime>
|
||||
</tds:GetSystemDateAndTimeResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetSystemDateAndTime") {
|
||||
t.Errorf("Request should contain GetSystemDateAndTime, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
dateTime, err := client.GetSystemDateAndTime(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemDateAndTime() failed: %v", err)
|
||||
}
|
||||
|
||||
// GetSystemDateAndTime returns interface{} - just verify no error
|
||||
// The actual structure depends on the camera's response format
|
||||
_ = dateTime // Acknowledge we received a response
|
||||
}
|
||||
|
||||
// TestGetHostname_Bosch tests GetHostname with real camera response.
|
||||
func TestGetHostname_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetHostnameResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:HostnameInformation>
|
||||
<tt:FromDHCP xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:FromDHCP>
|
||||
<tt:Name xmlns:tt="http://www.onvif.org/ver10/schema">BOSCH-404754734001050102</tt:Name>
|
||||
</tds:HostnameInformation>
|
||||
</tds:GetHostnameResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetHostname") {
|
||||
t.Errorf("Request should contain GetHostname, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
hostname, err := client.GetHostname(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetHostname() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if hostname == nil {
|
||||
t.Fatal("Expected HostnameInformation from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(hostname.Name, "BOSCH") {
|
||||
t.Errorf("Expected hostname to contain BOSCH (Bosch FLEXIDOME), got %s", hostname.Name)
|
||||
}
|
||||
if hostname.FromDHCP {
|
||||
t.Error("Expected FromDHCP=false from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetScopes_Bosch tests GetScopes with real camera response.
|
||||
func TestGetScopes_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/name/BOSCH-404754734001050102</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/location/</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/hardware/F000B543</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
</tds:GetScopesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetScopes") {
|
||||
t.Errorf("Request should contain GetScopes, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes, err := client.GetScopes(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetScopes() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(scopes) == 0 {
|
||||
t.Fatal("Expected at least one scope from Bosch FLEXIDOME")
|
||||
}
|
||||
|
||||
// Check for hardware scope
|
||||
foundHardware := false
|
||||
for _, scope := range scopes {
|
||||
if strings.Contains(scope.ScopeItem, "hardware") {
|
||||
foundHardware = true
|
||||
if !strings.Contains(scope.ScopeItem, "F000B543") {
|
||||
t.Errorf("Expected hardware ID F000B543 in scope (Bosch FLEXIDOME), got %s", scope.ScopeItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundHardware {
|
||||
t.Error("Expected hardware scope from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetUsers_Bosch tests GetUsers with real camera response.
|
||||
func TestGetUsers_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetUsersResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:User>
|
||||
<tt:Username xmlns:tt="http://www.onvif.org/ver10/schema">service</tt:Username>
|
||||
<tt:UserLevel xmlns:tt="http://www.onvif.org/ver10/schema">Administrator</tt:UserLevel>
|
||||
</tds:User>
|
||||
</tds:GetUsersResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetUsers") {
|
||||
t.Errorf("Request should contain GetUsers, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
users, err := client.GetUsers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetUsers() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(users) == 0 {
|
||||
t.Fatal("Expected at least one user from Bosch FLEXIDOME")
|
||||
}
|
||||
if users[0].Username != "service" {
|
||||
t.Errorf("Expected username=service (Bosch FLEXIDOME), got %s", users[0].Username)
|
||||
}
|
||||
if users[0].UserLevel != "Administrator" {
|
||||
t.Errorf("Expected UserLevel=Administrator (Bosch FLEXIDOME), got %s", users[0].UserLevel)
|
||||
}
|
||||
}
|
||||
@@ -1,597 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test device information from real camera:
|
||||
// Manufacturer: Bosch
|
||||
// Model: FLEXIDOME indoor 5100i IR
|
||||
// Firmware: 8.71.0066
|
||||
// Serial Number: 404754734001050102
|
||||
// Hardware ID: F000B543
|
||||
|
||||
// TestGetDeviceInformation_Bosch tests GetDeviceInformation with real camera response.
|
||||
func TestGetDeviceInformation_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetDeviceInformationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Manufacturer>Bosch</tds:Manufacturer>
|
||||
<tds:Model>FLEXIDOME indoor 5100i IR</tds:Model>
|
||||
<tds:FirmwareVersion>8.71.0066</tds:FirmwareVersion>
|
||||
<tds:SerialNumber>404754734001050102</tds:SerialNumber>
|
||||
<tds:HardwareId>F000B543</tds:HardwareId>
|
||||
</tds:GetDeviceInformationResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetDeviceInformation") {
|
||||
t.Errorf("Request should contain GetDeviceInformation, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
info, err := client.GetDeviceInformation(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDeviceInformation() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if info.Manufacturer != "Bosch" {
|
||||
t.Errorf("Expected Manufacturer=Bosch (Bosch FLEXIDOME), got %s", info.Manufacturer)
|
||||
}
|
||||
if info.Model != "FLEXIDOME indoor 5100i IR" {
|
||||
t.Errorf("Expected Model=FLEXIDOME indoor 5100i IR (Bosch FLEXIDOME), got %s", info.Model)
|
||||
}
|
||||
if info.FirmwareVersion != "8.71.0066" {
|
||||
t.Errorf("Expected FirmwareVersion=8.71.0066 (Bosch FLEXIDOME), got %s", info.FirmwareVersion)
|
||||
}
|
||||
if info.SerialNumber != "404754734001050102" {
|
||||
t.Errorf("Expected SerialNumber=404754734001050102 (Bosch FLEXIDOME), got %s", info.SerialNumber)
|
||||
}
|
||||
if info.HardwareID != "F000B543" {
|
||||
t.Errorf("Expected HardwareID=F000B543 (Bosch FLEXIDOME), got %s", info.HardwareID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetCapabilities_Bosch tests GetCapabilities with real camera response.
|
||||
func TestGetCapabilities_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetCapabilitiesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Capabilities>
|
||||
<tds:Device>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/device_service</tds:XAddr>
|
||||
<tds:Network>
|
||||
<tt:IPFilter xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:IPFilter>
|
||||
<tt:ZeroConfiguration xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:ZeroConfiguration>
|
||||
<tt:IPVersion6 xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:IPVersion6>
|
||||
<tt:DynDNS xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DynDNS>
|
||||
</tds:Network>
|
||||
<tds:System>
|
||||
<tt:DiscoveryResolve xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DiscoveryResolve>
|
||||
<tt:DiscoveryBye xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DiscoveryBye>
|
||||
<tt:RemoteDiscovery xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RemoteDiscovery>
|
||||
<tt:SystemBackup xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SystemBackup>
|
||||
<tt:SystemLogging xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SystemLogging>
|
||||
<tt:FirmwareUpgrade xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:FirmwareUpgrade>
|
||||
<tt:SupportedVersions xmlns:tt="http://www.onvif.org/ver10/schema">1 2</tt:SupportedVersions>
|
||||
</tds:System>
|
||||
<tds:IO>
|
||||
<tt:InputConnectors xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:InputConnectors>
|
||||
<tt:RelayOutputs xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:RelayOutputs>
|
||||
</tds:IO>
|
||||
<tds:Security>
|
||||
<tt:TLS1.1 xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:TLS1.1>
|
||||
<tt:TLS1.2 xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:TLS1.2>
|
||||
<tt:OnboardKeyGeneration xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:OnboardKeyGeneration>
|
||||
<tt:AccessPolicyConfig xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:AccessPolicyConfig>
|
||||
<tt:X509Token xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:X509Token>
|
||||
<tt:SAMLToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:SAMLToken>
|
||||
<tt:KerberosToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:KerberosToken>
|
||||
<tt:RELToken xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RELToken>
|
||||
</tds:Security>
|
||||
</tds:Device>
|
||||
<tds:Media>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/media_service</tds:XAddr>
|
||||
<tds:StreamingCapabilities>
|
||||
<tt:RTPMulticast xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:RTPMulticast>
|
||||
<tt:RTP_TCP xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:RTP_TCP>
|
||||
<tt:RTP_RTSP_TCP xmlns:tt="http://www.onvif.org/ver10/schema">true</tt:RTP_RTSP_TCP>
|
||||
</tds:StreamingCapabilities>
|
||||
</tds:Media>
|
||||
<tds:Imaging>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/imaging_service</tds:XAddr>
|
||||
</tds:Imaging>
|
||||
<tds:Events>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/event_service</tds:XAddr>
|
||||
<tds:WSSubscriptionPolicySupport>false</tds:WSSubscriptionPolicySupport>
|
||||
<tds:WSPullPointSupport>false</tds:WSPullPointSupport>
|
||||
<tds:WSPausableSubscriptionSupport>false</tds:WSPausableSubscriptionSupport>
|
||||
</tds:Events>
|
||||
<tds:Analytics>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/analytics_service</tds:XAddr>
|
||||
<tds:RuleSupport>true</tds:RuleSupport>
|
||||
<tds:AnalyticsModuleSupport>true</tds:AnalyticsModuleSupport>
|
||||
</tds:Analytics>
|
||||
</tds:Capabilities>
|
||||
</tds:GetCapabilitiesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetCapabilities") {
|
||||
t.Errorf("Request should contain GetCapabilities, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
caps, err := client.GetCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCapabilities() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if caps.Device == nil {
|
||||
t.Fatal("Expected Device capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(caps.Device.XAddr, "device_service") {
|
||||
t.Errorf("Expected device service XAddr from Bosch FLEXIDOME, got %s", caps.Device.XAddr)
|
||||
}
|
||||
if caps.Device.Network == nil {
|
||||
t.Fatal("Expected Network capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Device.Network.ZeroConfiguration {
|
||||
t.Error("Expected ZeroConfiguration=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Device.Security == nil {
|
||||
t.Fatal("Expected Security capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Device.Security.TLS12 {
|
||||
t.Error("Expected TLS12=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Media == nil {
|
||||
t.Fatal("Expected Media capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(caps.Media.XAddr, "media_service") {
|
||||
t.Errorf("Expected media service XAddr from Bosch FLEXIDOME, got %s", caps.Media.XAddr)
|
||||
}
|
||||
if caps.Media.StreamingCapabilities == nil {
|
||||
t.Fatal("Expected StreamingCapabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Media.StreamingCapabilities.RTPMulticast {
|
||||
t.Error("Expected RTPMulticast=true from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetServices_Bosch tests GetServices with real camera response.
|
||||
func TestGetServices_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetServicesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/device/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/device_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">3</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/media/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/media_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">3</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
<tds:Service>
|
||||
<tds:Namespace>http://www.onvif.org/ver10/events/wsdl</tds:Namespace>
|
||||
<tds:XAddr>http://192.168.1.201/onvif/event_service</tds:XAddr>
|
||||
<tds:Version>
|
||||
<tt:Major xmlns:tt="http://www.onvif.org/ver10/schema">1</tt:Major>
|
||||
<tt:Minor xmlns:tt="http://www.onvif.org/ver10/schema">4</tt:Minor>
|
||||
</tds:Version>
|
||||
</tds:Service>
|
||||
</tds:GetServicesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetServices") {
|
||||
t.Errorf("Request should contain GetServices, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
services, err := client.GetServices(ctx, false)
|
||||
if err != nil {
|
||||
t.Fatalf("GetServices() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(services) == 0 {
|
||||
t.Fatal("Expected at least one service from Bosch FLEXIDOME")
|
||||
}
|
||||
|
||||
// Check for Device service
|
||||
foundDevice := false
|
||||
for _, svc := range services {
|
||||
if svc.Namespace == "http://www.onvif.org/ver10/device/wsdl" {
|
||||
foundDevice = true
|
||||
if svc.Version.Major != 1 || svc.Version.Minor != 3 {
|
||||
t.Errorf("Expected Device service version 1.3 (Bosch FLEXIDOME), got %d.%d", svc.Version.Major, svc.Version.Minor)
|
||||
}
|
||||
if !strings.Contains(svc.XAddr, "device_service") {
|
||||
t.Errorf("Expected device_service in XAddr (Bosch FLEXIDOME), got %s", svc.XAddr)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundDevice {
|
||||
t.Error("Expected Device service from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetServiceCapabilities_Bosch tests GetServiceCapabilities with real camera response.
|
||||
func TestGetServiceCapabilities_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
// Note: Uses attributes, not child elements
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetServiceCapabilitiesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Capabilities>
|
||||
<tds:Network IPFilter="false" ZeroConfiguration="true" IPVersion6="false" DynDNS="false"/>
|
||||
<tds:System DiscoveryResolve="false" DiscoveryBye="false" RemoteDiscovery="false" SystemBackup="false" SystemLogging="false" FirmwareUpgrade="false"/>
|
||||
<tds:Security TLS1.1="false" TLS1.2="true" OnboardKeyGeneration="false" AccessPolicyConfig="false"/>
|
||||
</tds:Capabilities>
|
||||
</tds:GetServiceCapabilitiesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetServiceCapabilities") {
|
||||
t.Errorf("Request should contain GetServiceCapabilities, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
caps, err := client.GetServiceCapabilities(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetServiceCapabilities() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if caps.Network == nil {
|
||||
t.Fatal("Expected Network capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Network.ZeroConfiguration {
|
||||
t.Error("Expected ZeroConfiguration=true from Bosch FLEXIDOME")
|
||||
}
|
||||
if caps.Security == nil {
|
||||
t.Fatal("Expected Security capabilities from Bosch FLEXIDOME")
|
||||
}
|
||||
if !caps.Security.TLS12 {
|
||||
t.Error("Expected TLS12=true from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetSystemDateAndTime_Bosch tests GetSystemDateAndTime with real camera response.
|
||||
func TestGetSystemDateAndTime_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetSystemDateAndTimeResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:SystemDateAndTime>
|
||||
<tt:DateTimeType xmlns:tt="http://www.onvif.org/ver10/schema">Manual</tt:DateTimeType>
|
||||
<tt:DaylightSaving xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:DaylightSaving>
|
||||
<tt:TimeZone>
|
||||
<tt:TZ xmlns:tt="http://www.onvif.org/ver10/schema">CST6CDT</tt:TZ>
|
||||
</tt:TimeZone>
|
||||
<tt:UTCDateTime>
|
||||
<tt:Time>
|
||||
<tt:Hour xmlns:tt="http://www.onvif.org/ver10/schema">4</tt:Hour>
|
||||
<tt:Minute xmlns:tt="http://www.onvif.org/ver10/schema">56</tt:Minute>
|
||||
<tt:Second xmlns:tt="http://www.onvif.org/ver10/schema">14</tt:Second>
|
||||
</tt:Time>
|
||||
<tt:Date>
|
||||
<tt:Year xmlns:tt="http://www.onvif.org/ver10/schema">2025</tt:Year>
|
||||
<tt:Month xmlns:tt="http://www.onvif.org/ver10/schema">12</tt:Month>
|
||||
<tt:Day xmlns:tt="http://www.onvif.org/ver10/schema">2</tt:Day>
|
||||
</tt:Date>
|
||||
</tt:UTCDateTime>
|
||||
</tds:SystemDateAndTime>
|
||||
</tds:GetSystemDateAndTimeResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetSystemDateAndTime") {
|
||||
t.Errorf("Request should contain GetSystemDateAndTime, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
dateTime, err := client.GetSystemDateAndTime(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemDateAndTime() failed: %v", err)
|
||||
}
|
||||
|
||||
// GetSystemDateAndTime returns interface{} - just verify no error
|
||||
// The actual structure depends on the camera's response format
|
||||
_ = dateTime // Acknowledge we received a response
|
||||
}
|
||||
|
||||
// TestGetHostname_Bosch tests GetHostname with real camera response.
|
||||
func TestGetHostname_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetHostnameResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:HostnameInformation>
|
||||
<tt:FromDHCP xmlns:tt="http://www.onvif.org/ver10/schema">false</tt:FromDHCP>
|
||||
<tt:Name xmlns:tt="http://www.onvif.org/ver10/schema">BOSCH-404754734001050102</tt:Name>
|
||||
</tds:HostnameInformation>
|
||||
</tds:GetHostnameResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetHostname") {
|
||||
t.Errorf("Request should contain GetHostname, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
hostname, err := client.GetHostname(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetHostname() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if hostname == nil {
|
||||
t.Fatal("Expected HostnameInformation from Bosch FLEXIDOME")
|
||||
}
|
||||
if !strings.Contains(hostname.Name, "BOSCH") {
|
||||
t.Errorf("Expected hostname to contain BOSCH (Bosch FLEXIDOME), got %s", hostname.Name)
|
||||
}
|
||||
if hostname.FromDHCP {
|
||||
t.Error("Expected FromDHCP=false from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetScopes_Bosch tests GetScopes with real camera response.
|
||||
func TestGetScopes_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetScopesResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/name/BOSCH-404754734001050102</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/location/</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
<tds:Scopes>
|
||||
<tt:ScopeDef xmlns:tt="http://www.onvif.org/ver10/schema">Fixed</tt:ScopeDef>
|
||||
<tt:ScopeItem xmlns:tt="http://www.onvif.org/ver10/schema">onvif://www.onvif.org/hardware/F000B543</tt:ScopeItem>
|
||||
</tds:Scopes>
|
||||
</tds:GetScopesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetScopes") {
|
||||
t.Errorf("Request should contain GetScopes, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
scopes, err := client.GetScopes(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetScopes() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(scopes) == 0 {
|
||||
t.Fatal("Expected at least one scope from Bosch FLEXIDOME")
|
||||
}
|
||||
|
||||
// Check for hardware scope
|
||||
foundHardware := false
|
||||
for _, scope := range scopes {
|
||||
if strings.Contains(scope.ScopeItem, "hardware") {
|
||||
foundHardware = true
|
||||
if !strings.Contains(scope.ScopeItem, "F000B543") {
|
||||
t.Errorf("Expected hardware ID F000B543 in scope (Bosch FLEXIDOME), got %s", scope.ScopeItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundHardware {
|
||||
t.Error("Expected hardware scope from Bosch FLEXIDOME")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetUsers_Bosch tests GetUsers with real camera response.
|
||||
func TestGetUsers_Bosch(t *testing.T) {
|
||||
// Real SOAP response from Bosch FLEXIDOME indoor 5100i IR (FW: 8.71.0066)
|
||||
realResponse := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
|
||||
<SOAP-ENV:Body>
|
||||
<tds:GetUsersResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:User>
|
||||
<tt:Username xmlns:tt="http://www.onvif.org/ver10/schema">service</tt:Username>
|
||||
<tt:UserLevel xmlns:tt="http://www.onvif.org/ver10/schema">Administrator</tt:UserLevel>
|
||||
</tds:User>
|
||||
</tds:GetUsersResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>`
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read request body: %v", err)
|
||||
}
|
||||
bodyStr := string(body)
|
||||
|
||||
if !strings.Contains(bodyStr, "GetUsers") {
|
||||
t.Errorf("Request should contain GetUsers, got: %s", bodyStr)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(realResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, WithCredentials("service", "Service.1234"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
users, err := client.GetUsers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetUsers() failed: %v", err)
|
||||
}
|
||||
|
||||
// Validate response matches real camera
|
||||
if len(users) == 0 {
|
||||
t.Fatal("Expected at least one user from Bosch FLEXIDOME")
|
||||
}
|
||||
if users[0].Username != "service" {
|
||||
t.Errorf("Expected username=service (Bosch FLEXIDOME), got %s", users[0].Username)
|
||||
}
|
||||
if users[0].UserLevel != "Administrator" {
|
||||
t.Errorf("Expected UserLevel=Administrator (Bosch FLEXIDOME), got %s", users[0].UserLevel)
|
||||
}
|
||||
}
|
||||
@@ -1,527 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// Common XML request/response types for device security operations.
|
||||
// These are defined at package level to avoid repeated inline struct definitions.
|
||||
|
||||
// ipAddressFilterRequest is the common structure for IP address filter SOAP requests.
|
||||
type ipAddressFilterRequest struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address []prefixedIPv4AddressXML `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address []prefixedIPv6AddressXML `xml:"tds:IPv6Address,omitempty"`
|
||||
}
|
||||
|
||||
// prefixedIPv4AddressXML is the XML representation of a prefixed IPv4 address.
|
||||
type prefixedIPv4AddressXML struct {
|
||||
Address string `xml:"tds:Address"`
|
||||
PrefixLength int `xml:"tds:PrefixLength"`
|
||||
}
|
||||
|
||||
// prefixedIPv6AddressXML is the XML representation of a prefixed IPv6 address.
|
||||
type prefixedIPv6AddressXML struct {
|
||||
Address string `xml:"tds:Address"`
|
||||
PrefixLength int `xml:"tds:PrefixLength"`
|
||||
}
|
||||
|
||||
// buildIPAddressFilterRequest converts an IPAddressFilter to the XML request format.
|
||||
// Pre-allocates slices for efficiency when the source length is known.
|
||||
func buildIPAddressFilterRequest(filter *IPAddressFilter) ipAddressFilterRequest {
|
||||
req := ipAddressFilterRequest{
|
||||
Type: string(filter.Type),
|
||||
}
|
||||
|
||||
// Pre-allocate slices with known capacity
|
||||
if len(filter.IPv4Address) > 0 {
|
||||
req.IPv4Address = make([]prefixedIPv4AddressXML, 0, len(filter.IPv4Address))
|
||||
for _, addr := range filter.IPv4Address {
|
||||
req.IPv4Address = append(req.IPv4Address, prefixedIPv4AddressXML(addr))
|
||||
}
|
||||
}
|
||||
|
||||
if len(filter.IPv6Address) > 0 {
|
||||
req.IPv6Address = make([]prefixedIPv6AddressXML, 0, len(filter.IPv6Address))
|
||||
for _, addr := range filter.IPv6Address {
|
||||
req.IPv6Address = append(req.IPv6Address, prefixedIPv6AddressXML(addr))
|
||||
}
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
// newSOAPClient creates a SOAP client with the current client credentials.
|
||||
func (c *Client) newSOAPClient() *soap.Client {
|
||||
username, password := c.GetCredentials()
|
||||
return soap.NewClient(c.httpClient, username, password)
|
||||
}
|
||||
|
||||
// GetRemoteUser returns the configured remote user.
|
||||
func (c *Client) GetRemoteUser(ctx context.Context) (*RemoteUser, error) {
|
||||
type getRemoteUserRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetRemoteUser"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getRemoteUserResponse struct {
|
||||
XMLName xml.Name `xml:"GetRemoteUserResponse"`
|
||||
RemoteUser *struct {
|
||||
Username string `xml:"Username"`
|
||||
Password string `xml:"Password"`
|
||||
UseDerivedPassword bool `xml:"UseDerivedPassword"`
|
||||
} `xml:"RemoteUser"`
|
||||
}
|
||||
|
||||
req := getRemoteUserRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getRemoteUserResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetRemoteUser failed: %w", err)
|
||||
}
|
||||
|
||||
if resp.RemoteUser == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &RemoteUser{
|
||||
Username: resp.RemoteUser.Username,
|
||||
Password: resp.RemoteUser.Password,
|
||||
UseDerivedPassword: resp.RemoteUser.UseDerivedPassword,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetRemoteUser sets the remote user.
|
||||
func (c *Client) SetRemoteUser(ctx context.Context, remoteUser *RemoteUser) error {
|
||||
type remoteUserXML struct {
|
||||
Username string `xml:"tds:Username"`
|
||||
Password string `xml:"tds:Password,omitempty"`
|
||||
UseDerivedPassword bool `xml:"tds:UseDerivedPassword"`
|
||||
}
|
||||
|
||||
type setRemoteUserRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetRemoteUser"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RemoteUser *remoteUserXML `xml:"tds:RemoteUser,omitempty"`
|
||||
}
|
||||
|
||||
req := setRemoteUserRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
if remoteUser != nil {
|
||||
req.RemoteUser = &remoteUserXML{
|
||||
Username: remoteUser.Username,
|
||||
Password: remoteUser.Password,
|
||||
UseDerivedPassword: remoteUser.UseDerivedPassword,
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRemoteUser failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetIPAddressFilter gets the IP address filter settings from a device.
|
||||
func (c *Client) GetIPAddressFilter(ctx context.Context) (*IPAddressFilter, error) {
|
||||
type getIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type prefixedAddressXML struct {
|
||||
Address string `xml:"Address"`
|
||||
PrefixLength int `xml:"PrefixLength"`
|
||||
}
|
||||
|
||||
type getIPAddressFilterResponse struct {
|
||||
XMLName xml.Name `xml:"GetIPAddressFilterResponse"`
|
||||
IPAddressFilter struct {
|
||||
Type string `xml:"Type"`
|
||||
IPv4Address []prefixedAddressXML `xml:"IPv4Address"`
|
||||
IPv6Address []prefixedAddressXML `xml:"IPv6Address"`
|
||||
} `xml:"IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := getIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getIPAddressFilterResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterType(resp.IPAddressFilter.Type),
|
||||
}
|
||||
|
||||
// Pre-allocate slices with known capacity
|
||||
if len(resp.IPAddressFilter.IPv4Address) > 0 {
|
||||
filter.IPv4Address = make([]PrefixedIPv4Address, 0, len(resp.IPAddressFilter.IPv4Address))
|
||||
for _, addr := range resp.IPAddressFilter.IPv4Address {
|
||||
filter.IPv4Address = append(filter.IPv4Address, PrefixedIPv4Address(addr))
|
||||
}
|
||||
}
|
||||
|
||||
if len(resp.IPAddressFilter.IPv6Address) > 0 {
|
||||
filter.IPv6Address = make([]PrefixedIPv6Address, 0, len(resp.IPAddressFilter.IPv6Address))
|
||||
for _, addr := range resp.IPAddressFilter.IPv6Address {
|
||||
filter.IPv6Address = append(filter.IPv6Address, PrefixedIPv6Address(addr))
|
||||
}
|
||||
}
|
||||
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
// SetIPAddressFilter sets the IP address filter settings on a device.
|
||||
func (c *Client) SetIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type setIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := setIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddIPAddressFilter adds an IP filter address to a device.
|
||||
func (c *Client) AddIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type addIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:AddIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := addIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("AddIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveIPAddressFilter deletes an IP filter address from a device.
|
||||
func (c *Client) RemoveIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type removeIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:RemoveIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := removeIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("RemoveIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetZeroConfiguration gets the zero-configuration from a device.
|
||||
func (c *Client) GetZeroConfiguration(ctx context.Context) (*NetworkZeroConfiguration, error) {
|
||||
type getZeroConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetZeroConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getZeroConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetZeroConfigurationResponse"`
|
||||
ZeroConfiguration struct {
|
||||
InterfaceToken string `xml:"InterfaceToken"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
Addresses []string `xml:"Addresses"`
|
||||
} `xml:"ZeroConfiguration"`
|
||||
}
|
||||
|
||||
req := getZeroConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getZeroConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetZeroConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &NetworkZeroConfiguration{
|
||||
InterfaceToken: resp.ZeroConfiguration.InterfaceToken,
|
||||
Enabled: resp.ZeroConfiguration.Enabled,
|
||||
Addresses: resp.ZeroConfiguration.Addresses,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetZeroConfiguration sets the zero-configuration.
|
||||
func (c *Client) SetZeroConfiguration(ctx context.Context, interfaceToken string, enabled bool) error {
|
||||
type setZeroConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetZeroConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
InterfaceToken string `xml:"tds:InterfaceToken"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
}
|
||||
|
||||
req := setZeroConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
InterfaceToken: interfaceToken,
|
||||
Enabled: enabled,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetZeroConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDynamicDNS gets the dynamic DNS settings from a device.
|
||||
func (c *Client) GetDynamicDNS(ctx context.Context) (*DynamicDNSInformation, error) {
|
||||
type getDynamicDNSRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetDynamicDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getDynamicDNSResponse struct {
|
||||
XMLName xml.Name `xml:"GetDynamicDNSResponse"`
|
||||
DynamicDNSInformation struct {
|
||||
Type string `xml:"Type"`
|
||||
Name string `xml:"Name"`
|
||||
TTL string `xml:"TTL"`
|
||||
} `xml:"DynamicDNSInformation"`
|
||||
}
|
||||
|
||||
req := getDynamicDNSRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getDynamicDNSResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetDynamicDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return &DynamicDNSInformation{
|
||||
Type: DynamicDNSType(resp.DynamicDNSInformation.Type),
|
||||
Name: resp.DynamicDNSInformation.Name,
|
||||
// TTL would need duration parsing
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetDynamicDNS sets the dynamic DNS settings on a device.
|
||||
func (c *Client) SetDynamicDNS(ctx context.Context, dnsType DynamicDNSType, name string) error {
|
||||
type setDynamicDNSRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetDynamicDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Type DynamicDNSType `xml:"tds:Type"`
|
||||
Name string `xml:"tds:Name,omitempty"`
|
||||
}
|
||||
|
||||
req := setDynamicDNSRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Type: dnsType,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetDynamicDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPasswordComplexityConfiguration retrieves the current password complexity configuration settings.
|
||||
func (c *Client) GetPasswordComplexityConfiguration(ctx context.Context) (*PasswordComplexityConfiguration, error) {
|
||||
type getPasswordComplexityConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetPasswordComplexityConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getPasswordComplexityConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetPasswordComplexityConfigurationResponse"`
|
||||
MinLen int `xml:"MinLen"`
|
||||
Uppercase int `xml:"Uppercase"`
|
||||
Number int `xml:"Number"`
|
||||
SpecialChars int `xml:"SpecialChars"`
|
||||
BlockUsernameOccurrence bool `xml:"BlockUsernameOccurrence"`
|
||||
PolicyConfigurationLocked bool `xml:"PolicyConfigurationLocked"`
|
||||
}
|
||||
|
||||
req := getPasswordComplexityConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getPasswordComplexityConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetPasswordComplexityConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &PasswordComplexityConfiguration{
|
||||
MinLen: resp.MinLen,
|
||||
Uppercase: resp.Uppercase,
|
||||
Number: resp.Number,
|
||||
SpecialChars: resp.SpecialChars,
|
||||
BlockUsernameOccurrence: resp.BlockUsernameOccurrence,
|
||||
PolicyConfigurationLocked: resp.PolicyConfigurationLocked,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetPasswordComplexityConfiguration allows setting of the password complexity configuration.
|
||||
func (c *Client) SetPasswordComplexityConfiguration(
|
||||
ctx context.Context,
|
||||
config *PasswordComplexityConfiguration,
|
||||
) error {
|
||||
type setPasswordComplexityConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetPasswordComplexityConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
MinLen int `xml:"tds:MinLen,omitempty"`
|
||||
Uppercase int `xml:"tds:Uppercase,omitempty"`
|
||||
Number int `xml:"tds:Number,omitempty"`
|
||||
SpecialChars int `xml:"tds:SpecialChars,omitempty"`
|
||||
BlockUsernameOccurrence bool `xml:"tds:BlockUsernameOccurrence,omitempty"`
|
||||
PolicyConfigurationLocked bool `xml:"tds:PolicyConfigurationLocked,omitempty"`
|
||||
}
|
||||
|
||||
req := setPasswordComplexityConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
MinLen: config.MinLen,
|
||||
Uppercase: config.Uppercase,
|
||||
Number: config.Number,
|
||||
SpecialChars: config.SpecialChars,
|
||||
BlockUsernameOccurrence: config.BlockUsernameOccurrence,
|
||||
PolicyConfigurationLocked: config.PolicyConfigurationLocked,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetPasswordComplexityConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPasswordHistoryConfiguration retrieves the current password history configuration settings.
|
||||
func (c *Client) GetPasswordHistoryConfiguration(ctx context.Context) (*PasswordHistoryConfiguration, error) {
|
||||
type getPasswordHistoryConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetPasswordHistoryConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getPasswordHistoryConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetPasswordHistoryConfigurationResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
Length int `xml:"Length"`
|
||||
}
|
||||
|
||||
req := getPasswordHistoryConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getPasswordHistoryConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetPasswordHistoryConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &PasswordHistoryConfiguration{
|
||||
Enabled: resp.Enabled,
|
||||
Length: resp.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetPasswordHistoryConfiguration allows setting of the password history configuration.
|
||||
func (c *Client) SetPasswordHistoryConfiguration(ctx context.Context, config *PasswordHistoryConfiguration) error {
|
||||
type setPasswordHistoryConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetPasswordHistoryConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
Length int `xml:"tds:Length"`
|
||||
}
|
||||
|
||||
req := setPasswordHistoryConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: config.Enabled,
|
||||
Length: config.Length,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetPasswordHistoryConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAuthFailureWarningConfiguration retrieves the current authentication failure warning configuration.
|
||||
func (c *Client) GetAuthFailureWarningConfiguration(ctx context.Context) (*AuthFailureWarningConfiguration, error) {
|
||||
type getAuthFailureWarningConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetAuthFailureWarningConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getAuthFailureWarningConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetAuthFailureWarningConfigurationResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
MonitorPeriod int `xml:"MonitorPeriod"`
|
||||
MaxAuthFailures int `xml:"MaxAuthFailures"`
|
||||
}
|
||||
|
||||
req := getAuthFailureWarningConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getAuthFailureWarningConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetAuthFailureWarningConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &AuthFailureWarningConfiguration{
|
||||
Enabled: resp.Enabled,
|
||||
MonitorPeriod: resp.MonitorPeriod,
|
||||
MaxAuthFailures: resp.MaxAuthFailures,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetAuthFailureWarningConfiguration allows setting of the authentication failure warning configuration.
|
||||
func (c *Client) SetAuthFailureWarningConfiguration(
|
||||
ctx context.Context,
|
||||
config *AuthFailureWarningConfiguration,
|
||||
) error {
|
||||
type setAuthFailureWarningConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetAuthFailureWarningConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
MonitorPeriod int `xml:"tds:MonitorPeriod"`
|
||||
MaxAuthFailures int `xml:"tds:MaxAuthFailures"`
|
||||
}
|
||||
|
||||
req := setAuthFailureWarningConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: config.Enabled,
|
||||
MonitorPeriod: config.MonitorPeriod,
|
||||
MaxAuthFailures: config.MaxAuthFailures,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetAuthFailureWarningConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,539 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// Common XML request/response types for device security operations.
|
||||
// These are defined at package level to avoid repeated inline struct definitions.
|
||||
|
||||
// ipAddressFilterRequest is the common structure for IP address filter SOAP requests.
|
||||
type ipAddressFilterRequest struct {
|
||||
Type string `xml:"tds:Type"`
|
||||
IPv4Address []prefixedIPv4AddressXML `xml:"tds:IPv4Address,omitempty"`
|
||||
IPv6Address []prefixedIPv6AddressXML `xml:"tds:IPv6Address,omitempty"`
|
||||
}
|
||||
|
||||
// prefixedIPv4AddressXML is the XML representation of a prefixed IPv4 address.
|
||||
type prefixedIPv4AddressXML struct {
|
||||
Address string `xml:"tds:Address"`
|
||||
PrefixLength int `xml:"tds:PrefixLength"`
|
||||
}
|
||||
|
||||
// prefixedIPv6AddressXML is the XML representation of a prefixed IPv6 address.
|
||||
type prefixedIPv6AddressXML struct {
|
||||
Address string `xml:"tds:Address"`
|
||||
PrefixLength int `xml:"tds:PrefixLength"`
|
||||
}
|
||||
|
||||
// buildIPAddressFilterRequest converts an IPAddressFilter to the XML request format.
|
||||
// Pre-allocates slices for efficiency when the source length is known.
|
||||
func buildIPAddressFilterRequest(filter *IPAddressFilter) ipAddressFilterRequest {
|
||||
req := ipAddressFilterRequest{
|
||||
Type: string(filter.Type),
|
||||
}
|
||||
|
||||
// Pre-allocate slices with known capacity
|
||||
if len(filter.IPv4Address) > 0 {
|
||||
req.IPv4Address = make([]prefixedIPv4AddressXML, 0, len(filter.IPv4Address))
|
||||
for _, addr := range filter.IPv4Address {
|
||||
req.IPv4Address = append(req.IPv4Address, prefixedIPv4AddressXML{
|
||||
Address: addr.Address,
|
||||
PrefixLength: addr.PrefixLength,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(filter.IPv6Address) > 0 {
|
||||
req.IPv6Address = make([]prefixedIPv6AddressXML, 0, len(filter.IPv6Address))
|
||||
for _, addr := range filter.IPv6Address {
|
||||
req.IPv6Address = append(req.IPv6Address, prefixedIPv6AddressXML{
|
||||
Address: addr.Address,
|
||||
PrefixLength: addr.PrefixLength,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
// newSOAPClient creates a SOAP client with the current client credentials.
|
||||
func (c *Client) newSOAPClient() *soap.Client {
|
||||
username, password := c.GetCredentials()
|
||||
return soap.NewClient(c.httpClient, username, password)
|
||||
}
|
||||
|
||||
// GetRemoteUser returns the configured remote user.
|
||||
func (c *Client) GetRemoteUser(ctx context.Context) (*RemoteUser, error) {
|
||||
type getRemoteUserRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetRemoteUser"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getRemoteUserResponse struct {
|
||||
XMLName xml.Name `xml:"GetRemoteUserResponse"`
|
||||
RemoteUser *struct {
|
||||
Username string `xml:"Username"`
|
||||
Password string `xml:"Password"`
|
||||
UseDerivedPassword bool `xml:"UseDerivedPassword"`
|
||||
} `xml:"RemoteUser"`
|
||||
}
|
||||
|
||||
req := getRemoteUserRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getRemoteUserResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetRemoteUser failed: %w", err)
|
||||
}
|
||||
|
||||
if resp.RemoteUser == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &RemoteUser{
|
||||
Username: resp.RemoteUser.Username,
|
||||
Password: resp.RemoteUser.Password,
|
||||
UseDerivedPassword: resp.RemoteUser.UseDerivedPassword,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetRemoteUser sets the remote user.
|
||||
func (c *Client) SetRemoteUser(ctx context.Context, remoteUser *RemoteUser) error {
|
||||
type remoteUserXML struct {
|
||||
Username string `xml:"tds:Username"`
|
||||
Password string `xml:"tds:Password,omitempty"`
|
||||
UseDerivedPassword bool `xml:"tds:UseDerivedPassword"`
|
||||
}
|
||||
|
||||
type setRemoteUserRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetRemoteUser"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
RemoteUser *remoteUserXML `xml:"tds:RemoteUser,omitempty"`
|
||||
}
|
||||
|
||||
req := setRemoteUserRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
if remoteUser != nil {
|
||||
req.RemoteUser = &remoteUserXML{
|
||||
Username: remoteUser.Username,
|
||||
Password: remoteUser.Password,
|
||||
UseDerivedPassword: remoteUser.UseDerivedPassword,
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetRemoteUser failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetIPAddressFilter gets the IP address filter settings from a device.
|
||||
func (c *Client) GetIPAddressFilter(ctx context.Context) (*IPAddressFilter, error) {
|
||||
type getIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type prefixedAddressXML struct {
|
||||
Address string `xml:"Address"`
|
||||
PrefixLength int `xml:"PrefixLength"`
|
||||
}
|
||||
|
||||
type getIPAddressFilterResponse struct {
|
||||
XMLName xml.Name `xml:"GetIPAddressFilterResponse"`
|
||||
IPAddressFilter struct {
|
||||
Type string `xml:"Type"`
|
||||
IPv4Address []prefixedAddressXML `xml:"IPv4Address"`
|
||||
IPv6Address []prefixedAddressXML `xml:"IPv6Address"`
|
||||
} `xml:"IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := getIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getIPAddressFilterResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterType(resp.IPAddressFilter.Type),
|
||||
}
|
||||
|
||||
// Pre-allocate slices with known capacity
|
||||
if len(resp.IPAddressFilter.IPv4Address) > 0 {
|
||||
filter.IPv4Address = make([]PrefixedIPv4Address, 0, len(resp.IPAddressFilter.IPv4Address))
|
||||
for _, addr := range resp.IPAddressFilter.IPv4Address {
|
||||
filter.IPv4Address = append(filter.IPv4Address, PrefixedIPv4Address{
|
||||
Address: addr.Address,
|
||||
PrefixLength: addr.PrefixLength,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(resp.IPAddressFilter.IPv6Address) > 0 {
|
||||
filter.IPv6Address = make([]PrefixedIPv6Address, 0, len(resp.IPAddressFilter.IPv6Address))
|
||||
for _, addr := range resp.IPAddressFilter.IPv6Address {
|
||||
filter.IPv6Address = append(filter.IPv6Address, PrefixedIPv6Address{
|
||||
Address: addr.Address,
|
||||
PrefixLength: addr.PrefixLength,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
// SetIPAddressFilter sets the IP address filter settings on a device.
|
||||
func (c *Client) SetIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type setIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := setIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddIPAddressFilter adds an IP filter address to a device.
|
||||
func (c *Client) AddIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type addIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:AddIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := addIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("AddIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveIPAddressFilter deletes an IP filter address from a device.
|
||||
func (c *Client) RemoveIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
|
||||
type removeIPAddressFilterRequest struct {
|
||||
XMLName xml.Name `xml:"tds:RemoveIPAddressFilter"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
IPAddressFilter ipAddressFilterRequest `xml:"tds:IPAddressFilter"`
|
||||
}
|
||||
|
||||
req := removeIPAddressFilterRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
IPAddressFilter: buildIPAddressFilterRequest(filter),
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("RemoveIPAddressFilter failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetZeroConfiguration gets the zero-configuration from a device.
|
||||
func (c *Client) GetZeroConfiguration(ctx context.Context) (*NetworkZeroConfiguration, error) {
|
||||
type getZeroConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetZeroConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getZeroConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetZeroConfigurationResponse"`
|
||||
ZeroConfiguration struct {
|
||||
InterfaceToken string `xml:"InterfaceToken"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
Addresses []string `xml:"Addresses"`
|
||||
} `xml:"ZeroConfiguration"`
|
||||
}
|
||||
|
||||
req := getZeroConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getZeroConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetZeroConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &NetworkZeroConfiguration{
|
||||
InterfaceToken: resp.ZeroConfiguration.InterfaceToken,
|
||||
Enabled: resp.ZeroConfiguration.Enabled,
|
||||
Addresses: resp.ZeroConfiguration.Addresses,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetZeroConfiguration sets the zero-configuration.
|
||||
func (c *Client) SetZeroConfiguration(ctx context.Context, interfaceToken string, enabled bool) error {
|
||||
type setZeroConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetZeroConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
InterfaceToken string `xml:"tds:InterfaceToken"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
}
|
||||
|
||||
req := setZeroConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
InterfaceToken: interfaceToken,
|
||||
Enabled: enabled,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetZeroConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDynamicDNS gets the dynamic DNS settings from a device.
|
||||
func (c *Client) GetDynamicDNS(ctx context.Context) (*DynamicDNSInformation, error) {
|
||||
type getDynamicDNSRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetDynamicDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getDynamicDNSResponse struct {
|
||||
XMLName xml.Name `xml:"GetDynamicDNSResponse"`
|
||||
DynamicDNSInformation struct {
|
||||
Type string `xml:"Type"`
|
||||
Name string `xml:"Name"`
|
||||
TTL string `xml:"TTL"`
|
||||
} `xml:"DynamicDNSInformation"`
|
||||
}
|
||||
|
||||
req := getDynamicDNSRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getDynamicDNSResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetDynamicDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return &DynamicDNSInformation{
|
||||
Type: DynamicDNSType(resp.DynamicDNSInformation.Type),
|
||||
Name: resp.DynamicDNSInformation.Name,
|
||||
// TTL would need duration parsing
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetDynamicDNS sets the dynamic DNS settings on a device.
|
||||
func (c *Client) SetDynamicDNS(ctx context.Context, dnsType DynamicDNSType, name string) error {
|
||||
type setDynamicDNSRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetDynamicDNS"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Type DynamicDNSType `xml:"tds:Type"`
|
||||
Name string `xml:"tds:Name,omitempty"`
|
||||
}
|
||||
|
||||
req := setDynamicDNSRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Type: dnsType,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetDynamicDNS failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPasswordComplexityConfiguration retrieves the current password complexity configuration settings.
|
||||
func (c *Client) GetPasswordComplexityConfiguration(ctx context.Context) (*PasswordComplexityConfiguration, error) {
|
||||
type getPasswordComplexityConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetPasswordComplexityConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getPasswordComplexityConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetPasswordComplexityConfigurationResponse"`
|
||||
MinLen int `xml:"MinLen"`
|
||||
Uppercase int `xml:"Uppercase"`
|
||||
Number int `xml:"Number"`
|
||||
SpecialChars int `xml:"SpecialChars"`
|
||||
BlockUsernameOccurrence bool `xml:"BlockUsernameOccurrence"`
|
||||
PolicyConfigurationLocked bool `xml:"PolicyConfigurationLocked"`
|
||||
}
|
||||
|
||||
req := getPasswordComplexityConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getPasswordComplexityConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetPasswordComplexityConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &PasswordComplexityConfiguration{
|
||||
MinLen: resp.MinLen,
|
||||
Uppercase: resp.Uppercase,
|
||||
Number: resp.Number,
|
||||
SpecialChars: resp.SpecialChars,
|
||||
BlockUsernameOccurrence: resp.BlockUsernameOccurrence,
|
||||
PolicyConfigurationLocked: resp.PolicyConfigurationLocked,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetPasswordComplexityConfiguration allows setting of the password complexity configuration.
|
||||
func (c *Client) SetPasswordComplexityConfiguration(
|
||||
ctx context.Context,
|
||||
config *PasswordComplexityConfiguration,
|
||||
) error {
|
||||
type setPasswordComplexityConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetPasswordComplexityConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
MinLen int `xml:"tds:MinLen,omitempty"`
|
||||
Uppercase int `xml:"tds:Uppercase,omitempty"`
|
||||
Number int `xml:"tds:Number,omitempty"`
|
||||
SpecialChars int `xml:"tds:SpecialChars,omitempty"`
|
||||
BlockUsernameOccurrence bool `xml:"tds:BlockUsernameOccurrence,omitempty"`
|
||||
PolicyConfigurationLocked bool `xml:"tds:PolicyConfigurationLocked,omitempty"`
|
||||
}
|
||||
|
||||
req := setPasswordComplexityConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
MinLen: config.MinLen,
|
||||
Uppercase: config.Uppercase,
|
||||
Number: config.Number,
|
||||
SpecialChars: config.SpecialChars,
|
||||
BlockUsernameOccurrence: config.BlockUsernameOccurrence,
|
||||
PolicyConfigurationLocked: config.PolicyConfigurationLocked,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetPasswordComplexityConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPasswordHistoryConfiguration retrieves the current password history configuration settings.
|
||||
func (c *Client) GetPasswordHistoryConfiguration(ctx context.Context) (*PasswordHistoryConfiguration, error) {
|
||||
type getPasswordHistoryConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetPasswordHistoryConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getPasswordHistoryConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetPasswordHistoryConfigurationResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
Length int `xml:"Length"`
|
||||
}
|
||||
|
||||
req := getPasswordHistoryConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getPasswordHistoryConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetPasswordHistoryConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &PasswordHistoryConfiguration{
|
||||
Enabled: resp.Enabled,
|
||||
Length: resp.Length,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetPasswordHistoryConfiguration allows setting of the password history configuration.
|
||||
func (c *Client) SetPasswordHistoryConfiguration(ctx context.Context, config *PasswordHistoryConfiguration) error {
|
||||
type setPasswordHistoryConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetPasswordHistoryConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
Length int `xml:"tds:Length"`
|
||||
}
|
||||
|
||||
req := setPasswordHistoryConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: config.Enabled,
|
||||
Length: config.Length,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetPasswordHistoryConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAuthFailureWarningConfiguration retrieves the current authentication failure warning configuration.
|
||||
func (c *Client) GetAuthFailureWarningConfiguration(ctx context.Context) (*AuthFailureWarningConfiguration, error) {
|
||||
type getAuthFailureWarningConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:GetAuthFailureWarningConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type getAuthFailureWarningConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetAuthFailureWarningConfigurationResponse"`
|
||||
Enabled bool `xml:"Enabled"`
|
||||
MonitorPeriod int `xml:"MonitorPeriod"`
|
||||
MaxAuthFailures int `xml:"MaxAuthFailures"`
|
||||
}
|
||||
|
||||
req := getAuthFailureWarningConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
|
||||
var resp getAuthFailureWarningConfigurationResponse
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("GetAuthFailureWarningConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return &AuthFailureWarningConfiguration{
|
||||
Enabled: resp.Enabled,
|
||||
MonitorPeriod: resp.MonitorPeriod,
|
||||
MaxAuthFailures: resp.MaxAuthFailures,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetAuthFailureWarningConfiguration allows setting of the authentication failure warning configuration.
|
||||
func (c *Client) SetAuthFailureWarningConfiguration(
|
||||
ctx context.Context,
|
||||
config *AuthFailureWarningConfiguration,
|
||||
) error {
|
||||
type setAuthFailureWarningConfigurationRequest struct {
|
||||
XMLName xml.Name `xml:"tds:SetAuthFailureWarningConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Enabled bool `xml:"tds:Enabled"`
|
||||
MonitorPeriod int `xml:"tds:MonitorPeriod"`
|
||||
MaxAuthFailures int `xml:"tds:MaxAuthFailures"`
|
||||
}
|
||||
|
||||
req := setAuthFailureWarningConfigurationRequest{
|
||||
Xmlns: deviceNamespace,
|
||||
Enabled: config.Enabled,
|
||||
MonitorPeriod: config.MonitorPeriod,
|
||||
MaxAuthFailures: config.MaxAuthFailures,
|
||||
}
|
||||
|
||||
if err := c.newSOAPClient().Call(ctx, c.endpoint, "", req, nil); err != nil {
|
||||
return fmt.Errorf("SetAuthFailureWarningConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,786 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceSecurityServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "GetRemoteUser"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetRemoteUserResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:RemoteUser>
|
||||
<tt:Username>remote_admin</tt:Username>
|
||||
<tt:Password></tt:Password>
|
||||
<tt:UseDerivedPassword>true</tt:UseDerivedPassword>
|
||||
</tds:RemoteUser>
|
||||
</tds:GetRemoteUserResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRemoteUser"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRemoteUserResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetIPAddressFilter"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetIPAddressFilterResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:IPAddressFilter>
|
||||
<tt:Type>Allow</tt:Type>
|
||||
<tt:IPv4Address>
|
||||
<tt:Address>192.168.1.0</tt:Address>
|
||||
<tt:PrefixLength>24</tt:PrefixLength>
|
||||
</tt:IPv4Address>
|
||||
</tds:IPAddressFilter>
|
||||
</tds:GetIPAddressFilterResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetIPAddressFilter"),
|
||||
strings.Contains(bodyContent, "AddIPAddressFilter"),
|
||||
strings.Contains(bodyContent, "RemoveIPAddressFilter"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetIPAddressFilterResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetZeroConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetZeroConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:ZeroConfiguration>
|
||||
<tt:InterfaceToken>eth0</tt:InterfaceToken>
|
||||
<tt:Enabled>true</tt:Enabled>
|
||||
<tt:Addresses>169.254.1.100</tt:Addresses>
|
||||
</tds:ZeroConfiguration>
|
||||
</tds:GetZeroConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetZeroConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetZeroConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetPasswordComplexityConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetPasswordComplexityConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:MinLen>8</tds:MinLen>
|
||||
<tds:Uppercase>1</tds:Uppercase>
|
||||
<tds:Number>1</tds:Number>
|
||||
<tds:SpecialChars>1</tds:SpecialChars>
|
||||
<tds:BlockUsernameOccurrence>true</tds:BlockUsernameOccurrence>
|
||||
<tds:PolicyConfigurationLocked>false</tds:PolicyConfigurationLocked>
|
||||
</tds:GetPasswordComplexityConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetPasswordComplexityConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetPasswordComplexityConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetPasswordHistoryConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetPasswordHistoryConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
<tds:Length>5</tds:Length>
|
||||
</tds:GetPasswordHistoryConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetPasswordHistoryConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetPasswordHistoryConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetAuthFailureWarningConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetAuthFailureWarningConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
<tds:MonitorPeriod>60</tds:MonitorPeriod>
|
||||
<tds:MaxAuthFailures>5</tds:MaxAuthFailures>
|
||||
</tds:GetAuthFailureWarningConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetAuthFailureWarningConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetAuthFailureWarningConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetRemoteUser(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
remoteUser, err := client.GetRemoteUser(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRemoteUser failed: %v", err)
|
||||
}
|
||||
|
||||
if remoteUser.Username != "remote_admin" {
|
||||
t.Errorf("Expected username 'remote_admin', got %s", remoteUser.Username)
|
||||
}
|
||||
|
||||
if !remoteUser.UseDerivedPassword {
|
||||
t.Error("UseDerivedPassword should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRemoteUser(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
remoteUser := &RemoteUser{
|
||||
Username: "new_remote",
|
||||
Password: "password123",
|
||||
UseDerivedPassword: true,
|
||||
}
|
||||
|
||||
err = client.SetRemoteUser(ctx, remoteUser)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRemoteUser failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter, err := client.GetIPAddressFilter(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetIPAddressFilter failed: %v", err)
|
||||
}
|
||||
|
||||
if filter.Type != IPAddressFilterAllow {
|
||||
t.Errorf("Expected Allow filter type, got %s", filter.Type)
|
||||
}
|
||||
|
||||
if len(filter.IPv4Address) != 1 {
|
||||
t.Fatalf("Expected 1 IPv4 address, got %d", len(filter.IPv4Address))
|
||||
}
|
||||
|
||||
if filter.IPv4Address[0].Address != "192.168.1.0" {
|
||||
t.Errorf("Expected address 192.168.1.0, got %s", filter.IPv4Address[0].Address)
|
||||
}
|
||||
|
||||
if filter.IPv4Address[0].PrefixLength != 24 {
|
||||
t.Errorf("Expected prefix length 24, got %d", filter.IPv4Address[0].PrefixLength)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "10.0.0.0", PrefixLength: 8},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("SetIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.AddIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("AddIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.RemoveIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetZeroConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
zeroConf, err := client.GetZeroConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetZeroConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if zeroConf.InterfaceToken != "eth0" {
|
||||
t.Errorf("Expected interface token 'eth0', got %s", zeroConf.InterfaceToken)
|
||||
}
|
||||
|
||||
if !zeroConf.Enabled {
|
||||
t.Error("Zero configuration should be enabled")
|
||||
}
|
||||
|
||||
if len(zeroConf.Addresses) != 1 || zeroConf.Addresses[0] != "169.254.1.100" {
|
||||
t.Errorf("Expected address 169.254.1.100, got %v", zeroConf.Addresses)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetZeroConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
err = client.SetZeroConfiguration(ctx, "eth0", true)
|
||||
if err != nil {
|
||||
t.Fatalf("SetZeroConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPasswordComplexityConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetPasswordComplexityConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPasswordComplexityConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if config.MinLen != 8 {
|
||||
t.Errorf("Expected MinLen 8, got %d", config.MinLen)
|
||||
}
|
||||
|
||||
if config.Uppercase != 1 {
|
||||
t.Errorf("Expected Uppercase 1, got %d", config.Uppercase)
|
||||
}
|
||||
|
||||
if config.Number != 1 {
|
||||
t.Errorf("Expected Number 1, got %d", config.Number)
|
||||
}
|
||||
|
||||
if config.SpecialChars != 1 {
|
||||
t.Errorf("Expected SpecialChars 1, got %d", config.SpecialChars)
|
||||
}
|
||||
|
||||
if !config.BlockUsernameOccurrence {
|
||||
t.Error("BlockUsernameOccurrence should be true")
|
||||
}
|
||||
|
||||
if config.PolicyConfigurationLocked {
|
||||
t.Error("PolicyConfigurationLocked should be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPasswordComplexityConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &PasswordComplexityConfiguration{
|
||||
MinLen: 10,
|
||||
Uppercase: 2,
|
||||
Number: 2,
|
||||
SpecialChars: 1,
|
||||
BlockUsernameOccurrence: true,
|
||||
PolicyConfigurationLocked: false,
|
||||
}
|
||||
|
||||
err = client.SetPasswordComplexityConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetPasswordComplexityConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPasswordHistoryConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetPasswordHistoryConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPasswordHistoryConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if !config.Enabled {
|
||||
t.Error("Password history should be enabled")
|
||||
}
|
||||
|
||||
if config.Length != 5 {
|
||||
t.Errorf("Expected Length 5, got %d", config.Length)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPasswordHistoryConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &PasswordHistoryConfiguration{
|
||||
Enabled: true,
|
||||
Length: 10,
|
||||
}
|
||||
|
||||
err = client.SetPasswordHistoryConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetPasswordHistoryConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuthFailureWarningConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetAuthFailureWarningConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAuthFailureWarningConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if !config.Enabled {
|
||||
t.Error("Auth failure warning should be enabled")
|
||||
}
|
||||
|
||||
if config.MonitorPeriod != 60 {
|
||||
t.Errorf("Expected MonitorPeriod 60, got %d", config.MonitorPeriod)
|
||||
}
|
||||
|
||||
if config.MaxAuthFailures != 5 {
|
||||
t.Errorf("Expected MaxAuthFailures 5, got %d", config.MaxAuthFailures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAuthFailureWarningConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &AuthFailureWarningConfiguration{
|
||||
Enabled: true,
|
||||
MonitorPeriod: 120,
|
||||
MaxAuthFailures: 3,
|
||||
}
|
||||
|
||||
err = client.SetAuthFailureWarningConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetAuthFailureWarningConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPAddressFilterTypeConstants(t *testing.T) {
|
||||
if IPAddressFilterAllow != "Allow" {
|
||||
t.Errorf("IPAddressFilterAllow should be 'Allow', got %s", IPAddressFilterAllow)
|
||||
}
|
||||
|
||||
if IPAddressFilterDeny != "Deny" {
|
||||
t.Errorf("IPAddressFilterDeny should be 'Deny', got %s", IPAddressFilterDeny)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmarks for device security operations.
|
||||
|
||||
func BenchmarkGetRemoteUser(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetRemoteUser(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetRemoteUser(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
remoteUser := &RemoteUser{
|
||||
Username: "test_user",
|
||||
Password: "password123",
|
||||
UseDerivedPassword: true,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetRemoteUser(ctx, remoteUser)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetIPAddressFilter(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "192.168.1.0", PrefixLength: 24},
|
||||
{Address: "10.0.0.0", PrefixLength: 8},
|
||||
},
|
||||
IPv6Address: []PrefixedIPv6Address{
|
||||
{Address: "fe80::", PrefixLength: 64},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAddIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.AddIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRemoveIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.RemoveIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetZeroConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetZeroConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetZeroConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetZeroConfiguration(ctx, "eth0", true)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetPasswordComplexityConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetPasswordComplexityConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetPasswordComplexityConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &PasswordComplexityConfiguration{
|
||||
MinLen: 10,
|
||||
Uppercase: 2,
|
||||
Number: 2,
|
||||
SpecialChars: 1,
|
||||
BlockUsernameOccurrence: true,
|
||||
PolicyConfigurationLocked: false,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetPasswordComplexityConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetPasswordHistoryConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetPasswordHistoryConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetPasswordHistoryConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &PasswordHistoryConfiguration{
|
||||
Enabled: true,
|
||||
Length: 10,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetPasswordHistoryConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetAuthFailureWarningConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetAuthFailureWarningConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetAuthFailureWarningConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &AuthFailureWarningConfiguration{
|
||||
Enabled: true,
|
||||
MonitorPeriod: 120,
|
||||
MaxAuthFailures: 3,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetAuthFailureWarningConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkIPAddressFilterWithManyAddresses tests performance with larger address lists.
|
||||
func BenchmarkIPAddressFilterWithManyAddresses(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create filter with many addresses to test pre-allocation efficiency
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: make([]PrefixedIPv4Address, 100),
|
||||
IPv6Address: make([]PrefixedIPv6Address, 50),
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
filter.IPv4Address[i] = PrefixedIPv4Address{
|
||||
Address: "192.168.1.0",
|
||||
PrefixLength: 24,
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 50; i++ {
|
||||
filter.IPv6Address[i] = PrefixedIPv6Address{
|
||||
Address: "fe80::",
|
||||
PrefixLength: 64,
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
@@ -1,786 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newMockDeviceSecurityServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := xml.NewDecoder(r.Body)
|
||||
var envelope struct {
|
||||
Body struct {
|
||||
Content []byte `xml:",innerxml"`
|
||||
} `xml:"Body"`
|
||||
}
|
||||
_ = decoder.Decode(&envelope)
|
||||
bodyContent := string(envelope.Body.Content)
|
||||
|
||||
w.Header().Set("Content-Type", "application/soap+xml")
|
||||
|
||||
switch {
|
||||
case strings.Contains(bodyContent, "GetRemoteUser"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetRemoteUserResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:RemoteUser>
|
||||
<tt:Username>remote_admin</tt:Username>
|
||||
<tt:Password></tt:Password>
|
||||
<tt:UseDerivedPassword>true</tt:UseDerivedPassword>
|
||||
</tds:RemoteUser>
|
||||
</tds:GetRemoteUserResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetRemoteUser"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetRemoteUserResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetIPAddressFilter"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetIPAddressFilterResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:IPAddressFilter>
|
||||
<tt:Type>Allow</tt:Type>
|
||||
<tt:IPv4Address>
|
||||
<tt:Address>192.168.1.0</tt:Address>
|
||||
<tt:PrefixLength>24</tt:PrefixLength>
|
||||
</tt:IPv4Address>
|
||||
</tds:IPAddressFilter>
|
||||
</tds:GetIPAddressFilterResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetIPAddressFilter"),
|
||||
strings.Contains(bodyContent, "AddIPAddressFilter"),
|
||||
strings.Contains(bodyContent, "RemoveIPAddressFilter"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetIPAddressFilterResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetZeroConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetZeroConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:ZeroConfiguration>
|
||||
<tt:InterfaceToken>eth0</tt:InterfaceToken>
|
||||
<tt:Enabled>true</tt:Enabled>
|
||||
<tt:Addresses>169.254.1.100</tt:Addresses>
|
||||
</tds:ZeroConfiguration>
|
||||
</tds:GetZeroConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetZeroConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetZeroConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetPasswordComplexityConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetPasswordComplexityConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:MinLen>8</tds:MinLen>
|
||||
<tds:Uppercase>1</tds:Uppercase>
|
||||
<tds:Number>1</tds:Number>
|
||||
<tds:SpecialChars>1</tds:SpecialChars>
|
||||
<tds:BlockUsernameOccurrence>true</tds:BlockUsernameOccurrence>
|
||||
<tds:PolicyConfigurationLocked>false</tds:PolicyConfigurationLocked>
|
||||
</tds:GetPasswordComplexityConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetPasswordComplexityConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetPasswordComplexityConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetPasswordHistoryConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetPasswordHistoryConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
<tds:Length>5</tds:Length>
|
||||
</tds:GetPasswordHistoryConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetPasswordHistoryConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetPasswordHistoryConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "GetAuthFailureWarningConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:GetAuthFailureWarningConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
||||
<tds:Enabled>true</tds:Enabled>
|
||||
<tds:MonitorPeriod>60</tds:MonitorPeriod>
|
||||
<tds:MaxAuthFailures>5</tds:MaxAuthFailures>
|
||||
</tds:GetAuthFailureWarningConfigurationResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
case strings.Contains(bodyContent, "SetAuthFailureWarningConfiguration"):
|
||||
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
|
||||
<s:Body>
|
||||
<tds:SetAuthFailureWarningConfigurationResponse xmlns:tds="http://www.onvif.org/ver10/device/wsdl"/>
|
||||
</s:Body>
|
||||
</s:Envelope>`))
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGetRemoteUser(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
remoteUser, err := client.GetRemoteUser(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRemoteUser failed: %v", err)
|
||||
}
|
||||
|
||||
if remoteUser.Username != "remote_admin" {
|
||||
t.Errorf("Expected username 'remote_admin', got %s", remoteUser.Username)
|
||||
}
|
||||
|
||||
if !remoteUser.UseDerivedPassword {
|
||||
t.Error("UseDerivedPassword should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRemoteUser(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
remoteUser := &RemoteUser{
|
||||
Username: "new_remote",
|
||||
Password: "password123",
|
||||
UseDerivedPassword: true,
|
||||
}
|
||||
|
||||
err = client.SetRemoteUser(ctx, remoteUser)
|
||||
if err != nil {
|
||||
t.Fatalf("SetRemoteUser failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter, err := client.GetIPAddressFilter(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetIPAddressFilter failed: %v", err)
|
||||
}
|
||||
|
||||
if filter.Type != IPAddressFilterAllow {
|
||||
t.Errorf("Expected Allow filter type, got %s", filter.Type)
|
||||
}
|
||||
|
||||
if len(filter.IPv4Address) != 1 {
|
||||
t.Fatalf("Expected 1 IPv4 address, got %d", len(filter.IPv4Address))
|
||||
}
|
||||
|
||||
if filter.IPv4Address[0].Address != "192.168.1.0" {
|
||||
t.Errorf("Expected address 192.168.1.0, got %s", filter.IPv4Address[0].Address)
|
||||
}
|
||||
|
||||
if filter.IPv4Address[0].PrefixLength != 24 {
|
||||
t.Errorf("Expected prefix length 24, got %d", filter.IPv4Address[0].PrefixLength)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "10.0.0.0", PrefixLength: 8},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.SetIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("SetIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.AddIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("AddIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveIPAddressFilter(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
err = client.RemoveIPAddressFilter(ctx, filter)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveIPAddressFilter failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetZeroConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
zeroConf, err := client.GetZeroConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetZeroConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if zeroConf.InterfaceToken != "eth0" {
|
||||
t.Errorf("Expected interface token 'eth0', got %s", zeroConf.InterfaceToken)
|
||||
}
|
||||
|
||||
if !zeroConf.Enabled {
|
||||
t.Error("Zero configuration should be enabled")
|
||||
}
|
||||
|
||||
if len(zeroConf.Addresses) != 1 || zeroConf.Addresses[0] != "169.254.1.100" {
|
||||
t.Errorf("Expected address 169.254.1.100, got %v", zeroConf.Addresses)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetZeroConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
err = client.SetZeroConfiguration(ctx, "eth0", true)
|
||||
if err != nil {
|
||||
t.Fatalf("SetZeroConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPasswordComplexityConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetPasswordComplexityConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPasswordComplexityConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if config.MinLen != 8 {
|
||||
t.Errorf("Expected MinLen 8, got %d", config.MinLen)
|
||||
}
|
||||
|
||||
if config.Uppercase != 1 {
|
||||
t.Errorf("Expected Uppercase 1, got %d", config.Uppercase)
|
||||
}
|
||||
|
||||
if config.Number != 1 {
|
||||
t.Errorf("Expected Number 1, got %d", config.Number)
|
||||
}
|
||||
|
||||
if config.SpecialChars != 1 {
|
||||
t.Errorf("Expected SpecialChars 1, got %d", config.SpecialChars)
|
||||
}
|
||||
|
||||
if !config.BlockUsernameOccurrence {
|
||||
t.Error("BlockUsernameOccurrence should be true")
|
||||
}
|
||||
|
||||
if config.PolicyConfigurationLocked {
|
||||
t.Error("PolicyConfigurationLocked should be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPasswordComplexityConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &PasswordComplexityConfiguration{
|
||||
MinLen: 10,
|
||||
Uppercase: 2,
|
||||
Number: 2,
|
||||
SpecialChars: 1,
|
||||
BlockUsernameOccurrence: true,
|
||||
PolicyConfigurationLocked: false,
|
||||
}
|
||||
|
||||
err = client.SetPasswordComplexityConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetPasswordComplexityConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPasswordHistoryConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetPasswordHistoryConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPasswordHistoryConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if !config.Enabled {
|
||||
t.Error("Password history should be enabled")
|
||||
}
|
||||
|
||||
if config.Length != 5 {
|
||||
t.Errorf("Expected Length 5, got %d", config.Length)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPasswordHistoryConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &PasswordHistoryConfiguration{
|
||||
Enabled: true,
|
||||
Length: 10,
|
||||
}
|
||||
|
||||
err = client.SetPasswordHistoryConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetPasswordHistoryConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuthFailureWarningConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config, err := client.GetAuthFailureWarningConfiguration(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAuthFailureWarningConfiguration failed: %v", err)
|
||||
}
|
||||
|
||||
if !config.Enabled {
|
||||
t.Error("Auth failure warning should be enabled")
|
||||
}
|
||||
|
||||
if config.MonitorPeriod != 60 {
|
||||
t.Errorf("Expected MonitorPeriod 60, got %d", config.MonitorPeriod)
|
||||
}
|
||||
|
||||
if config.MaxAuthFailures != 5 {
|
||||
t.Errorf("Expected MaxAuthFailures 5, got %d", config.MaxAuthFailures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAuthFailureWarningConfiguration(t *testing.T) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
config := &AuthFailureWarningConfiguration{
|
||||
Enabled: true,
|
||||
MonitorPeriod: 120,
|
||||
MaxAuthFailures: 3,
|
||||
}
|
||||
|
||||
err = client.SetAuthFailureWarningConfiguration(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("SetAuthFailureWarningConfiguration failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPAddressFilterTypeConstants(t *testing.T) {
|
||||
if IPAddressFilterAllow != "Allow" {
|
||||
t.Errorf("IPAddressFilterAllow should be 'Allow', got %s", IPAddressFilterAllow)
|
||||
}
|
||||
|
||||
if IPAddressFilterDeny != "Deny" {
|
||||
t.Errorf("IPAddressFilterDeny should be 'Deny', got %s", IPAddressFilterDeny)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmarks for device security operations.
|
||||
|
||||
func BenchmarkGetRemoteUser(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetRemoteUser(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetRemoteUser(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
remoteUser := &RemoteUser{
|
||||
Username: "test_user",
|
||||
Password: "password123",
|
||||
UseDerivedPassword: true,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetRemoteUser(ctx, remoteUser)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetIPAddressFilter(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "192.168.1.0", PrefixLength: 24},
|
||||
{Address: "10.0.0.0", PrefixLength: 8},
|
||||
},
|
||||
IPv6Address: []PrefixedIPv6Address{
|
||||
{Address: "fe80::", PrefixLength: 64},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAddIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.AddIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRemoveIPAddressFilter(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: []PrefixedIPv4Address{
|
||||
{Address: "172.16.0.0", PrefixLength: 12},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.RemoveIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetZeroConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetZeroConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetZeroConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetZeroConfiguration(ctx, "eth0", true)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetPasswordComplexityConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetPasswordComplexityConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetPasswordComplexityConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &PasswordComplexityConfiguration{
|
||||
MinLen: 10,
|
||||
Uppercase: 2,
|
||||
Number: 2,
|
||||
SpecialChars: 1,
|
||||
BlockUsernameOccurrence: true,
|
||||
PolicyConfigurationLocked: false,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetPasswordComplexityConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetPasswordHistoryConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetPasswordHistoryConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetPasswordHistoryConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &PasswordHistoryConfiguration{
|
||||
Enabled: true,
|
||||
Length: 10,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetPasswordHistoryConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetAuthFailureWarningConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = client.GetAuthFailureWarningConfiguration(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetAuthFailureWarningConfiguration(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
config := &AuthFailureWarningConfiguration{
|
||||
Enabled: true,
|
||||
MonitorPeriod: 120,
|
||||
MaxAuthFailures: 3,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetAuthFailureWarningConfiguration(ctx, config)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkIPAddressFilterWithManyAddresses tests performance with larger address lists.
|
||||
func BenchmarkIPAddressFilterWithManyAddresses(b *testing.B) {
|
||||
server := newMockDeviceSecurityServer()
|
||||
defer server.Close()
|
||||
|
||||
client, _ := NewClient(server.URL)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create filter with many addresses to test pre-allocation efficiency
|
||||
filter := &IPAddressFilter{
|
||||
Type: IPAddressFilterAllow,
|
||||
IPv4Address: make([]PrefixedIPv4Address, 100),
|
||||
IPv6Address: make([]PrefixedIPv6Address, 50),
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
filter.IPv4Address[i] = PrefixedIPv4Address{
|
||||
Address: "192.168.1.0",
|
||||
PrefixLength: 24,
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 50; i++ {
|
||||
filter.IPv6Address[i] = PrefixedIPv6Address{
|
||||
Address: "fe80::",
|
||||
PrefixLength: 64,
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = client.SetIPAddressFilter(ctx, filter)
|
||||
}
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetStorageConfigurations retrieves storage configurations. ONVIF Specification: GetStorageConfigurations operation.
|
||||
func (c *Client) GetStorageConfigurations(ctx context.Context) ([]*StorageConfiguration, error) {
|
||||
type GetStorageConfigurationsBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetStorageConfigurations"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetStorageConfigurationsResponse struct {
|
||||
XMLName xml.Name `xml:"GetStorageConfigurationsResponse"`
|
||||
StorageConfigurations []*StorageConfiguration `xml:"StorageConfigurations"`
|
||||
}
|
||||
|
||||
request := GetStorageConfigurationsBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetStorageConfigurationsResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetStorageConfigurations failed: %w", err)
|
||||
}
|
||||
|
||||
return response.StorageConfigurations, nil
|
||||
}
|
||||
|
||||
// GetStorageConfiguration retrieves a storage configuration. ONVIF Specification: GetStorageConfiguration operation.
|
||||
func (c *Client) GetStorageConfiguration(ctx context.Context, token string) (*StorageConfiguration, error) {
|
||||
type GetStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Token string `xml:"tds:Token"`
|
||||
}
|
||||
|
||||
type GetStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetStorageConfigurationResponse"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"StorageConfiguration"`
|
||||
}
|
||||
|
||||
request := GetStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Token: token,
|
||||
}
|
||||
var response GetStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return response.StorageConfiguration, nil
|
||||
}
|
||||
|
||||
// CreateStorageConfiguration creates a storage configuration.
|
||||
// ONVIF Specification: CreateStorageConfiguration operation.
|
||||
func (c *Client) CreateStorageConfiguration(ctx context.Context, config *StorageConfiguration) (string, error) {
|
||||
type CreateStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:CreateStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"tds:StorageConfiguration"`
|
||||
}
|
||||
|
||||
type CreateStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"CreateStorageConfigurationResponse"`
|
||||
Token string `xml:"Token"`
|
||||
}
|
||||
|
||||
request := CreateStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
StorageConfiguration: config,
|
||||
}
|
||||
var response CreateStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return "", fmt.Errorf("CreateStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Token, nil
|
||||
}
|
||||
|
||||
// SetStorageConfiguration sets a storage configuration. ONVIF Specification: SetStorageConfiguration operation.
|
||||
func (c *Client) SetStorageConfiguration(ctx context.Context, config *StorageConfiguration) error {
|
||||
type SetStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"tds:StorageConfiguration"`
|
||||
}
|
||||
|
||||
type SetStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"SetStorageConfigurationResponse"`
|
||||
}
|
||||
|
||||
request := SetStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
StorageConfiguration: config,
|
||||
}
|
||||
var response SetStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteStorageConfiguration deletes a storage configuration.
|
||||
// ONVIF Specification: DeleteStorageConfiguration operation.
|
||||
func (c *Client) DeleteStorageConfiguration(ctx context.Context, token string) error {
|
||||
type DeleteStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Token string `xml:"tds:Token"`
|
||||
}
|
||||
|
||||
type DeleteStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteStorageConfigurationResponse"`
|
||||
}
|
||||
|
||||
request := DeleteStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Token: token,
|
||||
}
|
||||
var response DeleteStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHashingAlgorithm sets the hashing algorithm. ONVIF Specification: SetHashingAlgorithm operation.
|
||||
func (c *Client) SetHashingAlgorithm(ctx context.Context, algorithm string) error {
|
||||
type SetHashingAlgorithmBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetHashingAlgorithm"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Algorithm string `xml:"tds:Algorithm"`
|
||||
}
|
||||
|
||||
type SetHashingAlgorithmResponse struct {
|
||||
XMLName xml.Name `xml:"SetHashingAlgorithmResponse"`
|
||||
}
|
||||
|
||||
request := SetHashingAlgorithmBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Algorithm: algorithm,
|
||||
}
|
||||
var response SetHashingAlgorithmResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetHashingAlgorithm failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
|
||||
"github.com/0x524a/onvif-go/internal/soap"
|
||||
)
|
||||
|
||||
// GetStorageConfigurations retrieves storage configurations. ONVIF Specification: GetStorageConfigurations operation.
|
||||
func (c *Client) GetStorageConfigurations(ctx context.Context) ([]*StorageConfiguration, error) {
|
||||
type GetStorageConfigurationsBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetStorageConfigurations"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
}
|
||||
|
||||
type GetStorageConfigurationsResponse struct {
|
||||
XMLName xml.Name `xml:"GetStorageConfigurationsResponse"`
|
||||
StorageConfigurations []*StorageConfiguration `xml:"StorageConfigurations"`
|
||||
}
|
||||
|
||||
request := GetStorageConfigurationsBody{
|
||||
Xmlns: deviceNamespace,
|
||||
}
|
||||
var response GetStorageConfigurationsResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetStorageConfigurations failed: %w", err)
|
||||
}
|
||||
|
||||
return response.StorageConfigurations, nil
|
||||
}
|
||||
|
||||
// GetStorageConfiguration retrieves a storage configuration. ONVIF Specification: GetStorageConfiguration operation.
|
||||
func (c *Client) GetStorageConfiguration(ctx context.Context, token string) (*StorageConfiguration, error) {
|
||||
type GetStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:GetStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Token string `xml:"tds:Token"`
|
||||
}
|
||||
|
||||
type GetStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"GetStorageConfigurationResponse"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"StorageConfiguration"`
|
||||
}
|
||||
|
||||
request := GetStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Token: token,
|
||||
}
|
||||
var response GetStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return nil, fmt.Errorf("GetStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return response.StorageConfiguration, nil
|
||||
}
|
||||
|
||||
// CreateStorageConfiguration creates a storage configuration.
|
||||
// ONVIF Specification: CreateStorageConfiguration operation.
|
||||
func (c *Client) CreateStorageConfiguration(ctx context.Context, config *StorageConfiguration) (string, error) {
|
||||
type CreateStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:CreateStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"tds:StorageConfiguration"`
|
||||
}
|
||||
|
||||
type CreateStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"CreateStorageConfigurationResponse"`
|
||||
Token string `xml:"Token"`
|
||||
}
|
||||
|
||||
request := CreateStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
StorageConfiguration: config,
|
||||
}
|
||||
var response CreateStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return "", fmt.Errorf("CreateStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return response.Token, nil
|
||||
}
|
||||
|
||||
// SetStorageConfiguration sets a storage configuration. ONVIF Specification: SetStorageConfiguration operation.
|
||||
func (c *Client) SetStorageConfiguration(ctx context.Context, config *StorageConfiguration) error {
|
||||
type SetStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
StorageConfiguration *StorageConfiguration `xml:"tds:StorageConfiguration"`
|
||||
}
|
||||
|
||||
type SetStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"SetStorageConfigurationResponse"`
|
||||
}
|
||||
|
||||
request := SetStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
StorageConfiguration: config,
|
||||
}
|
||||
var response SetStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteStorageConfiguration deletes a storage configuration.
|
||||
// ONVIF Specification: DeleteStorageConfiguration operation.
|
||||
func (c *Client) DeleteStorageConfiguration(ctx context.Context, token string) error {
|
||||
type DeleteStorageConfigurationBody struct {
|
||||
XMLName xml.Name `xml:"tds:DeleteStorageConfiguration"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Token string `xml:"tds:Token"`
|
||||
}
|
||||
|
||||
type DeleteStorageConfigurationResponse struct {
|
||||
XMLName xml.Name `xml:"DeleteStorageConfigurationResponse"`
|
||||
}
|
||||
|
||||
request := DeleteStorageConfigurationBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Token: token,
|
||||
}
|
||||
var response DeleteStorageConfigurationResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("DeleteStorageConfiguration failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHashingAlgorithm sets the hashing algorithm. ONVIF Specification: SetHashingAlgorithm operation.
|
||||
func (c *Client) SetHashingAlgorithm(ctx context.Context, algorithm string) error {
|
||||
type SetHashingAlgorithmBody struct {
|
||||
XMLName xml.Name `xml:"tds:SetHashingAlgorithm"`
|
||||
Xmlns string `xml:"xmlns:tds,attr"`
|
||||
Algorithm string `xml:"tds:Algorithm"`
|
||||
}
|
||||
|
||||
type SetHashingAlgorithmResponse struct {
|
||||
XMLName xml.Name `xml:"SetHashingAlgorithmResponse"`
|
||||
}
|
||||
|
||||
request := SetHashingAlgorithmBody{
|
||||
Xmlns: deviceNamespace,
|
||||
Algorithm: algorithm,
|
||||
}
|
||||
var response SetHashingAlgorithmResponse
|
||||
|
||||
username, password := c.GetCredentials()
|
||||
soapClient := soap.NewClient(c.httpClient, username, password)
|
||||
|
||||
if err := soapClient.Call(ctx, c.endpoint, "", request, &response); err != nil {
|
||||
return fmt.Errorf("SetHashingAlgorithm failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user