ipc

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Code generated by godddx, DO AVOID EDIT.

Index

Constants

View Source
const (
	TypeGB28181 = "GB28181"
	TypeOnvif   = "ONVIF"
	TypeRTSP    = "RTSP"
	TypeRTMP    = "RTMP"
)

Variables

This section is empty.

Functions

func GenerateChannelID

func GenerateChannelID(c *Channel, uni uniqueid.Core) string

GenerateChannelID 根据通道类型生成唯一 ID

func GenerateDID

func GenerateDID(d *Device, uni uniqueid.Core) string

func GetType

func GetType(stream string) string

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

为协议适配,提供协议会用到的功能

func NewAdapter

func NewAdapter(store Storer, uni uniqueid.Core) Adapter

func (Adapter) Edit

func (g Adapter) Edit(deviceID string, changeFn func(*Device)) error

func (Adapter) EditPlaying

func (g Adapter) EditPlaying(ctx context.Context, deviceID, channelID string, playing bool) error

func (Adapter) EditPlayingByID

func (g Adapter) EditPlayingByID(ctx context.Context, id string, playing bool) error

func (Adapter) FindDevices

func (g Adapter) FindDevices(ctx context.Context) ([]*Device, error)

FindDevices 获取所有设备

func (Adapter) GetChannel

func (g Adapter) GetChannel(ctx context.Context, id string) (*Channel, error)

func (Adapter) GetDevice

func (g Adapter) GetDevice(ctx context.Context, id string) (*Device, error)

func (Adapter) GetDeviceByDeviceID

func (g Adapter) GetDeviceByDeviceID(gbDeviceID string) (*Device, error)

func (Adapter) Logout

func (g Adapter) Logout(deviceID string, changeFn func(*Device)) error

func (Adapter) SaveChannels

func (g Adapter) SaveChannels(channels []*Channel) error

SaveChannels 保存通道列表(增量更新 + 删除多余通道)

策略说明: 1. 批量查询现有通道(减少数据库查询) 2. 对比更新:存在则更新,不存在则新增 3. 删除多余:不在上报列表中的通道标记为离线或删除 4. 使用事务保证数据一致性

func (Adapter) Store

func (g Adapter) Store() Storer

type AddChannelInput

type AddChannelInput struct {
	Type       string `json:"type"`        // 通道类型 (RTMP/RTSP)
	Name       string `json:"name"`        // 通道名称
	DeviceID   string `json:"device_id"`   // 可选,关联的父设备 ID
	DeviceName string `json:"device_name"` // 可选,device_id 不存在时用于创建新设备

	// RTMP/RTSP 配置
	App    string       `json:"app"`    // 应用名
	Stream string       `json:"stream"` // 流 ID
	Config StreamConfig `json:"config"` // 流配置
}

AddChannelInput 添加通道请求参数,支持 RTMP/RTSP 类型

type AddDeviceInput

type AddDeviceInput struct {
	DeviceID string `json:"device_id"` // 20 位国标编号
	// onvif
	Username string `json:"username"` // 用户名
	IP       string `json:"ip"`       // ip
	Port     int    `json:"port"`     // port
	// 通用
	Name     string `json:"name"`     // 设备名称
	Password string `json:"password"` // 注册密码

	Type string `json:"type"` // 设备类型(ONVIF/GB28181)

}

目前仅应用于 onvif 添加

type AddZoneInput

type AddZoneInput struct {
	Name        string    `json:"name"`        // 区域名称
	Coordinates []float32 `json:"coordinates"` // 坐标
	Color       string    `json:"color"`       // 颜色,支持 hex 颜色值,如 #FF0000
	Labels      []string  `json:"labels"`      // 标签
	ChannelID   string    `json:"-"`           // 通道 id
}

type Channel

type Channel struct {
	ID        string    `gorm:"primaryKey" json:"id"`
	DID       string    `gorm:"column:did;index;notNull;default:'';comment:父级 ID" json:"did"`
	DeviceID  string    `gorm:"column:device_id;index;notNull;default:'';comment:国标编码" json:"device_id"`   // 国标编码
	ChannelID string    `gorm:"column:channel_id;index;notNull;default:'';comment:国标编码" json:"channel_id"` // 国标编码
	Name      string    `gorm:"column:name;notNull;default:'';comment:通道名称" json:"name"`                   // 通道名称
	PTZType   int       `gorm:"column:ptztype;notNull;default:0;comment:云台类型" json:"ptztype"`              // 云台类型
	IsOnline  bool      `gorm:"column:is_online;notNull;default:FALSE;comment:是否在线" json:"is_online"`      // 是否在线
	IsPlaying bool      `gorm:"column:is_playing;notNull;default:FALSE;comment:是否播放中" json:"is_playing"`   // 是否播放中
	Ext       DeviceExt `gorm:"column:ext;notNull;default:'{}';type:jsonb" json:"ext"`
	CreatedAt orm.Time  `gorm:"column:created_at;notNull;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
	UpdatedAt orm.Time  `gorm:"column:updated_at;notNull;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
	Type      string    `gorm:"column:type;notNull;default:'';comment:通道类型" json:"type"`                            // 通道类型,继承父级设备类型

	// RTMP/RTSP 流配置字段
	App    string       `gorm:"column:app;index;notNull;default:'';comment:应用名" json:"app"`        // 应用名 (RTMP/RTSP)
	Stream string       `gorm:"column:stream;index;notNull;default:'';comment:流 ID" json:"stream"` // 流 ID (RTMP/RTSP)
	Config StreamConfig `gorm:"column:config;notNull;default:'{}';type:jsonb" json:"config"`       // 流配置 (RTMP/RTSP)
}

Channel domain model

func (*Channel) GetChannelID

func (c *Channel) GetChannelID() string

func (*Channel) GetGB28181DeviceID

func (c *Channel) GetGB28181DeviceID() string

func (*Channel) GetID

func (c *Channel) GetID() string

实现 port.Channel 接口

func (*Channel) IsGB28181

func (c *Channel) IsGB28181() bool

func (*Channel) IsOnvif

func (c *Channel) IsOnvif() bool

func (*Channel) IsRTMP added in v1.3.0

func (c *Channel) IsRTMP() bool

func (*Channel) IsRTSP added in v1.3.0

func (c *Channel) IsRTSP() bool

func (*Channel) TableName

func (*Channel) TableName() string

TableName database table name

type ChannelStorer

type ChannelStorer interface {
	Find(context.Context, *[]*Channel, orm.Pager, ...orm.QueryOption) (int64, error)
	Get(context.Context, *Channel, ...orm.QueryOption) error
	Add(context.Context, *Channel) error
	Edit(context.Context, *Channel, func(*Channel) error, ...orm.QueryOption) error
	Del(context.Context, *Channel, ...orm.QueryOption) error

	BatchEdit(context.Context, string, any, ...orm.QueryOption) error // 批量更新一个字段
	Session(ctx context.Context, changeFns ...func(*gorm.DB) error) error
}

ChannelStorer Instantiation interface

type Core

type Core struct {
	// contains filtered or unexported fields
}

Core business domain

func NewCore

func NewCore(store Storer, uni uniqueid.Core, protocols map[string]Protocoler) Core

NewCore create business domain

func (*Core) AddChannel

func (c *Core) AddChannel(ctx context.Context, in *AddChannelInput) (*Channel, error)

AddChannel 添加 RTMP/RTSP 通道,支持自动创建虚拟设备

func (Core) AddDevice

func (c Core) AddDevice(ctx context.Context, in *AddDeviceInput) (*Device, error)

AddDevice Insert into database

func (*Core) AddZone

func (c *Core) AddZone(ctx context.Context, in *AddZoneInput, channelID string) (*Zone, error)

func (*Core) BatchOfflineRTMP added in v1.3.0

func (c *Core) BatchOfflineRTMP(ctx context.Context) error

func (*Core) DelChannel

func (c *Core) DelChannel(ctx context.Context, id string) (*Channel, error)

DelChannel Delete object

func (Core) DelDevice

func (c Core) DelDevice(ctx context.Context, id string) (*Device, error)

DelDevice Delete object

func (*Core) EditChannel

func (c *Core) EditChannel(ctx context.Context, in *EditChannelInput, id string) (*Channel, error)

EditChannel Update object information

func (*Core) EditChannelConfig added in v1.3.0

func (c *Core) EditChannelConfig(ctx context.Context, id string, fn func(*StreamConfig)) (*Channel, error)

EditChannelConfig 更新通道的流配置(用于 Hook 回调更新状态)

func (*Core) EditChannelConfigAndOnline added in v1.3.0

func (c *Core) EditChannelConfigAndOnline(ctx context.Context, id string, isOnline bool, fn func(*StreamConfig)) (*Channel, error)

EditChannelConfigAndOnline 更新通道的流配置和在线状态

func (*Core) EditChannelConfigAndOnlineByAppStream added in v1.3.0

func (c *Core) EditChannelConfigAndOnlineByAppStream(ctx context.Context, app, stream string, isOnline bool, fn func(*StreamConfig)) (*Channel, error)

EditChannelConfigAndOnlineByAppStream 通过 app+stream 更新通道配置和在线状态

func (*Core) EditChannelConfigByAppStream added in v1.3.0

func (c *Core) EditChannelConfigByAppStream(ctx context.Context, app, stream string, fn func(*StreamConfig)) (*Channel, error)

EditChannelConfigByAppStream 通过 app+stream 更新通道配置

func (Core) EditDevice

func (c Core) EditDevice(ctx context.Context, in *EditDeviceInput, id string) (*Device, error)

EditDevice Update object information

func (*Core) FindChannel

func (c *Core) FindChannel(ctx context.Context, in *FindChannelInput) ([]*Channel, int64, error)

FindChannel Paginated search

func (Core) FindChannelsForDevice

func (c Core) FindChannelsForDevice(ctx context.Context, in *FindDeviceInput) ([]*Device, int64, error)

func (Core) FindDevice

func (c Core) FindDevice(ctx context.Context, in *FindDeviceInput) ([]*Device, int64, error)

FindDevice Paginated search

func (*Core) GetChannel

func (c *Core) GetChannel(ctx context.Context, id string) (*Channel, error)

GetChannel Query a single object

func (*Core) GetChannelByAppStream added in v1.3.0

func (c *Core) GetChannelByAppStream(ctx context.Context, app, stream string) (*Channel, error)

GetChannelByAppStream 通过 app 和 stream 获取通道

func (Core) GetDevice

func (c Core) GetDevice(ctx context.Context, id string) (*Device, error)

GetDevice Query a single object

func (Core) GetDeviceByDeviceID

func (c Core) GetDeviceByDeviceID(ctx context.Context, deviceID string) (*Device, error)

func (*Core) GetProtocol

func (c *Core) GetProtocol(atype string) Protocoler

func (*Core) GetZones

func (c *Core) GetZones(ctx context.Context, channelID string) ([]Zone, error)

func (Core) QueryCatalog

func (c Core) QueryCatalog(ctx context.Context, deviceID string) error

func (*Core) SetAIEnabled

func (c *Core) SetAIEnabled(ctx context.Context, channelID string, enabled bool) (*Channel, error)

SetAIEnabled 设置通道的 AI 检测开关状态,同时返回更新后的完整通道信息供调用方使用

type Device

type Device struct {
	ID   string `gorm:"primaryKey" json:"id"`
	Type string `gorm:"column:type;notNull;default:'';comment:设备类型(onvif)" json:"type"` // 设备类型(onvif/gb28181)

	// 自定义 id 可以简写为 did,国标 device_id 应简写成 gbid,而不是 device_id
	// 起这个名义会难以区分,但为了兼容已经部署的旧数据库...保留
	DeviceID string `gorm:"column:device_id;notNull;uniqueIndex;default:'';comment:20 位国标编号" json:"device_id"` // 20 位国标编号

	Name         string    `gorm:"column:name;notNull;default:'';comment:设备名称" json:"name"`                                                    // 设备名称
	Transport    string    `gorm:"column:transport;notNull;default:'';comment:传输协议(tcp/udp)" json:"transport"`                                 // 传输协议(TCP/UDP)
	StreamMode   int8      `gorm:"column:stream_mode;notNull;default:1;comment:数据传输模式(0:UDP; 1:TCP_PASSIVE; 2:TCP_ACTIVE)" json:"stream_mode"` // 数据传输模式
	IP           string    `gorm:"column:ip;notNull;default:''" json:"ip"`
	Port         int       `gorm:"column:port;notNull;default:0" json:"port"`
	IsOnline     bool      `gorm:"column:is_online;notNull;default:FALSE" json:"is_online"`
	RegisteredAt orm.Time  `gorm:"column:registered_at;notNull;default:CURRENT_TIMESTAMP;comment:注册时间" json:"registered_at"` // 注册时间
	KeepaliveAt  orm.Time  `gorm:"column:keepalive_at;notNull;default:CURRENT_TIMESTAMP;comment:心跳时间" json:"keepalive_at"`   // 心跳时间
	Keepalives   int       `gorm:"column:keepalives;notNull;default:0;comment:心跳间隔" json:"keepalives"`                       // 心跳间隔
	Expires      int       `gorm:"column:expires;notNull;default:0;comment:注册有效期" json:"expires"`                            // 注册有效期
	Channels     int       `gorm:"column:channels;notNull;default:0;comment:通道数量" json:"channels"`                           // 通道数量
	CreatedAt    orm.Time  `gorm:"column:created_at;notNull;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"`       // 创建时间
	UpdatedAt    orm.Time  `gorm:"column:updated_at;notNull;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"`       // 更新时间
	Password     string    `gorm:"column:password;notNull;default:'';comment:注册密码" json:"password"`
	Address      string    `gorm:"column:address;notNull;default:'';comment:设备网络地址" json:"address"`
	Ext          DeviceExt `gorm:"column:ext;notNull;default:'{}';type:jsonb;comment:设备属性" json:"ext"` // 设备属性
	Username     string    `gorm:"column:username;notNull;default:'';comment:用户名" json:"username"`

	Children []*Channel `gorm:"-" json:"children,omitzero"`
}

Device domain model

func (Device) Check

func (d Device) Check() error

func (*Device) GetGB28181DeviceID

func (d *Device) GetGB28181DeviceID() string

func (*Device) GetID

func (d *Device) GetID() string

实现 port.Device 接口

func (*Device) GetIP

func (d *Device) GetIP() string

func (*Device) GetPassword

func (d *Device) GetPassword() string

func (*Device) GetPort

func (d *Device) GetPort() int

func (*Device) GetType

func (d *Device) GetType() string

func (*Device) GetUsername

func (d *Device) GetUsername() string

func (*Device) IsGB28181

func (d *Device) IsGB28181() bool

func (*Device) IsOnvif

func (d *Device) IsOnvif() bool

func (*Device) NetworkAddress

func (d *Device) NetworkAddress() string

func (*Device) TableName

func (*Device) TableName() string

TableName database table name

type DeviceExt

type DeviceExt struct {
	Manufacturer string `json:"manufacturer"` // 生产厂商
	Model        string `json:"model"`        // 型号
	Firmware     string `json:"firmware"`     // 固件版本
	Name         string `json:"name"`         // 设备名
	GBVersion    string `json:"gb_version"`   // GB版本
	Zones        []Zone `json:"zones"`        // 区域
	EnabledAI    bool   `json:"enabled_ai"`   // 是否启用 AI
}

DeviceExt domain model

func (*DeviceExt) Scan

func (i *DeviceExt) Scan(input any) error

Scan implements orm.Scaner.

func (DeviceExt) Value

func (i DeviceExt) Value() (driver.Value, error)

type DeviceStorer

type DeviceStorer interface {
	Find(context.Context, *[]*Device, orm.Pager, ...orm.QueryOption) (int64, error)
	Get(context.Context, *Device, ...orm.QueryOption) error
	Add(context.Context, *Device) error
	Edit(context.Context, *Device, func(*Device) error, ...orm.QueryOption) error
	Del(context.Context, *Device, ...orm.QueryOption) error

	Session(ctx context.Context, changeFns ...func(*gorm.DB) error) error
}

DeviceStorer Instantiation interface

type EditChannelInput

type EditChannelInput struct {
	DeviceID string    `json:"device_id"` // 国标编码
	Name     string    `json:"name"`      // 通道名称
	PTZType  int       `json:"ptztype"`   // 云台类型
	IsOnline bool      `json:"is_online"` // 是否在线
	Ext      DeviceExt `json:"ext"`

	// RTMP/RTSP 配置
	App    string       `json:"app"`    // 应用名
	Stream string       `json:"stream"` // 流 ID
	Config StreamConfig `json:"config"` // 流配置
}

type EditDeviceInput

type EditDeviceInput struct {
	DeviceID   string `json:"device_id"`   // 20 位国标编号
	Name       string `json:"name"`        // 设备名称
	Password   string `json:"password"`    // 注册密码
	StreamMode int    `json:"stream_mode"` // 数据传输模式

	Username string `json:"username"` // 用户名
	IP       string `json:"ip"`       // ip
	Port     int    `json:"port"`     // port

}

type FindChannelInput

type FindChannelInput struct {
	web.PagerFilter
	DID      string `form:"did"`       // 设备 id
	DeviceID string `form:"device_id"` // 国标编码
	Key      string `form:"key"`       // 名称/国标编码 模糊搜索,id 精确搜索
	IsOnline string `form:"is_online"` // 是否在线
	Type     string `form:"type"`      // 通道类型 (GB28181/ONVIF/RTMP/RTSP)
	App      string `form:"app"`       // 应用名
	Stream   string `form:"stream"`    // 流 ID
}

type FindDeviceInput

type FindDeviceInput struct {
	web.PagerFilter
	Key string `form:"key"`
}

type Hooker

type Hooker interface {
	OnStreamNotFound(ctx context.Context, app, stream string) error
	OnStreamChanged(ctx context.Context, stream string) error
}

type PlayResponse

type PlayResponse struct {
	SSRC   string // GB28181 SSRC
	Stream string // 流 ID
	RTSP   string // RTSP 地址 (ONVIF)
}

PlayResponse 播放响应

type Protocoler

type Protocoler interface {
	// ValidateDevice 验证设备连接(添加设备前调用)
	// 可以修改设备信息(如从 ONVIF 获取的固件版本等)
	ValidateDevice(ctx context.Context, device *Device) error

	// InitDevice 初始化设备连接(添加设备后调用)
	// 例如: GB28181 不需要主动初始化,ONVIF 需要查询 Profiles 作为通道
	InitDevice(ctx context.Context, device *Device) error

	// QueryCatalog 查询设备目录/通道
	QueryCatalog(ctx context.Context, device *Device) error

	// StartPlay 开始播放
	StartPlay(ctx context.Context, device *Device, channel *Channel) (*PlayResponse, error)

	// StopPlay 停止播放
	StopPlay(ctx context.Context, device *Device, channel *Channel) error

	DeleteDevice(ctx context.Context, device *Device) error

	Hooker
}

Protocoler 协议抽象接口(端口)

设计原则: 1. 接口在 ipc 包内定义,避免循环依赖 2. 接口方法直接使用领域模型 (*Device, *Channel) 3. 适配器实现此接口,可以直接依赖和修改领域模型 4. 符合依赖倒置原则 (DIP):

  • ipc (高层) 依赖 Protocoler 接口
  • adapter (低层) 实现 Protocoler 接口
  • adapter (低层) 依赖 ipc.Device (高层) ✅ 合理

这就是依赖反转!

type Storer

type Storer interface {
	Device() DeviceStorer
	Channel() ChannelStorer
}

Storer data persistence

type StreamConfig added in v1.3.0

type StreamConfig struct {
	// RTMP 推流配置
	IsAuthDisabled bool      `json:"is_auth_disabled"`    // 是否禁用推流鉴权
	Session        string    `json:"session,omitempty"`   // 推流鉴权参数(由推流客户端携带)
	PushedAt       *orm.Time `json:"pushed_at"`           // 最后推流时间
	StoppedAt      *orm.Time `json:"stopped_at"`          // 最后停止时间
	MediaServerID  string    `json:"media_server_id"`     // 媒体服务器 ID
	PushAddr       string    `json:"push_addr,omitempty"` // 推流地址(动态生成,不持久化)

	// RTSP 拉流配置
	SourceURL                 string `json:"source_url"`                   // 原始 URL
	Transport                 int    `json:"transport"`                    // 拉流方式 (0:tcp, 1:udp)
	TimeoutS                  int    `json:"timeout_s"`                    // 超时时间
	EnabledAudio              bool   `json:"enabled_audio"`                // 是否启用音频
	EnabledRemoveNoneReader   bool   `json:"enabled_remove_none_reader"`   // 无人观看时删除
	EnabledDisabledNoneReader bool   `json:"enabled_disabled_none_reader"` // 无人观看时禁用
	StreamKey                 string `json:"stream_key"`                   // ZLM 返回的 key
	Enabled                   bool   `json:"enabled"`                      // 是否启用
}

StreamConfig 流配置,用于 RTMP 推流和 RTSP 拉流代理

func (*StreamConfig) Scan added in v1.3.0

func (s *StreamConfig) Scan(input any) error

Scan implements orm.Scanner

func (StreamConfig) Value added in v1.3.0

func (s StreamConfig) Value() (driver.Value, error)

Value implements driver.Valuer

type Zone

type Zone struct {
	Name        string    `json:"name"`        // 区域名称
	Coordinates []float32 `json:"coordinates"` // 坐标
	Color       string    `json:"color"`       // 颜色,支持 hex 颜色值,如 #FF0000
	Labels      []string  `json:"labels"`      // 标签
}

Directories

Path Synopsis
store
ipcdb
Code generated by godddx, DO AVOID EDIT.
Code generated by godddx, DO AVOID EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL