This commit is contained in:
banshan
2023-01-27 17:33:39 +08:00
parent 5460988430
commit e08a1a6a3d
4 changed files with 597 additions and 191 deletions

136
main.go
View File

@@ -1,66 +1,70 @@
package onvif
import (
"time"
. "github.com/Monibuca/engine/v3"
)
var config struct {
DiscoverInterval int `toml:"DiscoverInterval"`
Interfaces []struct {
InterfaceName string `toml:"InterfaceName"`
Username string `toml:"Username"`
Password string `toml:"Password"`
} `toml:"interfaces"`
Devices []struct {
IP string `toml:"Ip"`
Username string `toml:"Username"`
Password string `toml:"Password"`
} `toml:"devices"`
}
var authCfg = &AuthConfig{
Interfaces: make(map[string]deviceAuth),
Devices: make(map[string]deviceAuth),
}
var deviceList = &DeviceList{Data: make(map[string]map[string]*DeviceStatus)}
func init() {
pconfig := PluginConfig{
Name: "ONVIF",
Config: &config,
}
pconfig.Install(runPlugin)
}
func runPlugin() {
preprocessAuth(authCfg)
if config.DiscoverInterval == 0 {
config.DiscoverInterval = 30
}
t := time.NewTicker(time.Duration(config.DiscoverInterval) * time.Second)
go func() {
for range t.C {
deviceList.discoveryDevice()
deviceList.pullStream()
}
}()
}
func preprocessAuth(c *AuthConfig) {
for _, i := range config.Interfaces {
c.Interfaces[i.InterfaceName] = deviceAuth{
Username: i.Username,
Password: i.Password,
}
}
for _, d := range config.Devices {
c.Devices[d.IP] = deviceAuth{
Username: d.Username,
Password: d.Password,
}
}
}
package onvif
import (
. "m7s.live/engine/v4"
"time"
)
type OnvifConfig struct {
DiscoverInterval int
Interfaces []struct {
InterfaceName string
Username string
Password string
}
Devices []struct {
IP string
Username string
Password string
}
}
func (o *OnvifConfig) init() {
preprocessAuth(authCfg)
if o.DiscoverInterval == 0 {
o.DiscoverInterval = 30
}
go func() {
deviceList.discoveryDevice()
deviceList.pullStream()
}()
t := time.NewTicker(time.Duration(o.DiscoverInterval) * time.Second)
go func() {
for range t.C {
deviceList.discoveryDevice()
deviceList.pullStream()
}
}()
}
func (o *OnvifConfig) OnEvent(event any) {
switch event.(type) {
case FirstConfig:
o.init()
}
}
var authCfg = &AuthConfig{
Interfaces: make(map[string]deviceAuth),
Devices: make(map[string]deviceAuth),
}
var deviceList = &DeviceList{Data: make(map[string]map[string]*DeviceStatus)}
var conf = &OnvifConfig{}
var plugin = InstallPlugin(conf)
func preprocessAuth(c *AuthConfig) {
for _, i := range conf.Interfaces {
c.Interfaces[i.InterfaceName] = deviceAuth{
Username: i.Username,
Password: i.Password,
}
}
for _, d := range conf.Devices {
c.Devices[d.IP] = deviceAuth{
Username: d.Username,
Password: d.Password,
}
}
}