Skip to content

bianc2018/OnvifSimServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ONVIF设备模拟器

1. 项目概述

ONVIF设备模拟器是一个基于Golang开发的ONVIF设备模拟工具,支持批量模拟ONVIF设备,提供完整的ONVIF服务支持,包括设备管理、媒体流服务和事件告警服务,并提供调试页面和客户端测试工具。

2. 功能特性

2.1 设备模拟

  • 支持批量模拟ONVIF设备,每个设备一个端口
  • 支持通过配置文件批量配置,自动分配端口
  • 支持设备上下线控制
  • 支持自动发现功能
  • 支持多通道设备模拟
  • 支持跨服务器访问

2.2 ONVIF服务

  • 设备服务:获取设备信息、服务列表、能力等
  • 媒体服务:支持实时流点播和录像流点播,通过配置加载本地MP4文件
  • 事件服务:支持告警推送(推模式和拉模式),可配置订阅周期和告警行为
  • 鉴权功能:支持设备登录鉴权和保活机制

2.3 调试页面

  • 展示服务运行状态
  • 显示所有模拟设备的状态
  • 显示码流状态和告警订阅状态
  • 显示性能报告
  • 支持通过页面操作设备上下线

2.4 日志系统

  • 支持分级日志(debug, info, warn, error)
  • 支持日志输出到文件,自动分割
  • 支持调试日志开关,每个客户端会话输出独立日志文件
  • 记录客户端和设备之间的所有交互报文

2.5 客户端测试工具

  • 支持设备信息获取
  • 支持服务列表查询
  • 支持媒体配置文件获取
  • 支持流URI获取
  • 支持事件订阅测试

2.6 性能要求

  • 支持模拟1500个设备
  • 支持100路并发取流

3. 技术栈

  • 主语言:Go 1.16+
  • 日志:zap + lumberjack
  • 配置:gopkg.in/yaml.v2
  • Web框架:标准库 net/http
  • 模板:html/template
  • 测试:github.com/stretchr/testify
  • 构建工具:Go build + 自定义脚本

4. 快速开始

4.1 直接运行

# 直接运行主程序
go run cmd/main.go

4.2 使用构建脚本

# Linux/macOS
./scripts/build.sh
cd dist
./run.sh

# Windows
scripts\build.bat
dist\run.bat

5. 配置说明

5.1 配置文件结构

配置文件采用YAML格式,主要包含以下几个部分:

  • http:HTTP服务配置
  • log:日志配置
  • debugLog:调试日志配置
  • batchConfig:批量设备配置
  • devices:设备配置列表

5.2 示例配置

http:
  addr: :8081

log:
  level: info
  filename: ./log/onvif-sim.log
  maxSize: 100
  maxAge: 7
  maxBackups: 30
  compress: true

# 调试日志配置
debugLog:
  enabled: false
  directory: "debug_log"
  maxSize: 100
  maxBackups: 10
  compress: true

# 批量设备配置
batchConfig:
  enabled: false
  startPort: 8000
  endPort: 8000
  template:
    name: "ONVIF Device {{.Index}}"
    ip: "0.0.0.0"
    username: "admin"
    password: "admin"
    authEnabled: true
    streamEnabled: true
    eventEnabled: true
    discoveryEnabled: true
    keepaliveTimeout: 300
    streamConfig:
      maxConcurrent: 10
    eventConfig:
      subscriptionDuration: 300
      pushInterval: 5
      events:
        - name: MotionAlarm
          description: Motion detected
          rate: 10
          active: true
    channels:
      - id: channel-1
        name: Channel 1
        videoURL: ./videos/sample.mp4

devices:
  - id: device-1
    name: ONVIF Device 1
    ip: 0.0.0.0
    port: 8000
    username: admin
    password: admin
    authEnabled: true
    streamEnabled: true
    eventEnabled: true
    discoveryEnabled: true
    keepaliveTimeout: 300
    streamConfig:
      maxConcurrent: 10
    eventConfig:
      subscriptionDuration: 300
      pushInterval: 5
      events:
        - name: MotionAlarm
          description: Motion detected
          rate: 10
          active: true
        - name: VideoLoss
          description: Video signal loss
          rate: 30
          active: false
    channels:
      - id: channel-1
        name: Channel 1
        videoURL: ./videos/sample.mp4

  - id: device-2
    name: ONVIF Device 2
    ip: 0.0.0.0
    port: 8001
    username: admin
    password: admin
    authEnabled: true
    streamEnabled: true
    eventEnabled: true
    discoveryEnabled: true
    keepaliveTimeout: 300
    streamConfig:
      maxConcurrent: 10
    eventConfig:
      subscriptionDuration: 300
      pushInterval: 5
      events:
        - name: MotionAlarm
          description: Motion detected
          rate: 15
          active: true
    channels:
      - id: channel-1
        name: Channel 1
        videoURL: ./videos/sample.mp4
      - id: channel-2
        name: Channel 2
        videoURL: ./videos/sample2.mp4

6. 使用说明

6.1 调试页面访问

运行程序后,可通过浏览器访问以下地址查看调试页面:

http://localhost:8081

6.2 使用客户端测试工具

# 获取设备信息
go run cmd/client.go -url http://localhost:8000/onvif/device_service -command info

# 获取服务列表
go run cmd/client.go -url http://localhost:8000/onvif/device_service -command services

# 获取媒体配置文件
go run cmd/client.go -url http://localhost:8000/onvif/device_service -command profiles

# 获取流URI
go run cmd/client.go -url http://localhost:8000/onvif/device_service -command streamuri -profile profile_1

# 发现设备
go run cmd/client.go -command discover

7. 构建和部署

7.1 跨平台构建

# Linux/macOS
./scripts/build.sh

# Windows
scripts\build.bat

构建产物将位于 dist 目录中,包含以下文件:

  • onvif-simulator:主程序
  • onvif-client:客户端工具
  • config.yaml:配置文件
  • run.sh/run.bat:运行脚本

7.2 一键运行

# Linux/macOS
./scripts/run.sh

# Windows
scripts\run.bat

8. 项目结构

.
├── client/            # ONVIF客户端实现
├── cmd/               # 命令行工具
│   ├── main.go        # 主程序入口
│   ├── client.go      # 客户端工具入口
│   ├── perf_test.go   # 性能测试
│   └── config/        # 配置文件
├── config/           # 配置相关(已迁移到cmd/config)
├── log/              # 日志模块
├── onvif/            # ONVIF协议实现
│   ├── device.go      # 设备服务
│   ├── event.go       # 事件服务
│   └── media.go       # 媒体服务
├── scripts/          # 构建和运行脚本
│   ├── build.sh       # Linux/macOS构建脚本
│   ├── build.bat     # Windows构建脚本
│   ├── run.sh         # Linux/macOS运行脚本
│   └── run.bat        # Windows运行脚本
├── simulator/        # 设备模拟实现
│   ├── device.go      # 模拟设备
│   └── manager.go     # 设备管理器
├── web/              # Web调试页面
│   ├── server.go      # Web服务器
│   └── templates/     # HTML模板
├── videos/           # 视频样例(可选)
├── go.mod            # Go模块文件
├── go.sum            # Go依赖校验文件
└── README.md         # 项目说明文档

9. 开发指南

9.1 环境要求

  • Go 1.16+
  • Git

9.2 安装依赖

go mod tidy

9.3 运行测试

# 运行所有测试
go test ./...

# 运行特定包的测试
go test ./onvif

9.4 代码风格

  • 代码缩进使用4个空格
  • 函数和变量命名使用驼峰命名法
  • 每个函数和结构体都要添加详细的中文注释

10. 许可证

本项目采用MIT许可证,详情请见LICENSE文件。

About

onvif设备模拟器

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors